Creating a software RAID array on an already installed Ubuntu 11.04

Let's say you got confused by a misleading fake-RAID feature on an HP Blade server and you decided to ignore that the Ubuntu installer was telling you it found 2 disks while it was supposed (if it was actual hardware RAID) to be detecting only one. And let's say you are lucky to have 3 disks, and you only one to use two as the RAID array (and they do not contain your operating system, i.e. the / partition). You might wonder: "And now what? S**** you, HP!" (that last bit is if you left panic get you, of course). Let's also say that you had an installation of a large production web application on /var/www, which is what you wanted to be on RAID, but that, by an incredibly lucky turn of events, it is just small enough to fit on what's left of your first disk... Well fear no more! There's a solution and I tested it for you... First take a safe copy of what is on that partition that you wanted to be in RAID, and put it on the disk that will not change. Also, unmount that partition. If it was /var/www, use:
$ sudo umount /var/www
If the system says it's busy, shutdown Apache and make sure no user is currently in a terminal in /var/www... (use lsof |grep /var/www for example). If you happen to read this Ubuntu RAID guide between the lines, you'll catch it talks about "mdadm", a software to manage multi-disk arrays. As you know, installing software on Ubuntu (or Debian) is dead-easy:
sudo apt-get install mdadm
Then let's say you wonder how to use it... As you know, getting documentation in English on Linux is dead easy:
man mdadm
Then you're up for a lot of reading, unless you don't really care about the details and you want to try it quick. This is what you would then do, considering your two unused disks (or "devices" for the geeks) are /dev/sdb and /dev/sdc (you can get an idea from "ls /dev/sd*" or "df" or "fdisk /dev/anything-you-can-think-about" :
$ sudo mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
...where /dev/md1 is the new multi-disks device you'll create, --level is the RAID type you want to use (1 for simple mirroring), and --raid-devices is the number of disks you will have inside your multi-disks device. You can then confirm that you don't care about the partitions it may find on these. Then launch
$ sudo mdadm /dev/md1
To check if that virtual disk exists. It should give you something like:
/dev/md1: 465.76GiB raid1 2 devices, 0 spares. Use mdadm --detail for more detail.
Now you've got a device, but that doesn't give you a partition...You'll have to create one with "fdisk":
$ sudo fdisk /dev/md1 fdisk> n fdisk> p fdisk> 1 fdisk> <enter> for default fdisk> <enter> for default fdisk> w
But the partition is not formatted. As you might know, formatting a partition (let's say in EXT4 because you like modern stuff moderately) on Debian/Ubuntu is dead-easy:
$ sudo mkfs.ext4 /dev/md1
You now have an EXT4 partition mounted on a RAID1 mirroring device. Almost done: you want to mount the partition in your file system. Let's say as /var/www (that directory must exist and it'd rather be empty):
$ sudo mount /dev/md1 /var/www
Now, you want this mounting to happen on its own when you reboot, right? To do that, you need to update your /etc/fstab. If you had already something mounted there before, chances are you will find its line is already in /etc/fstab . For example, if you had one of the disks alone mounted as /var/www, you'll have this kind of line:
UUID=244b687f-f8d9-4a48-986a-8a1a8a8d33bd /var/www        ext4    defaults        0       2
Right? Well, you'll need to edit that line to change the device ID now... But how do you get that UUID? No problem, just launch:
$ ls -l /dev/disk/by-uuid
You'll see your /dev/md1 listed there, along with its UUID. Just copy that UUID and replace the older one on the /var/www line of your /etc/fstab. Done. Now I recommend you reboot to check everything is mounting correctly, before you delete your safe backup file. Well, that wasn't too hard, was it? I actually believed I would have to reinstall the whole Ubuntu, but having a very slow connection for the update, I preferred not to. Seems like I won this round! Thanks to all the guys involved in these cool projects! You really made my day.

Comments

maybe I'm using a different version or something, but the flags for the mdadm command needed double hyphenation. ie --create and --level.

In reply to by YW

Permalink

they are double hypenated here, just doesn't look like it because of the web rendering. If you look at –raid-devices=2 the length of dashes are different.

In reply to by YW

Permalink

That's right, I get tricked all the time as well. You can't just copy and paste. Sorry. I don't know how to fix it, though.