all lists on 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 8/8] image convert: re-use generate_drive_blockdev()
Date: Fri, 25 Jul 2025 12:50:55 +0200	[thread overview]
Message-ID: <20250725105109.54093-9-f.ebner@proxmox.com> (raw)
In-Reply-To: <20250725105109.54093-1-f.ebner@proxmox.com>

This avoids having the handling for 'discard-no-unref' in two places.

In the tests, rename the relevant target images with a '-target'
suffix to test for them in the mocked volume_snapshot_info() helper.

Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

Seems quite complex now for the given use-case, but could be improved
and used as a basis for using target-image-opts for all target images
in the future.

New in v2.

 src/PVE/QemuServer/QemuImage.pm        | 64 +++++++++++++++++++++++---
 src/test/run_qemu_img_convert_tests.pl | 21 ++++++---
 2 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/src/PVE/QemuServer/QemuImage.pm b/src/PVE/QemuServer/QemuImage.pm
index 804cbc8e..658f6705 100644
--- a/src/PVE/QemuServer/QemuImage.pm
+++ b/src/PVE/QemuServer/QemuImage.pm
@@ -5,11 +5,13 @@ use warnings;
 
 use Fcntl qw(S_ISBLK);
 use File::stat;
+use JSON;
 
 use PVE::Format qw(render_bytes);
 use PVE::Storage;
 use PVE::Tools;
 
+use PVE::QemuServer::Blockdev;
 use PVE::QemuServer::Drive qw(checked_volume_format);
 use PVE::QemuServer::Helpers;
 
@@ -31,15 +33,62 @@ sub convert_iscsi_path {
 }
 
 my sub qcow2_target_image_opts {
-    my ($path, @qcow2_opts) = @_;
+    my ($storecfg, $drive, @qcow2_opts) = @_;
 
-    my $st = File::stat::stat($path) or die "stat for '$path' failed - $!\n";
+    # There is no machine version, the qemu-img binary version is what's important.
+    my $version = PVE::QemuServer::Helpers::kvm_user_version();
 
-    my $driver = S_ISBLK($st->mode) ? 'host_device' : 'file';
+    my $blockdev = PVE::QemuServer::Blockdev::generate_drive_blockdev(
+        $storecfg,
+        $drive,
+        $version,
+        { 'no-throttle' => 1 },
+    );
 
-    my $qcow2_opts_str = ',' . join(',', @qcow2_opts);
+    my $opts = [];
+    my $opt_prefix = '';
+    my $next_child = $blockdev;
+    while ($next_child) {
+        my $current = $next_child;
+        $next_child = delete($current->{file});
 
-    return "driver=qcow2$qcow2_opts_str,file.driver=$driver,file.filename=$path";
+        # TODO should cache settings be configured here (via appropriate drive configuration) rather
+        # than via dedicated qemu-img options?
+        delete($current->{cache});
+        # TODO e.g. can't use aio 'native' without cache.direct, just use QEMU default like for
+        # other targets for now
+        delete($current->{aio});
+
+        # no need for node names
+        delete($current->{'node-name'});
+
+        # it's the write target, while the flag should be 'false' anyways, remove to be sure
+        delete($current->{'read-only'});
+
+        # TODO should those be set (via appropriate drive configuration)?
+        delete($current->{'detect-zeroes'});
+        delete($current->{'discard'});
+
+        for my $key (sort keys $current->%*) {
+            my $value;
+            if (ref($current->{$key})) {
+                if ($current->{$key} eq JSON::false) {
+                    $value = 'false';
+                } elsif ($current->{$key} eq JSON::true) {
+                    $value = 'true';
+                } else {
+                    die "target image options: unhandled structured key: $key\n";
+                }
+            } else {
+                $value = $current->{$key};
+            }
+            push $opts->@*, "$opt_prefix$key=$value";
+        }
+
+        $opt_prefix .= 'file.';
+    }
+
+    return join(',', $opts->@*);
 }
 
 # The possible options are:
@@ -113,7 +162,10 @@ sub convert {
         $dst_path = convert_iscsi_path($dst_path);
     } elsif ($dst_needs_discard_no_unref) {
         push @$cmd, '--target-image-opts';
-        $dst_path = qcow2_target_image_opts($dst_path, 'discard-no-unref=true');
+        # don't use any other drive options, those are intended for use with a running VM and just
+        # use scsi0 as a dummy interface+index for now
+        my $dst_drive = { file => $dst_volid, interface => 'scsi', index => 0 };
+        $dst_path = qcow2_target_image_opts($storecfg, $dst_drive, 'discard-no-unref=true');
     } else {
         push @$cmd, '-O', $dst_format;
     }
