360. Building the kernel
With the configuration resolved, the linux port’s build.sh compiles the kernel and its modules and stages them in the package. This chapter covers what the configuration chapter leaves out: the values the build pins so that two builds carry the same identity, the module-signing key it keeps between builds, the logo it draws on the panic screen, the tools that Rust and BTF need, and the commands that compile and stage everything.
Source: the archive of the linux port
Installs into: /boot, /lib/modules/<version> and /usr/include
Depends on: dwarves, built just before it in the same phase, and ports that earlier phases installed
Running the build
60_kernel installs dwarves and then linux. Like every package step, each is one kpkg install in the chroot, run from the top of the repository:
script/chroot/exec.sh bash -c 'source script/phases/60_kernel/phase.env && kpkg install dwarves'
script/chroot/exec.sh bash -c 'source script/phases/60_kernel/phase.env && kpkg install linux'
kpkg unpacks the source, runs build.sh with the working directory at the top of the kernel tree, PORT_SRC set to the port directory and PKG to the staging directory, and then runs the port’s postinstall.sh on the root it installed into; Installing the kernel covers that script. The whole sequence is in Anatomy of a kpkg install. The make calls take their job count from MAKEFLAGS, which script/env/common.env sets to -j$KDOS_JOBS.
Pinned build identity
The first lines of build.sh fix four values kbuild otherwise takes from the machine and the moment of the build:
export KBUILD_BUILD_TIMESTAMP="$(LC_ALL=C date -u -d "@${SOURCE_DATE_EPOCH:-0}")"
export KBUILD_BUILD_USER=kdos
export KBUILD_BUILD_HOST=kdos
export KBUILD_BUILD_VERSION=1
ports/core/boot/linux/build.sh, lines 12–15
The meaning of the variables
KBUILD_BUILD_TIMESTAMP-
The date in the kernel’s version string, which
uname -vand the boot banner show. kbuild uses the time of the build unless this is set. The recipe writesSOURCE_DATE_EPOCH, whichscript/env/common.envsets for every phase, as a date, so every build of the same tree carries the same string. KBUILD_BUILD_USER,KBUILD_BUILD_HOST-
The
user@hostpart of/proc/version. Left unset they are the account and the host name of the build container. Both arekdos. KBUILD_BUILD_VERSION-
The
#Nat the start ofuname -v, which kbuild otherwise counts up in a.versionfile in the tree on every build. Pinned to1.
The package is still not byte-identical between two builds of the same tree: every module carries an ECDSA signature, and an ECDSA signature uses a fresh random number each time it is made. See Reproducible packages.
The module-signing key
The configuration turns module signing on (see Options the file does not set), so the build needs a module-signing key to sign with. Left alone, kbuild generates a new one for every build, and the modules of one build then fail verification under the kernel of another. Inside the KDOS build build.sh uses one persistent key instead:
modkey=/kdos/build/keys/linux-module-signing.pem
if [ -d /kdos/build ]; then
if [ ! -s "$modkey" ]; then
mkdir -p "${modkey%/*}"
( umask 077; openssl req -new -nodes -utf8 -sha512 -days 36500 -batch -x509 \
-config $PORT_SRC/x509.genkey -newkey ec -pkeyopt ec_paramgen_curve:secp384r1 \
-outform PEM -out "$modkey.new" -keyout "$modkey.new" )
mv "$modkey.new" "$modkey"
fi
./scripts/config --set-str CONFIG_MODULE_SIG_KEY "$modkey"
fi
ports/core/boot/linux/build.sh, lines 33–43
/kdos/build is the repository’s build/ directory as the chroot sees it, so the key lives on the machine that builds KDOS at build/keys/linux-module-signing.pem. make clean and make cleanbuild both keep build/keys, and git ignores all of build/, so the private key never enters the repository.
The first build that finds no key writes one with openssl req:
-new -x509 -nodes -batch- Make a new self-signed certificate, with an unencrypted private key, without asking anything: the subject comes from the configuration file.
-config $PORT_SRC/x509.genkey-
The request settings in the port directory. Its subject is
O = KDOS,CN = KDOS kernel module signing key, and its extensions mark the certificate as a signing key that is not a certificate authority. Itsdefault_bitsis a setting for RSA keys and plays no part with-newkey ec. -newkey ec -pkeyopt ec_paramgen_curve:secp384r1-
An elliptic-curve key on the P-384 curve, the ECDSA key type
kdos.configselects. -sha512,-days 36500,-utf8- Sign the certificate with SHA-512, make it valid for a hundred years, and read the subject as UTF-8.
-outform PEM -out "$modkey.new" -keyout "$modkey.new"-
Write the certificate and the private key into one PEM file, the form
CONFIG_expects.MODULE_ SIG_ KEY umask 077, thenmv "$modkey.new" "$modkey"-
The file is readable by its owner alone, and appears under its real name only once it is complete, so an interrupted
opensslleaves no half-written key for the next build to use.
./scripts/config --set-str CONFIG_MODULE_SIG_KEY "$modkey" then points the configuration at the key. This is the one setting the recipe makes outside kdos.config, and it is made after the checks of How the build applies it, so they never see it. The certificate is built into the kernel, and every module installed later is signed with the key.
A build where /kdos/build does not exist, such as one on an installed machine, skips the whole block and keeps the default, certs/signing_key.pem, which kbuild generates for that build alone. What the signature does and does not protect is in Module signing.
The logo
build.sh copies kdos-logo-mono.pbm from the port directory to drivers/video/logo/kdos-logo-mono.pbm, and kdos.config names that path in CONFIG_. kbuild converts the image into C with its own pnmtologo while it compiles the kernel, so the file has to be in place before make bzImage.
The panic screen draws this logo in its top-left corner at one screen pixel per image pixel, and leaves it out where it would overlap the message or the QR code. It takes only a one-bit logo: it asks for the logo of depth 1, which is the monochrome one. When it has none, because the logo it gets is of another kind, CONFIG_ is off, or DRM is loaded as a module rather than built into the kernel, it draws a small ASCII-art Tux in its place. That is why kdos.config sets both CONFIG_LOGO=y and CONFIG_DRM=y.
The .pbm is a committed file, in the plain-text P1 form so that a change to it reads as a diff. genlogo-mono.py beside the recipe cuts it from the KDOS artwork, kdos.png at the top of the repository, and writes it over the committed file:
ports/core/boot/linux/genlogo-mono.py kdos.png
The script needs the Python imaging library (PIL), which is not part of the build container, so it runs on a host; nothing in the build runs it. It keeps the penguin and drops the wordmark, which is unreadable at that size, and draws the mascot’s dark line art rather than a filled silhouette, so the face survives.
What Rust and BTF need
Two features of the configuration exist only when their tools are present when olddefconfig runs, and olddefconfig drops them without a word when they are not. build.sh’s checks turn that into a failed build (How the build applies it), and the recipe’s depends line names the tools:
- Rust (
CONFIG_)RUST -
The kernel runs
scripts/rust_is_available.shwhile it reads its Kconfig files. It needsrustcandbindgen, each new enough for this kernel, the libclang thatbindgenloads, and the source of Rust’scorelibrary underrustc --print sysroot, atlib/rustlib/src/rust/library. The rust port builds itssrccomponent, which installs that source; bindgen and clang provide the other two.make rustavailableruns the same script and prints what it found. Without Rust there is no QR-code panic screen, which is written in Rust. - BTF (
CONFIG_)DEBUG_ INFO_ BTF -
The BTF type information is made from the build’s DWARF debug information by
pahole, from the dwarves port, and Kconfig readspahole’s version to decide whether BTF can be on at all. BTF also governs Rust: with BTF on,CONFIG_additionally depends onRUST CONFIG_, aPAHOLE_ HAS_ LANG_ EXCLUDE paholenew enough to leave Rust’s compilation units out of the BTF it writes. Without BTF there is also no sched_ext and no BPF-based queueing discipline, both of which depend on it.
Of the other dependencies, bc, bison, flex and perl serve kbuild’s own tools, elfutils provides the libelf that objtool links, openssl writes the key and signs the modules, and zstd compresses the kernel image and every module. kmod and cpio are for postinstall.sh, which also uses zstd to decompress modules.
Compiling and staging
The rest of build.sh compiles, installs into the staging directory and removes what must not be packaged:
make HOSTCFLAGS="-D__attribute_const__= -D__always_inline=inline" bzImage modules
make INSTALL_MOD_PATH=$PKG INSTALL_MOD_STRIP=1 DEPMOD=/bin/true modules_install
rm -f "$PKG/lib/modules/$version/build"
rm -f "$PKG/lib/modules/$version/source"
mkdir -p $PKG/boot
cp arch/x86/boot/bzImage $PKG/boot/vmlinuz-kdos
cp System.map $PKG/boot/System.map-$version
make INSTALL_HDR_PATH=$PKG/usr headers_install
ports/core/boot/linux/build.sh, lines 45–55
The meaning of the options
HOSTCFLAGS="-D__attribute_const__= -D__always_inline=inline"-
Extra flags for the programs kbuild compiles to run on the build machine. Both names are macros that glibc’s
<sys/cdefs.h>defines and musl’s headers do not, and the kernel’slinux/swab.huses them. The recipe defines the first as nothing and the second asinline. bzImage modules-
Build the compressed kernel image,
arch/x86/boot/bzImage, and every module. INSTALL_MOD_PATH=$PKG-
Install the modules under
$PKG/lib/modules/<version>/rather than on the build root. INSTALL_MOD_STRIP=1-
Strip each module of its debug information as it is installed. BTF needs the full DWARF while the kernel is built, which would otherwise stay in every module. The
.BTFsection is not debug information and stays, and kbuild signs each module after stripping it, so the signature covers the stripped file. DEPMOD=/bin/true-
modules_installruns depmod through this variable;/bin/truemakes that step do nothing, so the package carries nomodules.depor other module index.postinstall.shbuilds the index in the root the package is installed into. rm -f "$PKG/lib/modules/$version/build"and…/source-
Both are links into the directory the kernel was built in, which is gone once the package is made.
70_image’s checks stop the build on a dangling link. cp arch/x86/boot/bzImage $PKG/boot/vmlinuz-kdos,cp System.map $PKG/boot/System.map-$version-
The kernel under one constant name, and its symbol map under a versioned one. Nothing boots
/boot/vmlinuz-kdoswhere it lies: the image copies it onto the medium, and on an installed machinekdos-bootctl deploycopies it to the EFI system partition (ESP). INSTALL_HDR_PATH=$PKG/usr headers_install-
The kernel’s user-space API headers, sanitised, into
$PKG/usr/include. They are the same files the bootstrap’s Linux API headers step wrote from the same source, and the package takes them over.
Contents of linux
Installed files: /boot/vmlinuz-kdos, /boot/System.map-<version>, the module tree /lib/modules/<version>/ (kernel/, modules.order, modules.builtin and modules.builtin.modinfo; the indexes depmod writes appear after postinstall.sh), and the API headers under /usr/include.
The linux port’s page lists every file the package owns.
See also
- Configuring the kernel:
kdos.config, the checks and the option tables - Installing the kernel:
postinstall.sh - The kernel phase: the phase in the build
- Module signing: what the signature means