How to Set Up Software RAID with Mdadm on Ubuntu Server Including Disk Failure Simulation and Recovery

Learning how to set up software RAID with mdadm on Ubuntu Server including disk failure simulation and recovery is one of the most valuable skills a Linux administrator can have. RAID (Redundant Array of Independent Disks) protects your data by spreading or mirroring it across multiple drives. When one disk fails, your server keeps running. This tutorial walks you through creating a RAID 1 mirror array using mdadm, the standard Linux software RAID tool. You’ll install mdadm, prepare your disks, build the array, and then deliberately simulate a drive failure. After that, you’ll learn how to remove the failed disk and add a replacement. By the end, you’ll have hands-on experience with the full RAID lifecycle on Ubuntu Server.

Prerequisites and Requirements for Software RAID with Mdadm

Before starting, make sure you have the following in place.

System requirements:

  • Ubuntu Server 20.04 or 22.04 LTS
  • Root or sudo access
  • At least two spare block devices (physical disks, partitions, or virtual disks)
  • Basic familiarity with the Linux command line

For this tutorial, the two spare disks are /dev/sdb and /dev/sdc. Your disk names may differ. Always verify your disk names with lsblk before running any commands. Using the wrong disk name can destroy data on the wrong drive.

Estimated time: 30–45 minutes.

You don’t need any prior RAID experience. This guide explains each command and why you’re running it. For deeper background on Linux storage concepts, the Ubuntu Community RAID documentation is a solid reference.

How to Set Up Software RAID with Mdadm on Ubuntu Server

For a related walkthrough, see: How to Automate Server Configuration with Ansible Playbooks on Ubuntu

Follow these steps carefully. Each step builds on the previous one.

Step 1: Update your system and install mdadm

Start by refreshing your package list and installing mdadm.

sudo apt update
sudo apt install mdadm -y

Mdadm is the command-line tool that manages Linux software RAID arrays. It handles everything from creation to monitoring and repair.

Step 2: Verify your disks

Confirm the disks you plan to use.

lsblk

Look for your two spare disks in the output. They should show no mounted partitions. If they have existing partitions, wipe them first with sudo wipefs -a /dev/sdb and repeat for /dev/sdc.

Step 3: Create the RAID 1 array

RAID 1 mirrors data across both disks. If one fails, the other has a complete copy.

sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

The flag --level=1 sets RAID 1 mirroring. The flag --raid-devices=2 tells mdadm you’re using two drives. The array will be available at /dev/md0.

Type y when prompted to confirm. The array starts building immediately. You can check progress with:

cat /proc/mdstat

Step 4: Create a filesystem and mount the array

Once the array is ready, format it and mount it.

sudo mkfs.ext4 /dev/md0
sudo mkdir -p /mnt/raid1
sudo mount /dev/md0 /mnt/raid1

Verify it’s mounted correctly with df -h /mnt/raid1.

Step 5: Save the RAID configuration

Save your mdadm configuration so the array reassembles automatically after a reboot.

sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u

Add the array to /etc/fstab for automatic mounting:

echo '/dev/md0 /mnt/raid1 ext4 defaults,nofail 0 0' | sudo tee -a /etc/fstab

The nofail option prevents the server from hanging at boot if the array isn’t available.

Step 6: Simulate a disk failure

This is where the tutorial gets practical. You’ll mark one disk as faulty to simulate a real drive failure.

sudo mdadm --manage /dev/md0 --fail /dev/sdb

Check the array status immediately after:

sudo mdadm --detail /dev/md0

You’ll see /dev/sdb listed as faulty. The array state will show degraded. Your data is still fully accessible from /dev/sdc. This is RAID 1 doing exactly what it’s supposed to do.

Step 7: Remove the failed disk and add a replacement

First, remove the faulty disk from the array.

sudo mdadm --manage /dev/md0 --remove /dev/sdb

In a real scenario, you’d physically swap the drive. Here, you’ll re-add the same disk to simulate plugging in a replacement. In production, the replacement would be a new disk at the same device path.

sudo mdadm --manage /dev/md0 --add /dev/sdb

The array immediately begins rebuilding. Monitor the rebuild progress with:

watch cat /proc/mdstat

Rebuilding time depends on disk size. A 1TB drive can take an hour or more. Don’t reboot during this process.

Troubleshooting Common Mdadm and RAID Issues

Even with careful setup, things can go wrong. Here are the most common problems and how to fix them.

Array won’t assemble after reboot

Run this command to manually assemble the array:

sudo mdadm --assemble --scan

If that fails, try assembling it explicitly:

sudo mdadm --assemble /dev/md0 /dev/sdb /dev/sdc

Array shows as inactive or failed

Check the detailed status first:

sudo mdadm --detail /dev/md0

If both disks show as failed, stop the array and reassemble it:

sudo mdadm --stop /dev/md0
sudo mdadm --assemble /dev/md0 /dev/sdb /dev/sdc

Disk not recognized after hot-add

Run sudo partprobe to refresh the kernel’s partition table. Then retry the --add command.

Email alerts for disk failures

Mdadm can email you when a disk fails. Edit /etc/mdadm/mdadm.conf and add:

MAILADDR [email protected]

Then restart the mdadm monitoring service:

sudo systemctl restart mdmonitor

This is critical for production servers. You want to know about failures before they become disasters. The Linux RAID Wiki covers advanced monitoring options in detail.

Conclusion: Next Steps After Setting Up Software RAID with Mdadm

You now know how to set up software RAID with mdadm on Ubuntu Server including disk failure simulation and recovery. You built a RAID 1 mirror, formatted and mounted it, simulated a drive failure, and walked through the full recovery process. That’s the complete lifecycle of a software RAID array.

From here, consider exploring RAID 5 or RAID 6 for arrays with three or more disks. These levels offer better storage efficiency while still protecting against drive failure. You should also set up regular backups even with RAID in place. RAID protects against hardware failure, not accidental deletion or filesystem corruption.

Keep monitoring your array with cat /proc/mdstat regularly. Set up email alerts so failures don’t catch you off guard. Your data is only as safe as the attention you give your storage infrastructure.

Similar Posts