]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
libxl: Add helper function for running the hook script
authorJim Fehlig <jfehlig@suse.com>
Wed, 30 Jun 2021 00:11:29 +0000 (18:11 -0600)
committerJim Fehlig <jfehlig@suse.com>
Mon, 12 Jul 2021 20:02:18 +0000 (14:02 -0600)
The same pattern of retrieving the domXML, running the hook script, and
checking for error is used throughout the libxl driver. Remove some
repetitive code by adding a helper function to perform these tasks.

Signed-off-by: Jim Fehlig <jfehlig@suse.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libxl/libxl_domain.c
src/libxl/libxl_domain.h
src/libxl/libxl_driver.c
src/libxl/libxl_migration.c

index 05c5ad4b4dcdee55b34019040bc20a7c8c153397..4f61584ceb56124402526208608d194c70e23585 100644 (file)
@@ -853,6 +853,25 @@ libxlNetworkUnwindDevices(virDomainDef *def)
     }
 }
 
+int
+libxlDomainHookRun(libxlDriverPrivate *driver,
+                   virDomainDef *def,
+                   unsigned int def_fmtflags,
+                   int hookop,
+                   int hooksubop,
+                   char **output)
+{
+    g_autofree char *xml = NULL;
+
+    if (!virHookPresent(VIR_HOOK_DRIVER_LIBXL))
+        return 0;
+
+    xml = virDomainDefFormat(def, driver->xmlopt, def_fmtflags);
+    return virHookCall(VIR_HOOK_DRIVER_LIBXL, def->name,
+                       hookop, hooksubop,
+                       NULL, xml, output);
+}
+
 /*
  * Internal domain destroy function.
  *
@@ -903,16 +922,10 @@ libxlDomainCleanup(libxlDriverPrivate *driver,
 
     hostdev_flags |= VIR_HOSTDEV_SP_USB;
 
-    /* now that we know it's stopped call the hook if present */
-    if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-
-        /* we can't stop the operation even if the script raised an error */
-        ignore_value(virHookCall(VIR_HOOK_DRIVER_LIBXL, vm->def->name,
-                                 VIR_HOOK_LIBXL_OP_STOPPED, VIR_HOOK_SUBOP_END,
-                                 NULL, xml, NULL));
-        VIR_FREE(xml);
-    }
+    /* Call hook with stopped operation. Ignore error and continue with cleanup */
+    ignore_value(libxlDomainHookRun(driver, vm->def, 0,
+                                    VIR_HOOK_LIBXL_OP_STOPPED,
+                                    VIR_HOOK_SUBOP_END, NULL));
 
     virHostdevReAttachDomainDevices(hostdev_mgr, LIBXL_DRIVER_INTERNAL_NAME,
                                     vm->def, hostdev_flags);
@@ -956,16 +969,10 @@ libxlDomainCleanup(libxlDriverPrivate *driver,
         VIR_DEBUG("Failed to remove domain XML for %s", vm->def->name);
     VIR_FREE(file);
 
-    /* The "release" hook cleans up additional resources */
-    if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-
-        /* we can't stop the operation even if the script raised an error */
-        ignore_value(virHookCall(VIR_HOOK_DRIVER_LIBXL, vm->def->name,
-                                 VIR_HOOK_LIBXL_OP_RELEASE, VIR_HOOK_SUBOP_END,
-                                 NULL, xml, NULL));
-        VIR_FREE(xml);
-    }
+    /* Call hook with release operation. Ignore error and continue with cleanup */
+    ignore_value(libxlDomainHookRun(driver, vm->def, 0,
+                                    VIR_HOOK_LIBXL_OP_RELEASE,
+                                    VIR_HOOK_SUBOP_END, NULL));
 
     virDomainObjRemoveTransientDef(vm);
 }
