On 07/19/2010 12:52 PM, Dag Wieers wrote:
Hi,

During a discussion it was apparent some people expect the KVM guests to be cleanly shut down when the host is being rebooted/shut down. Clearly this is not done as part of the /etc/init.d/libvirtd sysv script stop.

So what likely is happening is that /etc/init/halt is sending TERM signals to each remaining process, waits 5 seconds and then KILLs them. Which would be fine if the kvm/qemu processes perform a shutdown on a TERM signal _and_ they would in fact shut down cleanly within 5 seconds.

A mechanism like the above would be very helpful in some situations. Has this been discussed before, and should we ? :-)

Thanks for any insights !

Here is the /etc/init.d/libvirtd script that I use to automatically send kvm guests the ACPI poweroff signal, then wait 5 minutes for them to shutdown, then pull the virtual plug. If they shutdown before then, the script continues.

Jason
#!/bin/sh

# the following is the LSB init header see
# 
http://www.linux-foundation.org/spec//booksets/LSB-Core-generic/LSB-Core-generic.html#INITSCRCOMCONV
#
### BEGIN INIT INFO
# Provides: libvirtd
# Should-Start: xend
# Default-Start: 3 4 5
# Short-Description: daemon for libvirt virtualization API
# Description: This is a daemon for managing guest instances
#              and libvirt virtual networks
#              See http://libvirt.org
### END INIT INFO

# the following is chkconfig init header
#
# libvirtd:   guest and virtual network management daemon
#
# chkconfig: 345 97 03
# description:  This is a daemon for managing guest instances
#               and libvirt virtual networks
#               See http://libvirt.org
#
# processname: libvirtd
# pidfile: /var/run/libvirtd.pid
#

# Sanity checks.
[ -x /usr/sbin/libvirtd ] || exit 0

# Source function library.
. /etc/rc.d/init.d/functions

SERVICE=libvirtd
PROCESS=libvirtd
OLD_PROCESS=libvirt_qemud

LIBVIRTD_CONFIG=
LIBVIRTD_ARGS=
KRB5_KTNAME=/etc/libvirt/krb5.tab

test -f /etc/sysconfig/libvirtd && . /etc/sysconfig/libvirtd

LIBVIRTD_CONFIG_ARGS=
if [ -n "$LIBVIRTD_CONFIG" ]
then
    LIBVIRTD_CONFIG_ARGS="--config $LIBVIRTD_CONFIG"
fi

RETVAL=0

start() {
    echo -n $"Starting $SERVICE daemon: "
    mkdir -p /var/cache/libvirt
    rm -rf /var/cache/libvirt/*
    KRB5_KTNAME=$KRB5_KTNAME daemon --check $SERVICE $PROCESS --daemon 
$LIBVIRTD_CONFIG_ARGS $LIBVIRTD_ARGS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SERVICE
}

stop() {

    TIMEOUT=180
    VMLIST=`virsh list|grep running|awk '{print $2}'`
    
    # send all running VM's the shutdown signal
    for vm in $VMLIST; do
        echo shutting down guest: $vm
        virsh shutdown $vm
        sleep 1
    done
    
    COUNT=0
    while [ "$COUNT" -lt "$TIMEOUT" ]; do
        VMLIST=`virsh list|grep running|awk '{print $2}'`
        virsh list| grep running > /dev/null
        if [ $? == 1 ]; then
            echo "All VM's successfully shutdown"
            return
        fi
        echo "Waiting on the following VM's to shut down: $VMLIST"    
        sleep 5
        COUNT=$(($COUNT+5))
    done
    
    VMLIST=`virsh list|grep running|awk '{print $2}'`
    # send all running VM's the shutdown signal
    for vm in $VMLIST; do
        echo pulling the plug on guest: $vm
        virsh destroy $vm
    done

    echo -n $"Stopping $SERVICE daemon: "

    if [ "$(pidofproc $OLD_PROCESS)" != "" ]; then
        killproc $OLD_PROCESS
        RETVAL=$?
    else
        killproc $PROCESS
        RETVAL=$?
    fi
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/$SERVICE
        rm -f /var/run/$SERVICE.pid
        rm -rf /var/cache/libvirt/*
    fi
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $SERVICE configuration: "

    if [ "$(pidofproc $OLD_PROCESS)" != "" ]; then
        killproc $OLD_PROCESS -HUP
        RETVAL=$?
    else
        killproc $PROCESS -HUP
        RETVAL=$?
    fi
    echo
    return $RETVAL
}

# See how we were called.
case "$1" in
    start|stop|restart|reload)
        $1
        ;;
    status)
        if [ "$(pidofproc $OLD_PROCESS)" != "" ]; then
            status $OLD_PROCESS
            RETVAL=$?
        else
            status $PROCESS
            RETVAL=$?
        fi
        ;;
    force-reload)
        reload
        ;;
    condrestart|try-restart)
        [ -f /var/lock/subsys/$SERVICE ] && restart || :
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        exit 1
        ;;
esac
exit $RETVAL
_______________________________________________
rhelv5-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/rhelv5-list

Reply via email to