diff --git a/src/test/run_qemu_img_convert_tests.pl b/src/test/run_qemu_img_convert_tests.pl
index 3c8f09f0..5152bf47 100755
--- a/src/test/run_qemu_img_convert_tests.pl
+++ b/src/test/run_qemu_img_convert_tests.pl
@@ -532,7 +532,7 @@ my $tests = [
         name => "qcow2_external_snapshot_target",
         parameters => [
             "local:$vmid/vm-$vmid-disk-0.raw",
-            "localsnapext:$vmid/vm-$vmid-disk-0.qcow2",
+            "localsnapext:$vmid/vm-$vmid-disk-target.qcow2",
             1024 * 10,
         ],
         expected => [
@@ -544,14 +544,14 @@ my $tests = [
             "raw",
             "--target-image-opts",
             "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
-            "driver=qcow2,discard-no-unref=true,file.driver=file,"
-                . "file.filename=/var/lib/vzsnapext/images/$vmid/vm-$vmid-disk-0.qcow2",
+            "discard-no-unref=true,driver=qcow2,file.driver=file"
+                . ",file.filename=/var/lib/vzsnapext/images/$vmid/vm-$vmid-disk-target.qcow2",
         ],
     },
     {
         name => "lvmqcow2_external_snapshot_target",
         parameters => [
-            "local:$vmid/vm-$vmid-disk-0.raw", "lvm-store:vm-$vmid-disk-0.qcow2", 1024 * 10,
+            "local:$vmid/vm-$vmid-disk-0.raw", "lvm-store:vm-$vmid-disk-target.qcow2", 1024 * 10,
         ],
         expected => [
             "/usr/bin/qemu-img",
@@ -562,8 +562,8 @@ my $tests = [
             "raw",
             "--target-image-opts",
             "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
-            "driver=qcow2,discard-no-unref=true,file.driver=host_device,"
-                . "file.filename=/dev/pve/vm-$vmid-disk-0.qcow2",
+            "discard-no-unref=true,driver=qcow2,file.driver=host_device"
+                . ",file.filename=/dev/pve/vm-$vmid-disk-target.qcow2",
         ],
     },
 ];
@@ -588,6 +588,15 @@ $storage_module->mock(
     activate_volumes => sub {
         return 1;
     },
+    volume_snapshot_info => sub {
+        my ($cfg, $volid) = @_;
+        if ($volid eq "lvm-store:vm-$vmid-disk-target.qcow2"
+            || $volid eq "localsnapext:$vmid/vm-$vmid-disk-target.qcow2") {
+            # target volumes don't have snapshots
+            return {};
+        }
+        die "mocked volume_snapshot_info called with unexpected volid $volid\n";
+    },
 );
 
 my $lio_module = Test::MockModule->new("PVE::Storage::LunCmd::LIO");
-- 
2.47.2



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

  parent reply	other threads:[~2025-07-25 10:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-25 10:50 [pve-devel] [PATCH-SERIES qemu-server v2 0/8] blockdev and snapshot-as-volume-chain on LVM fixes Fiona Ebner
2025-07-25 10:50 ` [pve-devel] [PATCH qemu-server v2 1/8] blockdev: helper to add common options Fiona Ebner
2025-07-25 10:50 ` [pve-devel] [PATCH qemu-server v2 2/8] blockdev: fix discard Fiona Ebner
2025-07-25 10:50 ` [pve-devel] [PATCH qemu-server v2 3/8] tests: image convert: avoid hard-coded VM ID in result Fiona Ebner
2025-07-25 10:50 ` [pve-devel] [PATCH qemu-server v2 4/8] tests: image convert: properly set snapshot-as-volume-chain option Fiona Ebner
2025-07-25 10:50 ` [pve-devel] [PATCH qemu-server v2 5/8] tests: image convert: add tests where storages with 'snapshot-as-volume-chain' are the target Fiona Ebner
2025-07-25 10:50 ` [pve-devel] [PATCH qemu-server v2 6/8] fix #6543: use qcow2 'discard-no-unref' option when using snapshot-as-volume-chain Fiona Ebner
2025-07-25 10:50 ` [pve-devel] [PATCH qemu-server v2 7/8] tests: cfg2cmd: improve mocking File::stat Fiona Ebner
2025-07-25 10:50 ` Fiona Ebner [this message]
2025-07-25 12:15 ` [pve-devel] applied-series: [PATCH-SERIES qemu-server v2 0/8] blockdev and snapshot-as-volume-chain on LVM fixes 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=20250725105109.54093-9-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal