Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

366. The image stage

This page is for anyone building KDOS who wants to know what the installation medium carries, or who needs to add a program to it.

build/fs holds every package the build installed, about 2,450 of them, and both the next build and the binhost are made from it. The ISO carries only a subset, the base: the ports ports/base names and their dependency closure, about 900 packages. A machine installs every other package from the binhost of its release.

So the image is not made from build/fs itself but from a stage, an overlay over it. script/phases/70_image/017_stage.sh trims that stage to the base, and then trims each package left to its run-time files. This page shows where the stage comes from, how the step decides what to remove, and how to build an ISO that carries every package instead.

Runs in: the stage, build/image-root, an overlay over build/fs; not at all under KDOS_IMAGE=full
Reads: ports/base, ports/buildonly, the recipes and the phase lists, from /kdos; the stage’s package database
Writes: whiteouts in build/image-upper for every package it removes and every development and documentation file it strips; the stage’s /var/lib/kpkg/db/.facets/ and /etc/kpkg.conf; the mark build/image-stage.ok

The overlay

The stage is made by script/chroot/exec.sh, not by this step; The image stage in the chroot chapter shows the lines. Under KDOS_IMAGE=base, the default, every 70_image step from 017_stage.sh on is chrooted into build/image-root, an overlay with two layers:

010_binhost.sh and 015_pkgstore.sh run before the stage, on build/fs, so the binhost and the package store are made from every package.

This step starts the upper layer empty, so each image run trims a fresh copy of the tree as it is now. A mark file guards the order of the steps:

  1. exec.sh removes build/image-stage.ok when it starts this step.
  2. The step writes it again when it has finished.
  3. Every later step is refused while the mark is missing, so a later step run with no finished 017_stage.sh behind it stops instead of imaging an untrimmed tree.

KDOS_IMAGE=full

if [ "${KDOS_IMAGE:-base}" = full ]; then
    record_build_env
    echo "[stage] KDOS_IMAGE=full — the image carries every installed package"
    exit 0
fi

script/phases/70_image/017_stage.sh, lines 89–93

With make build KDOS_IMAGE=full, exec.sh mounts no overlay and every step runs on build/fs, as the other phases do. This step records the build’s environment (below), prints the line above and exits. The ISO then carries every installed package. Use it for a machine that must install with no network, and for the test rig. The Makefile and exec.sh both refuse any value but base and full. On your machine:

make build KDOS_IMAGE=full BUILD_ARGS="--phases 70_image"

The refusal

rootfs=$(awk '$2 == "/" { t = $3 } END { print t }' /proc/mounts)
if [ "$rootfs" != overlay ]; then
    echo "FATAL: / is $rootfs, not the image stage's overlay; refusing to remove packages from build/fs" >&2
    exit 1
fi

script/phases/70_image/017_stage.sh, lines 95–99

The step removes packages, and the same kpkg remove run over build/fs would delete most of the build. It reads the type of the filesystem mounted at / from /proc/mounts and stops unless it is overlay, which it is only when exec.sh made the stage.

The removal

python3 /kdos/script/lib/imageset.py --tree /kdos --check
drop=$(python3 /kdos/script/lib/imageset.py --tree /kdos --root / --drop)
n=$(printf '%s\n' "$drop" | grep -c . || true)
echo "[stage] removing $n package(s) outside the base"
if [ "$n" -gt 0 ]; then
    log=/kdos/build/logs/70_image/stage-remove.log
    if ! printf '%s\n' "$drop" | xargs kpkg remove > "$log" 2>&1; then
        grep -v '^==> \(Removing\|Package .* removed\|Keeping\)' "$log" | tail -40 >&2
        echo "FATAL: kpkg remove failed; its whole output is $log" >&2
        exit 1
    fi
fi
left=$(printf '%s\n' "$drop" | while IFS= read -r p; do
    if [ -n "$p" ] && [ -f "/var/lib/kpkg/db/$p" ]; then printf '%s ' "$p"; fi
done)
if [ -n "$left" ]; then
    echo "FATAL: still installed after the removal: $left" >&2
    exit 1
fi
python3 /kdos/script/lib/imageset.py --tree /kdos --root / --sizes

script/phases/70_image/017_stage.sh, lines 101–120

script/lib/imageset.py computes the set in two calls:

The set is the closure of the seeds over each recipe’s first depends = line. ports/base names its seeds in three forms:

Form Means Used for
Port names, any number to a line Those ports Almost every line
phase <dir> Every port that phase’s list names No line of ports/base
src <area> Every recipe under src/<area> art, desktop and daemons. The ports of src/system are named one by one, so that kdos-kfs, the build book, stays off the medium.

The seeds cover:

The closure does not follow a dependency into a port that ports/buildonly lists, unless ports/base names that port itself. That list holds the compilers and their bootstraps (clang, libclc, spirv-llvm-translator and binutils among them), the build systems and generators, the headers and shader compilers a build reads, the Python build backends, the font builders and the documentation generators.

