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

Alexandre Derumier aderumier at odiso.com
Tue Dec 4 12:20:17 CET 2012


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 60bd4e8..eaff83c 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) = @_;
 
@@ -75,19 +75,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";
 	}
     };
 
@@ -95,7 +108,7 @@ sub rbd_volume_info {
 
     $size = $size*1024*1024 if $size;
 
-    return ($size, $parent);
+    return ($size, $parent, $format, $protected);
 }
 
 sub addslashes {
@@ -352,4 +365,23 @@ sub volume_snapshot_delete {
     return undef;
 }
 
+sub volume_protect {
+    my ($class, $scfg, $storeid, $volname, $snap, $read_only) = @_;
+
+    die "missing snapshot name" 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