From 345f8981087fef2282cfbd8bdbb89eb602a7360f Mon Sep 17 00:00:00 2001 From: Stoiko Ivanov Date: Fri, 7 Sep 2018 15:08:40 +0200 Subject: [PATCH] add vm_diskname helpers (get_next, is_valid) Signed-off-by: Stoiko Ivanov --- PVE/Storage/Plugin.pm | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm index 7db3a95..32acf69 100644 --- a/PVE/Storage/Plugin.pm +++ b/PVE/Storage/Plugin.pm @@ -523,6 +523,49 @@ sub create_base { return $newvolname; } +sub is_valid_vm_diskname { + my ($disk_name, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_; + + $vmid = qr/\d+/ if !defined($vmid); + + my $suffix = (defined($fmt) && $add_fmt_suffix) ? ".$fmt" : ''; + + my $type = $scfg->{type}; + my $def = $defaultData->{plugindata}->{$type}; + my $valid_formats = $def->{format}[0]; + + my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/; + $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/ + if $valid_formats->{subvol}; + + if($disk_name =~ m/$disk_regex/){ + return wantarray ? (1, $2) : 1; + } +} + +sub get_next_vm_diskname { + my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_; + + my $disk_ids = {}; + my ($match, $disknum); + foreach my $disk (@$disk_list) { + ($match, $disknum) = is_valid_vm_diskname($disk, $scfg, $vmid, $fmt, $add_fmt_suffix); + $disk_ids->{$disknum} = 1 if $match; + } + + $fmt //= ''; + my $prefix = ($fmt eq 'subvol') ? 'subvol' : 'vm'; + my $suffix = $add_fmt_suffix ? ".$fmt" : ''; + + for (my $i = 1; $i < 100; $i++) { + if (!$disk_ids->{$i}) { + return "$prefix-$vmid-disk-$i$suffix"; + } + } + + die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" +} + my $find_free_diskname = sub { my ($imgdir, $vmid, $fmt) = @_;