Custom CPU types can be specified via the API, but to prevent arbitrary
ones we have to manually check if the given model exists (as default or
custom).

Signed-off-by: Stefan Reiter <s.rei...@proxmox.com>
---
 PVE/QemuServer.pm | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 7cc1674..5bdd729 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -187,7 +187,8 @@ my $cpu_fmt = {
     cputype => {
        description => "Emulated CPU type.",
        type => 'string',
-       enum => [ sort { "\L$a" cmp "\L$b" } keys %$cpu_vendor_list ],
+       format_description => 'string',
+       pattern => qr/[0-9a-zA-Z\-_]+/,
        default => 'kvm64',
        default_key => 1,
     },
@@ -215,6 +216,32 @@ my $cpu_fmt = {
     },
 };
 
+# we need to verify the "cpu" property of the VM config manually, since a
+# syntactially valid, but non-existant CPU model might be given
+PVE::JSONSchema::register_format('pve-cpu-conf', \&verify_cpu);
+sub verify_cpu {
+    my ($cpu, $noerr) = @_;
+
+    my $conf;
+    eval {
+       $conf = PVE::JSONSchema::parse_property_string($cpu_fmt, $cpu);
+    };
+    if ($@) {
+       die $@ if !$noerr;
+       return undef;
+    }
+
+    my $cputype = $conf->{cputype};
+
+    return $cpu if defined($cpu_vendor_list->{$cputype});
+
+    my $cpu_models = PVE::QemuServer::CustomCPUConfig::config_from_file();
+    return $cpu if $cpu_models && $cpu_models->get_model_by_name($cputype);
+
+    die "cputype '$cputype' not found\n" if !$noerr;
+    return undef;
+}
+
 my $watchdog_fmt = {
     model => {
        default_key => 1,
@@ -587,7 +614,7 @@ EODESCR
        optional => 1,
        description => "Emulated CPU type.",
        type => 'string',
-       format => $cpu_fmt,
+       format => 'pve-cpu-conf',
     },
     parent => get_standard_option('pve-snapshot-name', {
        optional => 1,
-- 
2.20.1


_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to