Replacing a drive in an mdadm mirror
With the new disk installed, rebuilding a software mirror takes a handful of commands. The care goes into choosing the right device each time.
What does the starting point look like?
One healthy disk carrying every array on its own, and one new blank disk. In /proc/mdstat each array shows one member and the pattern [U_] or [_U]. Before touching anything, write down which device is the survivor and which is new, using serial numbers rather than names, because the kernel may have renamed devices after the swap.
cat /proc/mdstat lsblk -o NAME,SIZE,SERIAL,TYPE,MOUNTPOINTS mdadm --detail /dev/md2 | grep -E 'State|active sync|removed'
In this guide the survivor is /dev/nvme0n1 and the new disk is /dev/nvme1n1. On SATA machines read /dev/sda and /dev/sdb, with partitions named sda1 rather than nvme0n1p1.
Do I need to remove the old member first?
Only if the array still lists it. When a disk dies, mdadm usually marks it faulty; after a physical swap it often shows as removed already. If a faulty entry remains, remove it so the array has a free slot:
mdadm /dev/md2 --remove failed mdadm /dev/md2 --remove detached
The keywords failed and detached remove any member in that state, which avoids typing a device name that no longer exists.
How do I give the new disk the same partitions?
Copy the partition table from the survivor, then give the copy its own identifiers. For GPT, which every current install uses, sgdisk does both. The first argument is the destination, which is the step people get wrong; double-check before pressing Enter.
sgdisk /dev/nvme0n1 --replicate=/dev/nvme1n1 sgdisk --randomize-guids /dev/nvme1n1 partprobe /dev/nvme1n1 lsblk /dev/nvme1n1
On an old MBR layout, use sfdisk -d /dev/sda | sfdisk /dev/sdb instead. If the new disk is slightly larger than the old one, that is fine; the extra space stays unused. If it is smaller, the copy fails, and a ticket asking for an equal or larger drive is the answer.
How do I add the partitions back to the arrays?
One --add per array, matching partition numbers to arrays as on the survivor. Check the mapping with mdadm --detail on each array, then add.
mdadm --manage /dev/md1 --add /dev/nvme1n1p2 mdadm --manage /dev/md2 --add /dev/nvme1n1p3 cat /proc/mdstat
Recovery starts immediately. Smaller arrays finish first; the large root or data array takes longest. Swap partitions that are not part of an array need only mkswap and a matching entry in /etc/fstab, by UUID.
What about the EFI boot partition?
It is not part of any array, so copy it by hand. Firmware reads a plain FAT partition and cannot assemble RAID, which is why each disk carries its own. Format the new one and copy the files from the survivor's partition, then register a boot entry for it.
mkfs.vfat -F 32 /dev/nvme1n1p1 mkdir -p /mnt/esp2 && mount /dev/nvme1n1p1 /mnt/esp2 rsync -a /boot/efi/ /mnt/esp2/ efibootmgr --create --disk /dev/nvme1n1 --part 1 --label "linux-disk2" --loader '\EFI\debian\shimx64.efi' umount /mnt/esp2
The loader path depends on the distribution: \EFI\ubuntu\shimx64.efi on Ubuntu and \EFI\almalinux\shimx64.efi or \EFI\rocky\shimx64.efi on the Enterprise family. The UEFI boot on software RAID guide shows how to keep both copies in sync after kernel updates. On a legacy BIOS install, run grub-install /dev/sdb instead.
How do I follow and speed up the resync?
Watch /proc/mdstat; it shows percentage, speed and an estimated finish time. The kernel throttles rebuilds to keep the server responsive. If the machine is quiet and you want the rebuild done sooner, raise the minimum speed for the duration:
sysctl -w dev.raid.speed_limit_min=200000 watch -n 20 cat /proc/mdstat sysctl -w dev.raid.speed_limit_min=1000
NVMe mirrors of 960 GB finish in minutes. The large SAS arrays in the Storage line take many hours per drive, and the server stays usable throughout.
How do I confirm the job is done?
Every array shows [UU], mdadm --detail reports clean, and the firmware lists a boot entry for each disk. Save the array definitions so the next boot assembles them by name, rebuild the initramfs, and run a SMART check on the new disk to record its starting values.
mdadm --detail --scan efibootmgr -v | grep -i linux smartctl -H /dev/nvme1n1
If mdadm --detail --scan differs from /etc/mdadm/mdadm.conf (or /etc/mdadm.conf on Enterprise Linux), update the file before regenerating the initramfs.
Related pages
Get a failed disk replaced
How to confirm a drive is failing, collect the details technicians need, and what happens before and after the new disk goes in.
Read moreUEFI boot on software RAID
Make a mirrored server start from either drive: one EFI partition per disk, boot entries for both, and a hook that keeps the copies in sync.
Read moreSoftware RAID with mdadm
Read, create, monitor and tune Linux software RAID arrays with mdadm: status fields, email alerts, scrubs and the write-intent bitmap.
Read moreRAID levels explained
RAID 0, 1, 5, 6 and 10 compared with usable capacity worked out for our storage chassis.
Read moreQuestions and answers
Can the server keep running during the rebuild?
Yes. The resync runs in the background and the kernel limits its speed so services stay responsive.
What does --replicate do in sgdisk?
It copies the partition table of the disk named first onto the disk given after --replicate. Always run --randomize-guids on the copy afterwards.
Why is the EFI partition not mirrored by mdadm?
Firmware cannot read software RAID, so each disk carries its own plain FAT boot partition that you copy by hand.
How long does an NVMe mirror take to resync?
A 960 GB NVMe mirror usually finishes in 10 to 20 minutes. Large hard drives take many hours.
The new disk is larger than the old one. Is that a problem?
No. The copied partitions fit and the extra space is simply left unused.