Mdadm is a tool which is used to manage software RAID arrays. In the past raidtools was the tool we have used for this. This article will show the most common usage of mdadm to manage software raid arrays. I believe that you have a good understanding of software RAID and Linux. I will just explain the commands line usage of mdadm. In below example I used RAID1, but you can use any RAID level the Linux Kernel driver supports.
Follow the below mdadm usage to manage software RAID arrays.
1. Create New RAID Array
Follow the below command to create a new RAID array.
# mdadm --create --verbose /dev/md0 --level=1 /dev/sda1 /dev/sdb2
Or with compact notation:
# mdadm -Cv /dev/md0 -l1 -n2 /dev/sd[ab]1
/etc/mdadm.conf (for CentOS/RHEL) or /etc/mdadm/mdadm.conf (for debian) is the main configuration file for mdadm. You can add the RAID arrays after creating it into this file.
# mdadm --detail --scan >> /etc/mdadm.conf
# mdadm --detail --scan >> /etc/mdadm/mdadm.conf
3. Remove Disk From Array
You can not remove the disk directly from the RAID array, unless it is failed, First you need to fail it if the drive is already in failed state then this step is not needed.
# mdadm --fail /dev/md0 /dev/sda1
Once failed you can remove it using below command.
# mdadm --remove /dev/md0 /dev/sda1
You can also do this just single step using below command.
# mdadm /dev/md0 --fail /dev/sda1 --remove /dev/sda1
4. Add New Disk To Existing Array
You can add a new disk to an array using below command.
# mdadm --add /dev/md0 /dev/sdb1
5. Check the RAID Arrays Status
You can verify the status of the arrays on the system using below command.
# cat /proc/mdstat Or # mdadm --detail /dev/md0
You will get some output like below:
# cat /proc/mdstat Personalities : [raid1] md0 : active raid1 sdb1 sda1 104320 blocks [2/2] [UU] md1 : active raid1 sdb3 sda3 19542976 blocks [2/2] [UU] md2 : active raid1 sdb4 sda4 223504192 blocks [2/2] [UU]
In the above example you can see the both drives are used and working fine –U. If there is any failed drive it will indicate with F.
# watch cat /proc/mdstat
6. Stop and Delete RAID Array
If you want to remove RAID array completely first you have to stop it and then remove it as shown below.
# mdadm --stop /dev/md0 # mdadm --remove /dev/md0
And finally you can even delete the superblock from the individual drives using below command.
# sfdisk -d /dev/sda | sfdisk /dev/sdb
Above command will dump the partition table of sda, removing completely the existing partitions on sdb, so be sure you want this before running this command, as it will not warn you at all.
There are lots of mdadm usage for each type of RAID level, You can use the manual page or help if you need more details on its usage.
# man mdadm # mdadm --help