The list is needed because depends = names build and run-time needs alike: without it, the closure would follow every recipe into the compilers it was built with. Three cases show where the line falls:

Every dropped name goes to one kpkg remove, so no removal is refused for a reverse dependency that is going too. kpkg’s index triggers then rebuild the font, icon, MIME, desktop-entry and linker caches once, for the trimmed tree.

The removal’s output, a line per package, goes to build/logs/70_image/stage-remove.log. If the removal fails, the step prints the lines that are not progress and stops the phase; it also stops if any dropped package is still installed afterwards. --sizes then logs the number of packages kept and the bytes their files take. On a full build tree of 2,446 packages, the base is 906 packages and 5.54 GB.

The facets

record_build_env
echo "[stage] removing the development and documentation facets"
strip=$(kpkg facets --strip)
printf '%s\n' "$strip" | sed 's/^==> /[stage] /'
conf=$(awk '!/^FACETS=/' /etc/kpkg.conf)
printf '%s\nFACETS="${KPKG_FACETS:-run}"\n' "$conf" > /etc/kpkg.conf.new
mv /etc/kpkg.conf.new /etc/kpkg.conf
grep -qx 'FACETS="${KPKG_FACETS:-run}"' /etc/kpkg.conf

script/phases/70_image/017_stage.sh, lines 122–129

BUILD_SHARE=/usr/share/kdos/build
record_build_env() {
    local k
    install -Dm644 /kdos/script/env/config.site "$BUILD_SHARE/config.site"
    install -Dm755 /kdos/script/bin/ninja "$BUILD_SHARE/bin/ninja"
    install -Dm755 /kdos/script/bin/meson "$BUILD_SHARE/bin/meson"
    {
        for k in CC CXX CFLAGS CXXFLAGS FFLAGS FCFLAGS LDFLAGS KDOS_TARGET \
                 CMAKE_BUILD_TYPE CARGO_PROFILE_RELEASE_DEBUG GOFLAGS \
                 CGO_CFLAGS CGO_CXXFLAGS CGO_LDFLAGS \
                 LIBGIT2_NO_VENDOR RUSTONIG_SYSTEM_LIBONIG \
                 LIBSQLITE3_SYS_USE_PKG_CONFIG ZSTD_SYS_USE_PKG_CONFIG \
                 SOURCE_DATE_EPOCH PIP_ROOT_USER_ACTION; do
            if [ -n "${!k+x}" ]; then printf '%s=%s\n' "$k" "${!k}"; fi
        done
        printf 'CONFIG_SITE=%s\n' "$BUILD_SHARE/config.site"
        printf 'KDOS_BUILD_PATH=%s\n' "$BUILD_SHARE/bin"
        printf 'KPKG_CC_ID=%s %s\n' "${CC:-cc}" "$("${CC:-cc}" -dumpversion)"
    } > /etc/kdos/chroot.env
    chmod 0644 /etc/kdos/chroot.env
    echo "[stage] /etc/kdos/chroot.env: $(grep '^KPKG_CC_ID=' /etc/kdos/chroot.env)"
}

script/phases/70_image/017_stage.sh, lines 66–87

First the step writes /etc/kdos/chroot.env, the record of how this build compiles:

kdos update loads the file before each kpkg it runs, and kpkg reads KPKG_CC_ID in place of asking a compiler. As a result, a machine installed from the image computes the binhost’s build-config hash and takes its packages, and a port built on that machine is built as the binhost’s was. Without the -sys switches, bat would link the copy of oniguruma its crate carries; without the site file, an autoconf port would put its configuration under /usr/etc.

What is left is the base, every package of it whole. kpkg facets --strip then removes from each package the files only a compiler or a reader of manuals uses:

The rules, and the exceptions that keep a running program whole, are in Facets. Each package’s manifest and file record are rewritten without those files, and each removed file is listed in /var/lib/kpkg/db/.facets/<name>, so kpkg check passes the stripped tree. Every package prints a line, and the last line says how much went: on a full build tree, about 0.95 GB of the base’s 5.5 GB, 270 MB of it gcc’s compiler.

The stage’s /etc/kpkg.conf then says FACETS="${KPKG_FACETS:-run}", so every later install on the medium, and on a machine installed from it, leaves the same files out. build/fs keeps FACETS=all and every file, which is what the next build compiles against. On a machine:

kpkg facets --list zlib                  # what zlib was installed without
sudo kdos update install --dev zlib      # zlib again, whole, from the binhost

What proves the base whole

Nothing in this step checks that the base runs. Every later step reads the trimmed stage, and 087_gates.sh is the proof:

A port that a base program needs at run time and that the closure missed stops the build there. The usual causes are a dependency a recipe does not declare, or a port wrongly listed in ports/buildonly.

To put a port on the medium, add it to ports/base and run the image phase again:

make build BUILD_ARGS="--phases 70_image"

What it leaves

On your machine:

They belong to root, like build/fs, and kdosbuild does not give them back to you when the build ends. build/fs is unchanged. The ISO, written by 110_iso.sh, is the stage.

See also