[pve-devel] [PATCH 03/48] rbd: add volume_protect

Alexandre Derumier aderumier at odiso.com
Tue Jan 29 17:13:45 CET 2013


We use the rbd protect command to protect a snapshot.
This is mandatory for clone a snapshot.

The rbd volume need to be at format V2

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/Storage/RBDPlugin.pm |   40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
index 7f46329..9a27199 100644
--- a/PVE/Storage/RBDPlugin.pm
+++ b/PVE/Storage/RBDPlugin.pm
@@ -29,7 +29,6 @@ my $rbd_cmd = sub {
 	       '--auth_supported', $scfg->{authsupported}, $op];
 
     push @$cmd, @options if scalar(@options);
-
     return $cmd;
 };
 
@@ -49,6 +48,7 @@ my $rados_cmd = sub {
     return $cmd;
 };
 
+
 sub rbd_ls {
     my ($scfg, $storeid) = @_;
 
@@ -82,11 +82,20 @@ sub rbd_ls {
 }
 
 sub rbd_volume_info {
-    my ($scfg, $storeid, $volname) = @_;
+    my ($scfg, $storeid, $volname, $snap) = @_;
+
+    my $cmd = undef;
+
+    if($snap){
+	$cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
+    }else{
+	$cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
+    }
 
-    my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
     my $size = undef;
     my $parent = undef;
+    my $format = undef;
+    my $protected = undef;
 
     my $parser = sub {
 	my $line = shift;
@@ -95,12 +104,16 @@ sub rbd_volume_info {
 	    $size = $1 * rbd_unittobytes()->{$2} if ($1);
 	} elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
 	    $parent = $2;
+	} elsif ($line =~ m/format:\s(\d+)/) {
+	    $format = $1;
+	} elsif ($line =~ m/protected:\s(\S+)/) {
+	    $protected = 1 if $1 eq "True";
 	}
     };
 
     run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
 
-    return ($size, $parent);
+    return ($size, $parent, $format, $protected);
 }
 
 sub addslashes {
@@ -371,4 +384,23 @@ sub volume_has_feature {
     return undef;
 }
 
+sub volume_protect {
+    my ($class, $scfg, $storeid, $volname, $snap, $read_only) = @_;
+
+    return if !$snap;
+
+    my (undef, undef, $format, $protected) = rbd_volume_info($scfg, $storeid, $volname, , $snap);
+
+    die "rbd image must be at format V2" if $format ne "2";
+
+    my $action = $read_only ? "protect":"unprotect";
+
+    if (($protected && !$read_only) || (!$protected && $read_only)){
+        my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', $action, $volname, '--snap', $snap);
+        run_command($cmd, errmsg => "rbd protect $volname snap $snap' error", errfunc => sub {});
+    }
+}
+
+
+
 1;
-- 
1.7.10.4




More information about the pve-devel mailing list