#!/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 "            : options are aufs and overlay"
    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' -a $uniontype != 'overlay']; then
			echo "only aufs and overlay 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`
if [ $uniontype = "aufs" ]; then
	sudo mount -t aufs -o br=$OVERLAY_DIR=rw:/var/lib/lxc/$LXC_BASE=ro,noplink none $LXC_DIR
else
	sudo mount -t overlayfs -oupperdir=$OVERLAY_DIR,lowerdir=/var/lib/lxc/$LXC_BASE none $LXC_DIR
fi
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
