public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier via pve-devel <pve-devel@lists.proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
Subject: [pve-devel] [PATCH pve-storage 4/5] storage: vdisk_free: remove external snapshots
Date: Tue, 22 Apr 2025 13:51:30 +0200	[thread overview]
Message-ID: <mailman.34.1745322752.394.pve-devel@lists.proxmox.com> (raw)
In-Reply-To: <20250422115141.808427-1-alexandre.derumier@groupe-cyllene.com>

[-- Attachment #1: Type: message/rfc822, Size: 9252 bytes --]

From: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH pve-storage 4/5] storage: vdisk_free: remove external snapshots
Date: Tue, 22 Apr 2025 13:51:30 +0200
Message-ID: <20250422115141.808427-10-alexandre.derumier@groupe-cyllene.com>

add a $include_snapshots param to free_image to
remove the whole chain of snapshots when deleting the main image.

Signed-off-by: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
---
 src/PVE/Storage.pm           |  2 +-
 src/PVE/Storage/LVMPlugin.pm | 72 ++++++++++++++++++++++++------------
 src/PVE/Storage/Plugin.pm    | 27 +++++++++++---
 3 files changed, 71 insertions(+), 30 deletions(-)

diff --git a/src/PVE/Storage.pm b/src/PVE/Storage.pm
index db9d190..55a9a43 100755
--- a/src/PVE/Storage.pm
+++ b/src/PVE/Storage.pm
@@ -1059,7 +1059,7 @@ sub vdisk_free {
 
 	my (undef, undef, undef, undef, undef, $isBase, $format) =
 	    $plugin->parse_volname($volname);
-	$cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
+	$cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format, 1);
     });
 
     return if !$cleanup_worker;
diff --git a/src/PVE/Storage/LVMPlugin.pm b/src/PVE/Storage/LVMPlugin.pm
index 8ee337a..b20fe98 100644
--- a/src/PVE/Storage/LVMPlugin.pm
+++ b/src/PVE/Storage/LVMPlugin.pm
@@ -463,10 +463,34 @@ sub alloc_snap_image {
 }
 
 sub free_image {
-    my ($class, $storeid, $scfg, $volname, $isBase) = @_;
+    my ($class, $storeid, $scfg, $volname, $isBase, $format, $include_snapshots) = @_;
 
     my $vg = $scfg->{vgname};
 
+    my $name = ($class->parse_volname($volname))[1];
+
+    #activate volumes && snapshot volumes
+    my $path = $class->path($scfg, $volname, $storeid);
+    $path = "\@pve-$name" if $format && $format eq 'qcow2';
+    my $cmd = ['/sbin/lvchange', '-aly', $path];
+    run_command($cmd, errmsg => "can't activate LV '$path' to zero-out its data");
+    $cmd = ['/sbin/lvchange', '--refresh', $path];
+    run_command($cmd, errmsg => "can't refresh LV '$path' to zero-out its data");
+
+    my $volnames = [];
+    if ($include_snapshots) {
+	my $snapshots =  $class->volume_snapshot_info($scfg, $storeid, $volname);
+	for my $snapid (sort { $snapshots->{$b}->{order} <=> $snapshots->{$a}->{order} } keys %$snapshots) {
+	    my $snap = $snapshots->{$snapid};
+	    next if $snapid eq 'current';
+	    next if !$snap->{volid};
+	    next if !$snap->{ext};
+	    my ($snap_storeid, $snap_volname) = PVE::Storage::parse_volume_id($snap->{volid});
+	    push @$volnames, $snap_volname;
+	}
+    }
+    push @$volnames, $volname;
+
     # we need to zero out LVM data for security reasons
     # and to allow thin provisioning
 
@@ -478,40 +502,40 @@ sub free_image {
 	if ($scfg->{saferemove_throughput}) {
 		$throughput = $scfg->{saferemove_throughput};
 	}
-
-	my $cmd = [
+	for my $name (@$volnames) {
+	    my $cmd = [
 		'/usr/bin/cstream',
 		'-i', '/dev/zero',
-		'-o', "/dev/$vg/del-$volname",
+		'-o', "/dev/$vg/del-$name",
 		'-T', '10',
 		'-v', '1',
 		'-b', '1048576',
 		'-t', "$throughput"
-	];
-	eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); };
-	warn $@ if $@;
-
-	$class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
-	    my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
-	    run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error");
-	});
-	print "successfully removed volume $volname ($vg/del-$volname)\n";
+	    ];
+	    eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); };
+	    warn $@ if $@;
+
+	    $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
+		my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$name"];
+		run_command($cmd, errmsg => "lvremove '$vg/del-$name' error");
+	    });
+	    print "successfully removed volume $name ($vg/del-$name)\n";
+	}
     };
 
