Software RAID with mdadm utility

Here are some useful commands to manage a GNU/Linux RAID with the help of mdadm utility.

Intro

Configuration

Commands

Reassemble RAID from a LiveCD or after reinstall

root@host:~# mdadm --assemble /dev/md0 /dev/sda1 /dev/sdb1

Populate /etc/mdadm/mdadm.conf file

root@host:~# mdadm --detail --scan >> /etc/mdadm.conf

Remove device permanently

root@host:~# mdadm --zero-superblock /dev/sda1

Check RAID status

root@host:~# cat /proc/mdstat

Run data scrubbing to check for and fix errors

root@host:~# echo check > /sys/class/block/md0/md/sync_action

Modify rebuild speed limits

root@host:~# echo 20000 > /proc/sys/dev/raid/{speed_limit_max,speed_limit_min}

Print details of one or more md devices

root@host:~# mdadm -D /dev/md0

Return status :

How To

Growing a RAID5 with the add of one disk

root@host:~# umount /dev/md0
root@host:~# sfdisk -d /dev/sdb | sfdisk --force /dev/sdd
root@host:~# mdadm --manage /dev/md0 --add /dev/sdd1
root@host:~# mdadm --grow /dev/md0 --raid-devices=4 --backup-file=/root/raid5.backup.file
root@host:~# resize2fs /dev/mapper/md0

Growing an encrypted RAID (convert RAID1 to RAID5)

root@host:~# cryptsetup luksHeaderBackup /dev/md0 --header-backup-file path/to/backup.img
root@host:~# cryptsetup luksDump /dev/md0 | grep "Payload offset"
root@host:~# dd if=/dev/md0 of=chemin/vers/backup.img bs=512 count=4040
root@host:~# cryptsetup luksHeaderRestore /dev/md0 --header-backup-file chemin/vers/backup.img
root@host:~# dd if=./backup.img of=/dev/md0 bs=512 count=4040
root@host:~# mdadm --stop /dev/md0
root@host:~# sfdisk -d /dev/sdb | sfdisk --force /dev/sdc
root@host:~# mdadm --create /dev/md0 --level=5 -n 2 /dev/sda1 /dev/sdb1
root@host:~# mdadm --add /dev/md0 /dev/sdc1
root@host:~# mdadm --grow /dev/md0 --raid-disks=3 --backup-file=/root/raid1-5.backup.file
root@host:~# cryptsetup resize /dev/mapper/md_crypt
root@host:~# e2fsck -f /dev/mapper/md_crypt
root@host:~# resize2fs /dev/mapper/md_crypt
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :