Trust the build check over needrestart when it's conclusive
needrestart run unprivileged (the agent's DynamicUser) can't read Ubuntu's root-only /boot images and reported a false ABI-compatible upgrade (KSTA=2) on a 24.04 HWE host whose running build matched its installed package. When the uname -v / package-version comparison is conclusive, decide from it plus the newest-installed comparison and ignore needrestart; fall back to needrestart only for unrecognised build formats. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
818a2e7a1c
commit
a430a13fc8
2 files changed
+37
-11
No files matched your search
@@ -47,29 +47,36 @@ _UBUNTU_BUILD = re.compile(r"^#(\S+)-Ubuntu\b")
|
|||||||
_UBUNTU_PKG = re.compile(r"^\d+\.\d+\.\d+-\d+\.(\S+)$")
|
_UBUNTU_PKG = re.compile(r"^\d+\.\d+\.\d+-\d+\.(\S+)$")
|
||||||
|
|
||||||
|
|
||||||
def running_build_outdated(report: dict[str, Any]) -> tuple[str, str] | None:
|
def running_build(report: dict[str, Any]) -> tuple[str, str] | None:
|
||||||
"""(running build, installed package version) if the running kernel's package was
|
"""(build of the running kernel, installed package version of that kernel), in
|
||||||
upgraded in place since boot (same `uname -r`, new build), else None.
|
comparable form, or None if we can't tell (e.g. an unrecognised build string).
|
||||||
|
|
||||||
None also means "can't tell", e.g. an unrecognised build string."""
|
If they differ, the package was upgraded in place since boot (same `uname -r`)."""
|
||||||
build = report.get("kernel_build") or ""
|
build = report.get("kernel_build") or ""
|
||||||
pkg = report.get("kernel_running_pkg_version")
|
pkg = report.get("kernel_running_pkg_version")
|
||||||
if not pkg:
|
if not pkg:
|
||||||
return None
|
return None
|
||||||
if m := _DEBIAN_BUILD.search(build):
|
if m := _DEBIAN_BUILD.search(build):
|
||||||
running = m.group(1)
|
return m.group(1), pkg
|
||||||
return (running, pkg) if running != pkg else None
|
|
||||||
if (m := _UBUNTU_BUILD.match(build)) and (p := _UBUNTU_PKG.match(pkg)):
|
if (m := _UBUNTU_BUILD.match(build)) and (p := _UBUNTU_PKG.match(pkg)):
|
||||||
return (m.group(1), p.group(1)) if m.group(1) != p.group(1) else None
|
return m.group(1), p.group(1)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def kernel_reboot_pending(report: dict[str, Any]) -> str | None:
|
def kernel_reboot_pending(report: dict[str, Any]) -> str | None:
|
||||||
"""Why the running kernel is out of date, or None if it isn't (as far as we can tell)."""
|
"""Why the running kernel is out of date, or None if it isn't (as far as we can tell)."""
|
||||||
running = report.get("kernel_running") or "?"
|
running = report.get("kernel_running") or "?"
|
||||||
if outdated := running_build_outdated(report):
|
newest = report.get("kernel_newest_installed")
|
||||||
built, installed = outdated
|
|
||||||
return f"running {running} build {built}, package upgraded in place to {installed}"
|
if build := running_build(report):
|
||||||
|
built, installed = build
|
||||||
|
if built != installed:
|
||||||
|
return f"running {running} build {built}, package upgraded in place to {installed}"
|
||||||
|
# Conclusive, so skip needrestart: run unprivileged it can't read Ubuntu's
|
||||||
|
# root-only /boot images and reports false ABI upgrades.
|
||||||
|
if newest and newest != running:
|
||||||
|
return f"running {running}, {newest} installed"
|
||||||
|
return None
|
||||||
|
|
||||||
ksta = report.get("needrestart_ksta")
|
ksta = report.get("needrestart_ksta")
|
||||||
if ksta == KSTA_VERSION_UPGRADE:
|
if ksta == KSTA_VERSION_UPGRADE:
|
||||||
@@ -81,7 +88,6 @@ def kernel_reboot_pending(report: dict[str, Any]) -> str | None:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# No usable needrestart result: compare against the newest installed image.
|
# No usable needrestart result: compare against the newest installed image.
|
||||||
newest = report.get("kernel_newest_installed")
|
|
||||||
if newest and newest != running:
|
if newest and newest != running:
|
||||||
return f"running {running}, {newest} installed"
|
return f"running {running}, {newest} installed"
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -100,6 +100,26 @@ class EvaluateTest(unittest.TestCase):
|
|||||||
kernel_build=build, kernel_running_pkg_version=pkg)
|
kernel_build=build, kernel_running_pkg_version=pkg)
|
||||||
self.assertEqual(self.level(rep).level, "ok", self.level(rep).reasons)
|
self.assertEqual(self.level(rep).level, "ok", self.level(rep).reasons)
|
||||||
|
|
||||||
|
def test_conclusive_build_overrides_needrestart(self):
|
||||||
|
# Real 24.04.4 HWE host: needrestart (unprivileged) claimed KSTA=2, build matches.
|
||||||
|
rep = report(kernel_running="7.0.0-38-generic", kernel_newest_installed="7.0.0-38-generic",
|
||||||
|
kernel_build="#38~24.04.4-Ubuntu SMP PREEMPT_DYNAMIC Mon Sep 14 16:37:11 UTC 2",
|
||||||
|
kernel_running_pkg_version="7.0.0-38.38~24.04.4", needrestart_ksta=2)
|
||||||
|
self.assertEqual(self.level(rep).level, "ok", self.level(rep).reasons)
|
||||||
|
rep["needrestart_ksta"] = 3
|
||||||
|
self.assertEqual(self.level(rep).level, "ok")
|
||||||
|
# ...but a newer installed kernel still counts.
|
||||||
|
rep["kernel_newest_installed"] = "7.0.0-40-generic"
|
||||||
|
self.assertIn("7.0.0-40-generic installed", self.level(rep).reasons[0])
|
||||||
|
|
||||||
|
def test_mixed_ga_and_hwe_kernels(self):
|
||||||
|
# GA 6.8 kernels installed alongside a running HWE 7.0: 7.0 is newest, so no kernel flag.
|
||||||
|
rep = report(kernel_running="7.0.0-38-generic", kernel_newest_installed="7.0.0-38-generic",
|
||||||
|
kernels_installed=["6.8.0-147-generic", "7.0.0-38-generic"],
|
||||||
|
kernel_build="#38~24.04.4-Ubuntu SMP", kernel_running_pkg_version="7.0.0-38.38~24.04.4",
|
||||||
|
needrestart_ksta=0)
|
||||||
|
self.assertEqual(self.level(rep).level, "ok")
|
||||||
|
|
||||||
def test_unrecognised_build_falls_back(self):
|
def test_unrecognised_build_falls_back(self):
|
||||||
rep = report(needrestart_ksta=None, kernel_build="#1 SMP custom",
|
rep = report(needrestart_ksta=None, kernel_build="#1 SMP custom",
|
||||||
kernel_running_pkg_version="1.0", kernel_newest_installed="6.12.60+deb13-amd64")
|
kernel_running_pkg_version="1.0", kernel_newest_installed="6.12.60+deb13-amd64")
|
||||||
|
|||||||
Reference in new issue
Block a user