3 function sense_all () {
4 virsh -c lxc:// list | grep running | while read line; do
5 pid=$(echo $line | cut -d' ' -f1)
6 lxc_name=$(echo $line | cut -d' ' -f2)
7 timestamp=$(cat /var/lib/lxc/$lxc_name/$lxc_name.timestamp)
8 echo "$lxc_name;$pid;$timestamp"
12 function start_all () {
13 virsh -c lxc:// list --inactive | grep " - "| while read line; do
14 lxc_name=$(echo $line | cut -d' ' -f2)
15 virsh -c lxc:// start $lxc_name
19 function stop_all () {
20 virsh -c lxc:// list | grep running | while read line; do
21 lxc_name=$(echo $line | cut -d' ' -f2)
22 virsh -c lxc:// destroy $lxc_name
26 function sense_lxc () {
29 if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" == "running" ] ; then
30 pid=$(virsh -c lxc:// dominfo $lxc_name| grep Id | cut -d' ' -f14)
31 timestamp=$(cat /var/lib/lxc/$lxc_name/$lxc_name.timestamp)
32 echo "$lxc_name;$pid;$timestamp"
36 function start_lxc () {
39 if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "running" ] ; then
40 virsh -c lxc:// start $lxc_name
44 function stop_lxc () {
47 if [ "$(virsh -c lxc:// dominfo $lxc_name | grep State| cut -d' ' -f11)" != "shut off" ] ; then
48 virsh -c lxc:// destroy $lxc_name
52 function restart_all () {
58 function restart_lxc () {
65 function destroy_all () {
68 virsh -c lxc:// list --all | while read line; do
69 lxc_name=$(echo $line | cut -d' ' -f2)
70 virsh -c lxc:// undefine $lxc_name
71 rm -fr /var/lib/lxc/$lxc_name
75 function destroy_lxc () {
79 virsh -c lxc:// undefine $lxc_name
80 rm -fr /var/lib/lxc/$lxc_name
84 echo "Usage: lxc-driver.sh [options]"
86 echo " This command is used to manage and retreive information on existing lxc containers "
87 echo "lxc-driver.sh -c <COMMAND>_all"
88 echo "lxc-driver.sh -c <COMMAND>_lxc -n <LXCNAME>"
89 echo "<COMMAND> in {sense,start,stop,restart,destroy}"
97 while getopts "c:n:" opt ; do
107 sense_all) sense_all ;;
108 start_all) start_all ;;
109 stop_all) stop_all ;;
110 restart_all) restart_all ;;
111 destroy_all) destroy_all ;;
112 sense_lxc) sense_lxc $lxc;;
113 start_lxc) start_lxc $lxc;;
114 stop_lxc) stop_lxc $lxc;;
115 restart_lxc) restart_lxc $lxc;;
116 destroy_lxc) destroy_lxc $lxc;;