#!/bin/bash

#
# lxc: linux Container library

# Authors:
# Daniel Lezcano <daniel.lezcano@free.fr>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

#
# This script allows to set or remove the capabilities on the lxc tools.
# When the capabilities are set, a non root user can manage the containers.
#

usage() {
    echo "usage: $(basename $0) -n NAME [-f]" >&2
}

help() {
    usage
    echo >&2
    echo "Remove an existing container on the system." >&2
    echo >&2
    echo "Options:" >&2
    echo "  -n NAME   specify the name of the container" >&2
    echo "  -f        stop the container if it is running (rather than abort)" >&2
}

shortoptions='hn:f'
longoptions='help,name:'
lxc_path=/var/lib/lxc
force=0

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
		;;
	    -n|--name)
		shift
		lxc_name=$1
		shift
		;;
	    -f)
		force=1
		shift
		;;
            --)
		shift
		break;;
            *)
		usage
		exit 1
		;;
        esac
done

if [ -z "$lxc_name" ]; then
    echo "$(basename $0): no container name specified" >&2
    usage $0
    exit 1
fi

if [ "$(id -u)" != "0" ]; then
   echo "$(basename $0): must be run as root" >&2
   exit 1
fi

if [ ! -d "$lxc_path/$lxc_name" ]; then
    echo "$(basename $0): '$lxc_name' does not exist" >&2
    exit 1
fi

# make sure the container isn't running
lxc-info -n $lxc_name 2>/dev/null | grep -q RUNNING
if [ $? -eq 0 ]; then
	if [ $force -eq 1 ]; then
		lxc-stop -n $lxc_name
		lxc-wait -n $lxc_name -s STOPPED
	else
		echo "$(basename $0): '$lxc_name' is running; aborted" >&2
		exit 1
	fi
fi

# Deduce the type of rootfs
# If LVM partition, destroy it. For btrfs, we delete the subvolue. If anything
# else, ignore it. We'll support deletion of others later.
rootdev=`grep lxc.rootfs $lxc_path/$lxc_name/config 2>/dev/null | sed -e 's/^[^/]*/\//'`
if [ -n "$rootdev" ]; then
	if [ -b "$rootdev" ]; then
		lvdisplay $rootdev > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "removing backing store: $rootdev"
			lvremove -f $rootdev
		fi
	elif [ -h "$rootdev" -o -d "$rootdev" ]; then
		if which btrfs >/dev/null 2>&1 &&
		   btrfs subvolume list "$rootdev" >/dev/null 2>&1; then
			btrfs subvolume delete "$rootdev"
		else
			# In case rootfs is not under $lxc_path/$lxc_name, remove it
			rm -rf --one-file-system --preserve-root $rootdev
		fi
	fi
fi
# recursively remove the container to remove old container configuration
rm -rf --one-file-system --preserve-root $lxc_path/$lxc_name
