public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Arthur Bied-Charreton <a.bied-charreton@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH pve-manager v5 15/21] api: add CRUD handlers for custom CPU models
Date: Fri, 15 May 2026 11:28:32 +0200	[thread overview]
Message-ID: <20260515092839.238064-16-a.bied-charreton@proxmox.com> (raw)
In-Reply-To: <20260515092839.238064-1-a.bied-charreton@proxmox.com>

Add CRUD endpoints for custom CPU models, allowing to interact with
/etc/pve/virtual-guest/cpu-models.conf [0].

The `custom-` prefix is not required from the consumers for the
`cputype` parameter, rather the handlers normalize it such that `foo`
evaluates to the same custom model as `custom-foo`.

[0] https://pve.proxmox.com/wiki/Manual:_cpu-models.conf

Based on & adapted from the following patches by Stefan Reiter:
https://lore.proxmox.com/pve-devel/20211028114150.3245864-4-s.reiter@proxmox.com/
https://lore.proxmox.com/pve-devel/20211028114150.3245864-5-s.reiter@proxmox.com/

Signed-off-by: Arthur Bied-Charreton <a.bied-charreton@proxmox.com>
---
 PVE/API2/Cluster/Qemu.pm                 |   8 +-
 PVE/API2/Cluster/Qemu/CustomCPUModels.pm | 204 +++++++++++++++++++++++
 PVE/API2/Cluster/Qemu/Makefile           |   5 +-
 3 files changed, 214 insertions(+), 3 deletions(-)
 create mode 100644 PVE/API2/Cluster/Qemu/CustomCPUModels.pm

diff --git a/PVE/API2/Cluster/Qemu.pm b/PVE/API2/Cluster/Qemu.pm
index f09481b2..38f59e44 100644
--- a/PVE/API2/Cluster/Qemu.pm
+++ b/PVE/API2/Cluster/Qemu.pm
@@ -3,6 +3,7 @@ package PVE::API2::Cluster::Qemu;
 use v5.36;
 
 use PVE::API2::Cluster::Qemu::CPUFlags;
+use PVE::API2::Cluster::Qemu::CustomCPUModels;
 
 use base qw(PVE::RESTHandler);
 
@@ -11,6 +12,11 @@ __PACKAGE__->register_method({
     path => "cpu-flags",
 });
 
