Run the agent unprivileged; detect in-place kernel upgrades

The agent ran as (capability-less) root only so needrestart could read
Ubuntu's root-only /boot images. Switch the unit to DynamicUser=yes and
make needrestart optional.

To cover the case needrestart was catching (running kernel's package
upgraded in place, same uname -r), the agent now reports the installed
package version of the running kernel and the server compares it with
the build in uname -v: "Debian 6.12.57-1" vs 6.12.57-1 on Debian,
"#85-Ubuntu" / "#33~24.04.1-Ubuntu" vs the upload part of 6.8.0-85.85 /
6.14.0-33.33~24.04.1 on Ubuntu. Unrecognised builds fall back to
needrestart and the newest-installed comparison.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-17 10:19:41 -04:00
1 parent 07749b01b0
commit 48f2d155af
9 files changed
+109 -23

No files matched your search

+30
View File
@@ -9,6 +9,7 @@ Levels, worst first:
from __future__ import annotations
import re
from dataclasses import dataclass, field
from typing import Any
@@ -38,9 +39,38 @@ class Status:
return _RANK[self.level]
# Debian `uname -v`: "#1 SMP PREEMPT_DYNAMIC Debian 6.12.57-1 (2025-11-05)"
_DEBIAN_BUILD = re.compile(r"\bDebian (\S+)")
# Ubuntu `uname -v`: "#85-Ubuntu SMP ..." (GA) or "#33~24.04.1-Ubuntu SMP ..." (HWE)
_UBUNTU_BUILD = re.compile(r"^#(\S+)-Ubuntu\b")
# Ubuntu package version: "6.8.0-85.85" or "6.14.0-33.33~24.04.1"; upload part after "-ABI."
_UBUNTU_PKG = re.compile(r"^\d+\.\d+\.\d+-\d+\.(\S+)$")
def running_build_outdated(report: dict[str, Any]) -> tuple[str, str] | None:
"""(running build, installed package version) if the running kernel's package was
upgraded in place since boot (same `uname -r`, new build), else None.
None also means "can't tell", e.g. an unrecognised build string."""
build = report.get("kernel_build") or ""
pkg = report.get("kernel_running_pkg_version")
if not pkg:
return None
if m := _DEBIAN_BUILD.search(build):
running = m.group(1)
return (running, pkg) if running != pkg else None
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 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)."""
running = report.get("kernel_running") or "?"
if outdated := running_build_outdated(report):
built, installed = outdated
return f"running {running} build {built}, package upgraded in place to {installed}"
ksta = report.get("needrestart_ksta")
if ksta == KSTA_VERSION_UPGRADE:
expected = report.get("needrestart_kexp") or report.get("kernel_newest_installed") or "?"