364. The binary host
This page is for anyone who builds KDOS and wants to publish what the build compiled, so that other KDOS machines can install those packages without compiling them.
A binary host (binhost) is a directory of prebuilt packages with a signed index over them. A KDOS machine that trusts the binhost’s key installs from it, with kdos update install or kpkg binhost, instead of building from source. A published binhost is the package repository of a release: the ISO carries only the base, and a machine installs everything else from the binhost of the release that its /ports/TREE names.
The first step of the image phase, script/phases/70_image/010_binhost.sh, writes the binhost. On an ordinary build it does nothing. With make build KDOS_MAKE_BINHOST=1 it moves every package the build kept into build/binhost/ and signs an index over them. It runs on build/fs, before the stage is trimmed to the base, so it sees every package. This page shows the step, the key it signs with, and which packages end up in the directory.
Switching it on
if [ "${KDOS_MAKE_BINHOST:-0}" != 1 ]; then
echo "[binhost] off — make build KDOS_MAKE_BINHOST=1 writes one"
exit 0
fi
script/phases/70_image/010_binhost.sh, lines 66–69
KDOS_MAKE_BINHOST comes from the make build command line, and reaches the step through the Makefile and script/chroot/exec.sh. With any value but 1, the step prints the line above and exits.
The flag has to be on for the whole build, not only for this phase, because it also decides whether packages are kept at all:
exec.shturns the same value intoKPKG_KEEP_CACHEfor every chroot command.- With
KPKG_KEEP_CACHEset, eachkpkg installkeeps the package it installs in/var/cache/kpkg/packagesinstead of deleting it, compressed with the heavierxz -9setting a kept package gets (see Anatomy of a kpkg install). - A package reused from the package store is copied into the cache as well.
- A port that is already installed and current is skipped by kpkg, so it contributes nothing.
On your machine:
make build KDOS_MAKE_BINHOST=1
The signing key
CACHE=/var/cache/kpkg/packages
OUT=/kdos/build/binhost
KEYDIR=/kdos/build/binhost-key
KEY=kdos-binhost
mkdir -p "$OUT" "$KEYDIR"
if [ ! -f "$KEYDIR/$KEY.key" ]; then
echo "[binhost] making the signing key $KEYDIR/$KEY.key"
( cd "$KEYDIR" && kpkg keygen "$KEY" )
fi
cp -f "$KEYDIR/$KEY.pub" "$OUT/$KEY.pub"
script/phases/70_image/010_binhost.sh, lines 71–81
The key pair is made once, by kpkg keygen, in build/binhost-key/, and every later run signs with the same key. kpkg writes the private half, kdos-binhost.key, with mode 0600 and refuses to overwrite an existing one. Neither half enters the image, because build/ is not part of the root.
The public half, kdos-binhost.pub, is copied beside the packages. A machine trusts the binhost by holding a copy of it in /etc/kdos/keys.
Keep the key directory. If you delete build/binhost-key/, the next run makes a new key, and every machine holding the old public half then refuses the index. make clean and make cleanbuild keep it, as they keep build/keys/ with the kernel’s module-signing key and the release-signing key, so only removing the directory by hand makes a new key.
Moving the packages
added=0
for p in "$CACHE"/*.tar.xz; do
[ -f "$p" ] || continue
mv -f "$p" "$OUT/"
added=$(( added + 1 ))
done
total=$(find "$OUT" -maxdepth 1 -name '*.tar.xz' | wc -l)
if [ "$total" -eq 0 ]; then
echo "FATAL: KDOS_MAKE_BINHOST=1 and no package was kept." >&2
echo " Packages are kept only by a build that installs them with the flag set." >&2
exit 1
fi
script/phases/70_image/010_binhost.sh, lines 83–95
Every package in the cache is moved into build/binhost/, replacing a file of the same name. The move leaves build/fs’s cache empty for the next build in either image mode; under KDOS_IMAGE=base, 020_cleanup.sh empties only the stage’s cache, never build/fs’s.
The directory accumulates from run to run. Packages left by earlier runs stay, and an incremental build adds only the packages it installed. A run in which nothing was built adds nothing. The step stops with an error only when the whole directory holds no package.
An older package left by an earlier run is indexed beside its successor, but with an empty E: (recipe hash). The index takes each package’s recipe hash from the ports tree at the moment the index is written, by package name (see The index), and gives it only to the file whose version and release match the recipe. The step then moves every file with an empty E: out of the directory (see Superseded packages), so you never have to clear old files by hand for the binhost to serve the current recipes.
One case the hash cannot catch: a package of the current version and release that was built from an earlier state of the recipe. That happens when a run without the flag rebuilt the port after the run that kept it. The kept file carries the current hash and passes a client’s recipe test. The next run with the flag that rebuilds the port copies the new package over it.
To get a binhost that holds nothing but this tree’s builds, start from an empty build/binhost/ and run a build that installs every port, which means a build from an empty tree. With the tree emptied as Starting from nothing describes:
rm -rf build/binhost
make build KDOS_MAKE_BINHOST=1 BUILD_ARGS=--fresh
The index
echo "[binhost] $added package(s) from this build, $total in $OUT"
# EVERY REPOSITORY THE BUILD INSTALLS FROM, so a package of KDOS's own gets
# its recipe's hash as its E: like any other. A machine looks its recipes up
# under /ports/src/<area>, the same files 105_ports.sh copies there, and
# computes the same hash; with /ports/core alone every src/ package would be
# indexed with an empty E:, which no machine takes.
#
# EACH PACKAGE'S N: LINE NAMES THE BUILD-ONLY PORTS IT NEEDS AT RUN TIME:
# its run-time needs record in this tree's database (/var/lib/kpkg/db/.needs,
# written by every `kpkg install`) resolved to the packages that provide
# them, intersected with ports/buildonly. `kdos update install` follows a
# dependency into a build-only port only where the line names it, so a
# prebuilt install of kcharselect takes no cmake; a stanza with no N: keeps
# every edge, and 087_gates.sh fails a build that leaves one.
bh_index() {
PORT_REPO="/ports/core /kdos/src/system /kdos/src/art /kdos/src/desktop /kdos/src/daemons" \
PKGDB_DIR=/var/lib/kpkg/db \
kpkg index "$OUT" --sign "$KEYDIR/$KEY.key" --buildonly /ports/buildonly
# Verified against the one key it was signed with, the way a client that
# trusts it will read it.
KPKG_KEYRING="$KEYDIR" kpkg verify-index "$OUT"
}
bh_index
script/phases/70_image/010_binhost.sh, lines 97–119
kpkg index --sign writes three things over every package in the directory: the index, PACKAGES; its signature, PACKAGES.sig; and a .sig signature beside each package.
The N: line of each stanza names the build-only ports that package needs at run time. --buildonly /ports/buildonly, with PKGDB_DIR at the build tree’s database, makes the index write it for each package file whose version and release are the ones installed there:
- kpkg reads the package’s run-time needs record,
/var/lib/kpkg/db/.needs/<name>. - It resolves each need to the installed package that provides it.
- It keeps the providers that
ports/buildonlylists.
kdos update install follows a dependency into a build-only port only where the N: line names it, so a prebuilt kcharselect does not pull in cmake. An empty N: means “measured, needs none”. A stanza with no N: at all is unmeasured: it keeps every edge, and it fails the gates when it belongs to a current package.
kpkg verify-index then reads the index back with KPKG_KEYRING pointed at build/binhost-key/, so the one key the index was signed with is the only key trusted, exactly as a client holding that key would read it. A failed verification stops the phase.
Two fields of the index limit which machines can use it:
| Field | Taken from | What a client must match |
|---|---|---|
B:, the build-config hash |
The environment the index is written in, which is the chroot’s | The same compiler and the same CC, CFLAGS, CXXFLAGS, LDFLAGS and KDOS_TARGET when it runs kpkg binhost, or every package reads as a different build config. kdos update install --source builds under /etc/kdos/chroot.env for that reason. |
E:, the recipe hash |
The PORT_REPO the step sets: /ports/core and the four areas of src/ at /kdos/src |
The same recipe. A machine finds KDOS’s own recipes under /ports/src/<area>, the copies 105_ports.sh stages, computes the same hash, and so takes a program of KDOS’s own prebuilt like any other port. |
The index format and the rules a client applies are in The binary host and Signing.
Superseded packages
# A SUPERSEDED FILE IS A PACKAGE STANZA (no O:, which marks a delta) WHOSE E:
# IS EMPTY: no client takes it and no release publishes it. bh_retire moves
# each, with its .sig, from <dir> into <dest>, outside the flat directory
# `kpkg index` lists, so the second index names none of them. It prints each
# name it moves: a current package an empty E: takes out by mistake is named
# in the log, and moving it back into <dir> restores it on the next run. A
# delta stays, whatever its base.
bh_superseded() {
awk -v RS= -F'\n' '{
file = ""; e = "-"; delta = 0
for (i = 1; i <= NF; i++) {
if ($i ~ /^F:/) file = substr($i, 3)
else if ($i ~ /^E:/) e = substr($i, 3)
else if ($i ~ /^O:/) delta = 1
}
if (file != "" && !delta && e == "") print file
}' "$1/PACKAGES"
}
bh_retire() {
local f
mkdir -p "$2"
while IFS= read -r f; do
[ -f "$1/$f" ] || continue
mv -f "$1/$f" "$2/"
if [ -f "$1/$f.sig" ]; then mv -f "$1/$f.sig" "$2/"; fi
printf '%s\n' "$f"
done < <(bh_superseded "$1")
}
retired=$(bh_retire "$OUT" /kdos/build/binhost-superseded)
if [ -n "$retired" ]; then
printf '[binhost] superseded, moved to /kdos/build/binhost-superseded: %s\n' $retired
if [ -z "$(find "$OUT" -maxdepth 1 -name '*.tar.xz' -print -quit)" ]; then
echo "FATAL: every package in $OUT is superseded." >&2
exit 1
fi
bh_index
fi
script/phases/70_image/010_binhost.sh, lines 121–157
A superseded package is a package stanza whose E: is empty: a file of an older version or release than its recipe’s, or of a port the tree has no recipe for. A stanza with an O: line is a delta and is never superseded, whatever its base. No client takes a superseded package and no release publishes one.
The step moves each superseded package, with its .sig, to build/binhost-superseded/. That directory sits beside build/binhost/ rather than inside it, because kpkg index lists every package in the one flat directory it is given. The step then indexes and verifies again, so PACKAGES, the catalogue and the release index below are all written over the same files, and a published PACKAGES never names a file the release does not carry. If every package in the directory is superseded, the phase stops.
The step prints the name of each file it moves. If it moves one by mistake (a current package whose recipe kpkg could not read), move that file and its .sig from build/binhost-superseded/ back into build/binhost/ before the next run.
The catalogue
# THE CATALOGUE NAMES ONLY WHAT THE INDEX HOLDS AND THIS TREE HAS INSTALLED:
# Discover offers each of its components as a port `kdos update install` takes
# from this binhost. A package file in the directory whose package this tree
# does not have stays indexed and goes uncatalogued. It is signed with the
# index's key, the one key a client checks the two against.
CAT=$OUT/swcatalog--xml--kdos-binhost.xml
rm -f "$CAT" "$CAT.sig"
if ! command -v kdos-discover-catalog >/dev/null 2>&1; then
echo "[binhost] kdos-discover-catalog is not installed — no catalogue"
else
pkgs=()
while IFS= read -r p; do
[ -f "/var/lib/kpkg/db/$p" ] && pkgs+=(--package "$p")
done < <(grep '^P:' "$OUT/PACKAGES" | cut -d: -f2- | LC_ALL=C sort -u)
if [ "${#pkgs[@]}" -eq 0 ]; then
echo "[binhost] no indexed package is installed in this tree — no catalogue"
else
kdos-discover-catalog --root / --origin kdos-binhost --out "$CAT" "${pkgs[@]}"
kpkg sign "$CAT" "$KEYDIR/$KEY.key"
echo "[binhost] $(grep -c '<component[ >]' "$CAT" || true) components in $CAT"
fi
fi
script/phases/70_image/010_binhost.sh, lines 159–180
Discover, the Plasma software centre, lists what a binhost can add from an AppStream catalogue beside its index:
- A client asks for it as
binhost/swcatalog/xml/kdos-binhost.xmland keeps it at that path under/var/lib/kdos/binhost.kdos update fetchdownloads it through the release index below and verifies it against the same keys. - The step writes it as
swcatalog--xml--kdos-binhost.xml, a flat name, because a GitHub release asset cannot sit in a directory. - It is written by
kdos-discover-catalog --origin kdos-binhost, from the kdos-discover port, and names each package the index lists that this tree has installed. A package file in the directory whose package this tree does not have stays in the index and gets no catalogue entry (component). kpkg signsigns it with the index’s key.
Each component carries its icon inline, base64-encoded in the custom value KDOS::IconData, because the machine reading the catalogue has not installed the package the icon comes in. Discover writes it to its icon cache. The icon is taken from this tree’s hicolor theme or /usr/share/pixmaps: the icon the metainfo names, else the desktop entry’s Icon=, at 64x64, else 48x48, else scalable, else a larger PNG scaled to 64x64, else a smaller PNG, and none over 32 KiB.
A tree without kdos-discover-catalog gets an index and no catalogue, and Discover then offers nothing from the binhost. How each component is read is on The software catalogue.
The release index
# THE RELEASE INDEX, over every file here but itself and the public key, the
# same set PACKAGES lists. A package larger than one GitHub asset stops the
# build: `kdos update install` takes a package whole, and the release could
# not carry it.
source ports/releaselib.sh
source ports/srclib.sh
IDX=$OUT/$REL_INDEX_FILE
rm -f "$IDX" "$IDX.sig"
version=${KDOS_GIT_COMMIT:-local}
version=${KDOS_GIT_RELEASE:-${version:0:12}}
too_big=0
for f in "$OUT"/*.tar.xz; do
[ -f "$f" ] || continue
if [ "$(stat -c %s "$f")" -gt "$SRC_PART_SIZE" ]; then
echo "FATAL: ${f##*/} is larger than one release asset ($SRC_PART_SIZE bytes)" >&2
too_big=1
fi
done
[ "$too_big" = 0 ] || exit 1
script/phases/70_image/010_binhost.sh, lines 182–200
The release index carries this tree’s packages and no others: the same set PACKAGES lists. A package larger than one GitHub release asset stops the phase, naming the file, because a machine takes a package whole and the release could not carry it. The limit is SRC_PART_SIZE from ports/srclib.sh: 1,900 MiB unless KDOS_PART_SIZE names another.
for f in "$OUT"/*; do
[ -f "$f" ] || continue
n=${f##*/}
case $n in
"$REL_INDEX_FILE"|"$REL_INDEX_FILE".*|*.pub|*.tmp) continue ;;
swcatalog--xml--kdos-binhost.xml) path=binhost/swcatalog/xml/kdos-binhost.xml ;;
swcatalog--xml--kdos-binhost.xml.sig) path=binhost/swcatalog/xml/kdos-binhost.xml.sig ;;
*) path=binhost/$n ;;
esac
rel_entry "$OUT" "$n" "$path"
done | rel_write "$IDX" "$version"
kpkg sign "$IDX" "$KEYDIR/$KEY.key"
KPKG_KEYRING="$KEYDIR" kpkg verify-pkg "$IDX"
echo "[binhost] $(rel_entries "$IDX" | wc -l) files in $IDX, version $version"
script/phases/70_image/010_binhost.sh, lines 201–214
kdos update never asks for a binhost file by its name alone. It asks the release index, kdos-update.idx, where the file is, and keeps the file only if it has the size and SHA-256 the index gives.
The step writes the release index last, over every file in the directory but the index itself and the public key:
| File | Path a client asks for |
|---|---|
| Each package, signature sidecar and delta | binhost/<name> |
| The package index and its signature | binhost/PACKAGES, binhost/PACKAGES.sig |
| The catalogue and its signature | binhost/swcatalog/xml/kdos-binhost.xml and .sig |
Every entry has the release -, which means “beside the index”. ports/releaselib.sh is the writer, the same one ports/publish --release uses for a release.
The index’s version is the release tag when the image is built from a clean tagged checkout (KDOS_GIT_RELEASE, which the recipe tree stamps into /ports/TREE too). Otherwise it is the short commit hash the build records, cut to twelve characters, which is what a machine whose /ports names no release calls its tree. kdos update refuses an index whose version differs from its tree’s.
kpkg sign signs the release index with the binhost key, and kpkg verify-pkg reads it back against that key alone. A machine trusting kdos-binhost.pub can therefore use the directory, copied to a USB stick or served as it is, as the place binhost = names in /etc/kdos/update.conf.
The format, and the published form, whose files are spread over releases binhost-<tag>-01, -02, … of at most 1,000 assets each, are in The release index and Publishing a release.
What it leaves
Nothing on the image. On your machine:
| Directory | Holds |
|---|---|
build/binhost/ |
The packages, PACKAGES, PACKAGES.sig, the per-package signatures, kdos-binhost.pub, the catalogue with its signature, and kdos-update.idx with its signature; every name flat |
build/binhost-superseded/ |
The superseded packages the step moved out, with their signatures |
build/binhost-key/ |
The key pair |
With KDOS_ISO_SOURCES=1, 110_iso.sh also puts a copy of build/binhost/ on the medium, under /sources/binhost, but only when the same build sets KDOS_MAKE_BINHOST=1 and the directory holds an index. A binhost left behind by an earlier opt-in build stays off the medium.
See also
- The binary host: the index, the tests a client applies and
kpkg binhost - Signing: the key rules
- Publishing a release: how
ports/publish --releaseputs this directory in a release - The package store: the other cache of built packages, used during a build