Use a common interface for find_free_diskname

We can use 'list_images' to get the desired volume IDs in
'find_free_diskname' for most plugins. For the two LVM plugins, 'list_images'
potentially skips untagged volumes, so we keep the custom version. For the
RBD plugin, 'list_images' is much more costly than the custom version, so we
keep the custom version.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
This commit is contained in:
Fabian Ebner
2019-12-11 10:25:49 +01:00
committed by Thomas Lamprecht
parent a573f66a2e
commit a44c0147bc
7 changed files with 30 additions and 50 deletions

View File

@ -572,18 +572,19 @@ sub get_next_vm_diskname {
die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
}
my $find_free_diskname = sub {
my ($imgdir, $vmid, $fmt, $scfg) = @_;
sub find_free_diskname {
my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
my $disks = $class->list_images($storeid, $scfg, $vmid);
my $disk_list = [];
if (defined(my $dh = IO::Dir->new($imgdir))) {
@$disk_list = $dh->read();
$dh->close();
foreach my $disk (@{$disks}) {
push @{$disk_list}, $disk->{volid};
}
return get_next_vm_diskname($disk_list, $imgdir, $vmid, $fmt, $scfg, 1);
};
return get_next_vm_diskname($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
}
sub clone_image {
my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
@ -607,7 +608,7 @@ sub clone_image {
mkpath $imagedir;
my $name = $find_free_diskname->($imagedir, $vmid, "qcow2", $scfg);
my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
@ -639,7 +640,7 @@ sub alloc_image {
mkpath $imagedir;
$name = $find_free_diskname->($imagedir, $vmid, $fmt, $scfg) if !$name;
$name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
my (undef, $tmpfmt) = parse_name_dir($name);