From 46769421088fd1b1697db3a251623502a22e87e8 Mon Sep 17 00:00:00 2001 From: patrick Date: Sun, 1 Nov 2026 15:24:27 -0500 Subject: [PATCH] Report packages on hold, and flag held kernel packages A held package never upgrades, and apt's simulation leaves it out, so its updates (security ones included) appear nowhere. A leftover hold on linux-image-amd64 or linux-generic keeps a host on its kernel while it looks current. The agent reports installed held packages (dpkg's "hi") as held_packages. A held linux-* package puts the host in update with the apt-mark unhold hint; other holds, usually deliberate, get a note. The host page lists them. Co-Authored-By: Claude Opus 5.5 --- agent/kernelwatch-agent | 10 ++++++++++ docs/how-it-works.md | 11 ++++++++++- server/kernelwatch/report.py | 1 + server/kernelwatch/status.py | 15 +++++++++++++++ server/kernelwatch/web.py | 1 + tests/test_agent.py | 12 ++++++++++++ tests/test_status.py | 12 ++++++++++++ 7 files changed, 61 insertions(+), 1 deletion(-) diff --git a/agent/kernelwatch-agent b/agent/kernelwatch-agent index 47f37b6..b91d1ca 100755 --- a/agent/kernelwatch-agent +++ b/agent/kernelwatch-agent @@ -308,6 +308,15 @@ security_repo() { return 0 } +# Installed packages on hold (`apt-mark hold`, or dpkg's "hold" selection). apt +# never upgrades them and the simulation doesn't list them, so their updates, +# security ones included, don't show up anywhere else. +held_packages() { + dpkg-query -W -f='${db:Status-Abbrev}\t${binary:Package}\n' 2>/dev/null | + awk -F '\t' 'substr($1, 1, 2) == "hi" { print $2 }' | + sort -u || true +} + # Size and free space of the filesystem holding /boot, in KiB, as "SIZE FREE"; # empty if df can't tell. That's the root filesystem when /boot isn't its own # mount. A full /boot makes the next kernel's postinst fail, so the host stays @@ -456,6 +465,7 @@ build_report() { printf '"boot_size_kb":%s,' "$(json_int "$boot_size")" printf '"boot_free_kb":%s,' "$(json_int "$boot_free")" printf '"dpkg_unfinished_packages":%s,' "$(unfinished_packages | json_str_array)" + printf '"held_packages":%s,' "$(held_packages | json_str_array)" case $pro_attached in true | false) printf '"ubuntu_pro_attached":%s,' "$pro_attached" ;; *) printf '"ubuntu_pro_attached":null,' ;; diff --git a/docs/how-it-works.md b/docs/how-it-works.md index 34c320d..d8e9f22 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -10,7 +10,7 @@ Each host gets one status. When several apply, the worst one wins: |----------|---------| | `stale` | No report in `stale_after_hours` (default 3h), or never. Other data can't be trusted. | | `reboot` | A newer kernel is installed but not running, or `/run/reboot-required` exists. | -| `update` | A kernel or security update is available but not installed, services are running outdated libraries and need a restart, or apt's package lists are too old (`apt_stale_after_hours`) for "no updates" to mean anything. Also when `/boot` has less than `boot_min_free_mb` free, dpkg left packages half-installed, the OS release no longer gets security updates, unattended-upgrades is enabled but hasn't succeeded in `unattended_upgrades_stale_after_days`, the release's security repository is missing from apt or hasn't refreshed in `security_repo_stale_after_days`, or one of kernelwatch's own helper units has failed three runs in a row. | +| `update` | A kernel or security update is available but not installed, services are running outdated libraries and need a restart, or apt's package lists are too old (`apt_stale_after_hours`) for "no updates" to mean anything. Also when `/boot` has less than `boot_min_free_mb` free, dpkg left packages half-installed, a kernel package is on hold, the OS release no longer gets security updates, unattended-upgrades is enabled but hasn't succeeded in `unattended_upgrades_stale_after_days`, the release's security repository is missing from apt or hasn't refreshed in `security_repo_stale_after_days`, or one of kernelwatch's own helper units has failed three runs in a row. | | `ok` | Running the newest installed kernel, with no security or kernel updates pending. | Each row also lists the reasons behind its status, and clicking a host shows @@ -106,6 +106,15 @@ How the agent works it out: host: `sudo dpkg --configure -a` (after freeing space, if that was the cause). A report taken while apt is mid-install can show this briefly; the next one clears it. Agents before 0.6.0 send neither field and are judged as before. +- **Packages on hold**: installed packages marked `hold` (`apt-mark hold`, or + dpkg's selection), from dpkg's status database. apt never upgrades them, and + the simulation doesn't list them, so their updates (security ones included) + don't show up anywhere else. A held `linux-*` package (`linux-image-amd64`, + `linux-generic`) would leave the host on its current kernel while looking up + to date, so it puts the host in `update`; release it with + `sudo apt-mark unhold `. Other holds are usually deliberate and only + get a note. The host page lists them all. Agents before 0.10.0 don't report + holds. - **OS end of support**: once a release stops getting security updates, apt never offers one again, so the host would show `ok` indefinitely. The server knows when each supported release's free security support ends diff --git a/server/kernelwatch/report.py b/server/kernelwatch/report.py index 19d62be..969c97c 100644 --- a/server/kernelwatch/report.py +++ b/server/kernelwatch/report.py @@ -45,6 +45,7 @@ FIELDS: dict[str, str] = { "boot_size_kb": "int", "boot_free_kb": "int", "dpkg_unfinished_packages": "strlist", + "held_packages": "strlist", "ubuntu_pro_attached": "bool", "ubuntu_esm_infra": "str", "unattended_upgrades": "str", diff --git a/server/kernelwatch/status.py b/server/kernelwatch/status.py index 36825ed..2c9b55e 100644 --- a/server/kernelwatch/status.py +++ b/server/kernelwatch/status.py @@ -327,6 +327,21 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float, more = f", +{n - 5} more" if n > 5 else "" status.raise_to("update", f"{n} package{'s' if n != 1 else ''} not fully installed: " f"{', '.join(unfinished[:5])}{more}; run dpkg --configure -a") + # Held packages never upgrade, and the simulation doesn't list them, so a held + # kernel package would make a host look current forever. Other holds are + # usually deliberate, but their updates (security ones too) are invisible. + if held := report.get("held_packages"): + kernel = [p for p in held if p.startswith("linux-")] + other = [p for p in held if not p.startswith("linux-")] + if kernel: + status.raise_to("update", f"kernel package{'s' if len(kernel) != 1 else ''} on hold, so " + f"kernel updates won't install: {', '.join(kernel[:5])}" + f"{', …' if len(kernel) > 5 else ''}; apt-mark unhold to release") + if other: + more = f", +{len(other) - 5} more" if len(other) > 5 else "" + status.note(f"{len(other)} package{'s' if len(other) != 1 else ''} on hold, " + f"updates to {'them' if len(other) != 1 else 'it'} won't show: " + f"{', '.join(other[:5])}{more}") if low := boot_space_low(report, cfg): status.raise_to("update", low) diff --git a/server/kernelwatch/web.py b/server/kernelwatch/web.py index ed0aaa1..21de8a4 100644 --- a/server/kernelwatch/web.py +++ b/server/kernelwatch/web.py @@ -547,6 +547,7 @@ class Handler(BaseHTTPRequestHandler): field("/boot free", None if r.get("boot_free_kb") is None else f"{r['boot_free_kb'] // 1024} MB of {(r.get('boot_size_kb') or 0) // 1024} MB"), field("Unfinished dpkg packages", r.get("dpkg_unfinished_packages"), code=True), + field("Packages on hold", r.get("held_packages"), code=True), field("unattended-upgrades", unattended_upgrades_summary(r)), field("Kept back by unattended-upgrades", r.get("unattended_upgrades_kept_back"), code=True), field("Package lists age", duration(r.get("apt_lists_age_seconds")) diff --git a/tests/test_agent.py b/tests/test_agent.py index 60715f5..78f5121 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -172,6 +172,18 @@ class AgentReportTest(AgentCase): "libbar2", "libfoo1:amd64", "linux-headers-6.12.60+deb13-amd64", "linux-image-6.12.60+deb13-amd64", "man-db"]) + def test_held_packages(self): + with open(os.path.join(self.root, "dpkg-all"), "w") as f: + f.write("ii \tcurl\n" + "hi \tlinux-image-amd64\n" + "hi \tpostgresql-16\n" + "hn \tnot-installed\n" + "hc \tremoved-but-held\n" + "iF \tbroken\n") + r = self.report() + self.assertEqual(r["held_packages"], ["linux-image-amd64", "postgresql-16"]) + self.assertEqual(r["dpkg_unfinished_packages"], ["broken"]) + def test_no_unfinished_packages(self): with open(os.path.join(self.root, "dpkg-all"), "w") as f: f.write("ii \tcurl\nrc \told-pkg\n") diff --git a/tests/test_status.py b/tests/test_status.py index f1fe019..37abf43 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -339,6 +339,18 @@ class EvaluateTest(unittest.TestCase): self.assertEqual(self.level(report(dpkg_unfinished_packages=[])).level, "ok") self.assertEqual(self.level(report(dpkg_unfinished_packages=None)).level, "ok") + def test_held_kernel_package_is_a_problem(self): + s = self.level(report(held_packages=["linux-image-amd64"])) + self.assertEqual(s.level, "update") + self.assertEqual(s.reasons, ["kernel package on hold, so kernel updates won't install: " + "linux-image-amd64; apt-mark unhold to release"]) + + def test_other_held_packages_are_a_note(self): + s = self.level(report(held_packages=["postgresql-16", "docker-ce"])) + self.assertEqual(s.level, "ok") + self.assertEqual(s.reasons, ["2 packages on hold, updates to them won't show: postgresql-16, docker-ce"]) + self.assertEqual(self.level(report(held_packages=[])).reasons, []) + def test_os_past_end_of_support(self): s = self.level(report(os_id="debian", os_version_id="11")) self.assertEqual(s.level, "update")