Monday, April 21, 2008

Testing mdadm's '--grow' option on RAID 5

Here's a simple script to test mdadm's ability to grow a RAID 5 volume onto new disks. This is an amazing feature, which would previously have only been found on expensive hardware RAID solutions.

# Install the required tools.
sudo aptitude install ext2resize mdadm

# Set up the loop devices to be used as RAID
# devices.
for foo in `seq 0 4`; do dd if=/dev/zero bs=1M count=100 of=/tmp/$foo; sudo mknod /dev/loop$foo b 7 $foo; sudo losetup /dev/loop$foo /tmp/$foo; done

# Create a 4 'disk' RAID 5 on /dev/md5.
sudo mdadm -C -l 5 -n 4 /dev/md5 /dev/loop0 /dev/loop1 /dev/loop2 /dev/loop3

# You may want to wait for the initial (re)build to
# complete before proceeding.
watch 'sudo mdadm -D /dev/md5|tail -n 20'

# Make a file system on the RAID 5 volume.
sudo mke2fs /dev/md5

# Mount it up.
sudo mkdir /mnt/tmp
sudo mount /dev/md5 /mnt/tmp

# Create a 250MB test file.
sudo dd if=/dev/urandom bs=1M count=250 of=/mnt/tmp/file

# Take a checksum of the file.
md5sum /mnt/tmp/file|tee /tmp/file.md5sum

# Unmount the volume.
sudo umount /mnt/tmp/

# Add the 5th 'disk' and grow the volume.
sudo mdadm -a /dev/md5 /dev/loop4
sudo mdadm -G /dev/md5 -n 5

# You may want to wait for the grow operation to
# complete before proceeding.
watch 'sudo mdadm -D /dev/md5|tail -n 20'

# Resize the file system to fill the volume.
sudo ext2resize /dev/md5

# Mount it back up.
sudo mount /dev/md5 /mnt/tmp

# Verify that the file is unchanged.
md5sum -c /tmp/file.md5sum

# Unmount the volume.
sudo umount /mnt/tmp/
sudo rmdir /mnt/tmp

# Stop the RAID volume
mdadm -S /dev/md5

# Remove the loop devices and checksum file.
for foo in `seq 0 4`; do sudo losetup -d /dev/loop$foo; rm /dev/loop$foo /tmp/$foo; done
rm /tmp/file.md5sum

No comments: