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:
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
- 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

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

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
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 ).
Jan • 27 Aug 2015
Hi, all is OK till the command: mkfs.ext3 /dev/sda1 I´ve got: mke2fs 1.42.5 (29-Jul-2012) mkfs.ext4: inode_size (128) * inodes_count (0) too big for a filesystem with 0 blocks, specify higher inode_ratio (-i) or lower inode count (-N). What is wrong pls? This is fdisk -l: root@xubuntu:/home/xubuntu# fdisk -l Disk /dev/sda: 60.0 GB, 60022480896 bytes 255 heads, 63 sectors/track, 7297 cylinders, total 117231408 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Device Boot Start End Blocks Id System /dev/sda1 63 112454 56196 83 Linux /dev/sda2 112455 117231407 58559476+ fd Linux raid autodetect Disk /dev/sdb: 60.0 GB, 60022480896 bytes 255 heads, 63 sectors/track, 7297 cylinders, total 117231408 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Device Boot Start End Blocks Id System /dev/sdb1 63 112454 56196 83 Linux /dev/sdb2 112455 117231407 58559476+ fd Linux raid autodetect Disk /dev/sdc: 16.0 GB, 16030597120 bytes 64 heads, 32 sectors/track, 15287 cylinders, total 31309760 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000bc84b Device Boot Start End Blocks Id System /dev/sdc1 * 32 31307775 15653872 83 Linux Disk /dev/md0: 54.0 GB, 53984755712 bytes 2 heads, 4 sectors/track, 13179872 cylinders, total 105438976 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/md0 doesn't contain a valid partition table
Nick • 19 Jan 2012
Hi, Could you explain what this does? ln /dev/md0 /dev/sde Regards, Nick