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

4. The build tree

Everything a build makes goes under one directory, build/ at the top of the repository, which is the only part of the repository the build container may write. Read this chapter when you need to find a log, free disk space or start a build again from nothing. It goes through what is in build/: the root being built, the cross toolchain, the markers and logs, the snapshots and caches, and the keys. It ends with what the two clean targets remove and how to start again from nothing.

The layout

A tree built as far as the middle of 41_system holds this much at its top:

build/
├── .devplan.json      the build plan in force
├── .kdosbuild         the compiled orchestrator
├── .kdosbuild.sum     the hash of what it was compiled from
├── .snap-lineage/     the index each next snapshot is compared against
├── ccache/            the compiler cache
├── cross/             the cross toolchain
├── fs/                the root filesystem being built
├── logs/              one log per step
├── mark/              the early phases' "already done" markers
├── snapshots/         phase snapshots and the build's timings
└── tmp/               scratch space for the early phases

Later phases and opt-in settings add more: iso-build/ with the finished kdos.iso, keys/, pkgstore/, image-ports/, kdos-base/, binhost/ and binhost-key/, the image stage’s image-upper/, image-work/, image-root/ and image-stage.ok, and the jobserver FIFO of a run given --port-jobs above 1. Other targets add their own: kdos.qcow2 from make run, fetch-home/ from make fetch, and kfs/, where make book renders this book. The full table, with every file and who writes it, is in Where the build puts things.

fs: the root being built

build/fs is the target tree, the root filesystem of the system being built. 10_bootstrap creates it, every later phase installs into it, and the image phase makes the binhost from it and the ISO’s system.sfs from a stage over it, trimmed to the base (see The image stage); under KDOS_IMAGE=full the ISO is made from build/fs itself. It is also the chroot every phase from 20_selfhost on runs inside.

It belongs to root on your machine and stays that way. When a build ends, script/kdosbuild.sh hands every other entry of build/ back to your user, but not this one, nor the image stage’s image-upper, image-work and image-root, which are layers of the same root: changing the owner of a file clears its setuid bit and rewrites its owner, and the root’s setuid programs and its /etc/shadow must ship exactly as the packages installed them. Reading or removing build/fs therefore needs root, or a container such as the one in A shell in the container.

Its package database, build/fs/var/lib/kpkg/db, is the record of what is installed, which recipe each package came from, and which files it owns. The database entry shows one.

cross: the cross toolchain

build/cross holds the binutils and GCC that 00_cross builds, which run in the container and produce programs for x86_64-kdos-linux-musl. 10_bootstrap compiles the first root with them. Nothing in build/cross is installed into the root.

mark: what the early phases have done

Every script of 00_cross and 10_bootstrap except the first step of 10_bootstrap ends by creating a marker file, and starts by exiting at once when its marker is already there. The markers are under build/mark/cross/ and build/mark/bootstrap/, one empty file per step, named after what the step built:

build/mark/cross:      binutils  gcc
build/mark/bootstrap:  bash  binutils  coreutils  diffutils  findutils  gawk  gcc  grep  gzip
                       kinstall  kpkg  libstdc++  linux_headers  make  musl_libc  ncurses  patch
                       readline  sed  tar  xz

The guard at the top of 00_cross/00_binutils.sh is typical:

if [ -f "$MARK/binutils" ] && [ "${KDOS_REPLAY:-0}" != "1" ]; then
    exit 0
fi

script/phases/00_cross/00_binutils.sh, lines 17–19

This is why a second run of the two early phases costs seconds. A marker stands down when kdosbuild exports KDOS_REPLAY=1, which it does for a step a build plan names explicitly, so a named step runs again whatever its marker says. The marker for kpkg is the one exception to “empty”: it holds a hash of every source file kpkg is compiled from and of the step script that compiles it, so that a change to kpkg, its libraries or its compile line rebuilds it. See Guards in the early phases.

logs: one file per step

Every step writes all its output, verbatim, to its own file:

build/logs/<phase>/<NNNN>_<name>.log

NNNN is the step’s position within its phase, counted from zero. A script step is named after its script, less the number in front, and a package step after its port with .install added:

build/logs/00_cross/0000_binutils.sh.log
build/logs/10_bootstrap/0000_file_system.sh.log
build/logs/41_system/0437_pv.install.log

A port’s number is its step’s position in the phase’s resolved order, which counts every step before it, the level commit steps of --port-jobs included, so it changes whenever ports are added to or removed from the phase.

When --port-jobs builds a phase level by level, each level’s install step has a log of its own, <NNNN>_L<level>.commit.log. A few files are not step logs. Directly under build/logs/ sit snapshots.log, every notice the build shows, with the time; chroot.log, warnings from the chroot wrapper; and, after a run with KDOS_PKG_STORE=check, pkgstore-check.log, every port whose rebuild differed from its stored package. A phase whose package list could not be resolved gets an expansion.log in its own directory saying why. When a step fails, its log is the first thing to read; see Build troubleshooting.

