[pve-devel] [PATCH] RBD: snap purge does not support automatic unprotect so list all snapshots and then unprotect and delete them

Stefan Priebe s.priebe at profihost.ag
Wed Nov 16 20:13:03 CET 2016


Signed-off-by: Stefan Priebe <s.priebe at profihost.ag>
---
 PVE/Storage/RBDPlugin.pm | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
index c1f88e4..09562fe 100644
--- a/PVE/Storage/RBDPlugin.pm
+++ b/PVE/Storage/RBDPlugin.pm
@@ -7,6 +7,7 @@ use Net::IP;
 use PVE::Tools qw(run_command trim);
 use PVE::Storage::Plugin;
 use PVE::JSONSchema qw(get_standard_option);
+use JSON;
 
 use base qw(PVE::Storage::Plugin);
 
@@ -461,21 +462,14 @@ sub free_image {
     my ($vtype, $name, $vmid, undef, undef, undef) =
 	$class->parse_volname($volname);
 
-    if ($isBase) {
-	my $snap = '__base__';
-	my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
-	if ($protected){
-	    my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
-	    run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
-	}
+    my $snaplist = volume_snapshot_ls($class, $storeid, $scfg, $volname);
+    foreach my $snap (@$snaplist) {
+	$class->volume_snapshot_delete($scfg, $storeid, $volname, $snap->{name}, 0);
     }
 
     $class->deactivate_volume($storeid, $scfg, $volname);
 
-    my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge',  $name);
-    run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
-
-    $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
+    my $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
     run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
 
     return undef;
@@ -623,6 +617,26 @@ sub volume_snapshot {
     return undef;
 }
 
+sub volume_snapshot_ls {
+    my ($class, $storeid, $scfg, $volname) = @_;
+
+    my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+
+    my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
+
+    my $out = "";
+    run_rbd_command($cmd, errmsg => "rbd error", outfunc => sub {$out.=shift});
+
+    # pvedaemon runs with -T switch so every output comming from a command
+    # which may be passed to a new command needs to be regex validated
+    # this (^(.*)$) is very ugly but no better idea
+    $out =~ m/^(.*)$/s;
+    my $list = decode_json($1);
+
+    return $list;
+}
+
+
 sub volume_snapshot_rollback {
     my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-- 
2.1.4




More information about the pve-devel mailing list