diff --git a/docs/superpowers/contracts/g29-shifter-hid-bpf-2026-09-14.json b/docs/superpowers/contracts/g29-shifter-hid-bpf-2026-09-14.json deleted file mode 100644 index 8263a77d..00000000 --- a/docs/superpowers/contracts/g29-shifter-hid-bpf-2026-09-14.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "title": "G29 Driving Force Shifter: 1st/3rd engage reliably via HID-BPF", - "objective": "The shifter's top row (1st/3rd) rests at hall Y 180-193 on this unit, below the firmware's ~195 engage threshold, so those gears often did not register. A HID-BPF device_event program re-decodes 1st/3rd from the raw X/Y bytes with hysteresis (engage >= 160, release <= 140) and ORs the bit into the button byte before hid-logitech parses it. It never clears a bit, so the firmware's own decode cannot regress. Persistent across boot and hotplug via udev-hid-bpf install, a systemd safety-net unit and an ensure rule. Live on the host since 2026-09-11; this commit puts it under version control.", - "acceptance_criteria": [ - "fix_g29_shifter.sh builds g29_shifter.bpf.c against the udev-hid-bpf headers and --status reports device, persistence, attachment and the safety net", - "g29_shifter_capture.py records raw hidraw reports and grades a capture per gate (attempts, misses); exit 1 on any miss", - "test_g29_shifter_capture.py + test_g29_shifter_capture_cli.py pass (29) with g29_shifter_capture.py at 100% branch coverage", - "every file is under the 250-line cap" - ], - "out_of_scope": [ - "5th gear (no capture data for the top-right gate; left to the firmware)", - "force feedback (separate: DOCS-g29-ffb.md, install_new_lg4ff.sh)" - ], - "verifier": "python3 -m pytest -q linux_configuration/tests/test_g29_shifter_capture.py linux_configuration/tests/test_g29_shifter_capture_cli.py; linux_configuration/fixes/fix_g29_shifter.sh --status" -} diff --git a/docs/superpowers/contracts/swap-switch-stale-grub-cfg-2026-11-27.json b/docs/superpowers/contracts/swap-switch-stale-grub-cfg-2026-11-27.json new file mode 100644 index 00000000..425e5586 --- /dev/null +++ b/docs/superpowers/contracts/swap-switch-stale-grub-cfg-2026-11-27.json @@ -0,0 +1,14 @@ +{ + "title": "setup_zram/setup_zswap repair a stale grub.cfg and fail loudly when the next boot cannot match", + "objective": "A rerun of either swap setup script heals a run cut off between the /etc/default/grub write and grub-mkconfig, stays byte-identical when everything already matches, and exits 1 before touching live swap when grub.cfg or a systemd-boot entry still would not boot the target zswap words.", + "acceptance_criteria": [ + "GRUB defaults on the target, grub.cfg on the old setup: the target's script exits 0 and every grub.cfg linux line carries exactly the target zswap words; the next boot runs the target only", + "grub.cfg already matching: no grub-mkconfig run, boot files and grub.cfg mtime unchanged, nothing reported as changed", + "grub.cfg still wrong after the rebuild, or a loader entry with no options line carrying the words: exit 1 naming the line or entry, no swapoff, zswap enabled unchanged" + ], + "out_of_scope": [ + "The host (systemd-boot, no derived file): no host runs or reboots", + "Rewriting the host's sysctl comment" + ], + "verifier": "bash linux_configuration/fixes/lib/tests/test_swap_switch.sh; vmbox_swap.sh stale-zram|stale-zswap|again|noopts in a fresh BIOS/GRUB guest; pre-commit run --files " +} diff --git a/docs/superpowers/evidence/swap-switch-stale-grub-cfg-2026-11-27.json b/docs/superpowers/evidence/swap-switch-stale-grub-cfg-2026-11-27.json new file mode 100644 index 00000000..d47ffbbe --- /dev/null +++ b/docs/superpowers/evidence/swap-switch-stale-grub-cfg-2026-11-27.json @@ -0,0 +1,35 @@ +{ + "intent": "setup_zram.sh and setup_zswap.sh never exit 0 while the next boot would run a different swap setup: a stale grub.cfg is rebuilt, and an unfixable boot config fails loudly before live swap changes.", + "scope": [ + "linux_configuration/fixes/lib/swap_switch.sh", + "linux_configuration/fixes/lib/tests/swap_switch_harness.sh, test_swap_switch.sh, vmbox_swap.sh", + "linux_configuration/fixes/DOCS-zswap-vs-zram.md", + "Non-goal: the host (systemd-boot) is not touched or rebooted" + ], + "changes": [ + "_ss_set_grub rebuilds grub.cfg when the defaults changed OR any grub.cfg linux line does not carry exactly the target zswap words (missing file counts), then re-checks and exits 1 naming the offending line", + "ss_set_boot_tokens checks every systemd-boot entry's options lines carry the target words and exits 1 naming the entry otherwise", + "Harness grub-mkconfig stub writes an Arch-like grub.cfg (tab-indented linux lines, recovery from GRUB_CMDLINE_LINUX); snapshot includes grub.cfg", + "vmbox_swap.sh: new stale-zram, stale-zswap and noopts phases; grub.cfg checks every linux line; again compares grub.cfg mtime" + ], + "verification": [ + { + "command": "~/.claude/scripts/capped.sh bash linux_configuration/fixes/lib/tests/test_swap_switch.sh", + "result": "pass", + "evidence": "Before the fix: 53 passed, 15 failed (stale grub.cfg not rebuilt, missing options line exited 0). After: 68 passed, 0 failed" + }, + { + "command": "vm run zc3 'sudo bash ~/wt/linux_configuration/fixes/lib/tests/vmbox_swap.sh ' (fresh BIOS/GRUB guest zc3 after vm hibernate-setup)", + "result": "pass", + "evidence": "zswap -> reboot -> booted-zswap OK; stale-zram RC=0 (precondition grub.cfg boots zswap; all 2 linux lines carry zswap.enabled=0) -> reboot -> booted-zram OK (cmdline zswap.enabled=0, zram0 prio 100 zstd, zswap N) -> again OK (md5, mtime, swap list unchanged); stale-zswap RC=0 (all 2 lines carry the 4 tokens) -> reboot -> booted-zswap OK -> again OK; noopts on zram and on zswap: exit 1 naming b.conf, GRUB files, swap list and zswap enabled unchanged" + } + ], + "risks": [ + "A grub.cfg whose kernel lines are not named `linux` (linux16/linuxefi) would read as having no matching line and be rebuilt each run, then fail loudly", + "A systemd-boot UKI entry without an options line now makes the scripts exit 1 instead of silently skipping it" + ], + "rollback": [ + "git revert the commit; setup_*.sh go back to rebuilding grub.cfg only when /etc/default/grub changed", + "Rerun bash linux_configuration/fixes/lib/tests/test_swap_switch.sh after the revert" + ] +} diff --git a/linux_configuration/fixes/DOCS-zswap-vs-zram.md b/linux_configuration/fixes/DOCS-zswap-vs-zram.md index 59095bea..0dfeb7eb 100644 --- a/linux_configuration/fixes/DOCS-zswap-vs-zram.md +++ b/linux_configuration/fixes/DOCS-zswap-vs-zram.md @@ -187,6 +187,12 @@ date env setup seed size_mib pages fill_crc touch_crc seconds ratio psi_some_ms needed. Pages still in the zswap pool and on the swapfiles (about 17 GB of slots) come back as they fault in; zram takes all new swap-outs again. +- Host sysctl comment, left on purpose: the host's + `/etc/sysctl.d/90-desktop-performance.conf` (mtime 2026-11-22) still says + the old zram-only comment. The values in it are the current ones; only + `optimize_arch_desktop.sh` rewrites the comment, and a full optimize run + on the host is not worth a comment. + ## vmbox checks `lib/tests/vmbox_swap.sh` (phases, run inside a BIOS/GRUB guest): @@ -195,8 +201,24 @@ date env setup seed size_mib pages fill_crc touch_crc seconds ratio psi_some_ms and `Y`. After a reboot `/proc/cmdline` carries all four tokens. - **Revert zswap → zram:** zram0 is back at prio 100 with zstd, zswap reads `N`, and after a reboot `/proc/cmdline` has `zswap.enabled=0`. -- **Rerun on the same state:** the GRUB files hash the same, the swap list - is unchanged and nothing is reported as changed. +- **Rerun on the same state:** the GRUB files hash the same, grub.cfg keeps + its mtime, the swap list is unchanged and nothing is reported as changed. +- **Half-finished switch is repaired (2026-11-27, guest `zc3`):** a run cut + off after `/etc/default/grub` but before grub-mkconfig used to leave + grub.cfg booting the old setup, and every rerun exited 0 (zswap `Y` plus + zram0 after the next boot). The scripts now rebuild grub.cfg whenever a + `linux` line does not carry exactly the target zswap words. `stale-zram` + (defaults `zswap.enabled=0`, grub.cfg on the zswap tokens) and + `stale-zswap` (the reverse) each exited 0 with both `linux` lines + rebuilt, and each reboot came up on the target: zram0 at prio 100 with + zswap `N`, then the four tokens with no zram0. +- **Next boot that cannot match fails loudly:** when grub.cfg is still wrong + after the rebuild (zswap words in `GRUB_CMDLINE_LINUX`), or a + systemd-boot entry has no `options` line carrying the words, the script + exits 1 and names the line or entry before it touches live swap. + `noopts` (one entry without `options`) exited 1 naming `b.conf`, with + the swap list, zswap `enabled` and the GRUB files unchanged, on zram and + on zswap. - **Exit 3:** zram held 1.7 GB of a 5 GB tmpfs file and MemAvailable was 50 MB. `setup_zswap.sh` exited 3 with "reboot to apply", leaving zram0 active and zswap off. The next boot came up on zswap. diff --git a/linux_configuration/fixes/lib/swap_switch.sh b/linux_configuration/fixes/lib/swap_switch.sh index 5a3b91e0..7dfbd606 100755 --- a/linux_configuration/fixes/lib/swap_switch.sh +++ b/linux_configuration/fixes/lib/swap_switch.sh @@ -100,11 +100,64 @@ _ss_rewrite() { echo changed } +# _ss_zswap_words — filter stdin: print a cmdline's zswap.* words, in order. +# Splits on spaces and tabs, so a grub.cfg `linux` line works as is. +_ss_zswap_words() { + local word out="" + local -a words + read -r -a words + for word in "${words[@]}"; do + if [[ $word == zswap.* ]]; then out+=" ${word}"; fi + done + printf '%s\n' "${out# }" +} + +# _ss_grub_cfg_problem TOKENS — print why grub.cfg would not boot TOKENS; +# nothing when it would. Every `linux` line with zswap.* words must carry +# exactly TOKENS and at least one must: a recovery line, built from +# GRUB_CMDLINE_LINUX alone, carries none. Read as root: grub.cfg can be 600. +_ss_grub_cfg_problem() { + local tokens="$1" cfg line words matched=0 + if ! cfg="$(as_root cat "$GRUB_CFG_FILE" 2>/dev/null)"; then + echo "$GRUB_CFG_FILE is missing" + return 0 + fi + while IFS= read -r line; do + [[ $line =~ ^[[:space:]]*linux[[:space:]] ]] || continue + words="$(_ss_zswap_words <<<"$line")" + [[ -n $words ]] || continue + if [[ $words != "$tokens" ]]; then + printf 'this line boots "%s": %s\n' "$words" "${line#"${line%%[![:space:]]*}"}" + return 0 + fi + matched=1 + done <<<"$cfg" + ((matched)) || echo "no linux line in $GRUB_CFG_FILE carries $tokens" +} + +# _ss_set_grub TOKENS — GRUB's defaults, then grub.cfg: rebuilt when the +# defaults changed OR it does not boot TOKENS yet. The second case heals a +# run cut off between the two writes, which left the defaults right and +# grub.cfg on the old setup. Prints "changed" when it wrote anything. +_ss_set_grub() { + local tokens="$1" changed log problem + changed="$(_ss_rewrite "$GRUB_DEFAULT_FILE" grub "$tokens")" + [[ -n $changed || -n $(_ss_grub_cfg_problem "$tokens") ]] || return 0 + # grub-mkconfig narrates every step on stderr; show it only on failure. + log="$(as_root grub-mkconfig -o "$GRUB_CFG_FILE" 2>&1)" || + ss_die "grub-mkconfig failed: $log" + problem="$(_ss_grub_cfg_problem "$tokens")" + [[ -z $problem ]] || ss_die "grub.cfg still wrong after grub-mkconfig, $problem" \ + "(zswap.* words belong only in GRUB_CMDLINE_LINUX_DEFAULT; check GRUB_CMDLINE_LINUX in $GRUB_DEFAULT_FILE)" + echo changed +} + # ss_set_boot_tokens TOKENS — persist the zswap.* words for the next boot: -# every systemd-boot entry when there are any, else GRUB's defaults (plus a -# grub.cfg rebuild when they changed). Other words, resume= included, stay. +# every systemd-boot entry when there are any, else GRUB. Other words, +# resume= included, stay. Exits 1 when the next boot would still not carry +# TOKENS, before the caller touches any live swap. ss_set_boot_tokens() { - local tokens="$1" entry changed="" + local tokens="$1" entry changed="" bad="" local -a entries=() if [[ -d $LOADER_ENTRIES_DIR ]]; then mapfile -t entries < <(find "$LOADER_ENTRIES_DIR" -maxdepth 1 -name '*.conf' | sort) @@ -112,15 +165,12 @@ ss_set_boot_tokens() { if ((${#entries[@]})); then for entry in "${entries[@]}"; do changed+="$(_ss_rewrite "$entry" loader "$tokens")" + [[ $(grep '^options ' "$entry" | tr '\n' ' ' | _ss_zswap_words) == "$tokens" ]] || + bad+=" $(basename "$entry")" done + [[ -z $bad ]] || ss_die "no options line carrying \"$tokens\" in${bad} ($LOADER_ENTRIES_DIR); fix the entry by hand" elif [[ -f $GRUB_DEFAULT_FILE ]]; then - changed="$(_ss_rewrite "$GRUB_DEFAULT_FILE" grub "$tokens")" - if [[ -n $changed ]]; then - # grub-mkconfig narrates every step on stderr; show it only on failure. - local log - log="$(as_root grub-mkconfig -o "$GRUB_CFG_FILE" 2>&1)" || - ss_die "grub-mkconfig failed: $log" - fi + changed="$(_ss_set_grub "$tokens")" else ss_die "no systemd-boot entries in $LOADER_ENTRIES_DIR and no $GRUB_DEFAULT_FILE" fi diff --git a/linux_configuration/fixes/lib/tests/swap_switch_harness.sh b/linux_configuration/fixes/lib/tests/swap_switch_harness.sh index 7e86ceda..d63576a4 100755 --- a/linux_configuration/fixes/lib/tests/swap_switch_harness.sh +++ b/linux_configuration/fixes/lib/tests/swap_switch_harness.sh @@ -82,9 +82,26 @@ _sw_machine() { _t_reset_calls } +# _sw_grub STATE — the _sw_machine STATE machine, booted by GRUB instead of +# systemd-boot: defaults carrying STATE's zswap words, grub.cfg built from them. +_sw_grub() { + local words="zswap.enabled=0" + _sw_machine "$1" + [[ $1 == zswap ]] && words="zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=35 zswap.shrinker_enabled=1" + rm -rf "$LOADER_ENTRIES_DIR" + mkdir -p "${GRUB_DEFAULT_FILE%/*}" "${GRUB_CFG_FILE%/*}" + printf 'GRUB_TIMEOUT=1\nGRUB_CMDLINE_LINUX=""\nGRUB_CMDLINE_LINUX_DEFAULT="loglevel=3 resume=/dev/vda2 %s"\n' \ + "$words" >"$GRUB_DEFAULT_FILE" + grub-mkconfig -o "$GRUB_CFG_FILE" + _t_reset_calls +} + +# _sw_cfg_lines PATTERN — how many grub.cfg `linux` lines match PATTERN. +_sw_cfg_lines() { grep -P '^\s*linux\s' "$GRUB_CFG_FILE" | grep -c -- "$1"; } + # _sw_snapshot — checksum of every boot-config file, for byte-identity checks. _sw_snapshot() { - cat "${LOADER_ENTRIES_DIR}"/*.conf "$GRUB_DEFAULT_FILE" 2>/dev/null | md5sum + cat "${LOADER_ENTRIES_DIR}"/*.conf "$GRUB_DEFAULT_FILE" "$GRUB_CFG_FILE" 2>/dev/null | md5sum } # _sw_param NAME — current fake zswap parameter. @@ -97,7 +114,19 @@ exec "$@" STUB _t_stub udevadm 'exit 0' _t_stub pacman 'exit 0' - _t_stub grub-mkconfig 'exit 0' + # Writes grub.cfg the way Arch's does: tab-indented `linux` lines, the + # default and fallback entries get both cmdline variables, recovery only + # GRUB_CMDLINE_LINUX. + _t_stub_stdin grub-mkconfig <<'STUB' +[[ $1 == -o ]] || exit 2 +. "$GRUB_DEFAULT_FILE" +both="${GRUB_CMDLINE_LINUX:-} ${GRUB_CMDLINE_LINUX_DEFAULT:-}" +{ + printf 'menuentry "Arch Linux" {\n\tlinux\t/vmlinuz-linux root=UUID=x rw %s\n}\n' "$both" + printf 'menuentry "Arch Linux (fallback)" {\n\tlinux\t/vmlinuz-linux root=UUID=x rw %s\n}\n' "$both" + printf 'menuentry "Arch Linux (recovery)" {\n\tlinux\t/vmlinuz-linux root=UUID=x rw single %s\n}\n' "${GRUB_CMDLINE_LINUX:-}" +} >"$2" +STUB _t_stub_stdin swapon <<'STUB' swaps="${LIB_TEST_DEV}/swaps" case "$*" in diff --git a/linux_configuration/fixes/lib/tests/test_swap_switch.sh b/linux_configuration/fixes/lib/tests/test_swap_switch.sh index 1d503c24..6587db1f 100755 --- a/linux_configuration/fixes/lib/tests/test_swap_switch.sh +++ b/linux_configuration/fixes/lib/tests/test_swap_switch.sh @@ -83,7 +83,7 @@ _t_contains "$out" "no disk swap" "setup_zswap: names the missing backing swap" echo "GRUB machine" _sw_machine zram rm -rf "$LOADER_ENTRIES_DIR" -mkdir -p "${GRUB_DEFAULT_FILE%/*}" +mkdir -p "${GRUB_DEFAULT_FILE%/*}" "${GRUB_CFG_FILE%/*}" printf 'GRUB_TIMEOUT=1\nGRUB_CMDLINE_LINUX_DEFAULT="loglevel=3 resume=/dev/vda2"\n' >"$GRUB_DEFAULT_FILE" _t_run bash "$ZSWAP" _t_eq 0 "$?" "setup_zswap grub: exits 0" @@ -102,7 +102,7 @@ _t_stub grub-mkconfig 'echo boom >&2; exit 1' _t_run bash "$ZSWAP" _t_eq 1 "$?" "setup_zswap grub: grub-mkconfig failure exits 1" _t_contains "$out" "grub-mkconfig failed: boom" "setup_zswap grub: shows grub-mkconfig's output" -_t_stub grub-mkconfig 'exit 0' +_sw_stubs echo "neither bootloader: refuse" _sw_machine zram @@ -117,5 +117,60 @@ _t_eq 0 "$?" "setup_zram ZRAM_SIZE_MIB: exits 0" _t_eq $((4370 * 1024 * 1024)) "$(cat "${ZRAM_SYSFS}/disksize")" "setup_zram ZRAM_SIZE_MIB: zram0 sized to it" _t_contains "$(cat "$ZRAM_CONF")" "zram-size = 4370" "setup_zram ZRAM_SIZE_MIB: conf carries it" +echo "stale grub.cfg is rebuilt, zswap -> zram (C3)" +_sw_grub zswap +sed -i "s/${ZSWAP_TOKENS}/zswap.enabled=0/" "$GRUB_DEFAULT_FILE" +_t_run bash "$ZRAM" +_t_eq 0 "$?" "setup_zram stale grub.cfg: exits 0" +_t_contains "$(_t_calls)" "grub-mkconfig -o ${GRUB_CFG_FILE}" "setup_zram stale grub.cfg: rebuilt it" +_t_eq 2 "$(_sw_cfg_lines 'zswap.enabled=0')" "setup_zram stale grub.cfg: both linux lines boot zswap.enabled=0" +_t_eq 0 "$(_sw_cfg_lines 'zswap.enabled=1')" "setup_zram stale grub.cfg: no line boots zswap" +_t_contains "$out" "cmdline now carries" "setup_zram stale grub.cfg: reports the cmdline change" + +echo "stale grub.cfg is rebuilt, zram -> zswap" +_sw_grub zram +sed -i "s/zswap.enabled=0/${ZSWAP_TOKENS}/" "$GRUB_DEFAULT_FILE" +_t_run bash "$ZSWAP" +_t_eq 0 "$?" "setup_zswap stale grub.cfg: exits 0" +_t_eq 2 "$(_sw_cfg_lines "$ZSWAP_TOKENS")" "setup_zswap stale grub.cfg: both linux lines boot the zswap tokens" +_t_eq 0 "$(_sw_cfg_lines 'zswap.enabled=0')" "setup_zswap stale grub.cfg: no line boots zswap.enabled=0" + +echo "matching grub.cfg: no rebuild (C4)" +_sw_grub zswap +before="$(_sw_snapshot)" +_t_run bash "$ZSWAP" +_t_eq 0 "$?" "setup_zswap grub.cfg current: exits 0" +_t_lacks "$(_t_calls)" "grub-mkconfig" "setup_zswap grub.cfg current: no grub-mkconfig" +_t_eq "$before" "$(_sw_snapshot)" "setup_zswap grub.cfg current: boot config byte-identical" +_t_lacks "$out" "cmdline now" "setup_zswap grub.cfg current: reports no change" + +echo "missing grub.cfg is rebuilt" +_sw_grub zram +rm -f "$GRUB_CFG_FILE" +_t_run bash "$ZRAM" +_t_eq 0 "$?" "setup_zram no grub.cfg: exits 0" +_t_eq 2 "$(_sw_cfg_lines 'zswap.enabled=0')" "setup_zram no grub.cfg: rebuilt with zswap.enabled=0" + +echo "zswap words grub-mkconfig cannot fix: loud" +_sw_grub zram +sed -i 's/^GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="zswap.enabled=1"/' "$GRUB_DEFAULT_FILE" +grub-mkconfig -o "$GRUB_CFG_FILE" # the box booted with that cmdline already +_t_reset_calls +_t_run bash "$ZRAM" +_t_eq 1 "$?" "setup_zram GRUB_CMDLINE_LINUX: exits 1" +_t_contains "$out" "GRUB_CMDLINE_LINUX" "setup_zram GRUB_CMDLINE_LINUX: names where the words come from" +_t_contains "$out" "/vmlinuz-linux" "setup_zram GRUB_CMDLINE_LINUX: shows the offending linux line" +_t_lacks "$(_t_calls)" "swapoff" "setup_zram GRUB_CMDLINE_LINUX: no swapoff" + +echo "loader entry without options: loud" +_sw_machine zswap +sed -i '/^options /d' "${LOADER_ENTRIES_DIR}/linux-lts.conf" +_t_run bash "$ZRAM" +_t_eq 1 "$?" "setup_zram no options line: exits 1" +_t_contains "$out" "linux-lts.conf" "setup_zram no options line: names the entry" +_t_lacks "$(_t_calls)" "swapoff" "setup_zram no options line: no swapoff" +_t_lacks "$(_t_calls)" "systemd-zram-setup" "setup_zram no options line: zram0 not started" +_t_eq Y "$(_sw_param enabled)" "setup_zram no options line: zswap still on" + printf '\n%d passed, %d failed\n' "$PASS" "$FAIL" [[ $FAIL -eq 0 ]] diff --git a/linux_configuration/fixes/lib/tests/vmbox_swap.sh b/linux_configuration/fixes/lib/tests/vmbox_swap.sh index a7cb7685..c85d6ba6 100755 --- a/linux_configuration/fixes/lib/tests/vmbox_swap.sh +++ b/linux_configuration/fixes/lib/tests/vmbox_swap.sh @@ -16,6 +16,9 @@ # vm run zsw 'sudo systemctl reboot'; vm start zsw; vm run zsw "$C booted-zswap" # vm run zsw "$C zram"; vm run zsw "$C fill"; vm run zsw "$C exit3" # vm run zsw 'sudo systemctl reboot'; vm start zsw; vm run zsw "$C booted-zswap" +# vm run zsw "$C stale-zram"; reboot as above; vm run zsw "$C booted-zram" +# vm run zsw "$C again"; vm run zsw "$C stale-zswap"; reboot as above +# vm run zsw "$C booted-zswap"; vm run zsw "$C again"; vm run zsw "$C noopts" # # The guest's zram device is scaled to the same RAM budget as the host's # 32 GiB on ~30 GiB: 35 % of RAM compressed at the 3.2:1 the bench measures. @@ -36,12 +39,25 @@ bad() { } # want WHAT EXPECTED ACTUAL want() { if [[ $3 == "$2" ]]; then ok "$1"; else bad "$1 (want '$2', got '$3')"; fi; } +# some WHAT COUNT — COUNT is above zero +some() { if (($2 > 0)); then ok "$1"; else bad "$1 (got $2)"; fi; } # has WHAT HAYSTACK NEEDLE has() { if [[ $2 == *"$3"* ]]; then ok "$1"; else bad "$1 (no '$3' in: $2)"; fi; } zram_mib() { awk '/^MemTotal:/ {print int($2 / 1024 * 35 * 32 / 1000)}' /proc/meminfo; } zram_swap() { swapon --noheadings --show=NAME,PRIO | awk '$1 == "/dev/zram0" {print $2}'; } grub_line() { grep '^GRUB_CMDLINE_LINUX_DEFAULT=' /etc/default/grub; } +# cfg_lines PATTERN — how many grub.cfg `linux` lines carry PATTERN. +cfg_lines() { grep -P '^\s*linux\s' /boot/grub/grub.cfg | grep -c -- "$1" || true; } + +# check_cfg WANT NOT — every grub.cfg `linux` line carries WANT, none NOT. +check_cfg() { + local total + total="$(cfg_lines vmlinuz)" + some "grub.cfg has linux lines" "$total" + want "grub.cfg: all ${total} linux lines carry '$1'" "$total" "$(cfg_lines "$1")" + want "grub.cfg: no linux line carries '$2'" 0 "$(cfg_lines "$2")" +} check_zram_live() { want "zram0 is swap at prio 100" 100 "$(zram_swap)" @@ -61,14 +77,14 @@ phase_zram() { ZRAM_SIZE_MIB="$(zram_mib)" bash "${FIXES}/setup_zram.sh" check_zram_live has "GRUB defaults carry zswap.enabled=0" "$(grub_line)" "zswap.enabled=0" - has "grub.cfg carries zswap.enabled=0" "$(cat /boot/grub/grub.cfg)" "zswap.enabled=0" + check_cfg "zswap.enabled=0" "zswap.enabled=1" } phase_zswap() { bash "${FIXES}/setup_zswap.sh" check_zswap_live has "GRUB defaults carry the zswap tokens" "$(grub_line)" "$ZSWAP_TOKENS" - has "grub.cfg carries the zswap tokens" "$(cat /boot/grub/grub.cfg)" "$ZSWAP_TOKENS" + check_cfg "$ZSWAP_TOKENS" "zswap.enabled=0" has "resume= kept" "$(grub_line)" "resume_offset=" } @@ -88,11 +104,13 @@ phase_again() { [[ -n $(zram_swap) ]] && script=setup_zram.sh before="$( md5sum /etc/default/grub /boot/grub/grub.cfg + stat -c '%Y %n' /boot/grub/grub.cfg swapon --show )" out="$(ZRAM_SIZE_MIB="$(zram_mib)" bash "${FIXES}/${script}" 2>&1)" after="$( md5sum /etc/default/grub /boot/grub/grub.cfg + stat -c '%Y %n' /boot/grub/grub.cfg swapon --show )" want "${script} rerun: GRUB files and swap list unchanged" "$before" "$after" @@ -120,6 +138,47 @@ phase_exit3() { has "GRUB defaults already carry the zswap tokens" "$(grub_line)" "$ZSWAP_TOKENS" } +# stale-zram / stale-zswap — the half-finished run C3 caught: GRUB defaults +# already name the target setup, grub.cfg still boots the old one. A rerun +# of the target's script must rebuild grub.cfg and exit 0. +phase_stale_zram() { + some "precondition: grub.cfg boots zswap" "$(cfg_lines zswap.enabled=1)" + sed -i "s/${ZSWAP_TOKENS}/zswap.enabled=0/" /etc/default/grub + phase_zram +} + +phase_stale_zswap() { + some "precondition: grub.cfg boots zswap.enabled=0" "$(cfg_lines zswap.enabled=0)" + sed -i "s/zswap.enabled=0/${ZSWAP_TOKENS}/" /etc/default/grub + phase_zswap +} + +# noopts — a loader entry without an options line: loud exit, live swap and +# the GRUB files untouched. Runs against a scratch entries dir. +phase_noopts() { + local dir=/root/le-test rc=0 out before after + rm -rf "$dir" /root/le-test-bak + mkdir -p "$dir" + printf 'title a\nlinux /vmlinuz-linux\noptions root=/dev/vda1 rw\n' >"${dir}/a.conf" + printf 'title b\nlinux /vmlinuz-linux\n' >"${dir}/b.conf" + before="$( + md5sum /etc/default/grub /boot/grub/grub.cfg + swapon --show + cat "$P/enabled" + )" + out="$(LOADER_ENTRIES_DIR="$dir" SWAP_SWITCH_BACKUP_DIR=/root/le-test-bak \ + ZRAM_SIZE_MIB="$(zram_mib)" bash "${FIXES}/setup_zram.sh" 2>&1)" || rc=$? + after="$( + md5sum /etc/default/grub /boot/grub/grub.cfg + swapon --show + cat "$P/enabled" + )" + want "setup_zram exits 1 on an entry without options" 1 "$rc" + has "setup_zram names the entry" "$out" "b.conf" + want "GRUB files, swap list and zswap enabled unchanged" "$before" "$after" + rm -rf "$dir" /root/le-test-bak +} + main() { [[ $EUID -eq 0 ]] || { echo "run as root inside the guest" >&2 @@ -133,8 +192,11 @@ main() { again) phase_again ;; fill) phase_fill ;; exit3) phase_exit3 ;; + stale-zram) phase_stale_zram ;; + stale-zswap) phase_stale_zswap ;; + noopts) phase_noopts ;; *) - echo "usage: $0 zram|zswap|booted-zram|booted-zswap|again|fill|exit3" >&2 + echo "usage: $0 zram|zswap|booted-zram|booted-zswap|again|fill|exit3|stale-zram|stale-zswap|noopts" >&2 exit 2 ;; esac