2010-05-14 21:37:55 +08:00
|
|
|
#!/bin/sh
|
|
|
|
|
2014-07-25 17:46:10 +08:00
|
|
|
# Copyright (C) 2011-2014 Red Hat, Inc.
|
2013-05-15 07:42:12 +08:00
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library. If not, see
|
|
|
|
# <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-01-05 02:13:56 +08:00
|
|
|
sysconfdir="@sysconfdir@"
|
|
|
|
localstatedir="@localstatedir@"
|
|
|
|
libvirtd="@sbindir@"/libvirtd
|
2010-05-14 21:37:55 +08:00
|
|
|
|
|
|
|
# Source function library.
|
2010-12-18 08:26:03 +08:00
|
|
|
test ! -r "$sysconfdir"/rc.d/init.d/functions ||
|
2011-01-05 02:13:56 +08:00
|
|
|
. "$sysconfdir"/rc.d/init.d/functions
|
|
|
|
|
|
|
|
# Source gettext library.
|
|
|
|
# Make sure this file is recognized as having translations: _("dummy")
|
|
|
|
. "@bindir@"/gettext.sh
|
|
|
|
|
|
|
|
export TEXTDOMAIN="@PACKAGE@" TEXTDOMAINDIR="@localedir@"
|
2010-05-14 21:37:55 +08:00
|
|
|
|
|
|
|
URIS=default
|
|
|
|
ON_BOOT=start
|
|
|
|
ON_SHUTDOWN=suspend
|
2012-02-28 23:09:42 +08:00
|
|
|
SHUTDOWN_TIMEOUT=300
|
|
|
|
PARALLEL_SHUTDOWN=0
|
2011-04-15 16:57:06 +08:00
|
|
|
START_DELAY=0
|
2011-07-15 07:22:53 +08:00
|
|
|
BYPASS_CACHE=0
|
2014-02-21 19:46:08 +08:00
|
|
|
CONNECT_RETRIES=10
|
|
|
|
RETRIES_SLEEP=1
|
2010-05-14 21:37:55 +08:00
|
|
|
|
2011-01-05 02:13:56 +08:00
|
|
|
test -f "$sysconfdir"/sysconfig/libvirt-guests &&
|
|
|
|
. "$sysconfdir"/sysconfig/libvirt-guests
|
2010-05-14 21:37:55 +08:00
|
|
|
|
|
|
|
LISTFILE="$localstatedir"/lib/libvirt/libvirt-guests
|
2010-05-27 20:47:11 +08:00
|
|
|
VAR_SUBSYS_LIBVIRT_GUESTS="$localstatedir"/lock/subsys/libvirt-guests
|
2010-05-14 21:37:55 +08:00
|
|
|
|
|
|
|
RETVAL=0
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# retval COMMAND ARGUMENTS...
|
|
|
|
# run command with arguments and convert non-zero return value to 1 and set
|
|
|
|
# the global return variable
|
2010-05-14 21:37:55 +08:00
|
|
|
retval() {
|
|
|
|
"$@"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
RETVAL=1
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# run_virsh URI ARGUMENTS...
|
|
|
|
# start virsh and let it execute ARGUMENTS on URI
|
|
|
|
# If URI is "default" virsh is called without the "-c" argument
|
|
|
|
# (using libvirt's default connection)
|
2010-05-14 21:37:55 +08:00
|
|
|
run_virsh() {
|
|
|
|
uri=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
if [ "x$uri" = xdefault ]; then
|
2011-03-09 16:54:57 +08:00
|
|
|
virsh "$@" </dev/null
|
2010-05-14 21:37:55 +08:00
|
|
|
else
|
2011-03-09 16:54:57 +08:00
|
|
|
virsh -c "$uri" "$@" </dev/null
|
2010-05-14 21:37:55 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# run_virsh_c URI ARGUMENTS
|
|
|
|
# Same as "run_virsh" but the "C" locale is used instead of
|
|
|
|
# the system's locale.
|
2010-05-14 21:37:55 +08:00
|
|
|
run_virsh_c() {
|
|
|
|
( export LC_ALL=C; run_virsh "$@" )
|
|
|
|
}
|
|
|
|
|
2012-02-28 21:39:17 +08:00
|
|
|
# test_connect URI
|
|
|
|
# check if URI is reachable
|
|
|
|
test_connect()
|
|
|
|
{
|
|
|
|
uri=$1
|
|
|
|
|
2014-04-07 17:11:07 +08:00
|
|
|
i=${CONNECT_RETRIES}
|
|
|
|
while [ $i -gt 0 ]; do
|
2014-02-21 19:46:08 +08:00
|
|
|
run_virsh "$uri" connect 2>/dev/null
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
sleep ${RETRIES_SLEEP}
|
|
|
|
eval_gettext "Unable to connect to libvirt currently. Retrying .. \$i"
|
2014-04-07 17:11:07 +08:00
|
|
|
i=$(($i-1))
|
2014-02-21 19:46:08 +08:00
|
|
|
done
|
|
|
|
eval_gettext "Can't connect to \$uri. Skipping."
|
|
|
|
echo
|
|
|
|
return 1
|
2012-02-28 21:39:17 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# list_guests URI PERSISTENT
|
|
|
|
# List running guests on URI.
|
|
|
|
# PERSISTENT argument options:
|
|
|
|
# --persistent: list only persistent guests
|
|
|
|
# --transient: list only transient guests
|
|
|
|
# [none]: list both persistent and transient guests
|
2010-05-14 21:37:55 +08:00
|
|
|
list_guests() {
|
|
|
|
uri=$1
|
2012-02-28 20:58:59 +08:00
|
|
|
persistent=$2
|
2010-05-14 21:37:55 +08:00
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
list=$(run_virsh_c "$uri" list --uuid $persistent)
|
2010-05-14 21:37:55 +08:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
RETVAL=1
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
echo $list
|
2010-05-14 21:37:55 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# guest_name URI UUID
|
|
|
|
# return name of guest UUID on URI
|
2010-05-14 21:37:55 +08:00
|
|
|
guest_name() {
|
|
|
|
uri=$1
|
|
|
|
uuid=$2
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
run_virsh "$uri" domname "$uuid" 2>/dev/null
|
2010-05-14 21:37:55 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# guest_is_on URI UUID
|
|
|
|
# check if guest UUID on URI is running
|
|
|
|
# Result is returned by variable "guest_running"
|
2010-05-14 21:37:55 +08:00
|
|
|
guest_is_on() {
|
|
|
|
uri=$1
|
|
|
|
uuid=$2
|
|
|
|
|
|
|
|
guest_running=false
|
2012-02-28 20:58:59 +08:00
|
|
|
id=$(run_virsh "$uri" domid "$uuid")
|
2010-05-14 21:37:55 +08:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
RETVAL=1
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ -n "$id" ] && [ "x$id" != x- ] && guest_running=true
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# started
|
|
|
|
# Create the startup lock file
|
2010-05-27 20:47:11 +08:00
|
|
|
started() {
|
|
|
|
touch "$VAR_SUBSYS_LIBVIRT_GUESTS"
|
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# start
|
|
|
|
# Start or resume the guests
|
2010-05-14 21:37:55 +08:00
|
|
|
start() {
|
2010-05-27 20:47:11 +08:00
|
|
|
[ -f "$LISTFILE" ] || { started; return 0; }
|
2010-05-14 21:37:55 +08:00
|
|
|
|
|
|
|
if [ "x$ON_BOOT" != xstart ]; then
|
2011-01-05 02:13:56 +08:00
|
|
|
gettext "libvirt-guests is configured not to start any guests on boot"
|
|
|
|
echo
|
2010-05-27 20:47:11 +08:00
|
|
|
rm -f "$LISTFILE"
|
|
|
|
started
|
2010-05-14 21:37:55 +08:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2011-04-15 16:57:06 +08:00
|
|
|
isfirst=true
|
2011-07-15 07:22:53 +08:00
|
|
|
bypass=
|
2015-02-18 18:59:22 +08:00
|
|
|
sync_time=false
|
2011-07-15 07:22:53 +08:00
|
|
|
test "x$BYPASS_CACHE" = x0 || bypass=--bypass-cache
|
2015-02-18 18:59:22 +08:00
|
|
|
test "x$SYNC_TIME" = x0 || sync_time=true
|
2010-05-14 21:37:55 +08:00
|
|
|
while read uri list; do
|
|
|
|
configured=false
|
2011-03-12 05:06:09 +08:00
|
|
|
set -f
|
2010-05-14 21:37:55 +08:00
|
|
|
for confuri in $URIS; do
|
2011-03-12 05:06:09 +08:00
|
|
|
set +f
|
2011-03-09 16:54:57 +08:00
|
|
|
if [ "x$confuri" = "x$uri" ]; then
|
2010-05-14 21:37:55 +08:00
|
|
|
configured=true
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2011-03-12 05:06:09 +08:00
|
|
|
set +f
|
2011-03-09 16:54:57 +08:00
|
|
|
if ! "$configured"; then
|
2011-01-05 02:13:56 +08:00
|
|
|
eval_gettext "Ignoring guests on \$uri URI"; echo
|
2010-05-14 21:37:55 +08:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2012-02-28 21:39:17 +08:00
|
|
|
test_connect "$uri" || continue
|
|
|
|
|
2011-01-05 02:13:56 +08:00
|
|
|
eval_gettext "Resuming guests on \$uri URI..."; echo
|
2010-05-14 21:37:55 +08:00
|
|
|
for guest in $list; do
|
2011-03-09 16:54:57 +08:00
|
|
|
name=$(guest_name "$uri" "$guest")
|
2011-01-05 02:13:56 +08:00
|
|
|
eval_gettext "Resuming guest \$name: "
|
2011-03-09 16:54:57 +08:00
|
|
|
if guest_is_on "$uri" "$guest"; then
|
|
|
|
if "$guest_running"; then
|
2011-01-05 02:13:56 +08:00
|
|
|
gettext "already active"; echo
|
2010-05-14 21:37:55 +08:00
|
|
|
else
|
2011-04-15 16:57:06 +08:00
|
|
|
if "$isfirst"; then
|
|
|
|
isfirst=false
|
|
|
|
else
|
|
|
|
sleep $START_DELAY
|
|
|
|
fi
|
2011-07-15 07:22:53 +08:00
|
|
|
retval run_virsh "$uri" start $bypass "$name" \
|
|
|
|
>/dev/null && \
|
2011-01-05 02:13:56 +08:00
|
|
|
gettext "done"; echo
|
2015-02-18 18:59:22 +08:00
|
|
|
if "$sync_time"; then
|
|
|
|
run_virsh "$uri" domtime --sync "$name" >/dev/null
|
|
|
|
fi
|
2010-05-14 21:37:55 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2010-05-27 20:47:11 +08:00
|
|
|
done <"$LISTFILE"
|
2010-05-14 21:37:55 +08:00
|
|
|
|
2010-05-27 20:47:11 +08:00
|
|
|
rm -f "$LISTFILE"
|
|
|
|
started
|
2010-05-14 21:37:55 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# suspend_guest URI GUEST
|
|
|
|
# Do a managed save on a GUEST on URI. This function returns after the guest
|
|
|
|
# was saved.
|
2010-05-14 21:37:55 +08:00
|
|
|
suspend_guest()
|
|
|
|
{
|
|
|
|
uri=$1
|
|
|
|
guest=$2
|
|
|
|
|
2011-03-09 16:54:57 +08:00
|
|
|
name=$(guest_name "$uri" "$guest")
|
2011-01-05 02:13:56 +08:00
|
|
|
label=$(eval_gettext "Suspending \$name: ")
|
2011-07-15 07:22:53 +08:00
|
|
|
bypass=
|
2012-08-21 23:03:40 +08:00
|
|
|
slept=0
|
2011-07-15 07:22:53 +08:00
|
|
|
test "x$BYPASS_CACHE" = x0 || bypass=--bypass-cache
|
2012-08-21 23:03:40 +08:00
|
|
|
printf '%s...\n' "$label"
|
2011-07-15 07:22:53 +08:00
|
|
|
run_virsh "$uri" managedsave $bypass "$guest" >/dev/null &
|
2010-05-14 21:37:55 +08:00
|
|
|
virsh_pid=$!
|
|
|
|
while true; do
|
|
|
|
sleep 1
|
2011-03-09 16:54:57 +08:00
|
|
|
kill -0 "$virsh_pid" >/dev/null 2>&1 || break
|
2012-08-21 23:03:40 +08:00
|
|
|
|
|
|
|
slept=$(($slept + 1))
|
|
|
|
if [ $(($slept % 5)) -eq 0 ]; then
|
|
|
|
progress=$(run_virsh_c "$uri" domjobinfo "$guest" 2>/dev/null | \
|
|
|
|
awk '/^Data processed:/{print $3, $4}')
|
|
|
|
if [ -n "$progress" ]; then
|
|
|
|
printf '%s%s\n' "$label" "$progress"
|
|
|
|
else
|
|
|
|
printf '%s%s\n' "$label" "..."
|
|
|
|
fi
|
2010-05-14 21:37:55 +08:00
|
|
|
fi
|
|
|
|
done
|
2012-08-21 23:03:40 +08:00
|
|
|
retval wait "$virsh_pid" && printf '%s%s\n' "$label" "$(gettext "done")"
|
2010-05-14 21:37:55 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# shutdown_guest URI GUEST
|
2014-07-25 17:46:10 +08:00
|
|
|
# Start an ACPI shutdown of GUEST on URI. This function returns after the guest
|
|
|
|
# was successfully shutdown or the timeout defined by $SHUTDOWN_TIMEOUT expired.
|
2010-05-14 21:37:55 +08:00
|
|
|
shutdown_guest()
|
|
|
|
{
|
|
|
|
uri=$1
|
|
|
|
guest=$2
|
|
|
|
|
2011-03-09 16:54:57 +08:00
|
|
|
name=$(guest_name "$uri" "$guest")
|
2012-08-21 23:03:40 +08:00
|
|
|
eval_gettext "Starting shutdown on guest: \$name"
|
|
|
|
echo
|
2011-03-09 16:54:57 +08:00
|
|
|
retval run_virsh "$uri" shutdown "$guest" >/dev/null || return
|
2010-05-14 21:37:55 +08:00
|
|
|
timeout=$SHUTDOWN_TIMEOUT
|
2012-02-28 23:09:42 +08:00
|
|
|
check_timeout=false
|
|
|
|
if [ $timeout -gt 0 ]; then
|
|
|
|
check_timeout=true
|
2012-08-21 23:03:40 +08:00
|
|
|
format=$(eval_gettext "Waiting for guest %s to shut down, %d seconds left\n")
|
|
|
|
else
|
|
|
|
slept=0
|
|
|
|
format=$(eval_gettext "Waiting for guest %s to shut down\n")
|
2012-02-28 23:09:42 +08:00
|
|
|
fi
|
|
|
|
while ! $check_timeout || [ "$timeout" -gt 0 ]; do
|
2010-05-14 21:37:55 +08:00
|
|
|
sleep 1
|
2011-03-09 16:54:57 +08:00
|
|
|
guest_is_on "$uri" "$guest" || return
|
|
|
|
"$guest_running" || break
|
2012-08-21 23:03:40 +08:00
|
|
|
|
2012-02-28 23:09:42 +08:00
|
|
|
if $check_timeout; then
|
2012-08-21 23:03:40 +08:00
|
|
|
if [ $(($timeout % 5)) -eq 0 ]; then
|
|
|
|
printf "$format" "$name" "$timeout"
|
|
|
|
fi
|
|
|
|
timeout=$(($timeout - 1))
|
|
|
|
else
|
|
|
|
slept=$(($slept + 1))
|
|
|
|
if [ $(($slept % 5)) -eq 0 ]; then
|
|
|
|
printf "$format" "$name"
|
|
|
|
fi
|
2012-02-28 23:09:42 +08:00
|
|
|
fi
|
2010-05-14 21:37:55 +08:00
|
|
|
done
|
|
|
|
|
2011-03-09 16:54:57 +08:00
|
|
|
if guest_is_on "$uri" "$guest"; then
|
|
|
|
if "$guest_running"; then
|
2012-08-21 23:03:40 +08:00
|
|
|
eval_gettext "Shutdown of guest \$name failed to complete in time."
|
2010-05-14 21:37:55 +08:00
|
|
|
else
|
2012-08-21 23:03:40 +08:00
|
|
|
eval_gettext "Shutdown of guest \$name complete."
|
2010-05-14 21:37:55 +08:00
|
|
|
fi
|
2013-03-20 22:54:08 +08:00
|
|
|
echo
|
2010-05-14 21:37:55 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-02-28 23:09:42 +08:00
|
|
|
# shutdown_guest_async URI GUEST
|
|
|
|
# Start a ACPI shutdown of GUEST on URI. This function returns after the command
|
|
|
|
# was issued to libvirt to allow parallel shutdown.
|
|
|
|
shutdown_guest_async()
|
|
|
|
{
|
|
|
|
uri=$1
|
|
|
|
guest=$2
|
|
|
|
|
|
|
|
name=$(guest_name "$uri" "$guest")
|
|
|
|
eval_gettext "Starting shutdown on guest: \$name"
|
|
|
|
echo
|
|
|
|
retval run_virsh "$uri" shutdown "$guest" > /dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
# guest_count GUEST_LIST
|
|
|
|
# Returns number of guests in GUEST_LIST
|
|
|
|
guest_count()
|
|
|
|
{
|
|
|
|
set -- $1
|
|
|
|
echo $#
|
|
|
|
}
|
|
|
|
|
|
|
|
# check_guests_shutdown URI GUESTS
|
|
|
|
# check if shutdown is complete on guests in "GUESTS" and returns only
|
|
|
|
# guests that are still shutting down
|
|
|
|
check_guests_shutdown()
|
|
|
|
{
|
|
|
|
uri=$1
|
|
|
|
guests=$2
|
|
|
|
|
|
|
|
guests_up=
|
|
|
|
for guest in $guests; do
|
|
|
|
if ! guest_is_on "$uri" "$guest" >/dev/null 2>&1; then
|
|
|
|
eval_gettext "Failed to determine state of guest: \$guest. Not tracking it anymore."
|
|
|
|
echo
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if "$guest_running"; then
|
|
|
|
guests_up="$guests_up $guest"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo "$guests_up"
|
|
|
|
}
|
|
|
|
|
|
|
|
# print_guests_shutdown URI BEFORE AFTER
|
|
|
|
# Checks for differences in the lists BEFORE and AFTER and prints
|
|
|
|
# a shutdown complete notice for guests that have finished
|
|
|
|
print_guests_shutdown()
|
|
|
|
{
|
|
|
|
uri=$1
|
|
|
|
before=$2
|
|
|
|
after=$3
|
|
|
|
|
|
|
|
for guest in $before; do
|
|
|
|
case " $after " in
|
|
|
|
*" $guest "*) continue;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
name=$(guest_name "$uri" "$guest")
|
|
|
|
eval_gettext "Shutdown of guest \$name complete."
|
|
|
|
echo
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# shutdown_guests_parallel URI GUESTS
|
|
|
|
# Shutdown guests GUESTS on machine URI in parallel
|
|
|
|
shutdown_guests_parallel()
|
|
|
|
{
|
|
|
|
uri=$1
|
|
|
|
guests=$2
|
|
|
|
|
|
|
|
on_shutdown=
|
|
|
|
check_timeout=false
|
|
|
|
timeout=$SHUTDOWN_TIMEOUT
|
|
|
|
if [ $timeout -gt 0 ]; then
|
|
|
|
check_timeout=true
|
2012-08-21 23:03:40 +08:00
|
|
|
format=$(eval_gettext "Waiting for %d guests to shut down, %d seconds left\n")
|
|
|
|
else
|
|
|
|
slept=0
|
|
|
|
format=$(eval_gettext "Waiting for %d guests to shut down\n")
|
2012-02-28 23:09:42 +08:00
|
|
|
fi
|
|
|
|
while [ -n "$on_shutdown" ] || [ -n "$guests" ]; do
|
|
|
|
while [ -n "$guests" ] &&
|
|
|
|
[ $(guest_count "$on_shutdown") -lt "$PARALLEL_SHUTDOWN" ]; do
|
|
|
|
set -- $guests
|
|
|
|
guest=$1
|
|
|
|
shift
|
|
|
|
guests=$*
|
|
|
|
shutdown_guest_async "$uri" "$guest"
|
|
|
|
on_shutdown="$on_shutdown $guest"
|
|
|
|
done
|
|
|
|
sleep 1
|
2012-08-21 23:03:40 +08:00
|
|
|
|
|
|
|
set -- $guests
|
|
|
|
guestcount=$#
|
|
|
|
set -- $on_shutdown
|
|
|
|
shutdowncount=$#
|
|
|
|
|
2012-02-28 23:09:42 +08:00
|
|
|
if $check_timeout; then
|
2012-08-21 23:03:40 +08:00
|
|
|
if [ $(($timeout % 5)) -eq 0 ]; then
|
|
|
|
printf "$format" $(($guestcount + $shutdowncount)) "$timeout"
|
|
|
|
fi
|
2012-02-28 23:09:42 +08:00
|
|
|
timeout=$(($timeout - 1))
|
|
|
|
if [ $timeout -le 0 ]; then
|
|
|
|
eval_gettext "Timeout expired while shutting down domains"; echo
|
|
|
|
RETVAL=1
|
|
|
|
return
|
|
|
|
fi
|
2012-08-21 23:03:40 +08:00
|
|
|
else
|
|
|
|
slept=$(($slept + 1))
|
|
|
|
if [ $(($slept % 5)) -eq 0 ]; then
|
|
|
|
printf "$format" $(($guestcount + $shutdowncount))
|
|
|
|
fi
|
2012-02-28 23:09:42 +08:00
|
|
|
fi
|
2012-08-21 23:03:40 +08:00
|
|
|
|
2012-02-28 23:09:42 +08:00
|
|
|
on_shutdown_prev=$on_shutdown
|
|
|
|
on_shutdown=$(check_guests_shutdown "$uri" "$on_shutdown")
|
|
|
|
print_guests_shutdown "$uri" "$on_shutdown_prev" "$on_shutdown"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# stop
|
|
|
|
# Shutdown or save guests on the configured uris
|
2010-05-14 21:37:55 +08:00
|
|
|
stop() {
|
|
|
|
# last stop was not followed by start
|
2010-05-27 20:47:11 +08:00
|
|
|
[ -f "$LISTFILE" ] && return 0
|
2010-05-14 21:37:55 +08:00
|
|
|
|
|
|
|
suspending=true
|
|
|
|
if [ "x$ON_SHUTDOWN" = xshutdown ]; then
|
|
|
|
suspending=false
|
2012-02-28 23:09:42 +08:00
|
|
|
if [ $SHUTDOWN_TIMEOUT -lt 0 ]; then
|
|
|
|
gettext "SHUTDOWN_TIMEOUT must be equal or greater than 0"
|
2011-01-05 02:13:56 +08:00
|
|
|
echo
|
2010-05-14 21:37:55 +08:00
|
|
|
RETVAL=6
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2010-05-27 20:47:11 +08:00
|
|
|
: >"$LISTFILE"
|
2011-03-12 05:06:09 +08:00
|
|
|
set -f
|
2010-05-14 21:37:55 +08:00
|
|
|
for uri in $URIS; do
|
2011-03-12 05:06:09 +08:00
|
|
|
set +f
|
2010-07-27 20:21:51 +08:00
|
|
|
|
2012-02-28 21:39:17 +08:00
|
|
|
test_connect "$uri" || continue
|
|
|
|
|
|
|
|
eval_gettext "Running guests on \$uri URI: "
|
2010-07-27 20:21:51 +08:00
|
|
|
|
2011-03-09 16:54:57 +08:00
|
|
|
list=$(list_guests "$uri")
|
2010-05-14 21:37:55 +08:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
empty=true
|
|
|
|
for uuid in $list; do
|
2011-03-09 16:54:57 +08:00
|
|
|
"$empty" || printf ", "
|
|
|
|
printf %s "$(guest_name "$uri" "$uuid")"
|
2010-05-14 21:37:55 +08:00
|
|
|
empty=false
|
|
|
|
done
|
2012-02-28 21:32:21 +08:00
|
|
|
|
2011-03-09 16:54:57 +08:00
|
|
|
if "$empty"; then
|
2012-02-28 21:32:21 +08:00
|
|
|
gettext "no running guests."
|
|
|
|
fi
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
|
|
|
|
if "$suspending"; then
|
|
|
|
transient=$(list_guests "$uri" "--transient")
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
empty=true
|
|
|
|
for uuid in $transient; do
|
|
|
|
if "$empty"; then
|
|
|
|
eval_gettext "Not suspending transient guests on URI: \$uri: "
|
|
|
|
empty=false
|
|
|
|
else
|
|
|
|
printf ", "
|
|
|
|
fi
|
|
|
|
printf %s "$(guest_name "$uri" "$uuid")"
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
# reload domain list to contain only persistent guests
|
|
|
|
list=$(list_guests "$uri" "--persistent")
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
eval_gettext "Failed to list persistent guests on \$uri"
|
|
|
|
echo
|
|
|
|
RETVAL=1
|
|
|
|
set +f
|
|
|
|
return
|
|
|
|
fi
|
2010-05-14 21:37:55 +08:00
|
|
|
else
|
2012-02-28 21:32:21 +08:00
|
|
|
gettext "Failed to list transient guests"
|
2010-05-14 21:37:55 +08:00
|
|
|
echo
|
2012-02-28 21:32:21 +08:00
|
|
|
RETVAL=1
|
|
|
|
set +f
|
|
|
|
return
|
2010-05-14 21:37:55 +08:00
|
|
|
fi
|
|
|
|
fi
|
2012-02-28 21:32:21 +08:00
|
|
|
|
|
|
|
if [ -n "$list" ]; then
|
|
|
|
echo "$uri" "$list" >>"$LISTFILE"
|
|
|
|
fi
|
2010-05-14 21:37:55 +08:00
|
|
|
done
|
2011-03-12 05:06:09 +08:00
|
|
|
set +f
|
2010-05-14 21:37:55 +08:00
|
|
|
|
2012-05-15 16:22:28 +08:00
|
|
|
if [ -s "$LISTFILE" ]; then
|
|
|
|
while read uri list; do
|
|
|
|
if "$suspending"; then
|
|
|
|
eval_gettext "Suspending guests on \$uri URI..."; echo
|
|
|
|
else
|
|
|
|
eval_gettext "Shutting down guests on \$uri URI..."; echo
|
|
|
|
fi
|
2010-05-14 21:37:55 +08:00
|
|
|
|
2012-05-15 16:22:28 +08:00
|
|
|
if [ "$PARALLEL_SHUTDOWN" -gt 1 ] &&
|
|
|
|
! "$suspending"; then
|
|
|
|
shutdown_guests_parallel "$uri" "$list"
|
|
|
|
else
|
|
|
|
for guest in $list; do
|
|
|
|
if "$suspending"; then
|
|
|
|
suspend_guest "$uri" "$guest"
|
|
|
|
else
|
|
|
|
shutdown_guest "$uri" "$guest"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done <"$LISTFILE"
|
|
|
|
else
|
|
|
|
rm -f "$LISTFILE"
|
|
|
|
fi
|
2010-05-27 20:47:11 +08:00
|
|
|
|
|
|
|
rm -f "$VAR_SUBSYS_LIBVIRT_GUESTS"
|
2010-05-14 21:37:55 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 20:58:59 +08:00
|
|
|
# gueststatus
|
|
|
|
# List status of guests
|
2010-05-14 21:37:55 +08:00
|
|
|
gueststatus() {
|
2011-03-12 05:06:09 +08:00
|
|
|
set -f
|
2010-05-14 21:37:55 +08:00
|
|
|
for uri in $URIS; do
|
2011-03-12 05:06:09 +08:00
|
|
|
set +f
|
2010-05-14 21:37:55 +08:00
|
|
|
echo "* $uri URI:"
|
2011-03-09 16:54:57 +08:00
|
|
|
retval run_virsh "$uri" list || echo
|
2010-05-14 21:37:55 +08:00
|
|
|
done
|
2011-03-12 05:06:09 +08:00
|
|
|
set +f
|
2010-05-14 21:37:55 +08:00
|
|
|
}
|
|
|
|
|
2010-07-28 03:50:35 +08:00
|
|
|
# rh_status
|
|
|
|
# Display current status: whether saved state exists, and whether start
|
|
|
|
# has been executed. We cannot use status() from the functions library,
|
|
|
|
# since there is no external daemon process matching this init script.
|
|
|
|
rh_status() {
|
|
|
|
if [ -f "$LISTFILE" ]; then
|
2011-01-05 02:13:56 +08:00
|
|
|
gettext "stopped, with saved guests"; echo
|
2010-07-28 03:50:35 +08:00
|
|
|
RETVAL=3
|
|
|
|
else
|
|
|
|
if [ -f "$VAR_SUBSYS_LIBVIRT_GUESTS" ]; then
|
2011-01-05 02:13:56 +08:00
|
|
|
gettext "started"; echo
|
2013-04-30 19:44:33 +08:00
|
|
|
RETVAL=0
|
2010-07-28 03:50:35 +08:00
|
|
|
else
|
2011-01-05 02:13:56 +08:00
|
|
|
gettext "stopped, with no saved guests"; echo
|
2013-04-30 19:44:33 +08:00
|
|
|
RETVAL=3
|
2010-07-28 03:50:35 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-07-28 02:05:27 +08:00
|
|
|
# usage [val]
|
|
|
|
# Display usage string, then exit with VAL (defaults to 2).
|
|
|
|
usage() {
|
2011-01-05 02:13:56 +08:00
|
|
|
program_name=$0
|
|
|
|
eval_gettext "Usage: \$program_name {start|stop|status|restart|"\
|
|
|
|
"condrestart|try-restart|reload|force-reload|gueststatus|shutdown}"; echo
|
2010-07-28 02:05:27 +08:00
|
|
|
exit ${1-2}
|
|
|
|
}
|
|
|
|
|
2010-05-14 21:37:55 +08:00
|
|
|
# See how we were called.
|
2010-07-28 02:05:27 +08:00
|
|
|
if test $# != 1; then
|
|
|
|
usage
|
|
|
|
fi
|
2010-05-14 21:37:55 +08:00
|
|
|
case "$1" in
|
2010-07-28 02:05:27 +08:00
|
|
|
--help)
|
|
|
|
usage 0
|
|
|
|
;;
|
2010-05-14 21:37:55 +08:00
|
|
|
start|stop|gueststatus)
|
2011-03-09 16:54:57 +08:00
|
|
|
"$1"
|
2010-05-14 21:37:55 +08:00
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
stop && start
|
|
|
|
;;
|
2010-07-28 02:21:31 +08:00
|
|
|
condrestart|try-restart)
|
|
|
|
[ -f "$VAR_SUBSYS_LIBVIRT_GUESTS" ] && stop && start
|
|
|
|
;;
|
|
|
|
reload|force-reload)
|
|
|
|
# Nothing to do; we reread configuration on each invocation
|
2010-05-14 21:37:55 +08:00
|
|
|
;;
|
|
|
|
status)
|
2010-07-28 03:50:35 +08:00
|
|
|
rh_status
|
2010-05-14 21:37:55 +08:00
|
|
|
;;
|
|
|
|
shutdown)
|
|
|
|
ON_SHUTDOWN=shutdown
|
|
|
|
stop
|
|
|
|
;;
|
|
|
|
*)
|
2010-07-28 02:05:27 +08:00
|
|
|
usage
|
2010-05-14 21:37:55 +08:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
exit $RETVAL
|