11.23
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/fstab file:
/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.
edit: Fixed problems with double dashes!.
Nice.
With regards to performance of software RAID: Using RAID 1 or 0 is usually ok with software because reads/writes are not very intensive operations. With RAID-5 though there is considerably more computation involved in writes, as parity data must be generated. This can become a serious issue if you’re using the array in a system which hammers it’s discs.
You won’t have any problems with it on a desktop, but for a server I would use hardware RAID given the choice, every time.
Nice write up!
You might want to add a bit on the STRIDE parameter for EXT2/3 filesystems on RAID5.
There’s a small writeup in the RAID Howto
http://unthought.net/Software-RAID.HOWTO/Software-RAID.HOWTO-5.html
Cheers
Ferg
Sorry that was a mirror, not the original link:
http://tldp.org/HOWTO/Software-RAID-HOWTO-5.html#ss5.11
Cheers
ferg
Thanks a lot for the guide, it was very helpfull to me, thanks to it I set up my raid in no time
There is only one concern, when I reboot my computer all the changes disappear, any ideas?? i am desperate!!!!
Hey alaka,
You can re-assemble your RAID array using mdadm –assemble /dev/md0 /dev/sda1 /dev/sdb1 /dev/sdc1 (etc)
Also try: Try “dpkg-reconfigure mdadm” as root
An option must be given to set the mode before a second device is listed
This an error Im geting
Just to make sure I understand… you did this from a fresh install or from and existing install? I’m hoping to do this from an existing install (Dapper). Is that possible?
Hey grzesiekp:
Don’t copy the commands from this web page and paste into the console; it doesn’t work (don’t know why) and you get that stupid error.
Type the same command by hand, and make sure you write all options with two hifens (–) instead of just one.
In case of doubts, read mdadm’s man page.
Great write up! Very clear instructions. Only 1 question: will I be able to create a RAID like this AND boot from it? If so, is there anything more needed to do so?
Thanks!
@SupaRice – No, you can only boot from a software RAID-1 array. So the solution is to create a single raid-1 array for /boot over 2 of the drives (~100mb should do) and then the rest of the discs can be used for the RAID-5 array.
All good stuff, but just because I’m a pedant… ;)
Technically, you cannot boot from any RAID devices created with mdadm. In practice you can boot from a RAID-1 because it’s simply a mirror. The boot loader doesn’t know the difference. But, technically you’re not booting from the array, merely one of the devices in the array.
For added reliability you can specify both devices in the array device as a possible boot disc (assuming BIOS as GRUB are setup for this), which will allow the system to boot even if one of the drives fails.
Just building my raid5 array. But when I try to do the next step I get “Permission denied” sudo echo “DEVICE partitions” > /etc/mdadm/mdadm.conf
What’s the problem.
It took 4 days to build on 2 dual p3 733. If I shut down my computer will I lose something?
^^ Tommi I get that error too on a ubuntu edgy box.
any ideas?
Silly question (but just to be sure), have you tried becoming root before issuing the command (as opposed to using sudo):
sudo su -
Yes Jonny I did use sudo.
I think there is something wrong with my array. How can I start building it again?
tommi@files:~$ sudo mdadm –create –verbose /dev/md0 –level=5 –raid-devices=4 /dev/hde1 /dev/hdk1 /dev/hdg1 /dev/hdi1
mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 64K
mdadm: Cannot open /dev/hde1: Device or resource busy
mdadm: Cannot open /dev/hdk1: Device or resource busy
mdadm: Cannot open /dev/hdg1: Device or resource busy
mdadm: Cannot open /dev/hdi1: Device or resource busy
mdadm: create aborted
Something is using my disks.
Just building my raid5 array. But when I try to do the next step I get “Permission denied” sudo echo “DEVICE partitions” > /etc/mdadm/mdadm.conf
What’s the problem.
It took 4 days to build on 2 dual p3 733. If I shut down my computer will I lose something?
I think u need sudo to do that however that line was not nessicary, all it did was cause errors with mdadm. The second line is tho :)
It took me about 3 hours to build a raid 5 array with 5 200gb IDE disks so i dunno.
I’ve got the same error about the denied permission.
But I make the thing work by editing mdadm.conf file myself ; here it is :
DEVICE /dev/sda1 /dev/sdb1
DEVICE /dev/sda5 /dev/sdb5
ARRAY /dev/md0 level=raid1 num-devices=2 devices=/dev/sda1,/dev/sdb1 auto=md
ARRAY /dev/md5 level=raid1 num-devices=2 devices=/dev/sda5,/dev/sdb5 auto=md
Hope it could help ;)
A big thx btw for that guide.
This guide is so great. I have been planning a RAID setup for a long time. Just tested it with a few small spare partitons, following this guide. It took only minutes and worked absolutely fine. Now, before I migrate my whole system to RAID 5, allow me to ask a question: The Software RAID HowTo on TLDP mentions the setting up of a file called /etc/raidtab in addition to using mdadm. Should I set up such a file, too? What is it good for in relation to mdadm?
Thanks a lot for any answer I might get. Great job, Jonny!
@labiculum – I did not need to.
Very good guide, wish this would come up in the top 10 on google, but the top 1000000000 just suck !
I am failing to mount the array in the end mind, getting error, something on the lines off:
bad superblock / wrong fs type
Nice guide and works OK for me. Have my configuration up and running with 4 500GB SATA drives. There is one problem I have. My RAID array sometimes does not initialise upon a (re-)boot. Any clue?
This is a good guide except that it does not cover what to do when a disk in your array fails. Isn’t that why one would use RAID5? If a disk fails, how does the data get rebuilt onto the replacement. This is something I would want test before really implementing an array like this.
This is a good guide except that it does not cover what to do when a disk in your array fails. Isn’t that why one would use RAID5? If a disk fails, how does the data get rebuilt onto the replacement? This is something I would want test before really implementing an array like this.
instead of
sudo echo “DEVICE partitions” > /etc/mdadm/mdadm.conf
sudo mdadm –detail –scan >> /etc/mdadm/mdadm.conf, I recommend
sudo mdadm –detail –scan –verbose > /etc/mdadm.conf
the verbose version prints a list of the devices formatted for use in your array. This will get you out of one difficulty that I had when I adopted this tutorial: I didn’t use physical drives as array components, but partitions instead. With the DEVICE partitions directive, when it reassembled the array, it assembled the physical drives as a partition, instead of the sd*2 partitions that I wanted. This obviously destroyed the array.
Did anyone figure out the “Device or resource busy” error listed above? I’m trying this with two disks on my second mobo IDE bus, and the other four (raid 6) on an ide raid controller card, two to a bus. Only one of the 6 disks doesn’t report ‘Device or resource busy’ when trying to create the array, and
sudo fuser /dev/hdf
returns 0 results for the ‘busy’ disks.
df
does not show them either.
How else can I investigate? Thanks
Great guide
Running into a problem on this command
sudo echo “DEVICE partitions” > /etc/mdadm/mdadm.conf (enters just fine)
sudo mdadm –detail –scan >> /etc/mdadm/mdadm.conf (comes up with: Unknown keyword “DEVICE
I know I am soo close I just need to mount it.
Hey all,
Since building my first array i have learnt of a few things, you might want to talk about chunk size’s. also please fix up the – - turning into — somehow :P
You should be enclosing code/commands/pre-formatted text in a <code> block:
<code>then –it –works –fine</code> becomes:
then --it --works --fineThanks Chris, I have edited the article – sorry to anyone who fell foul of that one :)
No probs..
“It’s good to learn, ‘cos knowledge is power!”
Cameron,
regarding the “device busy” message you’re seeing.. I got the same message and issued “mount” to see what was mounted. The partitions I was creating automounted after I formatted them. I had to manually unmount them all before I could run mdadm to build the array.
also, if your partition editor is still running, I believe that might cause an issue as well.
good luck…
I had exactly the same problem with the “device busy” message. I found that the device mapper needed switching off, I ran “sudo dmsetup remove_all”, and it then allowed me to use the disks in question. Am in the middle of following this tutorial, fingers crossed it will all work.
while issue commad
sudo echo “DEVICE partitions” > /etc/mdadam/mdadm.com “permission denied” will return;
better login as root and then issue
echo “DEVICE partitions” > /etc/mdadam/mdadm.com
mdadm –detail –scan >> /etc/mdadm/mdadm.conf
if still error returns kill running process “mdadm” and “mdadm_raid” and reissue these commands.i was able to complete raid5 on 3×80gb IDE-HDs.
sorry small typing mistake
pls. refer
echo “DEVICE partitions” > /etc/mdadam/mdadm.com
to
echo “DEVICE partitions” > /etc/mdadam/mdadm.conf
yeah man. thx a lot for this guide.
I created partitions manually since I don’t have GUI (ubuntu-server).
1.) sudo fdisk /dev/sd[x]
2.) sudo mkfs.ext3 /dev/sd[x]1
3.) mdadm :-)
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdd1[4] sdc1[2] sdb1[1] sda1[0]
1465151616 blocks level 5, 128k chunk, algorithm 2 [4/3] [UUU_]
[====>................] recovery = 23.5% (114799232/488383872) finish=77.3min speed=80524K/sec
cheers,
alex
Hello,
First thanks for taking the time to write this up, and thanks to all for the valuable comments. I made a stupid mistake and setup Ubuntu server, got Subversion, MySql, Bugzilla, etc, etc installed and then it was decided that this box will actually be USED as the development environment. Had I known this beforehand I would have gone through all the RAID steps prior to messing around with all of the other stuff…. My question is (and I saw it asked – but not responded to – sorry if I missed it – or it’s stupid :) ). I’m hoping to setup RAID… something like this after I’ve already installed the OS (and everything else) Is that even possible?
Thanks,
Jeremy
I am also in the works of creating a raid how to http://forumubuntusoftware.info/viewtopic.php?f=7&t=226 I do not however cover Raid 5; but personally find it very informative for the end user as I try to make it not so technical, please provide me feedback on my forum.
Thanks,
TheeMahn
Great tutorial!
i just finished it and everything is working just like it should.
unfortunatly i have already filled up all the space on my newly created /dev/md0 volume so i need to set up another volume.
how can i create a second volume? would it be just changeing /dev/md0 to /dev/md1 ???
what about the mdadm.conf? do i need to create a new one or change something in the current one??
thanks
morgan
Great guide. Thank you for the information!
I know hardly anything about Linux, and this helped me out immensely when migrating a RAID-1 Windows 2000 server to a donated Linux box. Thanks so much!
[...] Great tutorial – http://bfish.xaedalus.net/?p=188 [...]
Hi, I followed this post mut made a raid-6 instead, using four 500G sata drives. Everything worked out fine, device had synced and I copied 200GB of stuff on it.
Then I tried to simulate a disk failure by unplugging one of the drives. After powering back up, I found that the device was not mounted, syslog showed message “DeviceDisappeared event detected on device /dev/md0″ and cat /proc/mdstat said that the raid device was “Inactive”. I don’t get it – shouldn’t the system recover from one drive missing situation by itself?
Willard wouldnt you have to plug in the hotspare for it to rebuild?
Im interested in doing this but I will be using SATA hard drives for all drives is this difficult:
2 x 160Gb in RAID 1 as OS
3 x 500Gb in RAID 5 as storage (hopefully expanding to 6 or more drives)
I may also need to using a SATA card because my motherboard only has 4 SATA connectors. Would this be a problem?
Bar: using a SATA card is no problem. Just make sure you pay attention to the device naming.
Hi Chris,
What do you mean by that?
I just meant that you should make sure you know which physical drives map to which device names (e.g. /dev/sda). With lots of drives/multiple arrays this can get confusing. It might be worth labelling the drives (marker pen/sticky labels/etc) – I do this on my own machines.
Thanks for the tip Chris
Hi Bar,
yes but as I understand it, a single hard drive missing shouldn’t still stop the array from working. After detecting a missing device, mdadm should automatically rebuild the array in degraded mode.
But I got the issue fixed, I had to start the inactive array once with mdadm -R /dev/md0. Don’t really know why, but after that it has been working independently when I have tested removing and adding drives. Oops, forgot to say: great howto Jonny!
[...] make raid array on already installed system [...]