q41-raid-mdadm
Énoncé§
Solve this question on: terminal
- Build a RAID-1 array
/dev/md0from/dev/vddand/dev/vde. - Format it with
xfsand mount it persistently at/mnt/raid1. - Simulate a failure of
/dev/vdd, then replace it with/dev/vdf. - Write the contents of
/proc/mdstatinto/opt/course/41/mdstat.
Solution§
RAID levels worth knowing§
| Level | Min disks | Capacity | Fault tolerance | Notes |
|---|---|---|---|---|
| 0 | 2 | N × disk | none | stripe, fastest, risky |
| 1 | 2 | 1 × disk | N-1 | mirror |
| 5 | 3 | (N-1) × disk | 1 | parity striped |
| 6 | 4 | (N-2) × disk | 2 | double parity |
| 10 | 4 | N/2 × disk | depends | mirror of stripes |
Install§
sudo apt install mdadm
sudo dnf install mdadm
Step 1 — Create the array§
Wipe any old signatures first (otherwise mdadm refuses):
sudo wipefs -a /dev/vdd /dev/vde
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/vdd /dev/vde
# answer 'y' to the prompt
Watch the initial sync:
cat /proc/mdstat
sudo mdadm --detail /dev/md0
Save the array definition (so it reassembles after reboot):
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf # Debian
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm.conf # RHEL
sudo update-initramfs -u # Debian
sudo dracut -f # RHEL
Step 2 — Filesystem and mount§
sudo mkfs.xfs /dev/md0
sudo mkdir -p /mnt/raid1
sudo blkid /dev/md0
# UUID=xxxxx-xxxx ... TYPE="xfs"
echo 'UUID=xxxxx-xxxx /mnt/raid1 xfs defaults 0 2' | sudo tee -a /etc/fstab
sudo mount -a
Step 3 — Fail and replace a disk§
Mark /dev/vdd as failed:
sudo mdadm --manage /dev/md0 --fail /dev/vdd
sudo mdadm --detail /dev/md0 # state: degraded
Remove it from the array:
sudo mdadm --manage /dev/md0 --remove /dev/vdd
Add the replacement disk:
sudo wipefs -a /dev/vdf
sudo mdadm --manage /dev/md0 --add /dev/vdf
Watch the resync:
watch -n 1 cat /proc/mdstat
sudo mdadm --detail /dev/md0
Update the conf if the device list changed:
sudo sed -i '/^ARRAY \/dev\/md0/d' /etc/mdadm/mdadm.conf
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
Step 4 — Snapshot of state§
cat /proc/mdstat > /opt/course/41/mdstat
Spare disks and hot spares§
Create with a hot spare:
sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-devices=1 \
/dev/vdd /dev/vde /dev/vdf /dev/vdg
Convert a member to spare or back:
sudo mdadm --manage /dev/md0 --add-spare /dev/vdg
Grow an array (add a disk to a RAID 5)§
sudo mdadm --add /dev/md0 /dev/vdh
sudo mdadm --grow /dev/md0 --raid-devices=4
sudo xfs_growfs /mnt/raid1
Stop and reassemble§
sudo umount /mnt/raid1
sudo mdadm --stop /dev/md0
sudo mdadm --assemble /dev/md0 /dev/vd[ef]
sudo mdadm --assemble --scan # auto-discover from mdadm.conf
Monitor + email alerts§
sudo mdadm --monitor --daemonise --mail root@localhost --delay=300 /dev/md0
# or via systemd unit mdmonitor.service (already shipped)
Check / repair (data scrub)§
echo check | sudo tee /sys/block/md0/md/sync_action
echo repair | sudo tee /sys/block/md0/md/sync_action
cat /sys/block/md0/md/mismatch_cnt—The Gardener