Categories


Converting Raid-1 to Raid-5

So, I happened across this article today, written by a Scott Wallace, I’ve copied the text here in case his site goes down for whatever reason. Note, I haven’t tried this as of yet, but in the future when I fill up my drives, I’ll likely be able to just buy another 500 gig drive and convert my raid-1 array to a raid-5 array instead of having to buy another 3 whole drives.

Converting RAID1 to RAID5 with no data loss | scott.wallace.sh

When you record and download lots of TV, movies, music, etc. it can chew up the disk space pretty quickly. If you don’t keep on top of your DVD burning you will end up running out of disk space, like me. That’s okay, I have two 200GB drives mirrored in a RAID1 array. If I break the mirror and concatenate the drives I could use all 400GB of space available to me… but if one drive died, I would lose everything that was not backed up.

Buying two more drives isn’t the answer as I only have one more PATA drive connection available. Perhaps I could back everything up, buy two bigger drives, install a new RAID1 array and copy everything back over. That means giving up two perfectly servicable 200GB drives.

If only I could add a third drive and convert the RAID1 array to a RAID5 array. Then I would get the full 400GB of space, and still retain the redundancy. Yeah, right.

… Then I stumbled across this blog entry in which a guy creates some experimental loopback devices, creates a RAID1 array and then converts it to a RAID5 array with no data loss. I was intrigued.

The theory says that the RAID5 algorithm, when applied to 2 disks only, ends up looking like a RAID1 array except for the RAID metadata. If you overwrite the RAID1 metadata with the RAID5 metadata, mdadm should recognise the 2 disk RAID5 array and not mess with the contents. Once the metadata is updated, you can then add a third partition to the array and grow the RAID5 array to utilise it. All that remains is to then resize the filesystem to fill the new space.

The main question is, am I brave enough to try it?

You bet I am!

Of course, everything is caveated with the usual “back everything up before you attempt this procedure” and, like a good boy, I borrowed a 400GB external drive from work and rsync’d all the important stuff across… and, with heart in mouth, followed the procedure…

Boot from a Fedora Core 6 rescue CDROM and get to a command prompt. You must ensure you have a recent kernel (> 2.6.17) and that you have a recent version of the mdadm software:

# uname -a
Linux localhost.localdomain 2.6.18-1.2798.fc6 #1 SMP Mon Oct 16 14:54:20 EDT 2006 i686 unknown

# mdadm --version
mdadm - v2.5.4 - 13 Ocotober 2006

Stop the array:

# mdadm --stop /dev/md0

Overwrite the RAID1 metadata with the RAID5 metadata:

# mdadm --create /dev/md0 --level=5 -n 2 /dev/hda1 /dev/hdb1
mdadm: /dev/hda1 appears to contain an ext2fs file system
size=1946592K  mtime=Sat Apr 14 07:18:32 2007
mdadm: /dev/hda1 appears to be part of a raid array:
level=1 devices=2 ctime=Sat Sep 17 16:17:45 2005
mdadm: /dev/hdb1 appears to contain an ext2fs file system
size=1946592K  mtime=Sat Apr 17 07:18:32 2007
mdadm: /dev/hdb1 appears to be part of a raid array:
level=1 devices=2 ctime=Sat Sep 17 16:17:45 2005
Continue creating array? y
mdadm: array /dev/md0 started.

At this point the RAID software decided it wanted to rebuild the array. Uh-oh, there goes my data… I quickly mounted /dev/md0 and had a look… all my data is still intact! Oh well, let the software do it’s thing. Who am I to argue?

Add in the third, new partition:

# mdadm --add /dev/md0 /dev/hdd1

So far, so good. Once the rebuild is complete, grow the RAID5 onto the new partition: (NB: use the –backup-file option in case the grow is interrupted. It will allow a safe recovery.)

# mdadm --grow /dev/md0 --raid-disks=3 --backup-file=/mnt/tmp/raid1-5.backup.file
mdadm: Need to backup 128K of critical section ..
mdadm: ... critical section passed.

I’m impressed that I’ve had no problems so far. The reshaping of the RAID5 from a 2 disk to a 3 disk array takes quite a while (about 6.5 hours for around 200GB of raw data) but the filesystem resize shouldn’t take anywhere near as long:


# e2fsck -f /dev/md0

# resize2fs -p /dev/md0

