Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fork-kit/desk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ leaves a mark in `%LOCALAPPDATA%\fork-desk`, so the next run asks no takeover qu
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; without `-MemoryGB`, a higher one already there, raised by hand, is kept up to the RAM less 2 GB; one
WSL cannot read, such as `26 GB` or `26gb`, is rewritten, since WSL would run on half the RAM), 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; an old `networkingMode` or `dnsTunneling`
BOM; a dated copy of the old file is kept, other keys and sections stay; a section header is read as WSL reads it,
so a line WSL rejects, such as `[ wsl2 ]`, starts no section and the key under it is not taken as set; an old `networkingMode` or `dnsTunneling`
line under `[experimental]`, the form before WSL 2.0 that can still win over `[wsl2]`, is removed, and the check
FAILs while one is there; the question lists each change, old -> new, and the file is written only when it lists one,
so "no change" means the file is left alone); the distro's own settings the WSL2 walls
Expand Down
72 changes: 71 additions & 1 deletion fork-kit/desk/tests/windows.test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""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.

Expand Down Expand Up @@ -303,7 +303,11 @@
path = os.path.join(d, '.wslconfig')
for old, notes in (('[wsl2]\nmemory = 9GB\nNetworkingMode=NAT\n', []),
('[wsl2]\nmemory=9GB\nnetworkingMode=nat\nmemory=12GB\n',
['[wsl2] memory=12GB: a repeated line, removed'])):
['[wsl2] memory=12GB: a repeated line, removed']),
# WSL trims space and tab only: a key after a non-breaking space or a form feed is not
# networkingMode to WSL, so it is not removed (and the check does not FAIL on it either)
('[wsl2]\nmemory=9GB\nnetworkingMode=nat\n[experimental]\n networkingMode=mirrored\n'
'\fdnsTunneling=true\n', [])):
with self.subTest(old):
with open(path, 'w', encoding='utf-8', newline='\n') as f:
f.write(old)
Expand Down Expand Up @@ -362,6 +366,72 @@
'[experimental] dnsTunneling=false: removed', 'changed=True', 'left=sparseVhd', 'after=0'):
self.assertIn(line, r.stdout)

