Skip to content

Linux RAID with mdadm — Complete Guide

DodaTech Updated 2026-06-24 5 min read

Linux software RAID using mdadm provides enterprise-grade disk redundancy and performance without specialized hardware controllers. This guide covers all common RAID levels, array creation, monitoring, and recovery.

Hook

RAID combines multiple disks into one logical device for redundancy (mirroring), performance (striping), or both. You will learn to create, monitor, and recover mdadm RAID arrays for production storage. DodaZIP uses RAID 10 on its compression nodes for the performance of striping with the safety of mirroring.

Why RAID Matters

A single disk failure on a production server means data loss and downtime. RAID provides automatic redundancy so a disk can fail without service interruption. Hardware RAID controllers are expensive and vendor-locked; Linux mdadm software RAID is free, flexible, and equally reliable.

RAID Levels Overview

flowchart TD
  A[RAID Level Decision] --> B{Redundancy needed?}
  B -->|No| C[RAID 0 - Striping
Speed, no redundancy] B -->|Yes| D{Fault tolerance?} D -->|1 disk| E[RAID 1 - Mirroring] D -->|2 disks| F[RAID 5 - Striping + Parity] D -->|2 disks| G[RAID 6 - Striping + Double Parity] D -->|1-2 disks| H[RAID 10 - Mirror + Stripe]

Prerequisites

# Install mdadm
sudo apt install mdadm -y

# Identify available disks
lsblk -o NAME,SIZE,TYPE,TRAN

Creating RAID Arrays

RAID 0 — Striping

Spread data across disks for maximum speed. No redundancy — one failure destroys everything.

sudo mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sdb /dev/sdc
sudo mkfs.ext4 /dev/md0
sudo mount /dev/md0 /mnt/storage

RAID 1 — Mirroring

Two disks with identical data. Read performance improves, write is identical to single disk.

sudo mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
sudo mkfs.ext4 /dev/md1

RAID 5 — Striping with Parity

Three or more disks. One disk can fail without data loss. Parity overhead is 1/N.

sudo mdadm --create /dev/md5 --level=5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd

RAID 6 — Double Parity

Four or more disks. Two disks can fail. Parity overhead is 2/N.

sudo mdadm --create /dev/md6 --level=6 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde

RAID 10 — Mirroring + Striping

Best performance and redundancy. Requires even number of disks.

sudo mdadm --create /dev/md10 --level=10 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde

Checking Array Status

# Detailed status
cat /proc/mdstat

# mdadm details
sudo mdadm --detail /dev/md10

Expected output:

$ cat /proc/mdstat
Personalities : [raid10] [raid1] [raid6] [raid5] [raid0]
md10 : active raid10 sde[3] sdd[2] sdc[1] sdb[0]
      3907023872 blocks super 1.2 512K chunks 2 near-copies [4/4] [UUUU]

md1 : active raid1 sdc[1] sdb[0]
      976762432 blocks super 1.2 [2/2] [UU]

unused devices: <none>

Configuring Spare Disks

A spare disk automatically replaces a failed drive.

# Create RAID 5 with one spare
sudo mdadm --create /dev/md5 --level=5 --raid-devices=3 \
  --spare-devices=1 /dev/sdb /dev/sdc /dev/sdd /dev/sde

# Add spare to existing array
sudo mdadm /dev/md5 --add /dev/sde

# Check spare status
sudo mdadm --detail /dev/md5 | grep -i spare

Persisting Configuration

# Save array configuration
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf

# Update initramfs
sudo update-initramfs -u

Monitoring Arrays

# Monitor with mdadm
sudo mdadm --monitor --mail=admin@dodatech.com --delay=300 /dev/md*

# Check disk health
sudo smartctl -H /dev/sdb

# Regular checks
sudo mdadm --assemble --scan    # Assemble all arrays
sudo mdadm --detail /dev/md5    # Detailed health

Replacing a Failed Drive

Simulate a failure and replace:

# Mark disk as failed
sudo mdadm /dev/md5 --fail /dev/sdb

