]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Add vcpu IDs completion to vcpupin command
authorLin Ma <morecache@gmail.com>
Fri, 11 Sep 2020 07:13:12 +0000 (15:13 +0800)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 14 Sep 2020 13:52:40 +0000 (15:52 +0200)
Signed-off-by: Lin Ma <lma@suse.de>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tools/virsh-completer-domain.c
tools/virsh-completer-domain.h
tools/virsh-domain.c

index a13e2fa7794e56739bce03e664e0ea870ec3540d..795210972a3aa200f695515d8bc6d08ec71d97cf 100644 (file)
@@ -476,3 +476,45 @@ virshDomainIOThreadIdCompleter(vshControl *ctl,
     virshDomainFree(dom);
     return ret;
 }
+
+
+char **
+virshDomainVcpuCompleter(vshControl *ctl,
+                         const vshCmd *cmd,
+                         unsigned int flags)
+{
+    virDomainPtr dom = NULL;
+    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;
+}
index 91731da778b78c6b296610cb17e36305838c8085..a5388d92011268cf6ff6602fc51f4d05e406d9b0 100644 (file)
@@ -78,3 +78,7 @@ char ** virshDomainUUIDCompleter(vshControl *ctl,
 char ** virshDomainIOThreadIdCompleter(vshControl *ctl,
                                        const vshCmd *cmd,
                                        unsigned int flags);
+
+char ** virshDomainVcpuCompleter(vshControl *ctl,
+                                 const vshCmd *cmd,
+                                 unsigned int flags);
index 1d33c51b35d9386ecf2624f70836272284034135..01e18bdd8b08cc4f9f29d3ba4da5581a5e1820fd 100644 (file)
@@ -7002,6 +7002,7 @@ static const vshCmdOptDef opts_vcpupin[] = {
     VIRSH_COMMON_OPT_DOMAIN_FULL(0),
     {.name = "vcpu",
      .type = VSH_OT_INT,
+     .completer = virshDomainVcpuCompleter,
      .help = N_("vcpu number")
     },
     {.name = "cpulist",