sudo mdadm /dev/md0 --fail /dev/sdX --remove /dev/sdXmark and remove the failed one
sudo mdadm /dev/md0 --add /dev/sdYadd a disk in its place
sudo mdadm --grow /dev/md0 --raid-devices=4grow the array
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.confsave the config
Overview
mdadm combines several disks into a RAID array (/dev/md0) using the
kernel's own facilities. Why: fault tolerance (RAID1/5/6) or speed (RAID0).
Levels, briefly: RAID0 — striping (fast, no redundancy), RAID1 — a
mirror, RAID5/6 — parity (survives 1/2 disk failures). On top of the
array sits a filesystem (filesystems) or LVM.
Recipes
3
Creating an array
bash
cat /proc/mdstat # status of every array (the main command!)
sudo mdadm --detail /dev/md0 # array details
lsblk # the device tree# a mirror of two disks:
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
# RAID5 of three disks + a hot spare:
sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sd{b,c,d} --spare-devices=1 /dev/sde
sudo mkfs.ext4 /dev/md0
# save the configuration (so it assembles at boot):
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u
Replacing a disk and growing the array
bash
cat /proc/mdstat # which disk dropped out ([_U] / [U_])
sudo mdadm /dev/md0 --fail /dev/sdc --remove /dev/sdc # mark and remove the failed one# physically replace it, then add the new one:
sudo mdadm /dev/md0 --add /dev/sdc
watch cat /proc/mdstat # watch the rebuild (recovery)# add a disk and grow the array (RAID5):
sudo mdadm --add /dev/md0 /dev/sdf
sudo mdadm --grow /dev/md0 --raid-devices=4
# then grow the filesystem on top (resize2fs/xfs_growfs, see filesystems)
sudo mdadm --stop /dev/md0 # stop it; --assemble --scan to reassemble
sudo mdadm --zero-superblock /dev/sdX # wipe RAID metadata from a disk (to reuse it)
Monitoring
bash
sudo mdadm --monitor --scan --daemonise # emails on failures# address: MAILADDR in /etc/mdadm/mdadm.conf (see postfix)
sudo mdadm --detail /dev/md0 | grep -E 'State|Failed|Active'
Troubleshooting
5
SymptomCause and fix
The array is degraded ([_U])
A disk dropped out. /proc/mdstat and mdadm --detail show which one.
Replace it and --add — the rebuild starts. Don't wait: on RAID1/5, a
second failure means data loss.
It doesn't assemble after a reboot
The config wasn't saved. mdadm --detail --scan >> /etc/mdadm/mdadm.conf +
update-initramfs -u. Assemble it manually: mdadm --assemble --scan.
The rebuild takes forever / is slow
Normal for large disks (hours). Speed is controlled by
/proc/sys/dev/raid/speed_limit_* (see sysctl). Don't power off during
a rebuild.
RAID is not a backup!
RAID protects against a disk failure, but not against deletion,
ransomware, or a mistake. Keep backups (restic/rsync/tar)
separately.
A disk from an old array won't let you recreate on it
Danger. Running mdadm --create on disks that already hold data or
another array overwrites the superblock and can destroy data without
warning — use --assemble for an existing array, and --create only on
disks you're sure about. mdadm --zero-superblock erases RAID metadata
permanently: only use it on disks that have already been removed from an
array and are no longer needed in that role.
Running mkfs on top of /dev/md0 wipes the array's filesystem just as
irreversibly as on a plain partition — RAID protects against a disk
failure, not against a command mistake.
See also
filesystems — Creating filesystems, labels and UUIDs, fsck checks, ext4/xfs.