libxl: Add helper function for running the hook script

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>
This commit is contained in:
Jim Fehlig 2021-06-29 18:11:29 -06:00
parent fc94e5c1c2
commit 0d1ccad240
4 changed files with 58 additions and 93 deletions

View 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. * Internal domain destroy function.
* *
@ -903,16 +922,10 @@ libxlDomainCleanup(libxlDriverPrivate *driver,
hostdev_flags |= VIR_HOSTDEV_SP_USB; hostdev_flags |= VIR_HOSTDEV_SP_USB;
/* now that we know it's stopped call the hook if present */ /* Call hook with stopped operation. Ignore error and continue with cleanup */
if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) { ignore_value(libxlDomainHookRun(driver, vm->def, 0,
char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0); VIR_HOOK_LIBXL_OP_STOPPED,
VIR_HOOK_SUBOP_END, NULL));
/* 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);
}
virHostdevReAttachDomainDevices(hostdev_mgr, LIBXL_DRIVER_INTERNAL_NAME, virHostdevReAttachDomainDevices(hostdev_mgr, LIBXL_DRIVER_INTERNAL_NAME,
vm->def, hostdev_flags); vm->def, hostdev_flags);
@ -956,16 +969,10 @@ libxlDomainCleanup(libxlDriverPrivate *driver,
VIR_DEBUG("Failed to remove domain XML for %s", vm->def->name); VIR_DEBUG("Failed to remove domain XML for %s", vm->def->name);
VIR_FREE(file); VIR_FREE(file);
/* The "release" hook cleans up additional resources */ /* Call hook with release operation. Ignore error and continue with cleanup */
if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) { ignore_value(libxlDomainHookRun(driver, vm->def, 0,
char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0); VIR_HOOK_LIBXL_OP_RELEASE,
VIR_HOOK_SUBOP_END, NULL));
/* 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);
}
virDomainObjRemoveTransientDef(vm); virDomainObjRemoveTransientDef(vm);
} }
@ -1234,19 +1241,10 @@ libxlDomainStartPrepare(libxlDriverPrivate *driver,
return -1; return -1;
/* Run an early hook to set-up missing devices */ /* Run an early hook to set-up missing devices */
if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) { if (libxlDomainHookRun(driver, vm->def, 0,
g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0); VIR_HOOK_LIBXL_OP_PREPARE,
int hookret; VIR_HOOK_SUBOP_BEGIN, NULL) < 0)
goto error;
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 (virDomainLockProcessStart(driver->lockManager, if (virDomainLockProcessStart(driver->lockManager,
"xen:///system", "xen:///system",
@ -1300,21 +1298,10 @@ libxlDomainStartPerform(libxlDriverPrivate *driver,
goto cleanup; goto cleanup;
/* now that we know it is about to start call the hook if present */ /* now that we know it is about to start call the hook if present */
if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) { if (libxlDomainHookRun(driver, vm->def, 0,
char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0); VIR_HOOK_LIBXL_OP_START,
int hookret; VIR_HOOK_SUBOP_BEGIN, NULL) < 0)
goto cleanup;
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 (priv->hookRun) { if (priv->hookRun) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
@ -1404,21 +1391,10 @@ libxlDomainStartPerform(libxlDriverPrivate *driver,
goto destroy_dom; goto destroy_dom;
/* finally we can call the 'started' hook script if any */ /* finally we can call the 'started' hook script if any */
if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) { if (libxlDomainHookRun(driver, vm->def, 0,
char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0); VIR_HOOK_LIBXL_OP_STARTED,
int hookret; VIR_HOOK_SUBOP_BEGIN, NULL) < 0)
goto destroy_dom;
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;
}
ret = 0; ret = 0;
goto cleanup; goto cleanup;

View File

@ -108,6 +108,14 @@ libxlDomainSaveImageOpen(libxlDriverPrivate *driver,
libxlSavefileHeader *ret_hdr) libxlSavefileHeader *ret_hdr)
ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4); ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
int
libxlDomainHookRun(libxlDriverPrivate *driver,
virDomainDef *def,
unsigned int def_fmtflags,
int hookop,
int hooksubop,
char **output);
int int
libxlDomainDestroyInternal(libxlDriverPrivate *driver, libxlDomainDestroyInternal(libxlDriverPrivate *driver,
virDomainObj *vm); virDomainObj *vm);

View File

@ -450,26 +450,12 @@ libxlReconnectDomain(virDomainObj *vm,
if (virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0) if (virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
VIR_WARN("Cannot update XML for running Xen guest %s", vm->def->name); 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 */ /* now that we know it's reconnected call the hook */
if (virHookPresent(VIR_HOOK_DRIVER_LIBXL) && if (STRNEQ("Domain-0", vm->def->name) &&
STRNEQ("Domain-0", vm->def->name)) { (libxlDomainHookRun(driver, vm->def, 0,
char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0); VIR_HOOK_LIBXL_OP_RECONNECT,
int hookret; VIR_HOOK_SUBOP_BEGIN, NULL) < 0))
goto error;
/* 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;
}
}
ret = 0; ret = 0;

View File

@ -490,18 +490,13 @@ libxlDomainMigrationPrepareAny(virConnectPtr dconn,
/* Let migration hook filter domain XML */ /* Let migration hook filter domain XML */
if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) { if (virHookPresent(VIR_HOOK_DRIVER_LIBXL)) {
char *xml;
int hookret; int hookret;
if (!(xml = virDomainDefFormat(*def, driver->xmlopt, hookret = libxlDomainHookRun(driver, *def,
VIR_DOMAIN_XML_SECURE | VIR_DOMAIN_XML_SECURE | VIR_DOMAIN_XML_MIGRATABLE,
VIR_DOMAIN_XML_MIGRATABLE))) VIR_HOOK_LIBXL_OP_MIGRATE,
return -1; VIR_HOOK_SUBOP_BEGIN,
xmlout);
hookret = virHookCall(VIR_HOOK_DRIVER_LIBXL, (*def)->name,
VIR_HOOK_LIBXL_OP_MIGRATE, VIR_HOOK_SUBOP_BEGIN,
NULL, xml, xmlout);
VIR_FREE(xml);
if (hookret < 0) { if (hookret < 0) {
return -1; return -1;