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

Alexandre Derumier aderumier at odiso.com
Tue Jan 8 15:45:09 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 |   42 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 5 deletions(-)

diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
index 948be0d..b08fe54 100644
--- a/PVE/Storage/RBDPlugin.pm
+++ b/PVE/Storage/RBDPlugin.pm
@@ -21,7 +21,6 @@ my $rbd_cmd = sub {
 	       '--auth_supported', $scfg->{authsupported}, $op];
 
     push @$cmd, @options if scalar(@options);
-
     return $cmd;
 };
 
@@ -41,6 +40,7 @@ my $rados_cmd = sub {
     return $cmd;
 };
 
+
 sub rbd_ls {
     my ($scfg, $storeid) = @_;
 
@@ -74,19 +74,32 @@ 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;
-
 	if ($line =~ m/size (\d+) MB in (\d+) objects/) {
 	    $size = $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";
 	}
     };
 
@@ -94,7 +107,7 @@ sub rbd_volume_info {
 
     $size = $size*1024*1024 if $size;
 
-    return ($size, $parent);
+    return ($size, $parent, $format, $protected);
 }
 
 sub addslashes {
@@ -365,4 +378,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