3 # the following is the LSB init header
6 # Provides: libvirt-guests
7 # Required-Start: libvirtd
8 # Required-Stop: libvirtd
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
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
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
59 if [ "x$uri" = xdefault
]; then
65 virsh
$conn "$@" </dev
/null
69 ( export LC_ALL
=C
; run_virsh
"$@" )
75 list
=$
(run_virsh_c
$uri list
)
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
98 name
=$
(run_virsh_c
$uri dominfo
$uuid 2>/dev
/null | \
99 awk '/^Name:/{print $2}')
100 [ -n "$name" ] || name
=$uuid
110 info
=$
(run_virsh_c
$uri dominfo
$uuid)
111 if [ $?
-ne 0 ]; then
116 id
=$
(echo "$info" |
awk '/^Id:/{print $2}')
118 [ -n "$id" ] && [ "x$id" != x-
] && guest_running
=true
123 touch "$VAR_SUBSYS_LIBVIRT_GUESTS"
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"
136 while read uri list
; do
138 for confuri
in $URIS; do
139 if [ $confuri = $uri ]; then
144 if ! $configured; then
145 echo $
"Ignoring guests on $uri URI"
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"
157 retval run_virsh
$uri start
"$name" >/dev
/null
&& \
173 name
=$
(guest_name
$uri $guest)
174 label
=$
"Suspending $name: "
176 run_virsh
$uri managedsave
$guest >/dev
/null
&
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"
186 printf '\r%s%-12s ' "$label" "..."
189 retval
wait $virsh_pid && printf '\r%s%-12s\n' "$label" $
"done"
197 name
=$
(guest_name
$uri $guest)
198 label
=$
"Shutting down $name: "
200 retval run_virsh
$uri shutdown
$guest >/dev
/null ||
return
201 timeout
=$SHUTDOWN_TIMEOUT
202 while [ $timeout -gt 0 ]; do
204 timeout
=$
[timeout
- 1]
205 guest_is_on
$uri $guest ||
return
206 $guest_running ||
break
207 printf '\r%s%-12d ' "$label" $timeout
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"
214 printf '\r%s%-12s\n' "$label" $
"done"
220 # last stop was not followed by start
221 [ -f "$LISTFILE" ] && return 0
224 if [ "x$ON_SHUTDOWN" = xshutdown
]; then
226 if [ $SHUTDOWN_TIMEOUT -le 0 ]; then
227 echo $
"Shutdown action requested but SHUTDOWN_TIMEOUT was not set"
235 echo -n $
"Running guests on $uri URI: "
237 if [ "x$uri" = xdefault
] && [ ! -x "$libvirtd" ]; then
238 echo $
"libvirtd not installed; skipping this URI."
242 list
=$
(list_guests
$uri)
243 if [ $?
-eq 0 ]; then
245 for uuid
in $list; do
246 $empty ||
printf ", "
247 echo -n $
(guest_name
$uri $uuid)
251 echo $
"no running guests."
254 echo $uri $list >>"$LISTFILE"
259 while read uri list
; do
261 echo $
"Suspending guests on $uri URI..."
263 echo $
"Shutting down guests on $uri URI..."
266 for guest
in $list; do
268 suspend_guest
$uri $guest
270 shutdown_guest
$uri $guest
275 rm -f "$VAR_SUBSYS_LIBVIRT_GUESTS"
281 retval run_virsh
$uri list ||
echo
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.
290 if [ -f "$LISTFILE" ]; then
291 echo $
"stopped, with saved guests"
294 if [ -f "$VAR_SUBSYS_LIBVIRT_GUESTS" ]; then
297 echo $
"stopped, with no saved guests"
304 # Display usage string, then exit with VAL (defaults to 2).
306 echo $
"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|gueststatus|shutdown}"
310 # See how we were called.
311 if test $# != 1; then
318 start|stop|gueststatus
)
324 condrestart|try-restart
)
325 [ -f "$VAR_SUBSYS_LIBVIRT_GUESTS" ] && stop
&& start
328 # Nothing to do; we reread configuration on each invocation