13.5. Boot Integration and Failure Handling

Everything built so far was built by hand on a running system. At boot, the same stack must assemble itself in dependency order, and the failure modes are the ones this section collects: arrays that assemble degraded, volume groups with missing members, and containers whose unlock material is gone.

13.5.1. Root Storage, Initramfs, and Unlock Dependencies

The boot walk of Chapter 7 Section 7.1.1 - firmware, boot loader, kernel and initramfs, then the real root - gains steps when the root filesystem sits on layered storage. GRUB loads the kernel and initramfs from an unencrypted /boot or EFI partition outside the stack; the initramfs must then assemble the array, unlock the container, activate the volume group, and only then mount root=. Anything root depends on has to work before systemd exists, so its configuration travels inside the initramfs image, and the image must be regenerated whenever that configuration changes - the rule Section 13.1.3 stated, and Section 13.3.2 already applied by embedding mdadm.conf.

The cryptsetup package's initramfs integration is the piece that handles unlocking. Regenerate the image as the chapter's standing rule requires:

# update-initramfs -u
update-initramfs: Generating /boot/initrd.img-6.12.86+deb13-amd64
W: No zstd in /usr/bin:/sbin:/bin, using gzip

The gzip fallback notice is the same environmental warning Section 13.3.2 explained. Inspect what the image carries for unlocking:

# lsinitramfs /boot/initrd.img-$(uname -r) | grep -E "cryptsetup|cryptroot" | head
cryptroot
cryptroot/crypttab
scripts/local-block/cryptroot
scripts/local-bottom/cryptroot
scripts/local-top/cryptroot
usr/bin/cryptroot-unlock
usr/lib/cryptsetup
usr/lib/cryptsetup/askpass
usr/lib/cryptsetup/functions
usr/lib/x86_64-linux-gnu/libcryptsetup.so.12

The listing is the same pipeline form Section 13.3.2 used on the mdadm hooks. What it shows: cryptroot/crypttab, the crypttab-derived configuration the hook copied into the image; scripts/local-top/cryptroot and its siblings, the hook scripts that run at the right points of the early boot walk; askpass, the binary that puts the passphrase prompt on the console when a boot-critical container needs a credential; and cryptroot-unlock, the script for unlocking from an interactive initramfs shell when a boot that cannot prompt needs rescuing - its use is described in the cryptsetup-initramfs package's README.initramfs.gz. At a normal boot of an encrypted root, the prompt appears on the console before the root filesystem mounts; the system cannot proceed without the passphrase, which is precisely why /boot stays outside the container and why a lost passphrase with no header backup is unrecoverable by design.

Key files change that trade. A root container unlocked with a key file needs the key file available pre-root - which means shipping it inside the initramfs image or on the unencrypted boot partition, where anyone who steals the disk can read it. cryptsetup-initramfs supports including key files in the image (the KEYFILE_PATTERN setting in its configuration), and the package documentation is blunt about the consequence: a key readable from unencrypted storage defeats the container's purpose. The usual split is therefore a passphrase prompt for whatever root needs, and key files for data volumes unlocked by the crypttab generator once systemd starts - Section 13.4.4's arrangement. Swap follows root's rules when hibernation is in scope: a swap area inside the encrypted container is unlocked with it, but resuming from hibernation means the early boot code must unlock the container before reading the resume image, a requirement Chapter 4 Section 4.5.1 examined from the desktop side.

Two boot paths: the initramfs assembles, unlocks, and activates what root needs before systemd; data volumes are unlocked by systemd's crypttab generator and mounted by fstab once systemd starts.

Figure 13.7. Who unlocks what, and when. The left path runs before systemd with configuration embedded in the initramfs; the right path runs once systemd starts - the crypttab generator unlocks data volumes before /etc/fstab mounts them. Anything on the left must be regenerated into the image after every change.

