Skip to content

Set up a Linux software RAID1 mirror with mdadm and replace a failed disk

Build an mdadm RAID1 mirror on Linux, make it assemble at every boot, monitor it by email, and replace a failed RAID disk — with the exact commands and the output you should see.

A two-disk mdadm mirror takes about five commands to create and about five more to survive a disk failure — but the ones people forget are the boring ones in the middle, and they are why arrays come back as /dev/md127 after a reboot or fail silently for months. This guide walks the whole lifecycle: partition two disks with the Linux RAID type, create the array with mdadm --create --level=1 --raid-devices=2, persist it into mdadm.conf and the initramfs so it assembles at boot, mount by filesystem UUID, wire up email alerts, then fail a member on purpose and rebuild it. Everything below assumes root on a plain Linux host, and every command shows you what a healthy result looks like.

What you’ll accomplish

  • A /dev/md0 RAID1 mirror across two whole disks, with an internal write-intent bitmap and a completed initial resync.
  • An array that assembles under the same name on every boot, because its UUID is recorded in mdadm.conf and baked into the initramfs.
  • A filesystem mounted from /etc/fstab by its own UUID, so device renaming never breaks the mount.
  • Email alerts on degradation, verified with a test message rather than assumed.
  • A rehearsed disk-replacement procedure: fail, remove, clone the partition table, add, watch the rebuild finish.

Prerequisites

  • Root or sudo on a Linux host with mdadm 4.1 or newer (mdadm --version), plus gdisk/sgdisk or parted.
  • Two disks of the same size that you are willing to erase completely. Examples use /dev/sda and /dev/sdb. Confirm with lsblk before you touch anything — device letters move between boots.
  • The two config-file paths differ by distribution: Debian and Ubuntu use /etc/mdadm/mdadm.conf; RHEL, AlmaLinux, and Rocky use /etc/mdadm.conf. So does the initramfs tool: update-initramfs -u on Debian family, dracut -f on RHEL family. Every step below gives both.
  • This guide builds a data mirror, not a mirrored root or /boot. Mirroring the boot device also involves bootloader installation on both disks and is deliberately out of scope.
  • Sizing note: mdadm mirrors partitions, not disks-as-sold. If the replacement disk is even slightly smaller than the original, --add refuses it. Leaving a small unpartitioned tail on each disk avoids that trap.

How it works

An md RAID1 array is not a filesystem feature and not a disk feature. It is a block device — /dev/md0 — that the kernel builds by writing every incoming block to both members and reading from whichever member is convenient. Each member carries a small md superblock (metadata version 1.2, stored 4 KB from the start of the partition) recording the array UUID, this device’s role in the array, and the state of the write-intent bitmap. That superblock is the source of truth: the array’s identity travels with the disks, not with /dev/sda and /dev/sdb.

The RAID1 stack from physical disks through partitions and md superblocks up to the mounted filesystem, with the boot-time assembly sequence

At boot, mdadm scans block devices for superblocks and assembles anything it finds. Without a config file it has no idea what name you wanted, so it picks a high, out-of-the-way number — that is where /dev/md127 comes from. Writing ARRAY /dev/md0 ... UUID=... into mdadm.conf and rebuilding the initramfs is what makes the name stable. Mounting by filesystem UUID rather than by /dev/md0 gives you a second layer of insulation from that whole problem.

The write-intent bitmap matters more than it looks. RAID1 has no parity, so it does not have the classic RAID5 write hole, but it has a cousin: after an unclean shutdown the two members may disagree about writes that were in flight, and md has no way to tell which copy is correct. Its only recourse is to resynchronize by copying one member over the other. Without a bitmap that means re-reading the entire array; with an internal bitmap, md knows which regions had writes outstanding and only resyncs those — minutes instead of hours. The cost is a small write amplification on the bitmap itself, which is why mdadm only enables it by default on larger arrays.

Step-by-step

1. Confirm which disks you are about to erase. Get the device names, sizes, and models, and make sure neither disk is mounted or in use:

lsblk -o NAME,SIZE,TYPE,MODEL,MOUNTPOINT
NAME   SIZE TYPE MODEL             MOUNTPOINT
sda    1.8T disk ST2000NM000A-2MP
sdb    1.8T disk ST2000NM000A-2MP

No child partitions and no mountpoints is what you want to see here.

2. Create one Linux RAID partition per disk. Type code fd00 is the GPT “Linux RAID” type. Partitioning rather than using the raw disk keeps other tools from mistaking the disk for empty, and makes it obvious to a future operator what the disk is for:

