Build RAID 1 and RAID 5 from loop devices, fail a member, and rebuild
Task
Build a RAID 1 mirror and a RAID 5 array with Linux software RAID, write data with a known checksum, fail one member of each, confirm the data survives in the degraded state, then replace the member and watch the rebuild. The point is to see the three states -- optimal, degraded, rebuilding -- and to prove the data is unchanged through all of them.
Steps
- Create five 200 MB files with
truncate -s 200M /srv/disk{1..5}.imgand attach each withlosetup -f --show, noting the loop device names. - Build a RAID 1 array from two loop devices with
mdadm --create /dev/md1 --level=1 --raid-devices=2and a RAID 5 array from the other three as/dev/md5. mdadm builds RAID 5 by recovering its last member, so wait until/proc/mdstatshows[UUU]with no recovery line, then save it tolab/raid/optimal.txt. - Create a file system on each with
mkfs.ext4, mount them, copy the same test file to both, and savesha256sumof each copy tolab/raid/before.txt. - Fail one member of each array with
mdadm --failandmdadm --remove. Savecat /proc/mdstattolab/raid/degraded.txtand the checksums of both copies tolab/raid/degraded-sums.txt. - Add a fresh loop device back to each array with
mdadm --add, wait for the rebuild to finish (watch/proc/mdstat), then save it tolab/raid/rebuilt.txtand the checksums tolab/raid/after.txt. - Record in
lab/raid/levels.csvwith headerlevel,disks,usable_fraction,failures_survivedthe figures for RAID 0, 1, 5, 6 and 10 with four disks each.
Verify
These checks run in a POSIX shell: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. A stock Windows PowerShell or Command Prompt has no awk or grep, so there the first line fails.
grep -c 'UU' lab/raid/optimal.txt
grep -Ec '_U|U_' lab/raid/degraded.txt
awk '{print $1}' lab/raid/before.txt | sort -u | wc -l
diff <(awk '{print $1}' lab/raid/before.txt) <(awk '{print $1}' lab/raid/after.txt) && echo checksums unchanged
awk -F, 'NR>1 {n++} END {print n" level(s) recorded"}' lab/raid/levels.csv
The optimal capture shows all members up, the degraded one shows an underscore for the missing member, and the rebuilt checksums match the originals. One distinct checksum in the before file means both copies were identical to start with. RAID 0 in your table must survive zero failures; if it says anything else, reread the RAID lesson before the capstone.
Notes
mdadm --detail /dev/md5 shows the same states with more words, including rebuild progress as a percentage. On real disks the rebuild takes hours, which is the window the storage failure lesson warns about.
This is an independent study companion for CompTIA Server+ SK0-005 and is not produced by or endorsed by CompTIA.