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

361. Installing the kernel

Installing the linux package puts the kernel in /boot and its modules under /lib/modules, but neither is yet something a machine boots. The port’s postinstall.sh, which kpkgadd runs after the files are in place, does the rest: it removes module trees nothing can boot any more, indexes the new modules, builds an initramfs that matches the new kernel, and, on a running installed system, copies the kernel and that initramfs to the EFI system partition, the ESP. This chapter takes the script in order. How the two root slots and the ESP fit together is in A/B slot selection; this chapter assumes it.

How the script is run

kpkg packages postinstall.sh into the package as a hook, with the recipe’s metadata in front of it, so the script can read $version. kpkgadd runs the hook once the package’s files are installed, with PKG_ROOT set to the root it installed into and PKG_OLD_VERSION to the <version>-<release> it replaced, empty on a first install. A hook that fails is reported as a warning and does not undo the install.

$version is also the name of the module directory, /lib/modules/$version, which uname -r reports once the kernel runs. That holds because kdos.config sets no CONFIG_LOCALVERSION and the source is a release archive rather than a git tree, so nothing is appended to the release string; build.sh and this script both depend on it.

The root and the ESP

root="${PKG_ROOT:-/}"
root="${root%/}"

ports/core/boot/linux/postinstall.sh, lines 15–16

Every path the script touches is under $root, the root the package went into. On a running system that is /, which the second line turns into an empty string so that "$root"/lib reads /lib. kdos update installs into the inactive slot instead, mounted elsewhere, and working on / then would strip the running system’s modules and index the wrong tree.

esp=/boot/efi/EFI/kdos

ports/core/boot/linux/postinstall.sh, lines 24–24

The ESP is always the running system’s, mounted at /boot/efi, whichever root the package is going into: there is one ESP, and both slots boot from it. kinstall puts slot A’s kernel and initramfs in EFI/kdos/a/, and kdos-bootctl deploy puts each slot’s in its own directory. An ESP whose boot menu points at a flat pair in EFI/kdos/ itself, which either slot boots, is read too:

/boot/efi/EFI/kdos/a/vmlinuz              slot A's kernel
/boot/efi/EFI/kdos/a/initramfs.cpio.gz    and the initramfs built with it
/boot/efi/EFI/kdos/b/vmlinuz              slot B's, once an update has deployed one
/boot/efi/EFI/kdos/b/initramfs.cpio.gz
/boot/efi/EFI/kdos/vmlinuz                a flat pair, on an ESP set up without per-slot directories
/boot/efi/EFI/kdos/initramfs.cpio.gz

One kernel per slot explains the layout and the boot menu that goes with it.

Reading a kernel’s version

To know which module trees are still needed, the script must know which kernels are on the ESP, and a file called vmlinuz says nothing about its version. The kernel image does:

kernel_version() {
	[ "$(dd if="$1" bs=1 skip=514 count=4 2>/dev/null)" = HdrS ] || return 0
	off=$(od -An -tu2 -j 526 -N 2 "$1" 2>/dev/null | tr -d ' ')
	[ -n "$off" ] && [ "$off" -gt 0 ] || return 0
	dd if="$1" bs=1 skip=$((off + 512)) count=128 2>/dev/null |
		tr '\0' '\n' | head -n 1 | cut -d' ' -f1
}

esp_versions=
for k in "$esp/vmlinuz" "$esp"/a/vmlinuz "$esp"/b/vmlinuz; do
	[ -f "$k" ] && esp_versions="$esp_versions $(kernel_version "$k")"
done

ports/core/boot/linux/postinstall.sh, lines 29–40

The x86 boot protocol puts a setup header near the start of every bzImage, and that part of the file is never compressed:

The loop collects the versions of the flat kernel and of both slots’ kernels, those that exist, into esp_versions. Where /boot/efi/EFI/kdos holds no kernel, as in the build chroot, the list stays empty.

Pruning old module trees

