[pve-devel] [PATCH qemu-server 14/22] vm start/commandline: also clean up pci reservation when config_to_command() fails

Fiona Ebner f.ebner at proxmox.com
Thu Jun 12 16:02:45 CEST 2025


The config_to_command() function already calls into
print_hostpci_devices() which makes reservations.

Signed-off-by: Fiona Ebner <f.ebner at proxmox.com>
---
 PVE/QemuServer.pm | 56 +++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 24 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index dfd4351b..934adf60 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -5649,31 +5649,32 @@ sub vm_start_nolock {
     my $memory = get_current_memory($conf->{memory});
     my $start_timeout = $params->{timeout} // config_aware_timeout($conf, $memory, $resume);
 
-    # Note that for certain cases like templates, the configuration is minimized, so need to ensure
-    # the rest of the function here uses the same configuration that was used to build the command
-    (my $cmd, my $vollist, my $spice_port, my $pci_devices, $conf) = config_to_command(
-	$storecfg,
-	$vmid,
-	$conf,
-	$defaults,
-	$forcemachine,
-	$forcecpu,
-	$params->{'live-restore-backing'},
-    );
-
-    push $cmd->@*, $state_cmdline->@*;
-    push @$vollist, $statefile if $statefile_is_a_volume;
-
+    my ($cmd, $vollist, $spice_port);
     my $pci_reserve_list = [];
-    for my $device (values $pci_devices->%*) {
-	next if $device->{mdev}; # we don't reserve for mdev devices
-	push $pci_reserve_list->@*, map { $_->{id} } $device->{ids}->@*;
-    }
-
-    # reserve all PCI IDs before actually doing anything with them
-    PVE::QemuServer::PCI::reserve_pci_usage($pci_reserve_list, $vmid, $start_timeout);
-
     eval {
+	# Note that for certain cases like templates, the configuration is minimized, so need to ensure
+	# the rest of the function here uses the same configuration that was used to build the command
+	($cmd, $vollist, $spice_port, my $pci_devices, $conf) = config_to_command(
+	    $storecfg,
+	    $vmid,
+	    $conf,
+	    $defaults,
+	    $forcemachine,
+	    $forcecpu,
+	    $params->{'live-restore-backing'},
+	);
+
+	push $cmd->@*, $state_cmdline->@*;
+	push @$vollist, $statefile if $statefile_is_a_volume;
+
+	for my $device (values $pci_devices->%*) {
+	    next if $device->{mdev}; # we don't reserve for mdev devices
+	    push $pci_reserve_list->@*, map { $_->{id} } $device->{ids}->@*;
+	}
+
+	# reserve all PCI IDs before actually doing anything with them
+	PVE::QemuServer::PCI::reserve_pci_usage($pci_reserve_list, $vmid, $start_timeout);
+
 	my $uuid;
 	for my $id (sort keys %$pci_devices) {
 	    my $d = $pci_devices->{$id};
@@ -5981,13 +5982,20 @@ sub vm_commandline {
 
     my $defaults = load_defaults();
 
-    my $cmd = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu);
+    my $cmd;
+    eval {
+	$cmd = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu);
+    };
+    my $err = $@;
+
     # if the vm is not running, we need to clean up the reserved/created devices
     if (!PVE::QemuServer::Helpers::vm_running_locally($vmid)) {
 	eval { cleanup_pci_devices($vmid, $conf) };
 	warn $@ if $@;
     }
 
+    die $err if $err;
+
     return PVE::Tools::cmd2string($cmd);
 }
 
-- 
2.39.5





More information about the pve-devel mailing list