@unittest.skipUnless(shutil.which('pwsh'), 'pwsh is not installed')
def test_section_headers_are_read_as_wsl_reads_them(self):
"""lens #354: WSL's ParseSection (configfile.cpp) takes space or tab, '[', a letter then letters or digits,
']', space or tab, then the end or a '#' comment, in any case. On a '[' line it rejects, the keys after it go
under the old section's length of the letters it had read: none read keeps the section."""
d = tempfile.mkdtemp(prefix='desk-wslconfig-test-')
self.addCleanup(shutil.rmtree, d, True)
path = os.path.join(d, '.wslconfig')
want = '[wsl2]\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n'
# (old file, the [wsl2] memory WSL reads, [experimental] keys WSL reads, the file after the merge)
rows = (
('[wsl2]\nmemory=9GB\n[experimental] # from an old guide\ndnsTunneling=false\n', '9GB', 'dnsTunneling',
want + '[experimental] # from an old guide\n'),
('[experimental]\nautoProxy=true\n[ wsl2 ]\nmemory=4GB\n', '', 'autoProxy,memory',
'[experimental]\nautoProxy=true\n[ wsl2 ]\nmemory=4GB\n' + want),
('[WSL2]\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', None),
('\t[wsl2]\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', None),
# a vertical tab is not space to WSL: the line is rejected, so this is a repeated [wsl2] key
(want + '\v[experimental]\nnetworkingMode=mirrored\n', '9GB', '', want + '\v[experimental]\n'),
# rejected after the name: WSL keeps the old section's length (4) of it, 'expe' here, so neither section
('[wsl2]\nmemory=9GB\n[experimental] old\nnetworkingMode=mirrored\n', '9GB', '',
want + '[experimental] old\nnetworkingMode=mirrored\n'),
# ... and from [boot] (also 4 letters) into wsl2
('[boot]\nsystemd=true\n[wsl2] old\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '',
None),
# ... and from a longer one into 'wsl2' padded past its length, which is no section (lens re-check: a NUL
# pad compared equal to 'wsl2' in PowerShell). WSL takes no comment but '#' on a header.
('[experimental]\nautoMemoryReclaim=gradual\n[wsl2] ; set by hand\nmemory=20GB\n', '', 'autoMemoryReclaim',
'[experimental]\nautoMemoryReclaim=gradual\n[wsl2] ; set by hand\nmemory=20GB\n' + want),
# only space or tab after ']': a form feed rejects the header after its name
('[experimental]\nautoProxy=true\n[wsl2]\f\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n', '',
'autoProxy', '[experimental]\nautoProxy=true\n[wsl2]\f\nmemory=9GB\nnetworkingMode=nat\nguiApplications=false\n'
+ want),
# a name starts with a letter: none read, so the keys stay in [wsl2]
('[wsl2]\nmemory=9GB\n[1x]\nnetworkingMode=nat\nguiApplications=false\n', '9GB', '', None),
# names are ASCII any case (strcasecmp): under a Turkish culture .ToLower() makes this 'experımental'
(want + '[EXPERIMENTAL]\nnetworkingMode=mirrored\n', '9GB', 'networkingMode', want + '[EXPERIMENTAL]\n'),
)
script = os.path.join(d, 'merge.ps1')
with open(script, 'w', encoding='ascii') as f:
f.write(wslconfig_part() + '\n$want = [ordered]@{ memory = "9GB"; networkingMode = "nat"; '
'guiApplications = "false" }\n$l = Read-WslConfig $args[0]\n'
'"memory=$((Wsl2Keys $l)[\'memory\'])"\n'
'"experimental=$((SectionKeys $l \'experimental\').Keys | Sort-Object | Join-String -Separator \',\')"\n'
'"changed=$(Merge-WslConfig $args[0] $want)"\n'
'"after=$(@(Changes (Read-WslConfig $args[0]) $want).Count)"\n'
'"now=$((Wsl2Keys (Read-WslConfig $args[0]))[\'memory\'])"\n')
for (old, memory, experimental, new), culture in ((row, c) for c in ('C', 'tr_TR.UTF-8') for row in rows):
with self.subTest(old, culture=culture): # .NET takes its culture from LC_ALL (tr: I lowers to dotless ı)
with open(path, 'w', encoding='utf-8', newline='\n') as f:
f.write(old)
r = subprocess.run(['pwsh', '-NoProfile', '-NonInteractive', '-File', script, path],
capture_output=True, text=True, timeout=120, stdin=subprocess.DEVNULL,
env={**os.environ, 'LC_ALL': culture})
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
out = r.stdout.splitlines()
self.assertIn(f'memory={memory}', out) # what the check at the end reads
self.assertIn(f'experimental={experimental}', out)
self.assertIn(f'changed={new is not None}', out)
self.assertEqual(slurp(path), old if new is None else new)
self.assertIn('after=0', out)
self.assertIn('now=9GB', out)
for n in os.listdir(d):
if n.startswith('.wslconfig.bak-'):
os.unlink(os.path.join(d, n))