running=
[ -z "$root" ] && running=$(uname -r)
for d in "$root"/lib/modules/*/; do
	[ -d "$d" ] || continue
	ver=$(basename "$d")
	[ "$ver" = "$version" ] && continue
	[ "$ver" = "$running" ] && continue
	case " $esp_versions " in *" $ver "*) continue ;; esac
	echo "Removing stale kernel modules: $ver"
	rm -rf "$d"
done

ports/core/boot/linux/postinstall.sh, lines 48–58

Each kernel upgrade installs a new /lib/modules/<version>/, and nothing else removes the old one. The script removes every module tree under the root except three kinds:

Each tree removed is named on a Removing stale kernel modules: line. Inside the chroot during the KDOS build, uname -r is the build machine’s kernel, which has no tree in the target root, so only the new kernel’s tree survives.

The module index

echo "Generating modules.dep for $version..."
if command -v depmod >/dev/null; then
	depmod -b "${root:-/}" -a "$version"
else
	echo "Warning: depmod not found, skipping."
fi

ports/core/boot/linux/postinstall.sh, lines 60–65

build.sh installed the modules with DEPMOD=/bin/true, so the package holds no modules.dep and no other index. depmod -b writes them for the new version, in the root the package went into rather than in /. Without the index modprobe finds no module by name, and udev loads nothing.

A kernel already on the ESP

# A reinstall of this same version into the running system (PKG_OLD_VERSION
# is the version kpkgadd replaced: a `--dev` restore, a forced reinstall)
# builds and deploys nothing when this boot's ESP directory already holds what
# a deploy would write: a vmlinuz of this version identical to
# /boot/vmlinuz-kdos, beside an initramfs identical to the one deploy takes
# (/boot/initramfs-kdos.cpio.gz where it exists, else the image's
# /boot/initramfs.cpio.gz, which is all a freshly installed root has). The
# directory is the one this boot's menu entry names with kdos_slot=, or the
# flat EFI/kdos/ of an ESP with no slot directories. Any other install, a
# rebuilt kernel of the same version, and an initramfs-kdos built but never
# deployed, builds and deploys.
on_esp=
if [ -z "$root" ] && [ "${PKG_OLD_VERSION:-}" = "$version-$release" ] &&
	mountpoint -q /boot/efi 2>/dev/null; then
	slot=$(tr ' ' '\n' < /proc/cmdline 2>/dev/null |
		sed -n 's/^kdos_slot=\([ab]\)$/\1/p' | head -n 1)
	dir="$esp${slot:+/$slot}"
	rd="$root/boot/initramfs-kdos.cpio.gz"
	[ -f "$rd" ] || rd="$root/boot/initramfs.cpio.gz"
	if [ -f "$rd" ] && cmp -s "$rd" "$dir/initramfs.cpio.gz" &&
		[ "$(kernel_version "$dir/vmlinuz" 2>/dev/null)" = "$version" ] &&
		cmp -s "$root/boot/vmlinuz-kdos" "$dir/vmlinuz"; then
		on_esp=1
	fi
fi
if [ -n "$on_esp" ]; then
	echo "linux $version is already on the ESP"
	exit 0
fi

ports/core/boot/linux/postinstall.sh, lines 67–95

Some installs put the same version back: a source build (kdos update install --source), which first restores the development files of linux and of everything else it compiles against; kdos update install --dev linux, the same restore asked for by name; and a forced reinstall. When nothing on the ESP would change, the script prints linux <version> is already on the ESP and stops. That is the case when all of these hold:

The prune and depmod above have already run; the initramfs build and the deploy below would only write the same files to the ESP again. A kernel rebuilt at the same version, or an initramfs-kdos built and never deployed, differs from the ESP’s copy and goes through both.

The new kernel’s initramfs

The image ships one initramfs, /boot/initramfs.cpio.gz, built by 70_image for the image’s own kernel. Its modules are of that kernel’s version and do not load into any other, so a new kernel needs an initramfs of its own. The script does not build one from nothing. It keeps the image’s initramfs as the base and appends a second archive with the new kernel’s copies of the same modules:

Which modules

The set is the image’s own list, /boot/initramfs.modules, which the image’s initramfs step writes beside the initramfs. A root without that file takes the set the base itself carries, read out of the archive:

archive_modules() (
	f="$1" off=0
	# Uncompressed newc archives at the front — the microcode — are walked
	# header by header; the compressed part starts after the zero padding
	# that follows the last one's trailer.
	while [ "$(dd if="$f" bs=1 skip=$off count=6 2>/dev/null | tr -d '\0')" = 070701 ]; do
		fsz=$((16#$(dd if="$f" bs=1 skip=$((off + 54)) count=8 2>/dev/null)))
		nsz=$((16#$(dd if="$f" bs=1 skip=$((off + 94)) count=8 2>/dev/null)))
		name=$(dd if="$f" bs=1 skip=$((off + 110)) count=$((nsz - 1)) 2>/dev/null)
		off=$(( (off + 110 + nsz + 3) / 4 * 4 ))
		off=$(( (off + fsz + 3) / 4 * 4 ))
		[ "$name" = 'TRAILER!!!' ] || continue
		while [ "$(od -An -tx1 -j $off -N 4 "$f" 2>/dev/null | tr -d ' \n')" = 00000000 ]; do
			off=$((off + 4))
		done
	done
	tail -c +$((off + 1)) "$f" | gzip -dc 2>/dev/null | cpio -t 2>/dev/null |
		while read -r p; do
			case "$p" in
				lib/modules/*/*.ko*|./lib/modules/*/*.ko*) ;;
				*) continue ;;
			esac
			p=${p##*/}
			echo "${p%%.ko*}"
		done | sort -u
)

