Linux Labs EspañolES

Storage: disks, LVM and RAID Labs

Build a RAID 1, fail a member and rebuild it

Advanced35 minYour VMRHEL and Debian families

Objective

md0 survives the loss of one member with its data intact, rebuilds onto a replacement device and ends in a clean [UU] state.

Requirements

  • A VM where you have sudo. The lab creates disk images under /var/tmp and attaches them as loop devices, so no spare disk is needed.
  • The mdadm package.

Setup

bash
for i in a b c; do sudo truncate -s 512M /var/tmp/lab-md-$i.img; done
for i in a b c; do sudo losetup -f --show /var/tmp/lab-md-$i.img; done   # assume /dev/loop0, loop1, loop2

Steps

  1. Create /dev/md0 as RAID 1 from the first two devices. Answer y if mdadm asks to continue.
  2. Wait for the initial resync, put XFS on md0, mount it at /srv/mirror and write a file.
  3. Fail one member, then remove it. Read the file while the array is degraded.
  4. Add the third device as the replacement and follow the rebuild until it is clean.

Verification

bash
cat /proc/mdstat
sudo mdadm --detail /dev/md0 | grep -E 'State :|Active Devices|Failed Devices|/dev/loop'
cat /srv/mirror/probe.txt
Show the solution
bash
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/loop0 /dev/loop1
watch -n1 cat /proc/mdstat           # Ctrl-c when the resync reaches 100%
sudo mkfs.xfs /dev/md0
sudo mkdir -p /srv/mirror && sudo mount /dev/md0 /srv/mirror
echo mirrored | sudo tee /srv/mirror/probe.txt
sudo mdadm --manage /dev/md0 --fail /dev/loop1
cat /proc/mdstat                     # [U_] and loop1[1](F)
sudo mdadm --manage /dev/md0 --remove /dev/loop1
cat /srv/mirror/probe.txt            # still readable
sudo mdadm --manage /dev/md0 --add /dev/loop2
watch -n1 cat /proc/mdstat           # recovery progress, then [UU]

On a real server you would also persist the array with mdadm --detail --scan into mdadm.conf, rebuild the initramfs and make sure mdmonitor or another alert is running.

Clean up:

bash
sudo umount /srv/mirror
sudo mdadm --stop /dev/md0
sudo mdadm --zero-superblock /dev/loop0 /dev/loop1 /dev/loop2
for d in /dev/loop0 /dev/loop1 /dev/loop2; do sudo losetup -d $d; done
sudo rm /var/tmp/lab-md-*.img