Linux containers and btrfs snapshots.
I quite like Linux Containers (lxc) but have been looking for a nice way to create and destroy them quickly. I don’t use LVM on my laptop which seems to be the suggested way so I converted my rootfs to btrfs and using that instead. This should allow me to create a new test machine quickly and use very little disk space as the snapshots are copy-on-write.

I will not go into how to install lxc and setup an inital machine as there are much better guides on the web out there.
The lxc systems are created in /var/lib/lxc by default so this is what I will be working with. I assume you have created a base container as /var/lib/lxc/base-debian and want to create a new machine called debian-1 and that you have a single filesystem / that is all btrfs.
First off create a subvolume for your base image to live on. First move your current base-debian out the way then create the snapshot. Then move the content back in.
cd /var/lib/lxc
mv base-debian base-debian.tmp
btrfs subvolume create /var/lib/lxc/base-debian
mv base-debian.tmp/* base-debian/
rm -rf base-debian.tmp
Now check what you have
btrfs subvolume list /
Now we can create our first clone.
btrfs subvolume snapshot /var/lib/lxc/base-debian /var/lib/lxc/debian-1
Then fix the config files
sed -i “s+/lxc/base-debian/rootfs+/lxc/debian-1/rootfs+” /var/lib/lxc/debian-1/config
echo “debian-1” >/var/lib/lxc/debian-1/rootfs/etc/hostname
Now start it up
lxc-start —name debian-1
Simple as that!
You can delete them when you are finished using
btrfs subvolume delete /var/lib/lxc/debian-1
