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?