lxc-snap: lxc container snapshot management tool
BACKGROUND
Lxc supports containers backed by overlayfs snapshots. The way this is
typically done is to create a container backed by a regular directory,
then create a new container which mounts the first container’s rootfs
as a read-only lower mount, with a new private delta directory as
its read-write upper mount. For instance, you could
sudo lxc-create -t ubuntu -n r0 # create a normal directory
sudo lxc-clone -B overlayfs -s r0 o1 # create overlayfs clone
The second container, o1, when started up will mount /var/lib/lxc/o1/delta0
as a writeably overlay on top of /var/lib/lxc/r0/rootfs, and use that as its
root filesystem.
From here you can clone o1 to a new container o2. This simply copies the
the overlayfs delta from o1 to o2, and you is done with
sudo lxc-clone -s o1 o2
LXC-SNAP
One of the obvious use cases of these snapshot clones is to support
incremental development of rootfs images. Make some changes, snapshot,
make some more changes, snapshot, revert…
lxc-snap is a small program using the lxc API to more easily support
this use case. You begin with a overlayfs backed container, make some
changes, snapshot, make some changes, snapshot… This is a simpler
model than manually using clone because you continue developing the same
container, o1, while the snapshots are kept away until you need them.
EXAMPLE
Create your first container
sudo lxc-create -t ubuntu -n base
sudo lxc-clone -s -B overlayfs base mysql
Now make initial customizations, and snapshot:
sudo lxc-snap mysql
This will create a snapshot container /var/lib/lxcsnaps/mysql_0. You can actually
start it up if you like using ‘sudo lxc-start -P /var/lib/lxcsnaps -n mysql_0’.
(However, that is not recommended, as it will cause changes in the rootfs)
Next, make some more changes. Write a comment about the changes you made in this
version,
echo “Initial definition of table doomahicky” > /tmp/comment
sudo lxc-snap -c /tmp/comment mysql
Do this a few times. Now you realize you lost something you needed. You can
see the list of containers which have snapshots using
lxc-snap -l
and the list of versions of container mysql using
lxc-snap -l mysql
Note that it shows you the time when the snapshot was created, and any comments
you logged with the snapshot. You see that what you wanted was version 2, so
recover that snapshot. You can destroy container mysql and restore version 2
to it, or (I would recommend) use a different name to restore the snapshot to.
Use a different name with:
sudo lxc-snap -r mysql_2 mysql_tmp
or destroy mysql and restore the snapshot to it using
sudo lxc-destroy -n mysql
sudo lxc-snap -r mysql_2 mysql
When you’d like to export a container, you can clone it back to a directory
backed container and tar it up:
sudo lxc-clone -B dir mysql mysql_ship
sudo tar zcf /srv/mysql_ship.tar.gz /var/lib/lxc/mysql_ship
BUILD AND INSTALL
To use lxc-snap, you currently need to be using lxc from the ubuntu-lxc
daily ppa. On an ubuntu system (at least 12.04) you can
sudo add-apt-repository ppa:ubuntu-lxc/daily
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install lxc
lxc-snap will either become a part of the lxc package, or will become a
separate package. Currently it is available at
git://github.com/hallyn/lxc-snap. Fetch it using:
git clone git://github.com/hallyn/lxc-snap
Then build lxc-snap by typing ‘make’.
cd lxc-snap
make
Install into /usr/bin by typing
sudo DESTDIR=/usr make install
or install into /home/$USER/bin by typing
mkdir /home/$USER/bin
DESTDIR=/home/$USER make install
Note that lxc-snap is in very early development. It’s usage may
change over time, and as it currently ships a copy of liblxc .h
files it needs, it may occasionally break and need to be updated
from git and rebuilt. Using a package (as soon as it becomes
available) is recommended.
Note that lxc-snap is in very early development. It’s usage may
change over time, and as it currently ships a copy of liblxc .h
files it needs, it may occasionally break and need to be updated
from git and rebuilt. Using a package (as soon as it becomes
available) is recommended.
lxc package, or will become a
separate package. Currently it is available at
git://github.com/hallyn/lxc-snap. Fetch it using:
git clone git://github.com/hallyn/lxc-snap
Then build lxc-snap by typing ‘make’.
cd lxc-snap
make
Install into /usr/bin by typing
sudo DESTDIR=/usr make install
or install into /home/$USER/bin by typing
mkdir /home/$USER/bin
DESTDIR=/home/$USER make install
Note that lxc-snap is in very early development. It’s usage may
change over time, and as it currently ships a copy of liblxc .h
files it needs, it may occasionally break and need to be updated
from git and rebuilt. Using a package (as soon as it becomes
available) is recommended.
Serge this is a very useful LXC capability and I can see this being more manageable than past use of just doing lxc-clone’s.
thanks…!
This is just awesome. You guys are rendering docker useless. Any chance of making lxc-snap take a remote host parameter, and save a current snapshot of the container in a remote host. I am trying to draw parallels with docker registry (https://github.com/dotcloud/docker-registry) feature. “Pulling” and “Pushing” container snapshots to a remote host so that a team of developers can share a set of container snapshots.
Thanks, glad you like it. When openvz’s checkpoint/restart kernel functionality hits upstream kernel, then we might look into live migration, however I”m happy to have docker providing the registry and static migration as porcelain on top of lxc. There is plenty to be done underneath that in the meantime.
Nice!
Did you accidentally copy/paste a couple extra times? I see three of each section in the post.
Holy cow. I must’ve middle-clicked or shift-inserted a few times, yeah.
This is great.
Will lxc-snap work with ZFS?
I haven’t coded it to do so yet, but it should be trivial to do so. Creating a snapshot would come down to the equivalent of doing ‘lxc-clone -P /var/lib/lxcsnaps -s $container $container-$index’.
this is awesome !
So from what i gather it makes cloning a lot easier as the comments above say too. If say i clone or use snap for that matter a running lxc, is it possible to use a new conf file for the lxc that is spin off it ? (altered network configuration , other variables etc ?)
Oh yes of course i could start it with any configuration file i choose 🙂 sorry for the questions
Sorry, this is a year late – yeah, after making a snapshot you can alter the snapshot if you like, and start it like any other container by providing the snapshot path as “-P lxcpath” option.
Highly energetic article, I loved that a lot.
Will there be a part 2?
Guess not 🙂
But stay tuned, some cool new image related stuff is coming.
Thanks for taking the time to spell this out in detail. You make a good case for lxc snapshots as a means for evolving system configuration. However, I know it is not easy to keep documentation current, and this is now two years in the past.
So, my question is, “Is this still a close match to current usage, or, if not, can you point to another option that has more up to date information?”
Thanks again!
Hi,
lxc-snap is in fact obsolete now. lxc comes with a lxc-snapshot which does this for you, and lxd also has snapshots (using ‘lxc snapshot’ or https://lxdhost/1.0/containers/yourcontainer/snapshot POST)
Pingback: How do I export a lxc container?