[pve-devel] [PATCH qemu-server 2/6] api: allow listing custom and default CPU models

Stefan Reiter s.reiter at proxmox.com
Wed Apr 22 15:39:06 CEST 2020


More API calls will follow for this path, for now add the 'index' call to
list all custom and default CPU models.

Any user can list the default CPU models, as these are public anyway, but
custom models are restricted to users with Sys.Audit on /nodes.

Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---
 PVE/API2/Qemu/CPU.pm        | 61 +++++++++++++++++++++++++++++++++++++
 PVE/API2/Qemu/Makefile      |  2 +-
 PVE/QemuServer/CPUConfig.pm | 30 ++++++++++++++++++
 3 files changed, 92 insertions(+), 1 deletion(-)
 create mode 100644 PVE/API2/Qemu/CPU.pm

diff --git a/PVE/API2/Qemu/CPU.pm b/PVE/API2/Qemu/CPU.pm
new file mode 100644
index 0000000..b0bb32d
--- /dev/null
+++ b/PVE/API2/Qemu/CPU.pm
@@ -0,0 +1,61 @@
+package PVE::API2::Qemu::CPU;
+
+use strict;
+use warnings;
+
+use PVE::RESTHandler;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::QemuServer::CPUConfig;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    description => 'List all custom and default CPU models.',
+    permissions => {
+	user => 'all',
+	description => 'Only returns custom models when the current user has'
+		     . ' Sys.Audit on /nodes.',
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => 'object',
+	    properties => {
+		name => {
+		    type => 'string',
+		    description => "Name of the CPU model. Identifies it for"
+				 . " subsequent API calls. Prefixed with"
+				 . " 'custom-' for custom models.",
+		},
+		custom => {
+		    type => 'boolean',
+		    description => "True if this is a custom CPU model.",
+		},
+		vendor => {
+		    type => 'string',
+		    description => "CPU vendor visible to the guest when this"
+				 . " model is selected. Vendor of"
+				 . " 'reported-model' in case of custom models.",
+		},
+	    },
+	},
+	links => [ { rel => 'child', href => '{name}' } ],
+    },
+    code => sub {
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $authuser = $rpcenv->get_user();
+	my $include_custom = $rpcenv->check($authuser, "/nodes", ['Sys.Audit'], 1);
+
+	return PVE::QemuServer::CPUConfig::get_cpu_models($include_custom);
+    }});
+
+1;
diff --git a/PVE/API2/Qemu/Makefile b/PVE/API2/Qemu/Makefile
index 20c2a6c..f4b7be6 100644
--- a/PVE/API2/Qemu/Makefile
+++ b/PVE/API2/Qemu/Makefile
@@ -1,4 +1,4 @@
-SOURCES=Agent.pm
+SOURCES=Agent.pm CPU.pm
 
 .PHONY: install
 install:
diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index 61744dc..b884498 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -293,6 +293,36 @@ sub write_config {
     $class->SUPER::write_config($filename, $cfg);
 }
 
+sub get_cpu_models {
+    my ($include_custom) = @_;
+
+    my $models = [];
+
+    for my $default_model (keys %{$cpu_vendor_list}) {
+	push @$models, {
+	    name => $default_model,
+	    custom => 0,
+	    vendor => $cpu_vendor_list->{$default_model},
+	};
+    }
+
+    return $models if !$include_custom;
+
+    my $conf = load_custom_model_conf();
+    for my $custom_model (keys %{$conf->{ids}}) {
+	my $reported_model = $conf->{ids}->{$custom_model}->{'reported-model'};
+	$reported_model //= $cpu_fmt->{'reported-model'}->{default};
+	my $vendor = $cpu_vendor_list->{$reported_model};
+	push @$models, {
+	    name => "custom-$custom_model",
+	    custom => 1,
+	    vendor => $vendor,
+	};
+    }
+
+    return $models;
+}
+
 sub is_custom_model {
     my ($cputype) = @_;
     return $cputype =~ m/^custom-/;
-- 
2.26.2





More information about the pve-devel mailing list