Managing software RAID with mdadm

Linux software RAID protects most of our machines. Knowing how to read it and keep an eye on it takes ten minutes and saves data.

How do I read the state of my arrays?

Start with /proc/mdstat for an overview and mdadm --detail for one array. In the overview, each array lists its level, its members and a pattern such as [UU], where each U is a working member and an underscore marks a missing one.

cat /proc/mdstat
# md2 : active raid1 nvme1n1p3[1] nvme0n1p3[0]
#       937049088 blocks super 1.2 [2/2] [UU]
#       bitmap: 2/7 pages [8KB], 65536KB chunk
mdadm --detail /dev/md2

In the detail view, State : clean or active is healthy; degraded means a member is missing; recovering or resyncing shows progress of a rebuild. The table at the bottom lists each member with its role.

Why software RAID rather than a hardware controller?

Because it is portable, transparent and fast on modern processors. An mdadm array can be read by any Linux system, including the rescue system, with no special driver or vendor tool. NVMe drives connect straight to the processor, so there is no controller in the path to add latency. Parity calculations for RAID 5 and 6 use a small share of one core on current chips. Most mirrored models in our catalogue use it by default.

How do I create a new array?

Name the level, the number of devices and the members. The examples use partitions or whole spare drives; make sure they are empty, because creation overwrites them.

LevelCommandUsable space
RAID 1 mirrormdadm -C /dev/md5 -l 1 -n 2 /dev/sdc1 /dev/sdd1One drive
RAID 10mdadm -C /dev/md6 -l 10 -n 4 /dev/sd[c-f]Half of all drives
RAID 5mdadm -C /dev/md7 -l 5 -n 4 /dev/sd[c-f]All drives minus one
RAID 6mdadm -C /dev/md8 -l 6 -n 6 /dev/sd[c-h]All drives minus two

With very large hard drives, prefer RAID 6 or RAID 10 over RAID 5: rebuilding a 22 TB drive reads the others for many hours, and a second failure during that time would lose a RAID 5 array. After creating, record it so the system assembles it at boot:

mdadm --detail --scan | grep md8 >> /etc/mdadm/mdadm.conf
update-initramfs -u

How do I get warned when a disk drops out?

Run the mdadm monitor and give it a way to reach you. On Debian and Ubuntu the monitor service runs by default and mails root; on Enterprise Linux enable mdmonitor. Since a server rarely reads its own root mailbox, either set MAILADDR to a real address with a working send-only mail setup, or call a script that posts to your chat or monitoring tool.

# /etc/mdadm/mdadm.conf
MAILADDR ops@example.org
PROGRAM /usr/local/bin/raid-alert.sh
mdadm --monitor --scan --test --oneshot

The test command sends a test event for each array, which proves the alert path works. The Send email from your server guide sets up the mail side.

What is a RAID check and how often should it run?

A check reads every block of every member and compares them, catching unreadable sectors while the redundancy still exists to fix them. Debian and Ubuntu schedule one monthly through a timer or cron job; on other systems, start one yourself and schedule it.

echo check > /sys/block/md2/md/sync_action
cat /sys/block/md2/md/mismatch_cnt

A small mismatch count on a mirror that holds swap is normal. A growing count on a data array deserves a look at SMART values for each member.

What does the write-intent bitmap do?

It records which regions changed recently, so after a crash or a briefly missing member only those regions need resyncing rather than the whole drive. Arrays created by current installers include one. It costs a small amount of write speed; on large hard drive arrays it saves hours. Add one to an older array with mdadm --grow --bitmap=internal /dev/md8.

How do I tune rebuild speed?

Through two kernel limits in kilobytes per second per device: speed_limit_min, the speed guaranteed even under load, and speed_limit_max, the ceiling when idle. Raise the minimum when you want a degraded array safe again quickly and can accept slower service; lower it when customers come first. For RAID 5 and 6, a larger stripe_cache_size speeds up both rebuilds and sequential writes.

sysctl dev.raid.speed_limit_min dev.raid.speed_limit_max
echo 8192 > /sys/block/md8/md/stripe_cache_size

Where do I go when a disk fails?

To Get a failed disk replaced for getting the hardware swapped and Replace a disk in software RAID for rebuilding afterwards. If the server should boot from either disk of a mirror, UEFI boot on software RAID covers the boot partitions that sit outside the arrays.

Related pages

RAID levels explained

RAID 0, 1, 5, 6 and 10 compared with usable capacity worked out for our storage chassis.

Read more

Replace a disk in software RAID

The full command sequence to remove a dead member from an mdadm mirror, partition the new drive, rebuild and restore boot on both disks.

Read more

Plan disk partitions

Read the layout the installer created, decide where data, logs and swap belong, and change partitions safely on a running system or from rescue mode.

Read more

NVMe vs SATA SSD vs HDD

Throughput, latency and cost per terabyte of the three drive types in our machines, and which job suits each.

Read more

Questions and answers

What does [U_] mean in /proc/mdstat?

The array is missing one member. Each U is a working disk and the underscore marks the missing one. The array keeps running but has no redundancy left.

Is software RAID slower than hardware RAID?

Not on modern servers. NVMe drives connect directly to the processor and parity work takes a small share of one core.

Which RAID level suits large hard drives?

RAID 6 or RAID 10. Long rebuilds on large drives make RAID 5 risky because a second failure would lose the array.

How do I test RAID alerts?

Run mdadm --monitor --scan --test --oneshot. It sends a test event for each array through your configured mail address or program.

How often should a RAID check run?

Monthly is common. It reads every block and repairs unreadable sectors from the redundant copy.