Setting up RAID

Setting up software RAID in Ubuntu.

Alright so for those of you who haven’t had the “joy” of any sort of hardware failure yet, let me just say that you should hope it is anything but a hard drive. I had one go out on me a few months back and as a result had to restart a number of computationally heavy jobs in order to get finished up. As a result I went ahead and implemented a couple of local and remote solutions for future problems.

Local Solution:RAID Arrays

What is RAID ?

RAID (Redundant Array of Independent Disks) , colloquially, is a term used for data storage schemes that divide and replicate data across multiple drives. It allows your operating system to utilize multiple drives as a single drive. Schemes are denoted by a number following RAID (i.e. RAID-0, RAID-5, etc). Each of these schemes provides different levels of reliability, performance, and capacity. For the sake of this post I’ll just be discussing RAID-5 as it’s what we’re using to combat potential drive failures. I chose RAID-5 because its versatile , and gives minor disk read performances, and can survive a single hard drive failure. For a nice tutorial on the different types of RAID, how they look, and the pros and cons for each check out : raid.edu.

Setting up Software RAID in Ubuntu

First , we’re using software raid. It’s not optimal from a performance standpoint but it does what we need , is much less expensive to implement, and as I don’t generally have huge loads on the CPUs, it won’t kill my overall performance.

Installing your Hard drives - first you’ll need at least 3 hard drives in order use this scheme. I won’t go into how to physically install those on your machine just make sure you have them installed. Start up your computer open a terminal and issue the following commands:

1sudo -i
2(enter your pass word)
3apt-get update && apt-get upgrade -y
4apt-get install mdadm ssh parted gdisk

Next we’ll need to partition those newly installed drives

1fdisk -l

This will return a bunch of information , you need to be looking for lines like this:

1Disk /dev/sdb doesn't contain a valid partition table
2Disk /dev/sdc doesn't contain a valid partition table
3Disk /dev/sdd doesn't contain a valid partition table

Keep those drive letters in your head ( or better yet on a scrap of paper).

Partition your first drive with the following commands:

 1parted -a optimal /dev/sdb
 2GNU Parted 2.3
 3Using /dev/sdb
 4Welcome to GNU Parted! Type 'help' to view a list of commands.
 5(parted) mklabel gpt 
 6(parted) mkpart primary 1 -1
 7(parted) align-check                                                      
 8alignment type(min/opt)  [optimal]/minimal? optimal                       
 9Partition number? 1                                                       
101 aligned
11(parted) quit 

Use SGE to clone the partition table to your other three drives. (This will save you some typing)

1sgdisk --backup=table /dev/sdb
2sgdisk --load-backup=table /dev/sdc
3sgdisk --load-backup=table /dev/sdd

Create the Array. Keep in mind that depending on the number of hard drives you’re using and the address of those hard drives this command may change so do not just copy this verbatim.

1mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sd[bcd]1

Alright here is where you should probably just leave and get a beer. Assuming you’re using high capacity drives this could take several hours. You can watch the progress of the build with the following command

1watch cat /proc/mdstat

After that’s complete , we need to edit the mdadm config file so it knows how to assemble the raid on startup.

1echo "DEVICE partitions" > /etc/mdadm/mdadm.conf
2echo "HOMEHOST fileserver" >> /etc/mdadm/mdadm.conf
3echo "MAILADDR youruser@gmail.com" >> /etc/mdadm/mdadm.conf
4mdadm --detail --scan >> /etc/mdadm/mdadm.conf

You can view your shiny new toy like this:

1mdadm --detail /dev/md0

In order to make sure you receive notifications from your system on failures you need to make sure that your email server is set up. Test it with the following command , if it isn’t set up do so.

1mdadm --monitor -m username@email.com /dev/md0 -t

You should receive an email.

Optimizing your array. Go to this calculator here, and enter your information and copy the information it returns back to you. Your command should look something like :

1mkfs.ext4 -b 4096 -E stride=128,stripe-width=256

You should also take this time to edit the amount of reserved space on your drive. This stops the user from filling up all the space on a drive preventing daemons from writing to it. The default is 5%, and assuming that you’re using this just for data storage , daemons shouldn’t be using it anyways so I chose to set mine at 0%.

1tune2fs -m 0 /dev/md0

Edit /etc/fstab so that the array mounts on boot:

1vim /etc/fstab

Add the following line to your file:

/dev/md0        /storage            ext4        defaults        0        0

Save it and exit. Make your directory that it mounts to:

mkdir /storage

Mount everything.

mount -a

You should now be the proud owner of a RAID array. Start filling it up with all your super cool data.