Have windows_get_pinned_machine_version() require passing an
explicitly set $machine and adapt the callers.

Note that the callers of check_and_pin_machine_string() use the
get_vm_arch() helper too, so the $arch variable is not undef there.

Signed-off-by: Fiona Ebner <[email protected]>
---
 src/PVE/API2/Qemu.pm          |  4 ++--
 src/PVE/QemuServer/Machine.pm | 12 ++++++++----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index d6046a25..c2e185a6 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -1483,7 +1483,7 @@ __PACKAGE__->register_method({
 
                     # always pin Windows' machine version on create, they get 
confused too easily
                     my $machine_string = 
PVE::QemuServer::Machine::check_and_pin_machine_string(
-                        $conf->{machine}, $conf->{ostype},
+                        $conf->{machine}, $conf->{ostype}, $arch,
                     );
                     $conf->{machine} = $machine_string if $machine_string;
 
@@ -2464,7 +2464,7 @@ my $update_vm_api = sub {
                         eval {
                             my $machine_string =
                                 
PVE::QemuServer::Machine::check_and_pin_machine_string(
-                                    $conf->{machine}, $param->{ostype},
+                                    $conf->{machine}, $param->{ostype}, $arch,
                                 );
                             $conf->{pending}->{machine} = $machine_string if 
$machine_string;
                         };
diff --git a/src/PVE/QemuServer/Machine.pm b/src/PVE/QemuServer/Machine.pm
index 21c62a8f..0d4b77af 100644
--- a/src/PVE/QemuServer/Machine.pm
+++ b/src/PVE/QemuServer/Machine.pm
@@ -379,11 +379,13 @@ sub latest_installed_machine_version {
 sub windows_get_pinned_machine_version {
     my ($machine, $base_version, $kvmversion) = @_;
 
+    die "internal error - no machine provided" if !$machine;
+
     my $pin_version = $base_version;
     if (!defined($base_version) || !can_run_pve_machine_version($base_version, 
$kvmversion)) {
         $pin_version = latest_installed_machine_version($kvmversion);
     }
-    if (!$machine || $machine eq 'pc') {
+    if ($machine eq 'pc') {
         $machine = "pc-i440fx-$pin_version";
     } elsif ($machine eq 'q35') {
         $machine = "pc-q35-$pin_version";
@@ -404,6 +406,9 @@ sub get_vm_machine {
 
     if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) {
         my $kvmversion = PVE::QemuServer::Helpers::kvm_user_version();
+        my $arch = PVE::QemuServer::Helpers::get_vm_arch($conf);
+        $machine ||= default_machine_for_arch($arch);
+
         # we must pin Windows VMs without a specific version and no meta info 
about creation QEMU to
         # 5.1, as 5.2 fixed a bug in ACPI layout which confuses windows quite 
a bit and may result
         # in various regressions..
@@ -423,8 +428,6 @@ sub get_vm_machine {
             }
             $machine = windows_get_pinned_machine_version($machine, 
$base_version, $kvmversion);
         } else {
-            my $arch = PVE::QemuServer::Helpers::get_vm_arch($conf);
-            $machine ||= default_machine_for_arch($arch);
             my $pvever = get_pve_version($kvmversion);
             $machine .= "+pve$pvever";
         }
@@ -445,13 +448,14 @@ sub get_vm_machine {
 }
 
 sub check_and_pin_machine_string {
-    my ($machine_string, $ostype) = @_;
+    my ($machine_string, $ostype, $arch) = @_;
 
     my $machine_conf = parse_machine($machine_string);
     my $machine = $machine_conf->{type};
     if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) {
         # always pin Windows' machine version on create, they get confused too 
easily
         if (PVE::QemuServer::Helpers::windows_version($ostype)) {
+            $machine = default_machine_for_arch($arch) if !$machine;
             $machine_conf->{type} = 
windows_get_pinned_machine_version($machine);
             print "pinning machine type to '$machine_conf->{type}' for Windows 
guest OS\n";
         }
-- 
2.47.3




Reply via email to