04

As a follow on to my post about choosing RAID, here is a quick guide to setting up RAID-5 using mdadm. In theory this should apply to any distro that has mdadm, but I was already running Ubuntu 9.10 (just upgraded yesterday actually!).

Note: RAID-5 requires a minimum of 3 drives, and all should be the same size. It provides the ability for one drive to fail without any data loss.

Usable space = (no. of drives - 1) * size of smallest drive

In my set up I started with 3x 1.5TB drives, giving 3.0TB usable space. I have now grown it to 4x 1.5TB drives, giving 4.5TB usable space.

Required software

To create, format and resize the array we need to use various filesystem manipulation tools, but they should all come with Ubuntu. The only software you should need to install is mdadm.

sudo aptitude install mdadm

Initial setup

1. Preparing the drives

Before we jump into creating the actual RAID array, we first need to prepare partitions for the array to use. This can be done using a GUI tool such as GParted (available in the Ubuntu repositories), but I prefer using the terminal.

To get a list of available drives/partitions:

sudo fdisk -l

This will output, for each drive you have, something along the lines of:

Disk /dev/sda: 1500.3 GB, 1500301910016 bytes
255 heads, 63 sectors/track, 182401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000f1f05

There may well be slightly more information available too if you already have some partitions made. From here note the name of the drives you wish to use (for example, /dev/sda). For each drive, create a partition and mark it as a RAID partition.

sudo fdisk /dev/sda

This will open up fdisk, the partition manager. If you already have any partitions on the drive you should first delete them (obviously this will erase any data on them!). To create one partition that is the size of the whole drive:

Command (m for help): n
Command action
	e	extended
	p	primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-182401, default 1): [blank]
Last cylinder or +size or +sizeM or +sizeK (1-182401, default 182401): [blank]

When choosing the first and last cylinder simply hitting return will use the default values (which are probably the values you actually want).

Next we will mark the partition as being part of a RAID array, allowing mdadm to automatically detect it:

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid auto)

So far our changes haven’t actually been written to disk, so finally, issue the command to write the changes to disk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Remember to repeat this step for each drive you wish to use in the array.

2. Creating the array

To create the array, we use the mdadm create flag (who’d have guessed that!). We also need to specify what RAID level we want, as well as how many devices and what they are.

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

The verbose flag tells it to output extra information. In the above command I am creating a RAID-5 array at /dev/md0, using 3 partitions. If you already have a RAID array set up then you may need to use /dev/md1 for example. The number of partitions you are using, and their names will probably be different, so do not just copy and paste all of the command above. Note that the partition name is something like /dev/sda1, whereas the drive name is something like /dev/sda; the 1 refers to the partition number.

While the array is being built you can view its status in the file /proc/mdstat. Here the watch command comes in handy:

sudo watch cat /proc/mdstat

This will output the contents of the file to the screen, refreshing every 2 seconds (by default). While the array is being built it will show how much of the “recovery” has been done, and an estimated time remaining.

Now that we have set up the array, we need to edit the mdadm configuration so it knows how to reassemble it when the system boots.

sudo nano /etc/mdadm/mdadm.conf

There should already be some sample configuration here:

# by default, scan all partitions (/proc/partitions) for MD superblocks.
# alternatively, specify devices to scan, using wildcards if desired.
DEVICE partitions

# auto-create devices with Debian standard permissions
CREATE owner=root group=disk mode=0660 auto=yes

# automatically tag new arrays as belonging to the local system
HOMEHOST

# instruct the monitoring daemon where to send mail alerts
MAILADDR root

# definitions of existing MD arrays
ARRAY /dev/md0 level=raid5 num-devices=3 UUID=31283299:0c118170:8b4eb9a8:4e935bf2

The ARRAY line probably doesn’t already exist. It is used to describe an array (such as the one we just created). While similar, your array will not be the same as mine above so do not just copy and paste it! Luckily, mdadm provides a utility to fetch this line exactly (by looking for partitions marked as belonging to a RAID array, as we did when preparing the drives):

sudo mdadm --detail --scan

If you add the –verbose flag here it will also output the devices which are part of the array. Because we marked the partitions as being part of a RAID array mdadm can find them automatically so this isn’t required in our case, but it may be if you have more than one RAID array in your machine.

Now, I decided to wait for the array to finish syncing before moving on, but you should be able to continue and format the array while it syncs if you wish. In my case, building the array with 3x 1.5TB drives took around 6 hours.

