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

From: David Judkovics <djudkovi(a)linux.ibm.com> Add new virsh command 'hypervisor-cpu-models'. Command pulls from the existing domcapabilities XML and uses xpath to parse CPU model strings. By default, only models reported as usable by the hypervisor on the host system are printed. User may specify "--all" to also print models which are not supported on the host. Signed-off-by: David Judkovics <djudkovi@linux.ibm.com> Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com> Signed-off-by: Collin Walling <walling@linux.ibm.com> --- This is a continuation of a previous series found here: https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/message/I4YK... --- docs/manpages/virsh.rst | 24 +++++++++++++ tools/virsh-host.c | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index baced15dec..48b667736d 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -1034,6 +1034,30 @@ 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] [--all] + +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. + +By default, only the models that are claimed to be "usable" by the hypervisor +on the host are reported. The option *--all* will report every CPU model known +to the hypervisor, including ones that are not supported on the hypervisor (e.g. +newer generation models). + DOMAIN COMMANDS =============== diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 9e8f542c96..2884067bbb 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1766,6 +1766,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, + .unwanted_positional = true, + .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 = "all", + .type = VSH_OT_BOOL, + .help = N_("include all CPU models known to the hypervisor for the architecture") + }, + {.name = NULL} +}; + +static bool +cmdHypervisorCPUModelNames(vshControl *ctl, + const vshCmd *cmd) +{ + g_autofree char *caps_xml = NULL; + const char *virttype = NULL; + const char *emulator = NULL; + const char *arch = NULL; + const char *machine = NULL; + const char *xpath_all = "//cpu//model[@usable]/text()"; + const char *xpath_usable = "//cpu//model[@usable='yes']/text()"; + virshControl *priv = ctl->privData; + + 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; + + caps_xml = virConnectGetDomainCapabilities(priv->conn, emulator, arch, + machine, virttype, 0); + + if (!caps_xml) { + vshError(ctl, "%s", _("failed to get hypervisor CPU model names")); + return false; + } + + return virshDumpXML(ctl, caps_xml, "domcapabilities", + vshCommandOptBool(cmd, "all") ? xpath_all : xpath_usable, + false); +} + + const vshCmdDef hostAndHypervisorCmds[] = { {.name = "allocpages", .handler = cmdAllocpages, @@ -1833,6 +1901,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.1

On 3/6/25 2:07 PM, Collin Walling wrote: Ping and adding Daniel et al on CC :)
From: David Judkovics <djudkovi(a)linux.ibm.com>
Add new virsh command 'hypervisor-cpu-models'. Command pulls from the existing domcapabilities XML and uses xpath to parse CPU model strings. By default, only models reported as usable by the hypervisor on the host system are printed. User may specify "--all" to also print models which are not supported on the host.
Signed-off-by: David Judkovics <djudkovi@linux.ibm.com> Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com> Signed-off-by: Collin Walling <walling@linux.ibm.com> ---
This is a continuation of a previous series found here:
https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/message/I4YK...
--- docs/manpages/virsh.rst | 24 +++++++++++++ tools/virsh-host.c | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+)
diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index baced15dec..48b667736d 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -1034,6 +1034,30 @@ 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] [--all] + +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. + +By default, only the models that are claimed to be "usable" by the hypervisor +on the host are reported. The option *--all* will report every CPU model known +to the hypervisor, including ones that are not supported on the hypervisor (e.g. +newer generation models). + DOMAIN COMMANDS ===============
diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 9e8f542c96..2884067bbb 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1766,6 +1766,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, + .unwanted_positional = true, + .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 = "all", + .type = VSH_OT_BOOL, + .help = N_("include all CPU models known to the hypervisor for the architecture") + }, + {.name = NULL} +}; + +static bool +cmdHypervisorCPUModelNames(vshControl *ctl, + const vshCmd *cmd) +{ + g_autofree char *caps_xml = NULL; + const char *virttype = NULL; + const char *emulator = NULL; + const char *arch = NULL; + const char *machine = NULL; + const char *xpath_all = "//cpu//model[@usable]/text()"; + const char *xpath_usable = "//cpu//model[@usable='yes']/text()"; + virshControl *priv = ctl->privData; + + 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; + + caps_xml = virConnectGetDomainCapabilities(priv->conn, emulator, arch, + machine, virttype, 0); + + if (!caps_xml) { + vshError(ctl, "%s", _("failed to get hypervisor CPU model names")); + return false; + } + + return virshDumpXML(ctl, caps_xml, "domcapabilities", + vshCommandOptBool(cmd, "all") ? xpath_all : xpath_usable, + false); +} + + const vshCmdDef hostAndHypervisorCmds[] = { {.name = "allocpages", .handler = cmdAllocpages, @@ -1833,6 +1901,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,
-- Regards, Collin

On a Thursday in 2025, Collin Walling wrote:
From: David Judkovics <djudkovi(a)linux.ibm.com>
Add new virsh command 'hypervisor-cpu-models'. Command pulls from the existing domcapabilities XML and uses xpath to parse CPU model strings. By default, only models reported as usable by the hypervisor on the host system are printed. User may specify "--all" to also print models which are not supported on the host.
Signed-off-by: David Judkovics <djudkovi@linux.ibm.com> Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com> Signed-off-by: Collin Walling <walling@linux.ibm.com> ---
This is a continuation of a previous series found here:
https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/message/I4YK...
--- docs/manpages/virsh.rst | 24 +++++++++++++ tools/virsh-host.c | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+)
diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index baced15dec..48b667736d 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -1034,6 +1034,30 @@ 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] [--all] +
The actualy syntax for this (as reported by virsh hypervisor-cpu-models --help) is: hypervisor-cpu-models [--virttype <string>] [--emulator <string>] [--arch <string>] [--machine <string>] [--all] i.e. specifying the option name is mandatory
+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. + +By default, only the models that are claimed to be "usable" by the hypervisor +on the host are reported. The option *--all* will report every CPU model known +to the hypervisor, including ones that are not supported on the hypervisor (e.g. +newer generation models). + DOMAIN COMMANDS ===============
diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 9e8f542c96..2884067bbb 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1766,6 +1766,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, + .unwanted_positional = true,
The use of this option is discouraged, see: commit a67f737ddfc59b0f5b3905bd1c0935ced82de3d0 Author: Peter Krempa <pkrempa@redhat.com> AuthorDate: 2024-03-14 17:17:19 +0100 Commit: Peter Krempa <pkrempa@redhat.com> CommitDate: 2024-04-02 14:24:30 +0200 virt-admin: Annodate 'unwanted_positional' arguments https://gitlab.com/libvirt/libvirt/-/commit/a67f737ddfc59b0f5b3905bd1c0935ce...
+ .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 = "all", + .type = VSH_OT_BOOL, + .help = N_("include all CPU models known to the hypervisor for the architecture") + }, + {.name = NULL} +}; + +static bool +cmdHypervisorCPUModelNames(vshControl *ctl, + const vshCmd *cmd) +{ + g_autofree char *caps_xml = NULL; + const char *virttype = NULL; + const char *emulator = NULL; + const char *arch = NULL; + const char *machine = NULL; + const char *xpath_all = "//cpu//model[@usable]/text()"; + const char *xpath_usable = "//cpu//model[@usable='yes']/text()"; + virshControl *priv = ctl->privData; +
if (vshCommandOptBool(cmd, "all")) xpath = "//cpu//model[@usable]/text()"; else xpath = "//cpu//model[@usable='yes']/text()"; gets rid of the ternary operator and groups the command line processing together.
+ 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; + + caps_xml = virConnectGetDomainCapabilities(priv->conn, emulator, arch, + machine, virttype, 0); + + if (!caps_xml) { + vshError(ctl, "%s", _("failed to get hypervisor CPU model names")); + return false; + } + + return virshDumpXML(ctl, caps_xml, "domcapabilities", + vshCommandOptBool(cmd, "all") ? xpath_all : xpath_usable, + false); +} + +
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

