4

I have libvirt/KVM set up on my Ubuntu and before shutting down the machine I would like it to attempt shutting down the VM's using an ACPI poweroff (virsh shutdown), then I want it to stop shutdown for at least 60 seconds to give the VM's a chance to sync everything to disk, this is what I have:

pre-stop script
    SHUTDOWN_LOG=/var/log/libvirt/qemu/shutdown_vms.log
    for RUNNING_VM in `virsh list | grep -E running | awk -F" " '{ print $2 }'`
    do
        echo "Shutting down ${RUNNING_VM} on `date`" >> $SHUTDOWN_LOG
        virsh shutdown ${RUNNING_VM}
    done
    echo -n "Waiting for VM's to shut down: " >> $SHUTDOWN_LOG
    for I in `seq 1 10`
    do
        RUNNING=`virsh list | grep running | wc -l`
        if [[ "$RUNNING" == "0" ]] 
        then 
            echo "done." >> $SHUTDOWN_LOG
            exit 0
        else 
            /bin/sleep 6 && echo -n " ${RUNNING} " >> $SHUTDOWN_LOG
        fi
    done
    echo " Cleaning up..." >> $SHUTDOWN_LOG
end script  

The issue I am having is that for some odd reason it never gets to echo "done." even if the VM's are all shut down correctly, it will keep looping and thus stalling for 60 seconds, OR it seems to completely ignore the sleep, writes the total to the $SHUTDOWN_LOG and quits immediately anyway in the middle of my VM's syncing to disk.

Shutting down FreeBSD-services on Mon Aug 22 02:07:42 MDT 2011
Shutting down FreeBSD-pgsql on Mon Aug 22 02:07:42 MDT 2011
Waiting for VM's to shut down:  1 [EOF]

Is the log output ... is there any better way to log from upstart?

X-Istence
  • 752
  • 1
  • 8
  • 18

2 Answers2

1

https://bugs.launchpad.net/ubuntu/lucid/+source/libvirt/+bug/350936 was recently fixed, so there should be an updated libvirt soon.

sendmoreinfo
  • 1,742
  • 12
  • 33
  • This is fantastic news ... I ended up writing a SysV script that ran before sendsigs to shut down the VM's and replaced stop on with never stopping it. So libvirt eventually gets hard killed when the VM's are shut down. Worth it. – X-Istence Sep 10 '11 at 07:41
0

You can't echo from an upstart script unfortunately, the best option is the one you're already doing which is creating your own log.

If you want to also output to syslog you can do this in upstart using logger (as you would in init.d) but Upstart debugging so far is pretty harsh.

You can also add set -x at the beginning of your pre-stop script to get more output through the default upstart log channel (syslog in Ubuntu)

lynxman
  • 9,157
  • 3
  • 24
  • 28