summaryrefslogtreecommitdiffstats
path: root/startup/kvm-wrapper-startup.sh
blob: 92be7e10aac3ef926f5f97627469d32fdc25b11a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
### BEGIN INIT INFO
# Provides:          kvm-wrapper
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: kcm-wrapper init script
# Description:       This script starts a list of VMs and stops the running
#                    ones when asked to
### END INIT INFO

# -- bencoh, 2009/08/11
# -- Asmadeus, 2011/06

SCRIPTNAME=/etc/init.d/kvm-wrapper

. /lib/lsb/init-functions

KVM_WRAPPER_DIR=/usr/share/kvm-wrapper
KVM_WRAPPER="$KVM_WRAPPER_DIR"/kvm-wrapper.sh
KVM_VM_LIST="$KVM_WRAPPER_DIR"/startup/startup-list

start_vm()
{
	VM_NAME="$1"
	log_begin_msg "Starting up VM : $VM_NAME ..."
	$KVM_WRAPPER screen "$VM_NAME"
	EXITNUM="$?"
	echo $EXITNUM
	case "$EXITNUM" in 
		0) log_end_msg 0 ;;
		*) log_end_msg 1 ;;
	esac
	return 0
}

stop_vm ()
{
	VM_NAME="$1"
	log_begin_msg "Stopping VM : $VM_NAME ..."
	"$KVM_WRAPPER" stop "$VM_NAME"
	log_end_msg 0
}

do_start()
{
if [[ ! -f /usr/share/kvm-wrapper/kvm-wrapper.sh ]]; then
	log_begin_msg "Mounting /usr/share/kvm-wrapper since it doesn't seem here"
	echo
	mount /usr/share/kvm-wrapper
	sleep 3
fi

echo cleaning old pid files for `hostname -s`
rm -vf /usr/share/kvm-wrapper/run/`hostname -s`*

grep -E -v '^#' "$KVM_VM_LIST" |
while read line
do
	pcregrep "^KVM_CLUSTER_NODE=\"?`hostname -s`" $KVM_WRAPPER_DIR/vm/$line-vm >&/dev/null && \
	start_vm "$line"
done
}

do_stop()
{
"$KVM_WRAPPER" list|pcregrep "Running\ton (`hostname -s`|local)"|awk '{print $1}'|
while read line
do
	stop_vm "$line"
done
}

case "$1" in
  start)
	log_begin_msg "Autostarting VMs (kvm-wrapper) ..."
	echo
	do_start
	case "$?" in
		0|1) log_end_msg 0 ;;
		2) log_end_msg 1 ;;
	esac
	;;
  stop)
	log_begin_msg "Shutting down autostarted VMs (kvm-wrapper) ..."
	echo
	do_stop
	case "$?" in
		0|1) log_end_msg 0 ;;
		2) log_end_msg 1 ;;
	esac
	;;
  restart|force-reload)
	;;
  start-vm)
  	start_vm "$2"
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop}" >&2
	echo "       $SCRIPTNAME start-vm xxxxx" >&2
	exit 3
	;;
esac