[pve-devel] [PATCH storage v2 1/3] add vm_diskname helpers (get_next, is_valid)
Stoiko Ivanov
s.ivanov at proxmox.com
Fri Sep 7 15:08:40 CEST 2018
Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
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) = @_;
--
2.11.0
More information about the pve-devel
mailing list