virsh: Add vcpu list completion to guestvcpus command

Signed-off-by: Lin Ma <lma@suse.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Lin Ma 2020-11-10 17:50:52 +08:00 committed by Michal Privoznik
parent 43ee7c6db1
commit a9327362cd
3 changed files with 85 additions and 0 deletions

View File

@ -30,6 +30,7 @@
#include "virstring.h"
#include "virxml.h"
#include "virperf.h"
#include "virbitmap.h"
char **
virshDomainNameCompleter(vshControl *ctl,
@ -581,3 +582,82 @@ 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;
size_t i;
int nvcpus;
VIR_AUTOSTRINGLIST cpulist = 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 {
g_autofree char *onlineVcpuStr = NULL;
g_autofree unsigned char *vcpumap = NULL;
g_autoptr(virBitmap) vcpus = NULL;
size_t offset = 0;
int dummy;
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;
}

View File

@ -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);

View File

@ -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",