382. The recipe tree and the source offer
Every KDOS image carries the tree it was built from, at /ports, as recipes only. A shipped machine updates by building from that tree, and kpkg fetches each source over the network when a port is built, so none of the fetched tarballs has to be on the medium. This chapter is for anyone who wants to know what /ports on a running KDOS holds, which keys the image trusts for its updates, and where the source of everything on it can be had.
script/phases/70_image/105_ports.sh:
- stages the recipe tree;
- stamps it with the commit it came from;
- installs the keys a newer tree, and the prebuilt packages, must be signed with;
- writes
/usr/share/kdos/SOURCES, the image’s source offer: where the source of everything on the image can be had.
Staging the tree
umask 022
STAGE=/kdos/build/image-ports
TREE=$STAGE/ports
OFFER=/usr/share/kdos/SOURCES
ARCHIVE=https://github.com/kunaldawn/kdos
echo "Staging the recipe tree for /ports..."
rm -rf "$STAGE"
mkdir -p "$TREE/core"
for f in shelves sources.idx groups RELEASE buildonly buildheaders; do
[ -f "/ports/$f" ] || { echo "FATAL: /ports/$f is missing" >&2; exit 1; }
cp "/ports/$f" "$TREE/$f"
done
# `kdos update` reads 16 KiB of RELEASE and cuts the rest off mid-sentence.
if [ "$(stat -c %s "$TREE/RELEASE")" -gt 16384 ]; then
echo "FATAL: ports/RELEASE is over 16 KiB; kdos update notes shows only the first 16 KiB" >&2
exit 1
fi
script/phases/70_image/105_ports.sh, lines 57–76
In the chroot, /ports is a bind mount of the repository’s ports/ directory, which holds every fetched tarball beside its recipe and which the first squash of the root leaves out. It cannot be filtered where it is, so the tree is staged as a copy in build/image-ports/ports, and 110_iso.sh appends that to system.sfs as /ports. The staging directory is emptied first, and umask 022 keeps the group and other write bits off everything the step creates. Six files at the top of ports/ are copied, and a missing one stops the phase:
| File | What it is |
|---|---|
ports/shelves |
the list of shelves a port can sit on |
ports/sources.idx |
the index of the source archive |
ports/groups |
the groups of applications that kdos update install <group> and the installer offer; see Installing software |
ports/RELEASE |
the release notes that kdos update notes prints and What’s New shows |
ports/buildonly |
the build-only ports kdos update keeps out of a prebuilt plan |
ports/buildheaders |
the build-only ports a build on the machine installs prebuilt, because installed packages’ headers include them |
A RELEASE over 16 KiB stops the phase, because kdos update notes reads only the first 16 KiB and would cut the rest off.
nports=0
nfiles=0
for k in /ports/core/*/*/kpkgbuild; do
d=${k%/kpkgbuild}
rel=${d#/ports/}
unset hashed
declare -A hashed=()
while IFS= read -r f; do
[ -n "$f" ] && hashed[$f]=1
done < <(awk '$1 == "sha256" { print $4 }' "$k")
mkdir -p "$TREE/$rel"
while IFS= read -r -d '' f; do
f=${f#"$d"/}
case "$f" in
kpkgbuild|build.sh|postinstall.sh|*.patch) ;;
*) [ -n "${hashed[$f]:-}" ] && continue ;;
esac
case "$f" in
*.tar|*.tar.*|*.tgz|*.tbz2|*.txz|*.zip|*.7z|*.part|*.jar) continue ;;
esac
case "$f" in */*) mkdir -p "$TREE/$rel/${f%/*}" ;; esac
cp "$d/$f" "$TREE/$rel/$f"
nfiles=$((nfiles + 1))
done < <(find "$d" -mindepth 1 -name '.*' -prune -o -type f -print0)
nports=$((nports + 1))
done
[ "$nports" -gt 0 ] || { echo "FATAL: no recipe under /ports/core" >&2; exit 1; }
script/phases/70_image/105_ports.sh, lines 78–104
For each port under /ports/core/<shelf>/<name>, every file is copied except two kinds of source:
- a file the recipe hashes, named in a
sha256 = <hash> <file>line, which is how a recipe names its sources (see Checksums); - any archive, by its extension, whether or not a recipe names it. This catches a tarball that an earlier version of the recipe named and the present one does not, which would otherwise ride along.
A recipe file is never left out, even when a sha256 = line names it: kpkgbuild, build.sh, postinstall.sh and every .patch are in the recipe hash whatever names them. Without libgd’s heif-stride.patch, which its recipe also checksums, a machine would compute a recipe hash that no binhost package carries and have no patch to build with.
Files and directories whose names begin with a dot are skipped. What is left is the recipe, build.sh, patches, postinstall.sh, the port’s kfs.md note and any other file the recipe ships. No recipe at all under /ports/core stops the phase.
cp -R /kdos/src "$TREE/src"
for d in docs/kdos docs/screenshots docs/kfs script/book; do
[ -d "/kdos/$d" ] || { echo "FATAL: /kdos/$d is missing; kdos-doc and kdos-kfs link to it" >&2; exit 1; }
mkdir -p "$TREE/${d%/*}"
cp -R "/kdos/$d" "$TREE/$d"
done
find "$TREE/src" "$TREE/docs" "$TREE/script" -mindepth 1 \( -name '.*' -o -name __pycache__ \) -prune -exec rm -rf {} +
chmod -R u=rwX,go=rX "$STAGE"
nsrc=$(find "$TREE/src" -mindepth 3 -maxdepth 3 -name kpkgbuild | wc -l)
script/phases/70_image/105_ports.sh, lines 106–114
src/ is copied whole to /ports/src, because the programs of KDOS’s own are built from it and no archive holds their sources. Its areas stay side by side, since a desktop program’s build.sh reaches the libraries as $PORT_SRC/../../libs, and /etc/kpkg.conf names the areas that hold ports in its PORT_REPO.
The two books, docs/kdos with docs/screenshots, and docs/kfs with its renderer script/book, are copied to the same relative places under /ports. The ports kdos-doc and kdos-kfs, whose recipes are under src/system, reach them through links in their port directories: kdos-doc through kdos and screenshots (../../../docs/kdos, ../../../docs/screenshots), kdos-kfs through docs and book (../../../docs/kfs, ../../../script/book). The recipe hash follows those links. A link left dangling would drop out of the hash, so the tree on the machine would hash the port differently from the tree its package was built from, and kpkg would take the installed package for stale. A missing directory stops the phase. kdos-kfs builds only in a full checkout all the same, because its renderer also reads ports/core and script/phases.
Dot-files and __pycache__ directories are removed from the copies, and every mode is made readable by all.
# The tree must hold no source: one archive is the first of tens of gigabytes.
leak=$(find "$TREE/core" -type f -size +16M | head -n 5)
if [ -n "$leak" ]; then
echo "FATAL: the recipe tree holds files over 16 MB, which are sources:" >&2
echo "$leak" >&2
exit 1
fi
echo " $nports ports ($nfiles files), $nsrc of our own under src/, $(du -sh "$STAGE" | cut -f1)"
script/phases/70_image/105_ports.sh, lines 116–124
A file over 16 MB under the copied core/ is a source the filters above missed, and stops the phase, naming up to five of them. The staging then logs the number of ports, files and programs of our own, and the size of the staged tree.
The stamp and the key
tag=${KDOS_GIT_TAG:-}
release=${KDOS_GIT_RELEASE:-}
if [ -n "$tag" ] && ! [[ $tag =~ ^[A-Za-z0-9_+~][A-Za-z0-9._+~-]{0,39}$ ]]; then
echo "FATAL: tag '$tag' cannot name a release: letters, digits and . _ + ~ - only, not first . or -, at most 40" >&2
exit 1
fi
if [ -n "${KDOS_GIT_COMMIT:-}" ]; then
{
echo "kdos-tree 1"
echo "commit $KDOS_GIT_COMMIT"
echo "time ${KDOS_GIT_TIME:-0}"
[ -z "$release" ] || echo "version $release"
} > "$TREE/TREE"
chmod 0644 "$TREE/TREE"
if [ -n "$release" ]; then
echo " /ports/TREE names release $release"
elif [ -n "$tag" ]; then
echo " the checkout has changes: /ports/TREE does not name release $tag"
else
echo " HEAD carries no tag: /ports/TREE names no release, and prebuilt installs need binhost = in update.conf"
fi
else
echo " no commit given: /ports carries no TREE stamp"
fi
RELEASE_PUB=/kdos/build/keys/kdos-release.pub
TREE_RING=/etc/kdos/keys/tree
mkdir -p "$TREE_RING"
rm -f "$TREE_RING"/*.pub
if [ -f "$RELEASE_PUB" ]; then
install -m 0644 "$RELEASE_PUB" "$TREE_RING/kdos-release.pub"
echo " $TREE_RING trusts build/keys/kdos-release.pub"
else
echo " no build/keys/kdos-release.pub: $TREE_RING is empty and this image fetches no tree"
fi
script/phases/70_image/105_ports.sh, lines 161–194
/ports/TREE names the commit the tree is and that commit’s time, in the format ports/publish --release writes into a published tree. kdos update fetch refuses a signed tree whose TREE time is older than the one in /ports, and takes one of the same commit for current, so an image without the stamp would take any signed tree, an older one included. Without KDOS_GIT_COMMIT there is no stamp.
A fourth line, version <tag>, names the release the image is: the release whose signed kdos-update.idx the binhost is found through, so without it every prebuilt install refuses unless /etc/kdos/update.conf names a binhost = or KDOS_UPDATE_BINHOST is set. The line is KDOS_GIT_RELEASE, which script/chroot/exec.sh sets to the Makefile’s KDOS_GIT_TAG, the one release tag (v<digit>…) on HEAD, only when the checkout is clean, untracked files included: a dirty tree’s packages are not the release’s, and an archive tag (sources-N, src-*, packs-*) names no release. The step says which case it met: /ports/TREE names release <tag>, the checkout has changes: /ports/TREE does not name release <tag>, or HEAD carries no tag: /ports/TREE names no release, and prebuilt installs need binhost = in update.conf. A tag outside the alphabet ports/publish --release accepts stops the phase with FATAL: tag '<t>' cannot name a release: letters, digits and . _ + ~ - only, not first . or -, at most 40. How a tag is chosen and refused before the build starts is in The release tag.
/etc/kdos/keys/tree is the keyring kdos update fetch checks a published tree against. The step empties it of keys and installs the public half of the release key, build/keys/kdos-release.pub, the key ports/publish --release signs with. The key lives in build/keys/ on the machine that builds, never in git. A build without it ships an empty ring, and that image refuses every fetched tree rather than trusting an unsigned one.
/etc/kdos/keys itself, the host ring, is emptied of keys and gets the same build’s two: the binhost key’s public half, build/binhost-key/kdos-binhost.pub, which 010_binhost.sh signs the binhost with, and the release key’s, which signs the slot image and the release index. The release index, the binhost index, every prebuilt package and the slot image’s manifest are verified against this ring, so the image takes its own release’s updates and no other builder’s. A build with neither key ships the ring empty, and that image’s kdos update install --prebuilt and image lane refuse with no-key.
The source offer
recipe_of() {
local n=$1 r
for r in /ports/core/*/"$n"/kpkgbuild /kdos/src/*/"$n"/kpkgbuild; do
[ -f "$r" ] && { echo "$r"; return 0; }
done
return 1
}
hashes=$(mktemp)
for p in /var/lib/kpkg/db/*; do
[ -f "$p" ] || continue
r=$(recipe_of "${p##*/}") || continue
awk '$1 == "sha256" { print $3 }' "$r"
done | sort -u > "$hashes"
releases=$(awk 'NR == FNR { want[$1] = 1; next }
!/^#/ && ($1 in want) { print $2 }' "$hashes" /ports/sources.idx \
| sort -u -V)
unarchived=$(awk 'NR == FNR { if (!/^#/) have[$1] = 1; next }
!($1 in have)' /ports/sources.idx "$hashes" | wc -l)
rm -f "$hashes"
script/phases/70_image/105_ports.sh, lines 213–233
The step reads every installed package’s recipe, under /ports/core or /kdos/src, and collects the hash of every source it names. ports/sources.idx says which sources-<N> release of the archive holds each hash, so the releases the image’s sources are in are those the index gives for any of the collected hashes. The hashes the index does not hold are counted.
commit="${KDOS_GIT_COMMIT:-unrecorded}"
[ -n "${KDOS_GIT_COMMIT:-}" ] && [ "${KDOS_GIT_DIRTY:-0}" = 1 ] && commit="$commit-dirty"
mkdir -p "${OFFER%/*}"
{
echo "# The source of everything on this image."
echo "#"
echo "# The recipes and the programs of our own are the KDOS tree at this commit,"
echo "# and a copy of them is /ports. Each upstream source a package was built"
echo "# from is archived, by its sha256, in the releases below; /ports/sources.idx"
echo "# says which release holds which file."
echo
echo "repository $ARCHIVE"
echo "commit $commit"
[ -n "${KDOS_GIT_DATE:-}" ] && echo "date $KDOS_GIT_DATE"
for t in $releases; do
echo "release $ARCHIVE/releases/tag/$t"
done
} > "$OFFER"
chmod 0644 "$OFFER"
[ -n "$releases" ] || { echo "FATAL: no installed package's source is in sources.idx" >&2; exit 1; }
echo " commit $commit; releases: $(echo $releases)"
if [ "$unarchived" -gt 0 ]; then
echo " warning: $unarchived source(s) of installed packages are not in sources.idx;" >&2
echo " ports/publish has not run for them" >&2
fi
script/phases/70_image/105_ports.sh, lines 235–261
SOURCES names the repository, the commit (with -dirty when the tree had unrecorded changes, unrecorded without a commit), its date, and a release line for each archive release that holds a source of an installed package. The recipes and the programs of our own are the tree at that commit, a copy of which is /ports. A source the index does not hold is reported as a warning: ports/publish has not run for it, and a machine fetching it goes to the upstream URL its recipe names. If no installed package’s source is in the index at all, the phase stops.
KDOS’s licence text
This step writes no licence. The repository’s LICENSE, the MIT text of everything KDOS wrote, is on the tree from 000_file_system.sh on as /usr/share/licenses/kdos/LICENSE, and kpkg builds each package of our own with its /usr/share/licenses/<name>/LICENSE as a link to ../kdos/LICENSE, owned by that package, so a package of ours installed later from the binhost carries the link as the image’s copy does; see What kpkg does around build.sh.
What it leaves
On the image:
/etc/kdos/keys/tree/, holding the release key, or empty;/etc/kdos/keys/, holding whichever of the release and binhost keys the build has;/usr/share/kdos/SOURCES.
On your machine, build/image-ports/ports/, which becomes /ports on the image when 110_iso.sh appends it to system.sfs:
/ports/core/<shelf>/<name>/ each port's recipe files
/ports/shelves
/ports/sources.idx
/ports/groups
/ports/RELEASE the release notes
/ports/buildonly
/ports/buildheaders
/ports/TREE the commit, and the release on a clean tagged build
/ports/src/ the programs of KDOS's own
/ports/docs/kdos/ /ports/docs/screenshots/ /ports/docs/kfs/
/ports/script/book/
See also
- Packaging: 70_image: the same step in the build overview
- Updating a machine: how
/portsis used and replaced - kdos update:
fetchandapply - Sources and the archive:
sources.idxand thesources-<N>releases - The recipe hash: what the links feed into