Saturday, October 20, 2012

Increase/grow the capacity/space of a linux Raid 1 partition without losing data

Today I found I needed to increase the capacity of my data partition (on a Raid 1 array) to fit a hard drive upgrade. Below are a couple of good references on how that can be done and some arguments to mdadm need to be adjusted to work for me
http://www.flyonthenet.it/blog/?p=71
http://johnlange.wordpress.com/tech-tips/linux/expanding-linux-software-raid1-with-an-ext3-file-system/
The safe and lengthy procedure is as follows (assuming the old array consists of drive A and B and the new array with increased capacity consists of drive C and D):
  1. Back up all the data on the Raid 1 array (e.g. /dev/md2) and umount the array from the system
  2. Remove the partition on one old drive (A) from the Raid 1 array that needs to grow capacity
  3. Partition a new hard drive (C) as Linux raid autodetect/fd type with the wanted capacity and add that partition to the Raid 1 array. Mdadm will start recovery/resync of the array.
  4. Let mdadm finish resyncing the array and the array is now clean again (very important, otherwise data loss). Make sure the array status has [UU]
    cat /proc/mdstat
    md0 : active raid1 sdb1[1] sda1[0]
    76798592 blocks [2/2] [UU]
  5. Remove the partition on the remaining old drive (B) from the array.
  6. Partition the second new hard drive (D) as Linux raid autodetect/fd type with the wanted capacity and add that partition to the Raid 1 array. Mdadm will start recovery/resync of the array.
  7. After resyncing is complete, the underlying block device of the array is now of an appropriate size to hold the file system with increased capacity. Grow the capacity of the array and let the resyncing finishmdadm --grow /dev/md2 --size=max
  8. Resize the ext file system
    e2fsck -f /dev/md2
    resize2fs /dev/md2
    e2fsck -f /dev/md2
  9. Mount the array


    Alternatively, a quick and dirty solution to grow the capacity of the array (metadata version 0.90) on the same pair of drives (A&B) is shown below
  • Back up all the data on the Raid 1 array (e.g. /dev/md2) and umount the array from the system
  • Increase the size of the partitions of the raid devices on the existing hard drives (A&B)
  • Create new raid array with mdadm (note the metadata argument to specify the same metadata as in the previous array is critical. Otherwise e2fsck will not find the superblock since latest mdadm uses metadata version 1.2 as default)
    mdadm --create /dev/md2 --level=raid1 --raid-devices=2 /dev/sdc1 /dev/sdb1 --metadata 0.90
    If mdadm prompts that the partitions already contain an ext2fs file system, hit "y" to continue.
    Continue creating array? y
    mdadm: array /dev/md2 started.
  • Check the file system on the array and fix the errors. Then resize the file system.
    e2fsck -f /dev/md2
    resize2fs /dev/md2
    e2fsck -f /dev/md2
  • Mount the array