[pve-devel] [PATCH 09/31] plugin : add find_free_volname
Alexandre Derumier
aderumier at odiso.com
Tue Jan 22 12:36:07 CET 2013
Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
PVE/Storage/Plugin.pm | 45 ++++++++++++++++++++++++++++-----------------
1 file changed, 28 insertions(+), 17 deletions(-)
diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index 6f1cac9..9bdcf0d 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -400,29 +400,16 @@ sub path {
sub alloc_image {
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
- my $imagedir = $class->get_subdir($scfg, 'images');
- $imagedir .= "/$vmid";
-
- mkpath $imagedir;
-
- if (!$name) {
- for (my $i = 1; $i < 100; $i++) {
- my @gr = <$imagedir/vm-$vmid-disk-$i.*>;
- if (!scalar(@gr)) {
- $name = "vm-$vmid-disk-$i.$fmt";
- last;
- }
- }
- }
-
- die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
- if !$name;
+ $name = $class->find_free_volname($storeid, $scfg, $vmid, $fmt);
my (undef, $tmpfmt) = parse_name_dir($name);
die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
if $tmpfmt ne $fmt;
+ my $imagedir = $class->get_subdir($scfg, 'images');
+ $imagedir .= "/$vmid";
+
my $path = "$imagedir/$name";
die "disk image '$path' already exists\n" if -e $path;
@@ -438,6 +425,30 @@ sub alloc_image {
return "$vmid/$name";
}
+sub find_free_volname {
+ my ($class, $storeid, $scfg, $vmid, $fmt) = @_;
+
+ my $name = undef;
+
+ my $imagedir = $class->get_subdir($scfg, 'images');
+ $imagedir .= "/$vmid";
+
+ mkpath $imagedir;
+
+ for (my $i = 1; $i < 100; $i++) {
+ my @gr = <$imagedir/vm-$vmid-disk-$i.*>;
+ if (!scalar(@gr)) {
+ $name = "vm-$vmid-disk-$i.$fmt";
+ last;
+ }
+ }
+
+ die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
+ if !$name;
+
+ return $name;
+}
+
sub free_image {
my ($class, $storeid, $scfg, $volname) = @_;
--
1.7.10.4
More information about the pve-devel
mailing list