In April, lxc-clone gained the ability to create overlayfs snapshot clones of directory backed containers. In may, I wrote a little lxc-snap program based on that which introduced simple ‘snapshots’ to enable simple incremental development of container images. But a standalone program is not only more tedious to discover and install, it will also tend to break when the lxc API changes.
Now (well, recently) the ability to make snapshots has been moved into the lxc API itself, and the program lxc-snapshot, based on that, is shipped with lxc. (Leaving lxc-snap happily deprecated.)
As an example, let’s say you have a container c1, and you want to test a change in its /lib/init/fstab. You can snapshot it,
sudo lxc-snapshot -n c1
test your change, and, if you don’t like the result, you can recreate the original container using
sudo lxc-snapshot -n c1 -r snap0
The snapshot is stored as a full snapshot-cloned container, and restoring is done as a copy-clone using the original container’s backing store type. If your original container was /var/lib/lxc/c1, then the first snapshot will be /var/lib/lxcsnaps/c1/snap0, the next will be /var/lib/lxcsnaps/c1/snap1, etc.
There are some complications. Restoring a container to its original name as done in the above example will work if you have a btrfs backed container. But if your original container was directory backed, then the snapshot will be overlayfs-based, and will depend on the original container’s rootfs existing. Therefore it will pin the original container, and you’ll need to restore the snapshot to a new name, i.e.
sudo lxc-snapshot -n c1 -r snap0 c2
If you want to see a list of snapshots for container c1, do
sudo lxc-snapshot -n c1 -L
If you want to store a comment with the snapshot, you can
echo "This is before my /lib/init/fstab change" >> comment1 sudo lxc-snapshot -n c1 -c comment1
And then when you do
sudo lxc-snapshot -n c1 -L -C
you’ll see the snapshot comments after each snapshot.
There is certainly room for lots of feature development in lxc-snapshot. It could add removal support, improve comment snapshot support, sort snapshots in the listing, and for that matter could work around the overlayfs shortcomings to allow restoring a container to its original name. So if someone is looking for something to do, here’s one of many things waiting for an owner Meanwhile it seems to me plenty useful as is.