Update August 2010: It does not always work as expected. Though this method works well if the server is Ubuntu 8.04 or Ubuntu 9.04, but the shutdown script never get called in Debian Squeeze. Maybe a better init script would be required for newer versions.
Update February 2011: You should have a look at Sergey‘s comment below, where he mentions why it doesn’t work in Debian Squeeze. Thanks Sergey!
I have beenĀ using KVM for virtualization for quite some time now, probably more than a year. Though the initial use was on the servers which were rarely shutdown, now I’ve started using it in my home envir0nment where frequent restarts or shutdowns are there. The challenge that I faced was to automatically shutdown the KVM virtual machines when the system was shutdown or restarted.
Thanks to Joern at linux-kvm.com, I was able to setup a script which does an auto shutdown of the Linux (basically any VM which has ssh available) machines. I am using it on Ubuntu 9.04 (Jaunty Jackalope) 64 Bit Desktop edition.
Steps
- Add the host file entries
- Make sure you are able to do ssh without entering any password from root into the VMs
- Put the script into the /etc/init.d directory.
- Test the script before rebooting the system
- Reboot the system to check if it worked