--- libvirt-guests 2011-11-04 12:11:58.326115749 +0100 +++ libvirt-guests-modified 2011-11-04 12:17:55.032105427 +0100 @@ -9,8 +9,8 @@ # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: suspend/resume libvirt guests on shutdown/boot -# Description: This is a script for suspending active libvirt guests -# on shutdown and resuming them on next boot +# Description: This is a script for suspending or migrating active libvirt guests +# on shutdown and resuming the suspended guest on next boot. # See http://libvirt.org ### END INIT INFO @@ -19,8 +19,9 @@ # libvirt-guests: suspend/resume libvirt guests on shutdown/boot # # chkconfig: 345 99 01 -# description: This is a script for suspending active libvirt guests \ -# on shutdown and resuming them on next boot \ +# description: This is a script for suspending or relocation active \ +# libvirt guests on shutdown and resuming the suspended \ +# guests on next boot \ # See http://libvirt.org # @@ -35,7 +36,9 @@ URIS=default ON_BOOT=start ON_SHUTDOWN=suspend +#ON_SHUTDOWN=migrate SHUTDOWN_TIMEOUT=0 +RELOCATION_HOST=RelocationHostname.domain.local test -f "$sysconfdir"/sysconfig/libvirt-guests && . "$sysconfdir"/sysconfig/libvirt-guests @@ -208,14 +211,18 @@ $guest_running || break printf '\r%s%-12d ' "$label" $timeout done +} - if guest_is_on $uri $guest; then - if $guest_running; then - printf '\r%s%-12s\n' "$label" $"failed to shutdown in time" - else - printf '\r%s%-12s\n' "$label" $"done" - fi - fi +migrate_guest() +{ + uri=$1 + guest=$2 + + name=$(guest_name $uri $guest) + label=$"Migrating $name: to $RELOCATION_HOST" + echo -n "$label" + echo + retval run_virsh $uri migrate --live --verbose $guest "qemu+ssh://$RELOCATION_HOST/system" || return } stop() { @@ -223,14 +230,19 @@ [ -f "$LISTFILE" ] && return 0 suspending=true - if [ "x$ON_SHUTDOWN" = xshutdown ]; then - suspending=false - if [ $SHUTDOWN_TIMEOUT -le 0 ]; then - echo $"Shutdown action requested but SHUTDOWN_TIMEOUT was not set" - RETVAL=6 - return - fi - fi + case "x$ON_SHUTDOWN" in + xshutdown ) + suspending=false + if [ $SHUTDOWN_TIMEOUT -le 0 ]; then + echo $"Shutdown action requested but SHUTDOWN_TIMEOUT was not set" + RETVAL=6 + return + fi + ;; + xmigrate ) + suspending=false + ;; + esac : >"$LISTFILE" for uri in $URIS; do @@ -262,16 +274,28 @@ if $suspending; then echo $"Suspending guests on $uri URI..." else - echo $"Shutting down guests on $uri URI..." + if [ "x$ON_SHUTDOWN" = xshutdown ]; then + echo $"Shutting down guests on $uri URI..." + else + echo $"Migrating guests on $uri URI to $RELOCATION_HOST ..." + fi fi for guest in $list; do if $suspending; then suspend_guest $uri $guest else - shutdown_guest $uri $guest + if [ "x$ON_SHUTDOWN" = xshutdown ]; then + shutdown_guest $uri $guest + else + migrate_guest $uri $guest + fi fi done + if [ "x$ON_SHUTDOWN" = xmigrate ]; then + #delete listfile after migrating all VM's + rm -f $LISTFILE + fi done <"$LISTFILE" rm -f "$VAR_SUBSYS_LIBVIRT_GUESTS"