From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 00B40F573 for ; Fri, 16 Dec 2022 11:29:49 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DDC70207B8 for ; Fri, 16 Dec 2022 11:29:49 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Fri, 16 Dec 2022 11:29:48 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B940644798 for ; Fri, 16 Dec 2022 11:29:48 +0100 (CET) Date: Fri, 16 Dec 2022 11:29:47 +0100 From: Wolfgang Bumiller To: Leo Nunner Cc: pve-devel@lists.proxmox.com Message-ID: <20221216102947.msnzhznndhgkym7u@wobu-vie.proxmox.com> References: <20221215112147.100469-1-l.nunner@proxmox.com> <20221215112147.100469-2-l.nunner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221215112147.100469-2-l.nunner@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL 0.221 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [dirplugin.pm, cifsplugin.pm, nfsplugin.pm, plugin.pm] Subject: Re: [pve-devel] [PATCH storage] config: add overrides for default directory locations X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Dec 2022 10:29:50 -0000 This is going to need multiple sets of eyes as I don't trust that we don't have some rogue direct `$scfg->{path}` usage ruining this for some case ;-) As for the code, see inline comments: On Thu, Dec 15, 2022 at 12:21:46PM +0100, Leo Nunner wrote: > Allowing overrides for the default directory locations seems to > integrate rather well into the existing system. Custom locations > are specified using the "dirs" parameter as a comma-separated list > of "vtype:/location" values. > > For now, the option has been enabled for the Directory, CIFS and NFS > backends. > > Signed-off-by: Leo Nunner > --- > PVE/Storage/CIFSPlugin.pm | 1 + > PVE/Storage/DirPlugin.pm | 1 + > PVE/Storage/NFSPlugin.pm | 1 + > PVE/Storage/Plugin.pm | 43 +++++++++++++++++++++++++++++++++++---- > test/get_subdir_test.pm | 7 +++++++ > 5 files changed, 49 insertions(+), 4 deletions(-) > > diff --git a/PVE/Storage/CIFSPlugin.pm b/PVE/Storage/CIFSPlugin.pm > index 982040a..4284c35 100644 > --- a/PVE/Storage/CIFSPlugin.pm > +++ b/PVE/Storage/CIFSPlugin.pm > @@ -128,6 +128,7 @@ sub properties { > sub options { > return { > path => { fixed => 1 }, > + dirs => { optional => 1 }, > server => { fixed => 1 }, > share => { fixed => 1 }, > nodes => { optional => 1 }, > diff --git a/PVE/Storage/DirPlugin.pm b/PVE/Storage/DirPlugin.pm > index 8715a9d..3c907ca 100644 > --- a/PVE/Storage/DirPlugin.pm > +++ b/PVE/Storage/DirPlugin.pm > @@ -54,6 +54,7 @@ sub properties { > sub options { > return { > path => { fixed => 1 }, > + dirs => { optional => 1 }, > nodes => { optional => 1 }, > shared => { optional => 1 }, > disable => { optional => 1 }, > diff --git a/PVE/Storage/NFSPlugin.pm b/PVE/Storage/NFSPlugin.pm > index 5bd7313..b7e8318 100644 > --- a/PVE/Storage/NFSPlugin.pm > +++ b/PVE/Storage/NFSPlugin.pm > @@ -79,6 +79,7 @@ sub properties { > sub options { > return { > path => { fixed => 1 }, > + dirs => { optional => 1 }, > server => { fixed => 1 }, > export => { fixed => 1 }, > nodes => { optional => 1 }, > diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm > index 8a41df1..de9227b 100644 > --- a/PVE/Storage/Plugin.pm > +++ b/PVE/Storage/Plugin.pm > @@ -181,6 +181,11 @@ my $defaultData = { > default => 'metadata', > optional => 1, > }, > + dirs => { > + description => "Overrides for default directories", > + type => "string", format => "pve-volume-id-list", > + optional => 1, > + }, > }, > }; > > @@ -205,6 +210,17 @@ sub valid_content_types { > return $def->{content}->[0]; > } > > +sub dirs_hash_to_string { > + my $hash = shift; > + > + my @cta; Better just build the string right away and skip the cryptically named array ;-) (can do a `$str .= ',' if length($str);` line for the comma part) Alternatively you can use a more functional style with `map {}`, eg.: join(',', map { "$_:$hash->{$_}" } sort keys %$hash); Finally: please `sort` the keys to avoid adding yet another case of reordering happening on every single storage.cfg modification as perl hashes are randomized ;-) > + foreach my $dir (keys %$hash) { > + push @cta, "$dir:$hash->{$dir}" if $hash->{$dir}; > + } > + > + return join(',', @cta); > +} > + > sub default_format { > my ($scfg) = @_; > > @@ -405,6 +421,23 @@ sub decode_value { > # die "storage '$storeid' does not allow node restrictions\n"; > #} > > + return $res; > + } elsif ($key eq 'dirs') { > + my $valid_content = $def->{content}->[0]; > + my $res = {}; > + > + foreach my $dir (PVE::Tools::split_list($value)) { > + my ($c, $path) = parse_volume_id($dir); Using `parse_volume_id` here can lead to confusing error messages. Plus you verify each part below anyway, so IMO you can just do split(/:/, $dir, 2) > + if (!$valid_content->{$c}) { > + warn "storage does not support content type '$c'\n"; > + next; > + } elsif (!verify_path($path, 1)) { > + warn "not a valid path: $path"; > + next; > + } > + $res->{$c} = $path; > + } > + > return $res; > } > > @@ -419,6 +452,9 @@ sub encode_value { > } elsif ($key eq 'content') { > my $res = content_hash_to_string($value) || 'none'; > return $res; > + } elsif ($key eq 'dirs') { > + my $res = dirs_hash_to_string($value); > + return $res; > } > > return $value; > @@ -610,12 +646,11 @@ sub get_subdir { > my $path = $scfg->{path}; > > die "storage definition has no path\n" if !$path; > + die "unknown vtype '$vtype'\n" if !exists($vtype_subdirs->{$vtype}); > > - my $subdir = $vtype_subdirs->{$vtype}; > - > - die "unknown vtype '$vtype'\n" if !defined($subdir); > + my $subdir = $scfg->{dirs}->{$vtype} // "/".$vtype_subdirs->{$vtype}; > > - return "$path/$subdir"; > + return $path.$subdir; > } > > sub filesystem_path { > diff --git a/test/get_subdir_test.pm b/test/get_subdir_test.pm > index 1e58350..26c08d5 100644 > --- a/test/get_subdir_test.pm > +++ b/test/get_subdir_test.pm > @@ -27,6 +27,13 @@ foreach my $type (keys %$vtype_subdirs) { > push @$tests, [ $scfg_with_path, $type, $path ]; > } > > +# creates additional tests for overrides > +foreach my $type (keys %$vtype_subdirs) { > + my $override = "/${type}_override"; > + my $scfg_with_override = { path => '/some/path', dirs => { $type => $override } }; > + push @$tests, [ $scfg_with_override, $type, "$scfg_with_override->{path}$scfg_with_override->{dirs}->{$type}" ]; > +} > + > plan tests => scalar @$tests; > > foreach my $tt (@$tests) { > -- > 2.30.2