TEMP comment out libvirtd.stp
[libvirt/apevec.git] / tools / libvirt-guests.init.in
blob4e0682d996b3fe4de07ff3e33e1c3f3007d18dcc
1 #!/bin/sh
3 # the following is the LSB init header
5 ### BEGIN INIT INFO
6 # Provides: libvirt-guests
7 # Required-Start: libvirtd
8 # Required-Stop: libvirtd
9 # Default-Start: 3 4 5
10 # Short-Description: suspend/resume libvirt guests on shutdown/boot
11 # Description: This is a script for suspending active libvirt guests
12 # on shutdown and resuming them on next boot
13 # See http://libvirt.org
14 ### END INIT INFO
16 # the following is chkconfig init header
18 # libvirt-guests: suspend/resume libvirt guests on shutdown/boot
20 # chkconfig: 345 99 01
21 # description: This is a script for suspending active libvirt guests \
22 # on shutdown and resuming them on next boot \
23 # See http://libvirt.org
26 sysconfdir=@sysconfdir@
27 localstatedir=@localstatedir@
28 libvirtd=@sbindir@/libvirtd
30 # Source function library.
31 . "$sysconfdir"/rc.d/init.d/functions
33 URIS=default
34 ON_BOOT=start
35 ON_SHUTDOWN=suspend
36 SHUTDOWN_TIMEOUT=0
38 test -f "$sysconfdir"/sysconfig/libvirt-guests && . "$sysconfdir"/sysconfig/libvirt-guests
40 LISTFILE="$localstatedir"/lib/libvirt/libvirt-guests
41 VAR_SUBSYS_LIBVIRT_GUESTS="$localstatedir"/lock/subsys/libvirt-guests
43 RETVAL=0
45 retval() {
46 "$@"
47 if [ $? -ne 0 ]; then
48 RETVAL=1
49 return 1
50 else
51 return 0
55 run_virsh() {
56 uri=$1
57 shift
59 if [ "x$uri" = xdefault ]; then
60 conn=
61 else
62 conn="-c $uri"
65 virsh $conn "$@" </dev/null
68 run_virsh_c() {
69 ( export LC_ALL=C; run_virsh "$@" )
72 list_guests() {
73 uri=$1
75 list=$(run_virsh_c $uri list)
76 if [ $? -ne 0 ]; then
77 RETVAL=1
78 return 1
81 uuids=
82 for id in $(echo "$list" | awk 'NR > 2 {print $1}'); do
83 uuid=$(run_virsh_c $uri dominfo $id | awk '/^UUID:/{print $2}')
84 if [ -z "$uuid" ]; then
85 RETVAL=1
86 return 1
88 uuids="$uuids $uuid"
89 done
91 echo $uuids
94 guest_name() {
95 uri=$1
96 uuid=$2
98 name=$(run_virsh_c $uri dominfo $uuid 2>/dev/null | \
99 awk '/^Name:/{print $2}')
100 [ -n "$name" ] || name=$uuid
102 echo "$name"
105 guest_is_on() {
106 uri=$1
107 uuid=$2
109 guest_running=false
110 info=$(run_virsh_c $uri dominfo $uuid)
111 if [ $? -ne 0 ]; then
112 RETVAL=1
113 return 1
116 id=$(echo "$info" | awk '/^Id:/{print $2}')
118 [ -n "$id" ] && [ "x$id" != x- ] && guest_running=true
119 return 0
122 started() {
123 touch "$VAR_SUBSYS_LIBVIRT_GUESTS"
126 start() {
127 [ -f "$LISTFILE" ] || { started; return 0; }
129 if [ "x$ON_BOOT" != xstart ]; then
130 echo $"libvirt-guests is configured not to start any guests on boot"
131 rm -f "$LISTFILE"
132 started
133 return 0
136 while read uri list; do
137 configured=false
138 for confuri in $URIS; do
139 if [ $confuri = $uri ]; then
140 configured=true
141 break
143 done
144 if ! $configured; then
145 echo $"Ignoring guests on $uri URI"
146 continue
149 echo $"Resuming guests on $uri URI..."
150 for guest in $list; do
151 name=$(guest_name $uri $guest)
152 echo -n $"Resuming guest $name: "
153 if guest_is_on $uri $guest; then
154 if $guest_running; then
155 echo $"already active"
156 else
157 retval run_virsh $uri start "$name" >/dev/null && \
158 echo $"done"
161 done
162 done <"$LISTFILE"
164 rm -f "$LISTFILE"
165 started
168 suspend_guest()
170 uri=$1
171 guest=$2
173 name=$(guest_name $uri $guest)
174 label=$"Suspending $name: "
175 echo -n "$label"
176 run_virsh $uri managedsave $guest >/dev/null &
177 virsh_pid=$!
178 while true; do
179 sleep 1
180 kill -0 $virsh_pid >&/dev/null || break
181 progress=$(run_virsh_c $uri domjobinfo $guest 2>/dev/null | \
182 awk '/^Data processed:/{print $3, $4}')
183 if [ -n "$progress" ]; then
184 printf '\r%s%12s ' "$label" "$progress"
185 else
186 printf '\r%s%-12s ' "$label" "..."
188 done
189 retval wait $virsh_pid && printf '\r%s%-12s\n' "$label" $"done"
192 shutdown_guest()
194 uri=$1
195 guest=$2
197 name=$(guest_name $uri $guest)
198 label=$"Shutting down $name: "
199 echo -n "$label"
200 retval run_virsh $uri shutdown $guest >/dev/null || return
201 timeout=$SHUTDOWN_TIMEOUT
202 while [ $timeout -gt 0 ]; do
203 sleep 1
204 timeout=$[timeout - 1]
205 guest_is_on $uri $guest || return
206 $guest_running || break
207 printf '\r%s%-12d ' "$label" $timeout
208 done
210 if guest_is_on $uri $guest; then
211 if $guest_running; then
212 printf '\r%s%-12s\n' "$label" $"failed to shutdown in time"
213 else
214 printf '\r%s%-12s\n' "$label" $"done"
219 stop() {
220 # last stop was not followed by start
221 [ -f "$LISTFILE" ] && return 0
223 suspending=true
224 if [ "x$ON_SHUTDOWN" = xshutdown ]; then
225 suspending=false
226 if [ $SHUTDOWN_TIMEOUT -le 0 ]; then
227 echo $"Shutdown action requested but SHUTDOWN_TIMEOUT was not set"
228 RETVAL=6
229 return
233 : >"$LISTFILE"
234 for uri in $URIS; do
235 echo -n $"Running guests on $uri URI: "
237 if [ "x$uri" = xdefault ] && [ ! -x "$libvirtd" ]; then
238 echo $"libvirtd not installed; skipping this URI."
239 continue
242 list=$(list_guests $uri)
243 if [ $? -eq 0 ]; then
244 empty=true
245 for uuid in $list; do
246 $empty || printf ", "
247 echo -n $(guest_name $uri $uuid)
248 empty=false
249 done
250 if $empty; then
251 echo $"no running guests."
252 else
253 echo
254 echo $uri $list >>"$LISTFILE"
257 done
259 while read uri list; do
260 if $suspending; then
261 echo $"Suspending guests on $uri URI..."
262 else
263 echo $"Shutting down guests on $uri URI..."
266 for guest in $list; do
267 if $suspending; then
268 suspend_guest $uri $guest
269 else
270 shutdown_guest $uri $guest
272 done
273 done <"$LISTFILE"
275 rm -f "$VAR_SUBSYS_LIBVIRT_GUESTS"
278 gueststatus() {
279 for uri in $URIS; do
280 echo "* $uri URI:"
281 retval run_virsh $uri list || echo
282 done
285 # rh_status
286 # Display current status: whether saved state exists, and whether start
287 # has been executed. We cannot use status() from the functions library,
288 # since there is no external daemon process matching this init script.
289 rh_status() {
290 if [ -f "$LISTFILE" ]; then
291 echo $"stopped, with saved guests"
292 RETVAL=3
293 else
294 if [ -f "$VAR_SUBSYS_LIBVIRT_GUESTS" ]; then
295 echo $"started"
296 else
297 echo $"stopped, with no saved guests"
299 RETVAL=0
303 # usage [val]
304 # Display usage string, then exit with VAL (defaults to 2).
305 usage() {
306 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|gueststatus|shutdown}"
307 exit ${1-2}
310 # See how we were called.
311 if test $# != 1; then
312 usage
314 case "$1" in
315 --help)
316 usage 0
318 start|stop|gueststatus)
321 restart)
322 stop && start
324 condrestart|try-restart)
325 [ -f "$VAR_SUBSYS_LIBVIRT_GUESTS" ] && stop && start
327 reload|force-reload)
328 # Nothing to do; we reread configuration on each invocation
330 status)
331 rh_status
333 shutdown)
334 ON_SHUTDOWN=shutdown
335 stop
338 usage
340 esac
341 exit $RETVAL