use different methods for interacting with host machine virtual nodes are on
[tests.git] / system / template-qemu / qemu-kill-node
1 #!/bin/sh
2 # $Id$
3 COMMAND=$(basename $0)
4 cd $(dirname $0)
5 cd ..
6
7 function usage () {
8     echo "Usage: $COMMAND -l"
9     echo "  lists current qemu processes"
10     echo "usage: $COMMAND hostname"
11     echo "  kill qemu instance for that node"
12     exit 1
13 }
14
15 function list_pids () {
16     hostnames="$@"
17     if [[ -n "$hostnames" ]] ; then
18         for hostname in $hostnames; do
19             nodedir=qemu-$hostname
20             cat $nodedir/qemu.pid 2> /dev/null
21         done
22     else
23         pgrep qemu
24     fi
25 }
26
27 function kill_from_file () {
28     file=$1; shift
29     if [ -f $file ] ; then
30         pid=$(cat $file)
31         echo Killing $pid
32         kill $pid
33         mv $file $file.killed
34     else
35         echo "Could not find file $file - that qemu was killed already"
36     fi
37 }    
38
39 function kill_pids () {
40     hostnames="$@"
41     if [[ -n "$hostnames" ]] ; then
42         for hostname in $hostnames; do
43             nodedir=qemu-$hostname
44             kill_from_file $nodedir/qemu.pid
45         done
46     else
47         echo Killing all processes mathing qemu
48         pkill qemu
49     fi
50 }
51
52 function show_pids () {
53     pids=$(list_pids "$@")
54     if [ -n "$pids" ] ; then
55         ps $pids | grep -v $COMMAND || echo Nothing to show
56     else
57         echo Nothing to show
58     fi
59 }
60
61 function main () { 
62     while getopts "lk" opt; do
63         case $opt in
64             l) OPT_LIST=true ;;
65             k) OPT_GREP=true ;;
66             *) usage ;;
67         esac
68     done
69     shift $(($OPTIND -1))
70
71     # listing
72     if [ -n "$OPT_LIST" ] ; then
73         show_pids "$@"
74         exit 0
75     fi
76
77     kill_pids "$@"
78 }
79
80 main "$@"