Linux Software RAID Hack

You need to install a piece of software that requires 66GB of free space, but you don’t have that much in one place. Sure, you have 30GB free on one partition, and another 40Gb free on another, but the installer will still complain about the lack of space. How do you combine the free space on those two parititions into a new, much larger partition, without reformatting the existing partitions?

Easy! We’re going to use the Linux loopback device system, along with software raid. How does it work? You create a large file using dd on each of the filesystems with free space, and bind those files to loopback devices in the kernel using losetup. Then, you set up your /etc/raidtab to create a linear array from all the loop devices (RAID-0 in this case is no use), and create and mount a new filesystem on your virtual RAID device.

This type of affair is only something you should do as a last resort, since any serious disk activity on this array will bring your machine to its knees (you will be using 2 filesystem layers, and twice as much kernel cache for the same data). But, if you really need the space and reinstalling isn’t an option, it’s a pretty good way of getting the extra space. Below are some more detailed instructions on how to achieve this monstrous arrangement:

  1. Create the large files that will be the underpinnings of the array. Check how much free space you have on each of your mounts using the df -h command. Next, we’ll need to create the files. Using the above examples of 30GB and 40GB, we’ll create two files, each slightly smaller than the amount of free space (in case you need a bit of space later). Note in this example the 30GB filesystem is / and the 40GB one is /u01 (Oracle, me?).
    dd if=/dev/zero of=/raidpart-0.img bs=1M count=29K
    dd if=/dev/zero of=/u01/raidpart-1.img bs=1M count=39K
  2. Bind the files into loop devices so the kernel can use them:
    losetup /dev/loop0 /raidpart-0.img
    losetup /dev/loop1 /u01/raidpart-1.img
  3. Create the /etc/raidtab entry for our array (put the following lines in the said file):
    raiddev /dev/md0
        raid-level              linear
        nr-raid-disks           2
        persistent-superblock   0
        chunk-size              32
        device                  /dev/loop0
        raid-disk               0
        device                  /dev/loop1
        raid-disk               1
  4. Create the array. Note that this will also start up the array for us, so we don’t need to do that step.
    mkraid /dev/md0
  5. Create your filesystem on the array (best not use a journaled FS for this):
    mke2fs /dev/md0
  6. Mount your new, bigger filesystem:
    mkdir /u02
    mount /dev/md0 /u02

And that’s it! Note that I simply assume you know what you’re doing and you’ve got all the appropriate modules loaded into your kernel before you start. Also note that resizing either file will irreparably break the filesystem on the array. You have been warned! I’ve also written scripts, below, that help to start and stop the array.

u02-start.sh

#!/bin/bash
echo "*** Setting up loop devices..."
losetup /dev/loop0 /raidpart-0.img; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
losetup /dev/loop1 /u01/raidpart-1.img; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
echo "*** Starting RAID array..."
raid0run /dev/md0; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
echo "*** Array status:"
cat /proc/mdstat
echo "*** Mounting /u02..."
mount /dev/md0 /u02; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
echo "RAID array started and ready to use.":

u02-stop.sh

#!/bin/bash
echo "*** Unmounting /u02..."
umount /u02; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
echo "*** Stopping RAID array..."
raidstop /dev/md0; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
echo "*** Array status:"
cat /proc/mdstat
echo "*** Deleting loop devices..."
losetup -d /dev/loop0; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
losetup -d /dev/loop1; ret=$?
if [ $ret -ne 0 ]; then
        exit $ret;
fi
echo "RAID array stopped."

2 thoughts on “Linux Software RAID Hack

  1. Now you lost me, just checking in from the Apple Store, and shopping needs? btw this is coming at you from an eMac

  2. Great article. Saved me from purchasing new harddisk.

    However, persistent-superblock is recommended to be 1
    as this will prevent data corruption even if disk
    ordering changes by mistake.

Comments are closed.