Sunday, July 3, 2011

How to install Linux (Ubuntu, Debian, etc.) on two drive RAID mirror

Drives are cheap nowadays, and your data is valuable. Next time you are installing Ubuntu, consider buying a second disk, and making your fresh installation on a mirrored drive setup using Linux software RAID (MD).

This is a guide for how to install Linux/GNU on a RAID1 or RAID10 mirrored set of drives. We use a GPT disk label so it works for any size drives, even ones larger than 2TB. For this guide, I used Ubuntu LTS, but Debian, and any newer Ubuntu should work with little modification. Any other distribution, like Fedora that has a LiveCD which one can install from can be installed with little modification.

If you are looking to convert a running system which *doesn't* use the GPT disk label, consider following my guide for converting a Linux/GNU system to mirrored drive setup.

1) Download, burn, and boot into an Ubuntu LiveCD.

2) After it starts, start a terminal, and run:

sudo apt-get update
sudo apt-get install mdadm

When prompted about the Mail Transport Agent (postfix in LTS) configuration, select "No Configuration".

3) Turn off swap in case the LiveCD discovered and started using a swap partition on some drive in your system.

sudo swapoff -a

4) Delete all partitions on the target drives. Assuming they are /dev/sda and /dev/sdb:

sudo parted /dev/sda
mklabel msdos y

And similarly for /dev/sdb.

4) Create a RAID10 or RAID1 with one drive and second drive missing (we will add it later).
a) For RAID10:

sudo mdadm --create /dev/md0 -l10 -pn2 -c512 -n2 /dev/sdb missing

b) For RAID1:

sudo mdadm --create /dev/md0 -l1 -n2 /dev/sdb missing

5) Check that md0 exists and is operational:

cat /proc/mdstat

5) Create partition table and partitions on the RAID, but DO NOT MAKE A SWAP partition. This is important because the Ubuntu LiveCD will try to activate swap on the raw device (/dev/sda) even though the device is part of a RAID and will fail because the device is busy:

sudo parted /dev/md0
mklabel gpt y
mkpart "grub-bios" ext4 0 1
set 1 bios_grub on
mkpart "root" ext4 1 40000

I found it necessary to stop and start the array to get the partitions to show up.

mdadm --stop /dev/md0
rm -rf /dev/md0*
mdadm --assemble --scan

The 40000MB size above is just an example. Modify to suit your needs. Also, feel free to create separate /boot, /var, /usr, /tmp, etc. as you see fit.

5) Double-click the "Install Ubuntu" icon on the desktop, and go through the process until you reach the point of partitioning.

6) Select "Specify partitions manually", and click "Forward".

7) On the next screen, double-click the /dev/md0p2 partition.
a) In the "Use as" field, for a production server, select ext3. For a personal computer, you can select ext4.
b) Put a check-mark on "Format the partition".
c) Enter "/" in the "Mount point" field (just forward slash, without the quotes).
d) Click "OK", and then "Forward". You will likely get a warning that no swap has been created, but simply click "Continue".

8) On the next screen, enter your login information, and click "Forward", and on the screen after that, select "Advanced", and under "Device for boot loader installation", select /dev/md0. Installation of the boot-loader will fail, and we'll have to do it manually, but it's better than the installer trying to install it on some raw device.

9) As mentioned above, towards the end of the installation, the boot-loader will fail to install. Select the "Continue without a boot-loader" option, and finish the installation. Click "Continue testing" to stay in the LiveCD session.

10) Edit your fstab, and change ALL the entries that read UUID=XXXXXXXXX to /dev/md0pNN, where NN is a partition number. In other words, you need to make sure your system will not decide to use the raw devices which comprise /dev/md0, but the mirror itself. If your device is indicated by a UUID, then hopefully there is some indication what the device name used to be at installation time in a comment above the entry. So, for example, if the comment says the device used to be /dev/md0p2, delete ONLY the UUID=XXXXXXXXX part (make sure you leave the mount-point intact), and put /dev/md0p2 in place of the UUID=XXXXXXXXX.

11) Save your array into /etc/mdadm/mdadm.conf, and rebuild your initrd in order to make sure the array gets assembled at boot time, and under the correct name (e.g. /dev/md0 instead of something like /dev/md_d0).

sudo chroot /mnt

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev

apt-get update
apt-get install mdadm
mkdir -p /etc/mdadm/
mdadm --examine --scan > /etc/mdadm/mdadm.conf
update-initramfs -c -k all

If you get complaints that initrd is custom, you can force update-initramfs totake over the custom initrd with these options instead:

update-initramfs -ut -k all


12) Configure and install grub v2 into the MBR of the mirror. For Ubuntu 10.04, it was trivially easy to do. First we make sure that Grub v2 is installed.

sudo apt-get install grub2

Next, edit /etc/default/grub, and *uncomment* the line that reads "GRUB_DISABLE_LINUX_UUID=true".

grub-mkconfig > /boot/grub/grub.cfg
grub-install --recheck --modules="raid" /dev/sda

13) Reboot into your disk installation, but make sure you are booting off /dev/sda, on which the mirror lives now. To do so, you can usually call up a one-time Boot menu with one of the F-keys (like F8) on many systems. Otherwise, modify the necessary BIOS settings to do so.

Once you're in, you can add /dev/sdb to the array that was created in the steps above.

sudo mdadm --add /dev/md0 /dev/sdb

14) You can watch the process of resyncing the drives with:

watch cat /proc/mdstat

15) If you need swap, create a swap partition and add it to /etc/fstab. If you find that you would like to have this step described here (or linked from here), please post a comment requesting it.


  1. thank you for a guide to install Linux Ubuntu.

  2. I tried this on 11.10. Worked quite fine until I got to the install of grub. Then all I got was core dumps. It seems to be related to the /dev/md0 device, not the modules list.
    Carl L.

  3. Great guide. However I want to use 4 drives in a RAID 10 with a spare drive as well.
    What should I do different?
    What about creating a Swap partition?
    I would create a swap partition on all the drives, but outside the RAID. What do you think about that, any suggestions?
    Joseph B