December 16, 2015

LVM: Creating a File-backed Volume Group

Sometimes we find ourselves in the position where we have a very large physical partition and decide we would like to create some Virtual Machines by using LVM for them. Too late to repartition. What are we going to do now?

Here is a remarkably simple five step solution.

First we'll need a place for our LVM setup. Of course you'll want to pick more descriptive names for your own.
mkdir /var/LVM
Second we'll need a sparse file. This will become our PV. It can even be larger than the partition it lives in. 1K blocks are convenient; it makes for easy math. We'll make ours 50GiB (1K per block x 50M blocks).
dd if=/dev/null of=/var/LVM/PV bs=1K seek=50M
Third we have to turn our file into a device. We'll use losetup and use the first available loop device.
losetup `losetup -f` /var/LVM/PV
losetup -l # shows which loop device got used
Fourth we create a PV out of our new device using whichever loop device was shown above.
pvcreate /dev/loop0
Fifth we create a VG using our new PV. In this example extents are 1MiB. Remember to specify the same loop device again.
vgcreate -s 1M VG /dev/loop0

Try vgdisplay. You'll see the new group, 50GiB in size, 51,199 1MiB extents (costs 1 extent to set this all up).

One useful side effect is that all of your Virtuals live under your filesystem root, /. That means you can use rsync to back up all your LVs at once, rsync them separately, or back up your entire system, VMs and all.

Great. I rebooted. Where is everything now?

Everything is still there; after all, it is just a file. You just need to make it available to the system the same way we did before. This will attach our file based logical filesystem to the first available loop device.
losetup `losetup -f` /var/LVM/PV
Prove it!