Tuesday, July 29, 2014

Setting up software raid with mdadm

Summary
Creating and managing software raid (fake raid) is made quite simple with mdadm. This guide intends to cover the very basics necessary creating and managing raid1 (redundancy) arrays.

For the purpose of this guide, I will consider two disks, /dev/sdb and /dev/sdc, each with four partitions numbered 1-4, and I will refer to them in the short form sdb1, sdc2, and so on.


To install mdadm:


apt-get install mdadm


To observe:


sudo cat /proc/mdstat


sudo mdadm --detail /dev/md0


Create a new array from scratch:


sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1


This will create an array in /dev/md0 using two partitions.


Grow existing array:


Say you have the above array running and you want to add another partition in a brand new disk you just got. You can --add a partition to the running array, but you also have to --grow the array, or the disk will be treated as a spare.


You can also --grow an array without providing the partitions (use --force), but until you --add them, the array will be degraded.


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


The array becomes degraded.


sudo mdadm --add /dev/md0 /dev/sdd1


The array goes into rebuild/sync mode, and when done it becomes clean.


Start a raid member in virtual device

sudo mdadm -A -R /dev/md0/dev/sdb1