sgdisk --zap-all /dev/sda
sgdisk -n 1:0:0 -t 1:fd00 -c 1:"raid-data" /dev/sda
sgdisk --zap-all /dev/sdb
sgdisk -n 1:0:0 -t 1:fd00 -c 1:"raid-data" /dev/sdb
Creating new GPT entries in memory.
GPT data structures destroyed! ...
The operation has completed successfully.

The parted equivalent, if you prefer it, is parted -s /dev/sda mklabel gpt mkpart primary 0% 100% set 1 raid on. Confirm with lsblk that sda1 and sdb1 now exist.

3. Create the array. One command builds it, writes the superblocks, and starts the initial resync:

mdadm --create /dev/md0 --level=1 --raid-devices=2 --bitmap=internal /dev/sda1 /dev/sdb1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Recent mdadm already adds an internal bitmap for arrays above roughly 100 GB; --bitmap=internal makes that explicit and applies it to small arrays too. The metadata warning is expected and harmless for a data array.

4. Watch the initial resync. The array is usable immediately — the resync runs behind you — but redundancy is not real until it finishes:

cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdb1[1] sda1[0]
      1953382464 blocks super 1.2 [2/2] [UU]
      [==>..................]  resync = 12.7% (248901632/1953382464) finish=142.3min speed=199536K/sec
      bitmap: 15/15 pages [60KB], 65536KB chunk

[2/2] [UU] means both members are present and up. A [U_] or [_U] means one is missing.

5. Persist the array definition. Append the scan output to the distro’s config file — this is the step that prevents /dev/md127:

mdadm --detail --scan
ARRAY /dev/md0 metadata=1.2 name=web01:0 UUID=8f3a1c9e:4b2d77a0:1e5c0d33:9a6f2b81

On Debian or Ubuntu:

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

On RHEL, AlmaLinux, or Rocky:

mdadm --detail --scan >> /etc/mdadm.conf
dracut -f

Both initramfs commands are quiet on success. Run the append exactly once — a duplicated ARRAY line makes assembly ambiguous.

6. Put a filesystem on the array and mount it by UUID. Format /dev/md0, not the members:

mkfs.ext4 -L raid-data /dev/md0
mkdir -p /srv/data
blkid /dev/md0
/dev/md0: LABEL="raid-data" UUID="c41f2ba7-19d8-4a6c-9f10-7b2ed3c05a44" TYPE="ext4"

Add that UUID to /etc/fstab:

UUID=c41f2ba7-19d8-4a6c-9f10-7b2ed3c05a44  /srv/data  ext4  defaults  0  2

Then reload and mount:

systemctl daemon-reload
mount -a
findmnt /srv/data
TARGET     SOURCE     FSTYPE OPTIONS
/srv/data  /dev/md0   ext4   rw,relatime

7. Turn on monitoring and prove it works. mdadm --monitor is what actually notices a degraded array. Add a MAILADDR line to the same config file you edited in step 5:

MAILADDR ops@example.com

The monitor runs as a service — mdmonitor.service on current Debian, Ubuntu, and RHEL-family releases. Enable it and send a test alert:

systemctl enable --now mdmonitor.service
mdadm --monitor --scan --oneshot --test

--test generates a TestMessage for every array whether or not anything is wrong, so an empty inbox after this means your mail path is broken, not that your array is fine. Delivery needs a working local MTA; on a host with no mail agent, point PROGRAM at a script that calls your alerting system instead.

Validate it works

Reboot and confirm the name survived. This is the only honest test of steps 5 and 6:

reboot

After it comes back:

cat /proc/mdstat
findmnt /srv/data
md0 : active raid1 sda1[0] sdb1[1]
      1953382464 blocks super 1.2 [2/2] [UU]
      bitmap: 0/15 pages [0KB], 65536KB chunk

If you see md127 instead of md0, go back to step 5.

Check the array’s own view of itself.

mdadm --detail /dev/md0
        Version : 1.2
     Raid Level : raid1
   Raid Devices : 2
  Active Devices : 2
 Failed Devices : 0
          State : clean
  Intent Bitmap : Internal
           UUID : 8f3a1c9e:4b2d77a0:1e5c0d33:9a6f2b81

State : clean, Failed Devices : 0, and Intent Bitmap : Internal are the three lines worth reading.

Read a member’s superblock directly. --examine works on the partition even when the array is not assembled, which makes it the right tool during recovery:

mdadm --examine /dev/sda1
     Array UUID : 8f3a1c9e:4b2d77a0:1e5c0d33:9a6f2b81
    Raid Level : raid1
   Device Role : Active device 0
   Array State : AA ('A' == active, '.' == missing, 'R' == replacing)

Rehearse a disk failure. Do this once, on purpose, while nothing is broken — the first time you run these commands should not be at 3 a.m.

Four-stage lifecycle of a RAID1 member failure: healthy, degraded, rebuilding, and redundant again, with the mdstat state and commands for each stage

