Setup of RAID5 without losing data

I have here a server (Debian Linux of course) which is storing its data on a 1TB hard drive. I want to upgrade the storage to a RAID5 using 4x 1TB drives. I’d like the existing drive to be a part of the RAID array so I only need to buy 3 new drives, but I don’t want to lose the 600G or so of data on it.

The solution ? Set up a degraded array, copy the data and then add the original disk to the array.

Put simply, a degraded array is a RAID array with one or more drives missing. RAID5 can operate with any one drive down. This is very useful if a drive fails, you can replace the faulty drive without losing any data. So we are going to pretend the drive with the data on it is ‘faulty’ so it won’t be part of the RAID until later when we miraculously ‘fix’ it.

On my machine, the new drives are /dev/sde, /dev/sdf and /dev/sdg. The drive with the data is /dev/sdh1. First we partition the new drives. You’ll need to type these two commands for each new drive:

echo ",,L" | sfdisk /dev/sde

sfdisk --change-id /dev/sd1 1 fd

You don’t have to use sfdisk, as long as you set up the partitions somehow and set them to type 0xFD (Linux RAID).

Now to create the degraded array. This is the magic bit.

mdadm --create /dev/md3 --level=5 --raid-devices=4 --spare-devices=0 /dev/sde1 /dev/sdf1 /dev/sdg1 missing

The magic word missing stands in for the drive with the data on it.

Next format the RAID array, mount the data drive and copy the data

mkfs.xfs /dev/md3

mount /dev/md3 /home/adam

mount /dev/sdh1 /mnt

cp -prv /mnt/* /home/adam/

This will take a while so you might want to do it in a screen session.

Finally, after the data is copied, we can add the data drive to the array. This is the bit where we can lose everything if it goes wrong so check your data is all on the RAID before proceeding.

umount /dev/sdh1

sfdisk --change-id /dev/sdh 1 fd

mdadm --manage /dev/md3 --add /dev/sdh1

I’m really pleased at this neat trick. I’ve now got a shiny RAID array with a total storage of 3TB. It all worked so well, I just had to blog about it!

2 thoughts on “Setup of RAID5 without losing data

  1. Thanks for the awesome tutorial!
    This provided me exactly what i needed when already having 1 TB Samsung and making a raid 5 with three devices!

    Only one problem
    after the last command
    mdadm –manage /dev/md3 –add /dev/sdh1
    i actually did
    mdadm –manage /dev/md3 –add /dev/sdh
    without the ’1′ at the end..

    The array is now however recovering.
    Is this a problem? Can i remote that drive later when the array is clean and not degraded and add it anew correctly?

  2. Good you blogged this. I wanted to do this but wasn’t sure and when I found this post I’m little bit more sure now ;)

Comments are closed.