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!.
Hey dude, im a dumb idiot from South Africa, this write-up is so exact even i got it right
regards
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
To fix this error you need to unmount each hard drive, if you are using gparted you need to remove the lock that is nex to the name
How do you activate a raid 5 array if it has become inactive?
md3 : inactive sda[0](S) sdd[3](S) sdc[2](S) sdb[1](S)
2930297856 blocks
gparted say that may harddisks are unallocated.
everything worked fine untill one of my old ide hardrive (from witch I copied old data) was disconected, after this my raid 1 system array broke, but got it to work after moving it to my secondare IDE scannel.
Thanks – I used this guide this weekend to put together my 3×750GB RAID 5 and it worked great!
I had this problem with Ubuntu 8.10 until I turned off the RAID array in the Bios. For me the FakeRAID firmware was set to RAID0 with 2 160gig SATA drives. I could partition to my hearts content in gparted, but would not let me format, in “system-config-lvm” I could not format or create a RAID (thought drives were already mounted) and with dmadm I got the “Cannot open /dev/hde1: Device or resource busy” error. I just had to delete the array in FakeRAID firmware and I used LVM GUI software and was up and running in a few minutes. (I wasted 2 days up to that point)
GerryH
Good article, but please, one “loses” data. “Loose” is the opposite of “tight”. Thanks.
Hello,
THanks for the guide. i do have 1 question. can i set up this raid from a live disk. then install the os onto it? i tried but the install didnt see the raid only the individual drives. any help would be great.
Great Job on the write up! Being a newbie to Linux I took some notes that may help the other folks out there that are just getting started as well. I am using Ubuntu
* The “Device or resource is busy” is most likely due to your volume being mounted. You can unmount them directly from the desktop by right clicking on the volume and selecting unmount or use the following commands:
sudo fdisk -l (This will list the drives that -l is an L)
sudo umount /xxxx/xxxx (to unmount the drive)
* The “Permission denied” error is usually due to not being at the Root (I don’t fully understand this but the command to change yourself to the Root is):
sudo -s (Change to Root)
* Now if you want to create your own directory just issue this command
mkdir /var/media (I’ve used /var/media as an example if you want to put it in the same place as listed in the article)
* Finally, editing that Fstab file was tough for me but I found this command that opened it up nicely and was easy to edit. (For some reason I couldn’t save the Fstab using mousepad, I’m sure it’s a permissions issue but this method I show below works great)
sudo nano /etc/fstab (just make the changes and hit CTRL-X to save them…then Y….then ENTER)
Hope this helps someone…
DMac
great guide, have been looking for a step by step guide.
Am currently building my arry, but its going to take 9 hours.
Can i pause the array build and restart later?
TB
Just to add to DMac’s comment that yes nano is a great little text editor that you can use from a terminal window, it is pretty intuitive and there is help readily available.
*But* if you are working on a system file (as here) you should always start it with the -w parameter because otherwise it auto-wraps lines and this can cause the files to fail.
So in this case the line should be:
sudo nano -w /etc/fstabDominic
[...] bfish.xaedalus.net » Software RAID 5 in Ubuntu with mdadm (tags: ubuntu raid raid5) [...]
[...] After a quick shout to the Ubuntu Community I found this wonderfully simple guide on setting up a RAID5 array using mdadm: http://bfish.xaedalus.net/?p=188 [...]
Thanks for this great guide, I have only one question. Why does your fstab entry have pass=3?
I am a newb at fstab but from what I’ve found this is usually set to 0, 1, or 2… I can’t seem to figure out why you used ‘3′ ?
TiA,
-BassKozz
Thanks for this great guide, I have only one question.
when I use gparted, to formatted md0 device an error occourred, and didn’t format this Volume.
how it made?
thank you
thanks so much! Great instructions, thanks so much.
-mark-
How I can add a disk to array? I have 4 1tb drives so I have 3tb of space. But how I can add more 1Tb disks?
and
How I can change the disks to bigger ones? My 4x 1tb to 4x 2tb?
[...] Software RAID 5 in Ubuntu with mdadm [...]
I am doing a recovery but it is EXTREMELY slow 966K/sec. (4*750GB)
And just now it seem to have frozen my computer so I had to restart it.
BassKozz: pass=3 is invalid I think. You can probably use 0.
Ignazio: don’t use gparted to format md0 because it sees individual disks, not the array. As suggested, use the command line:
# sudo mke2fs -j /dev/md0
Jeepo: use the mdadm –add and –grow commands to expand your RAID volume. For example:
# sudo umount /dev/md0
(unmount the array)
# sudo mdadm –add /dev/md0 /dev/sde1
(add the new partition)
# sudo mdadm –grow /dev/md0 –raid-devices=5
(reshape the array)
# sudo fsck.ext3 /dev/md0
# sudo resize2fs /dev/md1
(the filesystem needs to be expanded to fill the new space, assuming ext3 is used)
# sudo mount /dev/md0
To swap 1TB drives with 2TB drives, you have to “mdadm –fail” each 1TB drive first before –add and –grow, sync and repeat the process, I think.
Fredrik: try:
# echo 50000 > /proc/sys/dev/raid/speed_limit_min
This would impose a 50MB/s minimum speed. The default was somehow set at 1 MB/s, causing problem for many.
^typo in above post: resize2fs /dev/md1 should read resize2fs /dev/md0
[...] Eine Liste der Festplatten anzeigen fdisk -l Alle Platten partitionieren und Filesystem anlegen: (statt [x] den Buchstaben des Laufwerks eintragen) sudo fdisk /dev/sd[x] sudo mkfs.ext3 /dev/sd[x]1 mdadm sudo apt-get install mdadm Raid erzeugen, hier Raid5 auf sda, sdb und sdc sudo mdadm –create –verbose /dev/md0 –level=5 –raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1 root werden sudo su mdadm.conf erzeugen/schreiben echo “DEVICE partitions” > /etc/mdadm/mdadm.conf mdadm –detail –scan >> /etc/mdadm/mdadm.conf Filesystem auf dem Raid erzeugen sudo mke2fs -j /dev/md0 fstab bearbeiten sudo nano /etc/fstab folgendes einfügen: /dev/md0 /var/media auto defaults 0 3 Ordner erzeugen, in dem das Raid gemountet wird: mkdir /var/media Raid mounten: mount /dev/md0 Alle Infos kommen von: bfish.xaedalus.net [...]
Great guide, thanks so much!
Two questions please: Under Ubuntu 8.10, do I really need to appended the mdadm scan to mdadm.conf? It seems to auto-configure just right?
Then, I somehow can’t write to the raid array (”you don’t have permissions”)—I’m not sure where to configure those.
Thanks again!
Permissions problem solved, silly me, turning into a wimp due to excessive visual UI use.
Just upgraded to Ubuntu 9.04. It doesn’t recognize the md0 device any more :-( GParted says the four soft raid partitions I created are unknown. Under Ubuntu 8.10 everything worked fine—it was a simple server setup, following exactly your steps, for disks each with an equal-size partition for the softraid, formatted in ext3. Any thoughts? Thanks!
bayave42 – had the same happen to me wiht a raid1 set up – check https://bugs.launchpad.net/ubuntu/+source/mdadm/+bug/330298 for a solution
Great stuff – this was the guide I needed to rebuild my 4-disk RAID5 array after I’d cleaned out the dross from the nvidia dmraid setup (2-disk RAID0) which was on the machine when I bought it…
Ian
[...] http://bfish.xaedalus.net/?p=188 [...]
Good article, quickest steps to done I’ve found. Keep up the good work.
awesome awesome awesome! I’ve used this howto for a long time and I keep coming back to it. It’s great! Step by step, works every time. Thank you for such a great writeup!
I got up to the part where you add /dev/md0 /var/media auto defaults 0 3 in the /etc/fstab file. As soon as I mount /dev/md0 it gave me an error “mount: moint point /var/media does not exist” What am I missing?
Awesome guide! I’m a complete noob on linux, but thanks to your guide I’ve got my NAS almost up and running! Only need to read up on configuring smb, but setting up the RAID 5 array was a piece of cake thanks to your guide!
Great guide, worked perfectly.
Same question as Marvin. “I got up to the part where you add /dev/md0 /var/media auto defaults 0 3 in the /etc/fstab file. As soon as I mount /dev/md0 it gave me an error “mount: moint point /var/media does not exist” What am I missing?”
@clifford / Marvin:
Sounds like the directory you’re trying to use as a mountpoint doesn’t exist. Try:
sudo mkdir /var/mediaMoved from Vista to Ubuntu…
Moved from Vista to Ubuntu…
Hello, This is a great play by play and will use it on all of my corporate server installs.
I have several machines that I am building. Each machine will have 3-1tb drives.
So does this mean that I should prepare a drive with an Ubuntu install (say a 160gb drive) build the raid5 config., then move my Ubunutu install from the 160gb to the Raid5 array?
I see that all the drives that will be added to the raid 5 arrary will be formatted, so I can not put the operating system on any of those. Maybe I should purchase an installation drive for each machine as the primary drive with the installation on it.
Or can I just install a new instance of Ubuntu onto the Raid5 array after it has been built.
Meaning can I move the 160gb drive off of the machine then create a new build from there?
I want to use the 160gb drive to setup all of my server’s.
Any help would be appreciated.
Reason is I need consistancy from machine to machine.
Thanks again for the great info..
ZJPS
[...] http://bfish.xaedalus.net/2006/11/software-raid-5-in-ubuntu-with-mdadm/ [...]
[...] Software RAID 5 in Ubuntu with mdadm [...]
In ubuntu 9.10 when i would reboot the raid wasn’t being mounted, and if i ran ’sudo mount -a’ i got the error ‘mount: /dev/md0 is already mounted or /var/media is busy’
To fix it change the /etc/fstab file entry
From: /dev/md0 /var/media auto defaults 0 3
To: /dev/md0 /var/media ext3 defaults 0 0
and it worked fine
Thanks for the tutorial! I’ve got everything working fine in Xubuntu, but when I do a “watch cat/proc/mdtats” it tells me “sh: cat/proc/mdstat: not found”
Did I miss something?