mdadm RAID Management

mdadm cheatsheet — create, assemble, monitor software RAID arrays. mdadm --create, --assemble, --detail, --manage. RAID 1/5/6/10 on Linux with real examples.

9 min read

What it is

mdadm is a Linux utility for managing software RAID arrays, providing tools to create, assemble, manage, and monitor RAID devices.

Installation

Linux

# Debian/Ubuntu
sudo apt update
sudo apt install mdadm

# Fedora/CentOS/RHEL
sudo dnf install mdadm
# or
sudo yum install mdadm

Mac

mdadm is not natively available on macOS. It is a Linux-specific tool.

Windows

mdadm is not natively available on Windows. Windows has its own built-in Disk Management tools for software RAID.

Core Concepts

  • RAID (Redundant Array of Independent Disks): A data storage virtualization technology that combines multiple physical disk drives into one or more logical units for the purposes of data redundancy, performance improvement, or both.
  • Array: A logical RAID device created by mdadm using one or more physical devices.
  • Component Device: A physical disk or partition that is part of a RAID array.
  • Superblock: Metadata stored on component devices that describes the RAID array they belong to. mdadm uses this to identify and reassemble arrays.
  • RAID Levels: Different configurations of RAID arrays offering varying levels of performance and redundancy (e.g., RAID 0, RAID 1, RAID 5, RAID 6, RAID 10).
  • Active/Ready: A state for a component device. "Active" means it’s currently part of the running array. "Ready" means it’s available to be added to an array.
  • Failed/Degraded: A state where one or more component devices have failed. The array is "degraded" and may still be operational but without redundancy.
  • Spare Device: A component device that is not actively part of the array but is ready to automatically replace a failed device.

Commands / Usage

Creating Arrays

  • Create a RAID 0 array with two disks:

    sudo mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sdb1 /dev/sdc1
    

    Creates a RAID 0 array named /dev/md0 using /dev/sdb1 and /dev/sdc1.

  • Create a RAID 1 array with two disks:

    sudo mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
    

    Creates a RAID 1 array named /dev/md1 using /dev/sdb1 and /dev/sdc1.

  • Create a RAID 5 array with three disks:

    sudo mdadm --create /dev/md5 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1
    

    Creates a RAID 5 array named /dev/md5 using /dev/sdb1, /dev/sdc1, and /dev/sdd1.

  • Create a RAID 10 array with four disks:

    sudo mdadm --create /dev/md10 --level=10 --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
    

    Creates a RAID 10 array named /dev/md10 using four component devices.

  • Create an array with a spare device:

    sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1 --spare-devices=1 /dev/sdd1
    

    Creates a RAID 1 array with /dev/sdb1 and /dev/sdc1, and designates /dev/sdd1 as a hot spare.

  • Create an array with specific chunk size (RAID 0, 5, 6, 10):

    sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1 --chunk=256
    

    Creates a RAID 5 array with a chunk size of 256 KiB.

Assembling Existing Arrays

  • Assemble all found RAID arrays:

    sudo mdadm --assemble --scan
    

    Scans for all RAID superblocks on the system and assembles any recognized arrays.

  • Assemble a specific array by its device name:

    sudo mdadm --assemble /dev/md0 /dev/sdb1 /dev/sdc1
    

    Assembles the array /dev/md0 using the specified component devices. Useful if --scan fails or you need to specify components.

  • Assemble an array that might be in a faulty state:

    sudo mdadm --assemble --force /dev/md0 /dev/sdb1 /dev/sdc1
    

    Forces the assembly of an array, potentially ignoring some errors. Use with caution.

Managing Arrays

  • Add a spare device to an existing array:

    sudo mdadm /dev/md0 --add /dev/sdd1
    

    Adds /dev/sdd1 as a spare to the array /dev/md0.

  • Remove a device from an array (e.g., a failed disk):

    sudo mdadm /dev/md0 --remove /dev/sdb1
    

    Removes /dev/sdb1 from the array /dev/md0. This is typically done after a disk has failed and been marked as faulty.

  • Mark a device as faulty:

    sudo mdadm /dev/md0 --fail /dev/sdb1
    

    Manually marks /dev/sdb1 as faulty within the array /dev/md0. This is often done before physically removing a disk.

  • Replace a failed device:

    sudo mdadm /dev/md0 --replace /dev/sdb1 /dev/sde1
    

    Replaces the failed device /dev/sdb1 with a new device /dev/sde1. If a hot spare is available and no replacement is specified, it will be used automatically.

  • Grow an array (add devices):

    sudo mdadm /dev/md0 --grow --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
    

    Grows the array /dev/md0 to use four devices, adding /dev/sde1 (assuming it was already added with --add or is a spare). The array must be assembled first.

  • Grow an array to use more space on existing devices (e.g., after resizing partitions):

    sudo mdadm --grow /dev/md0 --size=max
    

    Resizes the array /dev/md0 to use the maximum available space on its component devices.

  • Stop an array:

    sudo mdadm --stop /dev/md0
    

    Stops the RAID array /dev/md0. All component devices will be detached.

  • Zero out the superblock on a disk:

    sudo mdadm --zero-superblock /dev/sdb1
    

    Removes the mdadm superblock from /dev/sdb1, making it available for other uses or for re-adding to a different array.

