Grow zram swap to 32 GiB via zram-generator

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWUvCN36e1dx3Ejii1p
This commit is contained in:
Krzysztof Rudnicki 2026-11-22 14:34:53 +01:00
parent 86cf02bbe7
commit 1c193f78c3
3 changed files with 131 additions and 0 deletions

View File

@ -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"
]
}

View File

@ -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 "$@"

View File

@ -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