The division of labor in Figure 13.7 is the section's summary: boot does by hand exactly what this chapter did by hand - assemble, unlock in the order of Section 13.1.1's layering - and the initramfs carries the tools and configuration to do it. Verifying that the tools and configuration are present is update-initramfs plus lsinitramfs, as above; verifying the full boot behavior requires booting the stack, and a rehearsal of the manual steps is the next section's subject. When the unlock itself fails at boot - a wrong passphrase, a missing key file, an array that will not assemble - the recovery path runs through the initramfs shell or Chapter 16 Section 16.2's rescue procedure, which unlocks a stack from recovery media.

13.5.2. Assembling Existing Arrays and Activating Volume Groups

Boot's steps can be rehearsed by hand on a running system, which is also the procedure for recovering a stack that came up only partly. Start from a known state:

# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid1 vdf[2] vdd[0]
      2094080 blocks super 1.2 [2/2] [UU]
      bitmap: 0/1 pages [0KB], 65536KB chunk

unused devices: <none>

md0 is healthy - the rebuilt array of Section 13.3.4 with /dev/vdf in the second slot - and still mounted at /srv/raid. Unmount it so the array has no users, then stop it:

# umount /srv/raid
# mdadm --stop /dev/md0
mdadm: stopped /dev/md0

The array's data is on its members; stopping removes only the kernel device. Confirm the empty state:

# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10] 
unused devices: <none>

No arrays. This is what a boot sees before assembly - members with superblocks, no md device. Reassemble from the recorded configuration:

# mdadm --assemble --scan
mdadm: /dev/md0 has been started with 2 drives.

--scan reads the ARRAY line from /etc/mdadm/mdadm.conf, matches it against the members' superblocks, and builds the device under the recorded name - the same step the initramfs hook performs when root needs the array. Look at the result:

# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10] 
md0 : active (auto-read-only) raid1 vdd[0] vdf[2]
      2094080 blocks super 1.2 [2/2] [UU]
      bitmap: 0/1 pages [0KB], 65536KB chunk

unused devices: <none>

[2/2] [UU] - assembled from superblocks with no resync needed, because both members were in sync at the stop. The (auto-read-only) marking is normal for a freshly assembled array with no active users: the kernel keeps it read-only until the first write, avoiding pointless resync accounting. The volume group's activation is the LVM half of the same rehearsal - vgchange -ay, the command Section 13.2.4 demonstrated on all four of vgdata's volumes. No activation is needed here because LVM's device-discovery autoactivation already ran when the system came up; the group is present and active, as its report shows:

# vgs vgdata
  VG     #PV #LV #SN Attr   VSize VFree
  vgdata   2   4   0 wz--n- 3.99g 1.48g