On Tue, Mar 18, 2025 at 18:13:57 +0100, Ján Tomko via Devel wrote:
On a Thursday in 2025, Collin Walling wrote:
From: David Judkovics <djudkovi(a)linux.ibm.com>
Add new virsh command 'hypervisor-cpu-models'. Command pulls from the existing domcapabilities XML and uses xpath to parse CPU model strings. By default, only models reported as usable by the hypervisor on the host system are printed. User may specify "--all" to also print models which are not supported on the host.
Signed-off-by: David Judkovics <djudkovi@linux.ibm.com> Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com> Signed-off-by: Collin Walling <walling@linux.ibm.com> ---
[...]
diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 9e8f542c96..2884067bbb 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1766,6 +1766,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, + .unwanted_positional = true,
The use of this option is discouraged, see:
I'd say forbidden. The idea of the flag was that no new code would ever use it. In fact in waranted cases, where the options were conflicting or new options were added in between existing options I even changed old code to require the options instead of filling them to unwanted fields.
commit a67f737ddfc59b0f5b3905bd1c0935ced82de3d0 Author: Peter Krempa <pkrempa@redhat.com> AuthorDate: 2024-03-14 17:17:19 +0100 Commit: Peter Krempa <pkrempa@redhat.com> CommitDate: 2024-04-02 14:24:30 +0200
virt-admin: Annodate 'unwanted_positional' arguments
https://gitlab.com/libvirt/libvirt/-/commit/a67f737ddfc59b0f5b3905bd1c0935ce...
Commits 348010ac937fd9a71d81cd3d4154dd46bdbb6a87 and a7b10919e7d371b9e76ccec2956ba74773de6943 illustrate the problem further: Specifically some of the arguments would be handled differently regardless of documentation based on position. In case of some other commands new options which were parsed this way were added before pre-existing options thus would break how the arguments were filled. We do not want that to happen again. Commands now can have at most one positional optional option, any further optional argument must use explicit option string.

