Flag hosts without room on disk for their pending upgrades
CI / tests (Python 3.10) (pull_request) Successful in 43s
CI / tests (Python 3.11) (pull_request) Successful in 44s
CI / tests (Python 3.12) (pull_request) Successful in 43s
CI / tests (Python 3.13) (pull_request) Successful in 42s
CI / lint (pull_request) Successful in 3s

A filesystem that fills up while apt downloads or dpkg unpacks leaves the
upgrade half-done and apt broken, the same failure as a full /boot but on
/ or /var. Nothing caught it until the host's updates stopped.

The agent (0.11.0) now works out what the pending upgrades need. A
simulation doesn't print apt's "Need to get" or "additional disk space"
lines, so it sums the package records from apt-cache show for each version
the simulation would install: Size for each .deb not already in
/var/cache/apt/archives, and the growth in Installed-Size over the
installed versions (from dpkg-query). Removals aren't subtracted, so it
errs high. It also reports free space and the mount point of the
filesystems holding /var/cache/apt/archives and /usr.

The server puts a host in update when the upgrades would leave less than
upgrade_min_free_mb (default 100, 0 disables) on either filesystem. The
download and the install both count when they're on the same one. With
nothing pending there is nothing to check, so this doesn't turn into
general disk monitoring. The host page gets a "Pending upgrades size" row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-11-07 20:16:54 -05:00
1 parent b5be19f85d
commit 15b1f5be4f
13 files changed
+363 -7

No files matched your search

+86 -1
View File
@@ -6,7 +6,7 @@
# rules can change without redeploying agents.
set -euo pipefail
AGENT_VERSION=0.10.0
AGENT_VERSION=0.11.0
# Prefix for every file the agent reads. Only the test suite sets this.
ROOT=${KERNELWATCH_ROOT:-}
@@ -335,6 +335,81 @@ boot_space() {
awk 'NR == 2 && $2 ~ /^[0-9]+$/ && $4 ~ /^[0-9]+$/ { print $2, $4 }' || true
}
# Free space in KiB and mount point of the filesystem holding $1, as "FREE MOUNT";
# empty if df can't tell. The mount point is everything after df's fifth column,
# so one with spaces survives.
fs_free() {
df -Pk "$1" 2>/dev/null |
awk 'NR == 2 && $4 ~ /^[0-9]+$/ {
m = $0
for (i = 1; i <= 5; i++) sub(/^[^ ]+ +/, "", m)
print $4, m
}' || true
}
# Archives apt downloads into before installing. The agent assumes the default.
APT_ARCHIVES=$ROOT/var/cache/apt/archives
# Space the pending upgrades need, in KiB, as "DOWNLOAD INSTALL", from the
# simulation's Inst lines on stdin; empty if apt-cache can't say. A simulation
# doesn't print apt's "Need to get" and "additional disk space" lines, so this
# adds them up from the package records instead: Size for each .deb not already
# in $APT_ARCHIVES (unattended-upgrades often downloads ahead), and the growth in
# Installed-Size, the new versions' minus the installed ones'. INSTALL can be
# negative. Packages the upgrade removes aren't counted, so it errs high.
upgrade_space() {
local name f3 f4 ver file size isize records dl=0 grow=0
local -a specs=() upgraded=()
while read -r _ name f3 f4 _; do
[[ -n $name ]] || continue
# "Inst NAME [OLD] (NEW ...)" for an upgrade, "Inst NAME (NEW ...)" for a new package.
if [[ $f3 == '['* ]]; then
ver=${f4#(}
upgraded+=("$name")
else
ver=${f3#(}
fi
specs+=("$name=$ver")
done
if ((${#specs[@]} == 0)); then
echo "0 0"
return 0
fi
# apt stores archives as NAME_VERSION_ARCH.deb with ':' (epochs) as %3a.
records=$(LC_ALL=C apt-cache -q \
-o Dir::Cache::pkgcache= \
-o Dir::Cache::srcpkgcache= \
show "${specs[@]}" 2>/dev/null | awk 'BEGIN { RS = "" } {
pkg = ver = arch = ""; size = isize = 0
n = split($0, line, "\n")
for (i = 1; i <= n; i++) {
if (line[i] ~ /^Package: /) pkg = substr(line[i], 10)
else if (line[i] ~ /^Version: /) ver = substr(line[i], 10)
else if (line[i] ~ /^Architecture: /) arch = substr(line[i], 15)
else if (line[i] ~ /^Size: /) size = substr(line[i], 7) + 0
else if (line[i] ~ /^Installed-Size: /) isize = substr(line[i], 17) + 0
}
if (pkg == "" || ver == "") next
gsub(/:/, "%3a", ver)
file = pkg "_" ver "_" arch ".deb"
if (seen[file]++) next
print file, size, isize
}') || return 0
[[ -n $records ]] || return 0
while read -r file size isize; do
grow=$((grow + isize))
[[ -e $APT_ARCHIVES/$file ]] || dl=$((dl + size))
done <<<"$records"
if ((${#upgraded[@]})); then
while read -r isize; do
[[ $isize =~ ^[0-9]+$ ]] && grow=$((grow - isize))
done < <(dpkg-query -W -f='${Installed-Size}\n' "${upgraded[@]}" 2>/dev/null || true)
fi
echo "$(((dl + 1023) / 1024)) $grow"
}
# Packages dpkg didn't finish installing or removing: half-installed, unpacked,
# half-configured, awaiting or pending triggers, or flagged reinst-required.
# The same states `dpkg --audit` reports; usually a failed postinst, cleared by
@@ -407,6 +482,7 @@ uptime_seconds() {
build_report() {
local running newest kernels sim inst sim_err rc apt_ok=1 apt_err='' reboot_file=0 reboot_pkgs=
local boot_size='' boot_free='' pro_attached='' pro_esm='' sec_suite='' sec_age=''
local up_dl='' up_grow='' arch_free='' arch_mount='' usr_free='' usr_mount=''
running=$(uname -r)
kernels=$(installed_kernels)
@@ -431,6 +507,9 @@ build_report() {
fi
read -r boot_size boot_free <<<"$(boot_space)" || true
((apt_ok)) && { read -r up_dl up_grow <<<"$(upgrade_space <<<"$inst")" || true; }
read -r arch_free arch_mount <<<"$(fs_free "$APT_ARCHIVES")" || true
read -r usr_free usr_mount <<<"$(fs_free "$ROOT/usr")" || true
read -r pro_attached pro_esm <<<"$(ubuntu_pro)" || true
read -r sec_suite sec_age <<<"$(security_repo)" || true
[[ $pro_esm == - ]] && pro_esm=
@@ -476,6 +555,12 @@ build_report() {
printf '"livepatch_fix_count":%s,' "$(json_int "$LP_FIXES")"
printf '"boot_size_kb":%s,' "$(json_int "$boot_size")"
printf '"boot_free_kb":%s,' "$(json_int "$boot_free")"
printf '"upgrade_download_kb":%s,' "$(json_int "$up_dl")"
printf '"upgrade_install_kb":%s,' "$(json_int "$up_grow")"
printf '"apt_archives_free_kb":%s,' "$(json_int "$arch_free")"
printf '"apt_archives_mount":%s,' "$(json_str_or_null "$arch_mount")"
printf '"usr_free_kb":%s,' "$(json_int "$usr_free")"
printf '"usr_mount":%s,' "$(json_str_or_null "$usr_mount")"
printf '"dpkg_unfinished_packages":%s,' "$(unfinished_packages | json_str_array)"
printf '"held_packages":%s,' "$(held_packages | json_str_array)"
case $pro_attached in