From 4a4132b4625778cf80acb9c92d06351b44468ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Tue, 3 Dec 2019 10:49:49 +0000 Subject: [PATCH] conf: don't use passed in caps in post parse method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To enable the virCapsPtr parameter to the post parse method to be eliminated, the drivers must fetch the virCapsPtr from their own driver via the opaque parameter, or use an alternative approach to validate the parsed data. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrangé --- src/bhyve/bhyve_domain.c | 10 +++++-- src/esx/esx_driver.c | 2 +- src/libxl/libxl_conf.c | 3 ++- src/libxl/libxl_conf.h | 2 +- src/libxl/libxl_domain.c | 9 ++++--- src/libxl/libxl_driver.c | 2 +- src/lxc/lxc_conf.c | 3 ++- src/lxc/lxc_conf.h | 2 +- src/lxc/lxc_controller.c | 44 ++++++++++++++++++++++++------- src/lxc/lxc_domain.c | 8 ++++-- src/lxc/lxc_driver.c | 2 +- src/openvz/openvz_conf.c | 10 ++++--- src/openvz/openvz_conf.h | 2 +- src/openvz/openvz_driver.c | 2 +- src/phyp/phyp_driver.c | 8 +++--- src/qemu/qemu_capabilities.c | 38 +++++++++++++++++++++++++++ src/qemu/qemu_capabilities.h | 4 +++ src/qemu/qemu_domain.c | 48 +++++++++++++++++++++------------- src/vmware/vmware_driver.c | 11 ++++---- src/vmx/vmx.c | 8 +++--- src/vmx/vmx.h | 2 +- src/vz/vz_driver.c | 15 ++++++----- tests/Makefile.am | 14 +++++++--- tests/libxlxml2domconfigtest.c | 22 +++++----------- tests/lxcconf2xmltest.c | 17 ++++-------- tests/lxcxml2xmltest.c | 13 +++------ tests/openvzutilstest.c | 10 ++++--- tests/testutilslxc.c | 34 +++++++++++++++++++++++- tests/testutilslxc.h | 4 +++ tests/testutilsxen.c | 34 +++++++++++++++++++++++- tests/testutilsxen.h | 9 ++++--- tests/vmx2xmltest.c | 2 +- tests/xlconfigtest.c | 26 ++++++++---------- tests/xmconfigtest.c | 23 +++++++--------- tests/xml2vmxtest.c | 2 +- 35 files changed, 301 insertions(+), 144 deletions(-) diff --git a/src/bhyve/bhyve_domain.c b/src/bhyve/bhyve_domain.c index 575f141b53..eebf5a7650 100644 --- a/src/bhyve/bhyve_domain.c +++ b/src/bhyve/bhyve_domain.c @@ -20,6 +20,7 @@ #include +#include "bhyve_driver.h" #include "bhyve_conf.h" #include "bhyve_device.h" #include "bhyve_domain.h" @@ -74,11 +75,16 @@ bhyveDomainDefNeedsISAController(virDomainDefPtr def) static int bhyveDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr _caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED, + void *opaque, void *parseOpaque G_GNUC_UNUSED) { + bhyveConnPtr driver = opaque; + g_autoptr(virCaps) caps = bhyveDriverGetCapabilities(driver); + if (!caps) + return -1; + if (!virCapabilitiesDomainSupported(caps, def->os.type, def->os.arch, def->virtType)) diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c index 81ff502769..2db89c7986 100644 --- a/src/esx/esx_driver.c +++ b/src/esx/esx_driver.c @@ -928,7 +928,7 @@ esxConnectOpen(virConnectPtr conn, virConnectAuthPtr auth, if (!priv->caps) goto cleanup; - if (!(priv->xmlopt = virVMXDomainXMLConfInit())) + if (!(priv->xmlopt = virVMXDomainXMLConfInit(priv->caps))) goto cleanup; conn->privateData = priv; diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index 37fe360067..132e04b147 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -2470,8 +2470,9 @@ libxlBuildDomainConfig(virPortAllocatorRangePtr graphicsports, } virDomainXMLOptionPtr -libxlCreateXMLConf(void) +libxlCreateXMLConf(libxlDriverPrivatePtr driver) { + libxlDomainDefParserConfig.priv = driver; return virDomainXMLOptionNew(&libxlDomainDefParserConfig, &libxlDomainXMLPrivateDataCallbacks, NULL, NULL, NULL); diff --git a/src/libxl/libxl_conf.h b/src/libxl/libxl_conf.h index 305c8c41ef..4b5d240684 100644 --- a/src/libxl/libxl_conf.h +++ b/src/libxl/libxl_conf.h @@ -207,7 +207,7 @@ libxlMakeUSB(virDomainHostdevDefPtr hostdev, libxl_device_usbdev *usbdev); #endif virDomainXMLOptionPtr -libxlCreateXMLConf(void); +libxlCreateXMLConf(libxlDriverPrivatePtr driver); #ifdef LIBXL_HAVE_DEVICE_CHANNEL # define LIBXL_ATTR_UNUSED diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c index ad9424155a..f202f0fc3e 100644 --- a/src/libxl/libxl_domain.c +++ b/src/libxl/libxl_domain.c @@ -367,12 +367,15 @@ libxlDomainDeviceDefPostParse(virDomainDeviceDefPtr dev, static int libxlDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED, + void *opaque, void *parseOpaque G_GNUC_UNUSED) { - if (!virCapabilitiesDomainSupported(caps, def->os.type, + libxlDriverPrivatePtr driver = opaque; + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); + + if (!virCapabilitiesDomainSupported(cfg->caps, def->os.type, def->os.arch, def->virtType)) return -1; diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index dc0d9b58f3..fac3c1db49 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -769,7 +769,7 @@ libxlStateInitialize(bool privileged, goto error; } - if (!(libxl_driver->xmlopt = libxlCreateXMLConf())) + if (!(libxl_driver->xmlopt = libxlCreateXMLConf(libxl_driver))) goto error; /* Add Domain-0 */ diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c index de9793e523..2e866973ce 100644 --- a/src/lxc/lxc_conf.c +++ b/src/lxc/lxc_conf.c @@ -207,8 +207,9 @@ virCapsPtr virLXCDriverGetCapabilities(virLXCDriverPtr driver, virDomainXMLOptionPtr -lxcDomainXMLConfInit(void) +lxcDomainXMLConfInit(virLXCDriverPtr driver) { + virLXCDriverDomainDefParserConfig.priv = driver; return virDomainXMLOptionNew(&virLXCDriverDomainDefParserConfig, &virLXCDriverPrivateDataCallbacks, &virLXCDriverDomainXMLNamespace, diff --git a/src/lxc/lxc_conf.h b/src/lxc/lxc_conf.h index 12a201db38..59782d9cb2 100644 --- a/src/lxc/lxc_conf.h +++ b/src/lxc/lxc_conf.h @@ -112,7 +112,7 @@ int virLXCLoadDriverConfig(virLXCDriverConfigPtr cfg, virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver); virCapsPtr virLXCDriverGetCapabilities(virLXCDriverPtr driver, bool refresh); -virDomainXMLOptionPtr lxcDomainXMLConfInit(void); +virDomainXMLOptionPtr lxcDomainXMLConfInit(virLXCDriverPtr driver); static inline void lxcDriverLock(virLXCDriverPtr driver) { diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 2b9d6481df..c869e2e7c4 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -159,11 +159,41 @@ static void virLXCControllerQuitTimer(int timer G_GNUC_UNUSED, void *opaque) } +static virLXCDriverPtr +virLXCControllerDriverNew(void) +{ + virLXCDriverPtr driver = g_new0(virLXCDriver, 1); + + if (virMutexInit(&driver->lock) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("cannot initialize mutex")); + g_free(driver); + return NULL; + } + + driver->caps = virLXCDriverCapsInit(NULL); + driver->xmlopt = lxcDomainXMLConfInit(driver); + + return driver; +} + + +static void +virLXCControllerDriverFree(virLXCDriverPtr driver) +{ + if (!driver) + return; + virObjectUnref(driver->xmlopt); + virObjectUnref(driver->caps); + virMutexDestroy(&driver->lock); + g_free(driver); +} + + static virLXCControllerPtr virLXCControllerNew(const char *name) { virLXCControllerPtr ctrl = NULL; - virCapsPtr caps = NULL; - virDomainXMLOptionPtr xmlopt = NULL; + virLXCDriverPtr driver = NULL; char *configFile = NULL; if (VIR_ALLOC(ctrl) < 0) @@ -174,10 +204,7 @@ static virLXCControllerPtr virLXCControllerNew(const char *name) ctrl->name = g_strdup(name); - if (!(caps = virLXCDriverCapsInit(NULL))) - goto error; - - if (!(xmlopt = lxcDomainXMLConfInit())) + if (!(driver = virLXCControllerDriverNew())) goto error; if ((configFile = virDomainConfigFile(LXC_STATE_DIR, @@ -185,7 +212,7 @@ static virLXCControllerPtr virLXCControllerNew(const char *name) goto error; if ((ctrl->vm = virDomainObjParseFile(configFile, - caps, xmlopt, + driver->caps, driver->xmlopt, 0)) == NULL) goto error; ctrl->def = ctrl->vm->def; @@ -197,8 +224,7 @@ static virLXCControllerPtr virLXCControllerNew(const char *name) cleanup: VIR_FREE(configFile); - virObjectUnref(caps); - virObjectUnref(xmlopt); + virLXCControllerDriverFree(driver); return ctrl; error: diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c index b505a91c1c..bc54beeadf 100644 --- a/src/lxc/lxc_domain.c +++ b/src/lxc/lxc_domain.c @@ -351,11 +351,15 @@ virDomainXMLPrivateDataCallbacks virLXCDriverPrivateDataCallbacks = { static int virLXCDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr _caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED, + void *opaque, void *parseOpaque G_GNUC_UNUSED) { + virLXCDriverPtr driver = opaque; + g_autoptr(virCaps) caps = virLXCDriverGetCapabilities(driver, false); + if (!caps) + return -1; if (!virCapabilitiesDomainSupported(caps, def->os.type, def->os.arch, def->virtType)) diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 6c12543274..f28e20d756 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -1584,7 +1584,7 @@ static int lxcStateInitialize(bool privileged, if (!(caps = virLXCDriverGetCapabilities(lxc_driver, true))) goto cleanup; - if (!(lxc_driver->xmlopt = lxcDomainXMLConfInit())) + if (!(lxc_driver->xmlopt = lxcDomainXMLConfInit(lxc_driver))) goto cleanup; if (!(lxc_driver->closeCallbacks = virCloseCallbacksNew())) diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c index de8be1ed7d..5060e704a5 100644 --- a/src/openvz/openvz_conf.c +++ b/src/openvz/openvz_conf.c @@ -1082,12 +1082,13 @@ int openvzGetVEID(const char *name) static int openvzDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED, + void *opaque, void *parseOpaque G_GNUC_UNUSED) { - if (!virCapabilitiesDomainSupported(caps, def->os.type, + struct openvz_driver *driver = opaque; + if (!virCapabilitiesDomainSupported(driver->caps, def->os.type, def->os.arch, def->virtType)) return -1; @@ -1133,8 +1134,9 @@ virDomainDefParserConfig openvzDomainDefParserConfig = { .features = VIR_DOMAIN_DEF_FEATURE_NAME_SLASH, }; -virDomainXMLOptionPtr openvzXMLOption(void) +virDomainXMLOptionPtr openvzXMLOption(struct openvz_driver *driver) { + openvzDomainDefParserConfig.priv = driver; return virDomainXMLOptionNew(&openvzDomainDefParserConfig, NULL, NULL, NULL, NULL); } diff --git a/src/openvz/openvz_conf.h b/src/openvz/openvz_conf.h index 6463c1704a..9892f39481 100644 --- a/src/openvz/openvz_conf.h +++ b/src/openvz/openvz_conf.h @@ -63,4 +63,4 @@ int strtoI(const char *str); int openvzSetDefinedUUID(int vpsid, unsigned char *uuid); int openvzGetVEID(const char *name); int openvzReadNetworkConf(virDomainDefPtr def, int veid); -virDomainXMLOptionPtr openvzXMLOption(void); +virDomainXMLOptionPtr openvzXMLOption(struct openvz_driver *driver); diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index e479ebf58d..9b93bc5ca0 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -1311,7 +1311,7 @@ static virDrvOpenStatus openvzConnectOpen(virConnectPtr conn, if (!(driver->caps = openvzCapsInit())) goto cleanup; - if (!(driver->xmlopt = openvzXMLOption())) + if (!(driver->xmlopt = openvzXMLOption(driver))) goto cleanup; if (openvzLoadDomains(driver) < 0) diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c index 218d6f5b5c..05860b7dbe 100644 --- a/src/phyp/phyp_driver.c +++ b/src/phyp/phyp_driver.c @@ -1062,12 +1062,13 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth, static int phypDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED, + void *opaque, void *parseOpaque G_GNUC_UNUSED) { - if (!virCapabilitiesDomainSupported(caps, def->os.type, + phyp_driverPtr driver = opaque; + if (!virCapabilitiesDomainSupported(driver->caps, def->os.type, def->os.arch, def->virtType)) return -1; @@ -1157,6 +1158,7 @@ phypConnectOpen(virConnectPtr conn, if ((phyp_driver->caps = phypCapsInit()) == NULL) goto failure; + virPhypDriverDomainDefParserConfig.priv = phyp_driver; if (!(phyp_driver->xmlopt = virDomainXMLOptionNew(&virPhypDriverDomainDefParserConfig, NULL, NULL, NULL, NULL))) goto failure; diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index c02ff5b941..7a62dfb1a2 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -2094,6 +2094,44 @@ virQEMUCapsSetHostModel(virQEMUCapsPtr qemuCaps, } +bool +virQEMUCapsIsArchSupported(virQEMUCapsPtr qemuCaps, + virArch arch) +{ + if (arch == qemuCaps->arch) + return true; + + if (qemuCaps->arch == VIR_ARCH_X86_64 && arch == VIR_ARCH_I686) + return true; + + if (qemuCaps->arch == VIR_ARCH_AARCH64 && arch == VIR_ARCH_ARMV7L) + return true; + + if (qemuCaps->arch == VIR_ARCH_ARMV7L && arch == VIR_ARCH_ARMV6L) + return true; + + if (qemuCaps->arch == VIR_ARCH_PPC64 && arch == VIR_ARCH_PPC64LE) + return true; + + return false; +} + + +bool +virQEMUCapsIsVirtTypeSupported(virQEMUCapsPtr qemuCaps, + virDomainVirtType virtType) +{ + if (virtType == VIR_DOMAIN_VIRT_QEMU) + return true; + + if (virtType == VIR_DOMAIN_VIRT_KVM && + virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM)) + return true; + + return false; +} + + bool virQEMUCapsIsCPUModeSupported(virQEMUCapsPtr qemuCaps, virArch hostarch, diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 399496796d..a8a5c38d73 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -603,6 +603,10 @@ int virQEMUCapsGetCPUFeatures(virQEMUCapsPtr qemuCaps, bool migratable, char ***features); +bool virQEMUCapsIsArchSupported(virQEMUCapsPtr qemuCaps, + virArch arch); +bool virQEMUCapsIsVirtTypeSupported(virQEMUCapsPtr qemuCaps, + virDomainVirtType virtType); bool virQEMUCapsIsCPUModeSupported(virQEMUCapsPtr qemuCaps, virArch hostarch, virDomainVirtType type, diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index a342af3348..6159101bcc 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -4691,22 +4691,40 @@ qemuDomainDefPostParseBasic(virDomainDefPtr def, static int qemuDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr caps G_GNUC_UNUSED, unsigned int parseFlags, void *opaque, - void *parseOpaque) + void *parseOpaque G_GNUC_UNUSED) { virQEMUDriverPtr driver = opaque; g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); - /* Note that qemuCaps may be NULL when this function is called. This - * function shall not fail in that case. It will be re-run on VM startup - * with the capabilities populated. */ - virQEMUCapsPtr qemuCaps = parseOpaque; + g_autoptr(virQEMUCaps) qemuCaps = NULL; - if (!virCapabilitiesDomainSupported(caps, def->os.type, - def->os.arch, - def->virtType)) + if (!(qemuCaps = virQEMUCapsCacheLookup(driver->qemuCapsCache, + def->emulator))) { + return 1; + } + + if (def->os.type != VIR_DOMAIN_OSTYPE_HVM) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Emulator '%s' does not support os type '%s'"), + def->emulator, virDomainOSTypeToString(def->os.type)); return -1; + } + + if (!virQEMUCapsIsArchSupported(qemuCaps, def->os.arch)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Emulator '%s' does not support arch '%s'"), + def->emulator, virArchToString(def->os.arch)); + return -1; + } + + if (!virQEMUCapsIsVirtTypeSupported(qemuCaps, def->virtType)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Emulator '%s' does not support virt type '%s'"), + def->emulator, virDomainVirtTypeToString(def->virtType)); + return -1; + } if (def->os.bootloader || def->os.bootloaderArgs) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", @@ -4715,15 +4733,9 @@ qemuDomainDefPostParse(virDomainDefPtr def, } if (!def->os.machine) { - g_autofree virCapsDomainDataPtr capsdata = NULL; - - if (!(capsdata = virCapabilitiesDomainDataLookup(caps, def->os.type, - def->os.arch, - def->virtType, - NULL, NULL))) { - return -1; - } - def->os.machine = g_strdup(capsdata->machinetype); + const char *machine = virQEMUCapsGetPreferredMachine(qemuCaps, + def->virtType); + def->os.machine = g_strdup(machine); } qemuDomainNVRAMPathGenerate(cfg, def); diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c index be0adb1e45..308a941f8a 100644 --- a/src/vmware/vmware_driver.c +++ b/src/vmware/vmware_driver.c @@ -117,12 +117,13 @@ vmwareDataFreeFunc(void *data) static int vmwareDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED, void *parseOpaque G_GNUC_UNUSED) { - if (!virCapabilitiesDomainSupported(caps, def->os.type, + struct vmware_driver *driver = opaque; + if (!virCapabilitiesDomainSupported(driver->caps, def->os.type, def->os.arch, def->virtType)) return -1; @@ -148,11 +149,11 @@ virDomainDefParserConfig vmwareDomainDefParserConfig = { }; static virDomainXMLOptionPtr -vmwareDomainXMLConfigInit(void) +vmwareDomainXMLConfigInit(struct vmware_driver *driver) { virDomainXMLPrivateDataCallbacks priv = { .alloc = vmwareDataAllocFunc, .free = vmwareDataFreeFunc }; - + vmwareDomainDefParserConfig.priv = driver; return virDomainXMLOptionNew(&vmwareDomainDefParserConfig, &priv, NULL, NULL, NULL); } @@ -235,7 +236,7 @@ vmwareConnectOpen(virConnectPtr conn, if (!(driver->caps = vmwareCapsInit())) goto cleanup; - if (!(driver->xmlopt = vmwareDomainXMLConfigInit())) + if (!(driver->xmlopt = vmwareDomainXMLConfigInit(driver))) goto cleanup; if (vmwareLoadDomains(driver) < 0) diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c index c2a06daecb..958b168891 100644 --- a/src/vmx/vmx.c +++ b/src/vmx/vmx.c @@ -530,11 +530,12 @@ VIR_ENUM_IMPL(virVMXControllerModelSCSI, static int virVMXDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr _caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED, + void *opaque, void *parseOpaque G_GNUC_UNUSED) { + virCapsPtr caps = opaque; if (!virCapabilitiesDomainSupported(caps, def->os.type, def->os.arch, def->virtType)) @@ -612,8 +613,9 @@ static virXMLNamespace virVMXDomainXMLNamespace = { }; virDomainXMLOptionPtr -virVMXDomainXMLConfInit(void) +virVMXDomainXMLConfInit(virCapsPtr caps) { + virVMXDomainDefParserConfig.priv = caps; return virDomainXMLOptionNew(&virVMXDomainDefParserConfig, NULL, &virVMXDomainXMLNamespace, NULL, NULL); } diff --git a/src/vmx/vmx.h b/src/vmx/vmx.h index 18478ac0ae..63f47822fb 100644 --- a/src/vmx/vmx.h +++ b/src/vmx/vmx.h @@ -29,7 +29,7 @@ typedef struct _virVMXContext virVMXContext; -virDomainXMLOptionPtr virVMXDomainXMLConfInit(void); +virDomainXMLOptionPtr virVMXDomainXMLConfInit(virCapsPtr caps); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c index 66b15737a2..a0d1efb9dd 100644 --- a/src/vz/vz_driver.c +++ b/src/vz/vz_driver.c @@ -241,12 +241,13 @@ vzDomainDefAddDefaultInputDevices(virDomainDefPtr def) static int vzDomainDefPostParse(virDomainDefPtr def, - virCapsPtr caps, + virCapsPtr caps G_GNUC_UNUSED, unsigned int parseFlags G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED, + void *opaque, void *parseOpaque G_GNUC_UNUSED) { - if (!virCapabilitiesDomainSupported(caps, def->os.type, + vzDriverPtr driver = opaque; + if (!virCapabilitiesDomainSupported(driver->caps, def->os.type, def->os.arch, def->virtType)) return -1; @@ -289,10 +290,12 @@ vzDomainDeviceDefPostParse(virDomainDeviceDefPtr dev, static int vzDomainDeviceDefValidate(const virDomainDeviceDef *dev, const virDomainDef *def, - void *opaque G_GNUC_UNUSED) + void *opaque) { + vzDriverPtr driver = opaque; + if (dev->type == VIR_DOMAIN_DEVICE_DISK) - return vzCheckUnsupportedDisk(def, dev->data.disk, opaque); + return vzCheckUnsupportedDisk(def, dev->data.disk, driver->vzCaps); else if (dev->type == VIR_DOMAIN_DEVICE_GRAPHICS) return vzCheckUnsupportedGraphics(dev->data.graphics); @@ -323,7 +326,7 @@ vzDriverObjNew(void) if (!(driver = virObjectLockableNew(vzDriverClass))) return NULL; - vzDomainDefParserConfig.priv = &driver->vzCaps; + vzDomainDefParserConfig.priv = driver; if (!(driver->caps = vzBuildCapabilities()) || !(driver->xmlopt = virDomainXMLOptionNew(&vzDomainDefParserConfig, diff --git a/tests/Makefile.am b/tests/Makefile.am index 2362b770b5..9716d9d2be 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1048,11 +1048,17 @@ endif WITH_LXC if WITH_QEMU vircapstest_SOURCES += testutilsqemu.c testutilsqemu.h endif WITH_QEMU +vircapstest_LDADD = if WITH_QEMU -vircapstest_LDADD = $(qemu_LDADDS) -else ! WITH_QEMU -vircapstest_LDADD = $(LDADDS) -endif ! WITH_QEMU +vircapstest_LDADD += ../src/libvirt_driver_qemu_impl.la +if WITH_DTRACE_PROBES +vircapstest_LDADD += ../src/libvirt_qemu_probes.lo +endif WITH_DTRACE_PROBES +endif WITH_QEMU +if WITH_LXC +vircapstest_LDADD += ../src/libvirt_driver_lxc_impl.la +endif WITH_LXC +vircapstest_LDADD += $(LDADDS) libdomaincapsmock_la_SOURCES = domaincapsmock.c libdomaincapsmock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS) diff --git a/tests/libxlxml2domconfigtest.c b/tests/libxlxml2domconfigtest.c index 5bda6db360..5c8dcf8503 100644 --- a/tests/libxlxml2domconfigtest.c +++ b/tests/libxlxml2domconfigtest.c @@ -41,7 +41,7 @@ # define VIR_FROM_THIS VIR_FROM_LIBXL -static virCapsPtr caps; +static libxlDriverPrivatePtr driver; static int testCompareXMLToDomConfig(const char *xmlfile, @@ -50,19 +50,13 @@ testCompareXMLToDomConfig(const char *xmlfile, int ret = -1; libxl_domain_config actualconfig; libxl_domain_config expectconfig; - libxlDriverConfigPtr cfg; xentoollog_logger *log = NULL; virPortAllocatorRangePtr gports = NULL; - virDomainXMLOptionPtr xmlopt = NULL; virDomainDefPtr vmdef = NULL; char *actualjson = NULL; char *tempjson = NULL; char *expectjson = NULL; - - if (!(cfg = libxlDriverConfigNew())) - return -1; - - cfg->caps = caps; + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); libxl_domain_config_init(&actualconfig); libxl_domain_config_init(&expectconfig); @@ -82,10 +76,7 @@ testCompareXMLToDomConfig(const char *xmlfile, if (!(gports = virPortAllocatorRangeNew("vnc", 5900, 6000))) goto cleanup; - if (!(xmlopt = libxlCreateXMLConf())) - goto cleanup; - - if (!(vmdef = virDomainDefParseFile(xmlfile, caps, xmlopt, + if (!(vmdef = virDomainDefParseFile(xmlfile, cfg->caps, driver->xmlopt, NULL, VIR_DOMAIN_XML_INACTIVE))) goto cleanup; @@ -128,12 +119,9 @@ testCompareXMLToDomConfig(const char *xmlfile, VIR_FREE(tempjson); virDomainDefFree(vmdef); virPortAllocatorRangeFree(gports); - virObjectUnref(xmlopt); libxl_domain_config_dispose(&actualconfig); libxl_domain_config_dispose(&expectconfig); xtl_logger_destroy(log); - cfg->caps = NULL; - virObjectUnref(cfg); return ret; } @@ -177,7 +165,7 @@ mymain(void) return EXIT_FAILURE; } - if ((caps = testXLInitCaps()) == NULL) + if ((driver = testXLInitDriver()) == NULL) return EXIT_FAILURE; # define DO_TEST(name) \ @@ -216,6 +204,8 @@ mymain(void) unlink("libxl-driver.log"); + testXLFreeDriver(driver); + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/lxcconf2xmltest.c b/tests/lxcconf2xmltest.c index 7adc23be9f..fffe9cbf1a 100644 --- a/tests/lxcconf2xmltest.c +++ b/tests/lxcconf2xmltest.c @@ -10,8 +10,7 @@ # define VIR_FROM_THIS VIR_FROM_NONE -static virCapsPtr caps; -static virDomainXMLOptionPtr xmlopt; +static virLXCDriverPtr driver; static int testSanitizeDef(virDomainDefPtr vmdef) { @@ -34,7 +33,7 @@ testCompareXMLToConfigFiles(const char *xmlfile, if (virTestLoadFile(configfile, &config) < 0) goto fail; - vmdef = lxcParseConfigString(config, caps, xmlopt); + vmdef = lxcParseConfigString(config, driver->caps, driver->xmlopt); if ((vmdef && expectError) || (!vmdef && !expectError)) goto fail; @@ -42,7 +41,7 @@ testCompareXMLToConfigFiles(const char *xmlfile, if (testSanitizeDef(vmdef) < 0) goto fail; - if (!(actualxml = virDomainDefFormat(vmdef, xmlopt, caps, 0))) + if (!(actualxml = virDomainDefFormat(vmdef, driver->xmlopt, driver->caps, 0))) goto fail; if (virTestCompareToFile(actualxml, xmlfile) < 0) @@ -109,14 +108,9 @@ mymain(void) { int ret = EXIT_SUCCESS; - if (!(caps = testLXCCapsInit())) + if (!(driver = testLXCDriverInit())) return EXIT_FAILURE; - if (!(xmlopt = lxcDomainXMLConfInit())) { - virObjectUnref(caps); - return EXIT_FAILURE; - } - # define DO_TEST(name, expectError) \ do { \ const struct testInfo info = { name, expectError }; \ @@ -166,8 +160,7 @@ mymain(void) DO_TEST3("blkiotune", false); DO_TEST3("ethernet", false); - virObjectUnref(xmlopt); - virObjectUnref(caps); + testLXCDriverFree(driver); return ret; } diff --git a/tests/lxcxml2xmltest.c b/tests/lxcxml2xmltest.c index 6a720503a9..7b05f7d016 100644 --- a/tests/lxcxml2xmltest.c +++ b/tests/lxcxml2xmltest.c @@ -16,8 +16,7 @@ # define VIR_FROM_THIS VIR_FROM_NONE -static virCapsPtr caps; -static virDomainXMLOptionPtr xmlopt; +static virLXCDriverPtr driver; struct testInfo { const char *name; @@ -39,7 +38,7 @@ testCompareXMLToXMLHelper(const void *data) xml_out = g_strdup_printf("%s/lxcxml2xmloutdata/lxc-%s.xml", abs_srcdir, info->name); - ret = testCompareDomXML2XMLFiles(caps, xmlopt, xml_in, + ret = testCompareDomXML2XMLFiles(driver->caps, driver->xmlopt, xml_in, info->different ? xml_out : xml_in, !info->inactive_only, info->parse_flags, @@ -55,10 +54,7 @@ mymain(void) { int ret = 0; - if ((caps = testLXCCapsInit()) == NULL) - return EXIT_FAILURE; - - if (!(xmlopt = lxcDomainXMLConfInit())) + if (!(driver = testLXCDriverInit())) return EXIT_FAILURE; # define DO_TEST_FULL(name, is_different, inactive, parse_flags) \ @@ -95,8 +91,7 @@ mymain(void) DO_TEST("initdir"); DO_TEST("inituser"); - virObjectUnref(caps); - virObjectUnref(xmlopt); + testLXCDriverFree(driver); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/openvzutilstest.c b/tests/openvzutilstest.c index f43867ddc9..681f1a52a0 100644 --- a/tests/openvzutilstest.c +++ b/tests/openvzutilstest.c @@ -98,7 +98,10 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED) " \n" " \n" "\n"; - virDomainXMLOptionPtr xmlopt = openvzXMLOption(); + struct openvz_driver driver = {0}; + + driver.xmlopt = openvzXMLOption(&driver); + driver.caps = openvzCapsInit(); if (!(def = virDomainDefNew())) goto cleanup; @@ -113,7 +116,7 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED) goto cleanup; } - actual = virDomainDefFormat(def, xmlopt, NULL, VIR_DOMAIN_DEF_FORMAT_INACTIVE); + actual = virDomainDefFormat(def, driver.xmlopt, driver.caps, VIR_DOMAIN_DEF_FORMAT_INACTIVE); if (actual == NULL) { fprintf(stderr, "ERROR: %s\n", virGetLastErrorMessage()); @@ -128,7 +131,8 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED) result = 0; cleanup: - virObjectUnref(xmlopt); + virObjectUnref(driver.xmlopt); + virObjectUnref(driver.caps); VIR_FREE(actual); virDomainDefFree(def); diff --git a/tests/testutilslxc.c b/tests/testutilslxc.c index eed18304c4..b5e2f542e7 100644 --- a/tests/testutilslxc.c +++ b/tests/testutilslxc.c @@ -6,8 +6,10 @@ # include "viralloc.h" # include "domain_conf.h" +# define VIR_FROM_THIS VIR_FROM_LXC -virCapsPtr testLXCCapsInit(void) +virCapsPtr +testLXCCapsInit(void) { virCapsPtr caps; virCapsGuestPtr guest; @@ -54,4 +56,34 @@ virCapsPtr testLXCCapsInit(void) virObjectUnref(caps); return NULL; } + + +virLXCDriverPtr +testLXCDriverInit(void) +{ + virLXCDriverPtr driver = g_new0(virLXCDriver, 1); + + if (virMutexInit(&driver->lock) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", "cannot initialize mutex"); + g_free(driver); + return NULL; + } + + driver->caps = testLXCCapsInit(); + driver->xmlopt = lxcDomainXMLConfInit(driver); + + return driver; +} + + +void +testLXCDriverFree(virLXCDriverPtr driver) +{ + virObjectUnref(driver->xmlopt); + virObjectUnref(driver->caps); + virMutexDestroy(&driver->lock); + g_free(driver); +} + #endif diff --git a/tests/testutilslxc.h b/tests/testutilslxc.h index 6339f88734..fe170540b5 100644 --- a/tests/testutilslxc.h +++ b/tests/testutilslxc.h @@ -18,7 +18,11 @@ #include "capabilities.h" +#include "lxc/lxc_conf.h" + #define FAKEDEVDIR0 "/fakedevdir0/bla/fasl" #define FAKEDEVDIR1 "/fakedevdir1/bla/fasl" virCapsPtr testLXCCapsInit(void); +virLXCDriverPtr testLXCDriverInit(void); +void testLXCDriverFree(virLXCDriverPtr driver); diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c index 314d96abe8..75cd42ec43 100644 --- a/tests/testutilsxen.c +++ b/tests/testutilsxen.c @@ -6,7 +6,9 @@ #include "testutilshostcpus.h" #include "domain_conf.h" -virCapsPtr +#define VIR_FROM_THIS VIR_FROM_LIBXL + +static virCapsPtr testXLInitCaps(void) { virCapsPtr caps; @@ -79,3 +81,33 @@ testXLInitCaps(void) virObjectUnref(caps); return NULL; } + + +libxlDriverPrivatePtr testXLInitDriver(void) +{ + libxlDriverPrivatePtr driver = g_new0(libxlDriverPrivate, 1); + + if (virMutexInit(&driver->lock) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", "cannot initialize mutex"); + g_free(driver); + return NULL; + } + + driver->config = libxlDriverConfigNew(); + + driver->config->caps = testXLInitCaps(); + + driver->xmlopt = libxlCreateXMLConf(driver); + + return driver; +} + +void testXLFreeDriver(libxlDriverPrivatePtr driver) +{ + virObjectUnref(driver->config->caps); + virObjectUnref(driver->config); + virObjectUnref(driver->xmlopt); + virMutexDestroy(&driver->lock); + g_free(driver); +} diff --git a/tests/testutilsxen.h b/tests/testutilsxen.h index 95dadb04c4..a31d3d9047 100644 --- a/tests/testutilsxen.h +++ b/tests/testutilsxen.h @@ -18,7 +18,10 @@ #include "capabilities.h" #ifdef WITH_LIBXL -# include "libxl/libxl_capabilities.h" -#endif +# include "libxl/libxl_conf.h" -virCapsPtr testXLInitCaps(void); +libxlDriverPrivatePtr testXLInitDriver(void); + +void testXLFreeDriver(libxlDriverPrivatePtr driver); + +#endif /* WITH_LIBXL */ diff --git a/tests/vmx2xmltest.c b/tests/vmx2xmltest.c index 72ee2a38c8..beb82cbd7c 100644 --- a/tests/vmx2xmltest.c +++ b/tests/vmx2xmltest.c @@ -186,7 +186,7 @@ mymain(void) if (caps == NULL) return EXIT_FAILURE; - if (!(xmlopt = virVMXDomainXMLConfInit())) + if (!(xmlopt = virVMXDomainXMLConfInit(caps))) return EXIT_FAILURE; ctx.opaque = NULL; diff --git a/tests/xlconfigtest.c b/tests/xlconfigtest.c index 890d8bf2b4..d077933ca9 100644 --- a/tests/xlconfigtest.c +++ b/tests/xlconfigtest.c @@ -36,9 +36,7 @@ #define VIR_FROM_THIS VIR_FROM_NONE -static virCapsPtr caps; -static virDomainXMLOptionPtr xmlopt; - +static libxlDriverPrivatePtr driver; /* * This function provides a mechanism to replace variables in test @@ -74,6 +72,7 @@ testCompareParseXML(const char *xlcfg, const char *xml, bool replaceVars) int ret = -1; virDomainDefPtr def = NULL; char *replacedXML = NULL; + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); if (VIR_ALLOC_N(gotxlcfgData, wrote) < 0) goto fail; @@ -84,16 +83,16 @@ testCompareParseXML(const char *xlcfg, const char *xml, bool replaceVars) if (replaceVars) { if (!(replacedXML = testReplaceVarsXML(xml))) goto fail; - if (!(def = virDomainDefParseString(replacedXML, caps, xmlopt, + if (!(def = virDomainDefParseString(replacedXML, cfg->caps, driver->xmlopt, NULL, VIR_DOMAIN_XML_INACTIVE))) goto fail; } else { - if (!(def = virDomainDefParseFile(xml, caps, xmlopt, + if (!(def = virDomainDefParseFile(xml, cfg->caps, driver->xmlopt, NULL, VIR_DOMAIN_XML_INACTIVE))) goto fail; } - if (!virDomainDefCheckABIStability(def, def, xmlopt)) { + if (!virDomainDefCheckABIStability(def, def, driver->xmlopt)) { fprintf(stderr, "ABI stability check failed on %s", xml); goto fail; } @@ -133,6 +132,7 @@ testCompareFormatXML(const char *xlcfg, const char *xml, bool replaceVars) virConnectPtr conn; virDomainDefPtr def = NULL; char *replacedXML = NULL; + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); conn = virGetConnect(); if (!conn) goto fail; @@ -143,10 +143,10 @@ testCompareFormatXML(const char *xlcfg, const char *xml, bool replaceVars) if (!(conf = virConfReadString(xlcfgData, 0))) goto fail; - if (!(def = xenParseXL(conf, caps, xmlopt))) + if (!(def = xenParseXL(conf, cfg->caps, driver->xmlopt))) goto fail; - if (!(gotxml = virDomainDefFormat(def, xmlopt, caps, + if (!(gotxml = virDomainDefFormat(def, driver->xmlopt, cfg->caps, VIR_DOMAIN_XML_INACTIVE | VIR_DOMAIN_XML_SECURE))) goto fail; @@ -208,10 +208,7 @@ mymain(void) { int ret = 0; - if (!(caps = testXLInitCaps())) - return EXIT_FAILURE; - - if (!(xmlopt = libxlCreateXMLConf())) + if (!(driver = testXLInitDriver())) return EXIT_FAILURE; #define DO_TEST_PARSE(name, replace) \ @@ -303,10 +300,9 @@ mymain(void) DO_TEST("usb"); DO_TEST("usbctrl"); - virObjectUnref(caps); - virObjectUnref(xmlopt); + testXLFreeDriver(driver); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -VIR_TEST_MAIN(mymain) +VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("xl")) diff --git a/tests/xmconfigtest.c b/tests/xmconfigtest.c index 4a4d4192db..6971ad76e2 100644 --- a/tests/xmconfigtest.c +++ b/tests/xmconfigtest.c @@ -34,8 +34,7 @@ #define VIR_FROM_THIS VIR_FROM_NONE -static virCapsPtr caps; -static virDomainXMLOptionPtr xmlopt; +static libxlDriverPrivatePtr driver; static int testCompareParseXML(const char *xmcfg, const char *xml) @@ -46,6 +45,7 @@ testCompareParseXML(const char *xmcfg, const char *xml) virConnectPtr conn = NULL; int wrote = 4096; virDomainDefPtr def = NULL; + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0) goto fail; @@ -53,11 +53,11 @@ testCompareParseXML(const char *xmcfg, const char *xml) conn = virGetConnect(); if (!conn) goto fail; - if (!(def = virDomainDefParseFile(xml, caps, xmlopt, NULL, + if (!(def = virDomainDefParseFile(xml, cfg->caps, driver->xmlopt, NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; - if (!virDomainDefCheckABIStability(def, def, xmlopt)) { + if (!virDomainDefCheckABIStability(def, def, driver->xmlopt)) { fprintf(stderr, "ABI stability check failed on %s", xml); goto fail; } @@ -90,6 +90,7 @@ testCompareFormatXML(const char *xmcfg, const char *xml) g_autoptr(virConf) conf = NULL; int ret = -1; virDomainDefPtr def = NULL; + g_autoptr(libxlDriverConfig) cfg = libxlDriverConfigGet(driver); if (virTestLoadFile(xmcfg, &xmcfgData) < 0) goto fail; @@ -97,10 +98,10 @@ testCompareFormatXML(const char *xmcfg, const char *xml) if (!(conf = virConfReadString(xmcfgData, 0))) goto fail; - if (!(def = xenParseXM(conf, caps, xmlopt))) + if (!(def = xenParseXM(conf, cfg->caps, driver->xmlopt))) goto fail; - if (!(gotxml = virDomainDefFormat(def, xmlopt, caps, VIR_DOMAIN_DEF_FORMAT_SECURE))) + if (!(gotxml = virDomainDefFormat(def, driver->xmlopt, cfg->caps, VIR_DOMAIN_DEF_FORMAT_SECURE))) goto fail; if (virTestCompareToFile(gotxml, xml) < 0) @@ -152,10 +153,7 @@ mymain(void) { int ret = 0; - if (!(caps = testXLInitCaps())) - return EXIT_FAILURE; - - if (!(xmlopt = libxlCreateXMLConf())) + if (!(driver = testXLInitDriver())) return EXIT_FAILURE; #define DO_TEST_PARSE(name) \ @@ -225,10 +223,9 @@ mymain(void) DO_TEST("disk-drv-blktap-raw"); DO_TEST("disk-drv-blktap2-raw"); - virObjectUnref(caps); - virObjectUnref(xmlopt); + testXLFreeDriver(driver); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -VIR_TEST_MAIN(mymain) +VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("xl")) diff --git a/tests/xml2vmxtest.c b/tests/xml2vmxtest.c index 8dc37f265a..39781beafd 100644 --- a/tests/xml2vmxtest.c +++ b/tests/xml2vmxtest.c @@ -207,7 +207,7 @@ mymain(void) if (caps == NULL) return EXIT_FAILURE; - if (!(xmlopt = virVMXDomainXMLConfInit())) + if (!(xmlopt = virVMXDomainXMLConfInit(caps))) return EXIT_FAILURE; ctx.opaque = NULL;