Mdadm is used in most Linux distributions to manage software raid where the server does not have or use a hardware raid controller This page will show some of the common commands and usages of mdadm. The bellow use RAID1 as an example, but can be modified for any raid level you are using.
mdadm --create --verbose /dev/md0 --level=1 /dev/sda1 /dev/sdb2
After we create our raid arrays we add them mdadm.conf depending on your OS this will be in /etc/mdadm.conf or /etc/mdadm/mdadm.conf (debian) we can add the array to the configuration file with the below.
mdadm --detail --scan >> /etc/mdadm.conf
or
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
You can’t remove a disk directly from an array unless it is failed, to remove an active (healthy drive) you first have to fail it before you can remove the drive.
Fail drive
mdadm --fail /dev/md0 /dev/sdb1
Remove drive
mdadm --remove /dev/md0 /dev/sdb1
Alternatively you can fail and remove the disk as a one liner
mdadm /dev/md0 --fail /dev/sdb1 --remove /dev/sdb1
To add or replace a failed disk to an array
mdadm --add /dev/md0 /dev/sdb1
cat /proc/mdstat
To remove a raid array you will have to stop it first and then remove it
mdadm --stop /dev/md0
mdadm --remove /dev/md0
To remove the superblock from the drives
mdadm --zero-superblock /dev/sdb
In this example the disk sdb has failed and been replace, to start the rebuild the disk partitions will need to be copied from the remaining working drive and then the raid set to rebuild.
With this command it will not prompt with any warning so make sure you type correctly as you may add more work for yourself.
sfdisk -d /dev/sda | sfdisk /dev/sdb
This will dump the partition table of sda and copy to sdb replacing all existing partitions. We now need to re add the partitions to the raid replace * with number i.e. sdb1 number depends on the output of cat /proc/mdstat you will need to do this for all partitions.
mdadm /dev/md0 --add /dev/sdb*
You can then watch the status of the rebuild using watch
watch -n 10 cat /proc/mdstat
After the rebuild has completed you may need to reinstall grub if you have replaced a boot drive.
grub-install /dev/sdb
Hope this has helped you, remember to always double check before running commands.