Home > Best practices, Debian, Documentation, English, open-source, Tech Crunch, ubuntu > Creating a software RAID array on an already installed Ubuntu 11.04

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.

  1. February 23, 2012 at 2:19 pm

    it helped me out thanks

  2. mark
    November 18, 2012 at 5:20 pm

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

    • Mike
      February 28, 2013 at 11:19 pm

      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.

      • Yannick Warnier
        February 28, 2013 at 11:33 pm

        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.

  3. January 3, 2014 at 8:25 pm

    Ha. This is fun to read. I hope it works. :)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: