testsAndMisc/linux_configuration/fixes/setup_zswap.sh
Krzysztof kuhy Rudnicki 5e0d8727d6 A/B zswap vs zram: zram stays the default
Add setup_zswap.sh and make setup_zram.sh its one-command revert
(shared lib/swap_switch.sh: systemd-boot/GRUB cmdline, live switch or
exit 3 "reboot to apply"). Add swap_bench.sh + python_pkg/swap_bench, a
seeded memory-pressure benchmark. 3 runs x 2 setups in vmbox and on the
host: zram has lower median PSI full in both (VM 5996 vs 10360 ms, host
7427 vs 8973 ms), so the host keeps booting with zram. Results in
linux_configuration/fixes/DOCS-zswap-vs-zram.md.

Bug-class-check: none — new feature and measurement, not a bug fix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 12:34:25 +01:00

51 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# Switch swap to zswap: compressed RAM cache in front of the disk swapfiles.
#
# Persists zswap.enabled=1, zstd, a 35% pool cap (the RAM budget the 32 GiB
# zram device gets at ~3:1) and the shrinker on the kernel cmdline, removes
# the zram-generator conf, then switches live: swapoff zram0 first, zswap on
# after, so no page is ever compressed twice. When MemAvailable cannot absorb
# what zram holds, it leaves the live state alone and exits 3: reboot to
# apply. resume= stays on /swapfile. setup_zram.sh is the one-command revert.
#
# Env overrides (tests/VM): ZRAM_CONF plus those in lib/swap_switch.sh.
set -euo pipefail
SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SRC_DIR
readonly SS_NAME=setup_zswap
readonly ZRAM_CONF="${ZRAM_CONF:-/etc/systemd/zram-generator.conf}"
readonly ZSWAP_TOKENS="zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=35 zswap.shrinker_enabled=1"
# shellcheck source=lib/swap_switch.sh
. "${SRC_DIR}/lib/swap_switch.sh"
# Drop the zram-generator conf so the next boot makes no zram0. The package
# stays installed: setup_zram.sh reinstalls the conf to revert.
disable_zram_conf() {
[[ -e $ZRAM_CONF ]] || return 0
as_root mkdir -p "$SWAP_SWITCH_BACKUP_DIR"
as_root cp -p "$ZRAM_CONF" "${SWAP_SWITCH_BACKUP_DIR}/zram-generator.conf.last"
as_root rm -f "$ZRAM_CONF"
as_root systemctl daemon-reload
}
main() {
ss_has_disk_swap || ss_die "no disk swap is active; zswap needs one to back it"
ss_set_boot_tokens "$ZSWAP_TOKENS"
disable_zram_conf
if ss_zram_active; then
ss_can_drain || ss_reboot_to_apply "zram0 holds more than MemAvailable can absorb"
as_root swapoff "$ZRAM_DEV"
# ExecStop resets the device and frees its memory.
as_root systemctl stop systemd-zram-setup@zram0.service
fi
ss_zswap_set compressor zstd
ss_zswap_set max_pool_percent 35
ss_zswap_set shrinker_enabled Y
ss_zswap_set enabled Y
ss_report
}
main "$@"
exit $?