Treat Ubuntu Livepatch as cover for pending kernel reboots

New livepatch_* report fields. When Livepatch is healthy (applied or
nothing-to-apply, checked, supported) and the kernel's support end date is
more than 14 days away, a pending kernel reboot, or a reboot-required file
caused only by linux-* packages, becomes a note instead of the reboot status.
Anything else about Livepatch gives no cover and is noted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Sonnet 5.5 committed 2026-10-29 21:35:42 -04:00
1 parent 40bfdc3236
commit 4fa86de11f
5 files changed
+142 -2

No files matched your search

+48 -2
View File
@@ -11,6 +11,7 @@ from __future__ import annotations
import re
from dataclasses import dataclass, field
from datetime import date, datetime, timezone
from typing import Any
from . import AGENT_VERSION
@@ -19,6 +20,11 @@ from .config import Config
LEVELS = ("stale", "reboot", "update", "ok")
_RANK = {level: i for i, level in enumerate(LEVELS)}
# Ubuntu Livepatch: a pending kernel reboot stops counting this long before the
# running kernel's patch support ends (`UpgradeRequiredDate`).
LIVEPATCH_MARGIN_DAYS = 14
LIVEPATCH_HEALTHY_STATES = ("applied", "nothing-to-apply")
# needrestart's NEEDRESTART-KSTA values.
KSTA_CURRENT = 1
KSTA_ABI_UPGRADE = 2
@@ -98,6 +104,37 @@ def kernel_reboot_pending(report: dict[str, Any]) -> str | None:
return None
def livepatch_cover(report: dict[str, Any], now: float) -> tuple[bool, str | None]:
"""(is the running kernel kept patched by Livepatch, a note about it).
Livepatch only fixes high and critical CVEs and only until the kernel's
support ends, so cover lapses LIVEPATCH_MARGIN_DAYS before that date. Hosts
without Livepatch data, or with Livepatch in any state we don't recognise as
healthy, get no cover and are judged as before."""
state = report.get("livepatch_state")
if state is None:
return False, None
try:
deadline = date.fromisoformat(report.get("livepatch_upgrade_required") or "")
except ValueError:
deadline = None
if (state not in LIVEPATCH_HEALTHY_STATES or report.get("livepatch_check_state") != "checked"
or report.get("livepatch_supported") != "supported"):
return False, f"livepatch not healthy ({state}, {report.get('livepatch_supported') or '?'})"
if deadline is None:
return False, "livepatch end of support date unknown"
left = (deadline - datetime.fromtimestamp(now, timezone.utc).date()).days
if left <= LIVEPATCH_MARGIN_DAYS:
return False, f"livepatch support for this kernel ends {deadline}; reboot into a newer one"
return True, f"live-patched; kernel upgrade needed by {deadline}"
def _kernel_only_reboot_file(report: dict[str, Any]) -> bool:
"""/run/reboot-required exists only because of kernel packages."""
pkgs = report.get("reboot_required_pkgs") or []
return bool(pkgs) and all(p.startswith("linux-") for p in pkgs)
def _version_tuple(v: str) -> tuple[int, ...]:
return tuple(int(p) if p.isdigit() else 0 for p in v.split("."))
@@ -128,12 +165,21 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
if age_h > cfg.stale_after_hours:
status.raise_to("stale", f"last report {age_h:.0f}h ago")
covered, livepatch_note = livepatch_cover(report, now)
if why := kernel_reboot_pending(report):
status.raise_to("reboot", f"kernel: {why}")
if covered:
status.note(f"kernel: {why} ({livepatch_note})")
else:
status.raise_to("reboot", f"kernel: {why}")
if report.get("reboot_required"):
pkgs = report.get("reboot_required_pkgs") or []
detail = f" ({', '.join(pkgs[:5])}{', …' if len(pkgs) > 5 else ''})" if pkgs else ""
status.raise_to("reboot", f"/run/reboot-required{detail}")
if covered and _kernel_only_reboot_file(report):
status.note(f"/run/reboot-required{detail} ({livepatch_note})")
else:
status.raise_to("reboot", f"/run/reboot-required{detail}")
if livepatch_note and not covered:
status.note(livepatch_note)
if kernels := report.get("kernel_upgradable"):
status.raise_to("update", f"kernel update available: {', '.join(kernels)}")