[pve-devel] [PATCH 10/20] rbd : add find_free_volname

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


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

diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
index eaff83c..0f4a66f 100644
--- a/PVE/Storage/RBDPlugin.pm
+++ b/PVE/Storage/RBDPlugin.pm
@@ -209,24 +209,31 @@ sub alloc_image {
     die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
 	if  $name && $name !~ m/^vm-$vmid-/;
 
-    if (!$name) {
-	my $rdb = rbd_ls($scfg, $storeid);
-
-	for (my $i = 1; $i < 100; $i++) {
-	    my $tn = "vm-$vmid-disk-$i";
-	    if (!defined ($rdb->{$scfg->{pool}}->{$tn})) {
-		$name = $tn;
-		last;
-	    }
+    $name = $class->find_free_volname($storeid, $scfg, $vmid, $fmt);
+
+    my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name);
+    run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
+
+    return $name;
+}
+
+sub find_free_volname {
+    my ($class, $storeid, $scfg, $vmid, $fmt) = @_;
+
+    my $name = undef;
+    my $rdb = rbd_ls($scfg, $storeid);
+
+    for (my $i = 1; $i < 100; $i++) {
+	my $tn = "vm-$vmid-disk-$i";
+	if (!defined ($rdb->{$scfg->{pool}}->{$tn})) {
+	    $name = $tn;
+	    last;
 	}
     }
 
     die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
 	if !$name;
 
-    my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name);
-    run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
-
     return $name;
 }
 
-- 
1.7.10.4




More information about the pve-devel mailing list