microsoft / WSL-DistroLauncher

Sample/reference launcher app for WSL distro Microsoft Store packages.
MIT License
1.67k stars 517 forks source link

Unattended Install #90

Closed kousu closed 3 years ago

kousu commented 3 years ago

I want to be able to do an unattended install as if from the Microsoft Store. There is install --root, which you document prominently:

ubuntu1804.exe install --root

and this is what is used by

https://github.com/microsoft/windows-dev-box-setup-scripts/blob/a0d80679847fc668bdc1706ae29b45da7a65772b/scripts/WSL.ps1#L10

but that team knows they should be installing with a regular user, but declined to explain fully how to actually to implement https://github.com/microsoft/windows-dev-box-setup-scripts/blob/d488050/README.md#how-to-run-the-scripts

If you are using WSL there's a followup step we recommend after running the setup script. When the script finishes you will only have a root user with a blank password. You should manually create a non-root user via $ sudo adduser [USERNAME] sudo with a non-blank password. Use this user going forward. For more info on WSL please refer to the documentation.

From https://github.com/microsoft/WSL/issues/3369 I learned that I make an almost fully-automatic user account by:

Ubuntu1804 install --root
Ubuntu1804 run apt update
Ubuntu1804 run apt upgrade -y

Ubuntu1804 run adduser -m user
Ubuntu1804 run 'echo user:password | chpasswd'
Ubuntu1804 run 'chsh -s /bin/bash user'

# change default user to 'user'
reg.exe query HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\ # to get the GUID of the container
reg.exe add 'HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{1422a095-2e43-4bf8-8108-d209cabe2a07}' /v DefaultUid /t REG_DWORD /d 1000 /f

But this is really fragile:

  1. It requires parsing out a GUID from the registry; and even if I did script that, it would probably break down as soon as a second distro was installed
  2. It just guesses that 'user' == UID 1000

I wish this section of code was just slightly different:

https://github.com/microsoft/WSL-DistroLauncher/blob/2ed9a9335fc89a688a5150c95eff4fbdbc830f25/DistroLauncher/DistroLauncher.cpp#L39-L53

or maybe this one:

https://github.com/microsoft/WSL-DistroLauncher/blob/2ed9a9335fc89a688a5150c95eff4fbdbc830f25/DistroLauncher/DistributionInfo.cpp#L12-L14

Could there be a --unattended or --default-user flag that does a clean unattended install? Or, a wsl set-user that would let me change the default user after initially installing with root?

kousu commented 3 years ago

Ah, I read the docs: there's

wsl -u root

which partially helps. If, say, you generalized --root to --user which worked so that, given --user someone, DistroLauncher runs useradd -m someone instead of using adduser then it won't prompt, and I can later use wsl -u root passwd someone to set the password, or echo 'someone:pass' | wsl -u root chpasswd to set it noninteractively.

kousu commented 3 years ago

..and now I just noticed ubuntu1804 config --default-user username. Which pretty much covers everything I want.

DistroLauncher could make this smoother maybe with more obvious flags, but this works fine:

<distro>.exe install --root

wsl -u root -d <distro> useradd -m "$username"
wsl -u root -d <distro> sh -c "echo "${username}:${password}" | chpasswd"
wsl -u root -d <distro> chsh -s /bin/bash "$username"
wsl -u root -d <distro> usermod -aG adm,cdrom,sudo,dip,plugdev "$username"

<distro>.exe config --default-user "$username"
kousu commented 3 years ago

Or, this is even better:

Given that you've grabbted the .appx link for a distro:

$distro = "ubuntu1804"
$distro_link = "https://aka.ms/wsl-ubuntu-1804"

$username = "ubuntu"
$password = "ubuntu"

Invoke-WebRequest -Uri $distro_link -OutFile ~/${distro}.appx -UseBasicParsing
Add-AppxPackage -Path ~/${distro}.appx

& $distro install --root

& $distro run useradd -m "$username"
& $distro run sh -c "echo "${username}:${password}" | chpasswd"
& $distro run chsh -s /bin/bash "$username"
& $distro run usermod -aG adm,cdrom,sudo,dip,plugdev "$username"

& $distro config --default-user "$username"