lxc – Proper Way to Handle LXC Containers on btrfs

btrfscontainerslxcsnapshot

Lets say we have one server with lxc installed, and a lxc container used for as a base img /var/lib/lxc/ubuntu_base. For simplicity let's forget the config changes after copying the base img.

some people suggest using subvolumes and snapshots for making new containers, but one could easily do cp –reflink with simmilar results.

So what is the propper way (or which is better) for managing multiple containers?

  • snapshots

This way seems best, but commands like lxc-destroy won't work since it won't be able to delete the directory.

btrfs subvolume snapshot /var/lib/lxc/ubuntu_base /var/lib/lxc/container_1
  • cp with reflink

I am not sure if there is any performance difference between this or snapshots

cp --reflink=always /var/lib/lxc/ubuntu_base /var/lib/lxc/container_1
  • or Is there any other better way of doing this that I am not aware of.

edit:

One thing I've seen with the reflink option is, that you can't delete the base container if others are running, because the /proc and /dev are mounted and never changed, se the reference is always the same. But shutting down all the coppied containers seems to help.

Best Answer

I am on Ubuntu LTS 14 and just ran the following (for first time even) and it worked like a charm:

lxc-stop -n ubuntu_base
lxc-clone -o ubuntu_base -n ubuntu_base_c1 -s
lxc-start -n ubuntu_base_c1 -d # make changes if needed
lxc-stop -n ubuntu_base_c1
lxc-snapshot -n ubuntu_base_c1

Using -s with lxc-clone will take a snapshot if backing store is btrfs (in your case).

Verify new clone/snapshots with

lxc-ls -f
btrfs subvolume list /var/lib/lxc

Hope that helps!

Related Topic