[pve-devel] [PATCH 21/44] sheepdog : find_free_diskname

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


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

diff --git a/PVE/Storage/SheepdogPlugin.pm b/PVE/Storage/SheepdogPlugin.pm
index b94c79e..2076002 100644
--- a/PVE/Storage/SheepdogPlugin.pm
+++ b/PVE/Storage/SheepdogPlugin.pm
@@ -144,6 +144,29 @@ sub path {
     return ($path, $vmid, $vtype);
 }
 
+my $find_free_diskname = sub {
+    my ($storeid, $scfg, $vmid) = @_;
+
+    my $sheepdog = sheepdog_ls($scfg, $storeid);
+    my $dat = $sheepdog->{sheepdog};
+    my $disk_ids = {};
+
+    foreach my $image (keys %$dat) {
+	my $volname = $dat->{$image}->{name};
+	if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
+	    $disk_ids->{$2} = 1;
+	}
+    }
+
+    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 create_base {
     my ($class, $storeid, $scfg, $volname) = @_;
 
@@ -159,24 +182,10 @@ sub clone_image {
 sub alloc_image {
     my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
 
-
     die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
 	if  $name && $name !~ m/^vm-$vmid-/;
 
-    if (!$name) {
-	my $sheepdog = sheepdog_ls($scfg, $storeid);
-
-	for (my $i = 1; $i < 100; $i++) {
-	    my $tn = "vm-$vmid-disk-$i";
-	    if (!defined ($sheepdog->{$storeid}->{$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 = &$collie_cmd($scfg, 'vdi', 'create', $name , "${size}KB");
 
-- 
1.7.10.4




More information about the pve-devel mailing list