How To: Configuring Bootable RAID1 with mdadm

Creating RAID1 with mdadm

This example has two physical disks, /dev/sda and /dev/sdb, which are identical. Partition the first with your favourite partitioning tool. Example partitions of a 1TiB disk follow, set each raid partition to fd

root@host # sfdisk -l /dev/sda

Disk /dev/sda: 121601 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1   *      0+     15      16-    128488+  fd  Linux raid autodetect
/dev/sda2         16     139     124     996030   82  Linux swap / Solaris
/dev/sda3        140  121600  121461  975635482+  fd  Linux raid autodetect
/dev/sda4          0       -       0          0    0  Empty

There /dev/sda1 is 128MiB partition for boot and /dev/sda3 is for root, sda2 is for swap.

Then copy the partition table to the second disk

root@host # sfdisk -d /dev/sda | sfdisk /dev/sdb

Now the disks look exatly the same, first partition will be bound together for /boot, thrid partition will be bound toegether for /. Second Partitions will be created as two balanced swap areas.

Tune the settings for mdadm to (hopefully) speed up the sync. Then join the two raid partitions together, then wait for them to sync.

If you have data on one of the partitions then list that one first! In the examples below data on sda will be preserved, data on sdb will be lost.

root@host # echo 50000 > /proc/sys/dev/raid/speed_limit_min
root@host # echo 5000000 > /proc/sys/dev/raid/speed_limit_max
root@host # mdadm --create /dev/md/0 --level=1 --metadata=1.0 --raid-devices=2 /dev/sda1 /dev/sdb1
root@host # mdadm --create /dev/md/1 --level=1 --raid-devices=2 /dev/sda3 /dev/sdb3
root@host # watch cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md1 : active raid1 sdb3[1] sda3[0]
      975635350 blocks super 1.1 [2/2] [UU]
      [>....................]  resync =  3.4% (34107584/975635350) finish=142.5min speed=110115K/sec

md0 : active raid1 sdb1[1] sda1[0]
      128480 blocks super 1.1 [2/2] [UU]

unused devices: <none>

Writing mdadm.conf

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

Installing Bootloader

Our favourite is extlinux.

root@host # dd if=/usr/share/syslinux/mbr.bin of=/dev/sda
root@host # dd if=/usr/share/syslinux/mbr.bin of=/dev/sdb
root@host # mkfs -t ext2 -L'boot' /dev/md0
root@host # mount /dev/md0 /mnt/boot
root@host # extlinux --install /mnt/boot/
root@host # nano -w /mnt/boot/extlinux.conf
root@host # cat /mnt/boot/extlinux.conf

DEFAULT praxis-2010.19
DISPLAY extlinux.msg

LABEL praxis-2010.19
  KERNEL kernel-genkernel-x86_64-2.6.31-gentoo-r6
  INITRD initramfs-genkernel-x86_64-2.6.31-gentoo-r6
  APPEND root=/dev/md1

Repairing RAID1

Starting an RAID1 device in degraded mode, so we can mount and copy files out.

root@host # mdadm --build /dev/md2 --level=1 --raid-devices=2 /dev/sdc3 missing
root@host # mount -t ext3 /dev/md2 /mnt/failed-raid

See Also

Loading Comments from Disqus...