ports/core/boot/linux/postinstall.sh, lines 112–137

The front of the base is one or more uncompressed newc cpio archives, the microcode, and the rest of the file is a gzip-compressed archive. The while loop walks the uncompressed ones header by header. Each newc header starts with the magic 070701 and has the size of the file’s data at offset 54 and the size of its name at offset 94, both as eight hexadecimal digits; the name follows the 110-byte header, and the name and the data are each padded to four bytes. After the archive whose last entry is TRAILER!!!, the loop skips the zero words that pad it and starts again. When no header follows, off is the start of the compressed part, which gzip -dc | cpio -t lists. Every lib/modules/*/….ko* path becomes a module name.

Without a set, the new initramfs would hold no module of the new kernel’s version, and the init could load none of the drivers it carries as modules. With this configuration those include f2fs, the device mapper under LUKS and LVM, and software RAID, so a root on any of them would not mount. vfat and xfs, which the script’s comment also names, are built into this kernel and need no copy.

Building the archive

build_initramfs() {
	out="$1" base="$root/boot/initramfs.cpio.gz" list="$root/boot/initramfs.modules"
	if [ ! -f "$base" ]; then
		echo "No $base: cannot build an initramfs for $version"
		return 1
	fi
	if [ -f "$list" ]; then
		mods=$(cat "$list")
	else
		mods=$(archive_modules "$base")
	fi
	if [ -z "$mods" ]; then
		echo "No module set in $list or $base: cannot build an" \
			"initramfs for $version"
		return 1
	fi
	work=$(mktemp -d) || return 1
	mkdir -p "$work/lib/modules/$version"
	for m in $mods; do
		modprobe -d "${root:-/}" -S "$version" --show-depends "$m" 2>/dev/null
	done |
	while read -r kind path _; do
		[ "$kind" = insmod ] || continue
		rel=${path#*/lib/modules/$version/}
		dest=$work/lib/modules/$version/${rel%.zst}
		[ -e "$dest" ] && continue
		mkdir -p "${dest%/*}"
		case "$path" in
			*.zst) zstd -q -d -c "$path" > "$dest" ;;
			*) cp "$path" "$dest" ;;
		esac
	done
	for f in modules.order modules.builtin modules.builtin.modinfo; do
		[ -f "$root/lib/modules/$version/$f" ] &&
			cp "$root/lib/modules/$version/$f" "$work/lib/modules/$version/"
	done
	depmod -b "$work" "$version" &&
	(cd "$work" && find . | cpio -o -H newc 2>/dev/null) | gzip -9 > "$work.gz" &&
	cat "$base" "$work.gz" > "$out"
	rc=$?
	rm -rf "$work" "$work.gz"
	return $rc
}

