[pve-devel] [PATCH 13/48] lvm : add find_free_volname

Alexandre Derumier aderumier at odiso.com
Tue Jan 29 17:13:55 CET 2013


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/Storage/LVMPlugin.pm |   28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/PVE/Storage/LVMPlugin.pm b/PVE/Storage/LVMPlugin.pm
index 21d3ec9..f68ae73 100644
--- a/PVE/Storage/LVMPlugin.pm
+++ b/PVE/Storage/LVMPlugin.pm
@@ -242,9 +242,8 @@ sub alloc_image {
     die "illegal name '$name' - sould be 'vm-$vmid-*'\n" 
 	if  $name && $name !~ m/^vm-$vmid-/;
 
-    my $vgs = lvm_vgs();
-
     my $vg = $scfg->{vgname};
+    my $vgs = lvm_vgs();
 
     die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
 
@@ -252,11 +251,30 @@ sub alloc_image {
 
     die "not enough free space ($free < $size)\n" if $free < $size;
 
+    $name = $class->find_free_volname($storeid, $scfg, $vmid, $fmt);
+
+    my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
+
+    run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error");
+
+    return $name;
+}
+
+sub find_free_volname {
+    my ($class, $storeid, $scfg, $vmid, $fmt, $type) = @_;
+
+    $type = 'vm' if !$type;
+
+    my $name = undef;
+    my $vgs = lvm_vgs();
+
+    my $vg = $scfg->{vgname};
+
     if (!$name) {
 	my $lvs = lvm_lvs($vg);
 
 	for (my $i = 1; $i < 100; $i++) {
-	    my $tn = "vm-$vmid-disk-$i";
+	    my $tn = "$type-$vmid-disk-$i";
 	    if (!defined ($lvs->{$vg}->{$tn})) {
 		$name = $tn;
 		last;
@@ -267,10 +285,6 @@ sub alloc_image {
     die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
 	if !$name;
 
-    my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
-
-    run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error");
-
     return $name;
 }
 
-- 
1.7.10.4




More information about the pve-devel mailing list