Mdadm Cheat Sheet

by Scott, Revised on October 25, 2009 (Linux)

Mdadm is the modern tool most Linux distributions use these days to manage software RAID arrays. This cheat sheet will show the most common usages of mdadm to manage software raid arrays (it assumes a good understanding of software RAID and Linux). It will just explain the commands line usage of mdadm. The examples bellow use RAID1, but they can be adapted for any RAID level the Linux kernel driver supports.

Create a new RAID array

mdadm --create --verbose /dev/md0 --level=1 /dev/sda1 /dev/sdb2

or using the compact notation:

mdadm -Cv /dev/md0 -l1 -n2 /dev/sd[ab]1

/etc/mdadm.conf

/etc/mdadm.conf or /etc/mdadm/mdadm.conf (on Debian) is the main configuration file for mdadm. After the creation of RAID arrays, add them to this file using:

mdadm --detail --scan >> /etc/mdadm.conf

or on Debian

mdadm --detail --scan >> /etc/mdadm/mdadm.conf

Remove a disk from an array

A disk cannot be removed directly from the array, unless it is failed, so first it has to be failed (this step is not needed if the drive already in a failed state):

mdadm --fail /dev/md0 /dev/sda1

and now to remove it:

mdadm --remove /dev/md0 /dev/sda1

this can be done in a single step using:

mdadm /dev/md0 --fail /dev/sda1 --remove /dev/sda1

Add a disk to an existing array

mdadm --add /dev/md0 /dev/sdb1

Verifying the status of the RAID arrays

Check the status of the arrays on the system with:

cat /proc/mdstat

or:

mdadm --detail /dev/md0

The output of this command will look like:

cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdb1[1] sda1[0]
104320 blocks [2/2] [UU]

md1 : active raid1 sdb3[1] sda3[0]
19542976 blocks [2/2] [UU]

md2 : active raid1 sdb4[1] sda4[0]
223504192 blocks [2/2] [UU]

Here, both drives are used and working fine (shown as a "U"). A failed drive will show as "F", while a degraded array will miss the second disk.

Note: while monitoring the status of a RAID rebuild operation using watch can be useful:

watch cat /proc/mdstat

Stop and delete a RAID array

To completely remove a raid array, first stop and then remove it:

mdadm --stop /dev/md0
mdadm --remove /dev/md0

and finally, delete the superblock from the individual drives:

mdadm --zero-superblock /dev/sda

Finally in using RAID1 arrays, to create identical partitions on both drives (useful to copy the partitions from sda to sdb):

sfdisk -d /dev/sda | sfdisk /dev/sdb

This command will dump the partition table of sda, removing completely the existing partitions on sdb (be sure you want this before running this command, as it will not warn you at all).

Information source credit: MDLog:/sysadmin

Tags: Linux