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:
sudo -i (enter your pass word) apt-get update && apt-get upgrade -y apt-get install mdadm ssh parted gdisk
Next we’ll need to partition those newly installed drives
This will return a bunch of information , you need to be looking for lines like this:
Disk /dev/sdb doesn't contain a valid partition table Disk /dev/sdc doesn't contain a valid partition table Disk /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:
parted -a optimal /dev/sdb GNU Parted 2.3 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel gpt (parted) mkpart primary 1 -1 (parted) align-check alignment type(min/opt) [optimal]/minimal? optimal Partition number? 1 1 aligned (parted) quit
Use SGE to clone the partition table to your other three drives. (This will save you some typing)
sgdisk --backup=table /dev/sdb sgdisk --load-backup=table /dev/sdc sgdisk --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.
mdadm --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
watch 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.
echo "DEVICE partitions" > /etc/mdadm/mdadm.conf echo "HOMEHOST fileserver" >> /etc/mdadm/mdadm.conf echo "MAILADDR email@example.com" >> /etc/mdadm/mdadm.conf mdadm --detail --scan >> /etc/mdadm/mdadm.conf
You can view your shiny new toy like this:
mdadm --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.
mdadm --monitor -m firstname.lastname@example.org /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 :
mkfs.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%.
tune2fs -m 0 /dev/md0
Edit /etc/fstab so that the array mounts on boot:
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:
You should now be the proud owner of a RAID array. Start filling it up with all your super cool data.blog comments powered by Disqus