Ubuntu 16.04: Suspend and resume guest machine automatically on reboot

This article will describe suspending guest machine automatically on host machine's shutdown and resuming guest machine automatically on host machine's boot.

1 Install libvirt-bin

Install libvirt-bin which includes libvirt-guests.

$ sudo apt install -y libvirt-bin

2 Edit /etc/default/libvirt-guests

Edit /etc/default/libvirt-guests as below.

$ diff -uprN /etc/default/libvirt-guests{.org,}
--- /etc/default/libvirt-guests.org     2016-10-19 01:40:53.356931551 +0900
+++ /etc/default/libvirt-guests 2016-10-19 01:41:15.228674533 +0900
@@ -8,7 +8,7 @@
 # - ignore  libvirt-guests init script won't start any guest on boot, however,
 #           guests marked as autostart will still be automatically started by
 #           libvirtd
-#ON_BOOT=ignore
+ON_BOOT=start

 # Number of seconds to wait between each guest start. Set to 0 to allow
 # parallel startup.
@@ -22,7 +22,7 @@
 #             which just needs a long time to shutdown. When setting
 #             ON_SHUTDOWN=shutdown, you must also set SHUTDOWN_TIMEOUT to a
 #             value suitable for your guests.
-#ON_SHUTDOWN=shutdown
+ON_SHUTDOWN=suspend

 # If set to non-zero, shutdown will suspend guests concurrently. Number of
 # guests on shutdown at any time will not exceed number set in this variable.
@@ -33,7 +33,7 @@ PARALLEL_SHUTDOWN=10
 # guests on a single URI defined in the variable URIS. If this is 0, then there
 # is no time out (use with caution, as guests might not respond to a shutdown
 # request). The default value is 300 seconds (5 minutes).
-SHUTDOWN_TIMEOUT=120
+#SHUTDOWN_TIMEOUT=120

 # If non-zero, try to bypass the file system cache when saving and
 # restoring guests, even though this may give slower operation for

Reboot host machine.

$ sudo reboot

After reboot, when shutdowning host machine with runnning guest machine, guest machine will be suspended automatically. When booting host machine, suspended guest machine will be resumed automatically.