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(a)suse.com>
---
src/libxl/libxl_domain.c | 102 ++++++++++++++----------------------
src/libxl/libxl_domain.h | 8 +++
src/libxl/libxl_driver.c | 26 +++------
src/libxl/libxl_migration.c | 15 ++----
4 files changed, 58 insertions(+), 93 deletions(-)
diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index c656cba3a3..e699c1daa9 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -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.
*
@@ -904,16 +923,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);
@@ -957,16 +970,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);
}
@@ -1235,19 +1242,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",
@@ -1301,21 +1299,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];
@@ -1405,21 +1392,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;
diff --git a/src/libxl/libxl_domain.h b/src/libxl/libxl_domain.h
index 526c8e7332..1618c47ed5 100644
--- a/src/libxl/libxl_domain.h
+++ b/src/libxl/libxl_domain.h
@@ -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);
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 838747a1e3..0c3c53c1d1 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -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;
diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c
index 0af6e0d09a..4677f798fc 100644
--- a/src/libxl/libxl_migration.c
+++ b/src/libxl/libxl_migration.c
@@ -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;
--
2.31.1