[pve-devel] applied: [PATCH pve-storage v3] storage plugin: new list_volumes plugin method
Fabian Grünbichler
f.gruenbichler at proxmox.com
Fri Jul 19 14:12:41 CEST 2019
applied with whitespace cleanup and the following follow-up to unbreak it
for storages that don't have a 'path' property:
diff -w --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index e075a1d..08ec749 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -954,11 +954,12 @@ sub list_volumes {
foreach my $ct (@$content_types) {
my $data;
- my $path = $class->get_subdir($scfg, $ct);
-
if ($ct eq 'images') {
$data = $class->list_images($storeid, $scfg, $vmid);
- } elsif ($ct eq 'iso' && !defined($vmid)) {
+ } elsif ($scfg->{path}) {
+ my $path = $class->get_subdir($scfg, $ct);
+
+ if ($ct eq 'iso' && !defined($vmid)) {
$data = $get_subdir_files->($storeid, $path, 'iso');
} elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
$data = $get_subdir_files->($storeid, $path, 'vztmpl');
@@ -967,6 +968,7 @@ sub list_volumes {
} elsif ($ct eq 'snippets') {
$data = $get_subdir_files->($storeid, $path, 'snippets');
}
+ }
next if !$data;
On Fri, Jul 19, 2019 at 11:56:16AM +0200, Dietmar Maurer wrote:
> This cleanup improve code reuse, and allows plugins to override list_volumes.
> ---
>
> Changes in v3:
>
> - keep template_list compatible with previous behavior
> - rename $template_list into $get_subdir_files
>
> Changes in v2:
>
> - remove debug statements
> - move implementaion to Plugin.pm for max. compatibility with old code
> - cleanup regex (use i flag)
> - bump APIVER an d APIAGE
> - sort result inside volume_list (for all plugins)
> - only list supported/enabled content
>
> PVE/Storage.pm | 175 +++++++++------------------------------
> PVE/Storage/DirPlugin.pm | 4 +-
> PVE/Storage/Plugin.pm | 77 +++++++++++++++++
> 3 files changed, 117 insertions(+), 139 deletions(-)
>
> diff --git a/PVE/Storage.pm b/PVE/Storage.pm
> index 5925c69..67a9a29 100755
> --- a/PVE/Storage.pm
> +++ b/PVE/Storage.pm
> @@ -36,11 +36,11 @@ use PVE::Storage::ZFSPlugin;
> use PVE::Storage::DRBDPlugin;
>
> # Storage API version. Icrement it on changes in storage API interface.
> -use constant APIVER => 2;
> +use constant APIVER => 3;
> # Age is the number of versions we're backward compatible with.
> # This is like having 'current=APIVER' and age='APIAGE' in libtool,
> # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
> -use constant APIAGE => 1;
> +use constant APIAGE => 2;
>
> # load standard plugins
> PVE::Storage::DirPlugin->register();
> @@ -769,116 +769,6 @@ sub vdisk_free {
> $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
> }
>
> -# lists all files in the snippets directory
> -sub snippets_list {
> - my ($cfg, $storeid) = @_;
> -
> - my $ids = $cfg->{ids};
> -
> - storage_check_enabled($cfg, $storeid) if ($storeid);
> -
> - my $res = {};
> -
> - foreach my $sid (keys %$ids) {
> - next if $storeid && $storeid ne $sid;
> - next if !storage_check_enabled($cfg, $sid, undef, 1);
> -
> - my $scfg = $ids->{$sid};
> - next if !$scfg->{content}->{snippets};
> -
> - activate_storage($cfg, $sid);
> -
> - if ($scfg->{path}) {
> - my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
> - my $path = $plugin->get_subdir($scfg, 'snippets');
> -
> - foreach my $fn (<$path/*>) {
> - next if -d $fn;
> -
> - push @{$res->{$sid}}, {
> - volid => "$sid:snippets/". basename($fn),
> - format => 'snippet',
> - size => -s $fn // 0,
> - };
> - }
> - }
> -
> - if ($res->{$sid}) {
> - @{$res->{$sid}} = sort {$a->{volid} cmp $b->{volid} } @{$res->{$sid}};
> - }
> - }
> -
> - return $res;
> -}
> -
> -#list iso or openvz template ($tt = <iso|vztmpl|backup>)
> -sub template_list {
> - my ($cfg, $storeid, $tt) = @_;
> -
> - die "unknown template type '$tt'\n"
> - if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
> -
> - my $ids = $cfg->{ids};
> -
> - storage_check_enabled($cfg, $storeid) if ($storeid);
> -
> - my $res = {};
> -
> - # query the storage
> -
> - foreach my $sid (keys %$ids) {
> - next if $storeid && $storeid ne $sid;
> -
> - my $scfg = $ids->{$sid};
> - my $type = $scfg->{type};
> -
> - next if !storage_check_enabled($cfg, $sid, undef, 1);
> -
> - next if $tt eq 'iso' && !$scfg->{content}->{iso};
> - next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
> - next if $tt eq 'backup' && !$scfg->{content}->{backup};
> -
> - activate_storage($cfg, $sid);
> -
> - if ($scfg->{path}) {
> - my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
> -
> - my $path = $plugin->get_subdir($scfg, $tt);
> -
> - foreach my $fn (<$path/*>) {
> -
> - my $info;
> -
> - if ($tt eq 'iso') {
> - next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
> -
> - $info = { volid => "$sid:iso/$1", format => 'iso' };
> -
> - } elsif ($tt eq 'vztmpl') {
> - next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
> -
> - $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
> -
> - } elsif ($tt eq 'backup') {
> - next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
> -
> - $info = { volid => "$sid:backup/$1", format => $2 };
> - }
> -
> - $info->{size} = -s $fn // 0;
> -
> - push @{$res->{$sid}}, $info;
> - }
> -
> - }
> -
> - @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
> - }
> -
> - return $res;
> -}
> -
> -
> sub vdisk_list {
> my ($cfg, $storeid, $vmid, $vollist) = @_;
>
> @@ -923,6 +813,35 @@ sub vdisk_list {
> return $res;
> }
>
> +sub template_list {
> + my ($cfg, $storeid, $tt) = @_;
> +
> + die "unknown template type '$tt'\n"
> + if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets');
> +
> + my $ids = $cfg->{ids};
> +
> + storage_check_enabled($cfg, $storeid) if ($storeid);
> +
> + my $res = {};
> +
> + # query the storage
> + foreach my $sid (keys %$ids) {
> + next if $storeid && $storeid ne $sid;
> +
> + my $scfg = $ids->{$sid};
> + my $type = $scfg->{type};
> +
> + next if !$scfg->{content}->{$tt};
> +
> + next if !storage_check_enabled($cfg, $sid, undef, 1);
> +
> + $res->{$sid} = volume_list($cfg, $sid, undef, $tt);
> + }
> +
> + return $res;
> +}
> +
> sub volume_list {
> my ($cfg, $storeid, $vmid, $content) = @_;
>
> @@ -932,33 +851,15 @@ sub volume_list {
>
> my $scfg = PVE::Storage::storage_config($cfg, $storeid);
>
> - my $res = [];
> - foreach my $ct (@$cts) {
> - my $data;
> - if ($ct eq 'images') {
> - $data = vdisk_list($cfg, $storeid, $vmid);
> - } elsif ($ct eq 'iso' && !defined($vmid)) {
> - $data = template_list($cfg, $storeid, 'iso');
> - } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
> - $data = template_list ($cfg, $storeid, 'vztmpl');
> - } elsif ($ct eq 'backup') {
> - $data = template_list ($cfg, $storeid, 'backup');
> - foreach my $item (@{$data->{$storeid}}) {
> - if (defined($vmid)) {
> - @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
> - }
> - }
> - } elsif ($ct eq 'snippets') {
> - $data = snippets_list($cfg, $storeid);
> - }
> + $cts = [ grep { defined($scfg->{content}->{$_}) } @$cts ];
>
> - next if !$data || !$data->{$storeid};
> + my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
>
> - foreach my $item (@{$data->{$storeid}}) {
> - $item->{content} = $ct;
> - push @$res, $item;
> - }
> - }
> + activate_storage($cfg, $storeid);
> +
> + my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts);
> +
> + @$res = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @$res;
>
> return $res;
> }
> diff --git a/PVE/Storage/DirPlugin.pm b/PVE/Storage/DirPlugin.pm
> index e24ee09..39760a8 100644
> --- a/PVE/Storage/DirPlugin.pm
> +++ b/PVE/Storage/DirPlugin.pm
> @@ -21,7 +21,7 @@ sub plugindata {
> { images => 1, rootdir => 1 }],
> format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ],
> };
> -}
> +}
>
> sub properties {
> return {
> @@ -114,7 +114,7 @@ sub activate_storage {
> "directory is expected to be a mount point but is not mounted: '$mp'\n";
> }
>
> - $class->SUPER::activate_storage($storeid, $scfg, $cache);
> + $class->SUPER::activate_storage($storeid, $scfg, $cache);
> }
>
> sub check_config {
> diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
> index 842b4d2..e075a1d 100644
> --- a/PVE/Storage/Plugin.pm
> +++ b/PVE/Storage/Plugin.pm
> @@ -902,6 +902,83 @@ sub list_images {
> return $res;
> }
>
> +# list templates ($tt = <iso|vztmpl|backup|snippets>)
> +my $get_subdir_files = sub {
> + my ($sid, $path, $tt, $vmid) = @_;
> +
> + my $res = [];
> +
> + foreach my $fn (<$path/*>) {
> +
> + next if -d $fn;
> +
> + my $info;
> +
> + if ($tt eq 'iso') {
> + next if $fn !~ m!/([^/]+\.iso)$!i;
> +
> + $info = { volid => "$sid:iso/$1", format => 'iso' };
> +
> + } elsif ($tt eq 'vztmpl') {
> + next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
> +
> + $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
> +
> + } elsif ($tt eq 'backup') {
> + next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
> + next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
> +
> + $info = { volid => "$sid:backup/$1", format => $2 };
> +
> + } elsif ($tt eq 'snippets') {
> +
> + $info = {
> + volid => "$sid:snippets/". basename($fn),
> + format => 'snippet',
> + };
> + }
> +
> + $info->{size} = -s $fn // 0;
> +
> + push @$res, $info;
> + }
> +
> + return $res;
> +};
> +
> +sub list_volumes {
> + my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
> +
> + my $res = [];
> +
> + foreach my $ct (@$content_types) {
> + my $data;
> +
> + my $path = $class->get_subdir($scfg, $ct);
> +
> + if ($ct eq 'images') {
> + $data = $class->list_images($storeid, $scfg, $vmid);
> + } elsif ($ct eq 'iso' && !defined($vmid)) {
> + $data = $get_subdir_files->($storeid, $path, 'iso');
> + } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
> + $data = $get_subdir_files->($storeid, $path, 'vztmpl');
> + } elsif ($ct eq 'backup') {
> + $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
> + } elsif ($ct eq 'snippets') {
> + $data = $get_subdir_files->($storeid, $path, 'snippets');
> + }
> +
> + next if !$data;
> +
> + foreach my $item (@$data) {
> + $item->{content} = $ct;
> + push @$res, $item;
> + }
> + }
> +
> + return $res;
> +}
> +
> sub status {
> my ($class, $storeid, $scfg, $cache) = @_;
>
> --
> 2.20.1
>
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
More information about the pve-devel
mailing list