Two physical volumes, four logical volumes, zero snapshots (#SN - the exhausted snapshot was removed in Section 13.2.4). If a group ever needs the manual form, it is vgchange -ay vgdata from Section 13.2.4, and the activation failure to check for first is the one in Section 13.5.3: a group whose physical volume has vanished.

With both rehearsals done, build the chapter's capstone: the complete stack of Figure 13.1 on the reassembled array - RAID at the bottom, LUKS above it, LVM above that, filesystem on top - the same composition a root filesystem would use, practiced on a data volume.

Warning: The following steps erase the practice filesystem on /dev/md0 (the ext4 mounted at /srv/raid in Section 13.3) and rebuild the device as the base of a new layered stack. Everything on it is practice data, which is why the array was chosen as the stack's foundation.

First erase the ext4 signature the earlier section left on the array device:

# wipefs -a /dev/md0
/dev/md0: 2 bytes were erased at offset 0x00000438 (ext4): 53 ef

Two bytes at offset 0x438 - ext4's superblock magic, the same signature wipefs has been finding since Chapter 11. Format the array itself as a LUKS container:

# cryptsetup luksFormat --type luks2 --label handbook-stack /dev/md0

The prompts are those of Section 13.4.2: the uppercase-YES confirmation that the target will be overwritten, then a passphrase twice. Open it under the name stackcrypt:

# cryptsetup open --type luks2 /dev/md0 stackcrypt

One passphrase prompt, then silence. From here up, the layers are the commands this chapter has already exercised, applied to the mapping. Make the decrypted device a physical volume:

# pvcreate /dev/mapper/stackcrypt
  Physical volume "/dev/mapper/stackcrypt" successfully created.

Claim it for a volume group:

# vgcreate vgstack /dev/mapper/stackcrypt
  Volume group "vgstack" successfully created

Create a logical volume in it:

# lvcreate -L 512M -n lvstack vgstack
  Logical volume "lvstack" created.

Filesystem, mount point, mount, ownership - the familiar silent steps:

# mkfs.ext4 -q -L handbook-stack /dev/vgstack/lvstack
# mkdir -p /srv/stack
# chown john:john /srv/stack
# mount /dev/vgstack/lvstack /srv/stack
# chown john:john /srv/stack

As john, in /srv/stack:

$ printf "%s\n" "layered payload" > readme.txt

Silent. Now the payoff - the whole chapter in one tree:

# lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT /dev/vdd
NAME                   SIZE TYPE  FSTYPE            MOUNTPOINT
vdd                      2G disk  linux_raid_member 
└─md0                    2G raid1 crypto_LUKS       
  └─stackcrypt           2G crypt LVM2_member       
    └─vgstack-lvstack  512M lvm   ext4              /srv/stack

This is Figure 13.1 measured: a member disk, the redundant array it feeds, the encrypted mapping of the array, the volume group's volume, and the filesystem - five layers, each a block device. (The listing shows one member; /dev/vdf carries the same md0 child.) The capacity accounting tells the same story from the top:

# vgs -o vg_name,vg_size,vg_free vgstack
  VG      VSize VFree
  vgstack 1.98g 1.48g
# df -h /srv/stack
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/vgstack-lvstack  488M  156K  452M   1% /srv/stack

2 GiB of members became a 2045 MiB array (md's reservation), then a mapping of about 2.0 GiB (the 16 MiB LUKS header), then a 1.98 GiB volume group (LVM metadata and extent rounding) - every layer takes its small bite, and the filesystem on top reports 488 MiB for a 512 MiB volume.

Close the section with the record Section 13.1.3 asked for: the identifier inventory of the complete stack, every layer in place:

# pvs -o pv_name,pv_uuid,vg_name
  PV                     PV UUID                                VG     
  /dev/mapper/stackcrypt jV5r8w-wnV3-LrK1-eTf0-9zfE-9pbR-M8TJxn vgstack
  /dev/vdb               XDOJX8-YsOK-POOE-cenS-lTWs-9CMB-j0gkdp vgdata 
  /dev/vdc               gMGxFP-B5AB-MXHS-ZiFp-8c4m-f16V-nAF1Jh vgdata 
# vgs -o vg_name,vg_uuid,vg_size
  VG      VG UUID                                VSize
  vgdata  Sn1wfF-2m0A-PdZ8-dlyC-bkfY-qV6m-lbrJCh 3.99g
  vgstack 3fq6HU-WfnI-Y1gZ-t68O-dVG1-lqHQ-cseUeb 1.98g
# lvs -o lv_name,vg_name,lv_uuid,lv_size
  LV       VG      LV UUID                                LSize  
  lvdata   vgdata  BBdEld-bRgB-Lxce-3u2A-SHwI-0Nws-l8FcM0   1.00g
  snapdemo vgdata  d2JsJH-Yp0d-1doG-lo82-iOi4-EUF6-lVe01k 512.00m
  thinpool vgdata  UW46Qi-6Qd9-DBpa-AcGU-acFe-0lsd-j7U2pU   1.00g
  thinvol  vgdata  eiQl72-VvUU-wmSn-e1Uj-KlGG-1zkO-mhWjbi 512.00m
  lvstack  vgstack IRTvG2-bH82-3avB-Wk0a-3s2R-0vsU-veDuqO 512.00m

The array's two identity fields are worth pulling out of the long detail listing; the grep -E filter selects just the lines that match its expression:

# mdadm --detail /dev/md0 | grep -E "Name|UUID"
              Name : layers:0  (local to host layers)
              UUID : 5d54ca37:e049e215:e4a14a99:c1fe7a0e
# cryptsetup luksUUID /dev/vdg
809b56a1-bde7-4dd2-b3d3-23b826853806
# blkid /dev/vgdata/lvdata /dev/vdg /dev/md0
/dev/vgdata/lvdata: LABEL="handbook-lvm" UUID="9a09920c-2aea-4ae5-9fae-882627e6f412" BLOCK_SIZE="4096" TYPE="ext4"
/dev/vdg: UUID="809b56a1-bde7-4dd2-b3d3-23b826853806" LABEL="handbook-crypt" TYPE="crypto_LUKS"
/dev/md0: UUID="3e420c2a-a7ca-463f-b5ca-01d050f64648" LABEL="handbook-stack" TYPE="crypto_LUKS"

The blkid excerpt shows the naming discipline that keeps the three UUIDs distinct: the LUKS container's UUID on /dev/vdg for crypttab, the inner filesystem's UUID (9a09920c-…, on lvdata) for fstab, and md0's own container UUID plus label - handbook-stack - identifying the second encrypted volume in the stack just built.

13.5.3. Degraded Arrays, Missing Volumes, and Lost Unlock Material

Each layer's failure modes have now been demonstrated in the section that built it. What remains is to collect them the way they arrive - as symptoms at boot or on a running system - and to name the decision each one forces.

Degraded arrays. The [U_] state of Section 13.3.4 is an operating condition, not an emergency: the array serves, the alert is the duration. Replace the member promptly, because the danger window - a second failure before the rebuild finishes - is when RAID0-grade data loss meets a RAID1 array. The member table in mdadm --detail and the bracket line in /proc/mdstat are the two-minute inspection that should follow any md event in the journal. An array that fails to appear at all is an assembly problem, not a member problem: check that the members' superblocks are intact with mdadm --examine (Section 13.3.2), that the ARRAY line in mdadm.conf matches, and that the initramfs was regenerated after the array's configuration last changed. An array assembled degraded at boot - --assemble --scan found fewer members - reports itself in both views exactly as the manual degradation did in Section 13.3.4.

Missing physical volumes. A volume group whose PV device is absent reports itself as partial: vgs and lvs mark the group and the affected volumes, and I/O to extents that lived on the lost device fails. The commands for this state are documented in the lvm2 manuals and are not demonstrated here: first establish that the device is really gone rather than merely detached (lsblk, the lower layers - an unopened LUKS mapping or an unassembled array makes its PV vanish exactly the same way), and only then decide about the data. vgreduce --removemissing vgdata removes the group's metadata references to lost PVs and restores normal operation, but only when no logical volume had extents on them; when partial LVs do reference the missing PVs, adding --force is the explicit consent to delete those LVs completely, including the parts that survived on other disks. LVM also archives every metadata change under /etc/lvm/archive, and vgcfgrestore can return a group's metadata to a recorded state after a destructive command - both are documented in the lvm2 manual pages. The prevention is structural: a PV on the md array of Section 13.5.2 tolerates a disk loss with no missing-PV state at all, because the RAID layer absorbs it first.

Lost unlock material. The three LUKS failures of Section 13.4.3 are the complete set: a wrong passphrase (exit status 2, the mapping simply not created), a destroyed or damaged header (Device … is not a valid LUKS device), and a healthy container with no surviving credential. The first costs nothing; retry with another keyslot's credential. The second and third are decided by whether the header backup exists and is current - luksHeaderRestore returns the keyslots to the backup's moment, which is why the backup must be taken early and refreshed whenever credentials change. With no backup and no credential, the data is unreachable by design; that is the property that makes the container's confidentiality real. At boot, a root container in this state stops the boot at the unlock prompt, and the recovery route is Chapter 16 Section 16.2's procedure from rescue media rather than anything applied to the running system.

Symptom Layer First check Demonstrated
md0 missing from /proc/mdstat md mdadm --examine on members; mdadm.conf ARRAY line; initramfs currency Section 13.5.2
[U_] degraded, (F) member md mdadm --detail; replace and rebuild Section 13.3.4
State : clean, resyncing/recovering md Nothing - watch the percentage, mdadm --wait Sections 13.3.1–13.3.4
Volume group or LVs absent LVM The layers below: mapping open? array assembled? Section 13.5.2
Group/volumes report partial LVM Confirm the PV is truly gone, not closed beneath This section (documented path)
Device … is not a valid LUKS device LUKS cryptsetup luksDump; restore the header backup Section 13.4.3
No key available with this passphrase LUKS Another keyslot's credential, or the header backup Section 13.4.3
Boot waits at a passphrase prompt LUKS (root) Enter the credential, or cryptroot-unlock from the initramfs shell Section 13.5.1
Emergency mode after adding an encrypted mount fstab/crypttab Did crypttab activate before fstab mounted? nofail present? Sections 13.4.4 and Chapter 7 Section 7.6.2

The fstab row is the one that connects back to Chapter 7 Section 7.6.2's emergency-mode diagnosis: an fstab entry whose source depends on a crypttab activation that failed is an fstab entry that fails, and nofail plus the journal's record of which unit failed is what turns a stuck boot into a diagnostic one.

13.5.4. Removing Layers in the Correct Order

Teardown is the layering rule inverted: whatever was built bottom-up comes apart top-down, because each layer holds a device reference to the one below it. Try it in the wrong order first, on purpose, to meet the guards. In the root shell, attempt to remove the physical volume while its volume group still uses it:

# pvremove /dev/mapper/stackcrypt
  PV /dev/mapper/stackcrypt is used by VG vgstack so please use vgreduce first.
  (If you are certain you need pvremove, then confirm by using --force twice.)
  /dev/mapper/stackcrypt: physical volume label not removed.

The refusal is explicit - and note that even its suggested vgreduce would only work on an empty group, which this one is not. Try closing the LUKS mapping with LVM still holding it:

# cryptsetup close stackcrypt
Device stackcrypt is still in use.

Both commands exit with status 5. cryptsetup(8) documents that code for a device that is busy or in use; LVM assigns no documented meanings to its exit codes. Nothing was destroyed by either attempt; the guards did their job.

Now the correct order, from the top. Each removal below destroys the practice data on that layer - that is its purpose.

Warning: lvremove, vgremove, and pvremove erase the volumes, groups, and physical volumes they name, together with everything stored on them. Everything removed from here onward is practice data.

Unmount the filesystem at the top of the stack:

# umount /srv/stack

Remove the logical volume:

# lvremove -f vgstack/lvstack
  Logical volume "lvstack" successfully removed.

Remove the volume group - empty now, its last volume gone:

# vgremove vgstack
  Volume group "vgstack" successfully removed

Remove the physical volume that fed it:

# pvremove /dev/mapper/stackcrypt
  Labels on physical volume "/dev/mapper/stackcrypt" successfully wiped.

Now nothing holds the mapping; close it:

# cryptsetup close stackcrypt

Silent. Now nothing holds the array; stop it:

# mdadm --stop /dev/md0
mdadm: stopped /dev/md0
# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10] 
unused devices: <none>

And wipe the members so the practice disks return to blank:

# wipefs -a /dev/vdd /dev/vde /dev/vdf
/dev/vdd: 4 bytes were erased at offset 0x00001000 (linux_raid_member): fc 4e 2b a9
/dev/vde: 4 bytes were erased at offset 0x00001000 (linux_raid_member): fc 4e 2b a9
/dev/vdf: 4 bytes were erased at offset 0x00001000 (linux_raid_member): fc 4e 2b a9

The vgdata group comes apart in the same order - its volumes first, and note in the output that LVM removes the thin volume before the pool that holds it:

# lvremove -f vgdata
  Logical volume "thinvol" successfully removed.
  Logical volume "thinpool" successfully removed.
  Logical volume "lvdata" successfully removed.
  Logical volume "snapdemo" successfully removed.
# vgremove vgdata
  Volume group "vgdata" successfully removed
# pvremove /dev/vdb /dev/vdc
  Labels on physical volume "/dev/vdb" successfully wiped.
  Labels on physical volume "/dev/vdc" successfully wiped.
# pvs

pvs with no physical volumes left prints nothing at all. Wipe the LUKS practice disk - and note the two signatures it erases:

# wipefs -a /dev/vdg
/dev/vdg: 6 bytes were erased at offset 0x00000000 (crypto_LUKS): 4c 55 4b 53 ba be
/dev/vdg: 6 bytes were erased at offset 0x00004000 (crypto_LUKS): 53 4b 55 4c ba be

Two magic locations: the primary header at offset 0 and the backup metadata area at offset 0x4000 - the LUKS and reversed SKUL signatures from Figure 13.6's header geometry.

Two practice credentials from Section 13.4.3 still sit in john's workspace, and both have now outlived their container. Remove them from the root shell - both files are root-owned:

# rm /home/john/debian-handbook/storage-layers/luks-key.bin /home/john/debian-handbook/storage-layers/luks-header.img

Silent; a real header backup is worth keeping somewhere the machine does not go with, but this one describes a container that no longer exists, and an orphaned key file is a credential without a purpose. Credentials should be retired as deliberately as they were created - luksKillSlot, covered in Section 13.4.3, is the in-place form when the container survives.

What remains is the configuration the stack left behind, and it matters as much as the devices: a system left with a crypttab entry whose container no longer exists, or an ARRAY line whose members are blank, is a system whose next boot spends effort looking for things that are not there. The fstab entry was already restored at the end of Section 13.4.4. Remove the cryptdata line from /etc/crypttab - the file holds that single line, so deleting the line empties the file - and remove the ARRAY /dev/md0 line from /etc/mdadm/mdadm.conf - Chapter 3 Section 3.4.2's editor procedure handles each in one edit. Then apply the standing rule of Section 13.1.3: regenerate the initramfs so its embedded copies of both files match:

# update-initramfs -u
update-initramfs: Generating /boot/initrd.img-6.12.86+deb13-amd64
W: No zstd in /usr/bin:/sbin:/bin, using gzip

The same environmental gzip notice as before - the image is rebuilt without the practice array configuration. The final state, checked the way the chapter began:

# lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT /dev/vdb /dev/vdc /dev/vdd /dev/vde /dev/vdf /dev/vdg
NAME SIZE TYPE FSTYPE MOUNTPOINT
vdb    2G disk        
vdc    2G disk        
vdd    2G disk        
vde    2G disk        
vdf    2G disk        
vdg    2G disk        
# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10] 
unused devices: <none>

Six blank disks, no arrays, no volume groups, no mappings - the practice environment as it started, with the stack that was built on it now knowledge rather than configuration.

The layers this chapter assembled do not end here. Chapter 14 grows and shrinks the same stack layer by layer - extending a volume and its filesystem, adding members to an array, growing a mapping - and migrates data between devices while it is live. Chapter 15 builds the property no layer of this chapter provides, using the snapshot consistency boundary of Section 13.2.4 and the header backup of Section 13.4.3 as inputs. Chapter 16 handles the failures Section 13.5.3 tabulated from the recovery side, unlocking and mounting a stack from rescue media. All of them assume the habit this chapter kept repeating: identify the device, record the identifier, and let each layer depend only on the one below it.