[pve-devel] [PATCH v6 qemu-server 05/12] Add overrides and convenience functions to CPUConfig

Stefan Reiter s.reiter at proxmox.com
Thu Nov 21 15:53:45 CET 2019


Add two overrides to avoid writing redundant information to the config
file.

get_custom_model is used to retrieve a custom model configuration by
name.

Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---
 PVE/QemuServer/CPUConfig.pm | 62 +++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index be16eba..1cdc3c2 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -151,6 +151,68 @@ sub type {
     return 'cpu-model';
 }
 
+sub parse_section_header {
+    my ($class, $line) = @_;
+
+    my ($type, $sectionId, $errmsg, $config) =
+	$class->SUPER::parse_section_header($line);
+
+    return undef if !$type;
+    return ($type, $sectionId, $errmsg, {
+	# to avoid duplicate model name in config file, parse id as cputype
+	# models parsed from file are by definition always custom
+	cputype => "custom-$sectionId",
+    });
+}
+
+sub write_config {
+    my ($class, $filename, $cfg) = @_;
+
+    for my $model (keys %{$cfg->{ids}}) {
+	my $model_conf = $cfg->{ids}->{$model};
+
+	die "internal error: tried saving built-in CPU model (or missing prefix)\n"
+	    if !is_custom_model($model_conf->{cputype});
+
+	die "internal error: tried saving custom cpumodel with cputype (ignoring prefix) not equal to \$cfg->ids entry\n"
+	    if "custom-$model" ne $model_conf->{cputype};
+
+	# saved in section header
+	delete $model_conf->{cputype};
+    }
+
+    $class->SUPER::write_config($filename, $cfg);
+}
+
+sub is_custom_model {
+    my ($cputype) = @_;
+    return $cputype =~ m/^custom-/;
+}
+
+# Use this to get a single model in the format described by $cpu_fmt.
+# Allows names with and without custom- prefix.
+sub get_custom_model {
+    my ($name, $noerr) = @_;
+
+    $name =~ s/^custom-//;
+    my $conf = load_custom_model_conf();
+
+    my $entry = $conf->{ids}->{$name};
+    if (!defined($entry)) {
+	die "Custom cputype '$name' not found\n" if !$noerr;
+	return undef;
+    }
+
+    my $model = {};
+    for my $property (keys %$cpu_fmt) {
+	if (my $value = $entry->{$property}) {
+	    $model->{$property} = $value;
+	}
+    }
+
+    return $model;
+}
+
 # Print a QEMU device node for a given VM configuration for hotplugging CPUs
 sub print_cpu_device {
     my ($conf, $id) = @_;
-- 
2.20.1





More information about the pve-devel mailing list