369. Sweeping orphaned packages
This page explains how a package whose port was deleted from the tree is kept off the image. Read it if you remove a port, or if the image phase refuses to sweep.
build/fs is built incrementally: a package installed by one build stays installed through every later one unless something removes it. A port deleted from the tree therefore leaves its package behind, with its programs, libraries and service files, and nothing in a package phase notices.
script/phases/70_image/040_orphans.sh removes two kinds of package before the image is made:
- every installed package that has no recipe;
- every package whose recipe
ports/unbuiltlists.
This page shows how the step finds the recipes, and the two refusals that keep a broken lookup from emptying the image.
Where it looks for a recipe
DB=/var/lib/kpkg/db
# Every repository a phase's PORT_REPO names, and src/libs, searched the way
# kpkg searches them: a port sits directly under a repository or one shelf
# down. A repository or a shelf level missing here makes every package in it
# an orphan, and the sweep below deletes them all from the image.
REPOS="/ports/core /kdos/src/system /kdos/src/art /kdos/src/desktop /kdos/src/daemons /kdos/src/libs"
[ -d "$DB" ] || { echo "[KDOS] no package database — nothing to sweep"; exit 0; }
has_port() {
local repo d
for repo in $REPOS; do
for d in "$repo/$1" "$repo"/*/"$1"; do
[ -f "$d/kpkgbuild" ] && return 0
done
done
return 1
}
script/phases/70_image/040_orphans.sh, lines 29–46
REPOS is every repository a phase’s PORT_REPO names, and src/libs. has_port looks for a kpkgbuild directly under each repository and one shelf down, as kpkg itself does, so ports/core/cli/pv and src/system/kdos-tools are both found by name. See Where kpkg looks for a port.
The list is written out in the script, and it must hold every place a recipe can be. A repository or a shelf level missing from it makes every package from there look orphaned, and the sweep would uninstall them all. testing/preflight.sh fails when the list leaves out a src/ area that holds a recipe.
The refusals
# NO PORT FOUND ANYWHERE IS A BROKEN MOUNT OR A LAYOUT THE LOOKUP MISREADS, not
# an empty distribution: every installed package would read as an orphan and
# the ISO would ship with nothing in it. Stop the build instead.
nports=$(find /ports/core -mindepth 2 -maxdepth 3 -name kpkgbuild 2>/dev/null | wc -l)
if [ "$nports" -eq 0 ]; then
echo "[KDOS] ERROR: no kpkgbuild under /ports/core — refusing to sweep" >&2
exit 1
fi
UNBUILT=$(grep -v '^#' /ports/unbuilt 2>/dev/null || true)
unbuilt() {
local u
for u in $UNBUILT; do
[ "$u" = "$1" ] && return 0
done
return 1
}
orphans=""
norphans=0
ninstalled=0
for pkg in $(ls "$DB"); do
ninstalled=$((ninstalled + 1))
has_port "$pkg" && ! unbuilt "$pkg" && continue
orphans="$orphans $pkg"
norphans=$((norphans + 1))
done
script/phases/70_image/040_orphans.sh, lines 48–75
A package is an orphan when no recipe has its name, or when its name is in ports/unbuilt, the list of recipes no phase builds. A package of that second kind can only be left over from a build made before the port was listed there, and would otherwise ship.
The step stops the phase before it removes anything when /ports/core holds no recipe at all, which is a broken bind mount rather than an empty distribution. It also stops when more than half the installed packages would go:
# The same failure seen from the other side: a lookup that misses a whole
# repository or shelf level still finds some ports. Retiring half the system in
# one pass is never a port removal.
if [ $((norphans * 2)) -gt "$ninstalled" ]; then
echo "[KDOS] ERROR: $norphans of $ninstalled installed packages have no port — refusing to sweep" >&2
exit 1
fi
script/phases/70_image/040_orphans.sh, lines 77–83
Retiring half the system in one pass is never a port removal; it is a lookup that misses a whole repository or shelf level. Either way, sweeping would ship an image with most of the system missing.
The removal
if [ -z "$orphans" ]; then
echo "[KDOS] no orphaned packages — every installed package has a port"
exit 0
fi
echo "[KDOS] removing packages with no port, or a port no phase builds:"
for pkg in $orphans; do
echo " $pkg"
done
# One at a time and never `set -e`-fatal: an orphan whose manifest is damaged
# must not stop the build from shipping. kpkgdel reports what it could not do.
for pkg in $orphans; do
kpkgdel "$pkg" || echo " [WARN] $pkg: kpkgdel failed, left in place"
done
echo "[KDOS] orphan sweep done"
script/phases/70_image/040_orphans.sh, lines 85–101
Each orphan is listed in the log and then removed with kpkgdel, which deletes the files its database entry names and the entry itself. A package that fails to remove, for instance because its manifest is damaged, is reported with [WARN] and left in place, and the phase goes on: one damaged package does not stop the image from being made.
Why it runs here
The sweep runs before the hardware database, the manual index and the font cache are built, so an orphan’s udev rules, manual pages and fonts are not indexed into an image that does not carry them, and before 087_gates.sh checks the tree. Files under fs/ need no sweep: 000_file_system.sh keeps a manifest of them and removes a path fs/ has dropped.
What it leaves
Every package removed is gone from the root the step runs in and from its package database, /var/lib/kpkg/db, and so from the image:
- Under
KDOS_IMAGE=basethat root is the image stage. The orphan stays installed inbuild/fs. A package with no recipe is outside the base, so017_stage.shhas usually removed it from the stage before the sweep runs. - Under
KDOS_IMAGE=fullthe root isbuild/fs, and the orphan is gone for good.
The log names each package removed. On a tree that matches the recipes, it prints no orphaned packages.
See also
- Sweeping orphaned packages: the same rules in the build-system chapter
- Upgrades and removals: what
kpkgdeldoes - Files gone from fs/ are removed: the counterpart for
fs/