Storage Fundamentals Series

💾 A Top 1% Hands-On for Building a Linux Software RAID1 Array With mdadm and Reproducing a Disk Failure and Rebuild Yourself

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the fourth article in the Storage Fundamentals Series. You can follow along with one of the Ubuntu Server environments set up in the Hands-On Prep Manual.

Prerequisite Knowledge

Getting the Big Picture

This hands-on covers four steps.

graph LR
    Step1["Step 1<br/>Set up 2 virtual disks and<br/>build a RAID1 array"]
    Step2["Step 2<br/>Deliberately mark<br/>one disk as failed"]
    Step3["Step 3<br/>Confirm reads/writes<br/>still work, degraded"]
    Step4["Step4<br/>Add a new disk and<br/>confirm the rebuild"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Set Up Two Virtual Disks and Build a RAID1 Array

Instead of using real physical disks, treat files as loopback devices to set up two virtual disks.

sudo apt install -y mdadm
sudo dd if=/dev/zero of=/disk1.img bs=1M count=100
sudo dd if=/dev/zero of=/disk2.img bs=1M count=100
sudo losetup /dev/loop1 /disk1.img
sudo losetup /dev/loop2 /disk2.img

Create a RAID1 array across your two virtual disks (/dev/loop1 and /dev/loop2).

sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/loop1 /dev/loop2

Check the array’s state.

sudo mdadm --detail /dev/md0

Output (relevant part):

State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0

You’ve now built an array with Active Devices: 2 — both disks running normally. Create a filesystem and mount it.

sudo mkfs.ext4 /dev/md0
sudo mkdir -p /mnt/raid1
sudo mount /dev/md0 /mnt/raid1
echo "important data" | sudo tee /mnt/raid1/data.txt

Step 2: Deliberately Mark One Disk as Failed

Instead of waiting for a disk to actually fail, use mdadm’s own feature to deliberately mark one disk as “failed.”

sudo mdadm --manage /dev/md0 --fail /dev/loop2
sudo mdadm --detail /dev/md0

Output (relevant part):

State : clean, degraded
Active Devices : 1
Working Devices : 1
Failed Devices : 1

You confirmed the array’s state changed from clean to clean, degraded, with Active Devices dropping from 2 to 1.

Step 3: Confirm Reads/Writes Still Work in the Degraded State

Confirm the service keeps serving reads and writes without interruption, even in this one-disk-down, degraded state.

cat /mnt/raid1/data.txt
echo "still working after disk failure" | sudo tee -a /mnt/raid1/data.txt
cat /mnt/raid1/data.txt

Output:

important data
important data
still working after disk failure

You confirmed reads and writes continue without issue, on the single remaining disk, even after one disk got marked as failed. This is RAID1’s biggest value — the real-world effect of redundancy. But this degraded state is also a dangerous one: if the one remaining disk fails too, you lose all your data. You can feel firsthand, right here, exactly why a degraded state should never be left unattended indefinitely.

Step 4: Add a New Disk and Confirm the Rebuild

Set up a new virtual disk and add it to the array.

sudo mdadm --manage /dev/md0 --remove /dev/loop2
sudo dd if=/dev/zero of=/disk3.img bs=1M count=100
sudo losetup /dev/loop3 /disk3.img
sudo mdadm --manage /dev/md0 --add /dev/loop3

Check the rebuild’s progress.

cat /proc/mdstat

Output (relevant part):

md0 : active raid1 loop3[2] loop1[0]
      recovery = 45.2% (...) finish=0.1min speed=...

You confirmed concrete progress — recovery = 45.2% — showing the rebuild in motion. Once the rebuild finishes, mdadm --detail /dev/md0 confirms it’s back to Active Devices: 2 and State: clean.

Why Does a Rebuild Take So Long?

A rebuild is the process of copying the entire content of the remaining healthy disk, start to finish, onto the newly added disk. It’s not a differential copy — it covers the whole disk — so the larger the disk’s capacity, the longer the rebuild takes. And while the rebuild is running, the one remaining healthy disk carries both its normal read/write load and the extra load of being copied from. If that disk also fails during this window, you lose your data. For configurations using more disks than RAID1 (mirroring), like RAID5 or RAID6, this exact risk — a second failure during rebuild — is why a design like RAID6, which tolerates up to two simultaneous failures, sometimes gets chosen instead.

What a Pro Sees Here (Top 1% Understanding)

“Having RAID” and “Noticing a Failure” Are Two Entirely Different Things

In this hands-on, you noticed the failure because you deliberately marked the disk as failed in Step 2. But in real-world operations, incidents repeat where an array sits in a degraded state, unnoticed by anyone, for a very long time. A RAID configuration like RAID1 or RAID5 genuinely keeps the service running through one failure — but without a mechanism (monitoring and alerting) to detect that degraded state and notify someone, “it’s supposed to be redundant, but is actually defenseless” can persist, completely unnoticed. The single most important lesson from this hands-on is: whenever you build RAID, always build a regular status check or automated alerting alongside it.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. You don’t know how to check an array’s state: cat /proc/mdstat gives a quick status, while mdadm --detail /dev/mdX gives full detail.
  2. You replaced a disk, but it never gets added to the array: You need to explicitly add it with mdadm --manage /dev/mdX --add /dev/sdX — it’s never added automatically.
  3. The rebuild is abnormally slow: Check the speed value in /proc/mdstat. High disk I/O load elsewhere slows down the rebuild.

Summary

Takeaways to Apply Today

  1. Whenever you build RAID, build the habit of always setting up status monitoring and alerting alongside it.
  2. Rather than “I have RAID, so I’m fine,” keep in mind that RAID and backups serve entirely separate purposes, as separate defenses.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).