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 CB8641FF15E
	for <inbox@lore.proxmox.com>; Tue, 11 Mar 2025 11:31:04 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 81F65C69F;
	Tue, 11 Mar 2025 11:29:29 +0100 (CET)
To: pve-devel@lists.proxmox.com
Date: Tue, 11 Mar 2025 11:28:53 +0100
In-Reply-To: <20250311102905.2680524-1-alexandre.derumier@groupe-cyllene.com>
References: <20250311102905.2680524-1-alexandre.derumier@groupe-cyllene.com>
MIME-Version: 1.0
Message-ID: <mailman.953.1741688968.293.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 v4 pve-storage 2/5] lvmplugin: add qcow2 snapshot
Content-Type: multipart/mixed; boundary="===============7744954166413211362=="
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

--===============7744954166413211362==
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 CB3F3D5852
	for <pve-devel@lists.proxmox.com>; Tue, 11 Mar 2025 11:29:27 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 7A5DCC4DC
	for <pve-devel@lists.proxmox.com>; Tue, 11 Mar 2025 11:29:25 +0100 (CET)
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))
	(No client certificate requested)
	by firstgate.proxmox.com (Proxmox) with ESMTPS
	for <pve-devel@lists.proxmox.com>; Tue, 11 Mar 2025 11:29:18 +0100 (CET)
Received: from formationkvm1.odiso.net (unknown [10.11.201.57])
	by bastiontest.odiso.net (Postfix) with ESMTP id 0C8AD860944;
	Tue, 11 Mar 2025 11:29:09 +0100 (CET)
Received: by formationkvm1.odiso.net (Postfix, from userid 0)
	id 5D151119292E; Tue, 11 Mar 2025 11:29:09 +0100 (CET)
From: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH v4 pve-storage 2/5] lvmplugin: add qcow2 snapshot
Date: Tue, 11 Mar 2025 11:28:53 +0100
Message-Id: <20250311102905.2680524-6-alexandre.derumier@groupe-cyllene.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250311102905.2680524-1-alexandre.derumier@groupe-cyllene.com>
References: <20250311102905.2680524-1-alexandre.derumier@groupe-cyllene.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=y
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
	AWL                     0.117 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.001 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>
---
 src/PVE/Storage/LVMPlugin.pm | 228 ++++++++++++++++++++++++++++++++---
 1 file changed, 210 insertions(+), 18 deletions(-)

diff --git a/src/PVE/Storage/LVMPlugin.pm b/src/PVE/Storage/LVMPlugin.pm
index 38f7fa1..19dbd7e 100644
--- a/src/PVE/Storage/LVMPlugin.pm
+++ b/src/PVE/Storage/LVMPlugin.pm
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 
 use IO::File;
+use POSIX qw/ceil/;
 
 use PVE::Tools qw(run_command trim);
 use PVE::Storage::Plugin;
