pci: device selection: don't reserve PCI IDs when VM is already running

Since the only way this could happen is when we're being called
from 'qm showcmd' and there we don't want to reserve or create anything.

In case the VM was not running, we actually reserve the devices, so we
want to call 'cleanup_pci_devices' after to remove those again. This
minimizes the timespan where those devices are not available for real vm
starts.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Tested-by: Christoph Heiss <c.heiss@proxmox.com>
Reviewed-by: Christoph Heiss <c.heiss@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Dominik Csapak 2024-08-06 14:22:00 +02:00 committed by Thomas Lamprecht
parent c8a37e1993
commit fc23c72a42
3 changed files with 16 additions and 3 deletions

View File

@ -6163,6 +6163,11 @@ sub vm_commandline {
my $defaults = load_defaults(); my $defaults = load_defaults();
my $cmd = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu); my $cmd = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu);
# 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 $@;
}
return PVE::Tools::cmd2string($cmd); return PVE::Tools::cmd2string($cmd);
} }

View File

@ -523,6 +523,9 @@ sub parse_hostpci_devices {
my sub choose_hostpci_devices { my sub choose_hostpci_devices {
my ($devices, $vmid) = @_; my ($devices, $vmid) = @_;
# if the vm is running, we must be in 'showcmd', so don't actually reserve or create anything
my $is_running = PVE::QemuServer::Helpers::vm_running_locally($vmid) ? 1 : 0;
my $used = {}; my $used = {};
my $add_used_device = sub { my $add_used_device = sub {
@ -555,8 +558,10 @@ my sub choose_hostpci_devices {
my $ids = [map { $_->{id} } @$alternative]; my $ids = [map { $_->{id} } @$alternative];
next if grep { defined($used->{$_}) } @$ids; # already used next if grep { defined($used->{$_}) } @$ids; # already used
if (!$is_running) {
eval { reserve_pci_usage($ids, $vmid, 10, undef) }; eval { reserve_pci_usage($ids, $vmid, 10, undef) };
next if $@; next if $@;
}
# found one that is not used or reserved # found one that is not used or reserved
$add_used_device->($alternative); $add_used_device->($alternative);

View File

@ -196,7 +196,10 @@ $qemu_server_module->mock(
}, },
get_initiator_name => sub { get_initiator_name => sub {
return 'iqn.1993-08.org.debian:01:aabbccddeeff'; return 'iqn.1993-08.org.debian:01:aabbccddeeff';
} },
cleanup_pci_devices => {
# do nothing
},
); );
my $qemu_server_config; my $qemu_server_config;