links

contact

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.

sudo su

Be sure you use clean disks (without GPT's on them). If you're unsure, wipe them first:

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.

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:

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.

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.

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 2013

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 2014

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

David • 8 Aug 2014

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 ).