Friday, May 15, 2009

Software Raid | Raid Arrays | mdadm on Linux

Tech Blog - here is a quick howto on creating software RAID arrays on your Linux system using mdadm tool.

mdadm is a Linux utility that is used to manage software RAID devices on Linux, previously known as mdctl. it can create, delete, or monitor Linux software RAIDs, mdadm can perform (almost) all of its functions without having a configuration file and does not use one by default, mdadm can provide information about your arrays.

Before we go using mdadm a brief overview of common types of software RAID and hardware RAID, all the RAIDs below are supported by mdadm tool

RAID 0 (striped disks) distributes data across several disks if one of the disk on the array fail all data on all disks will be lost, this type of raid has no redundancy.

RAID 1 (mirrored disks) duplicates data across every disk in the array, Two disks or more each store exactly the same data, at the same time, and at all times. Data is not lost as long as one disk survives, the capacity of the array is the capacity of one disk.

RAID 5 (striped disks with parity) combines three or more disks in a way that protects data against loss of any one disk, if you lose two disk all data on all the disk will be lost.

RAID 6 (striped disks with dual parity) can recover from the loss of two disks.

RAID 10 (1+0) uses both striping and mirroring, Raid 10 is a combination of RAID 1 and RAID 0 hence RAID 10.

mdadm Basic Commands

Create a new RAID arrays
mdadm –create - used to create a new software RAID array, level is the type of RAID to be created

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

mdadm configuration file

/etc/mdadm.conf - main configuration file for mdadm, created RAID arrays must be added to this file to be detected during bootup you will also need to add the array to yoru vfstab for it to be mounted.

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

Remove a disk from an array

You can't remove disk from an active array unless it is marked failed, so we must mark the disk as failed disk using the --fail command then using --remove to remove the disk.

mdadm /dev/md0 --fail /dev/sda3 --remove /dev/sda3

Increasing Active Disk on an RAID 1 Array

You can increase an active disk in your array by using --grow, the sample increse the RAID 1 array to 3 active disk mirror from 2 active disk, to shrink the array you will need to set the spares to faulty then change the --raid-devices to 2, once you increased the raid it will automatically sync a spare disk if you have spare disk on standby.

mdadm --grow /dev/md0 --raid-devices=3

Adding disk to an existing array

mdadm --add - using --add option we can add disk to an existing array to replace and failed disk.

mdadm --add /dev/md0 /dev/sda3

Checking the status of the RAID arrays

mdadm --detail - using the --detail will let you check the status of your RAID array, --verbose will also show general overview of array with very much little details.

mdadm --detail /dev/md0

Halt and delete a RAID array

mdadm --stop - halt the array
mdadm --remove - remove the array
mdadm --zero-superblock - delete the superblock from the drive

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

mdadm --zero-superblock /dev/sda

Preparing Disk Replacement

Use the command below to create a copy of the partition of an existing disk then format then use mdadm --add to the array.

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

If you need more do man mdadm.

Check out other pages:


Pascal said...

Thank you. Excellent guide for all those commands you just never can remember.

Anonymous said...

Thanks for putting this very handy little guide together. Not only is it succinct and helpful, it is also one of the only places i have found concrete information on how to fully and completely *remove* a software RAID array from Linux!


Anonymous said...

I don't think you need to create that mdadm.conf file anymore, at least not on Ubuntu. Think the shape of the array is stored in superblock. Don't really understand how, and have not been able to find any good source of info on it either

Genius24k said...

Yeah when I build my latest server using Cent OS, I didn't create the mdadm.conf file, but just incase you need to its good to know how :)

For suggestion and concerns E-mail