[pve-devel] [PATCH qemu-server 7/7] Allow custom CPU types in API

Fabian Grünbichler f.gruenbichler at proxmox.com
Mon Sep 9 11:53:54 CEST 2019


On September 2, 2019 4:27 pm, Stefan Reiter wrote:
> 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.reiter at 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\-_]+/,

this removes information from our API schema dumps (e.g., API viewer, 
'man qm', ...). we could at least put it into format_description with 
some additional sentence referencing custom types)

>  	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);

and here I just realized something that affects the whole patch series - 
we probably need to namespace the custom cpu models somehow when 
referencing them in the config. otherwise a future QEMU version might 
add an additional CPU model that conflicts with a defined custom one.

we can either add a flag to 'cpu', just prefix the names in the VM 
config, or prefix the names both in the VM config and in the CPU model 
config. all approaches have pros and cons - maybe there is some other 
clever way to avoid this problem?

> +
> +    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 at pve.proxmox.com
> https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 
> 




More information about the pve-devel mailing list