diff --git a/.github/fork-ci-tests.txt b/.github/fork-ci-tests.txt index 495cdceec99f6..55c2903cd3e5e 100644 --- a/.github/fork-ci-tests.txt +++ b/.github/fork-ci-tests.txt @@ -43,6 +43,7 @@ fork-ci/actions/pr-queue/pr_queue.test.py fork-ci/actions/pr-queue/tested-base/tested_base.test.py fork-kit/common/tests/machine_file.test.py fork-kit/desk/tests/install.test.py +fork-kit/desk/tests/windows.test.py fork-kit/golive/tests/check.test.py fork-kit/golive/tests/contract.test.py fork-kit/golive/tests/journal.test.py diff --git a/fork-kit/desk/README.md b/fork-kit/desk/README.md index 86a0fe756420e..7da0fa4d9985d 100644 --- a/fork-kit/desk/README.md +++ b/fork-kit/desk/README.md @@ -53,6 +53,37 @@ every wall of this OS (`fork-fleet-walls.py describe`), the agent's settings, th files against the machine file, the runner users (and names a user the kit made that a smaller `slots` no longer uses) and IPv4 forwarding. +## Windows (WSL2) + +[windows/start-desk.ps1](windows/start-desk.ps1) does the Windows side and then this kit inside one WSL2 distro. +Save the machine file on Windows, then in a normal (not administrator) Windows PowerShell window: + +```powershell +powershell -ExecutionPolicy Bypass -File start-desk.ps1 -Repo OWNER/REPO -Sha RELEASE_SHA -MachineFile machine.json +``` + +It prints the PC's facts and asks before it changes anything. Before the question it checks the machine file and +`-FleetConfig` exist and refuses a WSL 1 distro of that name (`wsl -l -v`; WSL 1 runs no systemd: +`wsl --set-version DISTRO 2` first). A distro of that name that the script did not make +is named in the question (its whole `/etc/wsl.conf` is replaced) and must be confirmed by typing its name. Then: the +distro (`-Distro`, default `fork-desk`, Ubuntu 24.04, installed with `--version 2`; a run stopped while installing it +leaves a mark in `%LOCALAPPDATA%\fork-desk`, so the next run asks no takeover question, and a broken one names +`wsl --unregister DISTRO`); `.wslconfig`'s `[wsl2]` memory cap (`-MemoryGB`, default +three quarters of the RAM, at most the RAM less 6 GB, never under 4 GB; one that leaves Windows under 2 GB is +refused), swap (`-SwapGB`, 4), NAT and DNS tunnelling, for every WSL distro (read and written as UTF-8 without a +BOM; a dated copy of the old file is kept, other keys and sections stay); the distro's own settings the WSL2 walls +ask for (`/etc/wsl.conf`: systemd on, no Windows drives, programs or PATH; its own `/etc/resolv.conf`; WSL's binfmt +handler masked); one `wsl --shutdown` when one of these changed or the running WSL does not have them yet (PID 1 not +systemd, interop live, Windows drives mounted, `wsl.conf` or `.wslconfig` newer than the running one, more memory +than the cap, not NAT, or a `.wslconfig` change whose shutdown is still owed: a mark in `%LOCALAPPDATA%\fork-desk`, +written before the change and removed after the shutdown, because WSL's uptime skips sleep), +so a run stopped before its shutdown restarts next time (every WSL distro and Docker Desktop stop); the packages the fleet needs; the +machine file and `-FleetConfig`, if given; this kit's bootstrap; a logon task that keeps the distro running while +the owner is logged in. Last, `desk-install check` and the Windows side's own PASS/FAIL lines (the `[wsl2]` keys); +any FAIL or error exits 1. Safe to run again. `-Uninstall` (needs no `-Repo` or `-Sha`) runs `desk-install uninstall` in the distro, stops and removes the logon +task and names the first `.wslconfig.bak-*` (the file before the first run); the distro and `.wslconfig` +stay (`wsl --unregister DISTRO` removes the distro). + ## Runs again, stops half-way, old or empty state - **Again:** files, settings and units that did not change are not rewritten, and the agent is not stopped or diff --git a/fork-kit/desk/tests/windows.test.py b/fork-kit/desk/tests/windows.test.py new file mode 100644 index 0000000000000..250005e009e9d --- /dev/null +++ b/fork-kit/desk/tests/windows.test.py @@ -0,0 +1,287 @@ +"""start-desk.ps1, without Windows: its in-distro part run on a fake root (twice) and read by the real WSL2 walls, +and the rules a script for Windows PowerShell 5.1 must keep. With pwsh on PATH, it is also parsed. + + python3 -I -B fork-kit/desk/tests/windows.test.py +""" +import importlib.machinery, importlib.util, os, re, shutil, subprocess, tempfile, time, unittest + +KIT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +REPO = os.path.dirname(os.path.dirname(KIT)) +PS1 = os.path.join(KIT, 'windows', 'start-desk.ps1') +WSL2 = os.path.join(REPO, '.github', 'scripts', 'fork-fleet-walls', 'wsl2') + + +def slurp(path): + with open(path) as f: + return f.read() + + +def put(path, s): + with open(path, 'w') as f: + f.write(s) + + +def text(): + with open(PS1, 'rb') as f: + return f.read() + + +def prepare(name='Prepare'): + m = re.search(r"\$" + name + r" = @'\r?\n(.*?)\r?\n'@", text().decode('ascii'), re.S) + return m.group(1) + + +def wslconfig_part(): + src = text().decode('ascii') + return src[src.index('# --- wslconfig ---'):src.index('# --- end wslconfig ---')] + + +def walls(root): + os.environ['FORK_FLEET_WALLS_ROOT'] = root + try: + loader = importlib.machinery.SourceFileLoader('wsl2_walls', WSL2) + mod = importlib.util.module_from_spec(importlib.util.spec_from_loader('wsl2_walls', loader)) + loader.exec_module(mod) + return mod + finally: + del os.environ['FORK_FLEET_WALLS_ROOT'] + + +class Prepare(unittest.TestCase): + def setUp(self): + self.root = os.path.realpath(tempfile.mkdtemp(prefix='desk-windows-test-')) + self.addCleanup(shutil.rmtree, self.root, True) + os.makedirs(self.root + '/etc') + os.makedirs(self.root + '/proc/sys/fs/binfmt_misc') + put(self.root + '/proc/sys/fs/binfmt_misc/status', 'enabled\n') + + def run_prepare(self): + # as InDistro sends it: LF only, then '# end' and the CR LF Windows PowerShell adds + r = subprocess.run(['bash', '-s'], input=prepare() + '\n# end\r\n', text=True, capture_output=True, + env={'PATH': '/usr/bin:/bin', 'ROOT': self.root}) + self.assertEqual(r.returncode, 0, r.stderr) + return r.stdout + + def test_the_wsl2_walls_pass_after_it_and_a_second_run_changes_nothing(self): + # as WSL leaves a new distro: interop on, resolv.conf a link into /mnt/wsl + put(self.root + '/etc/wsl.conf', '[boot]\nsystemd=true\n[user]\ndefault=someone\n') + os.symlink('/mnt/wsl/resolv.conf', self.root + '/etc/resolv.conf') + w = walls(self.root) + self.assertIsNotNone(w.interop_wall()) + self.assertIsNotNone(w.resolv_wall()) + out = self.run_prepare() + self.assertIn('changed: /etc/wsl.conf', out) + self.assertEqual(slurp(self.root + '/etc/resolv.conf'), 'nameserver 10.255.255.254\n') + backups = [n for n in os.listdir(self.root + '/etc') if n.startswith('wsl.conf.bak-')] + self.assertEqual(len(backups), 1) + w = walls(self.root) + for wall in (w.interop_wall, w.path_wall, w.resolv_wall): + self.assertIsNone(wall(), wall.__name__) + self.assertEqual(w.wsl_conf('boot', 'systemd'), 'true') + self.assertEqual(w.wsl_conf('automount', 'enabled'), 'false') + before = self.etc() + self.assertEqual(self.run_prepare(), '') + self.assertEqual(self.etc(), before) + + def etc(self): + out = {} + for dirpath, _, files in os.walk(self.root + '/etc'): + for n in files: + full = os.path.join(dirpath, n) + out[full] = os.readlink(full) if os.path.islink(full) else slurp(full) + return out + + def test_an_empty_distro(self): + self.assertIn('changed: /etc/resolv.conf', self.run_prepare()) + self.assertFalse([n for n in os.listdir(self.root + '/etc') if '.bak-' in n]) + self.assertIsNone(walls(self.root).interop_wall()) + + def live(self, env=None): + # a wslinfo that answers nat first on PATH: inside a WSL with mirrored networking the real one would answer + fake = os.path.join(self.root, 'nat-bin') + if not os.path.exists(fake): + os.makedirs(fake) + put(fake + '/wslinfo', '#!/bin/sh\necho nat\n') + os.chmod(fake + '/wslinfo', 0o755) + r = subprocess.run(['bash', '-s'], input=prepare('Live') + '\n# end\r\n', text=True, capture_output=True, + env={'PATH': fake + ':/usr/bin:/bin', 'ROOT': self.root, **(env or {})}) + self.assertEqual(r.returncode, 0, r.stderr) + return r.stdout + + def test_the_restart_decision_reads_the_live_state(self): + # a run stopped after it wrote wsl.conf, before its `wsl --shutdown`: the next run changes nothing, but the + # running distro still has the old settings, so it restarts + os.makedirs(self.root + '/proc/1') + put(self.root + '/proc/1/comm', 'systemd\n') + put(self.root + '/proc/meminfo', 'MemTotal: 8000000 kB\n') + self.run_prepare() + past = time.time() - 3600 + os.utime(self.root + '/proc/1', (past, past)) + self.assertIn('restart: /etc/wsl.conf is newer than the running distro', self.live()) + os.utime(self.root + '/etc/wsl.conf', (past - 60, past - 60)) + self.assertEqual(self.live({'WANT_KB': '8388608'}), '') + put(self.root + '/proc/sys/fs/binfmt_misc/WSLInterop', 'enabled\n') + put(self.root + '/proc/1/comm', 'init\n') + out = self.live({'WANT_KB': '4194304'}) + for why in ('PID 1 is not systemd', 'WSL interop is live (WSLInterop)', 'above the cap'): + self.assertIn(why, out) + os.unlink(self.root + '/proc/sys/fs/binfmt_misc/WSLInterop') + put(self.root + '/proc/1/comm', 'systemd\n') + fake = os.path.join(self.root, 'bin') + os.makedirs(fake) + put(fake + '/wslinfo', '#!/bin/sh\necho mirrored\n') + os.chmod(fake + '/wslinfo', 0o755) + self.assertIn('the networking mode is mirrored, not nat', self.live({'PATH': fake + ':/usr/bin:/bin'})) + + def test_a_mounted_windows_drive_restarts(self): + os.makedirs(self.root + '/proc/1') + put(self.root + '/proc/1/comm', 'systemd\n') + put(self.root + '/proc/mounts', 'none /usr/lib/wsl/drivers 9p ro 0 0\n/dev/sdc / ext4 rw 0 0\n') + self.assertEqual(self.live(), '') # WSL's own driver share is not a Windows drive + put(self.root + '/proc/mounts', 'C:\\134 /mnt/c 9p rw 0 0\n') + self.assertIn('restart: Windows drives are mounted under /mnt', self.live()) + + def test_a_resolv_conf_of_its_own_is_kept(self): + put(self.root + '/etc/resolv.conf', 'nameserver 192.0.2.1\n') + self.run_prepare() + self.assertEqual(slurp(self.root + '/etc/resolv.conf'), 'nameserver 192.0.2.1\n') + + +class Script(unittest.TestCase): + def test_windows_powershell_reads_it(self): + raw = text() + self.assertTrue(raw.isascii(), 'Windows PowerShell 5.1 reads a file without a BOM as ANSI: ASCII only') + self.assertNotIn(b'\t', raw) + src = raw.decode('ascii') + for a, b in (('{', '}'), ('(', ')'), ('[', ']')): + self.assertEqual(src.count(a), src.count(b), a) + if shutil.which('pwsh'): + # the path inside the command: words after -Command join the command, they never reach $args + path = PS1.replace("'", "''") + r = subprocess.run(['pwsh', '-NoProfile', '-NonInteractive', '-Command', + f"$e=$null; [System.Management.Automation.Language.Parser]::ParseFile('{path}', " + "[ref]$null, [ref]$e) | Out-Null; if ($e) { $e | Out-String; exit 1 }"], + capture_output=True, text=True, timeout=120, stdin=subprocess.DEVNULL) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + + def test_it_asks_before_it_changes_anything(self): + src = text().decode('ascii') + body = src[src.index('try {'):] + first_change = min(body.index(x) for x in ('wsl.exe --install', 'Merge-WslConfig $WslConfig', + 'Unregister-ScheduledTask', 'InDistro', 'Register-ScheduledTask', + 'wsl.exe --shutdown')) + self.assertIn("Read-Host", body[:first_change]) + + def test_a_distro_it_did_not_make_is_named_and_must_be_typed(self): + src = text().decode('ascii') + body = src[src.index('try {'):] + ask = body.index("if ($have -and -not $ours) {\n Write-Host") + self.assertLess(ask, body.index('wsl.exe --install')) + self.assertIn("-cne $Distro", body[ask:ask + 800]) + self.assertIn('/etc/wsl.conf is REPLACED', body[ask:ask + 800]) + self.assertIn('/etc/fork-kit/desk-distro', prepare()) # the mark of a distro it made or took over + + def test_wsl1_is_refused_before_anything_changes_and_a_new_distro_is_wsl2(self): + src = text().decode('ascii') + body = src[src.index('try {'):] + install = [x for x in body.splitlines() if 'wsl.exe --install' in x] + self.assertEqual(len(install), 1) + self.assertIn('--version 2', install[0]) + refuse = body.index('if ($have -and $version -ne 2) { throw') + for later in ("Read-Host 'Type yes", 'Merge-WslConfig $WslConfig', 'wsl.exe --shutdown', 'Mark $Made', + "InDistro \"the distro's settings\""): + self.assertLess(refuse, body.index(later), later) + self.assertLess(body.index('wsl.exe --install'), body.index('if ($version -ne 2) { throw')) + self.assertLess(body.index('if ($version -ne 2) { throw'), body.index('wsl.exe --shutdown')) + self.assertIn('WslVersion @(wsl.exe -l -v) $Distro', body) + self.assertNotIn('wsl.exe -l -q', body) + + def test_inputs_are_checked_before_the_question(self): + src = text().decode('ascii') + body = src[src.index('if (-not $MachineFile)'):] + ask = min(body.index("Read-Host 'Type yes"), body.index('Read-Host "Type the distro')) + self.assertLess(body.index('Test-Path -LiteralPath $MachineFile -PathType Leaf'), ask) + self.assertLess(body.index('Test-Path -LiteralPath $FleetConfig -PathType Leaf'), ask) + self.assertIn("-cmatch '^[0-9a-f]{40}$'", src.split('try {')[0]) # an uppercase -Sha is refused too + + def test_a_stopped_run_keeps_its_marks(self): + src = text().decode('ascii') + body = src[src.index('try {'):] + self.assertLess(body.index('Mark $Made'), body.index('wsl.exe --install')) + self.assertIn('Merge-WslConfig $WslConfig $want { Mark $Owed }', body) + self.assertIn('& $before', wslconfig_part()) + self.assertLess(wslconfig_part().index('& $before'), wslconfig_part().index('WriteAllLines')) + shutdown = body.index('Run \'wsl --shutdown\'') + self.assertLess(body.index('if (Test-Path -LiteralPath $Owed)'), shutdown) + self.assertGreater(body.index('Remove-Item -LiteralPath $Owed -ErrorAction'), shutdown) + self.assertIn('wsl --unregister $Distro (removes it and all in it)', body) + uninstall = body[body.index('if ($Uninstall)'):] + self.assertLess(uninstall.index('Stop-ScheduledTask'), uninstall.index('Unregister-ScheduledTask')) + self.assertIn('.wslconfig as it was before the first run', uninstall) + + @unittest.skipUnless(shutil.which('pwsh'), 'pwsh is not installed') + def test_wsl_version_is_read_from_the_list(self): + d = tempfile.mkdtemp(prefix='desk-wslversion-test-') + self.addCleanup(shutil.rmtree, d, True) + script = os.path.join(d, 'v.ps1') + with open(script, 'w', encoding='ascii') as f: + f.write(wslconfig_part() + '\n$l = @(" NAME STATE VERSION", "* Ubuntu Running 2", ' + '" fork-desk Stopped 1")\n"$(WslVersion $l fork-desk) $(WslVersion $l Ubuntu) ' + '$(WslVersion $l none)"\n') + r = subprocess.run(['pwsh', '-NoProfile', '-NonInteractive', '-File', script], capture_output=True, + text=True, timeout=120, stdin=subprocess.DEVNULL) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertEqual(r.stdout.strip(), '1 2 0') + + def test_wslconfig_is_utf8_without_a_bom_never_ascii(self): + src = text().decode('ascii') + self.assertNotRegex(src, r'(?i)-Encoding\s+ascii') + self.assertNotRegex(src, r'(?i)(Get|Set)-Content[^\n]*\$WslConfig') + self.assertIn('[Text.UTF8Encoding]::new($false)', wslconfig_part()) + + @unittest.skipUnless(shutil.which('pwsh'), 'pwsh is not installed') + def test_wslconfig_merge_keeps_every_other_letter_and_key(self): + d = tempfile.mkdtemp(prefix='desk-wslconfig-test-') + self.addCleanup(shutil.rmtree, d, True) + path = os.path.join(d, '.wslconfig') + old = '[wsl2]\nkernel=D:\\kernels\\Jos\u00e9\\bzImage\nmemory=2GB\n[experimental]\nsparseVhd=true\nmemory=9GB\n' + with open(path, 'w', encoding='utf-8', newline='\n') as f: + f.write(old) + script = os.path.join(d, 'merge.ps1') + with open(script, 'w', encoding='ascii') as f: + f.write(wslconfig_part() + '\n$want = [ordered]@{ memory = "6GB"; swap = "4GB" }\n' + '"changed=$(Merge-WslConfig $args[0] $want)"\n"again=$(Merge-WslConfig $args[0] $want)"\n' + '$k = Wsl2Keys (Read-WslConfig $args[0]); "memory=$($k[\'memory\'])"\n') + r = subprocess.run(['pwsh', '-NoProfile', '-NonInteractive', '-File', script, path], capture_output=True, + text=True, timeout=120, stdin=subprocess.DEVNULL) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertIn('changed=True', r.stdout) + self.assertIn('again=False', r.stdout) + self.assertIn('memory=6GB', r.stdout) # the [wsl2] one, not [experimental]'s + with open(path, 'rb') as f: + raw = f.read() + self.assertFalse(raw.startswith(b'\xef\xbb\xbf')) + new = raw.decode('utf-8').replace('\r\n', '\n') + self.assertIn('kernel=D:\\kernels\\Jos\u00e9\\bzImage\n', new) + self.assertIn('[wsl2]\nkernel=D:\\kernels\\Jos\u00e9\\bzImage\nmemory=6GB\nswap=4GB\n[experimental]', new) + self.assertIn('sparseVhd=true\nmemory=9GB', new) + backups = [n for n in os.listdir(d) if n.startswith('.wslconfig.bak-')] + self.assertEqual(len(backups), 1) + + def test_text_piped_into_the_distro_ends_in_a_comment(self): + src = text().decode('ascii') + self.assertIn('''function Script([string]$s) { ($s -replace "`r", '') + "`n# end" }''', src) + for line in src.splitlines(): + if '| wsl.exe' in line and 'Script ' not in line: + self.assertIn('$text |', line) # Into: data (JSON), where a trailing CR LF is only white space + self.assertNotIn('`"', src.split('try {')[0].split('$Prepare')[0]) # no quotes inside native arguments + + def test_the_kit_it_runs_is_this_one(self): + src = text().decode('ascii') + self.assertIn('/fork-kit/desk/bootstrap.sh', src) + self.assertTrue(os.path.isfile(os.path.join(KIT, 'bootstrap.sh'))) + self.assertIn('/opt/fork-fleet/desk-install check', src) + + +if __name__ == '__main__': + unittest.main() diff --git a/fork-kit/desk/windows/start-desk.ps1 b/fork-kit/desk/windows/start-desk.ps1 new file mode 100644 index 0000000000000..22630df49f0f8 --- /dev/null +++ b/fork-kit/desk/windows/start-desk.ps1 @@ -0,0 +1,267 @@ +# fork-kit/desk/windows/start-desk.ps1: a Windows PC as a fork fleet desk pool, through one WSL2 distro +# (fork-kit/desk/README.md, "Windows"). Run it in a normal Windows PowerShell window (not as administrator): +# powershell -ExecutionPolicy Bypass -File start-desk.ps1 -Repo OWNER/REPO -Sha RELEASE_SHA -MachineFile machine.json +# [-FleetConfig fork-fleet.json] [-Distro fork-desk] [-MemoryGB N] [-SwapGB 4] [-Uninstall] +# It prints this PC's facts and asks before it changes anything. Safe to run again: every step checks what is there. +# Steps: the distro (Ubuntu 24.04); .wslconfig's [wsl2] memory cap and NAT (a dated copy of the old file is kept; +# other keys stay); the distro's own settings (the WSL walls: no interop, no Windows PATH or drives, its own +# resolv.conf, systemd); one `wsl --shutdown`; the machine file; the desk kit (bootstrap.sh, in the distro); a logon +# task that keeps the distro running; then `desk-install check` and this side's checks. +param( + [ValidatePattern('^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$')][string]$Repo, # both required, except with -Uninstall + [ValidateScript({ $_ -cmatch '^[0-9a-f]{40}$' })][string]$Sha, # -cmatch: ValidatePattern ignores case + [string]$MachineFile, [string]$FleetConfig, + [ValidatePattern('^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$')][string]$Distro = 'fork-desk', + [ValidateRange(0, 4096)][int]$MemoryGB = 0, [ValidateRange(0, 4096)][int]$SwapGB = 4, + [switch]$Uninstall +) +$ErrorActionPreference = 'Stop' +$env:WSL_UTF8 = '1' # wsl.exe's own lines in UTF-8, not UTF-16 +$OutputEncoding = [Text.UTF8Encoding]::new($false) # what is piped into wsl.exe: UTF-8, not ASCII +$Task = "fork-desk-$Distro" +$WslConfig = Join-Path $env:USERPROFILE '.wslconfig' +# Marks on the Windows side, for a run that stops: `made` (this script is installing the distro: a later run asks no +# takeover question), `restart` (.wslconfig changed and its `wsl --shutdown` is still owed: VM uptime skips sleep) +$Marks = Join-Path $env:LOCALAPPDATA 'fork-desk' +$Made = Join-Path $Marks "$Distro.made"; $Owed = Join-Path $Marks "$Distro.restart" +function Mark([string]$path) { New-Item -ItemType Directory -Force -Path $Marks | Out-Null; Set-Content -LiteralPath $path 'start-desk.ps1' } + +function Head([string]$t) { Write-Host "==== $t" -ForegroundColor Cyan } +function Run([string]$what, [scriptblock]$cmd) { + & $cmd + if ($LASTEXITCODE -ne 0) { throw "$what failed (exit $LASTEXITCODE)" } +} +# PowerShell ends what it pipes into a program with CR LF: the script ends in a comment line, so bash never sees a +# stray CR, and LF only before it. +function Script([string]$s) { ($s -replace "`r", '') + "`n# end" } +function InDistro([string]$what, [string]$script) { # bash as root in the distro, the script on stdin; its output + Script $script | wsl.exe -d $Distro -u root --cd / -- bash -s + if ($LASTEXITCODE -ne 0) { throw "$what failed (exit $LASTEXITCODE)" } +} +function Into([string]$file, [string]$dest) { # a Windows file into the distro, root-owned, mode 644 + if (-not (Test-Path -LiteralPath $file -PathType Leaf)) { throw "no such file: $file" } + $text = [IO.File]::ReadAllText((Resolve-Path -LiteralPath $file).ProviderPath) -replace "`r", '' # UNC paths too + $dir = $dest.Substring(0, $dest.LastIndexOf('/')) # no quotes in the command: Windows PowerShell mangles them + $text | wsl.exe -d $Distro -u root --cd / -- sh -c "umask 022; mkdir -p $dir && cat > $dest.new && mv $dest.new $dest" + if ($LASTEXITCODE -ne 0) { throw "copying $file into the distro failed (exit $LASTEXITCODE)" } +} + +# The distro's own settings, the ones only a restart of WSL applies. ROOT is for the tests only. +$Prepare = @' +set -eu +ROOT=${ROOT:-} +want='[boot] +systemd=true +[automount] +enabled=false +mountFsTab=false +[interop] +enabled=false +appendWindowsPath=false +[network] +generateResolvConf=false' +mkdir -p "$ROOT/etc/binfmt.d" +if [ "$(cat "$ROOT/etc/wsl.conf" 2>/dev/null)" != "$want" ]; then + [ ! -e "$ROOT/etc/wsl.conf" ] || cp -p "$ROOT/etc/wsl.conf" "$ROOT/etc/wsl.conf.bak-$(date +%Y%m%d-%H%M%S)" + printf '%s\n' "$want" > "$ROOT/etc/wsl.conf.new" && mv "$ROOT/etc/wsl.conf.new" "$ROOT/etc/wsl.conf" + echo 'changed: /etc/wsl.conf' +fi +# WSL's resolv.conf is a link into /mnt/wsl; a file of the distro's own instead (dnsTunneling answers at this address) +if [ -L "$ROOT/etc/resolv.conf" ] || [ ! -f "$ROOT/etc/resolv.conf" ]; then + rm -f "$ROOT/etc/resolv.conf" + printf 'nameserver 10.255.255.254\n' > "$ROOT/etc/resolv.conf" + echo 'changed: /etc/resolv.conf' +fi +[ "$(readlink "$ROOT/etc/binfmt.d/WSLInterop.conf" 2>/dev/null)" = /dev/null ] || + { ln -sfn /dev/null "$ROOT/etc/binfmt.d/WSLInterop.conf"; echo 'changed: /etc/binfmt.d/WSLInterop.conf'; } +mkdir -p "$ROOT/etc/fork-kit" # this distro is the desk's now: a later run asks no takeover question +[ -e "$ROOT/etc/fork-kit/desk-distro" ] || echo 'made or taken over by start-desk.ps1' > "$ROOT/etc/fork-kit/desk-distro" +'@ +# What the running WSL has now, whatever this run changed: a run stopped before its `wsl --shutdown` (or settings +# changed by hand) still restarts. One line per reason; nothing when the live state is what the walls need. +$Live = @' +set -eu +ROOT=${ROOT:-} +[ "$(cat "$ROOT/proc/1/comm" 2>/dev/null)" = systemd ] || echo 'restart: PID 1 is not systemd' +for h in "$ROOT"/proc/sys/fs/binfmt_misc/WSLInterop*; do + [ ! -e "$h" ] || { echo "restart: WSL interop is live (${h##*/})"; break; } +done +[ ! "$ROOT/etc/wsl.conf" -nt "$ROOT/proc/1" ] || echo 'restart: /etc/wsl.conf is newer than the running distro' +mem=$(sed -n 's/^MemTotal: *\([0-9]*\) kB$/\1/p' "$ROOT/proc/meminfo" 2>/dev/null || true) +[ -z "${WANT_KB:-}" ] || [ -z "$mem" ] || [ "$mem" -le "$WANT_KB" ] || echo "restart: WSL has $mem kB, above the cap" +mode=$(wslinfo --networking-mode 2>/dev/null || true) +[ -z "$mode" ] || [ "$mode" = nat ] || echo "restart: the networking mode is $mode, not nat" +# automount=false: no Windows drive mounted under /mnt +if awk '$2 ~ /^\/mnt\/[a-z]$/ { found = 1 } END { exit !found }' "$ROOT/proc/mounts" 2>/dev/null; then + echo 'restart: Windows drives are mounted under /mnt' +fi +'@ +# .wslconfig, read and written as UTF-8 without a BOM: Windows PowerShell 5.1's Get-Content reads a file without a +# BOM as ANSI and Set-Content with the ASCII encoding turns every other letter into '?' (a kernel= path with an accent in it +# then stops every distro). The tests run this part alone (between the two marker lines) under pwsh. +# --- wslconfig --- +function Read-WslConfig([string]$path) { + if (-not (Test-Path -LiteralPath $path)) { return @() } + @([IO.File]::ReadAllLines($path, [Text.UTF8Encoding]::new($false))) # a BOM (UTF-8 or UTF-16) is still read +} +function Wsl2Keys([string[]]$lines) { # the keys of the [wsl2] section only + $keys = @{}; $sec = '' + foreach ($l in $lines) { + if ($l -match '^\s*\[(.+)\]\s*$') { $sec = $Matches[1].Trim().ToLower(); continue } + if ($sec -eq 'wsl2' -and $l -match '^\s*([^=#;\s]+)\s*=\s*(.*?)\s*$') { $keys[$Matches[1]] = $Matches[2] } + } + $keys +} +function Merge-WslConfig([string]$path, $want, [scriptblock]$before = {}) { # True when it changed the file (a dated copy of the old one kept; $before runs first) + $lines = Read-WslConfig $path + $out = [Collections.Generic.List[string]]::new(); $sec = ''; $done = @{} + function AddMissing { foreach ($k in $want.Keys) { if (-not $done[$k]) { $out.Add("$k=$($want[$k])"); $done[$k] = 1 } } } + foreach ($l in $lines) { + if ($l -match '^\s*\[(.+)\]\s*$') { if ($sec -eq 'wsl2') { AddMissing }; $sec = $Matches[1].Trim().ToLower(); $out.Add($l); continue } + if ($sec -eq 'wsl2' -and $l -match '^\s*([^=#;\s]+)\s*=' -and $want.Contains($Matches[1])) { + $k = $Matches[1]; if (-not $done[$k]) { $out.Add("$k=$($want[$k])"); $done[$k] = 1 }; continue } + $out.Add($l) + } + if ($sec -eq 'wsl2') { AddMissing } + if ($done.Count -lt $want.Count) { $out.Add('[wsl2]'); AddMissing } + if (($out -join "`n") -eq ($lines -join "`n")) { return $false } + & $before + if (Test-Path -LiteralPath $path) { Copy-Item -LiteralPath $path "$path.bak-$(Get-Date -Format yyyyMMdd-HHmmss)" } + [IO.File]::WriteAllLines("$path.new", [string[]]$out, [Text.UTF8Encoding]::new($false)) + Move-Item -LiteralPath "$path.new" $path -Force + $true +} +function WslVersion([string[]]$lines, [string]$name) { # the WSL version of one distro in `wsl -l -v`; 0: not there + foreach ($l in $lines) { + $f = @(($l.Trim([char]0, ' ', '*', "`t") -replace [char]0, '') -split '\s+') + if ($f.Count -ge 3 -and $f[0] -eq $name -and $f[-1] -match '^[0-9]+$') { return [int]$f[-1] } + } + 0 +} +# --- end wslconfig --- +$Packages = 'set -eu; export DEBIAN_FRONTEND=noninteractive; need=; for p in python3 nftables iproute2 curl ca-certificates openssh-client tar; do dpkg -s $p >/dev/null 2>&1 || need="$need $p"; done; [ -z "$need" ] || { apt-get update -q && apt-get install -y -q $need; }' + +try { + Head 'This PC' + $os = Get-CimInstance Win32_OperatingSystem; $cs = Get-CimInstance Win32_ComputerSystem + $ram = [math]::Floor($cs.TotalPhysicalMemory / 1GB) + $asked = $MemoryGB -ne 0 + if (-not $asked) { $MemoryGB = [math]::Max(4, [math]::Min([math]::Floor($ram * 3 / 4), $ram - 6)) } + if (-not $Uninstall -and $MemoryGB -gt $ram - 2) { + if ($asked) { throw "-MemoryGB $MemoryGB leaves Windows less than 2 GB of this PC's $ram GB" } + throw "this PC has $ram GB of RAM: a desk needs at least 4 GB for WSL and 2 GB for Windows" + } + "Windows $($os.Version) build $($os.BuildNumber); $($cs.NumberOfLogicalProcessors) threads; RAM $ram GB" + 'WSL:'; wsl.exe --version + $version = WslVersion @(wsl.exe -l -v) $Distro # read without starting any distro + $have = $version -ne 0 + "distro ${Distro}: $(if ($have) { "there (WSL $version)" } else { 'not yet' })" + if (Test-Path $WslConfig) { ".wslconfig now:"; Read-WslConfig $WslConfig | ForEach-Object { " $_" } } + + if ($Uninstall) { + if ((Read-Host "Remove the desk kit from $Distro and the logon task $Task? Type yes") -ne 'yes') { 'Stopped: nothing changed.'; return } + if ($have) { InDistro 'desk-install uninstall' '[ ! -x /opt/fork-fleet/desk-install ] || /opt/fork-fleet/desk-install uninstall' } + Stop-ScheduledTask -TaskName $Task -ErrorAction SilentlyContinue # its `sleep infinity` too + Unregister-ScheduledTask -TaskName $Task -Confirm:$false -ErrorAction SilentlyContinue + Remove-Item -LiteralPath $Owed, $Made -ErrorAction SilentlyContinue + $bak = @(Get-ChildItem -Path "$WslConfig.bak-*" -ErrorAction SilentlyContinue | Sort-Object Name) + "Done. The distro and .wslconfig are kept; to remove the distro too: wsl --unregister $Distro" + if ($bak) { ".wslconfig as it was before the first run: $($bak[0].FullName) (copy it back over .wslconfig to undo)" } + return + } + if (-not $MachineFile) { throw '-MachineFile is required: the machine file with the desk section (README)' } + if (-not $Repo -or -not $Sha) { throw '-Repo and -Sha are required (the fork and its release commit)' } + # every input before the question, so a typo is found before anything changes + if (-not (Test-Path -LiteralPath $MachineFile -PathType Leaf)) { throw "-MachineFile ${MachineFile}: no such file" } + if ($FleetConfig -and -not (Test-Path -LiteralPath $FleetConfig -PathType Leaf)) { throw "-FleetConfig ${FleetConfig}: no such file" } + # WSL1 runs no systemd: every run would restart WSL and check would never pass + if ($have -and $version -ne 2) { throw "the distro $Distro is WSL $version; the desk needs WSL 2: wsl --set-version $Distro 2 (or pick another -Distro), then this again" } + # A distro of that name that this script did not make: its /etc/wsl.conf is replaced below. (This starts it; it + # changes nothing in it.) A run stopped while installing it left the Windows-side mark. + $ours = Test-Path -LiteralPath $Made + if ($have -and -not $ours) { wsl.exe -d $Distro -u root --cd / -- test -e /etc/fork-kit/desk-distro; $ours = $LASTEXITCODE -eq 0 } + + Write-Host '' + Write-Host "Next: the distro $Distro; .wslconfig [wsl2] memory=${MemoryGB}GB swap=${SwapGB}GB networkingMode=nat" -ForegroundColor Yellow + Write-Host 'dnsTunneling=true guiApplications=false (for ALL your WSL distros; a dated copy is kept); then ONE' -ForegroundColor Yellow + Write-Host 'wsl --shutdown (every WSL distro and Docker Desktop stop), the desk kit, and a logon task.' -ForegroundColor Yellow + if ($have -and -not $ours) { + Write-Host "The distro $Distro is already there and this script did not make it. Its /etc/wsl.conf is REPLACED" -ForegroundColor Yellow + Write-Host '(its [user] default= and every other line go; no /mnt/c, no Windows programs or PATH), and it runs fleet' -ForegroundColor Yellow + Write-Host 'jobs from now on. Pick another -Distro to keep it as it is.' -ForegroundColor Yellow + if ((Read-Host "Type the distro's name ($Distro) to take it over") -cne $Distro) { Write-Host 'Stopped: nothing changed.'; return } + } + elseif ((Read-Host 'Type yes to go on') -ne 'yes') { Write-Host 'Stopped: nothing changed.'; return } + + Head 'the distro' + if (-not $have) { + Mark $Made # before the install: a run stopped inside it asks no takeover question next time + wsl.exe --install -d Ubuntu-24.04 --name $Distro --no-launch --version 2 + if ($LASTEXITCODE -ne 0) { throw "wsl --install failed (exit $LASTEXITCODE): run wsl --update (or restart Windows after WSL's own install), then this again" } + $version = WslVersion @(wsl.exe -l -v) $Distro + if ($version -ne 2) { throw "the new distro $Distro is WSL $version, not 2: wsl --unregister $Distro, then wsl --set-default-version 2 and this again" } + } + wsl.exe -d $Distro -u root --cd / -- true + if ($LASTEXITCODE -ne 0) { + throw "the first start of the distro failed (exit $LASTEXITCODE). If a run was stopped while installing it: wsl --unregister $Distro (removes it and all in it), then this again" + } + + Head '.wslconfig' + $want = [ordered]@{ memory = "${MemoryGB}GB"; swap = "${SwapGB}GB"; networkingMode = 'nat'; dnsTunneling = 'true'; guiApplications = 'false' } + $restart = Merge-WslConfig $WslConfig $want { Mark $Owed } # the mark first: a run stopped after the write still restarts + if ($restart) { 'changed: .wslconfig' } + + Head "the distro's own settings" + $changed = InDistro "the distro's settings" $Prepare + $changed + Remove-Item -LiteralPath $Made -ErrorAction SilentlyContinue # the distro's own mark is there now + # The live state decides, not only what this run changed: a run stopped before its shutdown restarts next time + $live = @(InDistro 'the running settings' ("WANT_KB=$([long]$MemoryGB * 1048576)`n" + $Live)) + $up = [double]((wsl.exe -d $Distro -u root --cd / -- cat /proc/uptime) -split ' ')[0] + if ((Test-Path $WslConfig) -and ((Get-Date) - (Get-Item -LiteralPath $WslConfig).LastWriteTime).TotalSeconds -lt $up) { + $live += 'restart: .wslconfig is newer than the running WSL' + } + if (Test-Path -LiteralPath $Owed) { $live += 'restart: .wslconfig changed and its wsl --shutdown is still owed' } + $live + if ($restart -or $changed -or $live) { + Head 'wsl --shutdown (the new settings apply at the next start)' + Run 'wsl --shutdown' { wsl.exe --shutdown } + Remove-Item -LiteralPath $Owed -ErrorAction SilentlyContinue + } + InDistro 'the packages the fleet needs' $Packages + + Head 'the machine file and the desk kit' + Into $MachineFile '/etc/fork-kit/machine.json' + $extra = '' + if ($FleetConfig) { Into $FleetConfig '/etc/fork-kit/desk-fleet.json'; $extra = ' --fleet-config /etc/fork-kit/desk-fleet.json' } + InDistro 'the desk kit (its FAIL line above says why)' @" +set -eu +curl -fsSL --proto =https https://raw.githubusercontent.com/$Repo/$Sha/fork-kit/desk/bootstrap.sh -o /root/desk-bootstrap.sh +sh /root/desk-bootstrap.sh --repo $Repo --sha $Sha$extra +"@ + + Head "the logon task $Task (keeps the distro running while you are logged in)" + $a = New-ScheduledTaskAction -Execute 'conhost.exe' -Argument "--headless wsl.exe -d $Distro -u root --cd / -- sleep infinity" + $me = [Security.Principal.WindowsIdentity]::GetCurrent().Name + Register-ScheduledTask -TaskName $Task -Action $a -Trigger (New-ScheduledTaskTrigger -AtLogOn -User $me) ` + -Settings (New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries) -Force | Out-Null + if ((Get-ScheduledTask $Task).State -ne 'Running') { Start-ScheduledTask $Task } + + Head 'check' + $bad = 0 + wsl.exe -d $Distro -u root --cd / -- /opt/fork-fleet/desk-install check + if ($LASTEXITCODE -ne 0) { $bad++ } + $cfg = Wsl2Keys (Read-WslConfig $WslConfig) + foreach ($k in $want.Keys) { + if ($cfg[$k] -eq $want[$k]) { "PASS wslconfig [wsl2] $k=$($want[$k])" } else { "FAIL wslconfig [wsl2] no $k=$($want[$k])"; $bad++ } + } + $t = Get-ScheduledTask $Task -ErrorAction SilentlyContinue + if ($t -and $t.State -eq 'Running') { "PASS logon-task $Task running" } else { "FAIL logon-task $Task is $(if ($t) { $t.State } else { 'missing' })"; $bad++ } + if ($bad -eq 0) { Write-Host 'ALL PASS: this PC takes fleet jobs while you are logged in.' -ForegroundColor Green } + else { Write-Host 'Some FAIL lines above (a template FAIL clears by itself in a few minutes; run this again then).' -ForegroundColor Red; exit 1 } +} +catch { + Write-Host "FAIL: $($_.Exception.Message)" -ForegroundColor Red + Write-Host 'Nothing printed here is a secret. Fix it and run this again: every step checks what is there.' -ForegroundColor Red + exit 1 +}