From 6c74a2e23a6c8291103f95c3b07dbddd710762db Mon Sep 17 00:00:00 2001 From: Lin Ma Date: Fri, 11 Sep 2020 15:13:12 +0800 Subject: [PATCH] virsh: Add vcpu IDs completion to vcpupin command Signed-off-by: Lin Ma Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- tools/virsh-completer-domain.c | 42 ++++++++++++++++++++++++++++++++++ tools/virsh-completer-domain.h | 4 ++++ tools/virsh-domain.c | 1 + 3 files changed, 47 insertions(+) diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c index a13e2fa779..795210972a 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 = 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; +} diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h index 91731da778..a5388d9201 100644 --- a/tools/virsh-completer-domain.h +++ b/tools/virsh-completer-domain.h @@ -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); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 1d33c51b35..01e18bdd8b 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -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",