@@ -218,6 +219,7 @@ sub type {
 sub plugindata {
     return {
 	content => [ {images => 1, rootdir => 1}, { images => 1 }],
+	format => [ { raw => 1, qcow2 => 1 } , 'raw' ],
     };
 }
 
@@ -293,7 +295,10 @@ sub parse_volname {
     PVE::Storage::Plugin::parse_lvm_name($volname);
 
     if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
-	return ('images', $1, $2, undef, undef, undef, 'raw');
+	my $name = $1;
+	my $vmid = $2;
+	my $format = $volname =~ m/\.qcow2$/ ? 'qcow2' : 'raw';
+	return ('images', $name, $vmid, undef, undef, undef, $format);
     }
 
     die "unable to parse lvm volume name '$volname'\n";
@@ -302,11 +307,13 @@ sub parse_volname {
 sub filesystem_path {
     my ($class, $scfg, $volname, $snapname) = @_;
 
-    die "lvm snapshot is not implemented"if defined($snapname);
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
+	$class->parse_volname($volname);
 
-    my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+    die "snapshot is working with qcow2 format only" if defined($snapname) && $format ne 'qcow2';
 
     my $vg = $scfg->{vgname};
+    $name = $class->get_snap_volname($volname, $snapname) if $snapname;
 
     my $path = "/dev/$vg/$name";
 
@@ -334,7 +341,9 @@ sub find_free_diskname {
 
     my $disk_list = [ keys %{$lvs->{$vg}} ];
 
-    return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg);
+    $add_fmt_suffix = $fmt eq 'qcow2' ? 1 : undef;
+
+    return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
 }
 
 sub lvcreate {
@@ -363,9 +372,9 @@ sub lvrename {
 }
 
 sub alloc_image {
-    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size, $backing) = @_;
 
-    die "unsupported format '$fmt'" if $fmt ne 'raw';
+    die "unsupported format '$fmt'" if $fmt !~ m/(raw|qcow2)/;
 
     die "illegal name '$name' - should be 'vm-$vmid-*'\n"
 	if  $name && $name !~ m/^vm-$vmid-/;
@@ -378,12 +387,36 @@ sub alloc_image {
 
     my $free = int($vgs->{$vg}->{free});
 
+
+    #add extra space for qcow2 metadatas
+    #without sub-allocated clusters : For 1TB storage : l2_size = disk_size × 8 / cluster_size
+    #with sub-allocated clusters : For 1TB storage : l2_size = disk_size × 8 / cluster_size / 16
+                                   #4MB overhead for 1TB with extented l2 clustersize=128k
+
+    #can't use qemu-img measure, because it's not possible to define options like clustersize && extended_l2
+    #verification has been done with : qemu-img create -f qcow2 -o extended_l2=on,cluster_size=128k test.img 1G
+
+    my $qcow2_overhead = ceil($size/1024/1024/1024) * 4096;
+
+    my $lvmsize = $size;
+    $lvmsize += $qcow2_overhead if $fmt eq 'qcow2';
+
     die "not enough free space ($free < $size)\n" if $free < $size;
 
-    $name = $class->find_free_diskname($storeid, $scfg, $vmid)
+    $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
 	if !$name;
 
-    lvcreate($vg, $name, $size, ["pve-vm-$vmid"]);
+    my $tags = ["pve-vm-$vmid"];
+    #tags all snapshots volumes with the main volume tag for easier activation of the whole group
+    push @$tags, "\@pve-$name" if $fmt eq 'qcow2';
+    lvcreate($vg, $name, $lvmsize, $tags);
+
+    if ($fmt eq 'qcow2') {
+	#format the lvm volume with qcow2 format
+	$class->activate_volume($storeid, $scfg, $name, undef, {});
+	my $path = $class->path($scfg, $name, $storeid);
+	PVE::Storage::Plugin::qemu_img_create($scfg, $fmt, $size, $path, $backing);
+    }
 
     return $name;
 }
@@ -538,6 +571,12 @@ sub activate_volume {
 
     my $lvm_activate_mode = 'ey';
 
+    #activate volume && all snapshots volumes by tag
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
+	$class->parse_volname($volname);
+
+    $path = "\@pve-$name" if $format eq 'qcow2';
+
     my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
     run_command($cmd, errmsg => "can't activate LV '$path'");
     $cmd = ['/sbin/lvchange', '--refresh', $path];
@@ -550,6 +589,10 @@ sub deactivate_volume {
     my $path = $class->path($scfg, $volname, $storeid, $snapname);
     return if ! -b $path;
 
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
+	$class->parse_volname($volname);
+    $path = "\@pve-$name" if $format eq 'qcow2';
+
     my $cmd = ['/sbin/lvchange', '-aln', $path];
     run_command($cmd, errmsg => "can't deactivate LV '$path'");
 }
@@ -557,15 +600,27 @@ sub deactivate_volume {
 sub volume_resize {
     my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
 
-    $size = ($size/1024/1024) . "M";
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
+	$class->parse_volname($volname);
+
+    my $lvmsize = $size / 1024;
+    my $qcow2_overhead = ceil($size/1024/1024/1024/1024) * 4096;
+    $lvmsize += $qcow2_overhead if $format eq 'qcow2';
+    $lvmsize = "${lvmsize}k";
 
     my $path = $class->path($scfg, $volname);
-    my $cmd = ['/sbin/lvextend', '-L', $size, $path];
+    my $cmd = ['/sbin/lvextend', '-L', $lvmsize, $path];
 
     $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
 	run_command($cmd, errmsg => "error resizing volume '$path'");
     });
 
+    if(!$running && $format eq 'qcow2') {
+	my $prealloc_opt = PVE::Storage::Plugin::preallocation_cmd_option($scfg, $format);
+	my $cmd = ['/usr/bin/qemu-img', 'resize', "--$prealloc_opt", '-f', $format, $path , $size];
+	run_command($cmd, timeout => 10);
+    }
+
     return 1;
 }
 
@@ -587,30 +642,159 @@ sub volume_size_info {
 sub volume_snapshot {
     my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-    die "lvm snapshot is not implemented";
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
+        $class->parse_volname($volname);
+
+    die "can't snapshot this image format\n" if $format ne 'qcow2';
+
+    $class->activate_volume($storeid, $scfg, $volname, undef, {});
+
+    my $snap_volname = $class->get_snap_volname($volname, $snap);
+    my $snap_path = $class->path($scfg, $volname, $storeid, $snap);
+
+    my $size = $class->volume_size_info($scfg, $storeid, $volname, 5);
+
+    #rename current lvm volume to snap volume
+    my $vg = $scfg->{vgname};
+    print"rename $volname to $snap_volname\n";
+    eval { lvrename($vg, $volname, $snap_volname); };
+    if ($@) {
+	die "can't rename lvm volume from $volname to $snap_volname: $@ \n";
+    }
+
+    eval { $class->alloc_image($storeid, $scfg, $vmid, 'qcow2', $volname, $size/1024, $snap_path); };
+    if ($@) {
+        eval { $class->free_image($storeid, $scfg, $volname, 0) };
+        warn $@ if $@;
+    }
 }
 
+sub volume_rollback_is_possible {
+    my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_;
+
+    my $snap_path = $class->path($scfg, $volname, $storeid, $snap);
+
+    $class->activate_volume($storeid, $scfg, $volname, undef, {});
+    my $snapshots = $class->volume_snapshot_info($scfg, $storeid, $volname);
+    my $parent_snap = $snapshots->{current}->{parent};
+
+    return 1 if $snapshots->{$parent_snap}->{file} eq $snap_path;
+    die "can't rollback, '$snap' is not most recent snapshot on '$volname'\n";
+
+    return 1;
+}
+
+
 sub volume_snapshot_rollback {
     my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-    die "lvm snapshot rollback is not implemented";
+    die "can't rollback snapshot for this image format\n" if $volname !~ m/\.(qcow2)$/;
+
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
+        $class->parse_volname($volname);
+
+    $class->activate_volume($storeid, $scfg, $volname, undef, {});
+    my $size = $class->volume_size_info($scfg, $storeid, $volname, 5);
+    my $snap_path = $class->path($scfg, $volname, $storeid, $snap);
+
+    #simply delete the current snapshot and recreate it
+    eval { $class->free_image($storeid, $scfg, $volname, 0) };
+    if ($@) {
+	die "can't delete old volume $volname: $@\n";
+    }
+
+    eval { $class->alloc_image($storeid, $scfg, $vmid, 'qcow2', $volname, $size/1024, $snap_path) };
+    if ($@) {
+	die "can't allocate new volume $volname: $@\n";
+    }
+
+    return undef;
 }
 
 sub volume_snapshot_delete {
-    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+    my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
+
+   die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2)$/;
+
+   return 1 if $running;
+
+   my $cmd = "";
+   my $path = $class->filesystem_path($scfg, $volname);
+
+   my $snapshots = $class->volume_snapshot_info($scfg, $storeid, $volname);
+   my $snap_path = $snapshots->{$snap}->{file};
+   my $snap_volname = $snapshots->{$snap}->{volname};
+   die "volume $snap_path is missing" if !-e $snap_path;
 
-    die "lvm snapshot delete is not implemented";
+   my $parent_snap = $snapshots->{$snap}->{parent};
+   my $child_snap = $snapshots->{$snap}->{child};
+
+   my $parent_path = $snapshots->{$parent_snap}->{file} if $parent_snap;
+   my $child_path = $snapshots->{$child_snap}->{file} if $child_snap;
+   my $child_volname = $snapshots->{$child_snap}->{volname} if $child_snap;
+
+   #if first snapshot,as it should be bigger,  we merge child, and rename the snapshot to child
+   if(!$parent_snap) {
+	print"commit $child_path\n";
+	$cmd = ['/usr/bin/qemu-img', 'commit', $child_path];
+	eval {	run_command($cmd) };
+	if ($@) {
+	    die "error commiting $child_path to $parent_path: $@\n";
+	}
+	print"delete $child_volname\n";
+	eval { $class->free_image($storeid, $scfg, $child_volname, 0) };
+	if ($@) {
+	    die "error delete old snapshot volume $child_volname: $@\n";
+	}
+	print"rename $snap_volname to $child_volname\n";
+	my $vg = $scfg->{vgname};
+	eval { lvrename($vg, $snap_volname, $child_volname) };
+	if ($@) {
+	    die "error renaming snapshot: $@\n";
+	}
+    } else {
+	#we rebase the child image on the parent as new backing image
+	die "missing parentsnap snapshot to rebase child $child_path\n" if !$parent_path;
+	print "link $child_snap to $parent_snap\n";
+	$cmd = ['/usr/bin/qemu-img', 'rebase', '-b', $parent_path, '-F', 'qcow2', '-f', 'qcow2', $child_path];
+	eval { run_command($cmd) };
+	if ($@) {
+	    die "error rebase $child_path with $parent_path; $@\n";
+	}
+	#delete the snapshot
+	eval { $class->free_image($storeid, $scfg, $snap_volname, 0); };
+	if ($@) {
+	    die "error delete old snapshot volume $snap_volname: $@\n";
+	}
+    }
 }
 
 sub volume_has_feature {
     my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
 
     my $features = {
-	copy => { base => 1, current => 1},
-	rename => {current => 1},
+        copy => {
+            base => { qcow2 => 1, raw => 1},
+            current => { qcow2 => 1, raw => 1},
+            snap => { qcow2 => 1 },
+        },
+        'rename' => {
+            current => { qcow2 => 1, raw => 1},
+        },
+        snapshot => {
+            current => { qcow2 => 1 },
+            snap => { qcow2 => 1 },
+        },
+        template => {
+            current => { qcow2 => 1, raw => 1},
+        },
+	clone => {
+	    base => { qcow2 => 1, raw => 1 },
+	},
     };
 
-    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
+
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
 	$class->parse_volname($volname);
 
     my $key = undef;
@@ -619,7 +803,7 @@ sub volume_has_feature {
     }else{
 	$key =  $isBase ? 'base' : 'current';
     }
-    return 1 if $features->{$feature}->{$key};
+    return 1 if defined($features->{$feature}->{$key}->{$format});
 
     return undef;
 }
@@ -740,4 +924,12 @@ sub rename_volume {
     return "${storeid}:${target_volname}";
 }
 
+sub get_snap_volname {
+    my ($class, $volname, $snapname) = @_;
+
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname);
+    $name = !$snapname || $snapname eq 'current' ? $volname : "snap-$snapname-$name";
+    return $name;
+}
+
 1;
-- 
2.39.5



--===============7744954166413211362==
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

--===============7744954166413211362==--