[pve-devel] [RFC qemu-server 4/9] api: add /cpu/model/* get/create/delete/update endpoints
Stefan Reiter
s.reiter at proxmox.com
Thu Oct 28 13:41:45 CEST 2021
Standard API, loosely based on Storage config API code. Uses digests and
'delete'-parameter (parameters not given to an update call are
untouched).
Locking and writing helpers are added to CPUConfig. write_config is
fixed with adding 'type' to every section, otherwise SectionConfig
fails.
Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---
PVE/API2/Qemu/CPU.pm | 174 +++++++++++++++++++++++++++++++++++-
PVE/QemuServer/CPUConfig.pm | 17 +++-
2 files changed, 189 insertions(+), 2 deletions(-)
diff --git a/PVE/API2/Qemu/CPU.pm b/PVE/API2/Qemu/CPU.pm
index 9cc1d77..8a3dcfd 100644
--- a/PVE/API2/Qemu/CPU.pm
+++ b/PVE/API2/Qemu/CPU.pm
@@ -8,7 +8,8 @@ use PVE::JSONSchema qw(get_standard_option);
use PVE::INotify;
use PVE::QemuServer;
use PVE::QemuServer::CPUConfig;
-use PVE::Tools qw(run_command get_host_arch);
+use PVE::SectionConfig;
+use PVE::Tools qw(run_command get_host_arch extract_param);
use base qw(PVE::RESTHandler);
@@ -218,4 +219,175 @@ __PACKAGE__->register_method({
return $retval;
}});
+__PACKAGE__->register_method({
+ name => 'config',
+ path => 'model',
+ method => 'GET',
+ description => "Read all custom CPU model definitions.",
+ permissions => {
+ check => ['perm', '/nodes', ['Sys.Audit']],
+ },
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ node => get_standard_option('pve-node'),
+ },
+ },
+ returns => { type => 'object' },
+ code => sub {
+ my $conf = PVE::QemuServer::CPUConfig::load_custom_model_conf();
+ delete $conf->{order};
+ my $ids = [];
+ foreach my $id (keys %{$conf->{ids}}) {
+ delete $conf->{ids}->{$id}->{type};
+ push @$ids, $conf->{ids}->{$id};
+ }
+ $conf->{ids} = $ids;
+ return $conf;
+ }});
+
+__PACKAGE__->register_method({
+ name => 'create',
+ path => 'model',
+ method => 'POST',
+ description => "Add a custom CPU model definition.",
+ permissions => {
+ check => ['perm', '/nodes', ['Sys.Console']],
+ },
+ parameters => {
+ additionalProperties => 0,
+ properties => PVE::QemuServer::CPUConfig::add_cpu_json_properties({
+ digest => get_standard_option('pve-config-digest'),
+ node => get_standard_option('pve-node'),
+ }),
+ },
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+ delete $param->{node};
+
+ my $digest = extract_param($param, 'digest');
+
+ my $name = $param->{'cputype'};
+ die "custom cpu models 'cputype' must start with 'custom-' prefix\n"
+ if $name !~ m/^custom-/;
+ (my $name_no_prefix = $name) =~ s/^custom-//;
+
+ PVE::QemuServer::CPUConfig::lock_cpu_config(sub {
+ my $conf = PVE::QemuServer::CPUConfig::load_custom_model_conf();
+ PVE::SectionConfig::assert_if_modified($conf, $digest);
+
+ die "custom cpu model '$name' already exists\n"
+ if defined($conf->{ids}->{$name_no_prefix});
+ $conf->{ids}->{$name_no_prefix} = $param;
+
+ PVE::QemuServer::CPUConfig::write_custom_model_conf($conf);
+ });
+ }});
+
+__PACKAGE__->register_method({
+ name => 'delete',
+ path => 'model/{cputype}',
+ method => 'DELETE',
+ description => "Delete a custom CPU model definition.",
+ permissions => {
+ check => ['perm', '/nodes', ['Sys.Console']],
+ },
+ parameters => {
+ additionalProperties => 0,
+ properties => {
+ node => get_standard_option('pve-node'),
+ digest => get_standard_option('pve-config-digest'),
+ cputype => {
+ type => 'string',
+ format_description => 'string',
+ description => "The custom model to delete. Must be prefixed"
+ . " with 'custom-'.",
+ },
+ },
+ },
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+
+ my $digest = extract_param($param, 'digest');
+
+ my $name = $param->{'cputype'};
+ die "cputype must start with 'custom-' prefix\n"
+ if $name !~ m/^custom-/;
+ (my $name_no_prefix = $name) =~ s/^custom-//;
+
+ PVE::QemuServer::CPUConfig::lock_cpu_config(sub {
+ my $conf = PVE::QemuServer::CPUConfig::load_custom_model_conf();
+ PVE::SectionConfig::assert_if_modified($conf, $digest);
+
+ die "custom cpu model '$name' doesn't exist\n"
+ if !defined($conf->{ids}->{$name_no_prefix});
+ delete $conf->{ids}->{$name_no_prefix};
+
+ PVE::QemuServer::CPUConfig::write_custom_model_conf($conf);
+ });
+ }});
+
+__PACKAGE__->register_method({
+ name => 'update',
+ path => 'model/{cputype}',
+ method => 'PUT',
+ description => "Update a custom CPU model definition.",
+ permissions => {
+ check => ['perm', '/nodes', ['Sys.Console']],
+ },
+ parameters => {
+ additionalProperties => 0,
+ properties => PVE::QemuServer::CPUConfig::add_cpu_json_properties({
+ node => get_standard_option('pve-node'),
+ digest => get_standard_option('pve-config-digest'),
+ delete => {
+ type => 'string',
+ format => 'pve-configid-list',
+ description => "A list of properties to delete.",
+ optional => 1,
+ },
+ }),
+ },
+ returns => { type => 'null' },
+ code => sub {
+ my ($param) = @_;
+ delete $param->{node};
+
+ my $digest = extract_param($param, 'digest');
+ my $delete = extract_param($param, 'delete') // '';
+ my %delete_hash = map { $_ => 1 } PVE::Tools::split_list($delete);
+
+ my $name = $param->{'cputype'};
+ die "custom cpu models 'cputype' must start with 'custom-' prefix\n"
+ if $name !~ m/^custom-/;
+ (my $name_no_prefix = $name) =~ s/^custom-//;
+
+ PVE::QemuServer::CPUConfig::lock_cpu_config(sub {
+ my $conf = PVE::QemuServer::CPUConfig::load_custom_model_conf();
+
+ PVE::SectionConfig::assert_if_modified($conf, $digest);
+
+ my $model = $conf->{ids}->{$name_no_prefix};
+ die "custom cpu model '$name' doesn't exist\n"
+ if !defined($model);
+
+ my $props = PVE::QemuServer::CPUConfig::add_cpu_json_properties({});
+ for my $p (keys %$props) {
+ if ($delete_hash{$p}) {
+ die "cannot delete 'cputype' property\n"
+ if $p eq 'cputype';
+ die "cannot set and delete property '$p' at once\n"
+ if $param->{$p};
+ delete $model->{$p};
+ } elsif (defined($param->{$p})) {
+ $model->{$p} = $param->{$p};
+ }
+ }
+
+ PVE::QemuServer::CPUConfig::write_custom_model_conf($conf);
+ });
+ }});
+
1;
diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index 9be8022..08a174f 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -4,7 +4,7 @@ use strict;
use warnings;
use PVE::JSONSchema;
-use PVE::Cluster qw(cfs_register_file cfs_read_file);
+use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
use PVE::QemuServer::Helpers qw(min_version);
use base qw(PVE::SectionConfig Exporter);
@@ -29,6 +29,19 @@ sub load_custom_model_conf {
return cfs_read_file($default_filename);
}
+sub write_custom_model_conf {
+ my ($conf) = @_;
+ cfs_write_file($default_filename, $conf);
+}
+
+sub lock_cpu_config {
+ my ($code) = @_;
+ cfs_lock_file($default_filename, undef, $code);
+ if (my $err = $@) {
+ die $err;
+ }
+}
+
my $cpu_vendor_list = {
# Intel CPUs
486 => 'GenuineIntel',
@@ -270,6 +283,8 @@ sub write_config {
# saved in section header
delete $model_conf->{cputype};
+
+ $model_conf->{type} = $class->type();
}
$class->SUPER::write_config($filename, $cfg);
--
2.30.2
More information about the pve-devel
mailing list