Collect Ubuntu Livepatch status in the agent
CI / tests (Python 3.10) (pull_request) Successful in 22s
CI / tests (Python 3.11) (pull_request) Successful in 22s
CI / tests (Python 3.12) (pull_request) Successful in 21s
CI / tests (Python 3.13) (pull_request) Successful in 21s
CI / lint (pull_request) Successful in 2s
CI / tests (Python 3.10) (pull_request) Successful in 22s
CI / tests (Python 3.11) (pull_request) Successful in 22s
CI / tests (Python 3.12) (pull_request) Successful in 21s
CI / tests (Python 3.13) (pull_request) Successful in 21s
CI / lint (pull_request) Successful in 2s
kernelwatch-livepatch.service runs canonical-livepatch as root with no network and a read-only filesystem, and a small python3 helper keeps the running kernel's state, check state, support, end-of-support date and fix count in /run/kernelwatch/livepatch. The agent (0.4.0) reads that file the way it reads needrestart's and never runs the snap itself. NoNewPrivileges=yes can't be used: snap-confine fails under it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4fa86de11f
commit
b59336d7e2
11 files changed
+273
-8
No files matched your search
+2
-1
@@ -19,7 +19,8 @@ here=$(cd "$(dirname "$0")" && pwd)
|
||||
CONF=${KERNELWATCH_DEPLOY_CONF:-$here/deploy.conf}
|
||||
HOSTS_FILE=${KERNELWATCH_HOSTS:-$here/hosts}
|
||||
FILES=(kernelwatch-agent kernelwatch-agent.service kernelwatch-agent.timer
|
||||
kernelwatch-needrestart.service kernelwatch-apt-update.service install.sh)
|
||||
kernelwatch-needrestart.service kernelwatch-livepatch kernelwatch-livepatch.service
|
||||
kernelwatch-apt-update.service install.sh)
|
||||
NAME_RE='^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$'
|
||||
|
||||
SERVER_URL=
|
||||
|
||||
+3
-2
@@ -64,9 +64,10 @@ fi
|
||||
|
||||
install -d -m 0755 /etc/kernelwatch
|
||||
install -m 0755 "$here/kernelwatch-agent" /usr/local/sbin/kernelwatch-agent
|
||||
install -m 0755 "$here/kernelwatch-livepatch" /usr/local/sbin/kernelwatch-livepatch
|
||||
install -m 0644 "$here/kernelwatch-agent.service" "$here/kernelwatch-agent.timer" \
|
||||
"$here/kernelwatch-needrestart.service" "$here/kernelwatch-apt-update.service" \
|
||||
/etc/systemd/system/
|
||||
"$here/kernelwatch-needrestart.service" "$here/kernelwatch-livepatch.service" \
|
||||
"$here/kernelwatch-apt-update.service" /etc/systemd/system/
|
||||
|
||||
case $apt_update in
|
||||
on) touch /etc/kernelwatch/apt-update ;;
|
||||
|
||||
+38
-1
@@ -6,7 +6,7 @@
|
||||
# rules can change without redeploying agents.
|
||||
set -euo pipefail
|
||||
|
||||
AGENT_VERSION=0.3.2
|
||||
AGENT_VERSION=0.4.0
|
||||
|
||||
# Prefix for every file the agent reads. Only the test suite sets this.
|
||||
ROOT=${KERNELWATCH_ROOT:-}
|
||||
@@ -151,6 +151,37 @@ read_needrestart() {
|
||||
done <"$NEEDRESTART_FILE"
|
||||
}
|
||||
|
||||
# Ubuntu Livepatch status, flattened by kernelwatch-livepatch.service (root; the
|
||||
# Livepatch snap won't run unprivileged). Absent when Livepatch isn't enabled.
|
||||
LIVEPATCH_FILE=$ROOT/run/kernelwatch/livepatch
|
||||
LIVEPATCH_MAX_AGE=7200
|
||||
|
||||
# Sets LP_* from the file, if it's there and fresh.
|
||||
LP_STATE=
|
||||
LP_CHECK=
|
||||
LP_SUPPORTED=
|
||||
LP_UPGRADE=
|
||||
LP_FIXES=
|
||||
read_livepatch() {
|
||||
local mtime line
|
||||
mtime=$(stat -c %Y "$LIVEPATCH_FILE" 2>/dev/null) || return 0
|
||||
(($(date +%s) - mtime <= LIVEPATCH_MAX_AGE)) || return 0
|
||||
while IFS= read -r line; do
|
||||
case $line in
|
||||
LIVEPATCH-STATE:*) LP_STATE=${line#*: } ;;
|
||||
LIVEPATCH-CHECK:*) LP_CHECK=${line#*: } ;;
|
||||
LIVEPATCH-SUPPORTED:*) LP_SUPPORTED=${line#*: } ;;
|
||||
LIVEPATCH-UPGRADE-REQUIRED:*) LP_UPGRADE=${line#*: } ;;
|
||||
LIVEPATCH-FIXES:*) LP_FIXES=${line#*: } ;;
|
||||
esac
|
||||
done <"$LIVEPATCH_FILE"
|
||||
}
|
||||
|
||||
# String, or null when empty.
|
||||
json_str_or_null() {
|
||||
if [[ -n $1 ]]; then json_str "$1"; else printf 'null'; fi
|
||||
}
|
||||
|
||||
# Simulated dist-upgrade. Reads only: no locks, no cache files written.
|
||||
# dist-upgrade (not upgrade) so new-ABI kernel packages pulled in by the
|
||||
# meta-package show up. C locale because we parse apt's messages.
|
||||
@@ -205,6 +236,7 @@ build_report() {
|
||||
kernels=$(installed_kernels)
|
||||
newest=$(tail -n1 <<<"$kernels")
|
||||
read_needrestart
|
||||
read_livepatch
|
||||
|
||||
sim=$(apt_simulation)
|
||||
inst=$(grep '^Inst ' <<<"$sim" || true)
|
||||
@@ -239,6 +271,11 @@ build_report() {
|
||||
printf '"security_count":%s,' "$(json_int "$(grep -ci 'security' <<<"$inst" || true)")"
|
||||
printf '"phased_packages":%s,' "$(phased_packages <<<"$sim" | json_str_array)"
|
||||
printf '"kernel_upgradable":%s,' "$(awk '$2 ~ /^linux-image-/ { print $2 }' <<<"$inst" | json_str_array)"
|
||||
printf '"livepatch_state":%s,' "$(json_str_or_null "$LP_STATE")"
|
||||
printf '"livepatch_check_state":%s,' "$(json_str_or_null "$LP_CHECK")"
|
||||
printf '"livepatch_supported":%s,' "$(json_str_or_null "$LP_SUPPORTED")"
|
||||
printf '"livepatch_upgrade_required":%s,' "$(json_str_or_null "$LP_UPGRADE")"
|
||||
printf '"livepatch_fix_count":%s,' "$(json_int "$LP_FIXES")"
|
||||
printf '"apt_lists_age_seconds":%s,' "$(json_int "$(apt_lists_age)")"
|
||||
printf '"uptime_seconds":%s' "$(json_int "$(uptime_seconds)")"
|
||||
printf '}\n'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[Unit]
|
||||
Description=Report kernel and update status to kernelwatch
|
||||
Wants=network-online.target kernelwatch-needrestart.service kernelwatch-apt-update.service
|
||||
After=network-online.target apt-daily.service kernelwatch-needrestart.service kernelwatch-apt-update.service
|
||||
Wants=network-online.target kernelwatch-needrestart.service kernelwatch-livepatch.service kernelwatch-apt-update.service
|
||||
After=network-online.target apt-daily.service kernelwatch-needrestart.service kernelwatch-livepatch.service kernelwatch-apt-update.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/python3 -I
|
||||
"""Flatten `canonical-livepatch status --format json` (stdin) into KEY: value lines (stdout).
|
||||
|
||||
Run as root by kernelwatch-livepatch.service, whose output kernelwatch-agent reads
|
||||
without needing the Livepatch snap or a shell JSON parser. Only the running
|
||||
kernel's entry is used. Exits 1, printing nothing, if the input isn't Livepatch
|
||||
status for a running kernel (Livepatch not enabled, or an error message)."""
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
|
||||
def clean(value):
|
||||
return " ".join(str(value).split())[:64]
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
data = json.load(sys.stdin)
|
||||
entry = next(e for e in data["Status"] if e.get("Running"))
|
||||
except (ValueError, KeyError, TypeError, StopIteration, AttributeError):
|
||||
return 1
|
||||
livepatch = entry.get("Livepatch") or {}
|
||||
fixes = [f for f in livepatch.get("Fixes") or [] if isinstance(f, dict) and f.get("Patched")]
|
||||
for key, value in (
|
||||
("STATE", livepatch.get("State")),
|
||||
("CHECK", livepatch.get("CheckState")),
|
||||
("SUPPORTED", entry.get("Supported")),
|
||||
("UPGRADE-REQUIRED", entry.get("UpgradeRequiredDate")),
|
||||
("FIXES", len(fixes)),
|
||||
):
|
||||
if value is not None:
|
||||
print(f"LIVEPATCH-{key}: {clean(value)}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,30 @@
|
||||
[Unit]
|
||||
Description=Read Ubuntu Livepatch status (for kernelwatch)
|
||||
# Pulled in by kernelwatch-agent.service before each report. Skipped cleanly on
|
||||
# hosts without the Livepatch snap (or python3); the dashboard then treats a
|
||||
# pending kernel reboot as it always did.
|
||||
ConditionPathExists=/snap/bin/canonical-livepatch
|
||||
ConditionPathExists=/usr/bin/python3
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
# `canonical-livepatch status` needs root and a snap cgroup, so it can't run in
|
||||
# the agent's unprivileged unit. The helper keeps only the running kernel's
|
||||
# fields. If Livepatch isn't enabled or fails, no file is written and the agent
|
||||
# reports nothing for it.
|
||||
ExecStart=/bin/sh -c '/snap/bin/canonical-livepatch status --format json | /usr/bin/python3 -I /usr/local/sbin/kernelwatch-livepatch > /run/kernelwatch/livepatch.new && mv /run/kernelwatch/livepatch.new /run/kernelwatch/livepatch || { rm -f /run/kernelwatch/livepatch.new; exit 1; }'
|
||||
RuntimeDirectory=kernelwatch
|
||||
RuntimeDirectoryMode=0755
|
||||
RuntimeDirectoryPreserve=yes
|
||||
UMask=0022
|
||||
TimeoutStartSec=2min
|
||||
|
||||
# Root, no network. Snap's confinement (setuid snap-confine, mount namespaces)
|
||||
# rules out NoNewPrivileges, a capability bound, RestrictNamespaces and a
|
||||
# syscall filter, so the sandbox is limited to a read-only filesystem and no
|
||||
# network.
|
||||
User=root
|
||||
PrivateNetwork=yes
|
||||
ProtectSystem=strict
|
||||
ProtectHome=read-only
|
||||
PrivateTmp=yes
|
||||
Reference in new issue
Block a user