+__PACKAGE__->register_method({
+    subclass => "PVE::API2::Cluster::Qemu::CustomCPUModels",
+    path => "custom-cpu-models",
+});
+
 __PACKAGE__->register_method({
     name => 'index',
     path => '',
@@ -33,7 +39,7 @@ __PACKAGE__->register_method({
         my ($param) = @_;
 
         return [
-            { name => 'cpu-flags' },
+            { name => 'cpu-flags' }, { name => 'custom-cpu-models' },
         ];
     },
 });
diff --git a/PVE/API2/Cluster/Qemu/CustomCPUModels.pm b/PVE/API2/Cluster/Qemu/CustomCPUModels.pm
new file mode 100644
index 00000000..e4b1523f
--- /dev/null
+++ b/PVE/API2/Cluster/Qemu/CustomCPUModels.pm
@@ -0,0 +1,204 @@
+package PVE::API2::Cluster::Qemu::CustomCPUModels;
+
+use v5.36;
+
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::RESTHandler;
+use PVE::SectionConfig;
+use PVE::Tools qw(extract_param);
+
+use PVE::QemuServer::CPUConfig;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method({
+    name => 'config',
+    path => '',
+    method => 'GET',
+    description => 'Read all custom CPU models definitions',
+    permissions => {
+        check => ['perm', '/nodes', ['Sys.Audit']],
+    },
+    parameters => {
+        additionalProperties => 0,
+    },
+    returns => {
+        type => 'array',
+        items => {
+            type => 'object',
+            properties => get_standard_option('pve-qm-custom-cpu-model'),
+        },
+        links => [{ rel => 'child', href => "{cputype}" }],
+    },
+    code => sub {
+        my $conf = PVE::QemuServer::CPUConfig::load_custom_cpu_model_config();
+        delete $conf->{order};
+        my $res = [];
+        for my $id (keys $conf->{ids}->%*) {
+            delete $conf->{ids}->{$id}->{type};
+            push @$res, $conf->{ids}->{$id};
+        }
+        return $res;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'create',
+    path => '',
+    method => 'POST',
+    protected => 1,
+    description => 'Add a custom CPU model definition',
+    permissions => {
+        check => ['perm', '/nodes', ['Sys.Modify']],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => PVE::QemuServer::CPUConfig::add_cpu_json_properties({}),
+    },
+    returns => { type => 'null' },
+    code => sub {
+        my ($param) = @_;
+
+        (my $name = $param->{cputype}) =~ s/^custom-//;
+        $param->{cputype} = "custom-$name";
+
+        PVE::QemuServer::CPUConfig::lock_custom_cpu_model_config(sub {
+            my $conf = PVE::QemuServer::CPUConfig::load_custom_cpu_model_config();
+            my $opts = PVE::QemuServer::CPUConfig->check_config($name, $param, 1, 1);
+
+            die "custom CPU model '$name' already exists\n"
+                if defined($conf->{ids}->{$name});
+
+            $conf->{ids}->{$name} = $opts;
+
+            PVE::QemuServer::CPUConfig::write_custom_cpu_model_config($conf);
+        });
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'delete',
+    path => '{cputype}',
+    method => 'DELETE',
+    protected => 1,
+    description => 'Delete a custom CPU model definition',
+    permissions => {
+        check => ['perm', '/nodes', ['Sys.Modify']],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            cputype => {
+                type => 'string',
+                description => "The custom model to delete.",
+            },
+        },
+    },
+    returns => { type => 'null' },
+    code => sub {
+        my ($param) = @_;
+
+        (my $name = $param->{cputype}) =~ s/^custom-//;
+
+        PVE::QemuServer::CPUConfig::lock_custom_cpu_model_config(sub {
+            my $conf = PVE::QemuServer::CPUConfig::load_custom_cpu_model_config();
+
+            die "custom CPU model '$name' does not exist\n"
+                if !defined($conf->{ids}->{$name});
+            delete $conf->{ids}->{$name};
+
+            PVE::QemuServer::CPUConfig::write_custom_cpu_model_config($conf);
+        });
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'update',
+    path => '{cputype}',
+    method => 'PUT',
+    protected => 1,
+    description => "Update a custom CPU model definition.",
+    permissions => {
+        check => ['perm', '/nodes', ['Sys.Modify']],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => PVE::QemuServer::CPUConfig::add_cpu_json_properties({
+            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) = @_;
+
+        my $digest = extract_param($param, 'digest');
+        my $delete = extract_param($param, 'delete');
+
+        if ($delete) {
+            $delete = [PVE::Tools::split_list($delete)];
+        }
+
+        (my $name = $param->{cputype}) =~ s/^custom-//;
+        $param->{cputype} = "custom-$name";
+
+        PVE::QemuServer::CPUConfig::lock_custom_cpu_model_config(sub {
+            my $conf = PVE::QemuServer::CPUConfig::load_custom_cpu_model_config();
+
+            PVE::SectionConfig::assert_if_modified($conf, $digest);
+
+            my $opts = PVE::QemuServer::CPUConfig->check_config($name, $param, 0, 1);
+
+            my $model = $conf->{ids}->{$name};
+            die "custom CPU model '$name' does not exist\n" if !defined($model);
+
+            my $options = PVE::QemuServer::CPUConfig->private()->{options}->{'cpu-model'};
+
+            PVE::SectionConfig::delete_from_config($model, $options, $opts, $delete);
+
+            $model->{$_} = $opts->{$_} for keys $opts->%*;
+
+            PVE::QemuServer::CPUConfig::write_custom_cpu_model_config($conf);
+        });
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'info',
+    path => '{cputype}',
+    method => 'GET',
+    description => 'Retrieve details about a specific custom CPU model',
+    permissions => {
+        check => ['perm', '/nodes', ['Sys.Audit']],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            cputype => {
+                type => 'string',
+                description => "Name of the CPU model to query.",
+            },
+        },
+    },
+    returns => {
+        type => 'object',
+        properties => PVE::QemuServer::CPUConfig::add_cpu_json_properties({
+            digest => get_standard_option('pve-config-digest'),
+        }),
+    },
+    code => sub {
+        my ($param) = @_;
+        (my $name = $param->{cputype}) =~ s/^custom-//;
+        my $conf = PVE::QemuServer::CPUConfig::load_custom_cpu_model_config();
+        my $retval = PVE::QemuServer::CPUConfig::get_custom_model($name, 0, $conf);
+        $retval->{digest} = $conf->{digest};
+        return $retval;
+    },
+});
+
+1;
diff --git a/PVE/API2/Cluster/Qemu/Makefile b/PVE/API2/Cluster/Qemu/Makefile
index 2ab3e0b1..76b661db 100644
--- a/PVE/API2/Cluster/Qemu/Makefile
+++ b/PVE/API2/Cluster/Qemu/Makefile
@@ -2,8 +2,9 @@ include ../../../../defines.mk
 
 # for node independent, cluster-wide applicable, API endpoints
 # ensure we do not conflict with files shipped by pve-cluster!!
-PERLSOURCE= 	\
-	CPUFlags.pm
+PERLSOURCE= 		\
+	CPUFlags.pm		\
+	CustomCPUModels.pm
 
 all:
 
-- 
2.47.3




  parent reply	other threads:[~2026-05-15  9:31 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15  9:28 [PATCH docs/manager/qemu-server v5 00/21] Add API and UI for custom CPU models Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-docs v5 01/21] qm: add anchor to "CPU Type" section Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 02/21] cpu config: rename CPU models config path variable Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 03/21] cpu flags: move cpu flags-related utilities to their own module Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 04/21] cpu flags: compare against JSON::true when querying supported flags Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 05/21] cpu flags: normalize CPU flags to QEMU's format Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 06/21] cpu flags: add helper querying CPU flags with nodes supporting them Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 07/21] cpu config: rename custom CPU model config loader Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 08/21] cpu config: add helpers to lock and write config Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 09/21] cpu: register standard option for CPU format Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 10/21] api: cpu flags: improve flags list returned by endpoint Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH qemu-server v5 11/21] custom cpu models: avoid redundant config load Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 12/21] cluster: reorder imports Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 13/21] cluster: makefile: reorder perl sources and align backslashes Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 14/21] api: add endpoint querying available CPU flags cluster-wide Arthur Bied-Charreton
2026-05-15  9:45   ` Arthur Bied-Charreton
2026-05-15  9:28 ` Arthur Bied-Charreton [this message]
2026-05-15  9:28 ` [PATCH pve-manager v5 16/21] ui: cpu model selector: allow filtering out custom models Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 17/21] ui: add basic custom CPU model editor Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 18/21] ui: cpu flags selector: add CPU flag editor for custom models Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 19/21] ui: cpu flags selector: allow filtering out flags supported on 0 nodes Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 20/21] ui: cpu flags selector: add search bar for large lists of flags Arthur Bied-Charreton
2026-05-15  9:28 ` [PATCH pve-manager v5 21/21] ui: group custom CPU with resource mappings Arthur Bied-Charreton
2026-05-15 17:05 ` [PATCH docs/manager/qemu-server v5 00/21] Add API and UI for custom CPU models Max R. Carrara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260515092839.238064-16-a.bied-charreton@proxmox.com \
    --to=a.bied-charreton@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal