[pve-devel] [PATCH v3 qemu-server 06/11] Add overrides and convenience functions to CPUConfig

Stefan Reiter s.reiter at proxmox.com
Thu Oct 3 16:56:21 CEST 2019


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

get_model_by_name is used to return a cpu config with default values
filled out.

Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---

v2 -> v3:
* add validity checks to write_config


 PVE/QemuServer/CPUConfig.pm | 61 +++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/PVE/QemuServer/CPUConfig.pm b/PVE/QemuServer/CPUConfig.pm
index 6e2fb49..d335733 100644
--- a/PVE/QemuServer/CPUConfig.pm
+++ b/PVE/QemuServer/CPUConfig.pm
@@ -157,6 +157,67 @@ 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
+	cputype => $sectionId,
+	# models parsed from file are by definition always custom
+	'built-in' => 0,
+    });
+}
+
+sub write_config {
+    my ($class, $filename, $cfg) = @_;
+
+    for my $model (keys %{$cfg->{ids}}) {
+	my $model_conf = $cfg->{ids}->{$model};
+
+	die "internal error: tried saving custom cpumodel with cputype != cfg->ids entry\n"
+	    if $model != $model_conf->{cputype};
+
+	die "internal error: tried saving custom cpumodel with built-in set\n"
+	    if $model_conf->{'built-in'};
+
+	# models written to file must be custom anyway
+	delete $model_conf->{'built-in'};
+	# saved in section header
+	delete $model_conf->{cputype};
+    }
+
+    $class->SUPER::write_config($filename, $cfg);
+}
+
+# Use this to get a single model in the format described by $cpu_fmt.
+# Fills in defaults from schema if value is not set in config.
+# Returns undef for unknown $name.
+sub get_model_by_name {
+    my ($conf, $name) = @_;
+
+    my $entry = $conf->{ids}->{$name};
+    return undef if !defined($entry);
+
+    my $propList = $defaultData->{propertyList};
+
+    my $model = {};
+    for my $property (keys %$propList) {
+	next if $property eq 'type';
+
+	if (my $value = $entry->{$property}) {
+	    $model->{$property} = $value;
+	} elsif (my $default = $propList->{$property}->{default}) {
+	    $model->{$property} = $default;
+	}
+    }
+
+    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