Live Resizing of RAID5 Arrays in Linux

A friend of mine asked about the benefits of software RAID versus hardware RAID in Linux, and I proceeded to voice my support for the former. It was then brought up that online resizing was one of her concerns, and I set about to figure out how to do just that. These instructions were crafted in a CentOS 5 virtual machine, and come with no warranty or guarantee of any sort; I put them here on the off chance that they might help someone.

  1. First, I started by adding four additional SCSI drives to my CentOS 5 virtual machine. I then booted the VM up, and partitioned the disks, creating one partition that encompassed the entire drive and setting its type to “fd”:

    for i in a b c d ; do echo "; ; fd ;" | sfdisk /dev/sd$i ; done
    
  2. Then I created the new RAID5 array with four devices, none of which should be kept as spares:

    mdadm --create /dev/md0 --spare-devices=0 --level=5 --raid-devices=4 /dev/sd{a,b,c,d}1
    
  3. Third, create a physical volume for LVM to use:

    pvcreate /dev/md0
    
  4. After that, I made a volume group:

    vgcreate vg0 /dev/md0
    
  5. Then I made a logical volume in that group using all free physical extents:

    lvcreate --extents=100%VG --name=storage vg0
    
  6. Sixth, slap a file system on that guy:

    mke2fs -j /dev/vg0/storage
    
  7. Mount it, put some data on it, and enjoy.

After you’ve been using the array for a while (or, after it’s finished resyncing if you’re just following along at home), you add some new drives to the machine that you want to add to the array. I shut my VM down, added some additional virtual disks to it, and powered it back up; your steps would presumably be somewhat similar. After the system came back up, I added the new drives like so.

  1. Partition them as with the other drives in the set:

    for i in e f ; do sfdisk -d /dev/sda | sfdisk /dev/sd$i ; done
    
  2. Add them to the RAID array:

    mdadm --add /dev/md0 /dev/sd{e,f}1
    
  3. Expand the array to encompass all the disks (note that this will probably take a while, so you might want to do it overnight):

    mdadm --grow --raid-devices=6 /dev/md0
    
  4. Expand the LVM physical volume to include the new space:

    pvresize /dev/md0
    
  5. Then expand the logical volume as well:

    lvresize --extents=100%VG /dev/vg0/storage
    
  6. Finally, grow the file system:

    resize2fs /dev/vg0/storage
    

With the exception of powering down to add the new drives, nothing had to be brought offline, and if your storage controller supports the hot-adding of devices then you may not even have to that (though the scope of doing so is beyond this tutorial, since I don’t have ready access to such hardware).

Hope that helps, and please let me know if you see any inconsistencies or run into any problems!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>