MDADM (Linux Software RAID) Cheatsheet (BASH)

You should always partition disks rather than using the disk itself. Different manufacturers and models can have very slightly different capacities. So if you try to replace a 4TB WD drive with (say) a 4TB Hitachi, you may find you can't add it to the array. Creating a partition of a set size tends to address this, though on slightly larger disks it may leave a few bytes unused

After any changes, remember to skip down to the bottom and save the array config so it'll persist after reboot!

Details

  • Language: BASH

Snippet

## Creating Arrays

# Create a RAID1 array
mdadm --create /dev/md1 --metadata 1.2 --level=mirror --raid-devices=2 /dev/sdb1 /dev/sdb1

# Create a RAID10 array
mdadm --create /dev/md1 --level=10 --raid-devices=4 /dev/sd[b-e]1

# Create a RAID5 array (it's 2019, _don't_ do this!)
mdadm --create --verbose /dev/md1 --level=5 --raid-devices=3 /dev/sd[b-d]1

# Create a RAID6 array
mdadm --create --verbose /dev/md1 --level=6 --raid-devices=4 /dev/sd[b-e]1

# Temporarily create a RAID1 with 1 disk missing (i.e. only use a single disk)
#
# you might risk this if you've limited disk slots and are trying to transfer 
# from one array to a new one so you can use larger capacity disks
# Beware the risk of that single drive failing whilst you do it
#
# See Adding disks for next step below
mdadm --create /dev/md1 --metadata 1.2 --level=mirror --raid-devices=2 missing /dev/sdb1

## Adding Disks

# Add an drive into an array (will default to Hot Spare if enough drives already present)
mdadm --manage /dev/md1 --add /dev/sdc1

# Growing an array
# We assume here the array was previously created with 3 drives
mdadm --manage /dev/md1 --add /dev/sdc1 # Effectively creating hot spare
mdadm --grow /dev/md1 --raid-devices=4 # Grow it to 4 drives, will start a resync
umount /dev/md1
fsck -f /dev/md1
resize2fs /dev/md1 # Command may differ depending on your filesystem

## Removing disks

# Fail and remove 
mdadm --fail /dev/md1 /dev/sdb1 --remove /dev/sdb1

# Force remove (if the disk is no longer visible to the kernel)
mdadm -r /dev/md1 failed # Remove all failed devices
mdadm -r /dev/md1 detached # Remove all devices not in /dev

## Removing arrays
mdadm --stop /dev/md1
mdadm --remove /dev/md1
mdadm --zero-superblock /dev/sdb1

## General

# Check status of the arrays
cat /proc/mdstat

# Save array details to config so they persist after reboot
mdadm --detail --scan >> /etc/mdadm.conf