snapshots: restore points and timings

When a run is asked to write snapshots, each phase from 00_cross to 50_desktop is archived as it completes into build/snapshots/<phase>/, so that a later build can start from it instead of from the beginning. A snapshot directory holds one compressed archive per path the phase declares and a manifest.json describing it:

build/snapshots/30_foundation/
├── fs.tar.zst
└── manifest.json

The first snapshot of a path holds the whole tree; each later one is a layer holding only what changed since the one before, compared against the index in build/.snap-lineage/. A snapshot other snapshots still layer on is moved to build/snapshots/.held/ when it is replaced, until nothing needs it. Writing snapshots is off unless a run asks for it; Writing and listing snapshots shows how to ask, and Resuming and restoring how to start a build from one.

The same directory holds timings.json, the seconds each step took the last time it did its work and the machine each was measured on, which the book’s build times are computed from; see Typographical conventions. Like the snapshots, it survives make cleanbuild.

Caches and the package store

build/ccache is the compiler cache that CMake ports in the chroot compile through, up to 20 GB. build/pkgstore holds every package the chroot phases build when make build KDOS_PKG_STORE=1 is given, so that a later build can install a port whose inputs have not changed instead of building it again. Both make a build faster and change nothing about what it produces; see The compiler cache and The package store.

keys: signing keys

build/keys holds keys, not build output: the kernel’s module-signing key, which the linux port writes there the first time it builds without one, and the release-signing key when a maintainer has made one. It survives both clean targets. Losing the module-signing key means the next kernel carries a new certificate, so modules from earlier builds load only as tainted under it.

tmp and the rest

build/tmp is scratch space for 00_cross and 10_bootstrap, emptied at the start of each of their steps. build/.kdosbuild is the orchestrator, compiled from source at the start of every make build when its sources, flags or compiler differ from the hash in build/.kdosbuild.sum. build/.devplan.json is the build plan in force, which the plan picker opens on. build/jobserver is a FIFO holding the job slots that the ports of one level share when a run is given --port-jobs above 1; kdosbuild makes it at the first such level and replaces one left by a killed run.

Cleaning

Two targets empty build/, and they differ in what they keep:

# Wipe the build tree but keep build/snapshots, so a phase can still be
# restored, and build/ccache and build/pkgstore, whose contents stay valid for
# the next build. build/.snap-lineage goes with the tree it indexes, so the
# next snapshot is full.
#
# build/keys and build/binhost-key survive BOTH of these: a key is not a build
# artefact. Every image built beside kdos-release.key trusts its
# kdos-release.pub, and the key signs each `ports/publish --release`; lose it
# and no later release verifies there. linux-module-signing.pem signs the
# kernel's modules: lose it and the next kernel carries a new certificate, so
# modules from earlier builds load only as tainted. kdos-binhost.key in
# build/binhost-key signs the binhost's index and 010_binhost.sh makes it only
# when it is missing; lose it and the next KDOS_MAKE_BINHOST=1 build signs with
# a new key, which every machine holding the old kdos-binhost.pub refuses.
cleanbuild: ## wipe build/ except snapshots, ccache, pkgstore, keys and binhost-key
	test -d build && find build -mindepth 1 -maxdepth 1 \
		! -name snapshots ! -name keys ! -name binhost-key \
		! -name ccache ! -name pkgstore \
		-exec rm -rf {} + || true

# Removes build/snapshots, build/ccache and build/pkgstore along with everything
# else. Not build/keys or build/binhost-key.
clean: ## wipe build/ except keys and binhost-key
	test -d build && find build -mindepth 1 -maxdepth 1 \
		! -name keys ! -name binhost-key -exec rm -rf {} + || true

Makefile, lines 290–314

make cleanbuild keeps the snapshots, the compiler cache and the package store, which stay valid for the next build. make clean removes those too. Both keep build/keys and build/binhost-key, because a key is not a build artefact: every image built beside the release key trusts its public half, and a binhost signed with a new key is refused by every machine that holds the old kdos-binhost.pub.

Both run on your machine as your user, and build/fs belongs to root, so neither can remove it: the find fails on it, the target ignores the failure, and the old root is still there for the next build to run on. The image stage’s directories are root’s too. Remove them from a container:

docker run --rm -v "$PWD/build:/b" os-dev rm -rf /b/fs /b/image-upper /b/image-work /b/image-root

Starting from nothing

A build does not empty the tree it finds. Every early step whose marker exists exits at once, and every port installed from an unchanged recipe is skipped, which is what makes a second build cheap. To build from nothing, remove the three directories that record that progress, from a container, and run every phase:

docker run --rm -v "$PWD/build:/b" os-dev rm -rf /b/fs /b/mark /b/cross
make build BUILD_ARGS=--fresh

The snapshots, caches and keys are left alone. Budget a day or more for the build that follows; see Building from scratch.

See also