-    my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
-    run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data");
-    $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
-    run_command($cmd, errmsg => "can't refresh LV '$vg/$volname' to zero-out its data");
-
     if ($scfg->{saferemove}) {
-	# avoid long running task, so we only rename here
-	$cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
-	run_command($cmd, errmsg => "lvrename '$vg/$volname' error");
+	for my $name (@$volnames) {
+	    # avoid long running task, so we only rename here
+	    my $cmd = ['/sbin/lvrename', $vg, $name, "del-$name"];
+	    run_command($cmd, errmsg => "lvrename '$vg/$name' error");
+	}
 	return $zero_out_worker;
     } else {
-	my $tmpvg = $scfg->{vgname};
-	$cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
-	run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error");
+	for my $name (@$volnames) {
+	    my $tmpvg = $scfg->{vgname};
+	    my $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$name"];
+	    run_command($cmd, errmsg => "lvremove '$tmpvg/$name' error");
+	}
     }
 
     return undef;
diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index 3f83fae..0319ab2 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -959,7 +959,7 @@ sub alloc_snap_image {
 }
 
 sub free_image {
-    my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
+    my ($class, $storeid, $scfg, $volname, $isBase, $format, $include_snapshots) = @_;
 
     die "cannot remove protected volume '$volname' on '$storeid'\n"
 	if $class->get_volume_attribute($scfg, $storeid, $volname, 'protected');
@@ -975,12 +975,29 @@ sub free_image {
     if (defined($format) && ($format eq 'subvol')) {
 	File::Path::remove_tree($path);
     } else {
-	if (!(-f $path || -l $path)) {
-	    warn "disk image '$path' does not exist\n";
-	    return undef;
+
+	my $volnames = [];
+	if ($include_snapshots) {
+ 	    my $snapshots =  $class->volume_snapshot_info($scfg, $storeid, $volname);
+	    for my $snapid (sort { $snapshots->{$b}->{order} <=> $snapshots->{$a}->{order} } keys %$snapshots) {
+ 		my $snap = $snapshots->{$snapid};
+		next if $snapid eq 'current';
+		next if !$snap->{volid};
+		next if !$snap->{ext};
+		my ($snap_storeid, $snap_volname) = PVE::Storage::parse_volume_id($snap->{volid});
+		push @$volnames, $snap_volname;
+	    }
 	}
+	push @$volnames, $volname;
 
-	unlink($path) || die "unlink '$path' failed - $!\n";
+	for my $name (@$volnames) {
+	    my $path = $class->filesystem_path($scfg, $name);
+	    if (!(-f $path || -l $path)) {
+		warn "disk image '$path' does not exist\n";
+	    } else {
+		unlink($path) || die "unlink '$path' failed - $!\n";
+	    }
+	}
     }
 
     # try to cleanup directory to not clutter storage with empty $vmid dirs if
-- 
2.39.5



[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

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

  parent reply	other threads:[~2025-04-22 11:53 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250422115141.808427-1-alexandre.derumier@groupe-cyllene.com>
2025-04-22 11:51 ` [pve-devel] [PATCH pve-qemu 1/1] add block-commit-replaces option patch Alexandre Derumier via pve-devel
2025-05-06  9:00   ` Fiona Ebner
2025-05-06  9:19     ` DERUMIER, Alexandre via pve-devel
2025-05-06 13:35     ` DERUMIER, Alexandre via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH pve-storage 1/5] rename_volume: add source && target snap Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 01/14] tests: add cfg2cmd for disk passthrough, rbd, krbd && zfs-over-scsi Alexandre Derumier via pve-devel
2025-05-06  9:40   ` [pve-devel] applied: " Fiona Ebner
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 02/14] blockdev: cmdline: convert drive to blockdev syntax Alexandre Derumier via pve-devel
2025-05-06 11:12   ` Fiona Ebner
2025-05-06 14:20     ` DERUMIER, Alexandre via pve-devel
     [not found]     ` <c41fa01bb76db97a0e496255992abb33c292db78.camel@groupe-cyllene.com>
2025-05-08 11:27       ` Fiona Ebner
2025-05-06 12:57   ` Fiona Ebner
2025-05-06 14:48     ` DERUMIER, Alexandre via pve-devel
2025-05-06 15:40       ` DERUMIER, Alexandre via pve-devel
     [not found]       ` <3534d9cd994e60ca891cb5ad443ff572e387c33c.camel@groupe-cyllene.com>
2025-05-08 11:21         ` Fiona Ebner
2025-05-09  8:20           ` DERUMIER, Alexandre via pve-devel
     [not found]           ` <0e129451ee74c8e13d8f3087ff3edf52efb1c220.camel@groupe-cyllene.com>
2025-05-09  9:24             ` Fiona Ebner
2025-05-12 15:33               ` DERUMIER, Alexandre via pve-devel
     [not found]               ` <3f363e6e281acb4abadee5cc521a313c4c815a1f.camel@groupe-cyllene.com>
2025-05-13  7:17                 ` Fiona Ebner
2025-05-07  8:41     ` Fabian Grünbichler
2025-05-08 11:09       ` Fiona Ebner
2025-04-22 11:51 ` [pve-devel] [PATCH pve-storage 2/5] qcow2: add external snapshot support Alexandre Derumier via pve-devel
2025-05-09 10:30   ` Fabian Grünbichler
2025-05-14 13:01   ` Fabian Grünbichler
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 03/14] blockdev: convert ovmf && efidisk to blockdev Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH pve-storage 3/5] lvmplugin: add qcow2 snapshot Alexandre Derumier via pve-devel
2025-05-09 10:30   ` Fabian Grünbichler
2025-05-13  9:54   ` Fabian Grünbichler
2025-05-13 18:13     ` DERUMIER, Alexandre via pve-devel
     [not found]     ` <60d45a0673902097185cbb909a47ac7f8868016d.camel@groupe-cyllene.com>
2025-05-13 18:37       ` DERUMIER, Alexandre via pve-devel
     [not found]       ` <3f47953b87cda70c49c1c33104c0aa8e966173ff.camel@groupe-cyllene.com>
2025-05-14  7:05         ` DERUMIER, Alexandre via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 04/14] blockdev : convert qemu_driveadd && qemu_drivedel Alexandre Derumier via pve-devel
2025-04-22 11:51 ` Alexandre Derumier via pve-devel [this message]
2025-05-09 10:29   ` [pve-devel] [PATCH pve-storage 4/5] storage: vdisk_free: remove external snapshots Fabian Grünbichler
2025-05-10 12:28     ` DERUMIER, Alexandre via pve-devel
     [not found]     ` <5ce9a098f67adeb61244c597d610802e318494bf.camel@groupe-cyllene.com>
2025-05-13 12:06       ` Fabian Grünbichler
2025-05-13 17:57         ` DERUMIER, Alexandre via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 05/14] replace qemu_block_set_io_throttle with qom-set throttlegroup limits Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH pve-storage 5/5] volume_has_feature: return storage|qemu_internal|qemu_external snapshot_type Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 06/14] blockdev: vm_devices_list : fix block-query Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 07/14] blockdev: convert cdrom media eject/insert Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 08/14] blockdev: block_resize: convert to blockdev Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 09/14] blockdev: nbd_export: block-export-add : use drive-$id for nodename Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 10/14] blockdev: convert drive_mirror to blockdev_mirror Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 11/14] blockdev: change aio on target if io_uring is not default Alexandre Derumier via pve-devel
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 12/14] qemu_img convert : add external snapshot support Alexandre Derumier via pve-devel
2025-05-09 10:30   ` Fabian Grünbichler
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 13/14] blockdev: add backing_chain support Alexandre Derumier via pve-devel
2025-05-09 10:30   ` Fabian Grünbichler
2025-04-22 11:51 ` [pve-devel] [PATCH qemu-server 14/14] qcow2: add external snapshot support Alexandre Derumier via pve-devel
2025-05-09 10:30   ` Fabian Grünbichler
2025-05-13 10:11   ` Fabian Grünbichler
2025-05-13 10:48   ` Fabian Grünbichler
2025-05-13 18:02     ` DERUMIER, Alexandre via pve-devel
2025-05-14 10:45     ` DERUMIER, Alexandre via pve-devel
     [not found]     ` <7a7870acf85fdab270549692e05bf436a74c6f3c.camel@groupe-cyllene.com>
2025-05-14 12:14       ` Fabian Grünbichler
2025-05-14 12:56         ` DERUMIER, Alexandre via pve-devel

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=mailman.34.1745322752.394.pve-devel@lists.proxmox.com \
    --to=pve-devel@lists.proxmox.com \
    --cc=alexandre.derumier@groupe-cyllene.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