Flag hosts without room on disk for their pending upgrades
CI / tests (Python 3.10) (pull_request) Successful in 43s
CI / tests (Python 3.11) (pull_request) Successful in 44s
CI / tests (Python 3.12) (pull_request) Successful in 43s
CI / tests (Python 3.13) (pull_request) Successful in 42s
CI / lint (pull_request) Successful in 3s

A filesystem that fills up while apt downloads or dpkg unpacks leaves the
upgrade half-done and apt broken, the same failure as a full /boot but on
/ or /var. Nothing caught it until the host's updates stopped.

The agent (0.11.0) now works out what the pending upgrades need. A
simulation doesn't print apt's "Need to get" or "additional disk space"
lines, so it sums the package records from apt-cache show for each version
the simulation would install: Size for each .deb not already in
/var/cache/apt/archives, and the growth in Installed-Size over the
installed versions (from dpkg-query). Removals aren't subtracted, so it
errs high. It also reports free space and the mount point of the
filesystems holding /var/cache/apt/archives and /usr.

The server puts a host in update when the upgrades would leave less than
upgrade_min_free_mb (default 100, 0 disables) on either filesystem. The
download and the install both count when they're on the same one. With
nothing pending there is nothing to check, so this doesn't turn into
general disk monitoring. The host page gets a "Pending upgrades size" row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-11-07 20:16:54 -05:00
1 parent b5be19f85d
commit 15b1f5be4f
13 files changed
+363 -7

No files matched your search

