Wednesday, July 22, 2009

Setting up Linux Software RAID in Ubuntu 9.04

I decided that I needed some data redundancy for my media collection and personal documents.   I was running zfs for awhile, with 2, 1 gig drives, but then I decided to switch to Linux Software Raid.

I found 2 pretty articles on how to setup and configure a Linux Software RAID.

In my particular setup, I had 2, 1 gig disks, but I wanted to re-use those while switching to the software raid setup.  So, do that, I basically, stole a disk from the zfs raid setup, partitioned it, created the linux raid, formatted it, and then copied the zfs drive to the newly created linux raid device.  Pretty easy right?  Well, it was, but i'll into some extra details about the complete process.

I had 2 sata devices, /dev/sda, and /dev/sdb

First, install mdadm
# sudo apt-get install mdadm

When you install mdadm, it will want to install postfix.  I don't know why, but it does.  I just answered the basic install questions and carried on.

Next, prepare 1 of your 2 disks for a linux raid.
# sudo fdisk /dev/sda

If it doesn't have a partition, then create one (n), and then change the partition type to Raid Auto Detect (t then fd)

You should now have a /dev/sda1 device.  You can verify this using the print command (p) and then write (w) your changes.

Next, we create the raid array.  In my case, I will eventually have 2 disks, but I wanted to create the array with just a single disk (/dev/sda1) for now, and then add in the other disk (/dev/sdb1) later.

Creating the array was pretty simple.
# sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sda1 missing
Notice that for the second device, I used the word, "missing".  This is special reserved word for mdadm and it is used to acknowledge that you are creating an array, but you do not yet have all the devices.  You need at least 1 device, which I did have.

Now that you've created the array, we need to format it.
# sudo mkfs.ext4 /dev/md0

That should take a few minutes, and we are now ready to mount the device.

# sudo mkdir /media/Transfer
# sudo mount /dev/md0 /media/Transfer

We now have a mounted device under /media/Tranfer.  We'll use that to transfer all the files from our old device onto the new device.  In my case, my zfs drive was mounted under /home, so I used rsync to sync the zfs mounted drive with my newly mounted linux raid device

# sudo rsync -av /home /media/Transfer

This will take a long time, depending on your disk size.  In my case, it took a couple of hours.

Once the transfer is complete, I then added in my other zfs drive to my linux raid.  This required that I partition the drive in the same way that I partitioned my other drive.  Basically, using fdisk, I created a new partition, and then set the partition type to fd (Linux Raid)

Once that was done, all I needed to do was add the new drive to my existing linux raid array.

# sudo mdadm --manage /dev/md0 -a /dev/sdb1

Once you do this, you'll notice your hard drive light will remain on or flash very quickly.  Basically the Raid Array is now copying all data from the source disk to the newly added disk.  This will take a couple of hours as well, but the system is fully usable during that time.

You can check the status of the Raid at any time by issuing the command
# cat /proc/mdstat

The output will be something like,

Personalities : [raid1]
md0 : active raid1 sda1[2] sdb1[0]
      976759936 blocks [2/1] [U_]
      [========>............]  recovery = 44.4% (434131840/976759936) finish=135.6min speed=66645K/sec


Finally, don't forget to update your /etc/fstab to reflect that your new mount device is /dev/md0 and not whatever it used to be.  In my case, it was a matter of changing the device to that /home was mounted as device /dev/md0