5 #################### work on all containers - only deal with the ones that have a timestamp
6 function sense_all () {
7 virsh -c lxc:/// list 2> /dev/null | grep running | while read line; do
8 pid=$(echo $line | cut -d' ' -f1)
9 lxc_name=$(echo $line | cut -d' ' -f2)
10 # ignore regular vservers like testmaster and the like
11 timestamp_file=$path/$lxc_name/$lxc_name.timestamp
12 [ -f $timestamp_file ] || continue
13 timestamp=$(cat $timestamp_file 2> /dev/null)
14 echo "$lxc_name;$pid;$timestamp"
18 function start_all () {
19 virsh -c lxc:/// list --inactive | grep " - "| while read line; do
20 lxc_name=$(echo $line | cut -d' ' -f2)
21 # ignore regular vservers like testmaster and the like
22 timestamp_file=$path/$lxc_name/$lxc_name.timestamp
23 [ -f $timestamp_file ] || continue
24 virsh -c lxc:/// start $lxc_name
28 function stop_all () {
29 virsh -c lxc:/// list | grep running | while read line; do
30 lxc_name=$(echo $line | cut -d' ' -f2)
31 # ignore regular vservers like testmaster and the like
32 timestamp_file=$path/$lxc_name/$lxc_name.timestamp
33 [ -f $timestamp_file ] || continue
34 virsh -c lxc:/// destroy $lxc_name
38 function destroy_all () {
41 virsh -c lxc:/// list --all | while read line; do
42 lxc_name=$(echo $line | cut -d' ' -f2)
43 # ignore regular vservers like testmaster and the like
44 timestamp_file=$path/$lxc_name/$lxc_name.timestamp
45 [ -f $timestamp_file ] || continue
46 virsh -c lxc:/// undefine $lxc_name
47 rm -fr $path/$lxc_name
51 function restart_all () {
57 #################### deal with one user-specified container
58 function sense_lxc () {
61 if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" == "running" ] ; then
62 pid=$(virsh -c lxc:/// dominfo $lxc_name| grep Id | cut -d' ' -f14)
63 timestamp_file=$path/$lxc_name/$lxc_name.timestamp
64 timestamp=$(cat $timestamp_file)
65 echo "$lxc_name;$pid;$timestamp"
69 function start_lxc () {
72 if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "running" ] ; then
73 virsh -c lxc:/// start $lxc_name
77 function stop_lxc () {
80 if [ "$(virsh -c lxc:/// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "shut off" ] ; then
81 virsh -c lxc:/// destroy $lxc_name
85 function restart_lxc () {
92 function destroy_lxc () {
96 virsh -c lxc:/// undefine $lxc_name
97 rm -fr $path/$lxc_name
101 commands="sense_all|start_all|stop_all|restart_all|destroy_all|sense_lxc|start_lxc|stop_lxc|restart_lxc|destroy_lxc"
104 echo "Usage: lxc-driver.sh [options]"
106 echo " This command is used to manage and retreive information on existing lxc containers "
107 echo "lxc-driver.sh -c <COMMAND>_all"
108 echo "lxc-driver.sh -c <COMMAND>_lxc -n <LXCNAME>"
109 echo "<COMMAND> in {$commands}"
117 while getopts "c:n:" opt ; do
127 sense_all|start_all|stop_all|restart_all|destroy_all|sense_lxc|start_lxc|stop_lxc|restart_lxc|destroy_lxc) $command ;;