3. Creating and mounting the filesystem

Now that the array is built we need to format it. What filesystem you choose is up to you, but I would probably recommend ext3. Note: For my array I chose to use ext4. So far I have not had any problems, but ext4 is still fairly new and there are still various bug reports going around mentioning data loss [bugzilla.kernel.org] (which I conveniently didn’t notice until after!).

sudo mkfs.ext3 /dev/md0

This will take a while, especially if your array is large.

If you chose to use ext2/3/4 you should also be aware of reserved space. By default ext2/3/4 will reserve 5% of the drives space, which only root is able to write to. This is done so a user cannot fill the drive and prevent critical daemons writing to it, but 5% of a large RAID array which isn’t going to be written to by critical daemons anyway, is a lot of wasted space. I chose to set the reserved space to 0%, using tune2fs:

sudo tune2fs -m 0 /dev/md0

Next we should add the array to the fstab, so that it will automatically be mounted when the system boots up. This can be done by editing the file /etc/fstab. For more detailed information, see the fstab Wikipedia article.

sudo nano /etc/fstab

Your fstab should already contain a few entries (if it doesn’t something is wrong!). At the bottom add a line similar to the following:

/dev/md0	/mnt/raid	ext4	defaults	0	0

I chose to mount my array on /mnt/raid, but you may well wish to mount it somewhere else. If the folder you chose doesn’t exist you will need to create it. As I said earlier I chose to use ext4, but here you will need to enter whatever filesystem you chose earlier.

Now, mount the array.

sudo mount -a

This will mount anything mentioned in the fstab that isn’t currently mounted but should be (hopefully your array!).

You should now have a working RAID-5 array! Next I would strongly suggest you investigate how to look after and monitor it, being able to cope with one drive failure is no use if you don’t notice when it fails and let a second fail!

Growing

One of the advantages of software RAID is the flexibility it gives you, that would normally only be available from high end (expensive) RAID cards. This includes the ability to grow an existing array (only for certain RAID levels), which means if you run out of space you can easily plug in a new drive and keep going.

1. Growing the array

Growing a RAID-5 array with mdadm is a fairly simple (though slow) task. First you will need to prepare the new drive in the same we we prepared the initial drives (step 1, above). To start the actual growing of the array we then add the new drive to the array as a spare:

sudo mdadm --add /dev/md0 /dev/sdd1

Then we grow the array onto this device. Because I had 3 drives before, the new drive obviously makes 4. Make sure to change this to whatever number you now have!

sudo mdadm --grow /dev/md0 --raid-devices=4

As when creating the array, we can follow the progress by checking the file /proc/mdstat.

sudo watch cat /proc/mdstat

Again, I decided to wait for the operation to finish before attempting to grow the filesystem, but you should be able to do it while the array syncs.

2. Growing the filesystem

Before we can grow the filesystem we need to unmount it:

sudo umount /dev/md0

Now that it is unmounted we can run a filesystem check to make sure everything is in order:

sudo fsck.ext3 -f /dev/md0

If any issues arise, let it attempt to fix them for you.

Once the filesystem has been checked we can perform the resize. If you decided to use something other than ext2/3/4 this may be done differently, but for the ext2/3/4 filesystems we use the resize2fs tool.

sudo resize2fs /dev/md0

This will automatically grow the filesystem to the new size of the device.

You can now remount the drive and start filling it up again!

Sources

If you spot any errors, or have any problems then please leave a comment and let me know!

20 Comments

