Creating Portable DiskSafes With LoopbackFS And LVM Snapshots

This is the "DiskSafe" idea used to store backups of server data. This could be used to replace physical tape volume and still provide portablility. I have found pieces of this information around the Internet but nothing putting it all together.


  • OS LVM support
  • OS Loopback filesystem support

How it works:

1. Create a file based block device. This file will appear to Linux as a physical device.  This test has a capcity of 2GB.

sudo dd if=/dev/zero of=/tmp/diskfile.bak bs=1M count=2000
sudo losetup /dev/loop0 /tmp/diskfile.bak

2. Now we turn on LVM on this file based block device.

 sudo pvcreate /dev/loop0

3. And create an LVM Group.

 sudo vgcreate DiskBackup1 /dev/loop0

4. Display the LVM Group just to see that it worked.

 sudo vgdisplay

5. Now create a 1GB storage volume for testing.

 sudo lvcreate -L1G -nlvm1 DiskBackup1

6. Format it with your favorite filesystem. I like ext4.

 sudo mkfs.ext4 /dev/DiskBackup1/lvm1

7. And mount it.

 sudo mount /dev/DiskBackup1/lvm1 /mnt/backuptest

8. Check it out with df.  Up to this point this would only need to be done once to create the disk safe.

 df -h

9. Create a snapshot volume.  The would happen every time a new backup was run.

 sudo lvcreate -L500M -s -n snap /dev/DiskBackup1/lvm1

10. Dislay volumes just to see if it is working.

 sudo lvdisplay

11. Let mount the snapshot just to test.

 sudo mount /dev/DiskBackup1/snap /mnt/backupsnap

At this point there is a volume we can write to and the snapshot should remain unchanged.

/mnt/backuptest should be writable and /mnt/backupsnap should not be touched.  These snapshots are all stored within the file /tmp/backupdisk.bak so they can be unmounted moved/backedup and remounted later.  There are also some option in LVM2 to mirror volumes. Or there are tons of other possibilities.

The data is stored in ext4 so you can still run fsck against it.

When finished with backup export LVM group for safe keeping.

sudo vgchange -an DiskBackup1
sudo vgexport DiskBackup1
sudo losetup -d /dev/loop0

Now I just have to figure out how to get the data into the lvm.  Rsync would work great for Linux. Maybe a Windows version of dd, rsync and ssh....

Share this page:

Suggested articles

0 Comment(s)

Add comment