@unittest.skipUnless(shutil.which('pwsh'), 'pwsh is not installed')
def test_the_memory_cap_kept_is_one_wsl_reads_within_its_bounds(self):
"""Without -MemoryGB, a higher cap a person set (more agents or jobs in WSL) is neither lowered nor a FAIL, nor
Expand Down
16 changes: 14 additions & 2 deletions fork-kit/desk/windows/start-desk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,23 @@ function Read-WslConfig([string]$path) {
# networkingMode and dnsTunneling were [experimental] keys before WSL 2.0; an old line there can still win over
# [wsl2], so Merge-WslConfig removes them there (the [wsl2] ones are what the desk needs) and the check FAILs on them.
$Experimental = @('networkingMode', 'dnsTunneling')
# The section after line $l, as WSL's ParseSection reads a header: space or tab, an open bracket, a letter then letters
# or digits, a close bracket, space or tab, then the end or a # comment; any ASCII case. A bracket line it rejects keeps
# the length of $sec of the letters read (none: $sec as it was; fewer pad with '*' and match no section; WSL pads with
# NUL, but PowerShell's -eq ignores NUL, so 'wsl2' and NULs would equal 'wsl2'). $null: no bracket.
function NextSection([string]$l, [string]$sec) {
if ($l -cnotmatch '^[ \t]*\[(([A-Za-z][A-Za-z0-9]*)(\][ \t]*(#.*)?$)?)?') { return $null }
if ($Matches[3]) { return $Matches[2].ToLowerInvariant() }
if ($Matches[2]) { return $Matches[2].ToLowerInvariant().PadRight($sec.Length, '*').Substring(0, $sec.Length) }
$sec
}
# A key=value line, trimmed of space and tab only, as WSL's config parser does: a non-breaking space or a form feed
# stays in the value (WSL then rejects a memory= value, so it is rewritten), where .NET's \s would drop it.
function SectionKeys([string[]]$lines, [string]$section) { # the keys of one section only
$keys = @{}; $sec = ''
foreach ($l in $lines) {
if ($l -match '^\s*\[(.+)\]\s*$') { $sec = $Matches[1].Trim().ToLower(); continue }
$next = NextSection $l $sec
if ($null -ne $next) { $sec = $next; continue }
if ($sec -eq $section -and $l -match '^[ \t]*([^=#; \t]+)[ \t]*=[ \t]*(.*?)[ \t]*$') { $keys[$Matches[1]] = $Matches[2] }
}
$keys
Expand All @@ -139,7 +150,8 @@ function MergeLines([string[]]$lines, $want) {
$out = [Collections.Generic.List[string]]::new(); $notes = [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; $notes.Add(" [wsl2] ${k}: (not set) -> $($want[$k])") } } }
foreach ($l in $lines) {
if ($l -match '^\s*\[(.+)\]\s*$') { if ($sec -eq 'wsl2') { AddMissing }; $sec = $Matches[1].Trim().ToLower(); $out.Add($l); continue }
$next = NextSection $l $sec
if ($null -ne $next) { if ($sec -eq 'wsl2' -and $next -ne 'wsl2') { AddMissing }; $sec = $next; $out.Add($l); continue }
if ($sec -eq 'wsl2' -and $l -match '^[ \t]*([^=#; \t]+)[ \t]*=[ \t]*(.*?)[ \t]*$' -and $want.Contains($Matches[1])) {
$k = $Matches[1]; $v = $Matches[2]
if ($done[$k]) { $notes.Add(" [wsl2] $k=${v}: a repeated line, removed"); continue }
Expand Down
6 changes: 4 additions & 2 deletions fork-kit/pc-agents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ allowed (connection tracking).
- **Packages:** setup installs `nftables`, `libatomic1` and `unzip` when `dpkg` does not list one as installed (held
counts as installed; a distro set up by an older kit gets the two new ones at its next run; no T3 restart for
them). It installs them after it writes its files, so `apt-get` resolves through the machine file's `nameservers`
as they are now, and before it loads the firewall. A package install stopped half-way (`dpkg --audit` names it) is
finished first (`dpkg --configure -a`, run only then). Nothing asks a question: a changed config file is kept and
as they are now, and before it loads the firewall. A package install stopped half-way (`dpkg --audit` names it, or
`/var/lib/dpkg/updates` holds a file named all digits, which `apt-get install` refuses on as well) is finished first
(`dpkg --configure -a`, run only then). `apt-get` and `dpkg` run under `LC_ALL=C`, so a lock reads the same in
every language. Nothing asks a question: a changed config file is kept and
debconf takes its defaults. A lock another `apt-get` or `dpkg` holds (apt-daily on a new distro) is waited for, up
to 5 minutes a step: `apt-get` waits for dpkg's lock itself, and setup runs `dpkg`, `apt-get update` and
`apt-get install` again while they fail on the locks they take without a wait. A failed step stops setup, naming
Expand Down
32 changes: 24 additions & 8 deletions fork-kit/pc-agents/sbin/pc-agents
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,23 @@ def install_t3(c, version, done):
return None


# What dpkg and apt-get say when another one (apt-daily, unattended-upgrades) holds a lock they take without a wait
LOCKED = re.compile(r'could not get lock|unable to lock|(is|was) locked by another process', re.I)
# What dpkg and apt-get say (under LC_ALL=C) when another one (apt-daily, unattended-upgrades) holds a lock they take
# without a wait. Not apt's `Unable to lock directory` or dpkg's `unable to lock`: those follow any lock failure (a
# lock file that cannot be opened too), which waiting does not cure.
LOCKED = re.compile(r'could not get lock|(is|was) locked by another process', re.I)


def dpkg_interrupted():
"""True when a dpkg run stopped before it saved its status: /var/lib/dpkg/updates holds a file named all digits
(apt-get install then refuses: "dpkg was interrupted"). `dpkg --audit` does not name the packages of such a
journal when their last state is installed. A folder that cannot be read counts as interrupted."""
try:
names = os.listdir(k.ROOT + '/var/lib/dpkg/updates')
except FileNotFoundError:
return False
except OSError:
return True
return any(re.fullmatch(r'[0-9]+', n) for n in names)


def run_locked(argv, env):
Expand All @@ -210,21 +225,22 @@ def run_locked(argv, env):

def install_packages():
"""Install the k.PACKAGES that are not (fully) installed; held counts as installed. A dpkg run stopped half-way
(`dpkg --audit` names a package) is finished first (apt-get refuses until it is). Nothing asks a question (stdin
is /dev/null): a changed config file is kept, debconf takes its defaults, and a lock apt-daily holds is waited
for (run_locked). Returns why not, or None."""
(`dpkg --audit` names a package, or dpkg_interrupted()) is finished first (apt-get refuses until it is). Nothing
asks a question (stdin is /dev/null): a changed config file is kept, debconf takes its defaults, and a lock
apt-daily holds is waited for (run_locked). apt and dpkg run under LC_ALL=C: LOCKED reads their English (sudo
keeps the caller's LANG). Returns why not, or None."""
r = k.run(['/usr/bin/dpkg-query', '-W', '-f=${Package}\t${Status}\n', *k.PACKAGES])
have = {p for p, _, status in (line.partition('\t') for line in r.stdout.splitlines())
if status.endswith(' ok installed')}
missing = [p for p in k.PACKAGES if p not in have]
if not missing:
return None
say('installing ' + ' '.join(missing))
quiet = {'DEBIAN_FRONTEND': 'noninteractive'}
quiet = {'DEBIAN_FRONTEND': 'noninteractive', 'LC_ALL': 'C'}
busy = (' Another apt-get or dpkg still runs (apt-daily, on a new distro) after 5 minutes: '
'wait for it to end, then run setup again')
r = k.run(['/usr/bin/dpkg', '--audit']) # reads only: no lock
if r.returncode or r.stdout.strip():
r = k.run(['/usr/bin/dpkg', '--audit'], env={'LC_ALL': 'C'}) # reads only: no lock; exits 0 when it names one
if r.returncode or r.stdout.strip() or dpkg_interrupted():
r = run_locked(['/usr/bin/dpkg', '--force-confdef', '--force-confold', '--configure', '-a'], quiet)
if r.returncode:
return (f'cannot install {" ".join(missing)}: dpkg --configure -a failed ({r.stderr.strip()[-300:]}).'
Expand Down
Loading
Loading