[PATCH] libvirt: lxc: Add Get/Set vcpus for lxc

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

 



1.Add function to get vcpu count for lxc(vcpucount)
2.Add function to set vcpu count for lxc(setvcpus)

Signed-off-by: Li Yang <liyang.fnst@xxxxxxxxxxxxxx>
---
 src/lxc/lxc_driver.c |  159 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 159 insertions(+), 0 deletions(-)

diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 4741632..4df0738 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -5617,6 +5617,162 @@ lxcDomainGetMetadata(virDomainPtr dom,
     return ret;
 }
 
+static int
+lxcDomainSetVcpusFlags(virDomainPtr dom, unsigned int nvcpus,
+                        unsigned int flags)
+{
+    virLXCDriverPtr driver = dom->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    virDomainDefPtr persistentDef;
+    int ret = -1;
+    bool maximum;
+    unsigned int maxvcpus = 0;
+    virLXCDriverConfigPtr cfg = NULL;
+    virCapsPtr caps = NULL;
+
+    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
+                  VIR_DOMAIN_AFFECT_CONFIG |
+                  VIR_DOMAIN_VCPU_MAXIMUM |
+                  VIR_DOMAIN_VCPU_GUEST, -1);
+
+    if (!nvcpus || (unsigned short) nvcpus != nvcpus) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("argument out of range: %d"), nvcpus);
+        return -1;
+    }
+
+    if (!(vm = lxcDomObjFromDomain(dom)))
+        goto cleanup;
+
+    cfg = virLXCDriverGetConfig(driver);
+
+    if (virDomainSetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
+        goto cleanup;
+
+    if (!(caps = virLXCDriverGetCapabilities(driver, false)))
+        goto cleanup;
+
+    maximum = (flags & VIR_DOMAIN_VCPU_MAXIMUM) != 0;
+    flags &= ~VIR_DOMAIN_VCPU_MAXIMUM;
+
+    if (virDomainLiveConfigHelperMethod(caps, driver->xmlopt, vm, &flags,
+                                        &persistentDef) < 0)
+        goto cleanup;
+
+    /* MAXIMUM cannot be mixed with LIVE.  */
+    if (maximum && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("cannot adjust maximum on running domain"));
+        goto cleanup;
+    }
+
+    if (flags & VIR_DOMAIN_AFFECT_LIVE)
+        maxvcpus = vm->def->maxvcpus;
+    if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
+        if (!maxvcpus || maxvcpus > persistentDef->maxvcpus)
+            maxvcpus = persistentDef->maxvcpus;
+    }
+    if (!maximum && nvcpus > maxvcpus) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("requested vcpus is greater than max allowable"
+                         " vcpus for the domain: %d > %d"),
+                       nvcpus, maxvcpus);
+        goto cleanup;
+    }
+
+    if (flags & VIR_DOMAIN_VCPU_GUEST) {
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                       _("changing of vCPU count isn't supported "
+                         "via guest agent"));
+        goto cleanup;
+    } else {
+        if (flags & VIR_DOMAIN_AFFECT_LIVE) {
+            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                           _("Cannot hotplug vCPUS for LXC hypervisor"));
+                goto cleanup;
+        }
+
+        if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
+            if (maximum) {
+                persistentDef->maxvcpus = nvcpus;
+                if (nvcpus < persistentDef->vcpus)
+                    persistentDef->vcpus = nvcpus;
+            } else {
+                persistentDef->vcpus = nvcpus;
+            }
+
+            if (virDomainSaveConfig(cfg->configDir, persistentDef) < 0)
+                goto cleanup;
+        }
+    }
+
+    ret = 0;
+
+ cleanup:
+    if (vm)
+        virObjectUnlock(vm);
+    virObjectUnref(caps);
+    virObjectUnref(cfg);
+    return ret;
+}
+
+
+static int
+lxcDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
+{
+    return lxcDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_AFFECT_LIVE);
+}
+
+
+static int
+lxcDomainGetVcpusFlags(virDomainPtr dom, unsigned int flags)
+{
+    virLXCDriverPtr driver = dom->conn->privateData;
+    virDomainObjPtr vm;
+    virDomainDefPtr def;
+    int ret = -1;
+    virCapsPtr caps = NULL;
+
+    virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
+                  VIR_DOMAIN_AFFECT_CONFIG |
+                  VIR_DOMAIN_VCPU_MAXIMUM |
+                  VIR_DOMAIN_VCPU_GUEST, -1);
+
+    if (!(vm = lxcDomObjFromDomain(dom)))
+        return -1;
+
+    if (virDomainGetVcpusFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
+        goto cleanup;
+
+    if (!(caps = virLXCDriverGetCapabilities(driver, false)))
+        goto cleanup;
+
+    if (virDomainLiveConfigHelperMethod(caps, driver->xmlopt,
+                                        vm, &flags, &def) < 0)
+        goto cleanup;
+
+    if (flags & VIR_DOMAIN_AFFECT_LIVE)
+        def = vm->def;
+
+    if (flags & VIR_DOMAIN_VCPU_GUEST) {
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                       _("vCPU count cannot be provided by the guest agent"
+                         " for LXC hypervisor"));
+        goto cleanup;
+    } else {
+        if (flags & VIR_DOMAIN_VCPU_MAXIMUM)
+            ret = def->maxvcpus;
+        else
+            ret = def->vcpus;
+    }
+
+ cleanup:
+    if (vm)
+        virObjectUnlock(vm);
+    virObjectUnref(caps);
+    return ret;
+}
+
 
 static int
 lxcDomainGetCPUStats(virDomainPtr dom,
@@ -5746,6 +5902,9 @@ static virDriver lxcDriver = {
     .domainBlockStatsFlags = lxcDomainBlockStatsFlags, /* 1.2.2 */
     .domainInterfaceStats = lxcDomainInterfaceStats, /* 0.7.3 */
     .domainMemoryStats = lxcDomainMemoryStats, /* 1.2.2 */
+    .domainSetVcpus = lxcDomainSetVcpus, /* 1.2.8 */
+    .domainSetVcpusFlags = lxcDomainSetVcpusFlags, /* 1.2.8 */
+    .domainGetVcpusFlags = lxcDomainGetVcpusFlags, /* 1.2.8 */
     .nodeGetCPUStats = lxcNodeGetCPUStats, /* 0.9.3 */
     .nodeGetMemoryStats = lxcNodeGetMemoryStats, /* 0.9.3 */
     .nodeGetCellsFreeMemory = lxcNodeGetCellsFreeMemory, /* 0.6.5 */
-- 
1.7.1

--
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]