lxcDomainAttachDeviceFlags: Parse XML twice rather than use virDomainDeviceDefCopy

'virDomainDeviceDefCopy' formats the definition and parses it back.
Since we already are parsing the XML here, we're better off parsing it
twice and save the formatting step.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-11-22 17:01:31 +01:00
parent c2a0d09046
commit 904d80b773

View File

@ -4255,8 +4255,8 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom,
virLXCDriver *driver = dom->conn->privateData; virLXCDriver *driver = dom->conn->privateData;
virDomainObj *vm = NULL; virDomainObj *vm = NULL;
g_autoptr(virDomainDef) vmdef = NULL; g_autoptr(virDomainDef) vmdef = NULL;
virDomainDeviceDef *dev = NULL; g_autoptr(virDomainDeviceDef) dev_config = NULL;
virDomainDeviceDef *dev_copy = NULL; g_autoptr(virDomainDeviceDef) dev_live = NULL;
int ret = -1; int ret = -1;
g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver);
@ -4275,21 +4275,17 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom,
if (virDomainObjUpdateModificationImpact(vm, &flags) < 0) if (virDomainObjUpdateModificationImpact(vm, &flags) < 0)
goto endjob; goto endjob;
dev = dev_copy = virDomainDeviceDefParse(xml, vm->def, if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
driver->xmlopt, NULL, if (!(dev_config = virDomainDeviceDefParse(xml, vm->def, driver->xmlopt,
VIR_DOMAIN_DEF_PARSE_INACTIVE); NULL,
if (dev == NULL) VIR_DOMAIN_DEF_PARSE_INACTIVE)))
goto endjob; goto endjob;
}
if (flags & VIR_DOMAIN_AFFECT_CONFIG && if (flags & VIR_DOMAIN_AFFECT_LIVE) {
flags & VIR_DOMAIN_AFFECT_LIVE) { if (!(dev_live = virDomainDeviceDefParse(xml, vm->def, driver->xmlopt,
/* If we are affecting both CONFIG and LIVE NULL,
* create a deep copy of device as adding VIR_DOMAIN_DEF_PARSE_INACTIVE)))
* to CONFIG takes one instance.
*/
dev_copy = virDomainDeviceDefCopy(dev, vm->def,
driver->xmlopt, NULL);
if (!dev_copy)
goto endjob; goto endjob;
} }
@ -4299,22 +4295,22 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom,
if (!vmdef) if (!vmdef)
goto endjob; goto endjob;
if (virDomainDefCompatibleDevice(vmdef, dev, NULL, if (virDomainDefCompatibleDevice(vmdef, dev_config, NULL,
VIR_DOMAIN_DEVICE_ACTION_ATTACH, VIR_DOMAIN_DEVICE_ACTION_ATTACH,
false) < 0) false) < 0)
goto endjob; goto endjob;
if ((ret = lxcDomainAttachDeviceConfig(vmdef, dev_copy)) < 0) if ((ret = lxcDomainAttachDeviceConfig(vmdef, dev_config)) < 0)
goto endjob; goto endjob;
} }
if (flags & VIR_DOMAIN_AFFECT_LIVE) { if (flags & VIR_DOMAIN_AFFECT_LIVE) {
if (virDomainDefCompatibleDevice(vm->def, dev, NULL, if (virDomainDefCompatibleDevice(vm->def, dev_live, NULL,
VIR_DOMAIN_DEVICE_ACTION_ATTACH, VIR_DOMAIN_DEVICE_ACTION_ATTACH,
true) < 0) true) < 0)
goto endjob; goto endjob;
if ((ret = lxcDomainAttachDeviceLive(driver, vm, dev)) < 0) if ((ret = lxcDomainAttachDeviceLive(driver, vm, dev_live)) < 0)
goto endjob; goto endjob;
/* /*
* update domain status forcibly because the domain status may be * update domain status forcibly because the domain status may be
@ -4338,9 +4334,6 @@ static int lxcDomainAttachDeviceFlags(virDomainPtr dom,
virDomainObjEndJob(vm); virDomainObjEndJob(vm);
cleanup: cleanup:
if (dev != dev_copy)
virDomainDeviceDefFree(dev_copy);
virDomainDeviceDefFree(dev);
virDomainObjEndAPI(&vm); virDomainObjEndAPI(&vm);
return ret; return ret;
} }