From c8d8e1d1ccfee5db17fe464aacdb5336dd96b7bc Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Wed, 25 Apr 2012 16:12:46 +0200 Subject: [PATCH] bug #1211: change vmm/kvm/shutdown to make use of retry and force_destroy --- src/vmm_mad/remotes/kvm/kvmrc | 7 ++++ src/vmm_mad/remotes/kvm/shutdown | 59 +++++++++++++++----------------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/vmm_mad/remotes/kvm/kvmrc b/src/vmm_mad/remotes/kvm/kvmrc index 3c4650a9cd..0cb2f2791f 100644 --- a/src/vmm_mad/remotes/kvm/kvmrc +++ b/src/vmm_mad/remotes/kvm/kvmrc @@ -19,3 +19,10 @@ export LANG=C export LIBVIRT_URI=qemu:///system export QEMU_PROTOCOL=qemu+ssh + +# Senconds to wait after shutdown until timeout +export SHUTDOWN_TIMEOUT=120 + +# Uncoment this line to force VM cancellation after shutdown timeout +#export FORCE_DESTROY=yes + diff --git a/src/vmm_mad/remotes/kvm/shutdown b/src/vmm_mad/remotes/kvm/shutdown index 86f82002c9..936f80cb71 100755 --- a/src/vmm_mad/remotes/kvm/shutdown +++ b/src/vmm_mad/remotes/kvm/shutdown @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash # -------------------------------------------------------------------------- # # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # @@ -19,45 +19,42 @@ source $(dirname $0)/kvmrc source $(dirname $0)/../../scripts_common.sh -#------------------------------------------------------------------------------ -# Wait the VM to shutdown TIMEOUT (xPOLL_INTERVAL) seconds. -# Set to ~10min -#------------------------------------------------------------------------------ -POLL_INTERVAL=2 -TIMEOUT=300 -HALF_LOOP=$(($TIMEOUT/POLL_INTERVAL)) +count=0 deploy_id=$1 -virsh --connect $LIBVIRT_URI shutdown $deploy_id - -exit_code=$? - -if [ "$exit_code" != "0" ]; then - error_message "Could not shutdown $deploy_id" - exit $exit_code +if [ -z "$SHUTDOWN_TIMEOUT" ]; then + TIMEOUT=120 +else + TIMEOUT=$SHUTDOWN_TIMEOUT fi -count=0 -while [ $(virsh --connect $LIBVIRT_URI --readonly dominfo $deploy_id > /dev/null 2>&1; echo $?) = "0" ] -do - sleep $POLL_INTERVAL - if [ "$count" -gt "$TIMEOUT" ] - then - error_message "Timeout reached and VM $deploy_id is still alive" - echo "Timeout reached" >&2 - exit 1 - fi +HALF_LOOP=$(($TIMEOUT/2)) +function monitor +{ # Issue another shutdown to cover occasional libvirt lack of attention - if [ "$count" -eq "$HALF_LOOP" ] - then - virsh --connect $LIBVIRT_URI shutdown $deploy_id + if [ "$count" -eq "$HALF_LOOP" ] + then + virsh --connect $LIBVIRT_URI shutdown $deploy_id fi - let count=count+$POLL_INTERVAL -done + let count=count+1 + + export count + + virsh --connect $LIBVIRT_URI --readonly dominfo $deploy_id > /dev/null 2>&1 + + [ "x$?" != "x0" ] +} + +exec_and_log "virsh --connect $LIBVIRT_URI shutdown \"$deploy_id\"" \ + "Could not shutdown $deploy_id" + +retry $TIMEOUT monitor + +force_shutdown "$deploy_id" \ + "virsh --connect $LIBVIRT_URI destroy \"$deploy_id\"" sleep 4 -exit 0