Expanding Linux software RAID1 with an ext3 File System

Yes it is possible to expand a software RAID1 device without loosing data. As a proof of concept we will create a small RAID1 device, then expand it.

Of course, if you have real data you would be insane to undertake this procedure without first ensuring you have a complete backup.

I’ve seen other procedures posted online which advocate stopping half the array, expanding one partition, reassemble, stop the other side, expand, reassemble, grow, and then finally expand the file system. The theory being, no down-time, and since half the array is always running, no chance of data loss.

There is no problem with that method but there are some things to consider:

a) It’s more steps and each step is a opportunity for a mistake.

b) I always hate “breaking” my array then putting it back together again. There is always a risk that when you put it back together, instead of drive A replicating drive B, it will go the other way and overwrite the newer data.

c) “no downtime” is possible on file systems and kernels that support it. As of this writing that means pretty much all the modern releases of linux but older systems (I believe kernels older than 2.6.10), it can’t be done without unmounting. In all cases, unmounting is probably safer.

For the purpose of this test, I partitioned two drives with 1G partitions of type FD (Linux Raid auto-detect) and created the RAID1 array on them. In my example the drive letters are sdf and sdg, and the partitions are sdf1 and sdg1
but the letters will vary depending on your setup.

Of course, if you have an existing array you should skip the creation steps and start with the section called “Existing Arrays”.

# mdadm –create /dev/md0 –level=raid1 –raid-devices=2 /dev/sdf1 /dev/sdg1

You can check the status of the raid device(s) from mdstat. While it’s building it will look like this:

# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdg1[1] sdf1[0]
      2095552 blocks [2/2] [UU]
      [======>..............]  resync = 30.1% (631808/2095552) finish=0.1min speed=126361K/sec

You don’t have to wait for the device to finish syncing before using it but when it is finished it will look like this:

# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdg1[1] sdf1[0]
      2095552 blocks [2/2] [UU]

Format the device as ext3

# mke2fs -j /dev/md0

Mount the device and put a file on it just so you can prove there is no data loss.

# mount /dev/md0 /mnt
# cp <whatever> /mnt/

EXISTING ARRAYS:
unmount the device and stop the array.

# umount /dev/md0
# mdadm -S /dev/md0

Repartition the drives to whatever size you want. I changed my partition size from 1G to 2G. When you exit fdisk the kernel should be able to re-read the partition information. In rare cases it will fail and you get an error. You will most likely have to reboot so it can re-read the partition tables.

At this point, if you try to just re-assemble the array, mdadm will complain that it can’t find an raid superblock. Don’t panic!

The correct thing to do is recreate the array. mdadm will detect and warn you that it sees an existing file system. Continue with the raid creation:

# mdadm --create /dev/md0 --level=raid1 --raid-devices=2 /dev/sdf1 /dev/sdg1
mdadm: /dev/sdf1 appears to contain an ext2fs file system
    size=1038400K  mtime=Fri Jul  3 13:17:46 2009
mdadm: /dev/sdg1 appears to contain an ext2fs file system
    size=1038400K  mtime=Fri Jul  3 13:17:46 2009
Continue creating array? y
mdadm: array /dev/md0 started.

mdadm appears to be warning you that it is about to delete a filesystem so again, you might have a tendency to panic, don’t. What mdadm actually does is completely replicate all the data from the first device to the second device (or it might be the other way around but since we stopped the array, it doesn’t matter since both are identical).

When it’s done, the md0 device will be the new larger size and you can mount the file system and take a look to see if your data is still intact. However there is one last step. While the device is bigger, the file system is still needs to be resized.

Ensure that it is unmounted:

# umount /dev/md0

A file system check is forced and then resize the filesystem.

# e2fsck -f /dev/md0
# resize2fs /dev/md0

All done!

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: