[PATCH 4/4] virsh: Introduce new hypervisor-cpu-models command

Boris Fiuczynski posted 4 patches 5 days, 17 hours ago
[PATCH 4/4] virsh: Introduce new hypervisor-cpu-models command
Posted by Boris Fiuczynski 5 days, 17 hours ago
From: David Judkovics <djudkovi@linux.ibm.com>

Add new virsh 'host' group command 'hypervisor-cpu-models' to virsh.

Signed-off-by: David Judkovics <djudkovi@linux.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
---
 docs/manpages/virsh.rst | 20 +++++++++++
 tools/virsh-host.c      | 74 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+)

diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
index e801037c04..0f165944eb 100644
--- a/docs/manpages/virsh.rst
+++ b/docs/manpages/virsh.rst
@@ -1023,6 +1023,26 @@ listed in the XML description. If *--migratable* is specified, features that
 block migration will not be included in the resulting CPU.
 
 
+hypervisor-cpu-models
+---------------------
+
+**Syntax:**
+
+::
+
+   hypervisor-cpu-models [virttype] [emulator] [arch] [machine]
+
+Print the list of CPU models known by the hypervisor for the specified architecture.
+It is not guaranteed that a listed CPU will run on the host. To determine CPU
+model compatibility with the host, see ``virsh hypervisor-cpu-baseline`` and
+``virsh hypervisor-cpu-compare``.
+
+The *virttype* option specifies the virtualization type (usable in the 'type'
+attribute of the <domain> top level element from the domain XML). *emulator*
+specifies the path to the emulator, *arch* specifies the CPU architecture, and
+*machine* specifies the machine type.
+
+
 DOMAIN COMMANDS
 ===============
 
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index f4e7324f42..7d52f22baa 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -1758,6 +1758,74 @@ cmdHypervisorCPUBaseline(vshControl *ctl,
 }
 
 
+/*
+ * "hypervisor-cpu-models" command
+ */
+static const vshCmdInfo info_hypervisor_cpu_models = {
+    .help = N_("Hypervisor reported CPU models"),
+    .desc = N_("Get the CPU models reported by the hypervisor."),
+};
+
+static const vshCmdOptDef opts_hypervisor_cpu_models[] = {
+    {.name = "virttype",
+     .type = VSH_OT_STRING,
+     .completer = virshDomainVirtTypeCompleter,
+     .help = N_("virtualization type (/domain/@type)"),
+    },
+    {.name = "emulator",
+     .type = VSH_OT_STRING,
+     .help = N_("path to emulator binary (/domain/devices/emulator)"),
+    },
+    {.name = "arch",
+     .type = VSH_OT_STRING,
+     .completer = virshArchCompleter,
+     .help = N_("CPU architecture (/domain/os/type/@arch)"),
+    },
+    {.name = "machine",
+     .type = VSH_OT_STRING,
+     .help = N_("machine type (/domain/os/type/@machine)"),
+    },
+    {.name = NULL}
+};
+
+static bool
+cmdHypervisorCPUModelNames(vshControl *ctl,
+                           const vshCmd *cmd)
+{
+    const char *virttype = NULL;
+    char **models = NULL;
+    const char *emulator = NULL;
+    const char *arch = NULL;
+    const char *machine = NULL;
+    virshControl *priv = ctl->privData;
+    size_t i;
+    int nmodels;
+
+    if (vshCommandOptString(ctl, cmd, "virttype", &virttype) < 0 ||
+        vshCommandOptString(ctl, cmd, "emulator", &emulator) < 0 ||
+        vshCommandOptString(ctl, cmd, "arch", &arch) < 0 ||
+        vshCommandOptString(ctl, cmd, "machine", &machine) < 0)
+        return false;
+
+    nmodels = virConnectGetHypervisorCPUModelNames(priv->conn, emulator, arch,
+                                                   machine, virttype, &models, 0);
+
+    if (nmodels < 0) {
+        vshError(ctl, "%s", _("failed to get Hypervisor CPU model names"));
+        return false;
+    }
+
+    for (i = 0; i < nmodels; i++) {
+            vshPrint(ctl, "%s\n", models[i]);
+            VIR_FREE(models[i]);
+    }
+
+    VIR_FREE(models);
+
+    return true;
+}
+
+
 const vshCmdDef hostAndHypervisorCmds[] = {
     {.name = "allocpages",
      .handler = cmdAllocpages,
@@ -1825,6 +1893,12 @@ const vshCmdDef hostAndHypervisorCmds[] = {
      .info = &info_hypervisor_cpu_compare,
      .flags = 0
     },
+    {.name = "hypervisor-cpu-models",
+     .handler = cmdHypervisorCPUModelNames,
+     .opts = opts_hypervisor_cpu_models,
+     .info = &info_hypervisor_cpu_models,
+     .flags = 0
+    },
     {.name = "maxvcpus",
      .handler = cmdMaxvcpus,
      .opts = opts_maxvcpus,
-- 
2.47.0