396. The initramfs and the rescue shell
The initramfs is the archive the kernel unpacks into memory and runs first; its init finds, unlocks and mounts the root and hands over to it. This page covers where a KDOS machine’s initramfs comes from, how to look inside one, what an installed machine does when a new kernel arrives, and how to use the rescue shell the initramfs opens when it cannot finish. What init does at each stage, and why each program in it must be the one it is, is explained in The initramfs; this page gives the procedures.
Installation
The initramfs is not a package. script/phases/70_image/090_initramfs.sh generates it in the image phase (see Part XV — The image) from programs other ports installed, and 110_iso.sh copies the result to the medium’s boot/initrd.img and to /boot/initramfs.cpio.gz in the image, so every installed system carries it. The medium’s copy is named initrd.img because that is a path Ventoy recognises: Ventoy never reads limine.conf, and puts its hook only into an initrd at a path it knows. No package owns that file.
What it carries comes from these ports:
| Port | In the initramfs |
|---|---|
| bash, coreutils, grep, sed | The shell init is written in, and the tools it and the rescue shell run |
| util-linux | blkid, mount, umount, losetup, switch_root, dmesg, setsid |
| eudev, kmod | udevd, udevadm, modprobe and the udev rules whose programs the initramfs carries |
| linux | The modules in the build’s list |
| e2fsprogs | e2fsck, for the check an ext root gets before it is mounted |
| mdadm, cryptsetup, lvm2, thin-provisioning-tools | RAID assembly, the LUKS unlock and volume-group activation, each when installed |
| kdos-tools | kdos-bootctl, for A/B slot selection |
| kdos-splash | The boot splash |
| intel-ucode, firmware-amd | The microcode, in a separate archive in front; see Microcode and firmware |
On an installed machine, the linux package’s postinstall builds a second file for every kernel it installs, /boot/initramfs-kdos.cpio.gz: the image’s initramfs with one more archive appended, holding that kernel’s copies of the modules. kdos-bootctl deploy and the installer copy that file to the ESP when it exists, and the image’s own file otherwise.
Configuration
Nothing about the initramfs is configured on a running machine. It is read-only, and there is no mkinitcpio.conf or dracut.conf to edit. Its behaviour is set in the tree, by the image’s build script and the linux postinstall, and by one file on the machine.
The module list, in the build script, decides which disks and filesystems can hold a root:
# Core Modules for Booting (Storage, FS, Input, etc.)
MODULES="overlay squashfs isofs cdrom sr_mod loop sd_mod ata_piix ahci libahci virtio virtio_blk virtio_pci virtio_scsi xhci-pci xhci-hcd ehci-pci ehci-hcd ohci-pci ohci-hcd usb-storage uas"
# Every disk transport the installer classifies and installs to must be
# reachable before the root mounts, or the installed root is not found on its
# first boot. NVMe behind Intel VMD (RST), eMMC and SD (mmc), Hyper-V, Xen
# and VMware guests' disks, and the MegaRAID and mpt3sas controllers are all
# modules on this kernel; copy_module brings mmc_core, hv_vmbus and the rest
# of each one's dependencies.
MODULES="$MODULES vmd mmc_block sdhci-pci sdhci-acpi cqhci rtsx_pci_sdmmc hv_storvsc xen-blkfront vmw_pvscsi megaraid_sas mpt3sas"
# vfat, so the initramfs can read the boot state off the ESP.
MODULES="$MODULES vfat nls_cp437 nls_iso8859-1"
# Root filesystems the installer can create. EVERY ROW OF ki_filesystems[] NOT
# BUILT INTO THE KERNEL MUST APPEAR HERE: the installer's table offers the
# choice, this makes it bootable, and a root the initramfs cannot mount
# installs perfectly and never boots again. f2fs is CONFIG_F2FS_FS=m; xfs is =y
# and its copy is a no-op, kept so a kernel with xfs as a module still boots.
MODULES="$MODULES xfs f2fs"
# dm-crypt and the ciphers a LUKS2 default header actually uses. Carried
# unconditionally: they are small, and a kernel that has them built in makes
# copy_module a no-op anyway. The generic AES, SHA-256 and SHA-512 code is
# built into this kernel and has no module to name.
MODULES="$MODULES dm-crypt dm-mod aesni-intel xts crypto_null algif_skcipher"
# Software RAID. A root ON an array needs these before anything can be
# assembled, and a machine whose DATA disks are an array needs them before
# udev settles — without md_mod the members are bare disks with a superblock
# nobody reads, which looks like an empty drive rather than a missing module.
# The personalities are listed individually because md_mod loads none of them.
MODULES="$MODULES md_mod raid0 raid1 raid10 raid456 dm-raid"
# The device-mapper targets an LVM root can be built from beyond the linear one
# dm-mod has built in: a thin volume, a cached or write-cached one, and a
# snapshot. A group holding any of them activates only with its target loaded,
# and the init loads these before its first vgchange -- lvm2's own modprobe
# path is whatever its configure found at build time, not one this initramfs
# is known to have.
MODULES="$MODULES dm-snapshot dm-thin-pool dm-cache dm-cache-smq dm-writecache"
for MOD in $MODULES; do
copy_module $MOD
done
# The same list, kept on the image beside the initramfs it describes. A kernel
# installed later has none of these modules in this initramfs, and the linux
# package's postinstall reads this file to carry the new kernel's copies of
# exactly this set; a second copy of the list there would be the one that
# goes stale.
printf '%s\n' $MODULES > /boot/initramfs.modules
script/phases/70_image/090_initramfs.sh, lines 466–512
The last line writes the same list to the image as /boot/initramfs.modules. That file is the one setting on the machine: the linux postinstall reads it to decide which modules to append for a new kernel. A module named there is carried for the next kernel installed into that root.
The command-line words the generated init reads are below (\$ here is the generator’s escaping: the init is written by a heredoc, and the generated script has a plain $). The live path checks for nopersist separately, to skip the persistence store, and the medium scan reads kdos.medium= itself, to take only the medium it names. Every word is explained in Command-line parameters KDOS reads:
# Parse Boot Parameters. INIT_ARG starts empty: the kernel hands init every
# unrecognised name=value on its command line as an environment variable.
INIT_ARG=""
SPLASH_QUIET=""
for i in \$(cat /proc/cmdline); do
case "\$i" in
root=UUID=*)
ROOT_UUID="\${i#root=UUID=}"
;;
cryptdevice=*)
CRYPTDEV="\${i#cryptdevice=}"
;;
bootstate=UUID=*)
BOOTSTATE_UUID="\${i#bootstate=UUID=}"
;;
kdos_slot=*)
BOOT_SLOT="\${i#kdos_slot=}"
;;
# Handed on to init: \`single\` runs rcS and then sulogin, with no
# consoles and no desktop; \`emergency\` runs sulogin before rcS.
single|emergency)
INIT_ARG="\$i"
;;
# The splash draws only on a boot with \`quiet\`; without it nothing
# is on the screen to carry the passphrase prompt.
quiet)
SPLASH_QUIET=1
;;
esac
done
script/phases/70_image/090_initramfs.sh, lines 740–770
The rescue helper, which every failure ends in:
# EVERY FAILURE ENDS HERE, AND SAYS WHY WHERE SOMEBODY IS LOOKING. /dev/console
# is the last console= on the command line, which on the quiet live entries is
# the serial port: a shell there alone leaves a laptop showing the splash's fail
# state and nothing else. The reason goes to the splash as a detail line, held
# on the screen for RESCUE_HOLD seconds before the splash fades out (a quit
# straight after the detail shows it only during the fade, too dark to read),
# and then to tty1, where fbcon takes the framebuffer over at the first write
# and the keyboard is. An optional second argument is a hint printed under the
# reason on the screens, never on the splash, which has one detail line. Both
# screens say that Ctrl+Alt+Del restarts: the kernel reboots on it at once
# until init disables it, and nothing here does. That restart is immediate and
# syncs nothing, and a failure after the root is mounted (a missing /sbin/init,
# a persistent live upper) leaves a disk mounted read-write, so the boot's own
# writes are synced before the shell opens and the screens tell whoever
# changes a disk to run sync first. On tty1 under a serial /dev/console the
# screen names the menu's verbose entry, the one way to see the boot log
# there, and under the reason and hint tty1 lists RESCUE_TRIED, the devices
# a failed medium scan tried and why each was passed over: the scan's own
# line about them went to the serial console only. tty1 gets a shell loop of
# its own, in a process setsid -f detaches from this one: each shell is a
# setsid -c session with tty1 as its controlling terminal, and tty1 is
# released when that session ends, so the next shell can claim it and an exit
# there opens a fresh one. It is never a background job of this shell, which
# runs without job control: bash starts an asynchronous command with SIGINT
# and SIGQUIT ignored, every shell the loop opened would inherit that, and
# Ctrl+C would reach nothing it runs. This process runs a shell on /dev/console
# in a loop, so the serial line keeps its shell and the log it has been reading,
# and an exit there, or an input that ends the shell, opens a fresh one. When
# /dev/console is itself a virtual terminal there is one screen and one shell,
# and the reason is printed on it once: an echo before the branch and the printf
# in it would both land on that one screen. That shell is opened on the terminal
# itself, never on /dev/console, which cannot be a controlling terminal: a shell
# there has no job control, says so in two lines of noise, and lets Ctrl+C reach
# nothing it runs. setsid -c makes the terminal the shell's controlling terminal
# and -w waits for it, in a loop, so an exit opens a fresh shell. Every shell,
# whatever its exit status, is followed by a second's wait before the next: a
# shell that cannot start, or one that reads end-of-file at once from a hung-up
# line and exits 0, would otherwise spin the loop and flood the console with
# prompts. PID 1 must never return from here, on either branch: a shell that
# exits as PID 1, or a return from this function, panics the kernel with
# "Attempted to kill init".
#
# EVERY FIXED LINE AND EVERY HINT IS 80 COLUMNS OR FEWER. They are printed
# before fbcon's deferred takeover has widened the console, while the VT is 80
# columns, and a longer line wraps a lone word onto a line of its own.
RESCUE_HOLD=4
RESCUE_TRIED=""
RESCUE_RESTART="Ctrl+Alt+Del restarts at once; run sync first if you changed a disk."
RESCUE_VERBOSE="The boot menu's '(verbose)' entry shows the boot log on this screen."
rescue() {
local con
if [ -x /bin/kdos-splash ]; then
/bin/kdos-splash detail "\$1" 2>/dev/null
[ -n "\$SPLASH_QUIET" ] && sleep "\$RESCUE_HOLD"
/bin/kdos-splash quit 2>/dev/null
fi
sync
con=\$(cat /sys/class/tty/console/active 2>/dev/null)
con=\${con##* }
case "\$con" in
tty[0-9]*)
printf '\n%s\n' "\$1"
[ -n "\$2" ] && printf '%s\n' "\$2"
echo "KDOS cannot boot. This is a rescue shell."
echo "\$RESCUE_RESTART"
while :; do
setsid -c -w /bin/sh -i < "/dev/\$con" > "/dev/\$con" 2>&1; sleep 1
done
;;
esac
echo "\$1"
[ -n "\$2" ] && echo "\$2"
{
printf '\n%s\n' "\$1"
[ -n "\$2" ] && printf '%s\n' "\$2"
[ -n "\$RESCUE_TRIED" ] && printf 'Devices tried:\n%s\n' "\$RESCUE_TRIED"
echo "KDOS cannot boot. This is a rescue shell."
echo "\$RESCUE_VERBOSE"
echo "\$RESCUE_RESTART"
} > /dev/tty1 2>/dev/null
setsid -f /bin/sh -c 'while :; do setsid -c -w /bin/sh -i < /dev/tty1 > /dev/tty1 2>&1; sleep 1; done' < /dev/null > /dev/null 2>&1
echo "A rescue shell is also open on tty1."
while :; do
/bin/sh; sleep 1
done
}
script/phases/70_image/090_initramfs.sh, lines 589–675
The new kernel’s initramfs and its deploy, in the linux postinstall:
# A failed build removes the previous one too: it belongs to the kernel this
# package just replaced. `kdos-bootctl deploy` then finds only the image's
# initramfs, and refuses it beside a kernel whose modules it does not carry.
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
# Put this kernel on the ESP, into the directory of the slot this root is.
# Installing into the running system, that happens here; installing into
# another root — `kdos update` filling the inactive slot — it is `kdos update`
# that deploys, once every package of the run has gone in, because the slot's
# kernel must not reach the ESP beside a half-updated root.
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 183–212
Usage
Finding the initramfs a machine boots
| Machine | File |
|---|---|
| Live medium | boot/initrd.img on the medium, which the running live system sees as /mnt/iso/boot/initrd.img |
| Installed, booted | /boot/efi/EFI/kdos/<slot>/initramfs.cpio.gz, where <slot> is the kdos_slot= in /proc/cmdline |
| Installed, in the root | /boot/initramfs.cpio.gz (the image’s) and, after a kernel update, /boot/initramfs-kdos.cpio.gz; the kernel beside them is /boot/vmlinuz-kdos |
Until a kernel update, the file in slot A’s directory is a byte-for-byte copy of the root’s /boot/initramfs.cpio.gz, and the live medium’s is the same file again.
Looking inside
The image’s file is the uncompressed microcode archive followed by one gzip-compressed archive; an initramfs-kdos.cpio.gz has a second gzip-compressed archive after that. GNU cpio (the cpio port) stops at the end of the first archive, so on a machine with microcode it lists only that archive. The ESP is mounted readable by root alone, so let cpio open the file itself with -F rather than redirecting it from your own shell:
sudo cpio -t -F /boot/efi/EFI/kdos/a/initramfs.cpio.gz
On the shipped image that first listing is the kernel/x86/microcode/ directory with AuthenticAMD.bin and GenuineIntel.bin in it, followed by a count line such as 35184 blocks; the number changes with the microcode the release carries.
To list the rest:
Note the
N blockslinecpio -tprints on standard error for the first archive.Skip that many 512-byte blocks and decompress what follows:
sudo dd if=/boot/efi/EFI/kdos/a/initramfs.cpio.gz bs=512 skip=N | zcat | cpio -t
On a file with no microcode archive in front, the first cpio -t fails on the gzip header, and sudo zcat <file> | cpio -t lists the archive directly.
Either listing stops at the end of the image’s archive. In an initramfs-kdos.cpio.gz, the modules the linux postinstall appended follow it as one more archive, which cpio -t does not reach.
A new kernel on an installed machine
- Install or update the
linuxpackage, throughkdos update applyorkpkg. - Its postinstall builds
/boot/initramfs-kdos.cpio.gzin the root it installed into. When that root is the running system and/boot/efiis mounted, it runskdos-bootctl deploy /, and the next boot uses the new pair. - When it installed into another root, the A/B update deploys after every package is in; see A/B slots and rollback.
- Check what each slot boots:
sudo kdos-bootctl statusprints the kernel version per slot.
Using the rescue shell
When init cannot finish, the splash shows the reason and quits, and a shell opens on /dev/console with the reason printed above it. On an installed machine /dev/console is tty1, the screen. When it is the serial line, as on the live entries or on any command line whose last console= is ttyS0, a second shell opens on tty1 with the same reason, and the serial line prints A rescue shell is also open on tty1. above its own prompt. When no medium was found, tty1 lists under the reason, after Devices tried:, each device the scan tried and why it was not the medium, one to a line and at most eight. The screen’s banner says that the boot menu’s (verbose) entry shows the boot log on this screen, and ends with Ctrl+Alt+Del restarts at once; run sync first if you changed a disk. Every line of it fits 80 columns, the console’s width before the framebuffer console takes over.
- Read the reason. The table in Troubleshooting says what each one means.
- Look around.
PATHis/bin.blkid,mount,umount,dmesg,modprobeand the coreutilsinituses (ls,cat,cp,dd,mkdirand the like) are always there;cryptsetup,e2fsck,mdadmandlvmare there when the image carried them, as the shipped image does. There is no editor, nottyand no network tool. The shell ontty1hastty1as its controlling terminal, so job control works andCtrl+Cstops the program it runs. The shell on a serial/dev/consolehas no controlling terminal, so it has no job control and says so when it starts;Ctrl+Cdoes not reach a program it runs. - Repair what you can: run
e2fsckon an unmounted ext root, open a container withcryptsetup open, assemble an array withmdadm --assemble --scan. - Restart the machine:
Ctrl+Alt+Del, orsync; echo b > /proc/sysrq-trigger. The initramfs carries noreboot, andCtrl+Alt+Delthere restarts at once with nosyncof its own, so runsyncfirst after changing a disk.
exit opens a fresh shell, on tty1 and on the serial line alike: the init runs every rescue shell in a loop, because process 1 leaving it would panic the kernel with Attempted to kill init. Under a serial console the tty1 loop runs in a process of its own beside process 1’s loop on the serial line, so each screen gets its fresh shell independently.
Tips
blkidwith no arguments lists every filesystem the initramfs can see, with its UUID; compare it withroot=andcryptdevice=incat /proc/cmdline.- On an installed machine, the
KDOS <version> (verbose)entry prints every kernel message, which shows a missing driver before the splash reaches the stage that needs it. - The init, the programs and the microcode in the image’s initramfs are the medium’s. A kernel update, through
kdos updateorkpkg, runs thelinuxpostinstall, which appends modules and never rebuilds that base, so the copy in a root is the one it was installed or copied from. kdos doctorwarns when the mount namespace’s root is not/, the sign of aswitch_rootthat only chrooted; seeswitch_root.
Troubleshooting
| What the screen says | Check | Fix |
|---|---|---|
Error: Root device with UUID=… not found!, then Disk boot failed: no root at UUID=… with /sbin/init on it |
blkid in the rescue shell: is the UUID there, and on which device? |
A disk that is absent from blkid needs a driver the initramfs does not carry; add its module name to /boot/initramfs.modules in that root, and the next kernel the linux package installs there carries it. A changed UUID: record it with kdos-bootctl set-slot <slot> <fs-uuid> [<luks-uuid>], where <slot> is a or b, and the menu’s root= is regenerated from the boot state. On an encrypted slot give the container’s UUID too: set-slot clears it when it is left out. On a machine with no boot state, correct root= in the /KDOS entries of limine.conf |
Error: /sbin/init not found on root device!, then the same Disk boot failed line |
mount the root by hand in the rescue shell and look at /sbin/init |
The root is not a KDOS root, or is damaged; see Recovering an unbootable system |
cryptdevice: no device for … or Failed to unlock … |
blkid -t TYPE=crypto_LUKS |
A wrong container UUID on the command line or in the boot state |
Wrong passphrase three times. Ctrl+Alt+Del restarts and asks again. |
The keyboard layout: the prompt reads US key positions | Ctrl+Alt+Del gives three more tries; see Encrypted root |
ROOT FILESYSTEM HAS ERRORS - RUN e2fsck on the splash, and the boot goes on |
e2fsck -p left errors on an ext root |
Run e2fsck -f on the root while it is not mounted, from the live medium |
Failed to find the KDOS live medium. (live) |
Is the stick still in? Did it enumerate within the scan’s ten seconds? Devices tried: on tty1 under a serial console, or the No KDOS media; tried: line on the serial line or on the one screen of a verbose boot, names each device and what it was |
Try another port, or the KDOS Live (verbose) entry, and read the serial line |
Failed to find the KDOS live medium., then Write the whole ISO with dd, Rufus DD mode or Ventoy; copied files cannot boot., from a stick written as files (Rufus in ISO mode, or the ISO’s files copied onto a FAT stick) (live) |
Devices tried: on tty1 under a serial console, or the No KDOS media; tried: line on the serial line or on the one screen of a verbose boot, names the stick’s disk and partitions as not ISO 9660 |
The scan finds the medium only as an ISO 9660 filesystem. Write the whole ISO: dd, Rufus in DD mode, ISO Image Writer, or a Ventoy stick in normal mode |
Failed to find the KDOS live medium., with Cannot release /mnt/iso after <dev> — stopping the scan before it on the serial line (live) |
The scan’s one mount point stayed busy | Reboot; report the device |
Failed to set up a loop device for system.sfs, Failed to mount system.sfs or Error: /sbin/init not found in new root! (live) |
dmesg in the rescue shell, for read errors on the medium |
The medium was found but its system image cannot be read: write the ISO to the stick again, or use another stick |
Attempted to kill init |
The kernel panicked | init itself died: boot the KDOS <version> (verbose) entry and read the messages above the panic |
The stage the splash stopped on narrows it down; see When a boot stops.
Known gaps
- Per-slot kernels have been booted on virtual firmware only, which includes the initramfs the
linuxpostinstall appends to - A root filesystem on LVM has been booted in one shape only
- A disk-encryption key is typed, not sealed in the TPM
See also
- The initramfs and The rescue shell
- A new kernel
- Unlocking an encrypted root, Finding the root, Checking the root
- Microcode and firmware: the microcode archive in front of the initramfs, and checking that it loaded
- A/B slots and rollback: the slot the initramfs selects, and rolling an update back
- Recovering an unbootable system: every way into a broken machine, of which the rescue shell is one
- Encrypted root: unlocking at boot, and opening the root by hand
- LVM and RAID: volume groups and arrays the root can live on