On 3/19/25 4:15 AM, Peter Krempa via Devel wrote:
On Tue, Mar 18, 2025 at 18:13:57 +0100, Ján Tomko via Devel wrote:
On a Thursday in 2025, Collin Walling wrote:
From: David Judkovics <djudkovi(a)linux.ibm.com>
Add new virsh command 'hypervisor-cpu-models'. Command pulls from the existing domcapabilities XML and uses xpath to parse CPU model strings. By default, only models reported as usable by the hypervisor on the host system are printed. User may specify "--all" to also print models which are not supported on the host.
Signed-off-by: David Judkovics <djudkovi@linux.ibm.com> Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com> Signed-off-by: Collin Walling <walling@linux.ibm.com> ---
[...]
diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 9e8f542c96..2884067bbb 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1766,6 +1766,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, + .unwanted_positional = true,
The use of this option is discouraged, see:
I'd say forbidden. The idea of the flag was that no new code would ever use it.
In fact in waranted cases, where the options were conflicting or new options were added in between existing options I even changed old code to require the options instead of filling them to unwanted fields.
commit a67f737ddfc59b0f5b3905bd1c0935ced82de3d0 Author: Peter Krempa <pkrempa@redhat.com> AuthorDate: 2024-03-14 17:17:19 +0100 Commit: Peter Krempa <pkrempa@redhat.com> CommitDate: 2024-04-02 14:24:30 +0200
virt-admin: Annodate 'unwanted_positional' arguments
https://gitlab.com/libvirt/libvirt/-/commit/a67f737ddfc59b0f5b3905bd1c0935ce...
Commits 348010ac937fd9a71d81cd3d4154dd46bdbb6a87 and a7b10919e7d371b9e76ccec2956ba74773de6943 illustrate the problem further:
Specifically some of the arguments would be handled differently regardless of documentation based on position.
In case of some other commands new options which were parsed this way were added before pre-existing options thus would break how the arguments were filled.
We do not want that to happen again.
Commands now can have at most one positional optional option, any further optional argument must use explicit option string.
Thanks for the detailed explanation. I merely copied a the emulator parameter from the other virsh hypervisor-cpu-* commands, but did not understand the significance of this option. I will omit it in v2. -- Regards, Collin

