Comment 84 for bug 350936

Revision history for this message
Sebastian Marsching (sebastian-marsching) wrote :

The problem of shutting down the virtual machines consists of two parts:

If a init-script is used, libvirtd might be killed, before the shutdown command has been sent to the virtual machines. If the pre-stop script in the libvirt-bin upstart job is used or a new upstart job starting on stopping libvirt-bin is used, the computer is rebooted, the VMs are killed by the sendsigs script. Even if the sendsigs script is modified to not kill the VMs, the reboot or halt script might run before the libvirt-bin upstart job has been stopped.

I use the following approach, which works quite well for me:
I added a libvirt-shutdown-domains upstart job, which is a task starting on stopping libvirt-bin, thus running before libvirt-bin is stopped. This makes sure that libvirt-bin is not stopped, before the VMs are shutdown. In fact, I also included a runlevel check in the libvirt-shutdown-domains script, so that the VMs are only shutdown, when libvirtd is stopped because of a runlevel change. This allows for libvirtd to be restarted (e.g. when libvirt-bin is upgraded) without having to shutdown all VMs.
I also added a init-script running before the sendsigs script, which waits for libvirt-bin to be stopped (or a timeout, whichever occurs first). Thus, sendsigs will not kill VMs and the machine will not be rebooted or powered off, before the VMs have been stopped.

The scripts I wrote for this can be found at http://sebastian.marsching.com/wiki/Linux/KVM#Shutdown_virtual_machines_on_host_system_shutdown and some more explanations can be found at http://sebastian.marsching.com/blog/archives/112-KVM-and-Graceful-Shutdown-on-Ubuntu.html.

The solution I created is not perfect though because of two issues:
1. The python-libvirt package is needed, because I use a Python script to shutdown the virtual machines. You might want to use a shell script using virsh for that, thus eliminating this dependency.
2. The timeout, after which the shutdown will proceed anyway, is configured at two different places: The init-script and the Python script called by the upstart job. You might want to put both in the same place (e.g. /etc/default/libvirt-bin).