ports/core/boot/linux/postinstall.sh, lines 139–181

modprobe -d "$root" -S "$version" --show-depends prints, for each module in the set, an insmod line for it and for every module it needs, in the order they load, as found in the new kernel’s tree in the target root. A module built into the kernel prints a builtin line instead, which the read loop skips. Each module file is copied into a scratch tree under the same relative path, decompressed if it is a .zst. The three files depmod reads besides the modules, modules.order, modules.builtin and modules.builtin.modinfo, are copied too, and depmod -b indexes the scratch tree on its own. The tree is written as a newc archive, compressed with gzip -9, and appended to a copy of the base.

Keeping it, or removing the old one

initrd="$root/boot/initramfs-kdos.cpio.gz"
if build_initramfs "$initrd.new"; then
	mv -f "$initrd.new" "$initrd"
else
	rm -f "$initrd.new" "$initrd"
	echo "Warning: no initramfs for $version — a deploy of this root" \
		"is refused until one is built"
fi

ports/core/boot/linux/postinstall.sh, lines 186–193

The result is written beside the target and renamed over /boot/initramfs-kdos.cpio.gz, so a failure part-way never leaves a truncated file under the real name. A build that fails removes the previous /boot/initramfs-kdos.cpio.gz as well, because it belongs to the kernel this package has just replaced. kdos-bootctl deploy then finds only the image’s initramfs, refuses it beside a kernel whose modules it does not carry, and the slot keeps the kernel it had until an initramfs is built.

During the KDOS build itself, 60_kernel runs before 70_image has made the image’s initramfs, so the first build of a tree prints the No /boot/initramfs.cpio.gz message and the warning that follows it. That is expected. A later rebuild of the kernel in the same tree finds the image’s initramfs and builds one; 110_iso.sh removes /boot/initramfs-kdos.cpio.gz from the tree again, so the medium carries only the kernel and initramfs built together.

Putting the kernel on the ESP

if [ -n "$root" ]; then
	echo "Installed into $root: its kernel reaches the ESP with" \
		"\`kdos-bootctl deploy $root\`, which \`kdos update\` runs"
elif [ ! -d "$esp" ] || ! mountpoint -q /boot/efi 2>/dev/null; then
	:
elif ! command -v kdos-bootctl >/dev/null; then
	echo "Warning: no kdos-bootctl — $version is not on the ESP"
else
	echo "Putting $version on the ESP..."
	kdos-bootctl deploy / ||
		echo "Warning: $version is not on the ESP; the next boot runs" \
			"the kernel it ran before"
fi

ports/core/boot/linux/postinstall.sh, lines 200–212

The last step depends on the root the package went into:

Installed into What happens
Another root, such as the inactive slot during kdos update Nothing yet. The script prints the kdos-bootctl deploy command; kdos update apply runs it once every package of the run is in, so the slot’s kernel never reaches the ESP beside a half-updated root
The running system, with /boot/efi mounted and EFI/kdos on it kdos-bootctl deploy / copies the kernel and initramfs into the ESP directory of the slot this root is. The next boot runs the new kernel
The running system, with no ESP mounted, as on the live medium or in the build chroot Nothing
The running system, without kdos-bootctl A warning that the new kernel is not on the ESP

If deploy fails, the warning says that the next boot runs the kernel it ran before. What deploy checks before it writes anything is in A new kernel and the kdos-bootctl section.

See also