Whether you want to play around with Linux md to get some more experience or want to see if an actual migration scenario works, it is very useful to have a little play garden to test out difference md setups. As long as you’re not looking to do performance benchmarking, it is pretty easy to realize with a basic Linux system.

In the example below we’ll setup 4 disks and make a 4 way mirror with an ext3 filesystem on it. We’ll change the mirror to a 2 way mirror and finally clean up.

The basic idea is to use files as disks and translate them into devices using the loopback device. First you need to make a couple of files to use as disks. You can make them large or small depending on what you want to test.

You can either do this completely as root or insert the appropriate sudo’s.

cd /tmp dd if=/dev/zero of=disk1 bs=1024 count=10000 dd if=/dev/zero of=disk2 bs=1024 count=10000 dd if=/dev/zero of=disk3 bs=1024 count=10000 dd if=/dev/zero of=disk4 bs=1024 count=10000

With the files created we now need to associate them to loopback devices.

losetup /dev/loop1 disk1 losetup /dev/loop2 disk2 losetup /dev/loop3 disk3 losetup /dev/loop4 disk4

Now we have 4 disks associated to 4 loopback devices. We can use these instead of the usual sd’s or hd’s. You can check the configuration that you have made.

losetup -a /dev/loop1: [0801]:3287450 (/tmp/disk1) /dev/loop2: [0801]:3287451 (/tmp/disk2) /dev/loop3: [0801]:3287452 (/tmp/disk3) /dev/loop4: [0801]:3287453 (/tmp/disk4)

As we have the devices, we can now make a 4 way mirror out of them with mdadm. Check that you use a free md device, I used md0 in this example.

mdadm --create /dev/md0 -l 1 -n 4 /dev/loop1 /dev/loop2 /dev/loop3 /dev/loop4

Check that the array has been constructed successfully.

cat /proc/mdstat Personalities : [raid1] md0 : active raid1 loop4[3] loop3[2] loop2[1] loop1[0] 9992 blocks super 1.1 [4/4] [UUUU]

unused devices:

Now we can make a filesystem, mount it and check it is ok.

mkfs -t ext3 /dev/md0 mount /dev/md0 /mnt df -h /mnt Filesystem Size Used Avail Use% Mounted on /dev/md0 9.5M 1.1M 7.9M 13% /mnt

Now we have everything working we can change the setup from a 4 way mirror to a 2 way mirror. First we have to fail 2 of the devices.

mdadm --fail /dev/md0 /dev/loop4 /dev/loop3 mdadm --remove /dev/md0 /dev/loop4 /dev/loop3

If you check the status of the md device, you’ll see it has 2 disks left.

cat /proc/mdstat Personalities : [raid1] md0 : active raid1 loop2[1] loop1[0] 9992 blocks super 1.1 [4/2] [UU__]

unused devices:

Now we’ll “grow” the md device from 4 to 2 devices.

mdadm --grow -n 2 /dev/md0

When you check the status of the md device now, it looks like this:

cat /proc/mdstat Personalities : [raid1] md0 : active raid1 loop2[1] loop1[0] 9992 blocks super 1.1 [2/2] [UU]

unused devices:

You should still be able to use the filesystem, all is ok.

Finally, we clean up all the stuff.

umount /mnt mdadm --stop /dev/md0 losetup -d /dev/loop1 losetup -d /dev/loop2 losetup -d /dev/loop3 losetup -d /dev/loop4 rm disk?

Of course you can use this to test all kinds of other scenario’s. As long as you have the right support in your kernel :).