Proper Shutdown of VMs on Host Reboot

In the past, I’ve always used KVM’s GUI to properly shut-down remote Virtual Machines when I needed to reboot the host and then re-launch them again after the reboot. Over time, this is a bit of a tiresome process, especially as the frequency of host reboots after security updates seems to increase. So at some point I thought that there must be an easier way to do this:

And indeed there is: To automatically shut-down all guest-VMs on a host when it reboots, adapt the following config file:

nano /etc/default/libvirt-guests

# Change the following line:

No restart is required to activate the config change. According to the config file, there is a 120 seconds timeout to prevent a hanging VM blocking the host reboot.

And once the reboot is done, I can also automatically restart all VMs again by putting the ‘virsh start’ commands for all VMs in a batch file:

virsh start VM-NAME

And that’s it! A config change and a simple command save me a lot of time when rebooting hosts. Also, it lowers the psychological bar to host rebooting in the first place.