From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 0A34C1FF15D
	for <inbox@lore.proxmox.com>; Thu, 19 Sep 2024 15:19:50 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 0EB6416685;
	Thu, 19 Sep 2024 15:19:59 +0200 (CEST)
To: pve-devel@lists.proxmox.com
Date: Thu, 19 Sep 2024 15:19:15 +0200
In-Reply-To: <20240919131915.2624831-1-alexandre.derumier@groupe-cyllene.com>
References: <20240919131915.2624831-1-alexandre.derumier@groupe-cyllene.com>
MIME-Version: 1.0
Message-ID: <mailman.19.1726751998.332.pve-devel@lists.proxmox.com>
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Post: <mailto:pve-devel@lists.proxmox.com>
From: Alexandre Derumier via pve-devel <pve-devel@lists.proxmox.com>
Precedence: list
Cc: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
X-Mailman-Version: 2.1.29
X-BeenThere: pve-devel@lists.proxmox.com
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
Subject: [pve-devel] [PATCH qemu-server 1/1] implement external snapshot
Content-Type: multipart/mixed; boundary="===============7752927231757229114=="
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

--===============7752927231757229114==
Content-Type: message/rfc822
Content-Disposition: inline

Return-Path: <root@formationkvm1.odiso.net>
X-Original-To: pve-devel@lists.proxmox.com
Delivered-To: pve-devel@lists.proxmox.com
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by lists.proxmox.com (Postfix) with ESMTPS id 8D174C054B
	for <pve-devel@lists.proxmox.com>; Thu, 19 Sep 2024 15:19:58 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 6DCE0162F6
	for <pve-devel@lists.proxmox.com>; Thu, 19 Sep 2024 15:19:28 +0200 (CEST)
Received: from bastiontest.odiso.net (unknown [IPv6:2a0a:1580:2000:6700::14])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
	(No client certificate requested)
	by firstgate.proxmox.com (Proxmox) with ESMTPS
	for <pve-devel@lists.proxmox.com>; Thu, 19 Sep 2024 15:19:27 +0200 (CEST)
Received: from formationkvm1.odiso.net (unknown [10.11.201.57])
	by bastiontest.odiso.net (Postfix) with ESMTP id 0084B82E03E;
	Thu, 19 Sep 2024 15:19:19 +0200 (CEST)
Received: by formationkvm1.odiso.net (Postfix, from userid 0)
	id 6A4FE11339EF; Thu, 19 Sep 2024 15:19:17 +0200 (CEST)
From: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server 1/1] implement external snapshot
Date: Thu, 19 Sep 2024 15:19:15 +0200
Message-Id: <20240919131915.2624831-3-alexandre.derumier@groupe-cyllene.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20240919131915.2624831-1-alexandre.derumier@groupe-cyllene.com>
References: <20240919131915.2624831-1-alexandre.derumier@groupe-cyllene.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
	AWL                     0.046 Adjusted score from AWL reputation of From: address
	BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
	DMARC_NONE                0.1 DMARC none policy
	HEADER_FROM_DIFFERENT_DOMAINS  0.248 From and EnvelopeFrom 2nd level mail domains are different
	KAM_DMARC_NONE           0.25 DKIM has Failed or SPF has failed on the message and the domain has no DMARC policy
	KAM_DMARC_STATUS         0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
	KAM_LAZY_DOMAIN_SECURITY      1 Sending domain does not have any anti-forgery methods
	RDNS_NONE               0.793 Delivered to internal network by a host with no rDNS
	SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
	SPF_NONE                0.001 SPF: sender does not publish an SPF Record

Signed-off-by: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
---
 PVE/QemuServer.pm | 74 +++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 62 insertions(+), 12 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b26da505..646e3476 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4713,9 +4713,14 @@ sub qemu_volume_snapshot {
     my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
 
     my $running = check_running($vmid);
-
-    if ($running && do_snapshots_with_qemu($storecfg, $volid, $deviceid)) {
-	mon_cmd($vmid, 'blockdev-snapshot-internal-sync', device => $deviceid, name => $snap);
+    my $do_snapshots_with_qemu = do_snapshots_with_qemu($storecfg, $volid, $deviceid) if $running;
+    if ($do_snapshots_with_qemu) {
+	if($do_snapshots_with_qemu == 2) {
+	    my $snapshot_file = PVE::Storage::path($storecfg, $volid, $snap);
+	    mon_cmd($vmid, 'blockdev-snapshot-sync', device => $deviceid, 'snapshot-file' => $snapshot_file, format => 'qcow2');
+	} else {
+	    mon_cmd($vmid, 'blockdev-snapshot-internal-sync', device => $deviceid, name => $snap);
+	}
     } else {
 	PVE::Storage::volume_snapshot($storecfg, $volid, $snap);
     }
@@ -4735,13 +4740,48 @@ sub qemu_volume_snapshot_delete {
 	});
     }
 