On 3/18/25 1:13 PM, Ján Tomko wrote:
On a Thursday in 2025, Collin Walling wrote:
From: David Judkovics <djudkovi(a)linux.ibm.com>
Add new virsh command 'hypervisor-cpu-models'. Command pulls from the existing domcapabilities XML and uses xpath to parse CPU model strings. By default, only models reported as usable by the hypervisor on the host system are printed. User may specify "--all" to also print models which are not supported on the host.
Signed-off-by: David Judkovics <djudkovi@linux.ibm.com> Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com> Signed-off-by: Collin Walling <walling@linux.ibm.com> ---
This is a continuation of a previous series found here:
https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/message/I4YK...
--- docs/manpages/virsh.rst | 24 +++++++++++++ tools/virsh-host.c | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+)
diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index baced15dec..48b667736d 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -1034,6 +1034,30 @@ 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] [--all] +
The actualy syntax for this (as reported by virsh hypervisor-cpu-models --help) is:
hypervisor-cpu-models [--virttype <string>] [--emulator <string>] [--arch <string>] [--machine <string>] [--all]
i.e. specifying the option name is mandatory
Okay, I'll change the doc in virsh.rst to: hypervisor-cpu-models [--virttype virttype] [--emulator emulator] [--arch arch] [--machine machine] [--all]
+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. + +By default, only the models that are claimed to be "usable" by the hypervisor +on the host are reported. The option *--all* will report every CPU model known +to the hypervisor, including ones that are not supported on the hypervisor (e.g. +newer generation models). + DOMAIN COMMANDS ===============
diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 9e8f542c96..2884067bbb 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1766,6 +1766,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, + .unwanted_positional = true,
The use of this option is discouraged, see:
commit a67f737ddfc59b0f5b3905bd1c0935ced82de3d0 Author: Peter Krempa <pkrempa@redhat.com> AuthorDate: 2024-03-14 17:17:19 +0100 Commit: Peter Krempa <pkrempa@redhat.com> CommitDate: 2024-04-02 14:24:30 +0200
virt-admin: Annodate 'unwanted_positional' arguments
https://gitlab.com/libvirt/libvirt/-/commit/a67f737ddfc59b0f5b3905bd1c0935ce...
I'll remove it.
+ .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 = "all", + .type = VSH_OT_BOOL, + .help = N_("include all CPU models known to the hypervisor for the architecture") + }, + {.name = NULL} +}; + +static bool +cmdHypervisorCPUModelNames(vshControl *ctl, + const vshCmd *cmd) +{ + g_autofree char *caps_xml = NULL; + const char *virttype = NULL; + const char *emulator = NULL; + const char *arch = NULL; + const char *machine = NULL; + const char *xpath_all = "//cpu//model[@usable]/text()"; + const char *xpath_usable = "//cpu//model[@usable='yes']/text()"; + virshControl *priv = ctl->privData; +
if (vshCommandOptBool(cmd, "all")) xpath = "//cpu//model[@usable]/text()"; else xpath = "//cpu//model[@usable='yes']/text()";
gets rid of the ternary operator and groups the command line processing together.
Sure.
+ 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; + + caps_xml = virConnectGetDomainCapabilities(priv->conn, emulator, arch, + machine, virttype, 0); + + if (!caps_xml) { + vshError(ctl, "%s", _("failed to get hypervisor CPU model names")); + return false; + } + + return virshDumpXML(ctl, caps_xml, "domcapabilities", + vshCommandOptBool(cmd, "all") ? xpath_all : xpath_usable, + false); +} + +
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Jano
Thanks. I'll post a v2 with the changes. -- Regards, Collin
participants (3)
-
Collin Walling
-
Ján Tomko
-
Peter Krempa