diff --git a/wsl-oobe.sh b/wsl-oobe.sh index 3fbc38f..5cb4a69 100644 --- a/wsl-oobe.sh +++ b/wsl-oobe.sh @@ -11,6 +11,42 @@ set -ueo pipefail DEFAULT_USER_ID=1000 +function create_user() { + local rc=0 + + while true; do + read -r -p "Create a default Unix user account: " username + + # Create the user. + rc=$( + set +e + /usr/sbin/useradd -m -G wheel --uid "$DEFAULT_USER_ID" "$username" > /dev/null + echo $? + ) + + case $rc in + # 3: invalid argument to option + # 19: Bad login name (since Fedora 42) + 3 | 19) + echo "Invalid username. A valid username must start with a letter or underscore, and can contain letters, digits, underscores, dots, dashes and a dollar sign at the end." + continue + ;; + # 9: username or group name already in use + 9) + echo "User \"$username\" already exists" + continue + ;; + 0) + break + ;; + *) + echo "Unexpected error code from useradd: $rc" + break + ;; + esac + done +} + # When `cloud-init` is enabled it might take care of user creation and other bits, depending on its # configuration contained within the WSL image; or the WSL configuration as provided by the host. if systemctl is-enabled cloud-init.service > /dev/null ; then @@ -35,19 +71,15 @@ if systemctl is-enabled cloud-init.service > /dev/null ; then exit 0 fi -echo 'Please create a default user account. The username does not need to match your Windows username.' -echo 'For more information visit: https://aka.ms/wslusers' - if getent passwd $DEFAULT_USER_ID > /dev/null ; then echo 'User account already exists, skipping creation' exit 0 fi -# Prompt from the username -read -r -p 'Enter new UNIX username: ' username +echo 'Please create a default user account. The username does not need to match your Windows username.' +echo 'For more information visit: https://aka.ms/wslusers' -# Create the user -/usr/sbin/useradd -m -G wheel --uid $DEFAULT_USER_ID "$username" +create_user cat > /etc/sudoers.d/wsluser << EOF # Ensure the WSL initial user can use sudo without a password. diff --git a/wsl-setup b/wsl-setup index 41bb0b0..698ca18 100755 --- a/wsl-setup +++ b/wsl-setup @@ -73,6 +73,10 @@ case $key in exit 2 fi ;; +--help | -h ) + usage + exit 0 +;; * ) echo echo "ERROR BAD OPTION: $key" diff --git a/wsl-setup-tmpfiles.conf b/wsl-setup-tmpfiles.conf index 23ba7b5..a9c3d1f 100644 --- a/wsl-setup-tmpfiles.conf +++ b/wsl-setup-tmpfiles.conf @@ -1 +1 @@ -L+ /tmp/.X11-unix - - - - /mnt/wslg/.X11-unix +L /tmp/.X11-unix - - - - /mnt/wslg/.X11-unix diff --git a/wsl-setup.spec b/wsl-setup.spec index 6e895dd..4c637a0 100644 --- a/wsl-setup.spec +++ b/wsl-setup.spec @@ -67,7 +67,7 @@ install -Dpm0644 %{SOURCE5} %{buildroot}%{_unitdir}/systemd-firstboot.service.d/ %files %{_bindir}/wsl-setup %config(noreplace) %{_sysconfdir}/wsl.conf -%ghost %{_prefix}/lib/wsl-distribution.conf +%ghost %attr(644, root, root) %{_prefix}/lib/wsl-distribution.conf %{_sysconfdir}/wsl-distribution.conf %dir %{_datarootdir}/wsl-setup/ %{_datarootdir}/wsl-setup/wsl-distribution.conf.template