diff --git a/docs/superpowers/evidence/zram-32g-2026-11-22.json b/docs/superpowers/evidence/zram-32g-2026-11-22.json new file mode 100644 index 00000000..14beba81 --- /dev/null +++ b/docs/superpowers/evidence/zram-32g-2026-11-22.json @@ -0,0 +1,37 @@ +{ + "intent": "Grow zram swap from 4 GiB to 32 GiB, permanently, with disk swapfiles kept as lower-priority fallback.", + "scope": [ + "linux_configuration/fixes/setup_zram.sh and zram-generator.conf", + "oom-prevention skill wording only", + "non-goal: swappiness, swapfiles, other sysctl" + ], + "changes": [ + "Add zram-generator.conf (zram-size 32 GiB, zstd, priority 100)", + "Add setup_zram.sh: installs the conf, guards MemAvailable, swapoff, settle+retry reset, recreate" + ], + "verification": [ + { + "command": "vm run zr1 (RED zramctl 1.9G -> bash setup_zram.sh -> GREEN 32G, survives reboot)", + "result": "pass", + "evidence": "vmbox guest: DISKSIZE 32G, swapon zram0 prio 100 above swapfile -1 before and after reboot" + }, + { + "command": "fixes/setup_zram.sh on host; zramctl; swapon --show", + "result": "pass", + "evidence": "host zram0 32G prio 100, swapfile2/swapfile prio -1" + }, + { + "command": "shellcheck and shfmt -d fixes/setup_zram.sh", + "result": "pass", + "evidence": "no output" + } + ], + "risks": [ + "A full zram holds ~11 GiB of RAM at 3:1 zstd", + "Host swapoff raced udev once (reset busy); script now settles and retries" + ], + "rollback": [ + "Restore /etc/systemd/zram-generator.conf to a bare [zram0] section and rerun swapoff plus systemd-zram-setup@zram0 restart", + "Validate zramctl shows 4G" + ] +} diff --git a/linux_configuration/fixes/setup_zram.sh b/linux_configuration/fixes/setup_zram.sh new file mode 100755 index 00000000..0e21cc11 --- /dev/null +++ b/linux_configuration/fixes/setup_zram.sh @@ -0,0 +1,85 @@ +#!/bin/bash +# Install fixes/zram-generator.conf and resize the live /dev/zram0 to match. +# +# Resizing needs the device reset, i.e. swapoff first: the zram pages move back +# into RAM (disk swap is NOT a target of swapoff), so the script refuses when +# MemAvailable could not absorb them. Disk swapfiles are left alone. +# +# Env overrides (tests/VM): ZRAM_CONF, ZRAM_WANT_BYTES, ZRAM_SKIP_APPLY=1. +set -euo pipefail + +SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +readonly SRC_DIR +readonly ZRAM_CONF="${ZRAM_CONF:-/etc/systemd/zram-generator.conf}" +readonly ZRAM_WANT_BYTES="${ZRAM_WANT_BYTES:-34359738368}" # 32 GiB +readonly DEV=/dev/zram0 +readonly MIN_SPARE_KIB=$((2 * 1024 * 1024)) # keep 2 GiB free after swapoff + +die() { + echo "setup_zram: $*" >&2 + exit 1 +} + +as_root() { + if [[ $EUID -eq 0 ]]; then "$@"; else sudo -n "$@"; fi +} + +ensure_package() { + [[ -x /usr/lib/systemd/system-generators/zram-generator ]] && return 0 + as_root pacman -S --needed --noconfirm zram-generator +} + +disksize() { + # sysfs is world-readable; blockdev needs root. Absent device reads as 0. + cat /sys/block/zram0/disksize 2>/dev/null || echo 0 +} + +# KiB of RAM the swapoff will newly occupy: uncompressed data minus the +# compressed pages that get freed as it drains. +swapoff_cost_kib() { + local line orig total + line=$(zramctl --bytes --noheadings --raw -o DATA,TOTAL "$DEV") + read -r orig total <<<"$line" + echo $(((orig - total) / 1024)) +} + +mem_available_kib() { + awk '/^MemAvailable:/ {print $2}' /proc/meminfo +} + +resize_live() { + if [[ -b $DEV ]] && swapon --noheadings --show=NAME | grep -qx "$DEV"; then + local cost avail + cost=$(swapoff_cost_kib) + avail=$(mem_available_kib) + ((avail - cost >= MIN_SPARE_KIB)) || + die "MemAvailable ${avail}K too low to drain zram (needs ${cost}K + 2G spare)" + as_root swapoff "$DEV" + fi + # udev probes the freed device after swapoff and holds it open for a moment + # ("Device or resource busy" on reset), so settle and retry the restart: + # ExecStop resets the device, ExecStart recreates it from the new conf. + local try + for try in 1 2 3 4 5; do + as_root udevadm settle + as_root systemctl restart systemd-zram-setup@zram0.service && break + ((try < 5)) || die "zram0 stayed busy after 5 resets" + sleep 1 + done + as_root systemctl start dev-zram0.swap 2>/dev/null || true +} + +main() { + ensure_package + as_root install -D -m 644 "$SRC_DIR/zram-generator.conf" "$ZRAM_CONF" + [[ ${ZRAM_SKIP_APPLY:-0} == 1 ]] && return 0 + as_root systemctl daemon-reload + if [[ $(disksize) -ne $ZRAM_WANT_BYTES ]]; then + resize_live + fi + [[ $(disksize) -eq $ZRAM_WANT_BYTES ]] || die "zram0 is $(disksize) bytes, want $ZRAM_WANT_BYTES" + zramctl + swapon --show +} + +main "$@" diff --git a/linux_configuration/fixes/zram-generator.conf b/linux_configuration/fixes/zram-generator.conf new file mode 100644 index 00000000..0b5ac9e8 --- /dev/null +++ b/linux_configuration/fixes/zram-generator.conf @@ -0,0 +1,9 @@ +# zram swap for zram-generator -- installed by fixes/setup_zram.sh. +# +# zram-size is the uncompressed capacity. At ~3:1 zstd a full 32 GiB device +# holds about 11 GiB of RAM. Priority 100 puts it above the disk swapfiles +# (priority -1), which stay as the overflow fallback. +[zram0] +zram-size = 32 * 1024 +compression-algorithm = zstd +swap-priority = 100