]> git.proxmox.com Git - pve-storage.git/commitdiff
config: add overrides for default directory locations
authorLeo Nunner <l.nunner@proxmox.com>
Mon, 2 Jan 2023 16:04:37 +0000 (17:04 +0100)
committerWolfgang Bumiller <w.bumiller@errno.eu>
Thu, 5 Jan 2023 13:33:16 +0000 (14:33 +0100)
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 <l.nunner@proxmox.com>
PVE/Storage/CIFSPlugin.pm
PVE/Storage/DirPlugin.pm
PVE/Storage/NFSPlugin.pm
PVE/Storage/Plugin.pm
test/get_subdir_test.pm

index 982040acc99f73e6a4ec83b7d92a0bb248c97f20..4284c353d935027e82d716790731c322079c6b88 100644 (file)
@@ -128,6 +128,7 @@ sub properties {
 sub options {
     return {
        path => { fixed => 1 },
+       dirs => { optional => 1 },
        server => { fixed => 1 },
        share => { fixed => 1 },
        nodes => { optional => 1 },
index 8715a9d3d8b4ef24548128646e4e030bb439c731..3c907ca0ddae9a29ace298d1c9be760df6e4e2d6 100644 (file)
@@ -54,6 +54,7 @@ sub properties {
 sub options {
     return {
        path => { fixed => 1 },
+       dirs => { optional => 1 },
        nodes => { optional => 1 },
        shared => { optional => 1 },
        disable => { optional => 1 },
index 5bd73132b3f2abae103fc6e1f5c8095d3a26776a..b7e831862d878442046b48a3ecfea899d1cee1f9 100644 (file)
@@ -79,6 +79,7 @@ sub properties {
 sub options {
     return {
        path => { fixed => 1 },
+       dirs => { optional => 1 },
        server => { fixed => 1 },
        export => { fixed => 1 },
        nodes => { optional => 1 },
index 8a41df10b94b7112bf566561409305505fc8e67c..5cb3d900360530689df62c4a3c4ce3e338cf6f70 100644 (file)
@@ -181,6 +181,11 @@ my $defaultData = {
            default => 'metadata',
            optional => 1,
        },
+       dirs => {
+           description => "Overrides for default directories",
+           type => "string", format => "pve-dir-override-list",
+           optional => 1,
+       },
     },
 };
 
@@ -205,6 +210,12 @@ sub valid_content_types {
     return $def->{content}->[0];
 }
 
+sub dirs_hash_to_string {
+    my $hash = shift;
+
+    return join(',', map { "$_:$hash->{$_}" } sort keys %$hash);
+}
+
 sub default_format {
     my ($scfg) = @_;
 
@@ -335,6 +346,18 @@ sub parse_volume_id {
     die "unable to parse volume ID '$volid'\n";
 }
 
+PVE::JSONSchema::register_format('pve-dir-override', \&verify_dir_override);
+sub verify_dir_override {
+    my ($value, $noerr) = @_;
+
+    if($value =~ m/^([a-z]+):(.+)$/ &&
+       verify_content($1, $noerr) && verify_path($2, $noerr)) {
+       return $value;
+    }
+
+    return undef if $noerr;
+    die "invalid override '$value'\n";
+}
 
 sub private {
     return $defaultData;
@@ -405,6 +428,22 @@ 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 ($content, $path) = split(/:/, $dir, 2);
+
+           if (!$valid_content->{$content}) {
+               warn "storage does not support content type '$content'\n";
+               next;
+           }
+
+           $res->{$content} = $path;
+       }
+
        return $res;
     }
 
@@ -419,6 +458,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 +652,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 {
index 1e583504567138c306eef584504d7f59eb9e3365..26c08d53a68a1ada77c7d79518b6f9df28ff25fb 100644 (file)
@@ -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) {