[pve-devel] [PATCH 05/44] rbd : find_free_diskname

Alexandre Derumier aderumier at odiso.com
Tue Feb 5 12:55:32 CET 2013


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

diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
index 1c01f0e..7c7bb2c 100644
--- a/PVE/Storage/RBDPlugin.pm
+++ b/PVE/Storage/RBDPlugin.pm
@@ -206,6 +206,29 @@ sub clone_image {
     die "not implemented";
 }
 
+my $find_free_diskname = sub {
+    my ($storeid, $scfg, $vmid) = @_;
+
+    my $rbd = rbd_ls($scfg, $storeid);
+    my $disk_ids = {};
+    my $dat = $rbd->{$scfg->{pool}};
+
+    foreach my $image (keys %$dat) {
+	my $volname = $dat->{$image}->{name};
+	if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
+	    $disk_ids->{$2} = 1;
+	}
+    }
+    #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
+    for (my $i = 1; $i < 100; $i++) {
+        if (!$disk_ids->{$i}) {
+            return "vm-$vmid-disk-$i";
+        }
+    } 
+
+    die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
+};
+
 sub alloc_image {
     my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
 
@@ -213,20 +236,7 @@ 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;
-	    }
-	}
-    }
-
-    die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
-	if !$name;
+    $name = &$find_free_diskname($storeid, $scfg, $vmid);
 
     my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--format' , 2, '--size', ($size/1024), $name);
     run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
-- 
1.7.10.4




More information about the pve-devel mailing list