Skip to content

fix(system-optimize): disable zswap through systemd - #58

Merged
LucaLin233 merged 2 commits into
mainfrom
forge/fix-zswap-systemd
Oct 23, 2026
Merged

LucaLin233 merged 2 commits into
mainfrom
forge/fix-zswap-systemd

Conversation

@LucaLin233

Copy link
Copy Markdown
Owner

Summary

  • 删除 write_zram_config() 中针对不存在路径 /proc/sys/kernel/zswap/enabled 的死分支;该分支从未生效,zswap 一直保持开启。
  • 新增 one-shot unit linux-setup-disable-zswap.service 与 dev-zram0.swap、swap.target 两个 drop-in,在 swap 激活前写 /sys/module/zswap/parameters/enabled。使用 Wants 而非 Requires:关闭失败不阻断 swap 启动。
  • 新增 zswap 运行值快照(initial/previous)与还原;三个新文件纳入现有备份与恢复清单(五文件 → 八文件)。
  • setup_zram() 分离「配置已生效」与「设备需重建」;容量、活动算法、swap priority 从运行态读取,只有运行确证匹配才清除 zram-reconfigure.pending。
  • show_swap_status() 增加 zswap 状态、compressor、活动算法、mm_stat 压缩比与实际占用比。
  • 测试可加载性与路径隔离:source 不执行主流程;测试模式只接受显式隔离路径,缺失/逃逸即拒绝加载。

Scope

仅 modules/system-optimize.sh 与新增 tests/test-system-optimize.sh。未修改 tests/run.sh、CI、安装器及其他模块。

Validation

  • Ubuntu 24.04 x86_64 沙箱:bash tests/test-system-optimize.sh 129 条断言通过;shellcheck -x --severity=warning modules/system-optimize.sh 与 shellcheck -x --severity=error tests/test-system-optimize.sh 通过(shellcheck 0.9.0;CI 版本以流水线为准)。
  • hkaws(Debian 13 / 7.2.11-x64v3-xanmod1)实机:
    • 安装后 zswap=N,unit active,systemd-analyze verify 无输出,dev-zram0.swap 的 After 含该 unit;
    • 重启后 journal 显示 service Finished 早于 zram swap 激活;
    • 故障注入 unit 失败时,zram swap 仍可正常启动;
    • 再次 install 会 restart 并重跑 ExecStart;
    • restore initial 后两个配置文件与安装前逐字节一致,unit 与 drop-in 删除,zswap 回 Y、swappiness 回 60、swap 仍 active。
  • 完整 tests/run.sh 未在沙箱执行(部分套件依赖 systemd/root),由 CI 覆盖。

Risks

  • 关闭只阻止新页进入 zswap,已有 pool 不会立即释放;不能用 swapoff 追求排空。
  • 内核无 zswap sysfs 节点时 unit 由 ConditionPathExists 跳过,这不是「已验证关闭」。
  • initramfs 中提前激活的 swap 不受该 unit 约束。

Rollback

bash modules/system-optimize.sh restore initial(或 previous):先停 unit,再恢复文件与运行值;不执行 swapoff,设备容量等待安全重启。

FORGE added 2 commits October 24, 2026 01:45
zswap 在多数内核默认开启,旧实现把 kernel.zswap.enabled 写进
sysctl.d 配置,但该键在多数内核不存在,会让整份 sysctl 配置应用失败,
zswap 实际仍然开启。

- 新增 linux-setup-disable-zswap.service 与两个 swap drop-in,在
  dev-zram0.swap/swap.target 之前把 zswap 运行值写为 0 并回读校验;
  Wants 语义保证 unit 失败不会取消 swap 启动。
- 每次 install 都 restart 该 unit(active 时 start 不会重跑 ExecStart),
  失败后由脚本独立回读实际状态,回读仍为 enabled 时记 degraded。
- 保存 zswap/vm.swappiness/vm.page-cluster 运行快照,恢复时按
  stop unit → 恢复文件 → daemon-reload → 恢复运行值的顺序执行。
- 备份/恢复列表从五个文件扩展到八个文件,单个失败不阻断其余文件。
- 配置匹配与设备重建拆分:sysctl、service、drop-in 漂移不再触发
  zram stop/start;有交换页时只落配置并记录 pending。
- 状态展示增加 zswap 状态、compressor/zpool/max_pool_percent、
  活动压缩算法与 mm_stat 压缩比,分母为 0 时显示 N/A。
- 入口改为 source 守卫(不注册 ERR trap、不执行 main),并加入
  LINUX_SETUP_TEST_MODE 下的显式路径覆盖与 fail-closed 校验,
  生产路径保持不变。
- 新增 tests/test-system-optimize.sh:假 sysfs、函数桩与 PATH 桩,
  不调用真实 systemctl/sysctl/modprobe/apt-get/swapon。
Verify the running zram device (disksize, active algorithm, swap priority) before clearing the pending reconfigure marker; fail closed on config writes, daemon-reload, and post-start verification. Propagate restore errors instead of returning success, distinguish a missing unit from a failed query, and check THP snapshot/config writes. Tighten test path isolation for symlinked leaves and directories, and run the suite against real getters.
@LucaLin233
LucaLin233 merged commit 140cd99 into main Oct 23, 2026
16 of 18 checks passed
@LucaLin233
LucaLin233 deleted the forge/fix-zswap-systemd branch October 23, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant