]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: Fix auto-shutdown of qemu VMs by the qemu driver
authorPeter Krempa <pkrempa@redhat.com>
Fri, 27 Jun 2025 14:10:03 +0000 (16:10 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 8 Jul 2025 12:51:44 +0000 (14:51 +0200)
When auto-shutdown via the qemu driver is requested (rather than via
libvirt guests) we need to start the VMs in a way that they will be kept
around for libvirt to terminate them. This involves inverting the
dependancy relationship for the machined unit file.

Since the setup is done at startup of the VM, add a disclaimer to
qemu.conf that switching between the two modes with VMs running will not
work properly.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/qemu/qemu.conf.in
src/qemu/qemu_cgroup.c

index 221bfa8095773e71f1c098e4351dc7ac15281355..6358a45ae2be5d97a8154653fce6537f50baf1dd 100644 (file)
 # implemented for transient VMs.
 #
 # If 'libvirt-guests.service' is enabled, then this must be
-# set to 'none' for system daemons to avoid dueling actions
+# set to 'none' for system daemons to avoid dueling actions.
+# Warning: Switching between 'libvirt-guests.service' and this option
+# causes VMs running at that point to misbehave on host shutdown unless
+# they are restarted, or saved and restored.
 #auto_shutdown_try_save = "persistent"
 
 # As above, but with a graceful shutdown action instead of
 #
 # If 'libvirt-guests.service' is enabled, then this must be
 # set to 'none' for system daemons to avoid dueling actions
+# Warning: Switching between 'libvirt-guests.service' and this option
+# causes VMs running at that point to misbehave on host shutdown unless
+# they are restarted, or saved and restored.
 #auto_shutdown_try_shutdown = "all"
 
 # As above, but with a forced poweroff instead of managed
 #
 # If 'libvirt-guests.service' is enabled, then this must be
 # set to 'none' for system daemons to avoid dueling actions
+#
+# Warning: Switching between 'libvirt-guests.service' and this option
+# causes VMs running at that point to misbehave on host shutdown unless
+# they are restarted, or saved and restored.
+#
+# When using any 'auto_shutdown_try_save', 'auto_shutdown_try_shutdown' this
+# feature should to be enabled as well to ensure proper cleanup of the VMs.
 #auto_shutdown_poweroff = "all"
 
 # How may seconds to wait for running VMs to gracefully shutdown
index 04d637001188b17d0fc36b3ab9f9d824ce35613a..25e42ebfc69564d291475279d4106002e433df15 100644 (file)
@@ -910,6 +910,12 @@ qemuSetupCgroup(virDomainObj *vm,
 {
     qemuDomainObjPrivate *priv = vm->privateData;
     g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(priv->driver);
+    /* When users wants to auto-shutdown the VMs via the qemu daemon itself
+     * we need to instruct machined to create dependencies for the units
+     * in such way that the VMs will not be killed before the auto shutdown
+     * code is reached.
+     */
+    bool daemonAutoShutdown = virDomainDriverAutoShutdownActive(&cfg->autoShutdown);
 
     if (virDomainCgroupSetupCgroup("qemu",
                                    vm,
@@ -919,7 +925,7 @@ qemuSetupCgroup(virDomainObj *vm,
                                    cfg->cgroupControllers,
                                    cfg->maxThreadsPerProc,
                                    priv->driver->privileged,
-                                   false,
+                                   daemonAutoShutdown,
                                    priv->machineName) < 0)
 
         return -1;