mdadm is the utility you use to manage Linux software RAID arrays.

Let’s get a RAID 1 (mirror) array up and running with two disks.

First, we need some disks. For this example, I’ll use /dev/sdb and /dev/sdc. Important: These disks will be wiped. Back up anything important!

# Create the RAID 1 array
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

This command tells mdadm to create a new RAID device named /dev/md0. The --level=1 specifies a RAID 1 mirror, and --raid-devices=2 indicates we’re using two physical devices. Finally, /dev/sdb and /dev/sdc are the actual disks to be included in the array. The array will immediately start synchronizing the data between the disks.

You can check the sync progress with:

cat /proc/mdstat

You’ll see output like this:

md0 : active raid1 sdc[1] sdb[0]
      1953371648 blocks super 1.2 [2/2] [UU]
      [==============>........]  resync = 60.0% (1173025000/1953371648) finish=120.5min speed=16000K/sec

[UU] means both devices are up and running. If one failed, you’d see [U_]. The resync percentage shows how much data has been copied to rebuild the mirror.

Once the sync is complete, you need to format the RAID device like any other block device. Let’s format it with XFS:

sudo mkfs.xfs /dev/md0

Now, create a mount point and mount it:

sudo mkdir /mnt/raid1
sudo mount /dev/md0 /mnt/raid1

To make this persistent across reboots, you need to add an entry to /etc/fstab. First, get the UUID of the RAID device:

sudo blkid /dev/md0

You’ll get output like:

/dev/md0: UUID="a1b2c3d4-e5f6-7890-1234-567890abcdef" TYPE="xfs"

Now, edit /etc/fstab and add a line using this UUID:

UUID=a1b2c3d4-e5f6-7890-1234-567890abcdef /mnt/raid1 xfs defaults 0 0

This tells the system to mount the RAID device with the specified UUID to /mnt/raid1 using XFS, with default mount options, and no dump or fsck passes.

Finally, you need to save the RAID configuration so mdadm can reassemble the array on boot.

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

This command scans for active RAID arrays, formats their details into the mdadm.conf file, and appends it. This is crucial for the system to recognize and assemble your RAID array automatically during the boot process.

The most surprising thing about mdadm is that it doesn’t store the RAID metadata directly on the disks in a way that’s immediately obvious from fdisk or parted. Instead, it writes a superblock at a specific offset (usually the beginning of the device, or a designated chunk) that contains all the information needed to reconstruct the array. This means you can often recover arrays even if the partition table is damaged, as long as the superblock is intact.

When a disk fails, say /dev/sdc, you’ll see it in cat /proc/mdstat as [U_]. To remove the failed disk and mark it as failed:

sudo mdadm /dev/md0 --fail /dev/sdc
sudo mdadm /dev/md0 --remove /dev/sdc

Then, you can add a new disk (e.g., /dev/sdd) to replace it:

sudo mdadm /dev/md0 --add /dev/sdd

The array will immediately start resyncing to the new disk.

The next thing you’ll likely encounter is managing RAID levels beyond simple mirroring, like RAID 5 or RAID 6, which introduces parity calculations and different failure tolerance characteristics.

Want structured learning?

Take the full Linux & Systems Programming course →