Software RAID 5 in Ubuntu with mdadm

Software RAID in Linux is a great way to gain massive amounts of disk space which are required for storing digital media, with the added security of data redundancy incase one of the disks in the array fail. Many people still frown upon software RAID insisting that hardware raid is the better option. This may have been true when your 486 CPU was busy trying to load win.exe, but now we have access to multicore CPU’s there is little point in offloading such a ’small’ task. Software Raid in Linux, via mdadm, also offer many advanced features that are only available on the most high end of RAID controller cards such as expanding existing RAID-5 arrays, RAID Level Migration and Bitmap Caching (similar to having battery backed up cache). Finally, Software RAID can move with you, if you decide to change motherboards, or your PCI RAID controller fails then your array may be at risk – not so with Software RAID.

This guide details setting up software RAID 5 on Ubuntu / Debian using mdadm post install. It does not cover everything you need to know about RAID and the knowledge in this document is by no means extensive – please check out the further reading link for more information at the end of this article.

And as a final note before we get started, please remember that no RAID solution is a viable replacement for good backups! If your data is so valuable that you can’t afford to loose it (either due to hardware, or user error) then make damn sure it’s backed up on a remote machine / device.

Step One: Prepare the Disks

as root, issue issue an fdisk -l to bring up a list of available disks.

In my example I have 3, 300gb Hard Disks, sda1, sdb1 and sdc1 that I wish to combine into a RAID-5 volume. The disks are currently unformatted. The first set is to format them as ext3 and set the RAID flag, this can be done easily with gPartEd. If you do not have ‘Gnome Partition Editor’ on your System->Administration Menu, install it by issuing

sudo apt-get install gparted

In the Gnome Partition Editor application, select each disk in turn and opt to format it to ext3. Remember, you will loose all data on the device. Once the format is complete, right click on each new “volume” you have created and select “Manage Flags” – tick the ‘RAID’ flag to indicate the disk will become part of a RAID set.

Once you have your three disks set up, fdisk -l should now indicate that those disks are ready to become members of your new RAID 5 array.
Step Two: Creating the md Device
If you don’t already have mdadm (Mirrored Device Admin) installed, issue:

sudo apt-get install mdadm

You can now create your RAID volume (md0, if this is to be your first) by issuing:

sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1

To explain the switches, --verbose will display more information on the screen as the RAID is created (which is useful for debugging and gaining help from others if you become stuck, --level=5 dictates that we want to create a RAID-5 volume, --raid-devices=3 dictates that there are to be three members of our raid array, you then need to list the disks you wish to assign. For more information, you can issue: mdadm --help-options

Upon pressing return, your RAID device (md0) will be created, if you get the following error (as I did with a fresh install of Edgy-Eft):

mdadm: error opening /dev/md0: No such file or directory

Then you need to add the parameter --auto=md to the end of your mdadm create raid command.

Once the build is under-way, you can monitor its progress by issuing sudo watch cat /proc/mdstat, watch is a very useful linux command which will refresh the output of a command ever 2 seconds for you. When you are done watching, you can press CTRL+C to escape back to the command line.

The last stage before you create the file system on your new raid disk is to create your mdadm.conf file. The mdadm.conf file contains information about your RAID array that the mdm daemon will need on boot to “re-assemble” your array ready for mounting. To create your mdadm.conf file, issue the following:

sudo echo "DEVICE partitions" > /etc/mdadm/mdadm.conf
sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf

Step Three: Creating the Filesystem and Mount Point
Now that you have created a logical RAID 5 volume, it’s time to format it so that we can put some files on it. I personally chose to format my new RAID volume with plain ext3, if you’re feeling a bit more adventurous you could format it with reiserfs or one of the slightly less common disk formats – it’s up to you. If, like me, do you opt for ext3, then you can simply issue the following command to format the disk (note: you can format the disk whilst the array is still sync’ing).

sudo mke2fs -j /dev/md0

This will take a few minutes (especially if your array is re-sync’ing). Once it has completed you need to edit your fstab to make the RAID-5 volume automatically mount on the next boot. I chose to mount mine in /var/media so that all users on my system will have access to it. As a result I added this line to my /etc/fstabfile:

/dev/md0 /var/media auto defaults 0 3

Now all that’s left to do is issue a mount /dev/md0 and, fingers crossed, your RAID 5 array should now mount to your chosen mount point. Now before you go off ans starting copying files to it, I recommend that you let it finish re-syncing. Also, remember, like all new things, it’s a good idea to “run it in” first, keep backups of all data that you put on it for the first few weeks so that you can handle an emergency should one happen!

Further Reading:

This short guide is by no means everything there is to know on the subject of RAID and mdadm. Once your RAID 5 array is up and running there are still plenty more tweaks and obstacles to overcome in the future, as a result I will leave you with the following links in the hope that they may prove useful.

  • The Linux RAID Mailing List Archives
  • Wikipedia Entry on RAID
  • Growing RAID 5 volumes with mdadm

edit: Fixed problems with double dashes!.

Learn More