Server with hot-swap disk bays and one failed drive with a red LED

mdadm is the standard Linux tool for building and managing software RAID: it combines several physical disks into one logical device, watches their health and lets you replace a failed disk without losing data. Creating the array is the easy part. What makes the difference is setting up alerts, scheduling checks and having rehearsed a rebuild before you need one.

mdadm RAID key points in 20 seconds

  • mdadm handles RAID 0, 1, 5, 6 and 10 in software, with no dedicated controller.
  • Its arrays can be assembled on any other Linux machine.
  • Email alerts and scheduled scrubs are essential.
  • RAID is not a backup.

The main advantage of software RAID over a controller card is portability. When a hardware RAID controller dies, you often need an identical one to read the disks. With mdadm the metadata lives on the disks themselves, so the array can be assembled on any Linux box.

Choosing a level

  • RAID 0 (2+ disks): stripes data for speed with no redundancy. Lose one disk, lose everything. Scratch data only.
  • RAID 1 (2+): mirroring. Half the capacity, but simple and reliable. Common for the OS.
  • RAID 5 (3+): distributed data and parity; survives one disk failure with N-1 disks usable, at the cost of slower writes.
  • RAID 6 (4+): double parity; survives two failures and gives up two disks of capacity.
  • RAID 10: mirroring plus striping, good for databases and VMs, with half the capacity usable. In mdadm it is a native level, not two nested arrays.

With large disks, RAID 5 is increasingly questionable: a rebuild must read every surviving disk end to end, and a read error during that process can leave the array with no margin. For multi-terabyte volumes, RAID 6 or RAID 10 is the usual recommendation. Use disks with the same capacity, interface and technology, and avoid SMR drives, which handle sustained writes such as rebuilds poorly.

Creating the array and making it persist

Install mdadm with apt install mdadm or dnf install mdadm. A few creation examples, with device names you will need to adapt:

# RAID 1 with two disks
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

# RAID 5 with three disks and one spare
sudo mdadm --create /dev/md1 --level=5 --raid-devices=3 --spare-devices=1 /dev/sd[bcd] /dev/sde

# RAID 6 with four disks
sudo mdadm --create /dev/md2 --level=6 --raid-devices=4 /dev/sd[bcde]Code language: PHP (php)

To assemble the array at every boot, save its definition and rebuild the initramfs. The config path depends on the distro family: /etc/mdadm/mdadm.conf on Debian and Ubuntu, /etc/mdadm.conf on RHEL and derivatives. One common trap: sudo mdadm ... >> file fails for non-root users because the unprivileged shell does the redirection. Use tee instead:

sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u      # Debian/Ubuntu
sudo dracut --force           # RHEL/FedoraCode language: PHP (php)

Then format it (mkfs.ext4 /dev/md0) and add it to /etc/fstab by filesystem UUID with the nofail option, so a degraded array does not block boot.

Monitoring, scrubbing and replacing disks

The quick status is in cat /proc/mdstat: [UU] means both members are up and [U_] means one is missing. mdadm --detail /dev/md0 gives the full report. To avoid relying on manual checks, set an address on the MAILADDR line of mdadm.conf, enable your distribution’s monitoring service (mdmonitor on many of them) and test delivery with mdadm --monitor --scan --test --oneshot.

Scrubs read every block and verify parity so bad sectors surface before you need them. Start one by hand with echo check | sudo tee /sys/block/md0/md/sync_action. Debian and Ubuntu schedule them already: recent releases replaced the old cron job with systemd timers, where mdcheck_start.timer runs the check on the first Sunday of each month, with a random delay of up to 24 hours and a maximum run of 6 hours, and mdcheck_continue.timer resumes it daily until it finishes.

mdadm catches array failures, but S.M.A.R.T. warns before a disk dies. smartctl -H and smartctl -A show health and attributes; if Reallocated_Sector_Ct, Current_Pending_Sector or Offline_Uncorrectable keep growing, replace the disk now. The smartd daemon can run scheduled self-tests and send email.

When a disk fails, mark it, remove it and add the replacement; mdadm rebuilds in the background:

sudo mdadm --manage /dev/md0 --fail /dev/sdc
sudo mdadm --manage /dev/md0 --remove /dev/sdc
sudo mdadm --manage /dev/md0 --add /dev/sde

Before pulling anything, confirm which physical disk it is: smartctl -i shows the serial number and ls -l /dev/disk/by-id/ maps names to serials. Pulling the healthy disk out of a degraded RAID 5 is the fastest way to lose the lot.

The write hole and other common mistakes

A power cut mid-write can leave RAID 5 parity inconsistent with the data; if the array later runs degraded, reconstruction can silently produce corrupt data. This is the write hole. The kernel offers two fixes: a dedicated journal device or the Partial Parity Log (PPL), enabled with --consistency-policy=ppl. According to the kernel documentation, PPL needs no separate disk but cuts write performance by up to 30-40% and cannot be combined with a write-intent bitmap. That bitmap, incidentally, does not close the write hole: it speeds up resync after an unclean shutdown. A UPS remains the first line of defence.

Other frequent mistakes:

  • Treating RAID as a backup: it does not protect against deletion, ransomware or logical corruption. The 3-2-1 rule still applies.
  • Buying every disk from the same batch, risking near-simultaneous failures.
  • Forgetting that /boot and the EFI partition must also exist on every disk for the system to boot after a failure.
  • Using motherboard fakeRAID, which depends on firmware and makes recovery harder.
  • Never rehearsing a rebuild.

The last one is easy to fix. In a throwaway VM you can practise with loop devices, without touching real disks:

for i in 0 1 2; do
  truncate -s 1G /tmp/disk$i.img
  sudo losetup /dev/loop$i /tmp/disk$i.img
done
sudo mdadm --create /dev/md9 --level=5 --raid-devices=3 /dev/loop[012]
sudo mdadm /dev/md9 --fail /dev/loop1
sudo mdadm /dev/md9 --remove /dev/loop1
sudo mdadm /dev/md9 --add /dev/loop1Code language: PHP (php)

Frequently asked questions

What is mdadm?

The Linux tool for creating, managing and monitoring software RAID arrays.

RAID 5 or RAID 6 with large disks?

With high-capacity disks, RAID 6 or RAID 10 is usually recommended, because a RAID 5 rebuild reads every disk and an error during it leaves the array unprotected.

How often is an mdadm array checked?

On Debian and Ubuntu, mdcheck_start.timer runs it on the first Sunday of each month. You can also start one by writing check to sync_action.

Does RAID replace backups?

No. It protects against disk failure, not deletion, ransomware or data corruption.

Sources:

Scroll to Top