mdadm /dev/md0 --fail /dev/sdb1
mdadm /dev/md0 --remove /dev/sdb1
mdadm: set /dev/sdb1 faulty in /dev/md0
mdadm: hot removed /dev/sdb1 from /dev/md0

/proc/mdstat now shows [2/1] [U_], the mount is still up, and the monitor should have emailed you. Physically swap the disk at this point in a real failure, then give the replacement the same layout. The -R argument order is the reverse of cp — the target comes first, the source last:

sgdisk -R /dev/sdb /dev/sda
sgdisk -G /dev/sdb
sgdisk -p /dev/sdb

-G gives the copied partition table fresh GUIDs so the two disks are not twins, and -p prints the result so you can confirm before adding. Then add the member back:

mdadm /dev/md0 --add /dev/sdb1
watch cat /proc/mdstat
mdadm: added /dev/sdb1
md0 : active raid1 sdb1[2] sda1[0]
      1953382464 blocks super 1.2 [2/1] [U_]
      [======>..............]  recovery = 34.2% (668401920/1953382464) finish=94.1min speed=227450K/sec

When it reaches [2/2] [UU], you are redundant again. Nothing needed remounting — the filesystem never noticed.

Troubleshooting

  • The array comes up as /dev/md127. The kernel assembled it from superblocks alone because no ARRAY line named it. Verify the config file has exactly one line for the array (grep ARRAY /etc/mdadm/mdadm.conf), then rebuild the initramfs with update-initramfs -u or dracut -f and reboot. Editing the config without rebuilding the initramfs changes nothing, because the boot-time copy is the one that runs.
  • Rebuild crawls at a few MB/s. md throttles resync so it does not starve production I/O, using dev.raid.speed_limit_min (default 1000 KB/s) and dev.raid.speed_limit_max (default 200000 KB/s). Raise the floor while you rebuild with sysctl -w dev.raid.speed_limit_min=50000, and put it back afterward — a high floor during business hours will hurt application latency. If the array is slow even when idle, the disks themselves may be the constraint; diagnose disk I/O bottlenecks with iostat before blaming md.
  • --add fails with “device too small”. The replacement disk has fewer sectors than the original, which happens routinely between vendors at the same nominal capacity. There is no way around it other than a larger disk, or rebuilding the array smaller. Compare with blockdev --getsize64 /dev/sda1 and blockdev --getsize64 /dev/sdb1.
  • A disk still carries an old superblock. Reusing a disk from a previous array makes mdadm see a member that does not belong. Wipe the stale metadata with mdadm --zero-superblock /dev/sdb1 before adding it. If a whole array needs to go away, stop it first with mdadm --stop /dev/md0, then zero every member.
  • Alerts never arrive. Confirm mdmonitor.service is active with systemctl status mdmonitor.service, confirm MAILADDR is in the config file the monitor actually reads, and re-run mdadm --monitor --scan --oneshot --test. Most failures here are mail delivery, not mdadm. Also schedule a periodic consistency check — Debian ships /usr/share/mdadm/checkarray on a monthly cron, RHEL ships an equivalent timer — and watch /sys/block/md0/md/mismatch_cnt, which counts blocks where the two copies disagreed.

When this is the wrong approach

RAID1 buys you one thing: the machine keeps running when one disk dies. Be clear about everything it does not buy.

  • It is not a backup, and the distinction is not academic. Every write is copied to both members instantly, including rm -rf, a bad migration, and ransomware. You need point-in-time copies stored somewhere else. If you want consistent copies without stopping the application, LVM snapshots for consistent backups is the layer that gives you that.
  • It does not detect silent corruption. md has no per-block checksums, so if one member returns wrong-but-readable data it cannot tell which copy is right. mismatch_cnt tells you a disagreement exists, not which side is correct. If bit rot is in your threat model, use ZFS or Btrfs, which checksum every block and can repair from the good copy.
  • Two disks in one chassis share too much. Same power supply, same controller, same room, often the same manufacturing batch. A mirror does not survive a controller fault, a flood, or a fire. Cross-machine or cross-site replication is a different problem with different tools.
  • Hardware RAID and hosted volumes may already cover this. On a cloud provider’s replicated block storage, or behind a battery-backed hardware controller, adding md on top adds a layer to debug for little gain. Check what the platform already guarantees first.
  • Mirroring root or /boot is a bigger job. It needs the bootloader installed on both disks and firmware that will actually try the second one. Doing half of it produces a machine that survives a data disk failure and dies on a boot disk failure.

The authoritative references are the Linux RAID wiki’s RAID setup guide and the kernel’s own md driver documentation, which specifies the superblock formats and sysfs interfaces exactly.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.