Replacing A Hard Drive In A Software RAID1 Array In Order To Grow It

Let's start by checking the current status of the RAID system:

[philipp@lion ~]$ cat /proc/mdstat 
Personalities : [raid1] 
md1 : active raid1 sdc3[0] sda3[1]
      482213888 blocks super 1.2 [2/2] [UU]
      bitmap: 0/4 pages [0KB], 65536KB chunk

md0 : active raid1 sdc2[0] sda2[1]
      115175424 blocks super 1.2 [2/2] [UU]
      bitmap: 1/1 pages [4KB], 65536KB chunk

unused devices: <none>

From the information above we see that the two drives /dev/sda and /dev/sdc are used for the raid volumes /dev/md0 and /dev/md1 set up on different partitions of the hard disks. Let's check how they are partitioned:

[philipp@lion ~]$ for disk in /dev/sd{a,c}; do echo $disk; sudo parted -s $disk unit GiB print free; done
/dev/sda
Model: ATA HGST HTS541010A9 (scsi)
Disk /dev/sda: 932GiB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start    End      Size     File system  Name  Flags
 1      0.00GiB  0.00GiB  0.00GiB                     bios_grub
        0.00GiB  0.10GiB  0.10GiB  Free Space
 2      0.10GiB  110GiB   110GiB                      raid
        110GiB   120GiB   10.0GiB  Free Space
 3      120GiB   920GiB   800GiB                      raid
        920GiB   932GiB   11.5GiB  Free Space

/dev/sdc
Model: ATA ST9640320AS (scsi)
Disk /dev/sdc: 596GiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start    End      Size     File system  Name  Flags
 1      0.00GiB  0.00GiB  0.00GiB                     bios_grub
        0.00GiB  0.10GiB  0.10GiB  Free Space
 2      0.10GiB  110GiB   110GiB                      raid
        110GiB   120GiB   10.0GiB  Free Space
 3      120GiB   580GiB   460GiB                      raid
        580GiB   596GiB   16.2GiB  Free Space

/dev/sdc is the smaller hard disk drive. The raid /dev/md0 contains the system and doesn't need to grow bigger. But the raid volume /dev/md1 consists of two partitions, one with 800GiB, the other with 460GiB disk space. So just by replacing the smaller drive we can increase the size of the raid volume to 800GiB! Let's start.

RAID=/dev/md0
DRIVE=/dev/sdc2
mdadm -f $RAID $DRIVE
mdadm -r $RAID $DRIVE

RAID=/dev/md1
DRIVE=/dev/sdc3
mdadm -f $RAID $DRIVE
mdadm -r $RAID $DRIVE

After this operation, your RAID status looks like this:

[philipp@lion ~]$ cat /proc/mdstat 
Personalities : [raid1] 
md1 : active raid1 sda3[1]
      482213888 blocks super 1.2 [2/1] [_U]
      bitmap: 0/4 pages [0KB], 65536KB chunk

md0 : active raid1 sda2[1]
      115175424 blocks super 1.2 [2/1] [_U]
      bitmap: 1/1 pages [4KB], 65536KB chunk

unused devices: <none>

Now shut down and replace the drive, then boot again. Or if you have an awesome machine that supports hotplug, then replace the drives while powered on.

Once your up again, copy the partition table of the drive that you kept in your system to the new one. You can do this with sgdisk (from the package gdisk in Ubuntu or Arch Linux):

sgdisk --backup=table.mbr /dev/sda
sgdisk --load-backup=table.mbr /dev/sdc
sgdisk -g /dev/sdc

Instead, you could also create a new partition table and define the partitions again:

dev=/dev/sdc
parted -s $dev mktable gpt
parted -s $dev unit GiB print free
parted -s $dev mkpart primary 17kiB 1024kiB
parted -s $dev set 1 bios_grub on
parted -s $dev mkpart primary 0.10GiB 110GiB
parted -s $dev set 2 raid on
parted -s $dev mkpart primary 120GiB 920GiB
parted -s $dev set 3 raid on
parted -s $dev unit GiB print free

Now add the partitions of the new disk to the array:

mdadm --add /dev/md0 /dev/sdc2
mdadm --add /dev/md1 /dev/sdc3

Allow the resync to fully complete before continuing.

If the array has a write-intent bitmap, follow these instructions on removing it before growing the array! (For more info about write-intent bitmaps, check this.)
Once all of the drives in the array have been replaced with larger drives, grow the array:

mdadm --grow /dev/md1 --size=max

Now make sure, the new disk also has grub installed:

grub-install --target=i386-pc --recheck --debug /dev/sdc

Now grow the file system (should be safe to do this "online", i.e. while running your system from that file system and having it mounted) to make use of the bigger raid volume:

resize2fs -p /dev/md1

Resources

Comments