Monitoring Arrays

  • Get detailed status of all arrays:

    sudo mdadm --detail --scan
    

    Displays detailed information for all RAID arrays currently assembled on the system.

  • Get detailed status of a specific array:

    sudo mdadm --detail /dev/md0
    

    Displays detailed information about the /dev/md0 RAID array, including component devices, states, and RAID level.

  • Check the health of all arrays (summary):

    cat /proc/mdstat
    

    Provides a quick overview of active RAID arrays, their status (e.g., [UU], [U_]), and any ongoing operations (like rebuilding).

Configuration

  • Generate a configuration file:
    sudo mdadm --detail --scan | sudo tee /etc/mdadm/mdadm.conf
    
    Scans for active arrays and saves their configuration to /etc/mdadm/mdadm.conf. This file is used by the system to automatically assemble arrays on boot.

Common Patterns

  • Create a RAID 1 mirrored boot partition (e.g., /dev/sda1, /dev/sdb1):

    # Ensure partitions are the same size and type
    sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
    # Format the new RAID device
    sudo mkfs.ext4 /dev/md0
    # Add to mdadm.conf for auto-assembly on boot
    sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
    # Update initramfs (Debian/Ubuntu)
    sudo update-initramfs -u
    
  • Replace a failed disk in a degraded RAID 1 array:

    # Identify the failed disk (e.g., /dev/sda1) from /proc/mdstat or mdadm --detail
    sudo mdadm /dev/md0 --fail /dev/sda1
    sudo mdadm /dev/md0 --remove /dev/sda1
    # Physically replace the drive and partition it identically (e.g., /dev/sdc1)
    sudo mdadm /dev/md0 --add /dev/sdc1
    # Monitor rebuild progress
    watch cat /proc/mdstat
    
  • Increase the size of a RAID 5 array after extending underlying partitions:

    # Assume you have partitions /dev/sdb1, /dev/sdc1, /dev/sdd1 forming /dev/md0
    # You've resized the partitions (e.g., using fdisk/parted and growing filesystem)
    # Now tell mdadm to use the new space
    sudo mdadm --grow /dev/md0 --size=max
    # Resize the filesystem on /dev/md0 to use the new array space
    sudo resize2fs /dev/md0
    
  • Create a RAID 0 for performance (non-redundant):

    sudo mdadm --create /dev/md0 --level=0 --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
    sudo mkfs.ext4 /dev/md0
    sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
    
  • Add a disk to an existing array as a hot spare:

    # Ensure /dev/sdd1 is not in use and is partitionable/usable
    sudo mdadm /dev/md0 --add /dev/sdd1
    # Verify it's listed as a spare
    sudo mdadm --detail /dev/md0
    

Gotchas

  • Superblock Location: By default, mdadm stores metadata in the last 131072 sectors of the device. If you use the entire disk (e.g., /dev/sdb instead of /dev/sdb1), ensure there’s no critical data or partition table that would conflict. It’s generally safer and recommended to use partitions (/dev/sdb1).
  • Data Loss on RAID 0: RAID 0 offers no redundancy. If any single drive in a RAID 0 array fails, all data on the entire array is lost.
  • RAID 5 Write Hole: RAID 5 has a "write hole" issue where a power failure during a write operation can lead to data inconsistency. RAID 6 and RAID 10 mitigate this. mdadm attempts to handle this, but it’s a fundamental RAID 5 limitation.
  • Device Naming: /dev/mdX names are assigned sequentially. If an array was created as /dev/md0 and later stopped, a new array might be created as /dev/md0 if you don’t explicitly specify the name during assembly.
  • Auto-assembly: Ensure /etc/mdadm/mdadm.conf is correctly populated and that mdadm is configured to run on boot (often handled by initramfs updates). Without this, arrays might not be available after a reboot.
  • Partition Alignment: For performance, especially with SSDs and certain RAID levels, ensure partitions are properly aligned. mdadm itself doesn’t dictate partition alignment; this is handled by the partitioning tool (like fdisk or parted).
  • Resizing Arrays: Growing an array (--grow --raid-devices=N) requires the component devices to be available and typically necessitates adding new devices or reconfiguring existing ones. Growing the size of the array (--grow --size=max) requires the underlying partitions to have been extended first.
  • Hot Spare Behavior: A hot spare will only automatically replace a failed drive if the array is degraded and the spare is correctly added. If you manually fail and remove a disk, you usually need to manually add the replacement.
  • --force Flag: Use --force with extreme caution. It can be used to assemble arrays that mdadm thinks are inconsistent, potentially leading to data corruption if used incorrectly.
  • File System on RAID: mdadm creates the block device for the array. You still need to create a file system (e.g., ext4, xfs) on the /dev/mdX device before you can mount and use it.