LXC Linux Containers

I’ve been using LXC Linux Containers to host a number of OpenStack admin processes, as well as other things, like playing with Galera+MariaDB. Anyway, I thought I’d share my Bash shell scripts for container creation, management, etc. Your mileage may vary.

You’ll find them at http://whistl.com/files/cnutils.shar in a shell archive.
Just run it to extract the scripts, and examine them or use them at your own risk. They do assume an LVM environment with the vgname vg1, and a btrfs filesystem mounted at /var/logdata on the lxc host.

The script cncreate.sh creates a container, given the name, ip, mac address and lvm volume size (eg 20G). It’ll create a btrfs subvolume of /var/logdata and mount it as /var/log inside the container, for centralized logging. This script uses the base1.config file as a template for /var/lib/lxc/<containerneme>/config, and common.conf is a copy of my /etc/lxc/common.conf

The script cnclone.sh is kind of like cncreate.sh, except it uses the LVM snapshot feature to clones another “base” container instead of creating one from scratch. LVM snapshots are copy-on-write clones, so only things added or changed after the snapshot consume any additional disk space. You can create a base container, install 100GB of software, then make snapshots of that base, and each snapshot would have full access to all those packages, but only consume additional disk space for it’s config files, log files, and writable data files. This can be a huge space savings, but it’s saves time on configuring. If your base container already knows how to authenticate with your LDAP cluster, you might only need modify a few config files post snapshot, if individual accounts or certificates were required.

The script cnls.sh lists all configured containers in /var/lib/lxc, and if they are running, some additional info.

Then there’s cnman.sh, which is just a general management utility which takes an argument of stop, start, restart or status and operates on ALL containers. Nice when you need to fix something. Only if a container has lxc.start.auto = 1 will it be started by cnman.sh start, but it will shut any down it finds for stop.