From 82963fdcb67742fad3066d31f2bd9b14c8153574 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 27 Jun 2025 16:10:03 +0200 Subject: [PATCH] qemu: Fix auto-shutdown of qemu VMs by the qemu driver 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 Reviewed-by: Pavel Hrdina --- src/qemu/qemu.conf.in | 15 ++++++++++++++- src/qemu/qemu_cgroup.c | 8 +++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/qemu/qemu.conf.in b/src/qemu/qemu.conf.in index 221bfa8095..6358a45ae2 100644 --- a/src/qemu/qemu.conf.in +++ b/src/qemu/qemu.conf.in @@ -663,7 +663,10 @@ # 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 @@ -675,6 +678,9 @@ # # 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 @@ -687,6 +693,13 @@ # # 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 diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index 04d6370011..25e42ebfc6 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -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; -- 2.47.3