From ce8d025be86cd42dc2f5388837981b9f690035b8 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Thu, 6 Oct 2022 17:12:24 +0200 Subject: [PATCH] virsh: Add completer for hypervisor-cpu-baseline --model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jiri Denemark Reviewed-by: Ján Tomko --- tools/virsh-completer-host.c | 50 ++++++++++++++++++++++++++++++++++++ tools/virsh-completer-host.h | 5 ++++ tools/virsh-host.c | 1 + 3 files changed, 56 insertions(+) diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c index 05a84a43bb..93b633eb64 100644 --- a/tools/virsh-completer-host.c +++ b/tools/virsh-completer-host.c @@ -205,3 +205,53 @@ virshArchCompleter(vshControl *ctl G_GNUC_UNUSED, return virshEnumComplete(VIR_ARCH_LAST, (const char *(*)(int))virArchToString); } + + +char ** +virshCPUModelCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControl *priv = ctl->privData; + const char *virttype = NULL; + const char *emulator = NULL; + const char *arch = NULL; + const char *machine = NULL; + g_autofree char *domcaps = NULL; + g_autoptr(xmlDoc) xml = NULL; + g_autoptr(xmlXPathContext) ctxt = NULL; + g_autofree xmlNodePtr *nodes = NULL; + g_auto(GStrv) models = NULL; + int nmodels = 0; + size_t i; + + virCheckFlags(0, NULL); + + if (vshCommandOptStringReq(ctl, cmd, "virttype", &virttype) < 0 || + vshCommandOptStringReq(ctl, cmd, "emulator", &emulator) < 0 || + vshCommandOptStringReq(ctl, cmd, "arch", &arch) < 0 || + vshCommandOptStringReq(ctl, cmd, "machine", &machine) < 0) + return NULL; + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(domcaps = virConnectGetDomainCapabilities(priv->conn, emulator, arch, + machine, virttype, 0))) + return NULL; + + if (!(xml = virXMLParseStringCtxt(domcaps, _("domain capabilities"), &ctxt))) + return NULL; + + nmodels = virXPathNodeSet("/domainCapabilities/cpu/mode[@name='custom']/model", + ctxt, &nodes); + if (nmodels <= 0) + return NULL; + + models = g_new0(char *, nmodels + 1); + + for (i = 0; i < nmodels; i++) + models[i] = virXMLNodeContentString(nodes[i]); + + return g_steal_pointer(&models); +} diff --git a/tools/virsh-completer-host.h b/tools/virsh-completer-host.h index b182661cde..608ae9e3e7 100644 --- a/tools/virsh-completer-host.h +++ b/tools/virsh-completer-host.h @@ -51,3 +51,8 @@ char ** virshArchCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char ** +virshCPUModelCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 16c3585a1a..21d479fd01 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1719,6 +1719,7 @@ static const vshCmdOptDef opts_hypervisor_cpu_baseline[] = { }, {.name = "model", .type = VSH_OT_STRING, + .completer = virshCPUModelCompleter, .help = N_("Shortcut for calling the command with a single CPU model " "and no additional features") },