1. The build host and the os-dev container
Every phase of the KDOS build runs inside one container image, os-dev, built from the repository’s own Dockerfile. Read this chapter before your first build, or when you need to look inside a build by hand. It shows that file, explains what each package in it is for, goes through the docker run line make build starts it with, switch by switch, and shows how to open a shell in the same container yourself.
Why a container, and no other host
Your machine provides Docker, the network for fetching sources, and disk space. It provides no compiler, no library and no tool that reaches the result. The first two phases, 00_cross and 10_bootstrap, compile with whatever compiler and userland they find, and they find the container’s, which are the same on every machine. Every later phase runs inside the root those two phases built, with its own compiler. So a build on one developer’s machine and a build on another’s start from identical tools, whatever distribution each runs.
There is no supported way around the container. A build on your machine’s own toolchain would make the cross compiler with your compiler, configure the bootstrap with your userland, and could link against your libraries; none of that is tested, and the result would depend on the machine that made it. The container also gives the build two properties it relies on: it has no network, so a port that tries to download something fails at once instead of quietly depending on it, and it is privileged, so the later phases can mount filesystems inside the new root and chroot into it.
The image is Alpine Linux, which is built on musl, the C library KDOS itself uses. The compilers that start the bootstrap therefore already produce musl programs, and the tools make build compiles for itself inside the container, kdosbuild among them, are musl programs as well. Why the system is musl at all is in musl as the host C library, and why the bootstrap builds the GNU userland rather than relying on a smaller one is in GNU at every name.
The image
The base image:
FROM alpine:3.23
and everything installed on top of it, with the settings the build expects:
RUN apk update && apk add --no-cache \
nano \
git \
curl \
wget \
bash bash-doc \
coreutils coreutils-doc \
grep sed gawk findutils diffutils patch \
texinfo \
make \
gcc gcc-doc \
g++ \
musl-dev \
bison \
flex \
linux-headers \
rsync \
xz \
tar \
zstd \
python3
# Snapshot archives and the build TUI both assume UTF-8.
ENV LANG=C.UTF-8
WORKDIR /workspace
CMD ["/workspace/script/kdosbuild.sh"]
The meaning of the packages
gcc,g++,musl-dev,linux-headers-
The compilers that build the cross toolchain in
00_crossand kdosbuild itself, and the C library headers and kernel headers they compile against. Nothing they produce is installed into the new root: they build the cross compiler, and the cross compiler builds the root. The libraries GCC itself needs, GMP, MPFR and MPC, are not taken from Alpine; the cross GCC script unpacks them from their own ports and builds them inside GCC’s source tree. make,bison,flex,texinfo-
What the configure scripts and makefiles of binutils, GCC and the GNU userland call while they build:
bisonandflexgenerate parsers, andtexinfoprovidesmakeinfofor their manuals. bash,coreutils,grep,sed,gawk,findutils,diffutils,patch-
The GNU userland, so that
00_crossand10_bootstrapconfigure and build under the same tools upstream tests against, and that the tree’s own later phases use, rather than under the BusyBox applets an Alpine image has by default. The build scripts are bash scripts. tar,xz,zstd-
GNU tar, which unpacks the sources of
00_crossand10_bootstrapand writes every snapshot;xzfor the many sources shipped as.tar.xz;zstd, which kdosbuild compresses phase snapshots with when it is present. The later phases unpack their sources with the tools of the root being built. rsync-
Called by the kernel’s
headers_installtarget, which10_bootstrapuses to install the Linux API headers into the root. python3,git,curl,wget,nano,bash-doc,coreutils-doc,gcc-doc-
No step of
00_crossor10_bootstrapnames these. They make the image a usable shell for looking at a tree by hand. The repository’s.gitdirectory is never mounted into the container, sogitthere cannot see the checkout. ENV LANG=C.UTF-8- The snapshot archives hold UTF-8 file names, and kdosbuild draws its build screen in UTF-8.
WORKDIR /workspace,CMD-
The repository is mounted at
/workspace, and a container started with no command runs the build.make buildnames the command explicitly.
The image is rebuilt by every make build, which runs docker build -t os-dev . first. Docker’s cache makes that instant when the Dockerfile has not changed. When it has changed, building the image is the one part of make build that uses the network: it pulls the Alpine image and its packages.
How make build starts the container
This is the whole of the make build target, with the release tag it works out first:
# The release this image is: empty takes the one release tag on HEAD, a tag
# named `v<digit>…` (`v0.2`, `v0.2.1`); the archive tags `ports/publish` makes
# (`sources-N`, `src-*`, `packs-*`) never name a release. HEAD with several
# release tags needs KDOS_GIT_TAG=<one of them>, which must be a release tag
# on HEAD and a name `ports/publish --release` accepts, and KDOS_GIT_TAG=none
# builds a tagged commit as an unreleased one. A clean checkout's image writes
# the release as `version` into /ports/TREE and into build/binhost's index,
# the name `kdos update` finds the release's binhost by; publish refuses an
# ISO that names another release.
KDOS_GIT_TAG ?=
kdos_head_tags = $(shell git tag --points-at HEAD --list 'v[0-9]*' 2>/dev/null)
kdos_tag = $(if $(filter none,$(KDOS_GIT_TAG)),,$(if $(KDOS_GIT_TAG),$(KDOS_GIT_TAG),$(kdos_head_tags)))
build: check-iso-free ## build everything in the container (BUILD_ARGS narrows it)
@case "$(KDOS_IMAGE)" in base|full) ;; *) echo "KDOS_IMAGE must be base or full, got '$(KDOS_IMAGE)'" >&2; exit 1;; esac
@case "$(KDOS_JOBS)" in *[!0-9]*|0*) echo "KDOS_JOBS must be a positive integer, got '$(KDOS_JOBS)'" >&2; exit 1;; esac
@case "$(KDOS_ISO_GATE)" in 0|1) ;; *) echo "KDOS_ISO_GATE must be 0 or 1, got '$(KDOS_ISO_GATE)'" >&2; exit 1;; esac
@case "$(KDOS_ISO_MAX)" in ''|*[!0-9]*|0*) echo "KDOS_ISO_MAX must be a positive number of bytes, got '$(KDOS_ISO_MAX)'" >&2; exit 1;; esac
@set -- $(kdos_tag); [ $$# -le 1 ] || { echo "HEAD carries several release tags ($(kdos_tag)): name the release with KDOS_GIT_TAG=<tag>, or KDOS_GIT_TAG=none for none" >&2; exit 1; }
@t='$(kdos_tag)'; [ -z "$$t" ] || case " $(kdos_head_tags) " in *" $$t "*) ;; *) echo "KDOS_GIT_TAG=$$t is not a release tag (v<digit>…) on HEAD" >&2; exit 1;; esac
@t='$(kdos_tag)'; [ -z "$$t" ] || printf '%s\n' "$$t" | grep -Eqx '[A-Za-z0-9_+~][A-Za-z0-9._+~-]{0,39}' || { echo "tag '$$t' cannot name a release: letters, digits and . _ + ~ - only, not first . or -, at most 40" >&2; exit 1; }
mkdir -p build
docker build -t os-dev .
docker run --network none --cpu-shares=$(KDOS_CPU_SHARES) \
$(if $(KDOS_JOBS),--cpus=$$(n=$$(nproc); j=$(KDOS_JOBS); [ $$j -lt $$n ] && echo $$j || echo $$n)) \
--rm --privileged -e HOST_UID=$$(id -u) -e HOST_GID=$$(id -g) \
-e KDOS_JOBS="$(KDOS_JOBS)" \
-e KDOS_GIT_COMMIT="$$(git rev-parse --short=12 HEAD 2>/dev/null)" \
-e KDOS_GIT_DIRTY="$$(test -n "$$(git status --porcelain 2>/dev/null)" && echo 1 || echo 0)" \
-e KDOS_GIT_DATE="$$(git log -1 --format=%cs 2>/dev/null)" \
-e KDOS_GIT_TIME="$$(git log -1 --format=%ct 2>/dev/null)" \
-e KDOS_GIT_TAG="$(kdos_tag)" \
-e KDOS_ISO_SOURCES="$(KDOS_ISO_SOURCES)" \
-e KDOS_PACK_KDOS="$(KDOS_PACK_KDOS)" \
-e KDOS_MAKE_BINHOST="$(KDOS_MAKE_BINHOST)" \
-e KDOS_ISO_COMP="$(KDOS_ISO_COMP)" \
-e KDOS_ISO_BLOCK="$(KDOS_ISO_BLOCK)" \
-e KDOS_ISO_MAX="$(KDOS_ISO_MAX)" \
-e KDOS_ISO_GATE="$(KDOS_ISO_GATE)" \
-e KDOS_IMAGE="$(KDOS_IMAGE)" \
-e KDOS_CCACHE="$(KDOS_CCACHE)" \
-e KDOS_PKG_STORE="$(KDOS_PKG_STORE)" \
-e KDOS_PKG_STORE_MAX="$(KDOS_PKG_STORE_MAX)" \
-v $$(pwd)/build:/workspace/build \
-v $$(pwd)/src:/workspace/src:ro \
-v $$(pwd)/fs:/workspace/fs:ro \
-v $$(pwd)/script:/workspace/script:ro \
-v $$(pwd)/ports:/workspace/ports:ro \
-v $$(pwd)/docs:/workspace/docs:ro \
-v $$(pwd)/testing:/workspace/testing:ro \
-v $$(pwd)/Makefile:/workspace/Makefile:ro \
-v $$(pwd)/Dockerfile:/workspace/Dockerfile:ro \
-v $$(pwd)/LICENSE:/workspace/LICENSE:ro \
$(DOCKER_TTY) os-dev script/kdosbuild.sh $(BUILD_ARGS)
The meaning of the switches
check-iso-free-
A prerequisite target that refuses to start while
build/iso-build/kdos.isois held open by another process, normally a virtual machine booted from it, because rewriting an image under a running guest gives that guest I/O errors. It asksfuser, so on a machine withoutfuserit checks nothing.make build ALLOW_ISO_IN_USE=1overrides it. - The four
caselines -
Refuse a
KDOS_IMAGEthat is notbaseorfull, aKDOS_JOBSthat is not a positive whole number, aKDOS_ISO_GATEthat is not0or1and aKDOS_ISO_MAXthat is not a positive number of bytes, before anything else runs. An emptyKDOS_JOBS, the default, passes. - The three tag lines
-
Work out which release this image is. A release tag is a git tag named
vfollowed by a digit, such asv0.2, lightweight or annotated; the archive tagssources-N,src-*andpacks-*never name a release.- With
KDOS_GIT_TAGempty, the default, the release is the one release tag onHEAD(git tag --points-at HEAD --list 'v[0-9]*'). AHEADwith no release tag gives no release name, and so doesKDOS_GIT_TAG=none. - With
KDOS_GIT_TAG=<tag>, the release is that tag.
HEADcarries several release tags andKDOS_GIT_TAGnames none of them, when the name is not a release tag onHEAD, or when it is not a nameports/publish --releaseaccepts. Each error message is listed in The release tag. - With
--network none-
The container has no network interface but loopback. The build is a function of the sources
make fetchplaced beforehand, and a port whose build tries to download anything fails immediately. See Offline by construction. --cpu-shares=$(KDOS_CPU_SHARES)-
A weight, 256 unless you set
KDOS_CPU_SHARES, not a limit: an idle machine gives the build every thread, and a busy one shares the processor between the build and everything else. --cpus=...-
Present only when you set
KDOS_JOBS. It caps the container at that many processors, because cargo and go size their own parallelism from the container’s processor limit rather than from the job count. The cap is lowered to the number the machine has when that is fewer, since Docker refuses a larger one. --rm-
The container is deleted when the build ends. Everything the build keeps is under
build/, which is on your disk. --privileged-
The package phases mount
/proc,/sys,/devand the repository insidebuild/fsandchrootinto it, in a mount namespace of their own; a container needs these privileges to do so. -e HOST_UID,-e HOST_GID-
Your user and group. The build runs as the container’s root, and when it ends,
script/kdosbuild.shgives every entry ofbuild/back to you, with these exceptions:build/fs, which must stay root’s because it is the root filesystem of the system being built; the image stage’sbuild/image-upper,build/image-workandbuild/image-root, which are layers of that root; andbuild/podman, if one exists: no step creates it, but a podman container store there must keep its owner, because podman refuses a store owned by anyone other than the user running it. See kdosbuild. -e KDOS_JOBS-
The job count every phase uses for make,
cmake --build, ninja and cargo. Empty, it is computed inside the build from the machine’s threads and memory. -e KDOS_GIT_COMMIT,KDOS_GIT_DIRTY,KDOS_GIT_DATE,KDOS_GIT_TIME,KDOS_GIT_TAG-
The current commit, whether the working tree has changes (untracked files count), the commit’s date and time, and the release tag worked out above. The container cannot ask git, so they are passed in; kdosbuild records them in each snapshot, and the image phase stamps the commit into
/etc/os-releaseand/ports/TREE, and the tag into/ports/TREEonly when the tree is clean (see The recipe tree). -e KDOS_ISO_SOURCES,KDOS_PACK_KDOS,KDOS_MAKE_BINHOST,KDOS_ISO_COMP,KDOS_ISO_BLOCK,KDOS_ISO_MAX,KDOS_ISO_GATE,KDOS_IMAGE-
Switches that change what the image phase produces. All are off or at their defaults unless you set them on the
makecommand line:KDOS_IMAGEisbase, an ISO of the base alone (see The image stage), andKDOS_ISO_GATEis1, which fails a base ISO larger thanKDOS_ISO_MAX, 1,900 MiB (see The ISO). The table in The first build gives each. -e KDOS_CCACHE,KDOS_PKG_STORE,KDOS_PKG_STORE_MAX- The compiler cache, on by default, and the package store, off by default. Both change how much a build rebuilds and nothing about what it produces; see The compiler cache and The package store.
-v $(pwd)/build:/workspace/build-
The one writable mount. Everything a build makes, from the cross compiler to the ISO, is under
build/. -v ...:roforsrc,fs,script,ports,docs,testing, theMakefileand theDockerfile-
The rest of the repository, read-only, so that a build cannot change its own sources.
docsis there for the two ports that render the books,kdos-docfor the KDOS book andkdos-kfsfor this one, andtestingfor the checks the image phase runs. TheMakefileand theDockerfileare there so that this book, rendered inside the build, can show the listings of this chapter, and so that a developer medium made withKDOS_ISO_SOURCES=1carries both. Nothing else of the repository is mounted: the container has no.gitand none of the other files at the top of the checkout. $(DOCKER_TTY)-
-itwhenmakeitself is reading from a terminal, and nothing otherwise. Docker refuses-itwithout a terminal, so a build whose output is sent to a file still starts, and kdosbuild then prints plain lines instead of its full-screen view. os-dev script/kdosbuild.sh $(BUILD_ARGS)-
The image, and the command: compile the orchestrator if it changed and run it, with whatever you put in
BUILD_ARGS. kdosbuild explains both.
A shell in the container
To look at a tree by hand with exactly the tools the build has, start the same image with the same mounts and a shell instead of the build. From the top of the repository on your machine:
docker run --rm -it --network none --privileged \
-v "$PWD/build:/workspace/build" \
-v "$PWD/src:/workspace/src:ro" -v "$PWD/fs:/workspace/fs:ro" \
-v "$PWD/script:/workspace/script:ro" -v "$PWD/ports:/workspace/ports:ro" \
-v "$PWD/docs:/workspace/docs:ro" -v "$PWD/testing:/workspace/testing:ro" \
-v "$PWD/Makefile:/workspace/Makefile:ro" -v "$PWD/Dockerfile:/workspace/Dockerfile:ro" \
-v "$PWD/LICENSE:/workspace/LICENSE:ro" \
os-dev bash
You are root in /workspace. The image must exist: make build builds it, and so does docker build -t os-dev . from the top of the repository. Anything you create under build/ from this shell is owned by root on your machine, since nothing hands it back, and is removed again from a container rather than with sudo. To go on into the root being built, run script/chroot/exec.sh bash from this shell; A shell in the chroot explains what that gives you.
See also
- What your machine needs, and why everything runs in a container: the same container described in the KDOS book
- What a development machine needs: Docker, disk and the optional tools
- Sources and the archive: what must be fetched before the first build
- kdosbuild: what runs inside the container