Add functionality to lock & write the custom CPU config and some other
helpers that will be needed in custom CPU models CRUD endpoints.

Original patch:
https://lore.proxmox.com/pve-devel/[email protected]/

Originally-by: Stefan Reiter <[email protected]>
Signed-off-by: Arthur Bied-Charreton <[email protected]>
---
 src/PVE/QemuServer/CPUConfig.pm | 35 ++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm
index 32ec4954..2b6665a7 100644
--- a/src/PVE/QemuServer/CPUConfig.pm
+++ b/src/PVE/QemuServer/CPUConfig.pm
@@ -6,10 +6,10 @@ use warnings;
 use JSON;
 
 use PVE::JSONSchema qw(json_bool);
-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::ProcFSTools;
 use PVE::RESTEnvironment qw(log_warn);
-use PVE::Tools qw(run_command);
+use PVE::Tools qw(run_command lock_file);
 
 use PVE::QemuServer::Helpers qw(min_version);
 
@@ -51,6 +51,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;
+    }
+}
+
 #builtin models : reported-model is mandatory
 my $builtin_models_by_arch = {
     x86_64 => {
@@ -298,6 +311,19 @@ sub get_supported_cpu_flags {
     return $supported_cpu_flags_by_arch->{$arch};
 }
 
+sub description_by_flag {
+    my ($arch) = @_;
+    return { map { $_->{name} => $_->{description} } 
get_supported_cpu_flags($arch)->@* };
+}
+
+sub get_cpu_vendor {
+    my ($cputype, $arch) = @_;
+    $arch = $host_arch if !defined($arch);
+    my $retval = $cpu_models_by_arch->{$arch}->{$cputype}
+        or die "Built-in CPU type '$cputype' does not exist";
+    return $retval;
+}
+
 my $all_supported_cpu_flags = {};
 for my $arch ($supported_cpu_flags_by_arch->%*) {
     for my $flag ($supported_cpu_flags_by_arch->{$arch}->@*) {
@@ -375,6 +401,7 @@ my $cpu_fmt = {
         optional => 1,
     },
 };
+PVE::JSONSchema::register_standard_option('pve-qemu-cpu', $cpu_fmt);
 
 my $sev_fmt = {
     type => {
@@ -564,6 +591,7 @@ sub write_config {
 
         # saved in section header
         delete $model_conf->{cputype};
+        $model_conf->{type} = $class->type();
     }
 
     $class->SUPER::write_config($filename, $cfg);
@@ -612,7 +640,8 @@ sub get_cpu_models {
 
     my $conf = load_custom_model_conf();
     for my $custom_model (keys %{ $conf->{ids} }) {
-        my $reported_model = $conf->{ids}->{$custom_model}->{'reported-model'};
+        my $model = $conf->{ids}->{$custom_model};
+        my $reported_model = $model->{'reported-model'};
         $reported_model //= $cpu_fmt->{'reported-model'}->{default};
         my $vendor = $all_cpu_models->{$reported_model};
         push @$models,
-- 
2.47.3



Reply via email to