michielovertoom.com

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.

Typographical ornament

Preparing the disks

Become root. This saves you from having to type 'sudo' in front of every command.

sudo su

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:

cfdisk /dev/sda

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.

Typographical ornament

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
Typographical ornament

Install Ubuntu

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.

Typographical ornament

Post-install

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.

Typographical ornament

Disaster scenario

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[0] 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.

Typographical ornament

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[0] sdb2[1] 62468672 blocks [2/2] [UU]

The UU means both RAID1 components are 'Up'. If one disappears, it's 'U' will change to and underscore ('_').

Comments

Nick • 19 Jan 2012

Hi, Could you explain what this does? ln /dev/md0 /dev/sde Regards, Nick

wds • 8 Apr 2012

怎么没发现有交换分区SWAP呢?

wds • 8 Apr 2012

why not you create swap area?

Roland • 15 Feb

Is this a sequential or parallel RAID1? What I'm asking is does the write to the primary drive complete BEFORE it starts writing to the mirror (high fault tolerance) OR does it perform the write in parallel (extremely low fault tolerance but high speed).

baboon • 6 Jul

why not make the sd{a,b}1 raids as well?

David • 8 Aug

I am interested in high fault tolerant storage systems. Does anyone have a link or a book reference to "the write to the primary drive complete BEFORE it starts writing to the mirror (high fault tolerance)" mentioned by Roland? It sounds fascinating, even though as far as I know no one does that -- everyone uses "write in parallel". (Does a journaling file system on top of "write in parallel" give high fault tolerance? https://en.wikipedia.org/wiki/journaling_file_system ).

Leave a comment

name (required)



content last edited on July 10, 2011, 18:25 - rendered in 50.02 msec