]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
hypervisor: introduce helper for autostart
authorDaniel P. Berrangé <berrange@redhat.com>
Mon, 16 Dec 2024 18:53:28 +0000 (18:53 +0000)
committerDaniel P. Berrangé <berrange@redhat.com>
Wed, 12 Feb 2025 18:05:39 +0000 (18:05 +0000)
There's a common pattern for autostart of iterating over VMs, acquiring
a lock and ref count, then checking the autostart & is-active flags.
Wrap this all up into a helper method.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
src/hypervisor/domain_driver.c
src/hypervisor/domain_driver.h
src/libvirt_private.syms

index 85d68b056c20f9ea6c3c77aa1faea78980c93bc6..596157545b1e645a8df7241e2fc823c60759ccb7 100644 (file)
 #include "viraccessapicheck.h"
 #include "datatypes.h"
 #include "driver.h"
+#include "virlog.h"
 
 #define VIR_FROM_THIS VIR_FROM_DOMAIN
 
+VIR_LOG_INIT("hypervisor.domain_driver");
+
 char *
 virDomainDriverGenerateRootHash(const char *drivername,
                                 const char *root)
@@ -652,3 +655,41 @@ virDomainDriverGetIOThreadsConfig(virDomainDef *targetDef,
 
     return ret;
 }
+
+static int
+virDomainDriverAutoStartOne(virDomainObj *vm,
+                            void *opaque)
+{
+    virDomainDriverAutoStartConfig *cfg = opaque;
+
+    virObjectLock(vm);
+    virObjectRef(vm);
+
+    VIR_DEBUG("Autostart %s: autostart=%d",
+              vm->def->name, vm->autostart);
+
+    if (vm->autostart && !virDomainObjIsActive(vm)) {
+        virResetLastError();
+        cfg->callback(vm, cfg->opaque);
+    }
+
+    virDomainObjEndAPI(&vm);
+    virResetLastError();
+
+    return 0;
+}
+
+void
+virDomainDriverAutoStart(virDomainObjList *domains,
+                         virDomainDriverAutoStartConfig *cfg)
+{
+    bool autostart;
+    VIR_DEBUG("Run autostart stateDir=%s", cfg->stateDir);
+    if (virDriverShouldAutostart(cfg->stateDir, &autostart) < 0 ||
+        !autostart) {
+        VIR_DEBUG("Autostart already processed");
+        return;
+    }
+
+    virDomainObjListForEach(domains, false, virDomainDriverAutoStartOne, cfg);
+}
index 9942f58fda1fe6ec151be8d2d798a56d20eba557..2ad4e96f3e6c97ea26d8cc73ba78075b10b36d37 100644 (file)
@@ -23,6 +23,7 @@
 #include "node_device_conf.h"
 #include "virhostdev.h"
 #include "virpci.h"
+#include "virdomainobjlist.h"
 
 char *
 virDomainDriverGenerateRootHash(const char *drivername,
@@ -71,3 +72,19 @@ int virDomainDriverDelIOThreadCheck(virDomainDef *def,
 int virDomainDriverGetIOThreadsConfig(virDomainDef *targetDef,
                                       virDomainIOThreadInfoPtr **info,
                                       unsigned int bitmap_size);
+
+/*
+ * Will be called with 'vm' locked and ref count held,
+ * which will be released when this returns.
+ */
+typedef void (*virDomainDriverAutoStartCallback)(virDomainObj *vm,
+                                                 void *opaque);
+
+typedef struct _virDomainDriverAutoStartConfig {
+    const char *stateDir;
+    virDomainDriverAutoStartCallback callback;
+    void *opaque;
+} virDomainDriverAutoStartConfig;
+
+void virDomainDriverAutoStart(virDomainObjList *domains,
+                              virDomainDriverAutoStartConfig *cfg);
index 4eca252b2a1ecf8113a4111fe45a64e288b402ec..bd9564f2a7d2cd6860edffcbcc331b7518b38d0f 100644 (file)
@@ -1647,6 +1647,7 @@ virDomainCgroupSetupVcpuBW;
 
 # hypervisor/domain_driver.h
 virDomainDriverAddIOThreadCheck;
+virDomainDriverAutoStart;
 virDomainDriverDelIOThreadCheck;
 virDomainDriverGenerateMachineName;
 virDomainDriverGenerateRootHash;