Software RAID with mdadm
RAID levels as trade-offs, building an md array with mdadm, reading /proc/mdstat, scrubbing, and replacing a failed disk without losing the array.
Software RAID with mdadm
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- RAID spreads data over several disks so one can fail without data loss (RAID 1, 5, 6, 10) or so I/O is faster (RAID 0). It is not a backup.
- RAID 1 mirrors, RAID 5 adds one disk's worth of parity, RAID 6 two, RAID 10 stripes over mirrors. More redundancy costs capacity, and parity costs write speed.
- Linux software RAID is the md driver. mdadm creates, assembles, monitors and repairs arrays, which appear as /dev/md0 and so on.
- /proc/mdstat is the first place to look: each array, its members, [UU] or [U_] for healthy or degraded, and rebuild progress.
- Replacing a disk is fail, remove, physically swap, partition the new disk the same, add. The array rebuilds onto it while in use.
- A degraded array has no margin. Rebuilds stress the remaining disks, which is why RAID 6 is preferred over RAID 5 for large disks.
- Scrubs read every block to find bad sectors before a rebuild needs them. Debian schedules one on the first Sunday of each month with a systemd timer.
Disks die
Every disk eventually fails, and on a server with years of uptime, “eventually” is a scheduled event. When the only copy of the database is on that disk, the outage lasts as long as the restore from backup, if there is one.
RAID, a redundant array of independent disks, combines disks so that losing one does not lose data. The array keeps working, degraded, while you replace the disk. Linux does this in software with the md driver, managed by mdadm. No special controller is needed, and the array can move to any Linux machine.
Levels are trade-offs
| Level | Minimum disks | Survives | Usable space with n disks | Notes |
|---|---|---|---|---|
| RAID 0 | 2 | Nothing | n | Striping for speed. One failure loses everything |
| RAID 1 | 2 | All but one | 1 | Mirrors. Simple, fast reads |
| RAID 5 | 3 | One | n − 1 | Striping with one disk’s worth of parity |
| RAID 6 | 4 | Two | n − 2 | Two parity blocks per stripe |
| RAID 10 | 4 | One per mirror pair | n / 2 | Striped mirrors. Fast, rebuilds quickly |
Parity is how RAID 5 survives a loss with only one extra disk. The parity block is the XOR of the data blocks in a stripe, and XOR has a useful property: any one missing value can be recomputed from the others. I ran a toy version in Python on this Mac. It is my own code showing the arithmetic, not md:
disk1 b'LOGS' disk2 b'DBDB' disk3 b'WEB!' parity 5f484130
disk2 lost; disk1 ^ disk3 ^ parity = b'DBDB'
The cost is on writes: changing one block means reading and rewriting parity too. And during a rebuild every remaining disk is read end to end. With large disks that takes many hours, and a second failure or an unreadable sector during it loses a RAID 5 array. That is the usual argument for RAID 6 or RAID 10 on big disks.
RAID 5 and 6 also have a “write hole”: a crash between writing data and parity can leave a stripe inconsistent. The kernel’s md documentation lists two mitigations, a journal device for RAID 4/5/6 and, for RAID 5, the Partial Parity Log.
And whatever the level, RAID is not a backup. Delete a file and the deletion is written to every disk at once.
Building an array
From mdadm(8) (v4.6 page on man7.org). Not run here, since I have no Linux machine or spare disks:
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
cat /proc/mdstat # watch the initial sync
mkfs.ext4 /dev/md0
mdadm --detail --scan >> /etc/mdadm/mdadm.conf # Debian path; /etc/mdadm.conf on RHEL
update-initramfs -u # Debian: so the array assembles at boot
Use partitions of type “Linux RAID” rather than whole disks, so other tools recognise the disks as in use. On Debian, the package’s install script generates /etc/mdadm/mdadm.conf with its mkconf helper. Recording new arrays there keeps their names stable at boot.
Reading /proc/mdstat
/proc/mdstat lists each array with its level, its member devices, and a status like [2/2] [UU]: two devices expected, two working, both up. [2/1] [U_] means one is missing: degraded. During a rebuild it shows a progress bar, percentage, speed and an estimated finish. mdadm --detail /dev/md0 gives the full picture: state, each member’s role, which one is faulty.
Someone has to look. mdadm --monitor, which Debian runs as mdmonitor.service, sends mail or runs a program when an array degrades. Point its MAILADDR in mdadm.conf at an address that someone reads, or feed it to your alerting.
Replacing a failed disk
The steps, from mdadm(8):
mdadm --detail /dev/md0 # which member failed?
mdadm /dev/md0 --fail /dev/sdc1 # mark it failed, if md has not already
mdadm /dev/md0 --remove /dev/sdc1 # take it out; only failed or spare devices can be removed
# swap the disk, then copy the partition table from a healthy member:
sfdisk -d /dev/sdb | sfdisk /dev/sdc
mdadm /dev/md0 --add /dev/sdc1 # rebuild starts at once
watch cat /proc/mdstat
--add on a degraded array, per the man page, immediately starts rebuilding onto the new device. The array stays online the whole time. If the machine boots from the array, also reinstall the bootloader onto the new disk, or the next failure of the other disk leaves it unbootable. When there is time to plan, mdadm --replace with a spare already added copies onto the new disk before the old one is removed, so redundancy is never lost.
Scrubbing
A disk can hold a sector that has gone bad without anyone knowing, because nothing has read it in months. The rebuild after another disk fails is when you find out. A scrub prevents that: writing check to /sys/block/md0/md/sync_action makes md, per md(4), read every block on every device and check that copies or parity agree. A read error found then is repaired from redundancy.
Debian schedules this with systemd timers. From Debian 13’s mdadm 4.4 package, which I downloaded and unpacked on this Mac, mdcheck_start.timer:
[Timer]
OnCalendar=Sun *-*-1..7 1:00:00
RandomizedDelaySec=24h
Persistent=true
Sun *-*-1..7 matches a Sunday that falls on days 1 to 7, so the first Sunday of each month, using the calendar syntax from the timers lesson. The service runs mdcheck --duration "6 hours", and a daily mdcheck_continue.timer resumes long checks where they stopped.
About the video
The embed is all of BlueMonkey 4n6’s “Redundant Array of Independent Disks (RAID) - creating, formatting, and disaster recovery”, April 2022, 18 minutes. Its chapters cover RAID 0, 1, 5 and 10, creating partitions with fdisk, mdadm -C for a four-disk RAID 10, array status with /proc/mdstat and mdadm --detail, formatting, a simulated drive failure, replacement with mdadm -r and mdadm -a, and stopping the array. That is very close to this lesson’s practical path. It is a small digital-forensics channel. It does not cover RAID 6, scrubbing, monitoring or the bootloader on a replaced boot disk, which the lesson adds. English was judged from the channel and English auto-captions, the content from the chapters.
What was run: the XOR parity toy in Python, and downloading and reading Debian 13’s mdadm package and its timers, on macOS. Array behaviour is from mdadm(8), md(4) and the kernel md documentation, checked 2026-11-28.
Where this fits
RAID keeps data available when a disk dies. Encryption keeps it private when a disk leaves the building: disk encryption with LUKS.
Check yourself
/proc/mdstat shows md0 with [2/1] [U_]. What does that mean and what do you do?
The array expects two devices and only one is working, so it is degraded and has no redundancy. Find the failed member with mdadm --detail, replace it and add the new disk to start a rebuild.
Four 8 TB disks. How much usable space with RAID 5, RAID 6 and RAID 10?
RAID 5: 24 TB, one disk of parity. RAID 6: 16 TB, two disks of parity. RAID 10: 16 TB, half for mirrors.
Why is RAID not a backup?
It protects against a disk dying, not against deletion, corruption, ransomware or a fire. A mistaken rm is mirrored to every disk instantly.
What is a scrub, and why run one when nothing is wrong?
Writing check to md/sync_action makes md read every block and compare mirrors or parity. It finds unreadable sectors on disks that seem healthy, while there is still redundancy to repair them.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.