13.1. Designing a Layered Storage Stack
Layered storage is designed before a single layer is created. The order of the layers, the separation of the properties they provide, and the identifiers each layer stamps into its metadata determine what the stack can do, what can be reconfigured later, and how it will put itself back together at boot.
13.1.1. Ordering Disks, RAID, Encryption, LVM, and Filesystems
Every layer in this chapter is a program that reads one block device and
publishes another. The md RAID driver is its own kernel subsystem: it
takes whole disks or partitions as members and publishes an array
device such as /dev/md0. LVM and LUKS both build on device-mapper
instead: the kernel's device-mapper framework maps requests to a
synthetic device (everything under /dev/mapper/) onto an underlying
one, and LVM and cryptsetup are the userspace tools that program those
mappings. To the filesystem on top, each synthetic device is
indistinguishable from a disk - the layering Chapter 11
sketched in Figure 11.1, now filled in.
Start by confirming the practice targets are the six blank disks the rest of the chapter expects. In the root shell on layers:
# 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
# lsblk -f /dev/vdb /dev/vdc /dev/vdd /dev/vde /dev/vdf /dev/vdg
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
vdb
vdc
vdd
vde
vdf
vdg
The first listing shows six 2 GiB whole disks (TYPE disk) with no
partitions; the second shows no filesystem signatures - the blank-slate
check from Chapter 11 Section 11.1.2, applied
to devices that will be overwritten repeatedly. Neither command writes
anything.
Figure 13.1. The canonical layer order, drawn bottom-up. The chain matches the combined stack built in Section 13.5.2. Every layer is itself a block device; the side notes record why each layer belongs in this position.
The order in Figure 13.1 - RAID at the bottom, LUKS above it, LVM above that, filesystem on top - is the arrangement to reach for by default, and each position has a reason:
- RAID sees raw disks. Members must be devices the md driver can read and write directly, and each member carries a superblock identifying the array. Putting redundancy below everything else means one unlock, one volume group, and one filesystem all ride on the surviving copy after a disk dies.
- LUKS above RAID means one container for the whole redundant set. A stolen member disk is meaningless without the key, and nothing above the container ever re-encrypts when volumes are resized or renamed. The alternative - LUKS below RAID, encrypting each member separately - protects a single stolen member too, but requires one unlock per member and one header backup per member, and the array then mirrors already-encrypted sectors.
- LVM above LUKS turns one decrypted device into a pool of resizable volumes. The price is worth knowing: every logical volume shares the container's single master key, so volumes are not separately encrypted. A workload that needs distinct keys per volume puts LVM below LUKS instead - one container per volume - and gives up the shared pool.
- The filesystem stays on top, where it is indifferent to what lies beneath: ext4 behaves the same on a partition, a logical volume, or a decrypted mapping.
Layers can be skipped in both directions. A laptop with one disk
carries LVM on LUKS with no RAID; a scratch array may skip encryption
and put LVM directly on md. Two placement rules still apply. First,
whatever the boot loader must read - the kernel and initramfs - cannot
sit inside the stack, because firmware and GRUB understand plain
partitions, not assembled arrays and locked containers; a separate
/boot or EFI partition outside the stack holds them, exactly as
Chapter 2 Section 2.3.3 arranged for the guided
encrypted layout. Second, cross-layer interactions need a conscious
decision: TRIM commands from the filesystem must be forwarded by every
layer to reach the SSD (Chapter 11 Section
11.5.1 introduced the idea; Section 13.4.4 covers the options), and the
I/O scheduler that matters is the one on the underlying member device,
since device-mapper and md stack their queues on top of it
(Chapter 9 Section 9.4.3).
13.1.2. Redundancy, Encryption, and Backup as Different Properties
RAID, LUKS, and backup answer three unrelated questions, and confusing them is the most expensive design mistake in storage. Redundancy keeps a service running while hardware dies. Encryption keeps data opaque without a key. Backup produces a second copy that survives mistakes. A property one layer provides is exactly what the others do not:
| Property | Protects against | Does not protect against | Provided by |
|---|---|---|---|
| Redundancy | One device failing while the system runs | Deletion, corruption, reformatting, theft of the whole machine, simultaneous multi-device failure | md RAID (Section 13.3), Btrfs profiles (Chapter 12), ZFS (Chapter 30) |
| Confidentiality | Reading the physical media without the key | A machine that is running with the volume unlocked, deletion, device failure, a lost key or damaged header | LUKS2 (Section 13.4) |
| Recoverability | Mistakes, corruption, site loss, ransomware, lost unlock material | The service being down while data is restored | Backups (Chapter 15) |
The mirror replicates every change, including the wrong ones: rm
executes on both members of a RAID1 in the same instant, and a
filesystem reformat destroys both copies together. Encryption protects
nothing once the container is unlocked and mounted - access then follows
ordinary file permissions (Chapter 5).
And the backup row is the one no other layer substitutes for:
Chapter 15 designs that copy, including the
parts this chapter's stack cannot protect by itself, such as the LUKS
header backup of Section 13.4.3.
Figure 13.2. The three properties and their limits. A mirrored, encrypted volume still needs a separate copy for the failure modes the mirror and the lock cannot touch.
13.1.3. Recording Identifiers and Boot Dependencies
Each layer stamps metadata on its devices and generates identifiers
that outlive disk names. /dev/vdb is a discovery-order name that udev
assigns; the same physical disk can appear as /dev/vdc after a
reboot reshuffles detection. LVM, md, LUKS, and the filesystem each
record a UUID instead - Chapter 11 Section
11.1.3 applied this to partitions and filesystems, and the layered
stack adds more layers of the same idea:
| Layer | Identifier | How to read it | What consumes it |
|---|---|---|---|
| Physical volume | PV UUID | pvs |
LVM metadata; volume group membership |
| Volume group | VG UUID | vgs |
LVM metadata on every member PV |
| Logical volume | LV UUID | lvs |
LVM metadata |
| md array | Array UUID, plus a host-prefixed name | mdadm --detail |
The ARRAY line in /etc/mdadm/mdadm.conf |
| LUKS2 container | Container UUID and label | cryptsetup luksUUID, blkid |
The UUID= source in /etc/crypttab |
| Filesystem | Filesystem UUID and label | blkid |
The UUID= source in /etc/fstab |
Record these as they are created - the sections that follow show each identifier at the moment its layer comes into being, and Section 13.5.2 captures the complete inventory once the full stack exists. Two habits make the record useful: keep it outside the machine it describes (a volume group's UUID does you no good if the only copy lived on the failed disk), and note which identifiers boot depends on (Chapter 15 Section 15.1.2 makes this part of backup selection).
Boot dependencies follow the layer order, and each layer has its own way of being found:
- The md array is assembled from its members' superblocks, and the
ARRAYline in/etc/mdadm/mdadm.conftells mdadm which array name and UUID to expect. Section 13.3.2 records it and rebuilds the initramfs so the same line is available before the root filesystem exists. - The LUKS container is opened from
/etc/crypttab. Entries needed for boot travel inside the initramfs image; entries for data volumes are processed by systemd's crypttab support once systemd starts (Section 13.5.1 separates the two paths). - LVM needs no configuration file: the volume group's metadata is
written on every member physical volume, and activation discovers it
by scanning.
vgchange -ayis the manual form of the automatic activation. - The filesystem mount comes last, in
/etc/fstab, referencing the layers above by UUID so the order of discovery does not matter.
One rule closes the loop: whenever a boot-critical layer changes - an
array created or renamed, a container added, a volume group moved -
regenerate the initramfs with update-initramfs -u
(Chapter 7 Section 7.4.1 established the command;
this chapter exercises it in Sections 13.3.2 and 13.5.4).