-    if ($attached_deviceid && do_snapshots_with_qemu($storecfg, $volid, $attached_deviceid)) {
-	mon_cmd(
-	    $vmid,
-	    'blockdev-snapshot-delete-internal-sync',
-	    device => $attached_deviceid,
-	    name => $snap,
-	);
+    my $do_snapshots_with_qemu = do_snapshots_with_qemu($storecfg, $volid, $attached_deviceid) if $running;
+    if ($attached_deviceid && $do_snapshots_with_qemu) {
+
+	if ($do_snapshots_with_qemu == 2) {
+
+	    my $snapshots = PVE::Storage::volume_snapshot_info($storecfg, $volid);
+
+	    my $currentpath = $snapshots->{current}->{file};
+	    my $snappath = $snapshots->{$snap}->{file};
+	    my $parentsnap = $snapshots->{$snap}->{parent};
+	    die "error: we can't find a parent for this snapshot" if !$parentsnap;
+
+	    my $parentpath = $snapshots->{$parentsnap}->{file};
+	    my $parentformat = $snapshots->{$parentsnap}->{'format'} if $parentsnap;
+
+	    print "block-commit  top:$snappath base:$parentpath\n";
+            my $job_id = "commit-$attached_deviceid";
+	    my $jobs = {};
+	    mon_cmd(
+	        $vmid,
+		'block-commit',
+		'job-id' => $job_id,
+		device => $attached_deviceid,
+		top => $snappath,
+		base => $parentpath,
+	    );
+	    $jobs->{$job_id} = {};
+
+	    #if we delete the current, block-job-complete to finish
+	    my $completion = $currentpath eq $snappath ? 'complete' : 'auto';
+	    qemu_drive_mirror_monitor($vmid, undef, $jobs, $completion, 0, 'commit');
+
+	    my (undef, $snap_volid) = PVE::Storage::path_to_volume_id($storecfg, $snappath);
+	    PVE::Storage::vdisk_free($storecfg, $snap_volid);
+	} else {
+	    mon_cmd(
+	        $vmid,
+		'blockdev-snapshot-delete-internal-sync',
+		device => $attached_deviceid,
+		name => $snap,
+	    );
+	}
     } else {
 	PVE::Storage::volume_snapshot_delete(
 	    $storecfg, $volid, $snap, $attached_deviceid ? 1 : undef);
@@ -7776,6 +7816,8 @@ sub do_snapshots_with_qemu {
 	return 1;
     }
 
+    return 2 if $scfg->{snapext};
+
     if ($volid =~ m/\.(qcow2|qed)$/){
 	return 1;
     }
@@ -7849,8 +7891,16 @@ sub qemu_img_convert {
     if ($src_storeid) {
 	PVE::Storage::activate_volumes($storecfg, [$src_volid], $snapname);
 	my $src_scfg = PVE::Storage::storage_config($storecfg, $src_storeid);
-	$src_format = qemu_img_format($src_scfg, $src_volname);
-	$src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
+	if($src_scfg->{snapext} && $snapname) {
+	    my $snapshots = PVE::Storage::volume_snapshot_info($storecfg, $src_volid);
+	    my $parentsnap = $snapshots->{$snapname}->{parent};
+	    $src_format = $snapshots->{$parentsnap}->{format};
+	    $src_path = $snapshots->{$parentsnap}->{file};
+	    $snapname = undef;
+	} else {
+	    $src_format = qemu_img_format($src_scfg, $src_volname);
+	    $src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
+	}
 	$src_is_iscsi = ($src_path =~ m|^iscsi://|);
 	$cachemode = 'none' if $src_scfg->{type} eq 'zfspool';
     } elsif (-f $src_volid || -b $src_volid) {
-- 
2.39.2



--===============7752927231757229114==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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

--===============7752927231757229114==--