13.3. Building MD Software RAID
The md driver builds arrays - single devices with redundancy - out of
ordinary disks or partitions. Where LVM pools space and LUKS protects
it, md keeps the array serving through hardware failure: Section
13.1.2's redundancy property. Debian also offers redundancy inside
filesystems (Chapter 12's Btrfs profiles) and
inside LVM itself (lvmraid(7) builds md-style arrays as logical
volumes); md at the bottom of the stack remains the most common
choice for a dedicated RAID layer, and it is the one exercised here.
13.3.1. Selecting RAID Levels and Planning Usable Capacity
A RAID level is a layout rule: how blocks are distributed, what is redundant, and how many members the array survives losing. The levels md provides:
| Level | Minimum members | Usable capacity | Survives | Notes |
|---|---|---|---|---|
| RAID0 | 2 | All members, added | Nothing | Striping for speed only; one failure loses the array |
| RAID1 | 2 | One member's size | One member | Mirror; simple, flexible, the usual choice for small arrays |
| RAID5 | 3 | Members minus one | One member | Striped parity; read-heavy workloads; writes pay the parity update |
| RAID6 | 4 | Members minus two | Two members | Double parity for large or slow-to-rebuild arrays |
| RAID10 | 2 | Members divided by the copy count - half, for the usual two | One member, for two copies | Striped mirrors; near, far, and offset layouts; md accepts any member count at or above the copy count, odd counts included |
Two measured rows from the practice system anchor the arithmetic: two
2 GiB members in RAID1 yield 2045 MiB of array capacity, and three
2 GiB members in RAID5 yield 3.99 GiB. In each case a member
contributes 2045 MiB rather than 2048 because md reserves a few
mebibytes at the start of each member for the superblock and data
offset - the reservation whose consequences Section 13.3.2's
--examine output shows directly. RAID1 then costs one member's copy
(2045 × 1), RAID5 one member's parity (2045 × 2). Plan usable
capacity from the smallest member: md sizes a member's contribution
by the smallest disk in the array.
Create the RAID5 shape once, purely to verify the capacity plan. In the root shell, wipe any residue from the three RAID practice disks:
# wipefs -a /dev/vdd /dev/vde /dev/vdf
Silent - the disks were already blank, so there was nothing to erase. Create the array on all three:
# mdadm --create /dev/md5 --level=5 --raid-devices=3 --bitmap=internal /dev/vdd /dev/vde /dev/vdf
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
--bitmap=internal enables a write-intent bitmap inside the array:
the kernel tracks which regions are mid-write, so a resync or rebuild
interrupted by a power loss resumes over the dirty regions only,
instead of re-copying the whole member. It costs some write overhead
and is worth having on arrays whose hosts may restart uncleanly.
mdadm defaulted to metadata version 1.2, the current format, and
started the array - which immediately begins making three raw disks
into a consistent parity set:
# mdadm --detail /dev/md5
/dev/md5:
Version : 1.2
Raid Level : raid5
Array Size : 4188160 (3.99 GiB 4.29 GB)
Used Dev Size : 2094080 (2045.00 MiB 2144.34 MB)
Raid Devices : 3
Total Devices : 3
State : clean, degraded, recovering
Rebuild Status : 6% complete
Number Major Minor RaidDevice State
0 254 48 0 active sync /dev/vdd
1 254 64 1 active sync /dev/vde
3 254 80 2 spare rebuilding /dev/vdf
Selected output; timestamps, persistence, bitmap, consistency policy, layout, device-count, and identity fields omitted.
The header confirms the plan - Array Size : 4188160 (3.99 GiB),
built from Used Dev Size 2045 MiB per member - and the member table
shows the third disk still spare rebuilding, its parity being
computed. /proc/mdstat shows the same thing in the kernel's shorter
vocabulary:
# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10]
md5 : active raid5 vdf[3] vde[1] vdd[0]
4188160 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [UU_]
[==>..................] recovery = 12.4% (261768/2094080) finish=0.4min speed=65442K/sec
bitmap: 1/1 pages [4KB], 65536KB chunk
unused devices: <none>
[3/2] [UU_] reads "three devices, two fully in sync, the third
catching up"; the personality list is the set of RAID personalities
the kernel has loaded; 512k chunk is the data stripe unit and
algorithm 2 the left-symmetric parity layout. The array is already
usable while this initial synchronization runs. Wait for it to finish:
# mdadm --wait /dev/md5
Silent - --wait blocks until the array settles, and returns when the
recovery is complete. Confirm:
# mdadm --detail /dev/md5
/dev/md5:
Version : 1.2
Raid Level : raid5
Array Size : 4188160 (3.99 GiB 4.29 GB)
Used Dev Size : 2094080 (2045.00 MiB 2144.34 MB)
Raid Devices : 3
Total Devices : 3
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Number Major Minor RaidDevice State
0 254 48 0 active sync /dev/vdd
1 254 64 1 active sync /dev/vde
3 254 80 2 active sync /dev/vdf
Selected output; timestamps, persistence, bitmap, consistency policy, layout, and identity fields omitted.
State : clean with all three members active sync - the capacity row
of the table above, measured. This check established the array's size
only; no filesystem or data was written to it. Retire it to return the
disks to the RAID1 exercise:
# mdadm --stop /dev/md5
mdadm: stopped /dev/md5
# 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
wipefs reports what it erased: the md magic, four bytes at offset
0x1000, on each member. The same job can be done with mdadm's own
--zero-superblock, which writes zeros over the whole member
superblock rather than just the identifying bytes; either is enough
for the members to be reusable.
13.3.2. Creating Arrays and Recording mdadm Configuration
The array this section keeps is RAID1 on two members - the small-array
default from the level table. The --create options, for reference:
| Option | Description |
|---|---|
--level N |
RAID level: 0, 1, 5, 6, 10, or linear/contiguous |
--raid-devices N |
Number of active members the layout requires |
--bitmap=internal |
Keep a write-intent bitmap inside the array (Section 13.3.1) |
--spare-devices N |
Add N extra members standing by as hot spares |
--metadata |
Superblock format; 1.2 is the default |
Create it on two of the three freed disks:
# mdadm --create /dev/md0 --level=1 --raid-devices=2 --bitmap=internal /dev/vdd /dev/vde
mdadm: Defaulting to version 1.2 metadata
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
Continue creating array [y/N]? y
mdadm: array /dev/md0 started.
The note is a genuine design question, not an error: with 1.2 metadata
the superblock sits at the start of each member, where only a loader
that understands md metadata can find the data behind it. GRUB in
Debian 13 can read RAID1 members with 1.x metadata, but the common and
robust layout keeps /boot or the EFI partition on a plain partition
outside the array - the arrangement Chapter 2
Section 2.3.3 used - so the warning is acknowledged with y and the
array starts. Like the RAID5 before it, a fresh RAID1 immediately
synchronizes both members; watch and wait:
# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Raid Level : raid1
Array Size : 2094080 (2045.00 MiB 2144.34 MB)
Raid Devices : 2
Total Devices : 2
State : clean, resyncing
Resync Status : 12% complete
UUID : 5d54ca37:e049e215:e4a14a99:c1fe7a0e
Number Major Minor RaidDevice State
0 254 48 0 active sync /dev/vdd
1 254 64 1 active sync /dev/vde
Selected output; timestamps, persistence, bitmap, consistency policy, member-size, device-count, and the remaining identity fields omitted - the UUID is the identifier Section 13.1.3 promised.
# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10]
md0 : active raid1 vde[1] vdd[0]
2094080 blocks super 1.2 [2/2] [UU]
[===>.................] resync = 18.7% (392832/2094080) finish=0.2min speed=130944K/sec
bitmap: 1/1 pages [4KB], 65536KB chunk
unused devices: <none>
Both members are already marked [UU] - the mirror is serving - and
the initial resync in the background copies member zero's contents
across so both copies are bit-identical. The speed shown belongs to
these small virtual disks; it is an observation, not a benchmark, and
resync time on real hardware scales with member size. Block until it
completes:
# mdadm --wait /dev/md0
Silent. The settled state:
# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Fri Nov 20 11:15:02 2026
Raid Level : raid1
Array Size : 2094080 (2045.00 MiB 2144.34 MB)
Used Dev Size : 2094080 (2045.00 MiB 2144.34 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Intent Bitmap : Internal
Update Time : Fri Nov 20 11:15:22 2026
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Consistency Policy : bitmap
Name : layers:0 (local to host layers)
UUID : 5d54ca37:e049e215:e4a14a99:c1fe7a0e
Events : 21
Number Major Minor RaidDevice State
0 254 48 0 active sync /dev/vdd
1 254 64 1 active sync /dev/vde
State : clean, both members active sync. The Name field records
layers:0 - mdadm prefixes the array's name with the creating host, so
the same array moved to another machine is still identifiable. The UUID
is the identifier the boot-time configuration needs next.
Put a filesystem and data on the array, the same way as every other volume so far, so that the failure exercise in Section 13.3.4 has something to protect. Each of these steps is silent:
# mkfs.ext4 -q -L handbook-raid /dev/md0
# mkdir -p /srv/raid
# chown john:john /srv/raid
# mount /dev/md0 /srv/raid
# chown john:john /srv/raid
As john, in /srv/raid:
$ printf "%s\n" "raid payload" > readme.txt
$ sha256sum readme.txt
b244fc96563b209992719d67b80f28a0fb9bf995cf93779a039a29c5aa247982 readme.txt
The checksum is the before-state that the rebuild will have to
reproduce exactly. Now record the array so the system can find it again
after a reboot. mdadm --detail --scan prints the configuration line
mdadm itself would write:
# mdadm --detail --scan
ARRAY /dev/md0 metadata=1.2 UUID=5d54ca37:e049e215:e4a14a99:c1fe7a0e
Append it to the configuration file, using a pipeline to both keep and
see what is written - tee -a prints its input and appends it to the
named file:
# mdadm --detail --scan | tee -a /etc/mdadm/mdadm.conf
ARRAY /dev/md0 metadata=1.2 UUID=5d54ca37:e049e215:e4a14a99:c1fe7a0e
Read back the tail of the file to confirm the line landed:
# tail -3 /etc/mdadm/mdadm.conf
# This configuration was auto-generated on Fri, 20 Nov 2026 08:17:35 +0000 by mkconf
ARRAY /dev/md0 metadata=1.2 UUID=5d54ca37:e049e215:e4a14a99:c1fe7a0e
The package did not ship this file as-is: its install script generates
the stock mdadm.conf, which ends with the auto-generated ... by
mkconf comment; the new ARRAY line follows it. This line is
what tells boot-time assembly - and the initramfs - that a device
called /dev/md0 with this UUID should be built from these members,
rather than leaving mdadm to guess. Regenerate the initramfs so the
line is available before the root filesystem exists:
# 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 warning is environmental, not a failure: the example system has no
zstd compressor installed, so initramfs-tools falls back to
compressing the image with gzip. Installing the zstd package would
switch the compression back; the choice affects image size and boot
unpack time, not function. Verify what the image now carries for the
array:
# lsinitramfs /boot/initrd.img-$(uname -r) | grep mdadm | head
etc/mdadm
etc/mdadm/mdadm.conf
etc/modprobe.d/mdadm.conf
scripts/local-block/mdadm
scripts/local-bottom/mdadm
usr/sbin/mdadm
The pipeline unpacks the image's file list, selects the mdadm-related
names, and head caps the listing at ten lines; the mdadm-related set
numbers six, and all six matter. etc/mdadm/mdadm.conf is the
configuration just recorded, copied in by the hook;
scripts/local-block/mdadm is the hook script
that runs at boot to assemble arrays; usr/sbin/mdadm is the tool
itself. The $(uname -r) substitution selects the image matching the
running kernel, as Chapter 7 Section 7.1.1 did for
its initramfs listing.
One more identifier is worth reading before moving on: the member
superblock, examined directly with --examine:
# mdadm --examine /dev/vdd
/dev/vdd:
Magic : a92b4efc
Feature Map : 0x1
Array UUID : 5d54ca37:e049e215:e4a14a99:c1fe7a0e
Name : layers:0 (local to host layers)
Raid Level : raid1
Raid Devices : 2
Avail Dev Size : 4188160 sectors (2045.00 MiB 2144.34 MB)
Array Size : 2094080 KiB (2045.00 MiB 2144.34 MB)
Data Offset : 6144 sectors
Super Offset : 8 sectors
Unused Space : before=6064 sectors, after=0 sectors
State : clean
Device UUID : c2ac19fa:01db1475:865e7d9a:192a0903
Internal Bitmap : 8 sectors from superblock
Bad Block Log : 512 entries available at offset 16 sectors
Checksum : 178fb387 - correct
Events : 21
Device Role : Active device 0
Array State : AA ('A' == active, '.' == missing, 'R' == replacing)
Selected output; the metadata version line and timestamps omitted.
Two UUIDs live on one member: the array UUID (the one in
mdadm.conf) and this member's own device UUID. The offsets explain
the capacity arithmetic from Section 13.3.1 - the superblock sits at
sector 8, data begins at sector 6144, and the space before it is the
reservation that makes a 2 GiB member contribute 2045 MiB. Array
State : AA is the member's own view of the mirror: both positions
active. This is also the command that answers "is this disk a member
of something?" when a stranger disk turns up, because the superblock
survives everything the filesystem on top does not.
13.3.3. Monitoring Array State and Synchronization
The two reading commands carry all the state vocabulary. /proc/mdstat
is the kernel's live view; mdadm --detail adds configuration and
identity. The words they use are stable across md:
State : clean- all members in sync, the array consistent.degraded- a member is missing or faulty; the array serves from fewer members than its layout requires.recovering- a replacement member is being rebuilt from the surviving members (also the word used when a fresh array builds its parity).resyncing- a full member-to-member copy, as after array creation.- The bracket notation
[2/2] [UU]counts working devices and shows each member's position:Uup,_missing,(F)marked faulty.
Synchronization runs in the background while the array serves; the
percentage and finish= estimates in /proc/mdstat progress toward
completion, and mdadm --wait blocks until they arrive. The
write-intent bitmap changes what happens after an interruption: with
it, only regions the bitmap marks dirty are recopied; without it, the
whole member is rebuilt. Neither the initial resync nor a rebuild can
be safely skipped - a mirror whose copies were never synchronized is
not redundant in any meaningful sense.
Monitoring in practice is watching for the states that should not
persist. degraded means a member has failed and the array is running
on its last copy - Section 13.3.4 makes that state and its repair
concrete. Beyond reading states
yourself, mdadm has a monitor mode - mdadm --monitor --scan - that
watches /proc/mdstat and reports events (a member failing, a rebuild
finishing, a spare activating) to a configured destination such as
email; mdadm(8) documents the event set and configuration. The kernel
also logs every md event to the journal
(Chapter 8 Section 8.4), so journalctl -k
after an incident reconstructs the sequence. For data integrity rather
than device health, the md driver supports a periodic consistency
check - the check action through the array's sysfs entry, described
in the kernel's md documentation - which on a mirror reads both copies
and reports mismatches, and on parity levels verifies that the parity
still matches the data; it is the only way to detect a block that has
silently corrupted on one member of a RAID1 while both devices report
healthy.
13.3.4. Replacing a Failed Member and Verifying Rebuilds
This is the exercise RAID exists for, run deliberately on the practice array: fail one member, keep working, replace it, and watch the rebuild restore full redundancy. The mdadm verbs that manage members:
| Action | Effect |
|---|---|
mdadm ARRAY --fail DEVICE |
Mark a member faulty; the array stops using it |
mdadm ARRAY --remove DEVICE |
Remove the faulty member from the array |
mdadm ARRAY --add DEVICE |
Add a device as a spare or replacement |
mdadm --wait ARRAY |
Block until any sync or rebuild completes |
The array is /dev/md0, healthy and mounted at /srv/raid. Fail the
second member on purpose:
# mdadm /dev/md0 --fail /dev/vde
Silent - mdadm prints nothing on a successful --fail. Verify what
changed:
# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Fri Nov 20 11:15:02 2026
Raid Level : raid1
Array Size : 2094080 (2045.00 MiB 2144.34 MB)
Used Dev Size : 2094080 (2045.00 MiB 2144.34 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Intent Bitmap : Internal
Update Time : Fri Nov 20 11:16:11 2026
State : clean, degraded
Active Devices : 1
Working Devices : 1
Failed Devices : 1
Spare Devices : 0
Consistency Policy : bitmap
Name : layers:0 (local to host layers)
UUID : 5d54ca37:e049e215:e4a14a99:c1fe7a0e
Events : 25
Number Major Minor RaidDevice State
0 254 48 0 active sync /dev/vdd
- 0 0 1 removed
1 254 64 - faulty /dev/vde
State : clean, degraded: the surviving data is consistent, but
redundancy is gone. The member table shows the details - device slot 1
is removed, and /dev/vde is listed below as faulty. The kernel's
view:
# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10]
md0 : active raid1 vde[1](F) vdd[0]
2094080 blocks super 1.2 [2/1] [U_]
bitmap: 1/1 pages [4KB], 65536KB chunk
unused devices: <none>
[2/1] [U_] - two-member array, one in sync, the other position
empty; vde[1](F) marks the failed member. The crucial property of
the degraded state is that the array keeps serving. As john, in
/srv/raid:
$ cat readme.txt
raid payload
Reads and writes continue against the surviving member. Replace the failed disk. First remove it from the array's member list:
# mdadm /dev/md0 --remove /dev/vde
mdadm: hot removed /dev/vde from /dev/md0
Then add the spare practice disk as its replacement:
# mdadm /dev/md0 --add /dev/vdf
mdadm: added /dev/vdf
The array immediately begins rebuilding the new member. Watch it start:
# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Raid Level : raid1
Array Size : 2094080 (2045.00 MiB 2144.34 MB)
Raid Devices : 2
Total Devices : 2
State : clean, degraded, recovering
Rebuild Status : 12% complete
UUID : 5d54ca37:e049e215:e4a14a99:c1fe7a0e
Number Major Minor RaidDevice State
0 254 48 0 active sync /dev/vdd
2 254 80 1 spare rebuilding /dev/vdf
Selected output; timestamps, persistence, bitmap, consistency policy, member-size, device-count, and the remaining identity fields omitted - the UUID is unchanged from Section 13.3.2's.
vdf occupies the empty slot as spare rebuilding, and the
Rebuild Status line counts its progress. The same in
/proc/mdstat's vocabulary:
# cat /proc/mdstat
Personalities : [raid1] [raid0] [raid6] [raid5] [raid4] [raid10]
md0 : active raid1 vdf[2] vdd[0]
2094080 blocks super 1.2 [2/1] [U_]
[=====>...............] recovery = 25.0% (523776/2094080) finish=0.2min speed=104755K/sec
bitmap: 1/1 pages [4KB], 65536KB chunk
unused devices: <none>
The array is still [2/1] [U_] - degraded and serving - while
recovery copies the surviving member onto the replacement. Wait for
the rebuild to finish, exactly as for the initial resync:
# mdadm --wait /dev/md0
Silent. The end 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>
[2/2] [UU]: redundancy restored, with the bitmap now empty because
no region is mid-write. The detail view confirms the members:
# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Raid Level : raid1
Array Size : 2094080 (2045.00 MiB 2144.34 MB)
Raid Devices : 2
Total Devices : 2
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Number Major Minor RaidDevice State
0 254 48 0 active sync /dev/vdd
2 254 80 1 active sync /dev/vdf
Selected output; timestamps, persistence, bitmap, consistency policy, member-size, and identity fields omitted.
Slot 1 now belongs to /dev/vdf, active sync. The last check is the
one that matters: did the data survive the round trip? As john, in
/srv/raid:
$ cat readme.txt
raid payload
$ sha256sum readme.txt
b244fc96563b209992719d67b80f28a0fb9bf995cf93779a039a29c5aa247982 readme.txt
The checksum matches the one recorded before the failure, byte for byte. The rebuild copied the surviving member's contents onto the new disk, and the file is unchanged.
Figure 13.5. The failure and rebuild cycle with its captured states. A degraded array keeps serving; the danger window is a second failure before the rebuild completes, which is why a spare member and prompt replacement matter. The speed figures are from the small practice array and illustrate progress, not performance.
One housekeeping note closes the cycle: /etc/mdadm/mdadm.conf needs
no change after a member replacement - the ARRAY line identifies the
array by UUID, not by member names, so the array reassembles from
whatever members carry its superblocks. That same property is what
makes --fail/--remove/--add the safe replacement order even on
larger arrays: the failed member is out of the set before the
replacement joins, and the identity that boot depends on never wavers.