#!/bin/bash
+path=/vservers
+
+#################### work on all containers - only deal with the ones that have a timestamp
function sense_all () {
+ virsh -c lxc:/// list 2> /dev/null | grep running | while read line; do
+ pid=$(echo $line | cut -d' ' -f1)
+ lxc_name=$(echo $line | cut -d' ' -f2)
+ # ignore regular vservers like testmaster and the like
+ timestamp_file=$path/$lxc_name/$lxc_name.timestamp
+ [ -f $timestamp_file ] || continue
+ timestamp=$(cat $timestamp_file 2> /dev/null)
+ echo "$lxc_name;$pid;$timestamp"
+ done
+}
- for i in $(lxc-ls -1|sort|uniq); do
- [ "$(lxc-info -n $i | grep state| awk '{print $2;}' )" == "RUNNING" ] && echo "$i;$(lxc-info -n $i | grep pid | awk '{print $2;}');$(cat /var/lib/lxc/$i/$i.timestamp)" || :
+function start_all () {
+ virsh -c lxc:/// list --inactive | grep " - "| while read line; do
+ lxc_name=$(echo $line | cut -d' ' -f2)
+ # ignore regular vservers like testmaster and the like
+ timestamp_file=$path/$lxc_name/$lxc_name.timestamp
+ [ -f $timestamp_file ] || continue
+ virsh -c lxc:/// start $lxc_name
done
}
-function start_all () {
+function stop_all () {
+ virsh -c lxc:/// list | grep running | while read line; do
+ lxc_name=$(echo $line | cut -d' ' -f2)
+ # ignore regular vservers like testmaster and the like
+ timestamp_file=$path/$lxc_name/$lxc_name.timestamp
+ [ -f $timestamp_file ] || continue
+ virsh -c lxc:/// destroy $lxc_name
+ done
+}
- for i in $(lxc-ls -1|sort|uniq); do
- [ "$(lxc-info -n $i | grep state| awk '{print $2;}' )" != "RUNNING" ] && lxc-start -d -n $i || :
- lxc-wait -n $i -s RUNNING
+function destroy_all () {
+
+ stop_all
+ virsh -c lxc:/// list --all | while read line; do
+ lxc_name=$(echo $line | cut -d' ' -f2)
+ # ignore regular vservers like testmaster and the like
+ timestamp_file=$path/$lxc_name/$lxc_name.timestamp
+ [ -f $timestamp_file ] || continue
+ virsh -c lxc:/// undefine $lxc_name
+ rm -fr $path/$lxc_name
done
-
- #sense_all
}
-function stop_all () {
-
- for i in $(lxc-ls -1|sort|uniq); do
- [ "$(lxc-info -n $i | grep state| awk '{print $2;}' )" != "STOPPED" ] && lxc-stop -n $i
- lxc-wait -n $i -s STOPPED
- done
-
- sense_all
+function restart_all () {
+
+ stop_all
+ start_all
}
+#################### deal with one user-specified container
function sense_lxc () {
- lxc=$1; shift
- [ "$(lxc-info -n $lxc | grep state | awk '{print $2;}')" == "RUNNING" ] && echo "$lxc;$(lxc-info -n $lxc | grep pid | awk '{print $2;}');$(cat /var/lib/lxc/$lxc/$lxc.timestamp)" || :
+ lxc_name=$1; shift
+ if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" == "running" ] ; then
+ pid=$(virsh -c lxc:/// dominfo $lxc_name| grep Id | cut -d' ' -f14)
+ timestamp_file=$path/$lxc_name/$lxc_name.timestamp
+ timestamp=$(cat $timestamp_file)
+ echo "$lxc_name;$pid;$timestamp"
+ fi
}
function start_lxc () {
- lxc=$1; shift
- [ "$(lxc-info -n $lxc | grep state| awk '{print $2;}' )" != "RUNNING" ] && lxc-start -d -n $lxc ||:
- lxc-wait -n $lxc -s RUNNING
-
- sense_lxc $lxc
+ lxc_name=$1; shift
+ if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "running" ] ; then
+ virsh -c lxc:/// start $lxc_name
+ fi
}
function stop_lxc () {
- lxc=$1; shift
- [ "$(lxc-info -n $lxc | grep state| awk '{print $2;}' )" != "STOPPED" ] && lxc-stop -n $lxc
- lxc-wait -n $lxc -s STOPPED
-
- sense_lxc $lxc
-}
-
-function restart_all () {
-
- stop_all
- start_all
+ lxc_name=$1; shift
+ if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "shut off" ] ; then
+ virsh -c lxc:/// destroy $lxc_name
+ fi
}
function restart_lxc () {
- lxc=$1; shift
- stop_lxc $lxc
- start_lxc $lxc
-}
-
-function destroy_all () {
-
- stop_all
- for i in $(lxc-ls -1|sort|uniq); do
- lxc-destroy -n $i
- done
-
+ lxc_name=$1; shift
+ stop_lxc $lxc_name
+ start_lxc $lxc_name
}
function destroy_lxc () {
- lxc=$1; shift
- stop_lxc $lxc
- lxc-destroy -n $lxc
+ lxc_name=$1; shift
+ stop_lxc $lxc_name
+ virsh -c lxc:/// undefine $lxc_name
+ rm -fr $path/$lxc_name
}
+####################
+commands="sense_all|start_all|stop_all|restart_all|destroy_all|sense_lxc|start_lxc|stop_lxc|restart_lxc|destroy_lxc"
+
function usage () {
echo "Usage: lxc-driver.sh [options]"
echo "Description:"
echo " This command is used to manage and retreive information on existing lxc containers "
echo "lxc-driver.sh -c <COMMAND>_all"
- echo "lxc-driver.sh -c <COMMAND>_lxc -l <LXCNAME>"
- echo "<COMMAND> in {sense,start,stop,restart,destroy}"
+ echo "lxc-driver.sh -c <COMMAND>_lxc -n <LXCNAME>"
+ echo "<COMMAND> in {$commands}"
}
case $command in
- sense_all) sense_all ;;
- start_all) start_all ;;
- stop_all) stop_all ;;
- restart_all) restart_all ;;
- destroy_all) destroy_all ;;
- sense_lxc) sense_lxc $lxc;;
- start_lxc) start_lxc $lxc;;
- stop_lxc) stop_lxc $lxc;;
- restart_lxc) restart_lxc $lxc;;
- destroy_lxc) destroy_lxc $lxc;;
- *) usage
+ sense_all|start_all|stop_all|restart_all|destroy_all|sense_lxc|start_lxc|stop_lxc|restart_lxc|destroy_lxc) $command ;;
+ *) usage
esac