output status information during guest shutdown again

Since the move to systemd libvirt-guests doesn't output this progress
information anymore. This patch brings back this feature.

It is helpful to show the admin what the system is waiting for and what
is left of the timeout (e.g. for calibrating the shutdown timing of a ups).

Rewriting the current line with \r doesn't work anymore in the context
of systemd. So always write new lines, but move to 5 second intervals
to avoid flooding the console.
This commit is contained in:
Gerd v. Egidy 2012-08-21 17:03:40 +02:00 committed by Eric Blake
parent a46af26004
commit c18dc28b1f
2 changed files with 51 additions and 15 deletions

View File

@ -225,22 +225,27 @@ suspend_guest()
name=$(guest_name "$uri" "$guest") name=$(guest_name "$uri" "$guest")
label=$(eval_gettext "Suspending \$name: ") label=$(eval_gettext "Suspending \$name: ")
bypass= bypass=
slept=0
test "x$BYPASS_CACHE" = x0 || bypass=--bypass-cache test "x$BYPASS_CACHE" = x0 || bypass=--bypass-cache
printf %s "$label" printf '%s...\n' "$label"
run_virsh "$uri" managedsave $bypass "$guest" >/dev/null & run_virsh "$uri" managedsave $bypass "$guest" >/dev/null &
virsh_pid=$! virsh_pid=$!
while true; do while true; do
sleep 1 sleep 1
kill -0 "$virsh_pid" >/dev/null 2>&1 || break kill -0 "$virsh_pid" >/dev/null 2>&1 || break
progress=$(run_virsh_c "$uri" domjobinfo "$guest" 2>/dev/null | \
awk '/^Data processed:/{print $3, $4}') slept=$(($slept + 1))
if [ -n "$progress" ]; then if [ $(($slept % 5)) -eq 0 ]; then
printf '\r%s%12s ' "$label" "$progress" progress=$(run_virsh_c "$uri" domjobinfo "$guest" 2>/dev/null | \
else awk '/^Data processed:/{print $3, $4}')
printf '\r%s%-12s ' "$label" "..." if [ -n "$progress" ]; then
printf '%s%s\n' "$label" "$progress"
else
printf '%s%s\n' "$label" "..."
fi
fi fi
done done
retval wait "$virsh_pid" && printf '\r%s%-12s\n' "$label" "$(gettext "done")" retval wait "$virsh_pid" && printf '%s%s\n' "$label" "$(gettext "done")"
} }
# shutdown_guest URI GUEST # shutdown_guest URI GUEST
@ -252,30 +257,41 @@ shutdown_guest()
guest=$2 guest=$2
name=$(guest_name "$uri" "$guest") name=$(guest_name "$uri" "$guest")
label=$(eval_gettext "Shutting down \$name: ") eval_gettext "Starting shutdown on guest: \$name"
printf %s "$label" echo
retval run_virsh "$uri" shutdown "$guest" >/dev/null || return retval run_virsh "$uri" shutdown "$guest" >/dev/null || return
timeout=$SHUTDOWN_TIMEOUT timeout=$SHUTDOWN_TIMEOUT
check_timeout=false check_timeout=false
if [ $timeout -gt 0 ]; then if [ $timeout -gt 0 ]; then
check_timeout=true check_timeout=true
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")
fi fi
while ! $check_timeout || [ "$timeout" -gt 0 ]; do while ! $check_timeout || [ "$timeout" -gt 0 ]; do
sleep 1 sleep 1
guest_is_on "$uri" "$guest" || return guest_is_on "$uri" "$guest" || return
"$guest_running" || break "$guest_running" || break
if $check_timeout; then if $check_timeout; then
timeout=$((timeout - 1)) if [ $(($timeout % 5)) -eq 0 ]; then
printf '\r%s%-12d ' "$label" "$timeout" printf "$format" "$name" "$timeout"
fi
timeout=$(($timeout - 1))
else
slept=$(($slept + 1))
if [ $(($slept % 5)) -eq 0 ]; then
printf "$format" "$name"
fi
fi fi
done done
if guest_is_on "$uri" "$guest"; then if guest_is_on "$uri" "$guest"; then
if "$guest_running"; then if "$guest_running"; then
printf '\r%s%-12s\n' "$label" \ eval_gettext "Shutdown of guest \$name failed to complete in time."
"$(gettext "failed to shutdown in time")"
else else
printf '\r%s%-12s\n' "$label" "$(gettext "done")" eval_gettext "Shutdown of guest \$name complete."
fi fi
fi fi
} }
@ -356,6 +372,10 @@ shutdown_guests_parallel()
timeout=$SHUTDOWN_TIMEOUT timeout=$SHUTDOWN_TIMEOUT
if [ $timeout -gt 0 ]; then if [ $timeout -gt 0 ]; then
check_timeout=true check_timeout=true
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")
fi fi
while [ -n "$on_shutdown" ] || [ -n "$guests" ]; do while [ -n "$on_shutdown" ] || [ -n "$guests" ]; do
while [ -n "$guests" ] && while [ -n "$guests" ] &&
@ -368,14 +388,29 @@ shutdown_guests_parallel()
on_shutdown="$on_shutdown $guest" on_shutdown="$on_shutdown $guest"
done done
sleep 1 sleep 1
set -- $guests
guestcount=$#
set -- $on_shutdown
shutdowncount=$#
if $check_timeout; then if $check_timeout; then
if [ $(($timeout % 5)) -eq 0 ]; then
printf "$format" $(($guestcount + $shutdowncount)) "$timeout"
fi
timeout=$(($timeout - 1)) timeout=$(($timeout - 1))
if [ $timeout -le 0 ]; then if [ $timeout -le 0 ]; then
eval_gettext "Timeout expired while shutting down domains"; echo eval_gettext "Timeout expired while shutting down domains"; echo
RETVAL=1 RETVAL=1
return return
fi fi
else
slept=$(($slept + 1))
if [ $(($slept % 5)) -eq 0 ]; then
printf "$format" $(($guestcount + $shutdowncount))
fi
fi fi
on_shutdown_prev=$on_shutdown on_shutdown_prev=$on_shutdown
on_shutdown=$(check_guests_shutdown "$uri" "$on_shutdown") on_shutdown=$(check_guests_shutdown "$uri" "$on_shutdown")
print_guests_shutdown "$uri" "$on_shutdown_prev" "$on_shutdown" print_guests_shutdown "$uri" "$on_shutdown_prev" "$on_shutdown"

View File

@ -10,6 +10,7 @@ ExecStart=/etc/init.d/libvirt-guests start
ExecStop=/etc/init.d/libvirt-guests stop ExecStop=/etc/init.d/libvirt-guests stop
Type=oneshot Type=oneshot
RemainAfterExit=yes RemainAfterExit=yes
StandardOutput=journal+console
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target