[libvirt] [PATCH 09/12] vcpu: add virsh support

Eric Blake eblake at redhat.com
Thu Sep 30 00:02:13 UTC 2010


* tools/virsh.c (cmdSetvcpus): Add new flags.  Let invalid
commands through to driver, to ease testing of hypervisor argument
validation.
(cmdVcpucount): New command.
(commands): Add new command.
* tools/virsh.pod (setvcpus, vcpucount): Document new behavior.
---

I know - the typical API addition sequence adds driver support
first and then virsh support.  I can rearrange the patch order
if desired.

 tools/virsh.c   |  211 +++++++++++++++++++++++++++++++++++++++++++++++++------
 tools/virsh.pod |   28 +++++++-
 2 files changed, 217 insertions(+), 22 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 85014f2..0fcfdb7 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -2220,10 +2220,181 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
 }

 /*
+ * "vcpucount" command
+ */
+static const vshCmdInfo info_vcpucount[] = {
+    {"help", N_("domain vcpu counts")},
+    {"desc", N_("Returns the number of domain virtual CPUs.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_vcpucount[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"maximum", VSH_OT_BOOL, 0, N_("get maximum cap on vcpus")},
+    {"current", VSH_OT_BOOL, 0, N_("get current vcpu usage")},
+    {"persistent", VSH_OT_BOOL, 0, N_("get value to be used on next boot")},
+    {"active", VSH_OT_BOOL, 0, N_("get value from running domain")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdVcpucount(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    int ret = TRUE;
+    int maximum = vshCommandOptBool(cmd, "maximum");
+    int current = vshCommandOptBool(cmd, "current");
+    int persistent = vshCommandOptBool(cmd, "persistent");
+    int active = vshCommandOptBool(cmd, "active");
+    bool all = maximum + current + persistent + active == 0;
+    int count;
+
+    if (maximum && current) {
+        vshError(ctl, "%s",
+                 _("--maximum and --current cannot both be specified"));
+        return FALSE;
+    }
+    if (persistent && active) {
+        vshError(ctl, "%s",
+                 _("--persistent and --active cannot both be specified"));
+        return FALSE;
+    }
+    if (maximum + current + persistent + active == 1) {
+        vshError(ctl,
+                 _("when using --%s, either --%s or --%s must be specified"),
+                 maximum ? "maximum" : current ? "current"
+                 : persistent ? "persistent" : "active",
+                 maximum + current ? "persistent" : "maximum",
+                 maximum + current ? "active" : "current");
+        return FALSE;
+    }
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return FALSE;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return FALSE;
+
+    /* In all cases, try the new API first; if it fails because we are
+     * talking to an older client, try a fallback API before giving
+     * up.  */
+    if (all || (maximum && persistent)) {
+        count = virDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_MAXIMUM |
+                                             VIR_DOMAIN_VCPU_PERSISTENT));
+        if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
+                          || last_error->code == VIR_ERR_INVALID_ARG)) {
+            char *tmp;
+            char *xml = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
+            if (xml && (tmp = strstr(xml, "<vcpu"))) {
+                tmp = strchr(tmp, '>');
+                if (!tmp || virStrToLong_i(tmp + 1, &tmp, 10, &count) < 0)
+                    count = -1;
+            }
+            VIR_FREE(xml);
+        }
+
+        if (count < 0) {
+            virshReportError(ctl);
+            ret = FALSE;
+        } else if (all) {
+            vshPrint(ctl, "%-12s %-12s %3d\n", _("maximum"), _("persistent"),
+                     count);
+        } else {
+            vshPrint(ctl, "%d\n", count);
+        }
+        virFreeError(last_error);
+        last_error = NULL;
+    }
+
+    if (all || (maximum && active)) {
+        count = virDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_MAXIMUM |
+                                             VIR_DOMAIN_VCPU_ACTIVE));
+        if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
+                          || last_error->code == VIR_ERR_INVALID_ARG)) {
+            count = virDomainGetMaxVcpus(dom);
+        }
+
+        if (count < 0) {
+            virshReportError(ctl);
+            ret = FALSE;
+        } else if (all) {
+            vshPrint(ctl, "%-12s %-12s %3d\n", _("maximum"), _("active"),
+                     count);
+        } else {
+            vshPrint(ctl, "%d\n", count);
+        }
+        virFreeError(last_error);
+        last_error = NULL;
+    }
+
+    if (all || (current && persistent)) {
+        count = virDomainGetVcpusFlags(dom, VIR_DOMAIN_VCPU_PERSISTENT);
+        if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
+                          || last_error->code == VIR_ERR_INVALID_ARG)) {
+            char *tmp, *end;
+            char *xml = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
+            if (xml && (tmp = strstr(xml, "<vcpu"))) {
+                end = strchr(tmp, '>');
+                if (end) {
+                    *end = '\0';
+                    tmp = strstr(tmp, "current=");
+                    if (!tmp)
+                        tmp = end + 1;
+                    else {
+                        tmp += strlen("current=");
+                        tmp += *tmp == '\'' || *tmp == '"';
+                    }
+                }
+                if (!tmp || virStrToLong_i(tmp, &tmp, 10, &count) < 0)
+                    count = -1;
+            }
+            VIR_FREE(xml);
+        }
+
+        if (count < 0) {
+            virshReportError(ctl);
+            ret = FALSE;
+        } else if (all) {
+            vshPrint(ctl, "%-12s %-12s %3d\n", _("current"), _("persistent"),
+                     count);
+        } else {
+            vshPrint(ctl, "%d\n", count);
+        }
+        virFreeError(last_error);
+        last_error = NULL;
+    }
+
+    if (all || (current && active)) {
+        count = virDomainGetVcpusFlags(dom, VIR_DOMAIN_VCPU_ACTIVE);
+        if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
+                          || last_error->code == VIR_ERR_INVALID_ARG)) {
+            virDomainInfo info;
+            if (virDomainGetInfo(dom, &info) == 0)
+                count = info.nrVirtCpu;
+        }
+
+        if (count < 0) {
+            virshReportError(ctl);
+            ret = FALSE;
+        } else if (all) {
+            vshPrint(ctl, "%-12s %-12s %3d\n", _("current"), _("active"),
+                     count);
+        } else {
+            vshPrint(ctl, "%d\n", count);
+        }
+        virFreeError(last_error);
+        last_error = NULL;
+    }
+
+    virDomainFree(dom);
+    return ret;
+}
+
+/*
  * "vcpuinfo" command
  */
 static const vshCmdInfo info_vcpuinfo[] = {
-    {"help", N_("domain vcpu information")},
+    {"help", N_("detailed domain vcpu information")},
     {"desc", N_("Returns basic information about the domain virtual CPUs.")},
     {NULL, NULL}
 };
