Mdadm

From gr0x0rd
Jump to navigation Jump to search

Using RAID

If you have multiple hard drives, you may want to consider using RAID for the purpose of redundancy. Often, a dead drive can result in a dead system, and without frequent backups, not having RAID leaves you vulnerable to catastrophic data loss.

While mdadm provides redundancy for your data, it also is software based, so it consumes CPU cycles to perform its operations. If a drive is lost and an array needing to be rebuilt, your system will suffer from performance issues until the rebuild is completed. However, this is a small price to pay for safeguarding your system and your data.

This example uses four SATA hard drives (sda, sdb, sdc and sdd) in a RAID5 configuration for the root and swap partitions, and RAID1 for the boot partition. Since the kernel needs to be loaded from the boot partition, and mdadm cannot be run without the kernel or another boot device, the only configuration for a bootable software RAID linux system is RAID1.

Once you have partitioned your primary disk, you'll need to copy the partition table to the other drives you'll be leveraging in your RAID array.

sfdisk -d /dev/sda > sda.tmp
sfdisk /dev/sdb < sda.tmp
sfdisk /dev/sdc < sda.tmp
sfdisk /dev/sdd < sda.tmp

Initialize the arrays using mdadm. Note that newer versions of mdadm are not compatible with the current linux kernel without grub2 and an initramfs (May 2011) so the option --metadata=0.9 needs to be included.

mdadm --create --verbose --metadata=0.9 /dev/md1 --level=1 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 
mdadm --create --verbose --metadata=0.9 /dev/md2 --level=5 --raid-devices=4 /dev/sda2 /dev/sdb2 /dev/sdc2 /dev/sdd2
mdadm --create --verbose --metadata=0.9 /dev/md3 --level=5 --raid-devices=4 /dev/sda3 /dev/sdb3 /dev/sdc3 /dev/sdd3

View the status of the newly created arrays using

watch cat /proc/mdstat

You can now proceed with building your system, using md1, md2 and md3 in the place of sd1, sd2 and sd3.

Replacing a dead disk

If one of the drives in your array has died, you'll need to replace it. Once you have connected the new drive and powered up your system, copy the partition table from one of the surviving disks to the new drive. In this example, the new drive is recognized as /dev/sdY.

$ sudo sfdisk -d /dev/sda > sda.tmp
$ sfdisk /dev/sdY < sda.tmp

Now that the partition table is ready, you can use mdadm to rebuild the array. Here, X is the array in question [1-3], Y is the letter of the drive in question [a-d], and Z is the partition in question [1-3] based on the partition table setup explained above. Repeat as necessary for each array.

$ sudo mdadm /dev/mdX [-r /dev/sdYZ] -a /dev/sdYZ