fixed kill_qemus : uses qemu -pidfile for locating pids
[tests.git] / system / template-qemu / kill-qemu-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 $nodedir/shell.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     fi
35 }    
36
37 function kill_pids () {
38     hostnames="$@"
39     if [[ -n "$hostnames" ]] ; then
40         for hostname in $hostnames; do
41             nodedir=qemu-$hostname
42             kill_from_file $nodedir/qemu.pid
43             kill_from_file $nodedir/shell.pid
44         done
45     else
46         echo Killing all processes mathing qemu
47         pkill qemu
48     fi
49 }
50
51 function show_pids () {
52     pids=$(list_pids "$@")
53     if [ -n "$pids" ] ; then
54         ps $pids
55     else
56         echo Nothing to show
57     fi
58 }
59
60 function main () { 
61     while getopts "lk" opt; do
62         case $opt in
63             l) OPT_LIST=true ;;
64             k) OPT_GREP=true ;;
65             *) usage ;;
66         esac
67     done
68     shift $(($OPTIND -1))
69
70     # listing
71     if [ -n "$OPT_LIST" ] ; then
72         show_pids "$@"
73         exit 0
74     fi
75
76     kill_pids "$@"
77 }
78
79 main "$@"