public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server v2 18/32] vm start/commandline: activate volumes before config_to_command()
Date: Wed, 18 Jun 2025 15:01:55 +0200	[thread overview]
Message-ID: <20250618130209.90649-19-f.ebner@proxmox.com> (raw)
In-Reply-To: <20250618130209.90649-1-f.ebner@proxmox.com>

With '-blockdev', it is necessary to activate the volumes to generate
the command line, because it can be necessary to check whether the
volume is a block device or a regular file.

Do not deactivate after commandline generation for 'qm showcmd',
because there can be concurrent operations acting on the volumes.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Changes in v2:
* Do not deactivate after showcmd, because it might not be safe.

 src/PVE/QemuServer.pm                | 60 +++++++++++++++++++++-------
 src/test/run_config2command_tests.pl | 10 +++++
 2 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 68d5c1b5..38b3a569 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -4016,7 +4016,6 @@ sub config_to_command {
         push @$devices, '-watchdog-action', $wdopts->{action} if $wdopts->{action};
     }
 
-    my $vollist = [];
     my $scsicontroller = {};
     my $ahcicontroller = {};
     my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : $defaults->{scsihw};
@@ -4031,11 +4030,6 @@ sub config_to_command {
         sub {
             my ($ds, $drive) = @_;
 
-            if (PVE::Storage::parse_volume_id($drive->{file}, 1)) {
-                check_volume_storage_type($storecfg, $drive->{file});
-                push @$vollist, $drive->{file};
-            }
-
             # ignore efidisk here, already added in bios/fw handling code above
             return if $drive->{interface} eq 'efidisk';
             # similar for TPM
@@ -4234,7 +4228,6 @@ sub config_to_command {
 
     if (my $vmstate = $conf->{vmstate}) {
         my $statepath = PVE::Storage::path($storecfg, $vmstate);
-        push @$vollist, $vmstate;
         push @$cmd, '-loadstate', $statepath;
         print "activating and using '$vmstate' as vmstate\n";
     }
@@ -4250,7 +4243,7 @@ sub config_to_command {
         push @$cmd, @$aa;
     }
 
-    return wantarray ? ($cmd, $vollist, $spice_port, $pci_devices, $conf) : $cmd;
+    return wantarray ? ($cmd, $spice_port, $pci_devices, $conf) : $cmd;
 }
 
 sub spice_port {
@@ -6017,12 +6010,18 @@ sub vm_start_nolock {
         $state_cmdline = ['-S'];
     }
 
-    my ($cmd, $vollist, $spice_port, $start_timeout);
+    my $vollist = get_current_vm_volumes($storecfg, $conf);
+    push $vollist->@*, $statefile if $statefile_is_a_volume;
+
+    my ($cmd, $spice_port, $start_timeout);
     my $pci_reserve_list = [];
     eval {
+        # With -blockdev, it is necessary to activate the volumes before generating the command line
+        PVE::Storage::activate_volumes($storecfg, $vollist);
+
         # 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(
+        ($cmd, $spice_port, my $pci_devices, $conf) = config_to_command(
             $storecfg,
             $vmid,
             $conf,
@@ -6036,7 +6035,6 @@ sub vm_start_nolock {
         $start_timeout = $params->{timeout} // config_aware_timeout($conf, $memory, $resume);
 
         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
@@ -6080,13 +6078,13 @@ sub vm_start_nolock {
         push @$cmd, '-uuid', $uuid if defined($uuid);
     };
     if (my $err = $@) {
+        eval { PVE::Storage::deactivate_volumes($storecfg, $vollist); };
+        warn $@ if $@;
         eval { cleanup_pci_devices($vmid, $conf) };
         warn $@ if $@;
         die $err;
     }
 
-    PVE::Storage::activate_volumes($storecfg, $vollist);
-
     my %silence_std_outs = (outfunc => sub { }, errfunc => sub { });
     eval { run_command(['/bin/systemctl', 'reset-failed', "$vmid.scope"], %silence_std_outs) };
     eval { run_command(['/bin/systemctl', 'stop', "$vmid.scope"], %silence_std_outs) };
@@ -6369,12 +6367,22 @@ sub vm_commandline {
 
     my $defaults = load_defaults();
 
+    my $running = PVE::QemuServer::Helpers::vm_running_locally($vmid);
+    my $volumes = [];
+
+    # With -blockdev, it is necessary to activate the volumes before generating the command line
+    if (!$running) {
+        $volumes = get_current_vm_volumes($storecfg, $conf);
+        PVE::Storage::activate_volumes($storecfg, $volumes);
+    }
+
     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)) {
+    # If the vm is not running, need to clean up the reserved/created devices.
+    # There might be concurrent operations on the volumes, so do not deactivate.
+    if (!$running) {
         eval { cleanup_pci_devices($vmid, $conf) };
         warn $@ if $@;
     }
@@ -6421,6 +6429,28 @@ sub get_vm_volumes {
     return $vollist;
 }
 
+# Get volumes defined in the current VM configuration, including the VM state file.
+sub get_current_vm_volumes {
+    my ($storecfg, $conf) = @_;
+
+    my $volumes = [];
+
+    PVE::QemuConfig->foreach_volume_full(
+        $conf,
+        { extra_keys => ['vmstate'] },
+        sub {
+            my ($ds, $drive) = @_;
+
+            if (PVE::Storage::parse_volume_id($drive->{file}, 1)) {
+                check_volume_storage_type($storecfg, $drive->{file});
+                push $volumes->@*, $drive->{file};
+            }
+        },
+    );
+
+    return $volumes;
+}
+
 sub cleanup_pci_devices {
     my ($vmid, $conf) = @_;
 
diff --git a/src/test/run_config2command_tests.pl b/src/test/run_config2command_tests.pl
index 590f80f5..cbd8cd8b 100755
--- a/src/test/run_config2command_tests.pl
+++ b/src/test/run_config2command_tests.pl
@@ -256,6 +256,16 @@ $qemu_server_module->mock(
     },
 );
 
+my $storage_module = Test::MockModule->new("PVE::Storage");
+$storage_module->mock(
+    activate_volumes => sub {
+        return;
+    },
+    deactivate_volumes => sub {
+        return;
+    },
+);
+
 my $zfsplugin_module = Test::MockModule->new("PVE::Storage::ZFSPlugin");
 $zfsplugin_module->mock(
     zfs_get_lu_name => sub {
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2025-06-18 13:09 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-18 13:01 [pve-devel] [PATCH-SERIES common/qemu-server v2 00/32] preparation for switch to blockdev Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH common v2 01/32] schema: parse property string: support skipping keys Fiona Ebner
2025-06-20 11:00   ` Fabian Grünbichler
2025-06-18 13:01 ` [pve-devel] [PATCH common v2 02/32] json schema: add helper to convert to JSON boolean Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 03/32] buildsys: order Perl source files in QemuServer/Makefile Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 04/32] drive: code cleanup: drop unused $vmid parameter from get_path_and_format() Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 05/32] cfg2cmd: require at least QEMU binary version 6.0 Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 06/32] drive: parse: use hash argument for optional parameters Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 07/32] drive: parse: properly handle dropped properties for restore Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 08/32] drive: remove geometry options gone since QEMU 3.1 Fiona Ebner
2025-06-20 11:03   ` Fabian Grünbichler
2025-06-20 11:20     ` Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 09/32] clone disk: io uring check: fix call to determine cache direct Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 10/32] drive: move storage_allows_io_uring_default() and drive_uses_cache_direct() helpers to drive module Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 11/32] drive: introduce aio_cmdline_option() helper Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 12/32] drive: introduce detect_zeroes_cmdline_option() helper Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 13/32] vm start: assert that migration type is set for 'tcp' migration Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 14/32] introduce StateFile module for state file related helpers Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 15/32] vm start: move state file handling to dedicated module Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 16/32] vm start: move config_to_command() call further down Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 17/32] vm start/commandline: also clean up pci reservation when config_to_command() fails Fiona Ebner
2025-06-18 13:01 ` Fiona Ebner [this message]
2025-06-20 11:33   ` [pve-devel] [PATCH qemu-server v2 18/32] vm start/commandline: activate volumes before config_to_command() Fabian Grünbichler
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 19/32] pci: add missing includes Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 20/32] test: add tests for PCI reservations Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 21/32] cfg2cmd: print vga: fix call to print_pcie_addr() Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 22/32] pci: code cleanup: remove superfluous machine type paramater from print_pci_addr Fiona Ebner
2025-06-18 15:19   ` Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 23/32] cfg2cmd: collect optional parameters as a hash Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 24/32] qm: showcmd: never reserve PCI devices Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 25/32] vm devices list: prepare querying block device names for -blockdev Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 26/32] print drive device: explicitly set write-cache starting with machine version 10.0 Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 27/32] print drive device: set {r, w}error front-end properties " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 28/32] print drive device: don't reference any drive for 'none' " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 29/32] drive: create a throttle group for each drive " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 30/32] blockdev: add helpers to generate blockdev commandline Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [RFC qemu-server v2 31/32] blockdev: add support for NBD paths Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [RFC qemu-server v2 32/32] command line: switch to blockdev starting with machine version 10.0 Fiona Ebner
2025-06-23  9:12   ` DERUMIER, Alexandre via pve-devel
2025-06-23  9:31     ` Fiona Ebner
2025-06-23 13:06       ` DERUMIER, Alexandre via pve-devel
2025-06-20 13:03 ` [pve-devel] partially-applied: [PATCH-SERIES common/qemu-server v2 00/32] preparation for switch to blockdev Fabian Grünbichler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250618130209.90649-19-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal