plugin: add get_formats() method and use it instead of default_format()

The LVM plugin can only use qcow2 format when the
'snapshot-as-volume-chain' configuration option is set. The format
information is currently only recorded statically in the plugin data.
This causes issues, for example, restoring a guest volume that uses
qcow2 as a format hint on an LVM storage without the option set will
fail, because the plugin data indicates that qcow2 is supported.
Introduce a dedicated method, so that plugins can indicate what
actually is supported according to the storage configuration.

The implementation for LVM is done in a separate commit.

Remove the now unused default_format() function from Plugin.pm.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
[WB: docs: add missing params, drop =pod line, use !! for bools]
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Fiona Ebner
2025-07-21 14:10:47 +02:00
committed by Wolfgang Bumiller
parent cd7c8e0ce6
commit e9e24973fd
3 changed files with 57 additions and 27 deletions

View File

@ -287,23 +287,6 @@ sub storage_has_feature {
return;
}
sub default_format {
my ($scfg) = @_;
my $type = $scfg->{type};
my $def = $defaultData->{plugindata}->{$type};
my $def_format = 'raw';
my $valid_formats = [$def_format];
if (defined($def->{format})) {
$def_format = $scfg->{format} || $def->{format}->[1];
$valid_formats = [sort keys %{ $def->{format}->[0] }];
}
return wantarray ? ($def_format, $valid_formats) : $def_format;
}
PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
sub verify_path {
@ -640,6 +623,42 @@ sub preallocation_cmd_opt {
# Storage implementation
=head3 get_formats
my $formats = $plugin->get_formats($scfg, $storeid);
my $default_format = $formats->{default};
my $is_valid = !!$formats->{valid}->{$format};
Get information about the supported formats and default format according to the current storage
configuration C<$scfg>. The return value is a hash reference with C<default> mapping to the default
format and C<valid> mapping to a hash reference, where each supported format is present as a key
mapping to C<1>. For example:
{
default => 'raw',
valid => {
'qcow2 => 1,
'raw' => 1,
},
}
=cut
sub get_formats {
my ($class, $scfg, $storeid) = @_;
my $type = $scfg->{type};
my $plugin_data = $defaultData->{plugindata}->{$type};
return { default => 'raw', valid => { raw => 1 } } if !defined($plugin_data->{format});
return {
default => $scfg->{format} || $plugin_data->{format}->[1],
# copy rather than passing direct reference
valid => { $plugin_data->{format}->[0]->%* },
};
}
# called during addition of storage (before the new storage config got written)
# die to abort addition if there are (grave) problems
# NOTE: runs in a storage config *locked* context
@ -1526,8 +1545,8 @@ sub list_images {
my $imagedir = $class->get_subdir($scfg, 'images');
my ($defFmt, $vaidFmts) = default_format($scfg);
my $fmts = join('|', @$vaidFmts);
my $format_info = $class->get_formats($scfg, $storeid);
my $fmts = join('|', sort keys $format_info->{valid}->%*);
my $res = [];