Add Your Comment
  1. Hi Jamie,

    Thanks so much for tis tutorial. I’ve been using to try over and over to setup a whole new software RAID5 array on a whole new machine, containing 3 1,5TB Western Digital SATA drives, but to no avail. Things first start to go wrong for me with a different detail line from “mdadm –details –scan”:
    ARRAY /dev/md0 level=raid5 num-devices=3 metadata=00.90 spares=1 UUID=02229c0c:b3bf7f25:ead3296a:a14b1f73

    Most notably, I get the “metadata” attribute and the “spares” attribute.

    The “mkfs.ext3 /dev/md0” still runs okay (so it seems), but with the “tune2fs -m 0 /dev/md0”, the first errors start happening:

    tune2fs 1.41.9 (22-Aug-2009)

    tune2fs: Attempt to read block from filesystem resulted in short read while trying to open /dev/md0

    Couldn’t find valid filesystem superblock.

    Then when I want to mount the array with “mount -a, I get the following error:
    mount: wrong fs type, bad option, bad superblock on /dev/md0,

    missing codepage or helper program, or other error

    In some cases useful info is found in syslog – try

    dmesg | tail or so

    The suggested command “dmesg | tail¨ returns:

    [40245.334901] Buffer I/O error on device md0, logical block 1

    [40245.334903] Buffer I/O error on device md0, logical block 2

    [40245.334906] Buffer I/O error on device md0, logical block 3

    [40245.334911] Buffer I/O error on device md0, logical block 0

    [40245.359610] Buffer I/O error on device md0, logical block 0

    [40245.359621] Buffer I/O error on device md0, logical block 1

    [40245.359625] Buffer I/O error on device md0, logical block 2

    [40245.359628] Buffer I/O error on device md0, logical block 3

    [40245.359631] Buffer I/O error on device md0, logical block 4

    [40404.587948] EXT3-fs: unable to read superblock

    Lastly, “mdadm –detail –verbose /dev/md0″ returns:

    mdadm: metadata format 00.90 unknown, ignored.
    /dev/md0:
    Version : 00.90
    Creation Time : Tue Nov 17 23:07:58 2009
    Raid Level : raid5
    Array Size : 2930271872 (2794.53 GiB 3000.60 GB)
    Used Dev Size : 1465135936 (1397.26 GiB 1500.30 GB)
    Raid Devices : 3
    Total Devices : 3
    Preferred Minor : 0
    Persistence : Superblock is persistent

    Update Time : Wed Nov 18 08:24:39 2009
    State : clean, degraded
    Active Devices : 1
    Working Devices : 2
    Failed Devices : 1
    Spare Devices : 1

    Layout : left-symmetric
    Chunk Size : 64K

    UUID : 02229c0c:b3bf7f25:ead3296a:a14b1f73
    Events : 0.14

    Number Major Minor RaidDevice State
    0 0 0 0 removed
    1 8 33 1 active sync /dev/sdc1
    2 0 0 2 removed

    3 8 49 – spare /dev/sdd1
    4 8 17 – faulty spare /dev/sdb1

    I have tried creating this array 3 or 4 times now. Only the first time I had a bit of more luck, but after reboot things started going wrong. I have also formatted the individual disks and ran gparted disk check on them, to check if all disks were okay. No errors were reported.

    What to do? I’m lost! Any help would be greatly appreciated.

    Pascal.

    Pascal Lindelauf on Wed 18th Nov '09 at 7:40am
  2. Hey Pascal.

    The format of the metadata attribute seems to have been broken for a while now, mdadm reports it as 00.90, but expects it to be 0.90 in the configuration file (why it’s so picky I don’t know). Your array should work fine without it (indeed I think it’s often left out of auto-generated config), but if you want to include it just remove the leading 0.

    When you first add a drive to an array, it is marked as a spare until the resync is done, when it will no longer be spare. I probably wouldn’t add the spares line to the config.

    Did you let the array sync before continuing to try and format it, or just go ahead right away? From what I’ve read it should be fine to go ahead right away, but I didn’t try this so can’t confirm… Looking at the output you posted from mdadm it looks like it thinks one of your drives is faulty (/dev/sdb1), which is odd since you said you checked them individually and they were okay.

    I would probably suggest starting again (if it isn’t a problem), and after creating the array watch /proc/mdstat and wait for it to finish syncing the drives (probably overnight, it takes a bit!) before trying to create the filesystem. If one of the drives is faulty you should see it in /proc/mdstat.

    Jamie on Wed 18th Nov '09 at 9:17am
  3. Hi Jamie,

    It seems that mdadm was telling the truth: /dev/sdb appears to be faulty indeed. I ran smartctl and found several read errors, while the other two reported no errors. I’m gonna return that defective new drive immediately. I got good faith that everything will work fine once I get it replaced.

    Thanks! Pascal

    Pascal Lindelauf on Thu 19th Nov '09 at 6:51am
  4. Got my new drive in and all works fine now! Thanks again for the great tutorial!

    Pascal Lindelauf on Sun 22nd Nov '09 at 11:56am
  5. [...] Software RAID-5: using mdadm in Ubuntu 9.10 [...]

    Create and Mount RAID-5 Array - Jeremys Linux Blog on Tue 12th Jan '10 at 1:59am
  6. Thanks for this guide. Worked perfectly for me!

    windsok on Thu 14th Jan '10 at 12:40pm
  7. Thanks for the walk through! I am going to redo my home file server. I am switching from a raid 1 setup to raid 5. This should make it a breeze.

    rob on Fri 29th Jan '10 at 7:47pm
  8. Thanks for the guide!
    i have one drive ubuntu server 9.10 (no raid)
    live server, is it posible to add two drive same size
    and tern it to raid 5

    indu on Mon 8th Feb '10 at 1:48am
  9. I’d just like to tell you that this is the most simple, clear, and well written tutorial for the most common layman’s use case for RAID that I’ve ever found on the net. I was dreading setting up a new raid array and the amount of deciphering that it would take to read up on the things I had forgotten since I set up the last one. You reduced this time to about a minute and a half. Thanks!

    J on Fri 12th Feb '10 at 5:12pm
  10. What brand/model drives did you use?

    Doug on Fri 12th Feb '10 at 7:54pm
  11. Thanks for the replies :)

    @indu: Yes it is. If you don’t want to lose the data on the existing drive you can create an array in degraded mode using just 2 drives, then copy your data over, then add the third drive to get it out of degraded mode back to normal. I’ve never done it though so I can’t help with how sorry.

    @Doug: I used 1.5TB Seagate Barracuda 7200.11 drives, but if I was looking to do it now I would probably go with 1.5TB WD Caviar Greens.

    Jamie on Fri 12th Feb '10 at 8:01pm
  12. Have you had any issues of the drives falling out of the array? Reading the reviews at NewEgg some have experienced drives falling out with the Seagate’s.

    I have been trying to do exactly the same setup as you with the 1.5TB WD Caviar Greens but they just do not work in RAID. One drive always falls out at build time.

    Doug on Fri 12th Feb '10 at 8:19pm
  13. Not yet *fingers crossed*. I’m now actually running a total of 6 drives in the array, haven’t had any problems yet…

    Jamie on Fri 12th Feb '10 at 8:22pm
  14. Don’t worry about the drives falling out. From what I have read that does not affect mdadm, and is an issue with hardware raid cards.

    Managed to return my WD Green’s and exchange them for the Seagate 11′s now I am having no problems. Cheers for the info, it was a relief to know what drives could actually work.

    You might like to look into tweaking your file system for raid 5 too.
    http://www.linuxfoundation.org/collaborate/workgroups/linux-raid/raid_setup#Options_for_mke2fs

    Doug on Mon 15th Feb '10 at 3:04am
  15. Great tutorial! Worked like a charm for me. I only used the steps from ‘Creating the array’ and on. I first formatted my drives with GParted. The 5% reserve was included when I first formatted my three hdds. My RAID is all set and running. Is there any way I can get rid of the 5% reserve on each individual hdd without having to rebuild the entire RAID? These are 1.5TB hdds. It took about five hours to build and I would like to skip the process if possible. Any help is greatly appreciated.

    Thanks, Mike

    Mike on Tue 16th Mar '10 at 6:21am
  16. Jamie,

    Thanks for the great tutorial. Written in a very informative and concise manner. Managed to get my raid 5 with 3 x 1.5 WD Caviar Green drives up and running. Only issue was that after all the resync and create ext4 partition, I ran fdisk -l to check the array and the message showed no valid partition on md0.

    Was worried that this will come back and haunt me in the future and tried to search for a solution thru the web but found most people saying that this is normal.

    Do you have the same message as well in your system?

    Dennis on Fri 7th May '10 at 12:37pm
  17. Yup mine says the same, as far as I’m aware it’s normal and nothing to be worried about.

    Jamie on Fri 7th May '10 at 1:16pm
  18. Thanks for the clarification :)

    Dennis on Tue 11th May '10 at 2:58pm
  19. am i missing something, this seems to be what I want to do, but what I had been reading said that /boot could not be in the raid5 array.

    I really hope this is not the case.

    Wanted to build a 10.04 ubuntu server with 3 disks in raid5 with a 4th waiting as a spare

    AndyS on Tue 27th Jul '10 at 5:00am
  20. This guide is assuming you are using the array to store media or something other than the OS on it. As you’ve read, storing /boot on a software RAID partition wont work – since the RAID is done in software it wont be constructed/mountable at the time /boot is needed.

    Jamie on Tue 27th Jul '10 at 12:12pm

Your Comment