diff --git a/files/libvirt-guests.confd b/files/libvirt-guests.confd index ed2ce58..d794e85 100644 --- a/files/libvirt-guests.confd +++ b/files/libvirt-guests.confd @@ -66,3 +66,12 @@ # NATed network for you. Valid values: 'yes' or 'no' #LIBVIRT_NET_SHUTDOWN="yes" + + +# LIBVIRT_CONWAIT +# The timeout, in seconds, to wait for a connection to LIBVIRT_URIS +# Valid values are any integer greater than or equal to 0 +# 0 means there will be no wait +# +#LIBVIRT_CONWAIT=5 + diff --git a/files/libvirt-guests.init-r4 b/files/libvirt-guests.init-r4 index b29f04c..e672ad9 100644 --- a/files/libvirt-guests.init-r4 +++ b/files/libvirt-guests.init-r4 @@ -18,6 +18,10 @@ esac # default to 500 seconds [ -z ${LIBVIRT_MAXWAIT} ] && LIBVIRT_MAXWAIT=500 +# default to 5 seconds +[ -z ${LIBVIRT_CONWAIT} ] && LIBVIRT_CONWAIT=5 + + gueststatefile="/var/lib/libvirt/libvirt-guests.state" netstatefile="/var/lib/libvirt/libvirt-net.state" @@ -165,16 +169,47 @@ libvirtd_net_stop() { fi } +libvirtd_connect_wait() { + # Does up to LIBVIRT_CONWAIT connect attempts + # Fails if cannot connect + # Exits as soon as a successful connection is made + + local hvuri=$1 + local counter=${LIBVIRT_CONWAIT} + + # Since we call virsh directly, repeat the do_virsh check + # if unset, default to qemu + [ -z ${hvuri} ] && hvuri="qemu:///system" + # if only qemu was supplied then correct the value + [ "xqemu" = x${hvuri} ] && hvuri="qemu:///system" + + einfo " Checking connection to ${hvuri} ..." + + while [ ${counter} -gt 0 ]; do + virsh -c ${hvuri} connect &>/dev/null + if [ $? -ne 0 ]; then + counter=$((${counter} - 1)) + printf "." + sleep 1 + else + counter=0 + fi + done + virsh -c ${hvuri} connect &>/dev/null + if [ $? -ne 0 ]; then + eerror "Failed to connect to '${hvuri}'. Domains may not start." + else + einfo " Connection to ${hvuri} OK" + fi +} + start() { local uri= local uuid= local name= for uri in ${LIBVIRT_URIS}; do - do_virsh "${uri}" connect - if [ $? -ne 0 ]; then - eerror "Failed to connect to '${uri}'. Domains may not start." - fi + libvirtd_connect_wait ${uri} done [ ! -e "${netstatefile}" ] && touch "${netstatefile}"