How to replace drives in software raid 1 and grow the filesystem on CentOS

In my setup I have two drives, /dev/sda and /dev/sdb with three partitions each. The first partition is /, the 2nd is swap, and the third is home. You may need to adjust things below for your setup if different.

1. On the booted system the first thing is to remove the 2nd drive from the array, remember your may have more or less partitions to have to remove than me, but the thing to remember is to map the md device number with the partition number like below:

mdadm -f /dev/md0 /dev/sdb1
mdadm -r /dev/md0 /dev/sdb1
mdadm -f /dev/md1 /dev/sdb3
mdadm -r /dev/md1 /dev/sdb3

2. Next shut down the server, replace 2nd drive with the new drive, boot server.

3. Next copy the parition table from the 1st drive to the new drive. You can do this manually but the easiest way is to do it with dd:

dd if=/dev/sda of=/dev/sdb bs=512 count=1

4. In my setup, the third partition is the last partition and the one that can be grown once the drives ae replaced. I changed the size of the last partition by using fdisk to delete sdb3 and recreate it with the max space and type ‘fd’ for raid auto-detect. Now the problem is that the partition device files haven’t been created in /dev yet (e.g. /dev/sdb1). The easiest way to create these is to use fdisk and the ‘w’ option to write out the paritition table, then q to exit.

5. Now you need to add the new drive’s partitions to the raid array, remembering again to correctly map the drive partition numbers to the raid araray device number:

mdadm --add /dev/md0 /dev/sdb1
mdadm --add /dev/md1 /dev/sdb3

Then Verify raid is rebuilding with:

cat /proc/mdstat

6. Once teh sync finishes you need to install the grub boot loader on the new drive:

grub-install /dev/sdb

7. The shutdown server, move the new drive to the primary position and replace the 2nd original drive with the 2nd new drive.

8. Repeat the copy of the parition table like we did earlier:

# You won’t have to remove/create any partitions if you’re using two of the same drive, however you will need to go into fdisk and do ‘w to write changes so the device files in /etc are created correctly for /dev/sdb.
dd if=/dev/sda of=/dev/sdb bs=512 count=1
fdisk /dev/sdb

9. If you have swap partitions on your drives then those need to be formatted for swap and enabled:

mkswap /dev/sda2
mkswap /dev/sdb2
swapon /dev/sda2
swapon /dev/sdb2

10. Now add the new 2nd drive’s partitions to the raid array and verify the raid is rebuilding:

mdadm --add /dev/md0 /dev/sdb1
mdadm --add /dev/md1 /dev/sdb3
cat /proc/mdstat

11. Install the grub boot loader on the new 2nd drive:

grub-install /dev/sdb

12. Now, you need to grow the array to use the rest of the space on the new drives, again you can use cat /proc/mdstat to keep an eye on the sync process that will happen again.

mdadm --grow /dev/md1 --size=max
cat /proc/mdstat

13. Next you resize the filesystem in order for your fs (ext2, ext3, ext4, etc..) to use the space.

resize2fs /dev/md1