# Remove failed disk
sudo mdadm /dev/md5 --remove /dev/sdb

# Physically replace then add new disk
sudo mdadm /dev/md5 --add /dev/sdf

# Watch rebuild progress
watch -n 1 cat /proc/mdstat

Expected output during rebuild:

$ cat /proc/mdstat
md5 : active raid5 sdf[4] sdd[2] sdc[1] sdb[0](F)
      2930266544 blocks super 1.2 level 5, 512k chunk, algorithm 2 [4/3] [UU_U]
      [========>............]  recovery = 42.3% (310123456/732566636) finish=8.2min speed=832156K/sec

Performance Benchmarking

# Sequential read test
sudo hdparm -t /dev/md10

# Random I/O with fio
sudo fio --randrepeat=1 --ioengine=libaio --direct=1 --gtod_reduce=1 \
  --name=test --bs=4k --iodepth=64 --size=1G --readwrite=randrw \
  --rwmixread=75 --filename=/dev/md10

Common Errors

1. "mdadm: Device or resource busy"

The disk has leftover filesystem signatures. Wipe them: sudo wipefs -a /dev/sdb.

2. Array Fails to Assemble After Reboot

The mdadm.conf may be missing or wrong. Run sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf and sudo update-initramfs -u.

3. Degraded Array Not Detected

Monitors may not be configured. Add a cron job: @daily /usr/share/mdadm/checkarray --cron --all --quiet.

4. Wrong Disk Removed

Always verify with lsblk and smartctl before failing a drive. Label disks physically.

5. RAID 5 Write Performance Poor

RAID 5 has write penalty (4 I/Os per write). Tune chunk size: --chunk=512 for large files, --chunk=64 for mixed workloads.

6. Disks Show "Removed" But Still Connected

The disk may have intermittent errors. Check dmesg | tail for SCSI errors. Replace the SATA/SAS cable.

7. Cannot Add Disk to RAID

Disk may have a partition table. Use whole disk (/dev/sdb) or create partition type fd (Linux RAID auto).

Practice Questions

1. What RAID level provides mirroring only? RAID 1 mirrors data across two or more disks. Read performance improves but write is per-disk speed.

2. How many disk failures can RAID 6 tolerate? Two simultaneous disk failures. RAID 5 tolerates one.

3. What is the difference between RAID 5 and RAID 10? RAID 5 uses parity (1 disk overhead) with striping. RAID 10 mirrors then stripes (50% overhead) but offers better write performance and faster rebuilds.

4. How do you add a spare disk to an existing array? sudo mdadm /dev/md5 --add /dev/sde. The spare will automatically activate when a disk fails.

5. Challenge: Design a RAID layout for a database server that needs: (1) 4TB usable, (2) survive two disk failures, (3) fast reads and writes. Use 2TB disks. Show the mdadm command. Answer: RAID 10 with 4 disks provides 4TB usable (2TB mirrored + 2TB striped x 2 = 4TB). mdadm --create /dev/md10 --level=10 --raid-devices=4 /dev/sd[b-e]. RAID 6 with 6 disks also works: 3 usable pairs, survive two failures.

Mini Project: RAID Health Monitoring Script

Create a script that checks all arrays and emails alerts:

#!/bin/bash
# raid_health.sh — Check mdadm RAID health
# Usage: ./raid_health.sh

ALERT_EMAIL="admin@dodatech.com"
FAILED=0

for md in /dev/md*; do
    [ -e "$md" ] || continue
    STATUS=$(sudo mdadm --detail "$md" | grep "State :")
    echo "$md: $STATUS"
    if echo "$STATUS" | grep -qv "clean"; then
        FAILED=1
    fi
done

if [ "$FAILED" -eq 1 ]; then
    sudo mdadm --detail /dev/md* | mail -s "RAID Alert - Degraded Array" "$ALERT_EMAIL"
    exit 1
fi

echo "All arrays healthy"

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Updated 2026-06-24.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro