summaryrefslogtreecommitdiff
path: root/community/libvirt/libvirtd-guests.rc.d
blob: 3100b34fb1f075be7be4247b73bdd78597f45318 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash

source /etc/conf.d/libvirtd-guests
[ -f /etc/rc.conf ] && source /etc/rc.conf

if [ -f /etc/rc.d/functions ]; then
    . /etc/rc.d/functions
else
    stat_busy() {
        echo "$*"
    }

    stat_fail() {
        echo "FAIL"
    }

    stat_done() {
        echo "DONE"
    }

    add_daemon() {
        true
    }

    rm_daemon() {
        true
    }
fi


LIBVIRTD_LISTFILE="/var/state/libvirtd/vm-list"

# get guest state by name
libvirt_get_guest_state()
{
	virsh $LIBVIRTD_URI dominfo "$1" | grep -E '^State:' | awk '{print $2}'
}

# list IDs of running guests
libvirt_list()
{

	list=$(virsh $LIBVIRTD_URI list)

	if [ $? -ne 0 ]; then
		RETVAL=1
		return 1
	fi

	uuids=
	for id in $(echo "$list" | awk 'NR > 2 {print $1}'); do
		uuid=$(virsh $LIBVIRTD_UTI dominfo $id | awk '/^UUID:/{print $2}')
		if [ -z "$uuid" ]; then
			RETVAL=1
			return 1
		fi
		uuids="$uuids $uuid"
	done

	echo $uuids

}

libvirt_domname()
{
	uuid=$1
	name=$(virsh $LIBVIRTD_URI dominfo $uuid | awk 'NR == 2 {$1=""; print}')

	echo $name
}

# suspend guest by name
libvirt_suspend()
{
	virsh $LIBVIRTD_URI $LIBVIRTD_BYPASS_CACHE managedsave "$1" >/dev/null
	timeout=$LIBVIRTD_SHUTDOWN_TIMEOUT
	while [ "$timeout" -gt 0 ]; do
		sleep 1
		timeout=$((timeout - 1))
		state=`libvirt_get_guest_state "$1"`
		[ "x$state" == "xshut" ] && return 0
	done
	return 1
}

# shutdown guest by name
libvirt_shutdown()
{
	virsh $LIBVIRTD_URI shutdown "$1" >/dev/null
	timeout=$LIBVIRTD_SHUTDOWN_TIMEOUT
	while [ "$timeout" -gt 0 ]; do
		sleep 1
		timeout=$((timeout - 1))
		state=`libvirt_get_guest_state "$1"`
		[ "x$state" == "xshut" ] && return 0
	done
	return 1
}

# start guest by name
libvirt_start()
{
	virsh $LIBVIRTD_URI $LIBVIRTD_BYPASS_CACHE start "$1" >/dev/null
}

# stop all guests
libvirt_stop_all()
{
	mkdir -p `dirname $LIBVIRTD_LISTFILE`
	echo -n >$LIBVIRTD_LISTFILE

	for i in `libvirt_list`; do
		name=`libvirt_domname $i`
		if [ "x$LIBVIRTD_STOP_ACTION" == "xsuspend" ]; then
			stat_busy "Suspending libvirtd/$name guest"
			libvirt_suspend "$i"
		else
			stat_busy "Shutting libvirtd/$i guest down"
			libvirt_shutdown "$i"
		fi
		[ $? -eq 0 ] && stat_done || stat_fail
		echo $i >>$LIBVIRTD_LISTFILE
	done
}

# start all guests
libvirt_start_all()
{
	if [ -f $LIBVIRTD_LISTFILE ]; then
		for i in `cat $LIBVIRTD_LISTFILE`; do
			name=`libvirt_domname $i`
			stat_busy "Starting/resuming libvirtd/$name guest"
			libvirt_start "$i"
			[ $? -eq 0 ] && { sleep $LIBVIRTD_START_DELAY; stat_done; } || stat_fail
		done
	fi
	rm -f $LIBVIRTD_LISTFILE
}

# main
LC_ALL=C
LANG=C
case "$1" in
    start)
		libvirt_start_all
		add_daemon libvirtd-guests
	;;
    stop)
		libvirt_stop_all
		rm_daemon libvirtd-guests
	;;
    restart)
	$0 stop
	sleep 1
	$0 start
	;;
    *)
	echo $"Usage: $0 {start|stop|restart}"
	;;
esac
exit 0