stop 1.94 KB
Newer Older
Maria Matejka's avatar
INIT  
Maria Matejka committed
1 2 3 4 5 6 7
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib

NAME=$1
shift

if [ -z "$NAME" ]; then
8
	echo -e "Usage: $0 name\n    or $0 -m list of names"
Maria Matejka's avatar
INIT  
Maria Matejka committed
9 10 11
	exit 2
fi

12 13
if [ "$NAME" = "-m" ]; then
	set -e
14
  declare -a PIDS
15 16
	for N in "$@"; do
		echo Stopping "$N"
17 18
		$0 -q "$N" &
    PIDS+=($!)
19
	done
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  echo ${PIDS[@]}

  while true; do
    declare -a NEWPIDS
    NEWPIDS=()
    for P in ${PIDS[@]}; do
      if [ -d /proc/$P ]; then
        NEWPIDS+=($P)
      fi
    done
    PIDS=( ${NEWPIDS[@]} )
    if [ "${#PIDS[@]}" -eq 0 ]; then break; fi
    echo -e "Still ${#PIDS[@]} machine(s) running ... \r"
    sleep 1
  done
  echo "Everything OK.                               "
	exit 0 
fi

QUIET=false
if [ "$NAME" = "-q" ]; then
  QUIET=true
  NAME=$1
  shift
45 46
fi

Maria Matejka's avatar
INIT  
Maria Matejka committed
47
function ok() {
48
	E
Maria Matejka's avatar
INIT  
Maria Matejka committed
49
	rm -f $ROOT/run/$NAME.sock $ROOT/run/$NAME.pid
50

Maria Matejka's avatar
INIT  
Maria Matejka committed
51
	if gethost $NAME; then
52 53
    echo "$NAME"
    if [ -e $ROOT/run/tmp-$NAME ]; then rm $(readlink -f $ROOT/run/tmp-$NAME) $ROOT/run/tmp-$NAME; fi
Maria Matejka's avatar
Maria Matejka committed
54
		$ROOT/bin/omapi del $HOSTMAC
Maria Matejka's avatar
Maria Matejka committed
55
		vsctl del-port $HOSTTAP
Maria Matejka's avatar
Maria Matejka committed
56 57
		if [ -n "$HOSTNIC" ]; then
			for N in ${HOSTNIC[@]}; do
58
				IFS=';' read _ MAC TAG _ <<<"$N"
Maria Matejka's avatar
Maria Matejka committed
59
				TAPID=$(mactotap $MAC)
60 61 62
				E $TAPID
        brctl delif br-$TAG $TAPID
#				vsctl del-port $TAPID
63 64
			done
		fi
Maria Matejka's avatar
INIT  
Maria Matejka committed
65
	else
66
		E "Warning: Config not found for this host"
Maria Matejka's avatar
INIT  
Maria Matejka committed
67
	fi
68
	E "OK"
Maria Matejka's avatar
INIT  
Maria Matejka committed
69 70 71 72
	exit 0
}

if [ -e $ROOT/run/$NAME.sock ]; then
73
	E -n "Graceful powerdown "
Maria Matejka's avatar
INIT  
Maria Matejka committed
74 75 76
	echo "system_powerdown" | socat - UNIX-CONNECT:$ROOT/run/$NAME.sock >/dev/null
	if wait_pidfile $ROOT/run/$NAME.pid 30; then ok; fi
else
77
	E "No monitor socket found."
Maria Matejka's avatar
INIT  
Maria Matejka committed
78 79 80 81
fi

if [ -e $ROOT/run/$NAME.pid ]; then
	if [ ! -d /proc/$(<$ROOT/run/$NAME.pid) ]; then
82
		E "Stale pidfile found, cleaning up"
Maria Matejka's avatar
INIT  
Maria Matejka committed
83 84 85 86
		rm $ROOT/run/$NAME.pid
		return 0
	fi

87
	E -n "Sending TERM "
Maria Matejka's avatar
INIT  
Maria Matejka committed
88 89 90 91
	kill -15 $(<$ROOT/run/$NAME.pid)
	
	if wait_pidfile $ROOT/run/$NAME.pid 30; then ok; fi

92
	E -n "Sending KILL "
Maria Matejka's avatar
INIT  
Maria Matejka committed
93 94 95 96
	kill -9 $(<$ROOT/run/$NAME.pid)
	
	if wait_pidfile $ROOT/run/$NAME.pid 30; then ok; fi

97
	E "Did not stop even after KILL, strange."
Maria Matejka's avatar
INIT  
Maria Matejka committed
98 99
	exit 1
fi