Leadaxe:为小闪存路由器提出 sing-box-lx 分阶段更新方案

Leadaxe 记录 lxd update 需在原文件旁保留两份完整二进制,导致小闪存 OpenWrt 路由器空间检查无法通过。作者提出在 tmpfs 下载验证、压缩保存回滚副本、停止服务后替换并健康检查,且需处理脱离 daemon、压缩文件系统和更长停机时间;另建议发布 arm64 UPX 包。以上是方案,尚未宣称实现。

作者原文

Problem

sing-box lxd update cannot update routers whose /usr/bin lives on a small flash overlay (no extroot). Seen on two OpenWrt arm64 boxes (ubifs overlay, 70.8 MB partition), lx.33 → lx.35:

update: installed 1.14.4-lx.33, available 1.14.4-lx.35 (linux-arm64)
update: ERROR: need 81838 KiB free beside /usr/bin/sing-box, have 35516

The plain linux-arm64 binary is ~76 MB. runUpdate (lxd/update.go) streams the new binary into <target>.new on the same filesystem and keeps the old inode as a hard link <target>.old for rollback, so it needs room for two full copies at once. The room check (lxd/update.go:446) wants size + 10% free — more than the whole partition. No amount of cleanup makes it pass; the only way today is a manual swap over ssh.

The UPX fallback (*.upx.tar.gz, packedWithUPX) is meant for exactly this case, but releases currently publish no UPX asset for linux-arm64, and a box already running the plain binary would never switch to it anyway.

Proposal: low-space mode

When room() fails, instead of exiting, switch to a staged update that never holds two copies on flash:

  1. Stage in tmpfs. Download/extract into /tmp (or $TMPDIR), verify sha256, run version and check -c last_good.json. Flash untouched; any failure exits with nothing changed.
  2. Rollback copy in tmpfs, compressed. gzip the running binary into /tmp (~28 MB instead of 76 MB) — two uncompressed copies in RAM are risky on 512 MB boxes. Check tmpfs free space for staged + rollback before going further.
  3. Stop, then unlink. Stop the service before removing /usr/bin/sing-box: unlinking the binary of a running process frees no space (inode stays alive). Order: stop → remove old → copy new in (.new + rename) → start.
  4. Health/rollback as today. Service up + core started; otherwise stop → remove new → restore from the gz → start. Clean /tmp in every outcome.

Details to handle

  • Detach from the daemon. The command is spawned by the daemon, and in this mode the daemon is stopped before the swap. The updater must setsid/re-exec itself detached and log its result (the launcher's command output will be cut; result visible after the restart via log / a status file).
  • Room check on compressing filesystems. ubifs (and jffs2) compress: the 76 MB binary takes ~35 MB on flash. After the old one is gone there is ~66 MB free, enough for the new one, but a check against the raw size would refuse. Better: try the write and treat ENOSPC as a rollback trigger rather than predicting.
  • Downtime grows from "restart" to "restart + flash copy" (~10–20 s). Say so in the output.
  • Opt-in flag (e.g. --low-space) or automatic fallback when room() fails — automatic seems nicer, with a clear message.

Alternative (cheaper, complementary)

Publish *-linux-arm64.upx.tar.gz in releases; boxes installed with the packed binary keep updating through the existing path. Cost: UPX unpacks the whole binary into anonymous memory at every start (~+50 MB RSS, no page sharing), noticeable on 512 MB routers.