@@ -2453,6 +2624,9 @@ static const vshCmdInfo info_setvcpus[] = {
 static const vshCmdOptDef opts_setvcpus[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
     {"count", VSH_OT_DATA, VSH_OFLAG_REQ, N_("number of virtual CPUs")},
+    {"maximum", VSH_OT_BOOL, 0, N_("set maximum limit on next boot")},
+    {"persistent", VSH_OT_BOOL, 0, N_("affect next boot")},
+    {"active", VSH_OT_BOOL, 0, N_("affect running domain")},
     {NULL, 0, 0, NULL}
 };

@@ -2461,8 +2635,13 @@ cmdSetvcpus(vshControl *ctl, const vshCmd *cmd)
 {
     virDomainPtr dom;
     int count;
-    int maxcpu;
     int ret = TRUE;
+    int maximum = vshCommandOptBool(cmd, "maximum");
+    int persistent = vshCommandOptBool(cmd, "persistent");
+    int active = vshCommandOptBool(cmd, "active");
+    int flags = ((maximum ? VIR_DOMAIN_VCPU_MAXIMUM : 0) |
+                 (persistent ? VIR_DOMAIN_VCPU_PERSISTENT : 0) |
+                 (active ? VIR_DOMAIN_VCPU_ACTIVE : 0));

     if (!vshConnectionUsability(ctl, ctl->conn))
         return FALSE;
@@ -2471,26 +2650,15 @@ cmdSetvcpus(vshControl *ctl, const vshCmd *cmd)
         return FALSE;

     count = vshCommandOptInt(cmd, "count", &count);
-    if (count <= 0) {
-        vshError(ctl, "%s", _("Invalid number of virtual CPUs."));
-        virDomainFree(dom);
-        return FALSE;
-    }
-
-    maxcpu = virDomainGetMaxVcpus(dom);
-    if (maxcpu <= 0) {
-        virDomainFree(dom);
-        return FALSE;
-    }

-    if (count > maxcpu) {
-        vshError(ctl, "%s", _("Too many virtual CPUs."));
-        virDomainFree(dom);
-        return FALSE;
-    }
-
-    if (virDomainSetVcpus(dom, count) != 0) {
-        ret = FALSE;
+    if (!flags) {
+        if (virDomainSetVcpus(dom, count) != 0) {
+            ret = FALSE;
+        }
+    } else {
+        if (virDomainSetVcpusFlags(dom, count, flags) < 0) {
+            ret = FALSE;
+        }
     }

     virDomainFree(dom);
@@ -9452,6 +9620,7 @@ static const vshCmdDef commands[] = {
     {"vol-name", cmdVolName, opts_vol_name, info_vol_name},
     {"vol-key", cmdVolKey, opts_vol_key, info_vol_key},

+    {"vcpucount", cmdVcpucount, opts_vcpucount, info_vcpucount},
     {"vcpuinfo", cmdVcpuinfo, opts_vcpuinfo, info_vcpuinfo},
     {"vcpupin", cmdVcpupin, opts_vcpupin, info_vcpupin},
     {"version", cmdVersion, NULL, info_version},
diff --git a/tools/virsh.pod b/tools/virsh.pod
index e0471b1..87fecaa 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -456,7 +456,8 @@ Change the maximum memory allocation limit in the guest domain. This should
 not change the current memory use. The memory limit is specified in
 kilobytes.

-=item B<setvcpus> I<domain-id> I<count>
+=item B<setvcpus> I<domain-id> I<count> optional I<--maximum> I<--persistent>
+I<--active>

 Change the number of virtual CPUs active in the guest domain. Note that
 I<count> may be limited by host, hypervisor or limit coming from the
@@ -465,6 +466,16 @@ original description of domain.
 For Xen, you can only adjust the virtual CPUs of a running domain if
 the domain is paravirtualized.

+If I<--persistent> is specified, the change will only affect the next
+boot of a domain.  If I<--active> is specified, the domain must be
+running, and the change takes place immediately.  Both flags may be
+specified, if supported by the hypervisor; and I<--active> is implied
+when neither flag is given.
+
+If I<--maximum> is specified, then you must use I<--persistent> and
+avoid I<--active>; this flag controls the maximum limit of vcpus that
+can be hot-plugged the next time the domain is booted.
+
 =item B<shutdown> I<domain-id>

 Gracefully shuts down a domain.  This coordinates with the domain OS
@@ -503,6 +514,21 @@ is not available the processes will provide an exit code of 1.
 Undefine the configuration for an inactive domain. Since it's not running
 the domain name or UUID must be used as the I<domain-id>.

+=item B<vcpucount> I<domain-id>  optional I<--maximum> I<--current>
+I<--persistent> I<--active>
+
+Print information about the virtual cpu counts of the given
+I<domain-id>.  If no flags are specified, all possible counts are
+listed in a table; otherwise, the output is limited to just the
+numeric value requested.
+
+I<--maximum> requests information on the maximum cap of vcpus that a
+domain can add via B<setvcpus>, while I<--current> shows the current
+usage; these two flags cannot both be specified.  I<--persistent>
+requests information regarding the next time the domain will be
+booted, while I<--active> requires a running domain and lists current
+values; these two flags cannot both be specified.
+
 =item B<vcpuinfo> I<domain-id>

 Returns basic information about the domain virtual CPUs, like the number of
-- 
1.7.2.3




More information about the libvir-list mailing list