Declare and use virDomainDeviceDefOperationsCallbacks in test / QEMU /
LXC / libxl drivers to store detachable devices that driver support,
this would be useful in future.
Also add test driver's memballoon device detach function and QEMU
driver's char device detach function to their xmlopt.
Signed-off-by: Luke Yue <lukedyue(a)gmail.com>
---
src/conf/domain_conf.h | 31 ++++++++++++++++++++++++++
src/libxl/libxl_conf.c | 3 ++-
src/libxl/libxl_domain.c | 8 +++++++
src/libxl/libxl_domain.h | 1 +
src/lxc/lxc_conf.c | 3 ++-
src/lxc/lxc_domain.c | 7 ++++++
src/lxc/lxc_domain.h | 1 +
src/qemu/qemu_conf.c | 2 +-
src/qemu/qemu_domain.c | 39 +++++++++++++++++++++++++++++++++
src/qemu/qemu_domain.h | 6 +++++
src/test/test_driver.c | 47 +++++++++++++++++++++++++++++++++++++++-
11 files changed, 144 insertions(+), 4 deletions(-)
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 1b092b2ee5..070cb40509 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -90,6 +90,37 @@ typedef enum {
VIR_DOMAIN_DEVICE_LAST
} virDomainDeviceType;
+typedef enum {
+ VIR_DOMAIN_DEVICE_FLAG_NONE = (1 << VIR_DOMAIN_DEVICE_NONE),
+ VIR_DOMAIN_DEVICE_FLAG_DISK = (1 << VIR_DOMAIN_DEVICE_DISK),
+ VIR_DOMAIN_DEVICE_FLAG_LEASE = (1 << VIR_DOMAIN_DEVICE_LEASE),
+ VIR_DOMAIN_DEVICE_FLAG_FS = (1 << VIR_DOMAIN_DEVICE_FS),
+ VIR_DOMAIN_DEVICE_FLAG_NET = (1 << VIR_DOMAIN_DEVICE_NET),
+ VIR_DOMAIN_DEVICE_FLAG_INPUT = (1 << VIR_DOMAIN_DEVICE_INPUT),
+ VIR_DOMAIN_DEVICE_FLAG_SOUND = (1 << VIR_DOMAIN_DEVICE_SOUND),
+ VIR_DOMAIN_DEVICE_FLAG_VIDEO = (1 << VIR_DOMAIN_DEVICE_VIDEO),
+ VIR_DOMAIN_DEVICE_FLAG_HOSTDEV = (1 << VIR_DOMAIN_DEVICE_HOSTDEV),
+ VIR_DOMAIN_DEVICE_FLAG_WATCHDOG = (1 << VIR_DOMAIN_DEVICE_WATCHDOG),
+ VIR_DOMAIN_DEVICE_FLAG_CONTROLLER = (1 << VIR_DOMAIN_DEVICE_CONTROLLER),
+ VIR_DOMAIN_DEVICE_FLAG_GRAPHICS = (1 << VIR_DOMAIN_DEVICE_GRAPHICS),
+ VIR_DOMAIN_DEVICE_FLAG_HUB = (1 << VIR_DOMAIN_DEVICE_HUB),
+ VIR_DOMAIN_DEVICE_FLAG_REDIRDEV = (1 << VIR_DOMAIN_DEVICE_REDIRDEV),
+ VIR_DOMAIN_DEVICE_FLAG_SMARTCARD = (1 << VIR_DOMAIN_DEVICE_SMARTCARD),
+ VIR_DOMAIN_DEVICE_FLAG_CHR = (1 << VIR_DOMAIN_DEVICE_CHR),
+ VIR_DOMAIN_DEVICE_FLAG_MEMBALLOON = (1 << VIR_DOMAIN_DEVICE_MEMBALLOON),
+ VIR_DOMAIN_DEVICE_FLAG_NVRAM = (1 << VIR_DOMAIN_DEVICE_NVRAM),
+ VIR_DOMAIN_DEVICE_FLAG_RNG = (1 << VIR_DOMAIN_DEVICE_RNG),
+ VIR_DOMAIN_DEVICE_FLAG_SHMEM = (1 << VIR_DOMAIN_DEVICE_SHMEM),
+ VIR_DOMAIN_DEVICE_FLAG_TPM = (1 << VIR_DOMAIN_DEVICE_TPM),
+ VIR_DOMAIN_DEVICE_FLAG_PANIC = (1 << VIR_DOMAIN_DEVICE_PANIC),
+ VIR_DOMAIN_DEVICE_FLAG_MEMORY = (1 << VIR_DOMAIN_DEVICE_MEMORY),
+ VIR_DOMAIN_DEVICE_FLAG_IOMMU = (1 << VIR_DOMAIN_DEVICE_IOMMU),
+ VIR_DOMAIN_DEVICE_FLAG_VSOCK = (1 << VIR_DOMAIN_DEVICE_VSOCK),
+ VIR_DOMAIN_DEVICE_FLAG_AUDIO = (1 << VIR_DOMAIN_DEVICE_AUDIO),
+
+ VIR_DOMAIN_DEVICE_FLAG_LAST = (1 << VIR_DOMAIN_DEVICE_LAST)
+} virDomainDeviceTypeFlags;
+
struct _virDomainDeviceDef {
int type; /* enum virDomainDeviceType */
union {
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index a586454f23..4ff790ecfe 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -2505,5 +2505,6 @@ libxlCreateXMLConf(libxlDriverPrivate *driver)
return virDomainXMLOptionNew(&libxlDomainDefParserConfig,
&libxlDomainXMLPrivateDataCallbacks,
&libxlDriverDomainXMLNamespace,
- NULL, NULL, NULL);
+ NULL, NULL,
+ &libxlDriverDeviceDefOpsCallbacks);
}
diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index 366e3b9263..55152f9405 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -1603,3 +1603,11 @@ virXMLNamespace libxlDriverDomainXMLNamespace = {
.prefix = "xen",
.uri = "http://libvirt.org/schemas/domain/xen/1.0",
};
+
+
+virDomainDeviceDefOperationsCallbacks libxlDriverDeviceDefOpsCallbacks = {
+ .detachFlags = VIR_DOMAIN_DEVICE_FLAG_DISK |
+ VIR_DOMAIN_DEVICE_FLAG_NET |
+ VIR_DOMAIN_DEVICE_FLAG_HOSTDEV |
+ VIR_DOMAIN_DEVICE_FLAG_CONTROLLER,
+};
diff --git a/src/libxl/libxl_domain.h b/src/libxl/libxl_domain.h
index 981bfc2bca..1534597cd9 100644
--- a/src/libxl/libxl_domain.h
+++ b/src/libxl/libxl_domain.h
@@ -68,6 +68,7 @@ struct _libxlDomainObjPrivate {
extern virDomainXMLPrivateDataCallbacks libxlDomainXMLPrivateDataCallbacks;
extern virDomainDefParserConfig libxlDomainDefParserConfig;
extern virXMLNamespace libxlDriverDomainXMLNamespace;
+extern virDomainDeviceDefOperationsCallbacks libxlDriverDeviceDefOpsCallbacks;
extern const struct libxl_event_hooks ev_hooks;
int
diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c
index c1d695e97b..31546aaeb5 100644
--- a/src/lxc/lxc_conf.c
+++ b/src/lxc/lxc_conf.c
@@ -194,7 +194,8 @@ lxcDomainXMLConfInit(virLXCDriver *driver, const char *defsecmodel)
return virDomainXMLOptionNew(&virLXCDriverDomainDefParserConfig,
&virLXCDriverPrivateDataCallbacks,
&virLXCDriverDomainXMLNamespace,
- NULL, NULL, NULL);
+ NULL, NULL,
+ &virLXCDriverDeviceDefOpsCallbacks);
}
diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c
index 0920e91fd1..5c3bc89c1c 100644
--- a/src/lxc/lxc_domain.c
+++ b/src/lxc/lxc_domain.c
@@ -496,3 +496,10 @@ virLXCDomainSetRunlevel(virDomainObj *vm,
data.st_valid = NULL;
return ret;
}
+
+
+virDomainDeviceDefOperationsCallbacks virLXCDriverDeviceDefOpsCallbacks = {
+ .detachFlags = VIR_DOMAIN_DEVICE_FLAG_DISK |
+ VIR_DOMAIN_DEVICE_FLAG_NET |
+ VIR_DOMAIN_DEVICE_FLAG_HOSTDEV,
+};
diff --git a/src/lxc/lxc_domain.h b/src/lxc/lxc_domain.h
index 766837bdf1..2bdd67886e 100644
--- a/src/lxc/lxc_domain.h
+++ b/src/lxc/lxc_domain.h
@@ -92,6 +92,7 @@ struct _virLXCDomainObjPrivate {
extern virXMLNamespace virLXCDriverDomainXMLNamespace;
extern virDomainXMLPrivateDataCallbacks virLXCDriverPrivateDataCallbacks;
extern virDomainDefParserConfig virLXCDriverDomainDefParserConfig;
+extern virDomainDeviceDefOperationsCallbacks virLXCDriverDeviceDefOpsCallbacks;
int
virLXCDomainObjBeginJob(virLXCDriver *driver,
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index ec85b6f5a6..41ad081079 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1291,7 +1291,7 @@ virQEMUDriverCreateXMLConf(virQEMUDriver *driver,
&virQEMUDriverDomainXMLNamespace,
&virQEMUDriverDomainABIStability,
&virQEMUDriverDomainSaveCookie,
- NULL);
+ &virQEMUDriverDeviceDefOpsCallbacks);
}
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 2c10f185f8..050baa7c2c 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -29,6 +29,7 @@
#include "qemu_process.h"
#include "qemu_capabilities.h"
#include "qemu_hostdev.h"
+#include "qemu_hotplug.h"
#include "qemu_migration.h"
#include "qemu_migration_params.h"
#include "qemu_security.h"
@@ -11455,3 +11456,41 @@ qemuDomainGetVHostUserFSSocketPath(qemuDomainObjPrivate *priv,
return virFileBuildPath(priv->libDir, fs->info.alias, "-fs.sock");
}
+
+
+int
+qemuDomainDetachDeviceChrConfig(virDomainDef *vmdef,
+ virDomainDeviceDef *dev,
+ unsigned int flags)
+{
+ virDomainChrDef *chr;
+
+ virCheckFlags(VIR_DOMAIN_AFFECT_CONFIG, -1);
+
+ if (!(chr = qemuDomainChrRemove(vmdef, dev->data.chr)))
+ return -1;
+
+ virDomainChrDefFree(chr);
+
+ return 0;
+}
+
+
+virDomainDeviceDefOperationsCallbacks virQEMUDriverDeviceDefOpsCallbacks = {
+ .detachChr = qemuDomainDetachDeviceChrConfig,
+ .detachFlags = VIR_DOMAIN_DEVICE_FLAG_DISK |
+ VIR_DOMAIN_DEVICE_FLAG_NET |
+ VIR_DOMAIN_DEVICE_FLAG_SOUND |
+ VIR_DOMAIN_DEVICE_FLAG_HOSTDEV |
+ VIR_DOMAIN_DEVICE_FLAG_LEASE |
+ VIR_DOMAIN_DEVICE_FLAG_CONTROLLER |
+ VIR_DOMAIN_DEVICE_FLAG_CHR |
+ VIR_DOMAIN_DEVICE_FLAG_FS |
+ VIR_DOMAIN_DEVICE_FLAG_RNG |
+ VIR_DOMAIN_DEVICE_FLAG_MEMORY |
+ VIR_DOMAIN_DEVICE_FLAG_REDIRDEV |
+ VIR_DOMAIN_DEVICE_FLAG_SHMEM |
+ VIR_DOMAIN_DEVICE_FLAG_WATCHDOG |
+ VIR_DOMAIN_DEVICE_FLAG_INPUT |
+ VIR_DOMAIN_DEVICE_FLAG_VSOCK,
+};
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index f2a303d8a7..87a36b232f 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -712,6 +712,7 @@ void qemuDomainObjPrivateDataClear(qemuDomainObjPrivate *priv);
extern virDomainXMLPrivateDataCallbacks virQEMUDriverPrivateDataCallbacks;
extern virXMLNamespace virQEMUDriverDomainXMLNamespace;
extern virDomainDefParserConfig virQEMUDriverDomainDefParserConfig;
+extern virDomainDeviceDefOperationsCallbacks virQEMUDriverDeviceDefOpsCallbacks;
extern virDomainABIStability virQEMUDriverDomainABIStability;
extern virSaveCookieCallbacks virQEMUDriverDomainSaveCookie;
@@ -1032,3 +1033,8 @@ qemuDomainNamePathsCleanup(virQEMUDriverConfig *cfg,
char *
qemuDomainGetVHostUserFSSocketPath(qemuDomainObjPrivate *priv,
const virDomainFSDef *fs);
+
+int
+qemuDomainDetachDeviceChrConfig(virDomainDef *vmdef,
+ virDomainDeviceDef *dev,
+ unsigned int flags);
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index 7febb4f5a6..21caa83be7 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -435,6 +435,31 @@ testDomainObjPrivateFree(void *data)
}
+static int
+testDomainDetachMemballoonDevice(virDomainDef *vmdef,
+ virDomainDeviceDef *dev G_GNUC_UNUSED,
+ unsigned int flags)
+{
+ if (flags & VIR_DOMAIN_AFFECT_LIVE) {
+ virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+ _("detach memballon device on running domain "
+ "is not supported"));
+ return -1;
+ }
+
+ if (!vmdef->memballoon) {
+ virReportError(VIR_ERR_DEVICE_MISSING, "%s",
+ _("no memballoon device was found"));
+ return -1;
+ }
+
+ virDomainMemballoonDefFree(vmdef->memballoon);
+ vmdef->memballoon = NULL;
+
+ return 0;
+}
+
+
static testDriver *
testDriverNew(void)
{
@@ -458,6 +483,26 @@ testDriverNew(void)
.alloc = testDomainObjPrivateAlloc,
.free = testDomainObjPrivateFree,
};
+ virDomainDeviceDefOperationsCallbacks deviceOps = {
+ .detachMemballoon = testDomainDetachMemballoonDevice,
+ .detachFlags = VIR_DOMAIN_DEVICE_FLAG_DISK |
+ VIR_DOMAIN_DEVICE_FLAG_NET |
+ VIR_DOMAIN_DEVICE_FLAG_SOUND |
+ VIR_DOMAIN_DEVICE_FLAG_HOSTDEV |
+ VIR_DOMAIN_DEVICE_FLAG_LEASE |
+ VIR_DOMAIN_DEVICE_FLAG_CONTROLLER |
+ VIR_DOMAIN_DEVICE_FLAG_CHR |
+ VIR_DOMAIN_DEVICE_FLAG_FS |
+ VIR_DOMAIN_DEVICE_FLAG_RNG |
+ VIR_DOMAIN_DEVICE_FLAG_MEMORY |
+ VIR_DOMAIN_DEVICE_FLAG_REDIRDEV |
+ VIR_DOMAIN_DEVICE_FLAG_SHMEM |
+ VIR_DOMAIN_DEVICE_FLAG_WATCHDOG |
+ VIR_DOMAIN_DEVICE_FLAG_INPUT |
+ VIR_DOMAIN_DEVICE_FLAG_VSOCK |
+ VIR_DOMAIN_DEVICE_FLAG_TPM |
+ VIR_DOMAIN_DEVICE_FLAG_MEMBALLOON,
+ };
testDriver *ret;
if (testDriverInitialize() < 0)
@@ -467,7 +512,7 @@ testDriverNew(void)
return NULL;
if (!(ret->xmlopt = virDomainXMLOptionNew(&config, &privatecb, &ns,
- NULL, NULL, NULL)) ||
+ NULL, NULL, &deviceOps)) ||
!(ret->eventState = virObjectEventStateNew()) ||
!(ret->ifaces = virInterfaceObjListNew()) ||
!(ret->domains = virDomainObjListNew()) ||
--
2.34.1