Software RAID groups several disks into a single volume that survives the failure of one of them. On Linux, the standard tool is mdadm: it creates redundant arrays, monitors them and lets you replace a disk online. This guide shows how to choose a RAID level, create an array with a spare disk, make it persistent and repair it. For intermediate to advanced administrators, on Debian, Ubuntu or RHEL. Commands tested on AlmaLinux 10 (mdadm 4.4).
What you will learn
- Choose the right RAID level (0, 1, 5, 6, 10)
- Create an
mdadmarray with a spare disk - Make it persistent at boot (mdadm.conf + initramfs)
- Monitor the array and replace a failed disk
Prerequisites
- A Linux server with
sudoand themdadmpackage installed - At least 2 free disks (or partitions) of the same size
- Know how to identify disks and manage partitions
Choose a RAID level
The level defines the trade-off between capacity, performance and fault tolerance. Choose it before creating the array, it is not easy to change afterwards.
| Level | Min. disks | Fault tolerance | Usable capacity | For what |
|---|---|---|---|---|
| RAID 0 | 2 | none | 100% | Pure performance, disposable data |
| RAID 1 | 2 | 1 disk | 50% | System disk, small critical volumes |
| RAID 5 | 3 | 1 disk | (n-1)/n | Good capacity / safety trade-off |
| RAID 6 | 4 | 2 disks | (n-2)/n | Large arrays (long rebuild) |
| RAID 10 | 4 | 1 per mirror | 50% | Performance and redundancy (databases) |
Create a RAID 5 array with a spare disk
The spare disk is idle until a failure, then takes over automatically. That is what turns redundancy into resilience with no intervention.
-
Create the array: 3 active disks (
--raid-devices=3) plus 1 spare (--spare-devices=1).Terminal window sudo mdadm --create /dev/md0 --level=5 \--raid-devices=3 /dev/sdb /dev/sdc /dev/sdd \--spare-devices=1 /dev/sdeThe command is interactive, which surprises the first time and blocks any script that ignores it. It asks a question about the write-intent bitmap (the typos are mdadm's own), then asks for confirmation:
To optimalize recovery speed, it is recommended to enable write-indentbitmap, do you want to enable it now? [y/N]?mdadm: Defaulting to version 1.2 metadatamdadm: array /dev/md0 started.With no answer (in a script, or with standard input closed),
mdadmgives up:mdadm: create aborted., exit code 1. To automate, answer in advance by passing the matching options:Terminal window sudo mdadm --create /dev/md0 --level=5 \--raid-devices=3 /dev/sdb /dev/sdc /dev/sdd \--spare-devices=1 /dev/sde \--bitmap=internal --run -
Follow the synchronisation in
/proc/mdstat.Terminal window cat /proc/mdstatmd0 : active raid5 sde[4](S) sdd[3] sdc[1] sdb[0]241664 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]The
(S)marks the spare. The[UUU]notation gives the state of each active disk (U= up,_= missing); here all 3 are healthy. -
Check the details of the array.
Terminal window sudo mdadm --detail /dev/md0Raid Level : raid5Array Size : 241664 (236.00 MiB 247.46 MB)State : cleanActive Devices : 3Working Devices : 4Spare Devices : 1
The /dev/md0 array is ready: 3 active disks and 1 spare (Working Devices : 4).
Format and mount
An mdadm array behaves like an ordinary disk. Format it and mount it:
sudo mkfs.xfs /dev/md0sudo mkdir -p /mnt/raidsudo mount /dev/md0 /mnt/raidFor a persistent mount, add it to /etc/fstab by UUID, with the nofail option so that a degraded array does not block the boot.
Make the array persistent
An array created in memory is not reassembled automatically after a reboot until it is declared in the configuration and included in the initramfs. It is the number one cause of a RAID that "disappears" after a reboot. The procedure differs by distribution.
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.confsudo update-initramfs -uOn RHEL, the mdadm package is not installed on a minimal image
(rpm -q mdadm answers package mdadm is not installed), and the
/etc/mdadm.conf file does not exist either: you have to create both.
sudo dnf install -y mdadmsudo mdadm --detail --scan | sudo tee -a /etc/mdadm.confsudo dracut --forceWhat dracut adds to the image is not your /etc/mdadm.conf: an
inspection with lsinitrd finds no trace of it. It is the mdraid
module, with the mdadm and mdmon binaries and their units, without which
the initramfs would not know how to assemble an array. The step is still
needed, but for that reason.
The generated line identifies the array by UUID, regardless of the order of the disks:
ARRAY /dev/md0 metadata=1.2 spares=1 UUID=76755a1f:40a789ed:c6b8d707:16526c13Monitor the RAID
An unmonitored RAID is dangerous: if a disk failure goes unnoticed, a second disk that gives up means total loss. The mdmonitor service sends an alert by mail at the slightest degradation.
Declare the notification address at the top of mdadm.conf, then enable the service (confirmed enabled on AlmaLinux):
MAILADDR root@localhostsudo systemctl enable --now mdmonitorYou can test the state at any time: mdadm --detail --test /dev/md0 returns 0 on a healthy array and 1 as soon as it is degraded, handy in a monitoring script.
Replace a failed disk
When a disk fails, the workflow is always the same: mark, remove, add. If a spare is present, the rebuild starts even before the disk is physically replaced.
-
Mark the disk as failed (simulated here with
--fail).Terminal window sudo mdadm /dev/md0 --fail /dev/sdbIn
/proc/mdstat, the disk switches to(F)and the spare takes over at once:md0 : active raid5 sde[4] sdd[3] sdc[1] sdb[0](F)241664 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [_UU][=>...................] recovery = 9.0% (7424/241664) finish=1.9min speed=1856K/secRead carefully:
[3/2]and[_UU], not[UUU]. Out of three expected disks, only two are active, and the_marks the missing one. The spare has been enlisted, but it still has to be rebuilt.mdadm --detailsays it plainly:State : clean, degraded, recoveringActive Devices : 2Working Devices : 3Failed Devices : 1Rebuild Status : 9% complete -
Remove the failed disk from the array.
Terminal window sudo mdadm /dev/md0 --remove /dev/sdb -
Add the replacement disk, which becomes the new spare.
Terminal window sudo mdadm /dev/md0 --add /dev/sdf
Follow the rebuild with watch -n1 cat /proc/mdstat: the recovery = XX% line gives the progress and the estimated end.
Redundancy is only restored when /proc/mdstat goes back to [3/3] [UUU] and mdadm --detail shows State : clean again without degraded.
RAID and LVM
To combine redundancy and flexibility, the recommended order is RAID below, LVM on top: mdadm creates /dev/md0, which becomes an LVM physical volume (LVM). You keep the resilience of RAID and the flexibility of logical volumes.
Alternative: LVM RAID (lvcreate --type raid5 ...) builds RAID directly into LVM, relying on the same kernel drivers as mdadm. Prefer mdadm for a pure RAID layer, proven (notably for the system disk); LVM RAID for a unified LVM stack.
Traps to avoid
RAID incidents rarely come from mdadm itself: they come from a wrong assumption about what RAID protects, or from a forgotten persistence step. The six traps below come up most often, and each one has already cost data to someone who thought they were protected.
- RAID 0 and critical data: no redundancy, to ban outside disposable caches.
- No monitoring: without
mdmonitor+MAILADDR, a failure stays invisible until disaster strikes. - Forgetting the initramfs: without
update-initramfs/dracutafter changingmdadm.conf, the array does not come back at boot. - RAID 5/6 write hole: after a sudden stop, parity can become inconsistent. To close it, create the array with a PPL consistency policy (
--consistency-policy=ppl, RAID 5) or a journal on SSD; a write-intent bitmap speeds up resync but does not close the hole. Beware, this bitmap is not set by default:mdadm --createoffers it through an interactive question, and anNor a closed standard input leaves it out. - Believing a spare makes the failure painless: during the whole rebuild, the array is degraded and a second failure is fatal in RAID 5.
- Confusing RAID and backup: see the warning above.
Troubleshooting
Two commands are enough for the diagnosis: cat /proc/mdstat gives the state of each disk at a glance, and mdadm --detail the state of the array and the progress of a rebuild. Always start from them before touching a disk.
| Symptom | Likely cause | Fix |
|---|---|---|
| Array missing after reboot | mdadm.conf or initramfs not updated | Regenerate the config + update-initramfs/dracut |
/proc/mdstat shows [U_U] | A failed disk | mdadm --detail then replace the disk |
| Slow rebuild | I/O load or kernel limit | cat /proc/mdstat (speed), wait, avoid writes |
mdadm: cannot open /dev/sdX: Device or resource busy | Disk member of an array still active | mdadm --stop (or --fail then --remove), then mdadm --zero-superblock /dev/sdX before reuse |
| No failure alert | mdmonitor inactive | systemctl enable --now mdmonitor + MAILADDR |
Put it into practice
Persistence is the classic weak point of an array: it works at creation, then disappears at the first reboot if mdadm.conf and the initramfs were forgotten. This lab has you build a RAID 1 with mdadm on a VM fitted with blank disks, and the validation checks that the array is still assembled after a reboot, not only that it was at creation.
Key points
- Choose the level first: RAID 1 (mirror), RAID 5 (trade-off), RAID 6 (double parity), RAID 10 (perf + redundancy).
- A spare (
--spare-devices) triggers an automatic rebuild at the first failure, but the array stays degraded ([_UU]) during the whole rebuild: a second failure there is fatal in RAID 5. /proc/mdstatandmdadm --detailare the two inspection tools:[UUU]= healthy,[_UU]= degraded,(S)= spare,(F)= failed.mdadm --createis interactive: two questions, andcreate aborted.with rc=1 if nobody answers. In a script,--bitmap=internal --run.- Making the array persistent =
mdadm.conf+ initramfs (update-initramfson Debian,dracut --forceon RHEL). The initramfs embeds themdraidmodule, not your configuration file. - Enable
mdmonitor: an unmonitored RAID is not resilient. - RAID is not a backup; RAID 0 protects nothing.
Knowledge check
Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Encrypt a disk with LUKS and cryptsetup
A new full quiz with random questions
Work again only on the questions you missed