402. Encrypted root
This page is for anyone who encrypted, or wants to encrypt, the disk of an installed KDOS machine.
An installed KDOS machine can keep its root filesystem inside a LUKS container (Linux Unified Key Setup, the standard Linux disk encryption format). The container is opened at every boot with a passphrase typed at the boot splash, or, on the verbose and single-user boot entries, which show no splash, at a prompt on the screen. Without the passphrase, nothing on the disk can be read.
This page shows which pieces do that, the files that carry the container’s identity from the installer to the boot, and the tasks that come after an install: unlocking, adding and changing passphrases, keeping a copy of the header, and opening the root by hand from the live medium. How the initramfs finds, unlocks and mounts the root, and why the prompt is drawn the way it is, is in the KDOS book’s Unlocking an encrypted root; this page does not repeat it.
Installation
cryptsetup provides cryptsetup and libcryptsetup, through the kernel’s device mapper. It is on the live medium as well as on an installed system, because three programs use it:
- The installer, kdos-installer (built first in 130_kinstall) and its graphical front end kdos-installer-gui, which share one backend, creates the container and opens it while it installs.
- The initramfs carries its own copy of
cryptsetupand its libraries, put there byscript/phases/70_image/090_initramfs.sh; see The initramfs. An image built without it printsNote: cryptsetup not installed — the initramfs cannot unlock a LUKS rootduring that step. - udisks2 unlocks an encrypted removable disk for the desktop, through libblockdev’s crypto plugin, when Dolphin or the Device Notifier asks it to; that task is on Storage and removable media.
The kernel side is the dm-crypt and dm-mod modules of the linux port, which the initramfs carries and loads.
No service script is involved. The root is unlocked by the initramfs before /sbin/init runs, and nothing after switch_root touches the container again; there is no /etc/crypttab and nothing reads one. The live medium has no encrypted root and never shows the prompt.
Two other ports encrypt at a different level. gocryptfs encrypts a directory file by file, in user space through FUSE. tpm2-tss is installed, and tpm2-tools is not in the base (sudo kdos update install tpm2-tools; see Installing software); nothing in the boot path uses either; see Known gaps.
Configuration
Nothing is configured by hand. The installer writes the container’s identity into two places on the EFI system partition, and the initramfs reads both.
The kernel command line, in the boot entries of /boot/efi/limine.conf. The installer adds a cryptdevice= option naming the container beside the root= option naming the filesystem inside it, from src/system/kinstall-core/install.c:
/*
* Two UUIDs, and confusing them is the whole trap: `root=` names the
* FILESYSTEM inside the container, `cryptdevice=` names the container
* itself. The filesystem's UUID does not exist until the container is
* open, which is why the initramfs unlocks first and looks second.
*/
if (cfg.luks && luks_part[0]) {
Part lp;
probe_part(luks_part, &lp);
if (lp.uuid[0]) {
snprintf(crypt_opt, sizeof(crypt_opt),
"cryptdevice=UUID=%s:%s ", lp.uuid, LUKS_NAME);
kb_strlcpy(luks_uuid, lp.uuid, sizeof(luks_uuid));
}
else if (!cfg.dry_run)
fail("cannot read the LUKS UUID back from %s",
luks_part);
logf_("LUKS UUID %s", lp.uuid);
}
src/system/kinstall-core/install.c, lines 3766–3784
The two UUIDs are different. cryptdevice=UUID=<container>:kdosroot is the LUKS container on the partition, and root=UUID=<filesystem> is the filesystem that appears only once the container is open as /dev/mapper/kdosroot. The shape of the whole entry is in What the bootloader step writes, and how to change a kernel option is on Boot menu and kernel parameters.
The boot state, /boot/efi/EFI/kdos/bootstate, records the container again as crypt_a, beside slot A’s filesystem, from the same file:
mkpath(TARGET "/boot/efi/EFI/kdos");
wr("/boot/efi/EFI/kdos/bootstate",
"# KDOS boot state. Written by the installer; maintained by\n"
"# kdos-bootctl.\n"
"slot_a = %s\n"
"slot_b = %s\n"
"crypt_a = %s\n"
"crypt_b = %s\n"
"active = a\n"
"try = \n"
"attempts = 0\n",
root_uuid, b_uuid, luks_uuid, b_uuid[0] ? luks_uuid : "");
}
src/system/kinstall-core/install.c, lines 4067–4079
On the A/B layout both root slots are volumes of one LVM group inside the one container, so crypt_b names the same container as crypt_a, and one passphrase opens either slot. The initramfs asks kdos-bootctl crypt for the container of the slot it chose, and uses cryptdevice= only when the slot records none; see A slot knows its own container.
The unlock in the initramfs. The generated init runs this block when a container has been named, by the chosen slot’s crypt_ entry or by cryptdevice=, from script/phases/70_image/090_initramfs.sh (the \$ is the escape of the heredoc the init is written from):
if [ -n "\$CRYPTDEV" ]; then
# One extra stage on the progress bar, added here rather than up front:
# the total is additive, and only this branch knows there is an unlock.
sp_total 1
sp_step "UNLOCKING"
unlock_root "\$CRYPTDEV"
UNLOCK_RC=\$?
if [ \$UNLOCK_RC -eq 0 ]; then
sp_ok
# The filesystem inside the container has only just appeared, so the
# udev pass that ran before the unlock never saw it.
udevadm settle 2>/dev/null || true
activate_lvm
elif [ \$UNLOCK_RC -eq 2 ]; then
sp_fail
rescue "Wrong passphrase three times. Ctrl+Alt+Del restarts and asks again."
else
sp_fail
rescue "Failed to unlock \$CRYPTDEV"
fi
fi
script/phases/70_image/090_initramfs.sh, lines 1029–1049
unlock_root, the function it calls, is walked through on The initramfs init.
/etc/fstab names the root by the UUID of the filesystem inside the container, never by the container; see Filesystems and fstab.
Usage
Encrypting the root at install
Encryption is chosen while installing. Encrypting a partition erases what is on it, and the installer does not encrypt an existing installation in place. Choose the passphrase with care: there is no recovery key, and without the passphrase nothing on the disk can be read again.
In the graphical installer:
- Start the installer from the live medium and choose the disk and plan on the Disk page.
- On the Encryption page, tick Encrypt the system disk.
- Type the passphrase in both fields. They must match, and the passphrase cannot start or end with a space. With encryption on, the Account page offers to log in straight after the disk passphrase, without the login screen.
- Check the Summary page, whose Encryption line reads LUKS2, a passphrase at every start, and install.
On the reuse plan the root partition you chose is encrypted, which erases it; the other partitions are not touched.
In the text installer:
- Start the installer from the live medium and go to the Layout page.
- Choose the erase plan and tick
Encrypt the root filesystem (LUKS2); the text installer offers the box on the erase plan only. - Type the passphrase in both fields. They must match.
- Optionally tick
Two system slots for safe updates (LVM)as well; the volume group is then made inside the container, so one passphrase opens it; see LVM and RAID. - Check the Summary page and install. With LVM on, its
rootline saysLVM inside LUKS2; without it, anencryptionline saysroot inside LUKS2.
The tick stays set if you then switch to the reuse plan, or to Partition it myself first (cfdisk), which leads to it once cfdisk closes, and the root you choose there is encrypted; the Summary’s encryption line says so. Untick it on the erase plan before switching if you do not want that; see Encryption.
Either way, the installer runs cryptsetup luksFormat --type luks2 on the root partition and opens it as /dev/mapper/kdosroot, with the passphrase on standard input both times; see An encrypted root.
Unlocking at boot
- Choose the boot entry. The splash shows
DISK PASSPHRASE (attempt 1 of 3). OnKDOS <version> (verbose)andKDOS <version> (single user), which carry noquietand run no splash, the screen showsDisk passphrase (attempt 1 of 3):instead. - Type the passphrase and press Enter. Nothing is drawn as you type, not even asterisks.
- The splash shows
UNLOCKEDand the boot goes on, orWRONG PASSPHRASEand asks again; without the splash the screen showsUnlocked.orWrong passphrase. - After three wrong attempts the boot stops in a shell whose reason reads
Wrong passphrase three times. Ctrl+Alt+Del restarts and asks again.; see Troubleshooting.
Finding the container
The container is the partition cryptdevice= names. On the running system:
cat /proc/cmdline
sudo cryptsetup status kdosroot
sudo blkid -t TYPE=crypto_LUKS
cryptsetup status prints the partition under device:, beside the container’s type and cipher:
/dev/mapper/kdosroot is active.
type: LUKS2
cipher: aes-xts-plain64
keysize: 512 [bits]
key location: keyring
device: /dev/nvme0n1p2
The commands below write it as /dev/<part>; replace it with that partition.
Adding a second passphrase
The installer makes one passphrase and no recovery key. A LUKS2 container has several keyslots (places to store a passphrase), and a second passphrase kept somewhere safe serves as a recovery key.
Add a keyslot. cryptsetup asks for an existing passphrase, then the new one twice:
sudo cryptsetup luksAddKey /dev/<part>List the keyslots in use:
sudo cryptsetup luksDump /dev/<part>Its
Keyslots:section has one numbered entry per passphrase,0: luks2and now1: luks2.
Either passphrase opens the root at the boot prompt: the initramfs hands what you type to cryptsetup open, which tries it against every keyslot.
Changing a passphrase
sudo cryptsetup luksChangeKey /dev/<part>
It asks for the passphrase to replace, then the new one twice. sudo cryptsetup luksRemoveKey /dev/<part> removes a passphrase altogether; never remove the last one.
Keeping a copy of the header
A damaged LUKS header makes the whole container unreadable, whatever the passphrase. Keep a copy off the machine:
sudo cryptsetup luksHeaderBackup /dev/<part> --header-backup-file kdosroot-header.img
The file is the size of the container’s header area, 16 MiB for the installer’s LUKS2 container, and is made readable by root alone. sudo cryptsetup luksHeaderRestore /dev/<part> --header-backup-file kdosroot-header.img writes it back.
Keep the file as safe as the passphrase itself. A header backup opens the container with any passphrase that was valid when the copy was taken, even one removed since: sudo cryptsetup open --test-passphrase --header kdosroot-header.img /dev/<part> accepts it (cryptsetup-luksHeaderBackup(8)).
Opening the root from the live medium
To repair or copy an installed system that will not boot:
Boot the KDOS live medium on the machine.
Open the container:
sudo cryptsetup open /dev/<part> kdosrootIf the install used LVM, activate the volume group inside the container. The live boot activated its volume groups (
/etc/init.d/03_lvm.sh) before the container was open, so it did not see this one:sudo vgchange -aayThe root is then
/dev/kdos/root_a; without LVM it is/dev/mapper/kdosroot.Check an ext4 root before mounting it, then mount it on a directory of its own.
/mntitself would hide the live session’s boot medium at/mnt/isoand its persistence store at/mnt/persist. With LVM, use/dev/kdos/root_ain place of/dev/mapper/kdosroot:sudo e2fsck -f /dev/mapper/kdosroot sudo mkdir -p /mnt/target sudo mount /dev/mapper/kdosroot /mnt/targetRepairing from the live medium goes on from here: the logs to read, and how to reach the installed system’s EFI system partition.
When done, undo the steps in reverse:
sudo umount /mnt/target sudo vgchange -an kdos # only with LVM sudo cryptsetup close kdosroot
An encrypted second slot
A second root slot in its own container is recorded with both UUIDs, the filesystem’s and the container’s: sudo kdos-bootctl set-slot b <filesystem-uuid> <luks-uuid>. The steps for making the slot are on A/B slots and rollback. What is known about booting one is in Only an LVM erase install gets a second root slot.
Tips
- The passphrase prompt uses the console keymap named in
/boot/efi/EFI/kdos/keymap, which the installer writes andkdos-settingsdkeeps equal to/etc/keymap; the initramfs carriesloadkeysand kbd’s keymaps for it. A character that console map has no key for cannot be typed there, and a layout with no console map (Arabic, the Asian scripts) gets the kernel’s US map. For such a layout, add withluksAddKeya passphrase that a US keyboard can type.cat /boot/efi/EFI/kdos/keymapshows the map the next boot loads; see Unlocking an encrypted root. - Never pass a passphrase as a command-line argument. Any process can read another’s arguments through
/proc/<pid>/cmdline, which is why every KDOS program that feedscryptsetupuses standard input. Typed at cryptsetup’s own prompt, as in the procedures above, it is not exposed. - Encryption protects the disk while the machine is off. Once the root is open, every process that can read a file reads it in the clear; see What is not protected.
- For a directory that is synchronised or backed up as files, gocryptfs keeps one encrypted file per plain file, so a changed file changes one file in the copy, not a whole container image.
kdos why cryptsetupprints the reasons recorded against the port, including why the prompt is read fromtty1.
Troubleshooting
When the initramfs (the small system that runs before the real root is mounted) cannot finish, it puts the reason on the screen and opens a shell on tty1; see The rescue shell. That shell runs in a loop, so exit opens a fresh one rather than crashing the kernel. The initramfs has no reboot, and Ctrl+Alt+Del restarts at once without writing pending data to disk, so after changing a disk restart with:
sync; echo b > /proc/sysrq-trigger
Wrong passphrase three times. Ctrl+Alt+Del restarts and asks again. The container rejected every attempt. Besides a mistyped passphrase, the keyboard map can cause this (see Tips): an ESP with no EFI/kdos/keymap, or one naming a map without the passphrase’s characters, leaves the US map. Restart and type the passphrase by its US key positions, or open the root from the live medium, whose layout you can set, and add a passphrase that types the same on both.
cryptdevice: no device for UUID=…:kdosroot. No partition carries the container UUID the boot asked for. In the rescue shell, compare the two:
cat /proc/cmdline
blkid
A disk that is not connected, or a container recreated since the install, gives this. Boot the live medium, mount the installed machine’s EFI system partition on a directory of its own with sudo mkdir -p /mnt/esp and sudo mount /dev/<esp> /mnt/esp, and correct the cryptdevice= option in the entries of /mnt/esp/limine.conf, and crypt_a in /mnt/esp/EFI/kdos/bootstate, to the UUID that sudo blkid -t TYPE=crypto_LUKS reports. Unmount it with sudo umount /mnt/esp.
cryptdevice: this initramfs has no cryptsetup. The image was built without cryptsetup, so its initramfs cannot open a container. The step’s Note: line is in the 70_image build log; build the image with the cryptsetup port installed.
cryptdevice: cannot parse '…'. The option is neither UUID=<uuid>:<name> nor /dev/<node>:<name>. Correct it in limine.conf from the live medium, as above.
UNLOCKED, then the boot stops at ROOT DEVICE with Disk boot failed: no root at UUID=… with /sbin/init on it. The container opened but no filesystem with the root= UUID appeared inside it. In the rescue shell, blkid lists /dev/mapper/kdosroot with the UUID it carries. On an LVM install the splash shows whether the VOLUME GROUPS stage after the unlock failed; see LVM and RAID.
ROOT FILESYSTEM HAS ERRORS - RUN e2fsck. The root opened and its check left errors. Repair it from the live medium as in Opening the root from the live medium, running e2fsck -f before you mount.
Known gaps
- A disk-encryption key is typed, not sealed in the TPM
- Only an LVM erase install gets a second root slot, and what it means for an encrypted second slot
- Secure Boot is not supported
- Security: disk encryption protects data at rest only
- Status: how far the unlock path has been tested
See also
- Unlocking an encrypted root: the prompt, the two UUIDs and the order of the boot
- A slot knows its own container: why the container is recorded per slot
- Encrypting the root: the installer’s side, in the user guide
- An encrypted root: the commands the installer runs
- Storage and removable media: unlocking a removable disk through udisks2
- The initramfs init: every stage of the boot before
switch_root - LVM and RAID, Filesystems and fstab, A/B slots and rollback, Boot menu and kernel parameters, Backups