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
6 changes: 6 additions & 0 deletions fork-kit/desk/tests/windows.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ def test_the_memory_cap_kept_is_one_wsl_reads_within_its_bounds(self):
('memory=16777216TB', 30, '8GB', 8388608, '[wsl2] memory: 16777216TB -> 8GB'), # WSL: 0, its default
('memory=26 GB', 30, '8GB', 8388608, '[wsl2] memory: 26 GB -> 8GB'), # WSL rejects these two
('memory=30720mb', 30, '8GB', 8388608, '[wsl2] memory: 30720mb -> 8GB'),
('memory=26Gb', 30, '8GB', 8388608, '[wsl2] memory: 26Gb -> 8GB'), # the unit is case-sensitive
# WSL trims only space and tab: a non-breaking space (pasted from a web page) or a form feed after
# the unit stays in its value, and it rejects that (#321)
('memory=26GB\u00a0', 30, '8GB', 8388608, '[wsl2] memory: 26GB\u00a0 -> 8GB'),
('memory=26GB\f', 30, '8GB', 8388608, '[wsl2] memory: 26GB\f -> 8GB'),
('memory=\u00a026GB', 30, '8GB', 8388608, '[wsl2] memory: \u00a026GB -> 8GB'),
('memory=4GB', 30, '8GB', 8388608, '[wsl2] memory: 4GB -> 8GB'), # below: raised
('memory=8191MB', 30, '8GB', 8388608, '[wsl2] memory: 8191MB -> 8GB'),
('memory=lots', 30, '8GB', 8388608, '[wsl2] memory: lots -> 8GB'),
Expand Down
10 changes: 6 additions & 4 deletions fork-kit/desk/windows/start-desk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ 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')
# 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 }
if ($sec -eq $section -and $l -match '^\s*([^=#;\s]+)\s*=\s*(.*?)\s*$') { $keys[$Matches[1]] = $Matches[2] }
if ($sec -eq $section -and $l -match '^[ \t]*([^=#; \t]+)[ \t]*=[ \t]*(.*?)[ \t]*$') { $keys[$Matches[1]] = $Matches[2] }
}
$keys
}
Expand All @@ -138,13 +140,13 @@ function MergeLines([string[]]$lines, $want) {
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 }
if ($sec -eq 'wsl2' -and $l -match '^\s*([^=#;\s]+)\s*=\s*(.*?)\s*$' -and $want.Contains($Matches[1])) {
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 }
$done[$k] = 1
if ($v -eq $want[$k]) { $out.Add($l) } else { $out.Add("$k=$($want[$k])"); $notes.Add(" [wsl2] ${k}: $v -> $($want[$k])") }
continue }
if ($sec -eq 'experimental' -and $l -match '^\s*([^=#;\s]+)\s*=\s*(.*?)\s*$' -and $Matches[1] -in $Experimental) {
if ($sec -eq 'experimental' -and $l -match '^[ \t]*([^=#; \t]+)[ \t]*=[ \t]*(.*?)[ \t]*$' -and $Matches[1] -in $Experimental) {
$notes.Add(" [experimental] $($Matches[1])=$($Matches[2]): removed (the [wsl2] one is used)"); continue }
$out.Add($l)
}
Expand All @@ -166,7 +168,7 @@ function Merge-WslConfig([string]$path, $want, [scriptblock]$before = {}) { #
# (ParseMemorySize) takes a number and B, K, KB, M, MB, G, GB, T or TB exactly: no space before the unit, upper case
# (`26 GB` or `26gb` makes it warn and run on half the RAM).
function CapKb([string]$v) {
if ($v -cnotmatch '^\s*([0-9]{1,13})([KMGT]?)B?\s*$') { return [long]-1 }
if ($v -cnotmatch '^([0-9]{1,13})([KMGT]?)B?$') { return [long]-1 }
[long][math]::Min(1e15, [math]::Floor([double]$Matches[1] * [math]::Pow(1024, ' KMGT'.IndexOf($Matches[2])) / 1024))
}
# The memory cap to want: $gb GB, or the one already in .wslconfig, kept as it is written (neither lowered nor a FAIL,
Expand Down
18 changes: 11 additions & 7 deletions fork-kit/pc-agents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ The kit reads the `pc-agents` section of the machine file `/etc/fork-kit/machine

`t3Version` must be 0.1.7 or newer: older releases have no managed policy, so they would run with none of the kit's
locks and with updates on. A 0.1.7 pre-release counts as older (some 0.1.7 nightlies were built before the policy),
and a pre-release of a later version as that version. Setup and `check` refuse them, naming the key. (`t3 pair`,
and a pre-release of a later version as that version. This fork's stable build of a release (`0.1.7-fork.1`) counts
as that release, its nightlies as pre-releases. Setup and `check` refuse the older ones, naming the key. (`t3 pair`,
step 10, came earlier.)

Every T3 setting can be locked through `lockedSettings`, the fork's own settings (`agentSlots`, `usagePacingGate`,
Expand Down Expand Up @@ -235,12 +236,15 @@ 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 is finished first
(`dpkg --configure -a`). Nothing asks a question: a changed config file is kept, debconf takes its defaults, and
`apt-get` waits up to 5 minutes for a lock (apt-daily on a new distro). A failed step stops setup, naming the
command to run; the files it wrote stay, and when one of them changed what T3 runs with, the walls check's
`restart` wall fails (and the 2-minute check stops T3) until setup runs again to the end, as after a failed T3
download.
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
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
the command to run (for a lock still held, to wait for the other one); the files it wrote stay, and when one of them
changed what T3 runs with, the walls check's `restart` wall fails (and the 2-minute check stops T3) until setup runs
again to the end, as after a failed T3 download. A distro restart in between starts T3 again with the new files and
firewall, without the packages.
- **A distro that is not fresh:** refused before anything is written, with the reasons: another login user, an
`agent` with another uid or in another group, a file the kit would write that it did not write (or that was
edited since, for example a hand-written `wsl.conf` or `policy.json`), a cron, at or ssh unit where the kit masks
Expand Down
6 changes: 4 additions & 2 deletions fork-kit/pc-agents/lib/pc_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BOOT_ID = '/proc/sys/kernel/random/boot_id' # the marker holds this boot's id and CLOCK_MONOTONIC, like systemd's
AS_AGENT = 'pc-agents-as-agent' # name prefix of `pc-agents as-agent`'s transient units (check --enforce stops them)
SETTLE, SETTLE_STEP = 15, 1 # after a (re)start, setup watches T3 this many seconds, every SETTLE_STEP, before done
LOCK_WAIT, LOCK_STEP = 300, 5 # setup retries a dpkg or apt-get step that found a lock held this long, every LOCK_STEP
LOADED = '/run/pc-agents/firewall.loaded' # sha256 of the firewall file and `nft list` of it, saved when loaded
T3 = OPT + '/t3' # T3's runtime, installed by root (T3CODE_HOME of scripts/install.sh): the agent cannot change it
UNIT, WALLS = 't3code-agents.service', 'pc-agents-walls'
Expand Down Expand Up @@ -98,10 +99,11 @@ def is_ip_list(value, net, glob=False, most=64):


def new_enough(version):
"""Whether VERSION version is OLDEST_T3 or newer, in semver order: a pre-release ranks below its release."""
"""Whether VERSION version is OLDEST_T3 or newer, in semver order: a pre-release ranks below its release. The
fork's stable build of a release (`-fork.<n>`, fork-release.yml) is that release; its nightlies are pre-releases."""
m = VERSION.fullmatch(version)
numbers = tuple(map(int, m.groups()[:3]))
return numbers > OLDEST_T3 or numbers == OLDEST_T3 and not m[4]
return numbers > OLDEST_T3 or numbers == OLDEST_T3 and (not m[4] or bool(re.fullmatch(r'-fork\.[1-9]\d*', m[4])))


def port_range(text):
Expand Down
43 changes: 32 additions & 11 deletions fork-kit/pc-agents/sbin/pc-agents
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,27 @@ 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)


def run_locked(argv, env):
"""k.run(argv), tried again every k.LOCK_STEP s for up to k.LOCK_WAIT s while it fails on a lock another apt-get
or dpkg holds. apt-get's DPkg::Lock::Timeout waits only for dpkg's frontend lock: `update` takes the lists lock and
`install` the archives lock without a wait, and so does dpkg its own."""
end = time.monotonic() + k.LOCK_WAIT
while True:
r = k.run(argv, timeout=900, env=env)
if not r.returncode or not LOCKED.search(r.stderr) or time.monotonic() >= end:
return r
time.sleep(k.LOCK_STEP)


def install_packages():
"""Install the k.PACKAGES that are not (fully) installed; held counts as installed. A dpkg run stopped half-way
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 apt-get waits for a lock apt-daily holds. Returns why not, or None."""
(`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."""
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')}
Expand All @@ -204,20 +221,24 @@ def install_packages():
return None
say('installing ' + ' '.join(missing))
quiet = {'DEBIAN_FRONTEND': 'noninteractive'}
r = k.run(['/usr/bin/dpkg', '--force-confdef', '--force-confold', '--configure', '-a'], timeout=900, env=quiet)
if r.returncode:
return (f'cannot install {" ".join(missing)}: dpkg --configure -a failed ({r.stderr.strip()[-300:]}). If '
'another apt-get or dpkg still runs (apt-daily, on a new distro), wait for it to end; then run '
'`dpkg --configure -a` as root, and setup again')
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 = 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:]}).'
+ (busy if LOCKED.search(r.stderr) else ' Run `dpkg --configure -a` as root, then setup again'))
apt = ['/usr/bin/apt-get', '-o', 'Dpkg::Options::=--force-confdef', '-o', 'Dpkg::Options::=--force-confold',
'-o', 'DPkg::Lock::Timeout=300']
for argv in (apt + ['update'], apt + ['install', '-y', *missing]):
r = k.run(argv, timeout=900, env=quiet)
r = run_locked(argv, quiet)
if r.returncode:
return (f'cannot install {" ".join(missing)}: apt-get {argv[len(apt)]} failed ({r.stderr.strip()[-300:]}).'
' Fix the distro\'s network (the machine file\'s nameservers are in /etc/resolv.conf now) or '
f'package sources, run `apt-get update && apt-get install -y {" ".join(missing)}` as root, then '
'setup again')
+ (busy if LOCKED.search(r.stderr) else
' Fix the distro\'s network (the machine file\'s nameservers are in /etc/resolv.conf now) or '
f'package sources, run `apt-get update && apt-get install -y {" ".join(missing)}` as root, '
'then setup again'))
return None


Expand Down
34 changes: 25 additions & 9 deletions fork-kit/pc-agents/tests/fake_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@
' [ -e "$S/$u.status" ] && { echo "ExecMainCode=$(cat "$S/$u.code")"; echo "ExecMainStatus=$(cat "$S/$u.status")"; }\n'
' echo "ExecMainStartTimestampMonotonic=$(cat "$S/$u.start" 2>/dev/null || echo 0)";;\n'
'esac\n',
# apt-get [-o OPTION]... update|install -y PKG...: logs the rest; installs each package in /log/packages (nftables
# brings the fake nft kept in /log/nft-package). As real apt-get: refuses while a package is unpacked (dpkg was
# interrupted). /log/apt-fail makes it fail; /log/dns holds the resolv.conf line `update` needs to reach the
# mirror; with /log/questions it fails unless noninteractive and keeping changed config files.
# apt-get [-o OPTION]... update|install -y PKG...: logs the rest (and the options, in /log/apt-get-options);
# installs each package in /log/packages (nftables brings the fake nft kept in /log/nft-package). As real apt-get:
# refuses while a package is unpacked (dpkg was interrupted). /log/apt-locked-update (or -install) holds how many
# more runs find the lists (or archives) lock held, as while apt-daily runs: real apt-get does not wait for those.
# /log/apt-fail makes it fail; /log/dns holds the resolv.conf line `update` needs to reach the mirror; with
# /log/questions it fails unless noninteractive and keeping changed config files.
'usr/bin/apt-get': 'R="${0%/usr/bin/apt-get}"; o=; while [ "$1" = -o ]; do o="$o $2"; shift 2; done\n'
'echo "$*" >>"$R/log/apt-get"\n'
'echo "$*" >>"$R/log/apt-get"; echo "${o# }" >>"$R/log/apt-get-options"\n'
'n=$(cat "$R/log/apt-locked-$1" 2>/dev/null || echo 0); if [ "$n" -gt 0 ]; then\n'
' echo $((n - 1)) >"$R/log/apt-locked-$1"; [ "$1" = update ] && d=/var/lib/apt/lists || '
'd=/var/cache/apt/archives\n'
' echo "E: Could not get lock $d/lock. It is held by process 4242 (apt-get) (test)" >&2\n'
' echo "E: Unable to lock directory $d/" >&2; exit 100; fi\n'
'[ -e "$R/log/apt-fail" ] && { echo "E: no network (test)" >&2; exit 100; }\n'
'grep -qs unpacked "$R"/log/packages/* && { echo "E: dpkg was interrupted, you must manually '
'run \'dpkg --configure -a\' to correct the problem." >&2; exit 100; }\n'
Expand All @@ -100,11 +107,19 @@
'usr/bin/dpkg-query': 'R="${0%/usr/bin/dpkg-query}"; echo "$*" >>"$R/log/dpkg-query"; shift 2; rc=0\n'
'for p; do if [ -e "$R/log/packages/$p" ]; then printf "%s\\t%s\\n" "$p" "$(cat "$R/log/packages/$p")"\n'
' else echo "dpkg-query: no packages found matching $p" >&2; rc=1; fi; done; exit $rc\n',
# dpkg [--force-...]... --configure -a: finishes what an apt-get stopped half-way left unpacked. /log/dpkg-fail
# makes it fail as with apt-daily holding the lock; /log/questions as for apt-get.
# dpkg --audit: names each package an apt-get stopped half-way left unpacked (no lock: it only reads).
# dpkg [--force-...]... --configure -a: finishes them. /log/dpkg-locked holds how many more runs find the lock
# held (real dpkg does not wait); /log/questions as for apt-get.
'usr/bin/dpkg': 'R="${0%/usr/bin/dpkg}"; echo "$*" >>"$R/log/dpkg"\n'
'[ -e "$R/log/dpkg-fail" ] && { echo "dpkg: error: dpkg frontend lock was locked by another process '
'(test)" >&2; exit 2; }\n'
'if [ "$1" = --audit ]; then rc=0; for f in "$R"/log/packages/*; do grep -qs unpacked "$f" || '
'continue\n'
' [ $rc = 1 ] || echo "The following packages have been unpacked but not yet configured:"\n'
' echo " ${f##*/}"; rc=1; done; exit $rc; fi\n'
'n=$(cat "$R/log/dpkg-locked" 2>/dev/null || echo 0); if [ "$n" -gt 0 ]; then\n'
' echo $((n - 1)) >"$R/log/dpkg-locked"\n'
' echo "dpkg: error: dpkg frontend lock was locked by another process with pid 4242 (test)" >&2\n'
' exit 2; fi\n'

'[ ! -e "$R/log/questions" ] || { [ "$DEBIAN_FRONTEND" = noninteractive ] && case "$*" in '
'*--force-confold*) :;; *) false;; esac; } || '
'{ echo "dpkg: error: end of file on stdin at conffile prompt (test)" >&2; exit 1; }\n'
Expand Down Expand Up @@ -139,6 +154,7 @@ class Case(unittest.TestCase):

def setUp(self):
k.SETTLE, k.SETTLE_STEP = self.settle, 0.05
k.LOCK_WAIT, k.LOCK_STEP = 0.3, 0.01 # 300 s and 5 s on a machine
# No power cut in a fake root, so when a write reaches the disk is not tested here: fsync only costs time.
fsync = mock.patch.object(os, 'fsync', lambda fd: None)
fsync.start()
Expand Down
Loading
Loading