31. Entering the chroot
Every phase from 20_selfhost on runs inside build/fs, the root the bootstrap built, entered as a chroot. kdosbuild, the program make build runs inside the build container to order and run every step, enters it once for every command it runs there, always through one script, script/chroot/exec.sh. Read this chapter when a step behaves differently inside the chroot than you expect, when you add a build setting, or when you want a shell in the tree to try something by hand.
The chapter goes through the script part by part: the mount namespace it takes, the filesystems and repository directories it mounts, the limit it raises, and the empty environment it starts the command in. It then shows how to open a shell in the chroot by hand, with the same script or with its interactive counterpart, script/chroot/enter.sh.
Why a chroot
chroot makes a directory the root directory for the command it starts. Inside, /usr/bin/gcc is build/fs/usr/bin/gcc, the compiler the bootstrap built, and the container’s own compiler, libraries and tools are out of reach. From 20_selfhost on, a package is therefore built only by what the tree itself holds. The empty environment the script starts each command in does the same for variables: nothing set in the container reaches a build unless the script names it.
script/chroot/exec.sh
kdosbuild runs every command of a chroot phase as script/chroot/exec.sh <command>, from the top of the repository in the container. A package step’s command is
script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && kpkg install pv'
and a script step’s is script/chroot/exec.sh bash script/phases/70_image/<script>.
A private mount namespace
set -e
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root (or with sudo) for chroot execution."
exit 1
fi
# Every entry runs in its own private mount namespace. Each has its own /proc,
# /dev, /sys, /tmp, /run and repository binds: nothing it mounts is visible to
# a concurrent entry or to the container, and all of it disappears with the
# entry's last process, so there is nothing to unmount and nothing a killed
# run can leave behind. Concurrent entries are safe for that reason alone.
# KDOS_CHROOT_NS marks the re-executed copy; it is not forwarded into the
# chroot.
if [ -z "${KDOS_CHROOT_NS:-}" ]; then
KDOS_CHROOT_NS=1 exec unshare --mount --propagation private -- bash "$0" "$@"
fi
script/chroot/exec.sh, lines 13–29
The script requires root, and then runs itself again under unshare --mount --propagation private, which gives the new copy a mount namespace of its own: every mount it makes from here on is visible only to it and to the processes it starts. KDOS_CHROOT_NS tells the second copy that it is the second copy. When the command’s last process exits, the namespace and every mount in it disappear, even when the command is killed, so there is nothing to unmount afterwards and nothing a stopped build can leave mounted. Two commands running at once, as two ports do under --port-jobs, each have their own /proc, /tmp and repository mounts and never see the other’s.
Finding the tree
# The repository root is two levels above this file, script/chroot/.
SCRIPT_DIR="$(readlink -f "$0")"
SCRIPT_DIR="${SCRIPT_DIR%/*}"
REPO_ROOT="${SCRIPT_DIR%/*/*}"
CHROOT_DIR="$REPO_ROOT/build/fs"
if [ ! -d "$CHROOT_DIR" ]; then
echo "Error: Chroot directory $CHROOT_DIR does not exist"
exit 1
fi
# Diagnostics go to a file, never to stdout/stderr: the orchestrator parses the output
# of commands run through this wrapper (kpkgdepends prints the install order
# and nothing else), so a stray message here becomes a bogus package name.
MOUNT_LOG="$REPO_ROOT/build/logs/chroot.log"
log_mount() {
mkdir -p "${MOUNT_LOG%/*}" 2>/dev/null || return 0
printf '%(%Y-%m-%d %H:%M:%S)T %s\n' -1 "$*" >> "$MOUNT_LOG" 2>/dev/null || true
}
script/chroot/exec.sh, lines 31–49
The repository root is found from the script’s own path, two directories up, and the tree is build/fs under it; the script stops if the tree does not exist. Warnings go to build/logs/chroot.log and never to the command’s output, because kdosbuild reads the output of kpkgdepends run through this script as a list of package names, and a stray line there would be taken for one.
The image stage
# THE BASE IMAGE IS MADE FROM A STAGE, NEVER FROM build/fs ITSELF. build/fs
# keeps every package the build installed: the next build continues from it
# and the binhost is made from it. Under KDOS_IMAGE=base (the default) every
# 70_image step from $STAGE_FIRST on runs in an overlay over it: the lower
# layer build/fs, read and never written; the upper build/image-upper, which
# receives every change those steps make, the packages 017_stage.sh removes
# included; the merged view build/image-root, which becomes this entry's
# root and which 110_iso.sh squashes. The overlay is mounted in this entry's
# own namespace like every other mount here, so each step mounts it again
# over the same upper and sees what the steps before it left.
# $STAGE_FIRST starts from an empty upper; a later step whose stage was never
# made (build/image-stage.ok absent: 017 did not finish since its last start)
# is refused, since it would squash a tree nobody trimmed. KDOS_IMAGE=full
# runs every step on build/fs, as a build of every package's medium.
STAGE_FIRST=017_stage.sh
STAGE_OK="$REPO_ROOT/build/image-stage.ok"
case "${KDOS_IMAGE:=base}" in
base|full) ;;
*) echo "Error: KDOS_IMAGE must be base or full, got '$KDOS_IMAGE'" >&2; exit 1 ;;
esac
if [ "$KDOS_IMAGE" = base ] && [ "${1:-}" = bash ] &&
[[ "${2:-}" == script/phases/70_image/* ]] && [[ ! "${2##*/}" < "$STAGE_FIRST" ]]; then
UPPER="$REPO_ROOT/build/image-upper"
WORK="$REPO_ROOT/build/image-work"
MERGED="$REPO_ROOT/build/image-root"
if [ "${2##*/}" = "$STAGE_FIRST" ]; then
rm -f "$STAGE_OK"
rm -rf "$UPPER" "$WORK"
elif [ ! -f "$STAGE_OK" ]; then
echo "Error: ${2##*/} runs in the image stage, and $STAGE_FIRST has not made it;" >&2
echo " run 70_image from $STAGE_FIRST, or build with KDOS_IMAGE=full" >&2
exit 1
fi
mkdir -p "$UPPER" "$WORK" "$MERGED"
mount -t overlay overlay \
-o "lowerdir=$CHROOT_DIR,upperdir=$UPPER,workdir=$WORK" "$MERGED"
log_mount "image stage: ${2##*/} in $MERGED over $CHROOT_DIR"
CHROOT_DIR=$MERGED
fi
script/chroot/exec.sh, lines 51–89
Only a step of 70_image numbered 017_stage.sh or later, run under KDOS_IMAGE=base, the default, takes this branch; every other command, and every step under KDOS_IMAGE=full, runs on build/fs itself. The branch mounts an overlay with build/fs as its read-only lower layer, build/image-upper as the layer that receives every write, and build/image-root as the merged view, and makes that view the root the rest of the script enters. 017_stage.sh starts from an empty upper layer and removes the stage mark build/image-stage.ok, which it writes again when it has trimmed the stage; every later step mounts the overlay again over the same upper layer, and is refused while the mark is missing, because it would otherwise image a tree nobody trimmed. The image stage is the step.
The home directory
mkdir -p "$CHROOT_DIR"/{dev,proc,sys,tmp,run,ports,kdos}
# The build's HOME, a scratch directory and not /root: every tool's dotfiles,
# caches and registries land here, under var/tmp, which the snapshots leave out
# and 70_image neither squashes nor ships. On disk rather than in the per-entry
# /tmp, because a Go or Rust build caches gigabytes under it.
mkdir -p "$CHROOT_DIR/var"
[ -d "$CHROOT_DIR/var/tmp" ] || mkdir -m 1777 "$CHROOT_DIR/var/tmp"
[ -d "$CHROOT_DIR/var/tmp/kdos-home" ] || mkdir -m 0700 "$CHROOT_DIR/var/tmp/kdos-home"
script/chroot/exec.sh, lines 91–99
The mount points are created when missing. The build’s HOME is /var/tmp/kdos-home rather than /root, so that every tool’s dotfiles, caches and package registries land under /var/tmp, which no snapshot archives and the image does not ship. It is on disk rather than in the per-command /tmp, because a Go or Rust build keeps gigabytes in its cache there.
Virtual filesystems
# Virtual filesystems. The namespace is fresh, so every mount is made
# unconditionally: none of them can already be there.
mount --bind /dev "$CHROOT_DIR/dev"
mount -t proc proc "$CHROOT_DIR/proc"
mount -t sysfs sysfs "$CHROOT_DIR/sys"
# The cgroup tree, read-only, over the fresh sysfs's empty /sys/fs/cgroup.
# cargo and go size themselves from cpu.max through it; without it they
# see every host thread whatever --cpus cap the container runs under. The bind
# and the remount are two steps because busybox mount ignores ro on a bind.
if [ -d /sys/fs/cgroup ]; then
if mount --bind /sys/fs/cgroup "$CHROOT_DIR/sys/fs/cgroup"; then
mount -o remount,bind,ro "$CHROOT_DIR/sys/fs/cgroup" \
|| { umount "$CHROOT_DIR/sys/fs/cgroup"; log_mount "warning: cgroup tree not mounted: no read-only bind"; }
else
log_mount "warning: cgroup tree not mounted"
fi
fi
mount -t tmpfs tmpfs "$CHROOT_DIR/tmp"
mount -t tmpfs tmpfs "$CHROOT_DIR/run"
script/chroot/exec.sh, lines 101–119
/dev is the container’s, bound in. /proc and /sys are mounted fresh, so that inside the chroot they describe the processes of this namespace. The container’s cgroup tree is bound read-only over the fresh /sys/fs/cgroup, because cargo and go read their processor limit from it, and would otherwise see every thread of the machine whatever --cpus cap the container has. /tmp and /run are new, empty tmpfs filesystems for every command, so nothing one step leaves there survives into the next.
The repository
# Mount repository and ports
mount --bind "$REPO_ROOT" "$CHROOT_DIR/kdos"
mkdir -p "$CHROOT_DIR"/kdos/{build,script,src,fs,docs}
mount --bind "$REPO_ROOT/build" "$CHROOT_DIR/kdos/build"
mount --bind "$REPO_ROOT/ports" "$CHROOT_DIR/ports"
# Explicitly mount sub-mounts that might be hidden by the main bind mount
mount --bind "$REPO_ROOT/script" "$CHROOT_DIR/kdos/script"
mount --bind "$REPO_ROOT/src" "$CHROOT_DIR/kdos/src"
# fs/ too: a medium that carries the sources copies the overlay from here, and
# without the bind /kdos/fs is the container's empty mount point.
mount --bind "$REPO_ROOT/fs" "$CHROOT_DIR/kdos/fs"
# docs/ read-only: the kdos-doc port renders docs/kdos from it through its
# `kdos` and `screenshots` links, kdos-kfs renders docs/kfs through its `docs`
# link, and a build has no business writing either book. Without the bind
# /kdos/docs is the container's empty mount point and both ports fail.
# kdos-kfs also reads the recipes at /kdos/ports, bound below with testing/.
if [ -d "$REPO_ROOT/docs" ]; then
mount --bind "$REPO_ROOT/docs" "$CHROOT_DIR/kdos/docs"
mount -o remount,bind,ro "$CHROOT_DIR/kdos/docs" \
|| log_mount "warning: docs bound read-write: no read-only remount"
fi
# testing/ read-only and ports/ again under /kdos: 70_image's 087_gates.sh runs
# testing/linkcheck.py and its siblings from /kdos, and they read the recipes
# at /kdos/ports. Without the binds both are the container's empty mount
# points and the gate fails every image.
if [ -d "$REPO_ROOT/testing" ]; then
mkdir -p "$CHROOT_DIR/kdos/testing" "$CHROOT_DIR/kdos/ports"
mount --bind "$REPO_ROOT/testing" "$CHROOT_DIR/kdos/testing"
mount -o remount,bind,ro "$CHROOT_DIR/kdos/testing" \
|| log_mount "warning: testing bound read-write: no read-only remount"
mount --bind "$REPO_ROOT/ports" "$CHROOT_DIR/kdos/ports"
fi
# The Makefile and the Dockerfile read-only: kdos-kfs renders Part I of KDOS
# from Scratch from /kdos, and that part shows lines of both; 110_iso.sh
# copies both onto a medium that carries the sources. Without the binds each
# is the container's empty mount point, and the book prints "not in this tree"
# where the listing goes. A file the container lacks is skipped.
for _f in Makefile Dockerfile; do
[ -f "$REPO_ROOT/$_f" ] && [ -e "$CHROOT_DIR/kdos/$_f" ] || continue
mount --bind "$REPO_ROOT/$_f" "$CHROOT_DIR/kdos/$_f"
mount -o remount,bind,ro "$CHROOT_DIR/kdos/$_f" \
|| log_mount "warning: $_f bound read-write: no read-only remount"
done
script/chroot/exec.sh, lines 121–164
The repository is bound at /kdos and build/ at /kdos/build, both writable, and ports/ at /ports, the path every phase’s PORT_REPO and kpkg’s own configuration name.
A bind mount of a directory does not carry the mounts beneath it. In the container, script/, src/, fs/, docs/, testing/, the Makefile, the Dockerfile and LICENSE are each a separate read-only mount, so the script binds each of them again, all but LICENSE, and remounts docs/, testing/ and the two files read-only. Each is there for a reader:
script/,src/andfs/: the phase scripts and environments, KDOS’s own ports, and the overlay that a medium carrying the sources copies.docs/: the two ports that render the books,kdos-docfor the KDOS book andkdos-kfsfor this one.testing/: the checks the image phase’s087_gates.shruns from/kdos.ports/is bound once more at/kdos/portsbecause those checks, andkdos-kfs, read the recipes there.- The
Makefileand theDockerfile:kdos-kfsagain, for the listings of Part I, and110_iso.sh, the image step that copies the sources onto a developer medium.
LICENSE is read only outside the chroot, by 000_file_system.sh, which installs it into the tree as the MIT text of KDOS’s own packages; nothing in the chroot reads it.
The open-files limit
# AT LEAST 4096 OPEN FILES, soft and hard. QtWebEngine's configure writes a
# linker wrapper that runs `ulimit -n 4096` before its bfd link, and that call
# fails, and the link with it, when the hard limit is lower. Root may raise
# the hard limit; a limit already at or above 4096 is left alone.
_nofile=$(ulimit -Hn)
if [ "$_nofile" != unlimited ] && [ "$_nofile" -lt 4096 ]; then
ulimit -Hn 4096 2>/dev/null || log_mount "warning: hard open-files limit stays at $_nofile"
fi
_nofile=$(ulimit -Sn)
if [ "$_nofile" != unlimited ] && [ "$_nofile" -lt 4096 ]; then
ulimit -Sn 4096 2>/dev/null || log_mount "warning: soft open-files limit stays at $_nofile"
fi
script/chroot/exec.sh, lines 166–177
Both the hard and the soft limit on open files are raised to at least 4096. QtWebEngine’s link runs ulimit -n 4096 itself and fails when the hard limit is lower. A limit already that high is left alone, and one that cannot be raised is noted in build/logs/chroot.log.
The package store
case "${KDOS_PKG_STORE:-0}" in
1) _store=/kdos/build/pkgstore; _store_check=0 ;;
check) _store=/kdos/build/pkgstore; _store_check=1 ;;
*) _store=; _store_check=0 ;;
esac
script/chroot/exec.sh, lines 179–183
make build KDOS_PKG_STORE=1 or =check turns on the package store, and the script turns that into the two variables kpkg itself reads: the store’s directory as the chroot sees it, and whether to build every hit anyway and compare.
The command, in an empty environment
exec chroot "$CHROOT_DIR" /usr/bin/env -i \
HOME=/var/tmp/kdos-home \
TERM="$TERM" \
KDOS_REPLAY="${KDOS_REPLAY:-0}" \
KDOS_JOBS="${KDOS_JOBS:-}" \
KDOS_ISO_SOURCES="${KDOS_ISO_SOURCES:-0}" \
KDOS_PACK_KDOS="${KDOS_PACK_KDOS:-0}" \
KDOS_MAKE_BINHOST="${KDOS_MAKE_BINHOST:-0}" \
KDOS_ISO_COMP="${KDOS_ISO_COMP:-}" \
KDOS_ISO_BLOCK="${KDOS_ISO_BLOCK:-}" \
KDOS_ISO_MAX="${KDOS_ISO_MAX:-}" \
KDOS_ISO_GATE="${KDOS_ISO_GATE:-1}" \
KDOS_IMAGE="$KDOS_IMAGE" \
KDOS_CCACHE="${KDOS_CCACHE:-1}" \
KPKG_KEEP_CACHE="${KDOS_MAKE_BINHOST:-0}" \
KDOS_PKG_STORE="${KDOS_PKG_STORE:-0}" \
KDOS_PKG_STORE_MAX="${KDOS_PKG_STORE_MAX:-60G}" \
KPKG_STORE="$_store" \
KPKG_STORE_CHECK="$_store_check" \
KPKG_STORE_SALT="${KPKG_STORE_SALT:-}" \
KDOS_GIT_COMMIT="${KDOS_GIT_COMMIT:-}" \
KDOS_GIT_DIRTY="${KDOS_GIT_DIRTY:-0}" \
KDOS_GIT_DATE="${KDOS_GIT_DATE:-}" \
KDOS_GIT_TIME="${KDOS_GIT_TIME:-}" \
KDOS_GIT_TAG="${KDOS_GIT_TAG:-}" \
KDOS_GIT_RELEASE="$_release" \
PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin \
/bin/bash -c "cd /kdos && exec \"\$@\"" -- "$@"
script/chroot/exec.sh, lines 252–279
chroot makes build/fs the root and runs the tree’s own /usr/bin/env -i, which clears the environment completely and sets only the variables listed. Then the tree’s bash changes to /kdos, so that a relative path such as script/phases/41_system/phase.env means the same file inside as outside, and replaces itself with the command. In bash -c '...' -- "$@", the -- becomes the shell’s $0 and the command and its arguments become "$@".
The variables that cross into the chroot:
HOME,TERM-
The build’s home directory, above, and the caller’s terminal type, which the phase environment below replaces with
dumb. PATH-
/usr/binfirst, then/usr/sbin,/bin,/sbin, and/usr/local/binlast./usr/bincomes before/bin, which is a link to it, because CMake turns each entry into a search prefix, and a package’s CMake configuration found through/bincomputes its prefix as/./usr/local/bin, where KDOS’s own tools such askdos-appboxinstall, is last so that the image steps that call them by name find them, while none of them can shadow a system tool during a port’s configure. KDOS_REPLAY-
1for a step a build plan named, so that its “already done” marker stands down. KDOS_JOBS-
The job count from
make build, or empty, in which case the phase environment computes it inside the chroot. KDOS_ISO_SOURCES,KDOS_PACK_KDOS,KDOS_MAKE_BINHOST,KDOS_ISO_COMP,KDOS_ISO_BLOCK,KPKG_KEEP_CACHE-
The opt-in switches of the image phase, the codec and block size of the image’s
system.sfs, andKDOS_MAKE_BINHOSTagain under the name kpkg reads, so that every package is kept for the binhost. KDOS_IMAGE-
baseorfull, defaulted tobaseby the stage block above, which has already read it;017_stage.shreads it again and does nothing underfull, and110_iso.shgates only abaseISO. KDOS_ISO_MAX,KDOS_ISO_GATE-
The release size gate of
110_iso.sh: the largest base ISO in bytes, empty for 1,900 MiB, and1, the default, to enforce it, or0for a developer image. KDOS_CCACHE-
Whether CMake ports compile through the cache in
build/ccache. KDOS_PKG_STORE,KDOS_PKG_STORE_MAX,KPKG_STORE,KPKG_STORE_CHECK,KPKG_STORE_SALT- The package store, as above, its size cap, and the hash of the bootstrap phases that kdosbuild computes on the host and every store key includes.
KDOS_GIT_COMMIT,KDOS_GIT_DIRTY,KDOS_GIT_DATE,KDOS_GIT_TIME-
The commit the tree is being built from, whether the checkout had uncommitted changes, and the commit’s date, which 045_identity.sh writes into
/etc/os-releaseasBUILD_IDandKDOS_BUILD_DATE; and the commit’s time in Unix seconds, which goes into/etc/kdos-releaseand, through105_ports.sh, into/ports/TREE. KDOS_GIT_TAG,KDOS_GIT_RELEASE-
KDOS_GIT_TAGis the release tag (v<digit>…) on that commit, passed bymake build. It is empty when the commit has no tag, or whenKDOS_GIT_TAG=noneasked for none.KDOS_GIT_RELEASEis set by this script: it is the same tag, but only whenKDOS_GIT_DIRTYis0, because a dirty tree’s packages are not the release’s. 045_identity.sh stops the build whenKDOS_GIT_RELEASEwithout itsvis not/etc/os-release’sVERSION_ID.105_ports.shwrites it into/ports/TREEasversion <tag>, and010_binhost.shmakes it the binhost index’sversion;kdos updatematches the two by that name. When it is empty, both/ports/TREEand the binhost index name the commit instead (see The recipe tree).
A variable the Makefile passes into the container but this line does not name reaches the two container phases and none of the chroot ones. Adding a build setting is therefore two edits: the -e in the Makefile and the name on this line. Every variable is described in env -i means every variable must be named.
The phase environment
The environment env -i leaves is not the one a port is built with. A package step’s command starts with source script/phases/<phase>/phase.env, and that file, through script/env/chroot.env and script/env/common.env, sets everything the build needs: the compilers named outright, the release flags, the job count, the reproducibility settings, the repositories kpkg looks in. Part of what it gives a 41_system step, printed with env in a container whose job count came to 16, with the flag values cut short:
CC=gcc
CXX=g++
CFLAGS=-O2 -pipe -std=gnu11 -fPIC -fno-semantic-interposition -fstack-clash-protection ...
KDOS_JOBS=16
MAKEFLAGS=-j16
KPKG_STRICT_RECIPE=1
LANG=C.UTF-8
PATH=/kdos/script/bin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin
PORT_REPO=/ports/core /kdos/src/system /kdos/src/art
SOURCE_DATE_EPOCH=1735689600
TZ=UTC
/kdos/script/bin, put first once ninja is installed, holds wrappers that add the job count to a ninja call and --wrap-mode=nodownload to a meson setup; it is not part of the image. KPKG_STRICT_RECIPE=1 makes kpkg rebuild a port whose recipe changed since it was installed. SOURCE_DATE_EPOCH and TZ fix every timestamp a build writes. The full list of what each file sets is in The phase environment.
A shell in the chroot
To work in the chroot by hand with exactly what a build step has, run exec.sh with a shell as its command. From the top of the repository on your machine, start the build container with the same mounts make build gives it and exec.sh inside it:
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 script/chroot/exec.sh bash
You are root in /kdos, inside build/fs, with the empty environment above. Load the phase environment of the port you are working on, and kpkg behaves exactly as it does in the build:
source script/phases/41_system/phase.env
kpkg info pv
Package: pv
Version: 1.12.0-2
License: GPL-3.0-or-later
Files: 19
Version is the recipe’s version and release, and Files counts every path the package’s database entry lists, directories included. Leave the shell with exit; its mounts go with it.
A command can be given in place of the shell, as kdosbuild gives one. To see the install order kpkgdepends resolves for pv, with the database hidden so that installed ports are not dropped from it, as kdosbuild resolves a phase:
script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && PKGDB_DIR=/dev/null kpkgdepends pv'
ncurses pv
Without PKGDB_DIR=/dev/null the same command prints an empty line on a tree where both are installed and current, because the solver leaves out every port with nothing to do.
script/chroot/enter.sh
enter.sh is a lighter way into a tree, for looking around rather than building:
set -e
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo)"
exit 1
fi
# The shell runs in its own private mount namespace, as script/chroot/exec.sh
# does: its /proc, /dev, /sys, /tmp and /run are visible to nothing else, a
# build running beside it keeps its own, and all of them go away when the shell
# exits. KDOS_CHROOT_NS marks the re-executed copy.
if [ -z "${KDOS_CHROOT_NS:-}" ]; then
KDOS_CHROOT_NS=1 exec unshare --mount --propagation private -- bash "$0" "$@"
fi
unset KDOS_CHROOT_NS
# The repository root is two levels above this file, script/chroot/.
SCRIPT_DIR="$(readlink -f "$0")"
SCRIPT_DIR="${SCRIPT_DIR%/*}"
REPO_ROOT="${SCRIPT_DIR%/*/*}"
CHROOT_DIR="$REPO_ROOT/build/fs"
if [ ! -d "$CHROOT_DIR" ]; then
echo "Error: Chroot directory $CHROOT_DIR does not exist"
exit 1
fi
echo "Setting up chroot environment at $CHROOT_DIR..."
mkdir -p "$CHROOT_DIR"/{dev,proc,sys,tmp,run}
# Mount virtual filesystems
mount --bind /dev "$CHROOT_DIR/dev"
mount -t proc proc "$CHROOT_DIR/proc"
mount -t sysfs sysfs "$CHROOT_DIR/sys"
mount -t tmpfs tmpfs "$CHROOT_DIR/tmp"
mount -t tmpfs tmpfs "$CHROOT_DIR/run"
# Copy resolv.conf for networking
cp /etc/resolv.conf "$CHROOT_DIR/etc/resolv.conf" 2>/dev/null || true
echo "Entering chroot..."
if [ $# -gt 0 ]; then
chroot "$CHROOT_DIR" "$@"
else
chroot "$CHROOT_DIR" /usr/bin/bash -l
fi
echo "Exited chroot."
script/chroot/enter.sh, lines 13–61
It takes a private mount namespace in the same way and mounts /dev, /proc, /sys, /tmp and /run, but it differs from exec.sh in three ways. It does not mount the repository, so /kdos and /ports inside are empty directories, and kpkg cannot find a port. It does not clear the environment, so the shell inherits whatever the caller had. And it copies the caller’s /etc/resolv.conf into the tree, over the copy fs/ provides, so that name resolution works from a chroot on a machine with a network; the next run of 000_file_system.sh puts fs/’s copy back. With no arguments it starts a login shell, bash -l, which reads the tree’s own /etc/profile; with arguments it runs them instead.
It needs only root, unshare, mount and chroot. In the build container it runs the same way as exec.sh above, with script/chroot/enter.sh as the command. The build never uses it.
See also
- Anatomy of a kpkg install: what the command inside the chroot then does
- The chroot: the same script in the KDOS book
- Entering the chroot: where the chroot fits in the whole build
- The os-dev container: the container the chroot is entered from