lxc-clone

A common workflow these days is to keep a single pristine image, keep that uptodate, and spin off instances of that image when needed – to buid a package, reproduce a bug, whatever. You can see this in schroot (see also this), vm-tools, and all sorts of cloud based provisioning systems.

To do the same thing with lxc, I’ve implemented a very simple lxc-clone program. It’s in ppa:serge-hallyn/lxc-natty, which is built from lp:~serge-hallyn/ubuntu/natty/lxc/lxc-clone. For now it’s designed to work with the current ubuntu lxc templates. I would prefer to use it with LVM based rootfs’s, so I will probably add that soon. Meanwhile, here is how I use it:

{{{
lxc-create -f /etc/lxc-basic.conf -t natty -n natty
cat >> /var/lib/lxc/natty/fstab << EOF
/etc/passwd /var/lib/lxc/natty/rootfs/etc/passwd none defaults,bind,ro 0 0
/etc/shadow /var/lib/lxc/natty/rootfs/etc/shadow none defaults,bind,ro 0 0
/home /var/lib/lxc/natty/rootfs/home none defaults,bind 0 0
EOF
}}}

(See here for more on lxc-create and lxc-basic.conf).

That creates the pristine natty-based container and mounts /home and /etc/passwd and /etc/shadow into the container. Then, to spin off an instance I do:

{{{
lxc-clone -o natty -n natty-lp123456
}}}

Start it as usual with

{{{
lxc-start -n natty-lp123456
}}}

When done, just toss it

{{{
lxc-destroy -n natty-lp123456
}}}

To update the pristine image, just:

{{{
lxc-start -n natty
# log in as root
apt-get update
apt-get -y dist-upgrade
shutdown
}}}

Then fire off a new instance

{{{
lxc-clone -o natty -n natty-lp123456
}}}

Note that if you bind mount /etc/shadow as above, then either your host needs to have a root password, or you need to install and configure sudo in your container. But if you’ve started a container you can’t log in to, don’t worry – just kill it using ‘lxc-stop -n natty’.

There’s no single command to do the equivalent as sbuild, but what it does give you is isolated pid and network namespaces, which can be very useful in their own right. Needless to say I’ll be continuing to use both.

Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

One Response to lxc-clone

  1. Pingback: lxc-lvm-clone | S3hh's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s