[pve-devel] [PATCH 1/2] Refactored $nokvm to $kvm

Philip Abernethy p.abernethy at proxmox.com
Wed Aug 16 18:24:29 CEST 2017


Where possible I reworked the code to remove the resulting inversion.
The result should be easier to read and understand.
I also fixed a type in the process.
---
 PVE/QemuServer.pm | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 1f34101..88b1c5b 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -1740,8 +1740,8 @@ sub print_netdev_full {
 sub print_cpu_device {
     my ($conf, $id) = @_;
 
-    my $nokvm = defined($conf->{kvm}) && $conf->{kvm} == 0 ? 1 : 0;
-    my $cpu = $nokvm ? "qemu64" : "kvm64";
+    my $kvm = $conf->{kvm} // 1;
+    my $cpu = $kvm ? "kvm64" : "qemu64";
     if (my $cputype = $conf->{cpu}) {
 	my $cpuconf = PVE::JSONSchema::parse_property_string($cpu_fmt, $cputype)
 	    or die "Cannot parse cpu description: $cputype\n";
@@ -2835,6 +2835,7 @@ sub config_to_command {
     my $vernum = 0; # unknown
     my $ostype = $conf->{ostype};
     my $winversion = windows_version($ostype);
+    my $kvm = $conf->{kvm} // 1;
 
     if ($kvmver =~ m/^(\d+)\.(\d+)$/) {
 	$vernum = $1*1000000+$2*1000;
@@ -3085,7 +3086,6 @@ sub config_to_command {
     # time drift fix
     my $tdf = defined($conf->{tdf}) ? $conf->{tdf} : $defaults->{tdf};
 
-    my $nokvm = defined($conf->{kvm}) && $conf->{kvm} == 0 ? 1 : 0;
     my $useLocaltime = $conf->{localtime};
 
     if ($winversion >= 5) { # windows
@@ -3104,7 +3104,7 @@ sub config_to_command {
 
     push @$rtcFlags, 'driftfix=slew' if $tdf;
 
-    if ($nokvm) {
+    if (!$kvm) {
 	push @$machineFlags, 'accel=tcg';
     } else {
 	die "No accelerator found!\n" if !$cpuinfo->{hvm};
@@ -3120,7 +3120,7 @@ sub config_to_command {
 	push @$rtcFlags, 'base=localtime';
     }
 
-    my $cpu = $nokvm ? "qemu64" : "kvm64";
+    my $cpu = $kvm ? "kvm64" : "qemu64";
     if (my $cputype = $conf->{cpu}) {
 	my $cpuconf = PVE::JSONSchema::parse_property_string($cpu_fmt, $cputype)
 	    or die "Cannot parse cpu description: $cputype\n";
@@ -3139,13 +3139,13 @@ sub config_to_command {
 
     if (qemu_machine_feature_enabled ($machine_type, $kvmver, 2, 3)) {
 
-	push @$cpuFlags , '+kvm_pv_unhalt' if !$nokvm;
-	push @$cpuFlags , '+kvm_pv_eoi' if !$nokvm;
+	push @$cpuFlags , '+kvm_pv_unhalt' if $kvm;
+	push @$cpuFlags , '+kvm_pv_eoi' if $kvm;
     }
 
-    add_hyperv_enlighments($cpuFlags, $winversion, $machine_type, $kvmver, $nokvm, $conf->{bios}, $gpu_passthrough);
+    add_hyperv_enlightenments($cpuFlags, $winversion, $machine_type, $kvmver, $kvm, $conf->{bios}, $gpu_passthrough);
 
-    push @$cpuFlags, 'enforce' if $cpu ne 'host' && !$nokvm;
+    push @$cpuFlags, 'enforce' if $cpu ne 'host' && $kvm;
 
     push @$cpuFlags, 'kvm=off' if $kvm_off;
 
@@ -6359,10 +6359,10 @@ sub scsihw_infos {
     return ($maxdev, $controller, $controller_prefix);
 }
 
-sub add_hyperv_enlighments {
-    my ($cpuFlags, $winversion, $machine_type, $kvmver, $nokvm, $bios, $gpu_passthrough) = @_;
+sub add_hyperv_enlightenments {
+    my ($cpuFlags, $winversion, $machine_type, $kvmver, $kvm, $bios, $gpu_passthrough) = @_;
 
-    return if $nokvm;
+    return if !$kvm;
     return if $winversion < 6;
     return if $bios && $bios eq 'ovmf' && $winversion < 8;
 
-- 
2.11.0





More information about the pve-devel mailing list