[pve-devel] [PATCH storage 3/3] rbd: add protect/unprotect helpers

Fabian Grünbichler f.gruenbichler at proxmox.com
Wed Apr 23 15:59:04 CEST 2025


this is a bit repetitive otherwise, no functional changes intended.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 src/PVE/Storage/RBDPlugin.pm | 55 ++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 28 deletions(-)

diff --git a/src/PVE/Storage/RBDPlugin.pm b/src/PVE/Storage/RBDPlugin.pm
index 3bb5807..6604bc3 100644
--- a/src/PVE/Storage/RBDPlugin.pm
+++ b/src/PVE/Storage/RBDPlugin.pm
@@ -96,7 +96,7 @@ my $rbd_cmd = sub {
 	my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
 	push $cmd->@*, '-p', $pool;
 	if (defined($scfg->{namespace})) {
-	    push @$cmd, '--namespace', $cfg->{namespace};
+	    push @$cmd, '--namespace', $scfg->{namespace};
 	}
     }
 
@@ -262,6 +262,25 @@ sub rbd_ls_snap {
     return $res;
 }
 
+my sub rbd_protect_snap {
+    my ($scfg, $storeid, $image, $snap) = @_;
+    my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $image, $snap);
+
+    if (!$protected){
+	my $snap_spec = get_rbd_path($scfg, $image, $snap);
+	my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $snap_spec);
+	run_rbd_command($cmd, errmsg => "rbd protect '$snap_spec' error");
+    }
+}
+
+my sub rbd_unprotect_snap {
+    my ($scfg, $storeid, $image, $snap) = @_;
+
+    my $snap_spec = get_rbd_path($scfg, $image, $snap);
+    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $snap_spec);
+    run_rbd_command($cmd, errmsg => "rbd unprotect '$snap_spec' error");
+}
+
 sub rbd_volume_info {
     my ($scfg, $storeid, $volname, $snap) = @_;
 
@@ -552,13 +571,7 @@ sub create_base {
 
     $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
 
-    my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
-
-    if (!$protected){
-	my $snap_spec = get_rbd_path($scfg, $newname, $snap);
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $snap_spec);
-	run_rbd_command($cmd, errmsg => "rbd protect '$snap_spec' error");
-    }
+    rbd_protect_snap($scfg, $storeid, $newname, $snap);
 
     return $newvolname;
 
@@ -580,15 +593,7 @@ sub clone_image {
 
     warn "clone $volname: $basename snapname $snap to $name\n";
 
-    if (length($snapname)) {
-	my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
-
-	if (!$protected) {
-	    my $snap_spec = get_rbd_path($scfg, $volname, $snapname);
-	    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $snap_spec);
-	    run_rbd_command($cmd, errmsg => "rbd protect '$snap_spec' error");
-	}
-    }
+    rbd_protect_snap($scfg, $storeid, $volname, $snap) if length($snapname);
 
     my $newvol = "$basename/$name";
     $newvol = $name if length($snapname);
@@ -634,11 +639,8 @@ sub free_image {
     my $image_spec = get_rbd_path($scfg, $name);
     my $snaps = rbd_ls_snap($scfg, $storeid, $image_spec);
     foreach my $snap (keys %$snaps) {
-	if ($snaps->{$snap}->{protected}) {
-	    my $snap_spec = get_rbd_path($scfg, $name, $snap);
-	    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $snap_spec);
-	    run_rbd_command($cmd, errmsg => "rbd unprotect $snap_spec error");
-	}
+	rbd_unprotect_snap($scfg, $storeid, $name, $snap)
+	    if $snaps->{$snap}->{protected};
     }
 
     $class->deactivate_volume($storeid, $scfg, $volname);
@@ -821,13 +823,10 @@ sub volume_snapshot_delete {
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
     my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
+
+    rbd_unprotect_snap($scfg, $storeid, $volname, $snap) if $protected;
+
     my $snap_spec = get_rbd_path($scfg, $volname, $snap);
-
-    if ($protected){
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $snap_spec);
-	run_rbd_command($cmd, errmsg => "rbd unprotect '$snap_spec' error");
-    }
-
     my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', $snap_spec);
 
     run_rbd_command($cmd, errmsg => "rbd snapshot delete '$snap_spec' error");
-- 
2.39.5





More information about the pve-devel mailing list