@@ -1234,19 +1241,10 @@ libxlDomainStartPrepare(libxlDriverPrivate *driver,
         return -1;
 
     /* Run an early hook to set-up missing devices */
-    if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
-        g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-        int hookret;
-
-        hookret = virHookCall(VIR_HOOK_DRIVER_LIBXL, vm->def->name,
-                              VIR_HOOK_LIBXL_OP_PREPARE, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, NULL);
-        /*
-         * If the script raised an error abort the launch
-         */
-        if (hookret < 0)
-            goto error;
-    }
+    if (libxlDomainHookRun(driver, vm->def, 0,
+                           VIR_HOOK_LIBXL_OP_PREPARE,
+                           VIR_HOOK_SUBOP_BEGIN, NULL) < 0)
+        goto error;
 
     if (virDomainLockProcessStart(driver->lockManager,
                                   "xen:///system",
@@ -1300,21 +1298,10 @@ libxlDomainStartPerform(libxlDriverPrivate *driver,
         goto cleanup;
 
     /* now that we know it is about to start call the hook if present */
-    if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-        int hookret;
-
-        hookret = virHookCall(VIR_HOOK_DRIVER_LIBXL, vm->def->name,
-                              VIR_HOOK_LIBXL_OP_START, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, NULL);
-        VIR_FREE(xml);
-
-        /*
-         * If the script raised an error abort the launch
-         */
-        if (hookret < 0)
-            goto cleanup;
-    }
+    if (libxlDomainHookRun(driver, vm->def, 0,
+                           VIR_HOOK_LIBXL_OP_START,
+                           VIR_HOOK_SUBOP_BEGIN, NULL) < 0)
+        goto cleanup;
 
     if (priv->hookRun) {
         char uuidstr[VIR_UUID_STRING_BUFLEN];
@@ -1404,21 +1391,10 @@ libxlDomainStartPerform(libxlDriverPrivate *driver,
         goto destroy_dom;
 
     /* finally we can call the 'started' hook script if any */
-    if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-        int hookret;
-
-        hookret = virHookCall(VIR_HOOK_DRIVER_LIBXL, vm->def->name,
-                              VIR_HOOK_LIBXL_OP_STARTED, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, NULL);
-        VIR_FREE(xml);
-
-        /*
-         * If the script raised an error abort the launch
-         */
-        if (hookret < 0)
-            goto destroy_dom;
-    }
+    if (libxlDomainHookRun(driver, vm->def, 0,
+                           VIR_HOOK_LIBXL_OP_STARTED,
+                           VIR_HOOK_SUBOP_BEGIN, NULL) < 0)
+        goto destroy_dom;
 
     ret = 0;
     goto cleanup;
index 526c8e7332edafe31fa46300815269f5c6d078de..1618c47ed547c0e718fb1757763e8d75e022e8a5 100644 (file)
@@ -108,6 +108,14 @@ libxlDomainSaveImageOpen(libxlDriverPrivate *driver,
                          libxlSavefileHeader *ret_hdr)
     ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
 
+int
+libxlDomainHookRun(libxlDriverPrivate *driver,
+                   virDomainDef *def,
+                   unsigned int def_fmtflags,
+                   int hookop,
+                   int hooksubop,
+                   char **output);
+
 int
 libxlDomainDestroyInternal(libxlDriverPrivate *driver,
                            virDomainObj *vm);
index 838747a1e3027091478c6f07b7249b19d41aa7c5..0c3c53c1d157bdcad4bdd6ff1f1857022555f423 100644 (file)
@@ -450,26 +450,12 @@ libxlReconnectDomain(virDomainObj *vm,
     if (virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
         VIR_WARN("Cannot update XML for running Xen guest %s", vm->def->name);
 
-    /* now that we know it's reconnected call the hook if present */
-    if (virHookPresent(VIR_HOOK_DRIVER_LIBXL) &&
-        STRNEQ("Domain-0", vm->def->name)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-        int hookret;
-
-        /* we can't stop the operation even if the script raised an error */
-        hookret = virHookCall(VIR_HOOK_DRIVER_LIBXL, vm->def->name,
-                              VIR_HOOK_LIBXL_OP_RECONNECT, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, NULL);
-        VIR_FREE(xml);
-        if (hookret < 0) {
-            /* Stop the domain if the hook failed */
-            if (virDomainObjIsActive(vm)) {
-                libxlDomainDestroyInternal(driver, vm);
-                virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_FAILED);
-            }
-            goto error;
-        }
-    }
+    /* now that we know it's reconnected call the hook */
+    if (STRNEQ("Domain-0", vm->def->name) &&
+        (libxlDomainHookRun(driver, vm->def, 0,
+                            VIR_HOOK_LIBXL_OP_RECONNECT,
+                            VIR_HOOK_SUBOP_BEGIN, NULL) < 0))
+        goto error;
 
     ret = 0;
 
index 0af6e0d09a9e2c3bd7b00cc8a97d447059a4f5f9..4677f798fc1c677c29ad549944b97f8c48d76452 100644 (file)
@@ -490,18 +490,13 @@ libxlDomainMigrationPrepareAny(virConnectPtr dconn,
 
     /* Let migration hook filter domain XML */
     if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
-        char *xml;
         int hookret;
 
-        if (!(xml = virDomainDefFormat(*def, driver->xmlopt,
-                                       VIR_DOMAIN_XML_SECURE |
-                                       VIR_DOMAIN_XML_MIGRATABLE)))
-            return -1;
-
-        hookret = virHookCall(VIR_HOOK_DRIVER_LIBXL, (*def)->name,
-                              VIR_HOOK_LIBXL_OP_MIGRATE, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, xmlout);
-        VIR_FREE(xml);
+        hookret = libxlDomainHookRun(driver, *def,
+                                     VIR_DOMAIN_XML_SECURE | VIR_DOMAIN_XML_MIGRATABLE,
+                                     VIR_HOOK_LIBXL_OP_MIGRATE,
+                                     VIR_HOOK_SUBOP_BEGIN,
+                                     xmlout);
 
         if (hookret < 0) {
             return -1;