Apart from the modification of the RAID metadata, the whole operation can be done “online”. I chose to do it from single-user/rescue mode as I wanted to make sure there was no data loss. If you’re not too bothered then you could leave the whole thing up and running.


5 Comments Add Yours ↓

  1. 1

    I can confirm that it indeed does work and is reliable. Did this on two RAID arrays a little while back, and am liable to do it again soon when I get new drives.

    Setting up the 2x500Gb originally as RAID1 -> RAID5 was easy… adding the 3rd 500Gb drive later took a LOT of time for the array to rebuild (was a slow Dual PIII/1.2Ghz machine mind). but otherwise no problems.

  2. 2

    Thanks for the post Kelly – I unfortunately don’t get on my blog very often, but am glad this worked for you as well. I’m actually looking to upgrade some old 160s to 1TB drives, which will be interesting, if things go well I’ll post another guide here.

  3. Ron #
    3

    Awesome, thanks for this. I just used it to convert my 2x2TB RAID1 into a 4x2TB RAID5. Now to find stuff to fill it. haha

  4. 4

    For anyone else’s edification, here is my experience. My RAID 1 volumes were created recently, like 2010, using a very modern version of mdadm on Fedora. I am using GPT partitioning, but as far as I know, this really shouldn’t matter. I have several RAID 1 volumes. The first one I tried to convert was about 30GB, and I did it this way, all 100% online:

    Convert into a RAID5 array:
    mdadm –grow /dev/md3 –level 5

    Add a hot spare(s):
    mdadm –manage /dev/md3 –add /dev/sdc3

    Add a disk to the array, to get a clean state for the array:
    mdadm –grow /dev/md3 –raid-devices 3

    Online resize to fill partition
    resize2fs /dev/md3

    I think I got some kind of weird warning/error at some point there, but it worked fine, and was able to be mounted during rebuild / reshape. Then, as I got more adventurous, I tried this with my 400GB partition. I got some message like, “Unable to set to raid 5″. (That’s not verbatim… but something like that.) I looked it up on Google and had very few results, none of which helped me. I tried unmounting it and doing the same thing, but no. It just wouldn’t do anything with my RAID partition, and /var/log/messages really didn’t help either. Then, I stumbled upon this site, which gives a slightly older approach, that requires unmounting the volume and stopping it, prior to newly creating the RAID 5 one. Ok, fine, I want/need to do this, so I went into init 3 and tried it out. The part where you run this (in my case):

    mdadm –create /dev/md5 –level=5 -n 2 /dev/sda5 /dev/sdb5

    … started rebuilding the array in RAID5, but I got e2fsck errors when I tried to run it, and was unable to mount it, whereas this blog says it mounted just fine. It said something about how the superblocks claimed the filesystem (?) was slightly larger than the partition, er something. I thought, “OK, well it will probably fix itself once it’s done rebuilding. Then, shortly after I thought that, I suspected otherwise, and my latter suspicions were right. e2fsck didn’t like it when I ran it with -n during rebuild or normally, but after the rebuild I ran resize2fs and it resized to fill the partition which was just a couple more blocks. (I can only assume the extra space has something to do with RAID 5. The resize that doubles the space is still yet to happen….) Then, it mounts just fine, all RAID1 data intact. So, I boot back into graphical mode and proceed with the rest of the process outlined here, with the “–grow” and stuff. Now I’m golden. I run another resize2fs to make it fill the partition, and it starts showing more and more space available as I run “df” to check it. Point being, you may run into an issue where it’s just not going to mount properly until you resize it, even during the initial setup or RAID 5.

    So, I now have my RAID1 —> RAID5 transition that worked fine, but needed some slight adjustements from this (admittedly old) process described here. Not sure if it’s because of a newer mdadm or what, but yes, it can be done. So, good luck. ;)

    Infinality

  5. Richard Thomas #
    5

    I came across this site and another when searching for this topic on Google. The other site uses a very similar method. There are some comments on the other site that give some serious warnings that this is not a safe way to do things with some of the later versions of the RAID software. It seems that likely the best way to do things is to break your mirrored array, use one of the disks and the new disk to create a 2-disk RAID 5 array, copy the data and then add the remaining RAID 1 disk into the RAID 5. The other site is: http://www.arkf.net/blog/?p=47



Your Comment