+114 -2
View File
@@ -31,6 +31,37 @@ Inst curl [8.14.1-2] (8.14.1-2+deb13u1 Debian:13.2/stable [amd64])
Conf libssl3t64 (3.5.1-1+deb13u1 Debian-Security:13/stable-security [amd64])
"""
# `apt-cache show` records for the simulation's three new versions. curl's record
# appears twice, as it does when two sources carry the same version.
APT_CACHE_DEBIAN = """\
Package: libssl3t64
Architecture: amd64
Version: 3.5.1-1+deb13u1
Installed-Size: 9000
Size: 2500000
Package: linux-image-6.12.60+deb13-amd64
Architecture: amd64
Version: 6.12.60-1
Installed-Size: 400000
Size: 100000000
Package: curl
Architecture: amd64
Version: 8.14.1-2+deb13u1
Installed-Size: 500
Size: 300000
Package: curl
Architecture: amd64
Version: 8.14.1-2+deb13u1
Installed-Size: 500
Size: 300000
"""
# Installed-Size of the installed libssl3t64 and curl, the two upgrades.
DPKG_SIZES_DEBIAN = "8900\n500\n"
def stub(bindir, name, script):
path = os.path.join(bindir, name)
@@ -61,11 +92,21 @@ class AgentCase(unittest.TestCase):
stub(self.bin, "hostname", "echo web01.example\n")
# The full-database query (unfinished packages) prints $KERNELWATCH_ROOT/dpkg-all.
# The unattended-upgrades query prints $KERNELWATCH_ROOT/dpkg-uu (its status, e.g. "ii ").
stub(self.bin, "dpkg-query", 'case "$*" in *" unattended-upgrades") cat "$KERNELWATCH_ROOT/dpkg-uu" 2>/dev/null;; '
# The Installed-Size query prints $KERNELWATCH_ROOT/dpkg-sizes.
stub(self.bin, "dpkg-query", 'case "$*" in *Installed-Size*) cat "$KERNELWATCH_ROOT/dpkg-sizes" 2>/dev/null;; '
'*" unattended-upgrades") cat "$KERNELWATCH_ROOT/dpkg-uu" 2>/dev/null;; '
'*binary:Package*) cat "$KERNELWATCH_ROOT/dpkg-all" 2>/dev/null;; '
'*Version*) echo "ii 6.12.48-2";; *) cat <<\'X\'\n'
f"{DPKG_DEBIAN}X\n;; esac\n")
self.stub_df("/dev/sda1 999320 187364 743144 21% /boot\n")
# df answers per path: /boot and /usr on the root filesystem, apt's archives on /var.
stub(self.bin, "df", "echo 'Filesystem 1024-blocks Used Available Capacity Mounted on'\n"
'case "$2" in */var/cache/apt/archives) '
"echo '/dev/sda2 4062912 3500000 358124 91% /var';; "
"*) echo '/dev/sda1 999320 187364 743144 21% /';; esac\n")
with open(os.path.join(self.root, "dpkg-sizes"), "w") as f:
f.write(DPKG_SIZES_DEBIAN)
# apt-cache records its arguments in $KERNELWATCH_ROOT/apt-cache-args.
self.stub_apt_cache(APT_CACHE_DEBIAN)
# `apt-config dump KEY` prints $KERNELWATCH_ROOT/apt-config (nothing: the key is unset).
stub(self.bin, "apt-config", 'cat "$KERNELWATCH_ROOT/apt-config" 2>/dev/null; exit 0\n')
stub(self.bin, "apt-get", f"cat <<'X'\n{APT_SIM_DEBIAN}X\n")
@@ -77,6 +118,10 @@ class AgentCase(unittest.TestCase):
stub(self.bin, "df", "echo 'Filesystem 1024-blocks Used Available Capacity Mounted on'\n"
f"cat <<'X'\n{line}X\nexit {rc}\n")
def stub_apt_cache(self, records, rc=0):
stub(self.bin, "apt-cache", 'printf "%s\\n" "$@" >"$KERNELWATCH_ROOT/apt-cache-args"\n'
f"cat <<'X'\n{records}X\nexit {rc}\n")
def run_agent(self, *args, env=None):
full_env = {
"PATH": f"{self.bin}:{os.environ['PATH']}",
@@ -173,6 +218,73 @@ class AgentReportTest(AgentCase):
self.stub_df("/dev/sda1 - - - - /boot\n")
self.assertIsNone(self.report()["boot_free_kb"])
def test_upgrade_space(self):
r = self.report()
# 102.8 MB of .debs, rounded up to KiB; 409500 KiB of new versions less the
# 9400 KiB of the libssl3t64 and curl they replace.
self.assertEqual(r["upgrade_download_kb"], 100391)
self.assertEqual(r["upgrade_install_kb"], 400100)
self.assertEqual((r["apt_archives_free_kb"], r["apt_archives_mount"]), (358124, "/var"))
self.assertEqual((r["usr_free_kb"], r["usr_mount"]), (743144, "/"))
with open(os.path.join(self.root, "apt-cache-args")) as f:
args = f.read().split()
self.assertEqual(args[-4:], ["show", "libssl3t64=3.5.1-1+deb13u1",
"linux-image-6.12.60+deb13-amd64=6.12.60-1", "curl=8.14.1-2+deb13u1"])
def test_upgrade_space_skips_downloaded_archives(self):
archives = os.path.join(self.root, "var/cache/apt/archives")
os.makedirs(archives)
open(os.path.join(archives, "curl_8.14.1-2+deb13u1_amd64.deb"), "w").close()
open(os.path.join(archives, "libssl3t64_3.5.1-1_amd64.deb"), "w").close() # the old version
self.assertEqual(self.report()["upgrade_download_kb"], 100098)
def test_upgrade_space_epoch_in_archive_name(self):
stub(self.bin, "apt-get", "echo 'Inst tzdata [1:2024a-1] (1:2025b-1 Debian:13/stable [all])'\n")
self.stub_apt_cache("Package: tzdata\nArchitecture: all\nVersion: 1:2025b-1\n"
"Installed-Size: 3000\nSize: 2048\n")
archives = os.path.join(self.root, "var/cache/apt/archives")
os.makedirs(archives)
open(os.path.join(archives, "tzdata_1%3a2025b-1_all.deb"), "w").close()
with open(os.path.join(self.root, "dpkg-sizes"), "w") as f:
f.write("3100\n")
r = self.report()
self.assertEqual((r["upgrade_download_kb"], r["upgrade_install_kb"]), (0, -100))
def test_upgrade_space_new_package_counts_in_full(self):
stub(self.bin, "apt-get", "echo 'Inst linux-image-6.12.60+deb13-amd64 (6.12.60-1 Debian:13/stable [amd64])'\n")
self.stub_apt_cache(APT_CACHE_DEBIAN.split("\n\n")[1] + "\n")
os.remove(os.path.join(self.root, "dpkg-sizes"))
r = self.report()
self.assertEqual((r["upgrade_download_kb"], r["upgrade_install_kb"]), (97657, 400000))
def test_upgrade_space_nothing_pending(self):
stub(self.bin, "apt-get", "echo 'NOTE: This is only a simulation!'\n")
r = self.report()
self.assertEqual((r["upgrade_download_kb"], r["upgrade_install_kb"]), (0, 0))
self.assertFalse(os.path.exists(os.path.join(self.root, "apt-cache-args")), "apt-cache not run")
def test_upgrade_space_unknown_when_apt_cache_fails(self):
self.stub_apt_cache("", rc=100)
r = self.report()
self.assertIsNone(r["upgrade_download_kb"])
self.assertIsNone(r["upgrade_install_kb"])
def test_upgrade_space_unknown_when_simulation_fails(self):
stub(self.bin, "apt-get", "echo 'E: Unmet dependencies.' >&2; exit 100\n")
r = self.report()
self.assertIsNone(r["upgrade_download_kb"])
self.assertFalse(os.path.exists(os.path.join(self.root, "apt-cache-args")))
def test_filesystem_free_unknown_when_df_fails(self):
self.stub_df("", rc=1)
r = self.report()
self.assertIsNone(r["apt_archives_free_kb"])
self.assertIsNone(r["usr_mount"])
def test_mount_point_with_spaces(self):
self.stub_df("/dev/sdb1 999320 187364 743144 21% /srv/my disk\n")
self.assertEqual(self.report()["usr_mount"], "/srv/my disk")
def test_unfinished_packages(self):
with open(os.path.join(self.root, "dpkg-all"), "w") as f:
f.write("ii \tcurl\n"
+8
View File
@@ -100,6 +100,14 @@ class ConfigTest(unittest.TestCase):
with self.subTest(bad=bad), self.assertRaises(SystemExit):
config.load(self.write(tmp, f"boot_min_free_mb = {bad}\n"))
def test_upgrade_min_free_validated(self):
with tempdir() as tmp:
self.assertEqual(config.Config().upgrade_min_free_mb, 100)
self.assertEqual(config.load(self.write(tmp, "upgrade_min_free_mb = 0\n")).upgrade_min_free_mb, 0)
for bad in ("-1", "1.5", '"100"'):
with self.subTest(bad=bad), self.assertRaises(SystemExit):
config.load(self.write(tmp, f"upgrade_min_free_mb = {bad}\n"))
def test_os_eol_warn_days_validated(self):
with tempdir() as tmp:
self.assertEqual(config.Config().os_eol_warn_days, 90)
+52 -1
View File
@@ -264,7 +264,8 @@ class EvaluateTest(unittest.TestCase):
self.assertEqual(s.level, "ok")
self.assertIn("agent 0.1.0 is older", s.reasons[0])
self.assertEqual(self.level(report(agent_version=AGENT_VERSION)).reasons, [])
self.assertEqual(self.level(report(agent_version="0.10.0")).reasons, [], "numeric compare")
self.assertEqual(self.level(report(agent_version="0.100.0")).reasons, [],
"numeric compare: lexically older than 0.11.0")
def test_server_knows_shipped_agent_version(self):
with open(os.path.join(REPO, "agent", "kernelwatch-agent")) as f:
@@ -326,6 +327,56 @@ class EvaluateTest(unittest.TestCase):
self.assertEqual(evaluate(NOW, report(boot_free_kb=0), NOW, Config(boot_min_free_mb=0)).level,
"ok", "0 disables")
@staticmethod
def upgrade(download_mb, install_mb, archives=("/", 1000), usr=("/", 1000), **kw):
fields = dict(upgrade_download_kb=download_mb * 1024, upgrade_install_kb=install_mb * 1024,
apt_archives_mount=archives[0], apt_archives_free_kb=archives[1] * 1024,
usr_mount=usr[0], usr_free_kb=usr[1] * 1024)
return report(**{**fields, **kw})
def test_upgrade_space_shared_filesystem(self):
# 300 + 650 needed on /, 1000 free: 50 MB left, under the 100 MB default.
s = self.level(self.upgrade(300, 650))
self.assertEqual(s.level, "update")
self.assertEqual(s.reasons, ["pending upgrades need 950 MB on / (300 MB download + 650 MB installed), "
"1000 MB free; they may fail part-way"])
self.assertEqual(self.level(self.upgrade(300, 600)).level, "ok", "exactly 100 MB left")
def test_upgrade_space_separate_filesystems(self):
s = self.level(self.upgrade(300, 650, archives=("/var", 350), usr=("/", 2000)))
self.assertEqual(s.reasons, ["pending upgrades need 300 MB on /var (300 MB download), "
"350 MB free; they may fail part-way"])
s = self.level(self.upgrade(300, 1950, archives=("/var", 5000), usr=("/", 2000)))
self.assertEqual(s.reasons, ["pending upgrades need 1950 MB on / (1950 MB installed), "
"2000 MB free; they may fail part-way"])
self.assertEqual(len(self.level(self.upgrade(300, 1950, archives=("/var", 350))).reasons), 2)
def test_upgrade_space_shrinking_install_counts_as_zero(self):
self.assertEqual(self.level(self.upgrade(850, -500)).level, "ok")
self.assertEqual(self.level(self.upgrade(950, -500)).level, "update",
"the download still has to fit before the old files go")
def test_upgrade_space_ignores_full_disk_with_nothing_pending(self):
self.assertEqual(self.level(self.upgrade(0, 0, archives=("/", 5), usr=("/", 5))).level, "ok")
def test_upgrade_space_rounds_need_up(self):
rep = report(upgrade_download_kb=10, upgrade_install_kb=0, apt_archives_mount="/",
apt_archives_free_kb=50 * 1024, usr_mount="/", usr_free_kb=50 * 1024)
self.assertEqual(self.level(rep).reasons, ["pending upgrades need 1 MB on / (1 MB download), "
"50 MB free; they may fail part-way"])
def test_upgrade_space_unknown_or_unreported(self):
self.assertEqual(self.level(report()).level, "ok", "agents before 0.11.0 don't send it")
self.assertEqual(self.level(self.upgrade(300, 650, upgrade_download_kb=None)).level, "ok")
self.assertEqual(self.level(self.upgrade(300, 650, apt_archives_free_kb=None, usr_mount=None)).level,
"ok", "df failed")
def test_upgrade_space_threshold_is_configurable(self):
self.assertEqual(evaluate(NOW, self.upgrade(300, 650), NOW, Config(upgrade_min_free_mb=0)).level,
"ok", "0 disables")
self.assertEqual(evaluate(NOW, self.upgrade(300, 300), NOW, Config(upgrade_min_free_mb=500)).level,
"update")
def test_unfinished_dpkg_packages(self):
s = self.level(report(dpkg_unfinished_packages=["linux-image-6.12.60+deb13-amd64"]))
self.assertEqual(s.level, "update")
+12
View File
@@ -197,6 +197,18 @@ class DashboardTest(ServerCase):
self.assertIn("<code>linux-image-6.8.0-138-generic</code>", body)
self.assertIn("/boot has 95 MB free, 81% used; the next kernel may not fit", body)
def test_host_page_shows_upgrade_space(self):
self.add_user("admin", "a-long-password")
token = self.add_host("web01")
self.post_report(token, {"kernel_running": "6.8.0-136-generic", "apt_lists_age_seconds": 60,
"upgrade_download_kb": 300 * 1024, "upgrade_install_kb": 650 * 1024,
"apt_archives_mount": "/var", "apt_archives_free_kb": 350 * 1024,
"usr_mount": "/", "usr_free_kb": 2000 * 1024})
_, headers, _ = self.login("admin", "a-long-password")
body = self.request("GET", "/host/web01", headers={"Cookie": headers["Set-Cookie"].split(";")[0]})[2]
self.assertIn("300 MB to download, +650 MB installed; /var has 350 MB free, / has 2000 MB free", body)
self.assertIn("pending upgrades need 300 MB on /var (300 MB download), 350 MB free", body)
def test_host_page_shows_unattended_upgrades(self):
self.add_user("admin", "a-long-password")
token = self.add_host("web01")