]> git.proxmox.com Git - pve-storage.git/commitdiff
nexenta : find_free_diskname
authorAlexandre Derumier <aderumier@odiso.com>
Tue, 5 Feb 2013 11:56:01 +0000 (12:56 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 14 Feb 2013 09:26:05 +0000 (10:26 +0100)
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
PVE/Storage/NexentaPlugin.pm

index b24a8e09bd68848ea0e68e1930f595aa73e214ee..a39faf4794e1db0074ff5961ea302f627c38fd19 100644 (file)
@@ -210,6 +210,34 @@ sub path {
     return ($path, $vmid, $vtype);
 }
 
+my $find_free_diskname = sub {
+    my ($storeid, $scfg, $vmid) = @_;
+
+    my $name = undef;
+    my $volumes = nexenta_list_zvol($scfg);
+       die "unable de get zvol list" if !$volumes;
+
+    my $disk_ids = {};
+    my $dat = $volumes->{$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 create_base {
     my ($class, $storeid, $scfg, $volname) = @_;
 
@@ -228,25 +256,9 @@ sub alloc_image {
     die "unsupported format '$fmt'" if $fmt ne 'raw';
 
     die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
-       if $name && $name !~ m/^vm-$vmid-/;
-
-    my $nexentapool = $scfg->{'pool'};
-
-    if (!$name) {
-       my $volumes = nexenta_list_zvol($scfg);
-       die "unable de get zvol list" if !$volumes;
+        if $name && $name !~ m/^vm-$vmid-/;
 
-       for (my $i = 1; $i < 100; $i++) {
-           my $tn = "vm-$vmid-disk-$i";
-           if (!defined ($volumes->{$nexentapool}->{$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);
 
     nexenta_create_zvol($scfg, $name, $size);
     nexenta_create_lu($scfg, $name);