[Lxc-users] [RFC 2/2] lxc-start-ephemeral

Serge E. Hallyn serge.hallyn at canonical.com
Fri Aug 5 13:59:36 UTC 2011


Hi, here is a script we're toying with which starts an ephemeral
container based on an existing one, with an aufs read-write layer
on top.  When the container shuts down, it disappears.  This same
script should be trivial to extend to support use of overlayfs in
place of aufs.  (It belongs in src/lxc/lxc-start-ephemeral.in, but
I have it sitting elsewhere so that it stands cleanly apart from
upstream code)

Thoughts?

Many thanks to Robert Collins for the original script.

================================================

#!/bin/bash

# This script runs up and removes an aufs layered lxc container.
#
# Usage: lxc-start-aufs BASECONTAINER (BINDMOUNT or --) [COMMAND [ARGS...]]
# BASECONTAINER should be the simple name of a container to layer on. This
# container probably shouldn't be running.
# BINDMOUNT is a path in the host environment to bind mount into the container
# - e.g. /home/username or some such.
# COMMAND and ARGS are the command and args to run in the container.
# If no BINDMOUNT is desired *and* a COMMAND is desired, provide -- as the
# BINDMOUNT.

# (C) Copyright Canonical 2011

# What lxc container to clone
LXC_BASE=""
# $2 is a path to bind mount e.g. /tmp/foo.
LXC_BIND=""
uniontype="aufs"

usage() {
    echo "usage: lxc-start-ephemeral [-h] [-t type] [-b bdir] -o orig -- [COMMAND [ARGS...]]"
}

help() {
    usage
    echo
    echo "Runs an ephemeral (one-off) container"
    echo
    echo "Options:"
    echo "type        : type of union fs to use.  aufs by default"
    echo "            : Overlayfs and others will be possible one day"
    echo "orig        : name of the original container"
    echo "bdir        : directory to bind mount into container"
}

shortoptions='ht:b:o:'
longoptions='help,orig:,bdir:,type:'

getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
if [ $? != 0 ]; then
    usage
    exit 1;
fi

eval set -- "$getopt"

while true; do
        case "$1" in
	    -h|--help)
		help
		exit 1
		;;
	    -t|--type)
		shift
		uniontype=$1
		shift
		if [ $uniontype != 'aufs' ]; then
			echo "only aufs is supported"
		fi
		;;
	    -o|--orig)
		shift
		LXC_BASE=$1
		shift
		;;
	    -b|--bdir)
		shift
		LXC_BIND=$1
		shift
		;;
            --)
		shift
		break;;
            *)
		echo $1
		usage
		exit 1
		;;
        esac
done

LXC_USER=`id -un`

# validation
if [ -z $LXC_BASE ]; then
    echo "original container must be specified"
    usage
    exit 1
fi
if [ ! -d /var/lib/lxc/$LXC_BASE ] ; then
    echo 'no such lxc container $LXC_BASE'
    exit 1
fi

echo "Setting up ephemeral container..."
OVERLAY_DIR=`mktemp -d /tmp/lxc-lp-XXXXXXX`
sudo mount -t tmpfs none $OVERLAY_DIR
LXC_DIR=`sudo mktemp -d --tmpdir=/var/lib/lxc $LXC_BASE-temp-aufs-XXXXXXX`
LXC_NAME=`basename $LXC_DIR`
sudo mount -t $uniontype -o br=$OVERLAY_DIR=rw:/var/lib/lxc/$LXC_BASE=ro,noplink none $LXC_DIR
if [ -n "$LXC_BIND" ]; then
    sudo mkdir -p $LXC_DIR/rootfs$LXC_BIND
    sudo mount --bind $LXC_BIND $LXC_DIR/rootfs$LXC_BIND
fi
sudo sed -i -e "s/$LXC_BASE/$LXC_NAME/" $LXC_DIR/fstab $LXC_DIR/config $LXC_DIR/rootfs/etc/hostname $LXC_DIR/rootfs/etc/hosts
LEASES=$LXC_DIR/rootfs/var/lib/dhcp3/dhclient.eth0.leases
if [ ! -f $LEASES ]; then
	LEASES=$LXC_DIR/rootfs/var/lib/dhcp/dhclient.leases
fi
sudo truncate -c -s0 $LEASES

echo "Starting up the container..."
sudo lxc-start -n $LXC_NAME -d

echo "$LXC_NAME is running"
echo "You connect with the command:"
echo "    lxc-console -n $LXC_NAME"
lxc-monitor -Q -n $LXC_NAME

echo "Stopping lxc" >&2
sudo lxc-stop -n $LXC_NAME
sleep 2
if [ -n "$LXC_BIND" ]; then
    echo "umounting bind" >&2
    sudo umount $LXC_DIR/rootfs$LXC_BIND
fi
# echo "umounting lxc_dir $LXC_DIR" >&2
sudo umount $LXC_DIR
# echo "umounting overlay" >&2
sudo umount $OVERLAY_DIR
# echo "rming lxc_dir $LXC_DIR" >&2
sudo rmdir $LXC_DIR
# echo "rming overlay dir $OVERLAY_DIR" >&2
rmdir $OVERLAY_DIR




More information about the lxc-users mailing list