[PATCH 2/4] qemu: Add support for virDomainGetCPUMode and virDomainSetCPUMode

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds support for virDomainGetCPUMode
and virDomainSetCPUMode into qemu driver.

Signed-off-by: Ken ICHIKAWA <ichikawa.ken@xxxxxxxxxxxxxx>
---
 src/libvirt_private.syms |   1 +
 src/qemu/qemu_driver.c   | 129 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 130 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 499ab3b..8988efa 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -207,6 +207,7 @@ virCPUDefFormatBuf;
 virCPUDefFree;
 virCPUDefFreeModel;
 virCPUDefParseXML;
+virCPUModeTypeFromString;
 virCPUModeTypeToString;
 
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e099c5c..f86ec1f 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -14871,6 +14871,133 @@ cleanup:
     return ret;
 }
 
+static int
+qemuDomainSetCPUMode(virDomainPtr dom,
+                     const char *cpuMode,
+                     unsigned int flags)
+{
+    virQEMUDriverPtr driver = dom->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    virDomainDefPtr persistentDef = NULL;
+    int mode = -1;
+    int ret = -1;
+
+    virCheckFlags(0, -1);
+
+    qemuDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+    qemuDriverUnlock(driver);
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        virReportError(VIR_ERR_NO_DOMAIN,
+                       _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if ((mode = virCPUModeTypeFromString(cpuMode)) < 0){
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                       _("Invalid cpu mode '%s'"), cpuMode);
+        goto cleanup;
+    }
+
+    /* get persistent configuration */
+    if (!vm->persistent) {
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                       _("cannot change persistent config of a "
+                         "transient domain"));
+        goto cleanup;
+    }
+    if (!(persistentDef = virDomainObjGetPersistentDef(driver->caps, vm))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Get persistent config failed"));
+        goto cleanup;
+    }
+
+    if (!persistentDef->cpu) {
+        if (VIR_ALLOC(persistentDef->cpu) < 0)
+            goto no_memory;
+        persistentDef->cpu->type = VIR_CPU_TYPE_GUEST;
+    }
+
+    persistentDef->cpu->mode = mode;
+
+    if (virDomainSaveConfig(driver->configDir, persistentDef) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+cleanup:
+    if (vm)
+        virDomainObjUnlock(vm);
+    return ret;
+no_memory:
+    virReportOOMError();
+    goto cleanup;
+}
+
+static char *
+qemuDomainGetCPUMode(virDomainPtr dom, unsigned int flags)
+{
+    virQEMUDriverPtr driver = dom->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    virDomainDefPtr def = NULL;
+    const char *cpuMode = NULL;
+    char *ret = NULL;
+
+    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
+                  VIR_DOMAIN_AFFECT_CONFIG, NULL);
+
+    qemuDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+    qemuDriverUnlock(driver);
+
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        virReportError(VIR_ERR_NO_DOMAIN,
+                       _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if (virDomainLiveConfigHelperMethod(driver->caps, vm, &flags, &def) < 0)
+        goto cleanup;
+
+    /* use correct domain definition according to flags */
+    if (flags & VIR_DOMAIN_AFFECT_LIVE)
+        def = vm->def;
+
+    /* If there is no cpu element, return default cpu mode. */
+    if (!def->cpu) {
+        if (!(cpuMode = virCPUModeTypeToString(VIR_CPU_MODE_CUSTOM))){
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("unknown cpu mode in domain definition '%d'"),
+                           VIR_CPU_MODE_CUSTOM);
+            goto cleanup;
+        }
+        if (!(ret = strdup(cpuMode)))
+            goto no_memory;
+        goto cleanup;
+    }
+
+    if (!(cpuMode = virCPUModeTypeToString(def->cpu->mode))){
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("unknown cpu mode in domain definition '%d'"),
+                       def->cpu->mode);
+        goto cleanup;
+    }
+    if (!(ret = strdup(cpuMode)))
+        goto no_memory;
+
+cleanup:
+    if (vm)
+        virDomainObjUnlock(vm);
+    return ret;
+no_memory:
+    virReportOOMError();
+    goto cleanup;
+}
+
 static virDriver qemuDriver = {
     .no = VIR_DRV_QEMU,
     .name = QEMU_DRIVER_NAME,
@@ -15045,6 +15172,8 @@ static virDriver qemuDriver = {
     .nodeSetMemoryParameters = nodeSetMemoryParameters, /* 0.10.2 */
     .nodeGetCPUMap = nodeGetCPUMap, /* 1.0.0 */
     .domainFSTrim = qemuDomainFSTrim, /* 1.0.1 */
+    .domainSetCPUMode = qemuDomainSetCPUMode, /* 1.0.1 */
+    .domainGetCPUMode = qemuDomainGetCPUMode, /* 1.0.1 */
 };
 
 
-- 
1.7.11.7

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]