Installing Ubuntu with software RAID1 (mirroring) • 6 Jul 2011
Usually I deploy FreeBSD on my servers, but recently a collegue at work wanted to set up an Ubuntu system with software RAID1, which is mirroring. I think the Ubuntu Server Edition does this out of the box, but out of curiosity I researched how to do this for the 10.10 Desktop Edition.
Remember, mirroring is never a substitute for backup. Always keep recent backups of your data at a remote location, and restore them regularly on test systems to verify their correctness.
The system I use has two SATA disks of the same type and size. Ubuntu sees them as /dev/sda and /dev/sdb. During the installation the system needs a working Internet connection.
First, prepare a Ubuntu USB stick (or CD) and boot from it. When the desktop comes up, do not start the installler, but instead launch the Terminal. We have to prepare the disks first.
Preparing the disks
Become root. This saves you from having to type 'sudo' in front of every command.
Be sure you use clean disks (without GPT's on them). If you're unsure, wipe them first:
dd if=/dev/zero of=/dev/sda bs=1048576 dd if=/dev/zero of=/dev/sdb bs=1048576
Then, start 'cfdisk' with the device name of the first harddisk:
- Create a new 60 MB partition at the beginning
- Move the selection bar into the Free Space
- Create a new partition with the remaining space
- Change the type of this partition to FD.
- Write the changes
- Quit cfdisk
- Now do exactly the same for the second harddisk (cfdisk /dev/sdb).
Inspect the partitions:
root@ubuntu:/home/ubuntu# cfdisk -P s /dev/sda Partition Table for /dev/sda First Last # Type Sector Sector Offset Length Filesystem Type (ID) Flag -- ------- ----------- ----------- ------ ----------- -------------------- ---- 1 Primary 0 112454 63 112455 Linux (83) None 2 Primary 112455 125033894 0 124921440 Linux raid auto (FD) None root@ubuntu:/home/ubuntu# cfdisk -P s /dev/sdb Partition Table for /dev/sdb First Last # Type Sector Sector Offset Length Filesystem Type (ID) Flag -- ------- ----------- ----------- ------ ----------- -------------------- ---- 1 Primary 0 112454 63 112455 Linux (83) None 2 Primary 112455 125033894 0 124921440 Linux raid auto (FD) None
Take note of the 'First Sector' of the second partition (112455 in this case), you'll need that later on for a dd command.
Install mdadm and configure the RAID array
During the mdadm install a postfix configuration dialog might pop up. In that case, select the top option, 'No configuration'.
apt-get install mdadm mdadm --create /dev/md0 --level=raid1 --raid-devices=2 /dev/sda2 /dev/sdb2
While we're at it, let's format it too:
mkfs.ext3 /dev/md0 ln /dev/md0 /dev/sde
Also format the boot partition on both harddisks:
mkfs.ext3 /dev/sda1 mkfs.ext3 /dev/sdb1
Start the installer, it has an icon on the desktop. When you get to the 'Allocate drive space' dialog, choose the 'Specify partitions manually' option. Click the /dev/sda1 partition, choose 'Change...' and fill out the following options:
Use as: Ext3 journaling file system Format the partition: No (do not check the checkmark) Mount point: /boot
Click the /dev/md0 partition, choose 'Change', and fill it out like this:
Use as: Ext3 journaling file system Format the partition: No (do not check the checkmark Mount point: /
There's also a dropdown for 'Device for boot loader installation'. Make sure that /dev/sda is selected there. Click the 'Install Now' button, and 'Continue' on any confirmation dialogs you might get. Fill out the rest of the install questions and sit back while Ubuntu installs itself.
When the installer is done, do not reboot the system yet (choose 'Continue testing'). Go back to your terminal screen and prepare the boot partition:
mkdir /raid mount /dev/md0 /raid mount /dev/sda1 /raid/boot mount --bind /dev /raid/dev mount -t devpts devpts /raid/dev/pts mount -t proc proc /raid/proc mount -t sysfs sysfs /raid/sys chroot /raid apt-get install mdadm
Copy the first part of the first harddisk to the second one using the 'dd' command. This ensures the MBR, GRUB boot loader, and the boot partition is also available on the second harddisk, in the case the system has to start from that disk. The count you specify should be the sector start count of the second partition (you took note of earlier), minus one.
dd if=/dev/sda of=/dev/sdb count=112454
Now you can restart the system using reboot and if all is well, it should boot to the desktop.
Let's emulate a failure on the second harddisk. You can do this by just unplugging the SATA cable of the second harddisk while the system is running. Open a terminal and give the command: ll -alR / which will show a very long list of all the files on the system. While this list is running, yank the SATA cable from the second harddisk. Notice that after a few seconds, the listing suddenly stops. Ubuntu didn't crash, the mouse is still responsive, but the file system is having troubles at the moment. After two minutes Ubuntu notices that the second harddisk is permanently missing, marks it as such, and continues listing the files. The system continues to operate, albeit on only one disk.
Do a cat /proc/mdstat. You'll see output like this:
md0 : active raid1 sda2 62468672 blocks [2/1] [U_]
Will the PC be bootable with only one harddisk operational? Perform a sudo reboot. During the boot, which might take a little longer than usual, you might be presented by a GRUB boot selection menu; in that case, choose the first option. Next, a textual screen might appear asking you if you want to boot with the degraded RAID array. Answer 'Yes'. After this, the system comes up with the Ubuntu desktop.
More or less the same happens if you'd have disconnected the first harddisk instead of the second one. Repairing/rebuilding a broken RAID1 array is currently out of the scope of this article. An alternative would be rebuilding from scratch and restoring the data files from backup.
Checking the RAID
A useful command that will tell you the status of the RAID is cat /proc/mdstat
It's output is something like:
md0 : active raid1 sda2 sdb2 62468672 blocks [2/2] [UU]
The UU means both RAID1 components are 'Up'. If one disappears, it's 'U' will change to and underscore ('_').