[libvirt] [PATCH v2 01/13] virsh: Add vcpu list completion to guestvcpus command

Lin Ma lma at suse.com
Tue Nov 10 09:50:52 UTC 2020


Signed-off-by: Lin Ma <lma at suse.com>
---
 tools/virsh-completer-domain.c | 75 ++++++++++++++++++++++++++++++++++
 tools/virsh-completer-domain.h |  4 ++
 tools/virsh-domain.c           |  1 +
 3 files changed, 80 insertions(+)

diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c
index c657627ac1..f653471b89 100644
--- a/tools/virsh-completer-domain.c
+++ b/tools/virsh-completer-domain.c
@@ -30,6 +30,7 @@
 #include "virstring.h"
 #include "virxml.h"
 #include "virperf.h"
+#include "virbitmap.h"
 
 char **
 virshDomainNameCompleter(vshControl *ctl,
@@ -581,3 +582,77 @@ virshDomainCpulistCompleter(vshControl *ctl,
 
     return virshCommaStringListComplete(cpuid, (const char **)cpulist);
 }
+
+char **
+virshDomainVcpulistViaAgentCompleter(vshControl *ctl,
+                                     const vshCmd *cmd,
+                                     unsigned int flags)
+{
+    virDomainPtr dom;
+    bool enable = vshCommandOptBool(cmd, "enable");
+    bool disable = vshCommandOptBool(cmd, "disable");
+    virTypedParameterPtr params = NULL;
+    unsigned int nparams = 0;
+    int dummy;
+    size_t i;
+    size_t offset = 0;
+    int nvcpus;
+    VIR_AUTOSTRINGLIST cpulist = NULL;
+    g_autoptr(virBitmap) vcpus = NULL;
+    g_autofree unsigned char *vcpumap = NULL;
+    g_autofree char *onlineVcpuStr = NULL;
+    const char *vcpuid = NULL;
+    char **ret = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        return NULL;
+
+    if (vshCommandOptStringQuiet(ctl, cmd, "cpulist", &vcpuid) < 0)
+        goto cleanup;
+
+    /* retrieve vcpu count from the guest instead of the hypervisor */
+    if ((nvcpus = virDomainGetVcpusFlags(dom, VIR_DOMAIN_VCPU_GUEST |
+                                         VIR_DOMAIN_VCPU_MAXIMUM)) < 0)
+        goto cleanup;
+
+    if (!enable && !disable) {
+        cpulist = g_new0(char *, nvcpus + 1);
+        for (i = 0; i < nvcpus; i++)
+            cpulist[i] = g_strdup_printf("%zu", i);
+    } else {
+        if (virDomainGetGuestVcpus(dom, &params, &nparams, 0) < 0)
+            goto cleanup;
+        onlineVcpuStr = vshGetTypedParamValue(ctl, &params[1]);
+        if (virBitmapParse(onlineVcpuStr, &vcpus, nvcpus) < 0)
+            goto cleanup;
+
+        if (virBitmapToData(vcpus, &vcpumap, &dummy) < 0)
+            goto cleanup;
+        if (enable) {
+            cpulist = g_new0(char *, nvcpus - virBitmapCountBits(vcpus) + 1);
+            for (i = 0; i < nvcpus; i++) {
+                if (VIR_CPU_USED(vcpumap, i) != 0)
+                    continue;
+
+                cpulist[offset++] = g_strdup_printf("%zu", i);
+            }
+        } else if (disable) {
+            cpulist = g_new0(char *, virBitmapCountBits(vcpus) + 1);
+            for (i = 0; i < nvcpus; i++) {
+                if (VIR_CPU_USED(vcpumap, i) == 0)
+                    continue;
+
+                cpulist[offset++] = g_strdup_printf("%zu", i);
+            }
+        }
+    }
+
+    ret = virshCommaStringListComplete(vcpuid, (const char **)cpulist);
+
+ cleanup:
+    virTypedParamsFree(params, nparams);
+    virshDomainFree(dom);
+    return ret;
+}
diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h
index d38efd5ea8..d5021f6aa6 100644
--- a/tools/virsh-completer-domain.h
+++ b/tools/virsh-completer-domain.h
@@ -90,3 +90,7 @@ char ** virshDomainVcpulistCompleter(vshControl *ctl,
 char ** virshDomainCpulistCompleter(vshControl *ctl,
                                     const vshCmd *cmd,
                                     unsigned int flags);
+
+char ** virshDomainVcpulistViaAgentCompleter(vshControl *ctl,
+                                             const vshCmd *cmd,
+                                             unsigned int flags);
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 1ae936c6b2..675d96440d 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -7436,6 +7436,7 @@ static const vshCmdOptDef opts_guestvcpus[] = {
     VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
     {.name = "cpulist",
      .type = VSH_OT_STRING,
+     .completer = virshDomainVcpulistViaAgentCompleter,
      .help = N_("list of cpus to enable or disable")
     },
     {.name = "enable",
-- 
2.26.0





More information about the libvir-list mailing list