[libvirt] [PATCH 11/17] virsh: Add vcpu IDs completion to vcpupin command

Lin Ma morecache at gmail.com
Fri Sep 11 07:13:12 UTC 2020


Signed-off-by: Lin Ma <lma at suse.de>
---
 tools/virsh-completer-domain.c | 42 ++++++++++++++++++++++++++++++++++
 tools/virsh-completer-domain.h |  5 ++++
 tools/virsh-domain.c           |  2 ++
 3 files changed, 49 insertions(+)

diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c
index 56f014ae40..9b22db2119 100644
--- a/tools/virsh-completer-domain.c
+++ b/tools/virsh-completer-domain.c
@@ -476,3 +476,45 @@ virshDomainIOThreadIdCompleter(vshControl *ctl,
     virshDomainFree(dom);
     return ret;
 }
+
+
+char **
+virshDomainVcpuCompleter(vshControl *ctl,
+                         const vshCmd *cmd,
+                         unsigned int flags)
+{
+    virDomainPtr dom;
+    xmlDocPtr xml = NULL;
+    xmlXPathContextPtr ctxt = NULL;
+    int nvcpus = 0;
+    unsigned int id;
+    char **ret = NULL;
+    VIR_AUTOSTRINGLIST tmp = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        return NULL;
+
+    if (virshDomainGetXMLFromDom(ctl, dom, VIR_DOMAIN_XML_INACTIVE,
+                                 &xml, &ctxt) < 0)
+        goto cleanup;
+
+    /* Query the max rather than the current vcpu count */
+    if (virXPathInt("string(/domain/vcpu)", ctxt, &nvcpus) < 0)
+        goto cleanup;
+
+    if (VIR_ALLOC_N(tmp, nvcpus + 1) < 0)
+        goto cleanup;
+
+    for (id = 0; id < nvcpus; id++)
+        tmp[id] = g_strdup_printf("%u", id);
+
+    ret = g_steal_pointer(&tmp);
+
+ cleanup:
+    xmlXPathFreeContext(ctxt);
+    xmlFreeDoc(xml);
+    virshDomainFree(dom);
+    return ret;
+}
diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h
index 91731da778..7c3b4c3cc0 100644
--- a/tools/virsh-completer-domain.h
+++ b/tools/virsh-completer-domain.h
@@ -78,3 +78,8 @@ char ** virshDomainUUIDCompleter(vshControl *ctl,
 char ** virshDomainIOThreadIdCompleter(vshControl *ctl,
                                        const vshCmd *cmd,
                                        unsigned int flags);
+
+char **
+virshDomainVcpuCompleter(vshControl *ctl,
+                         const vshCmd *cmd,
+                         unsigned int flags);
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 6f8424ec14..7dd566b573 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -7004,6 +7004,8 @@ static const vshCmdOptDef opts_vcpupin[] = {
     VIRSH_COMMON_OPT_DOMAIN_FULL(0),
     {.name = "vcpu",
      .type = VSH_OT_INT,
+     .completer = virshDomainVcpuCompleter,
+     .completer_flags = 0,
      .help = N_("vcpu number")
     },
     {.name = "cpulist",
-- 
2.26.0




More information about the libvir-list mailing list