]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::DirPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
ddb32630 | 5 | |
d547f26c | 6 | use Cwd; |
43f8112f | 7 | use Encode qw(decode encode); |
1dc01b9f | 8 | use File::Path; |
56897a92 | 9 | use IO::File; |
ddb32630 FE |
10 | use POSIX; |
11 | ||
1dc01b9f DM |
12 | use PVE::Storage::Plugin; |
13 | use PVE::JSONSchema qw(get_standard_option); | |
14 | ||
15 | use base qw(PVE::Storage::Plugin); | |
16 | ||
17 | # Configuration | |
18 | ||
19 | sub type { | |
20 | return 'dir'; | |
21 | } | |
22 | ||
23 | sub plugindata { | |
24 | return { | |
d1eb35ea | 25 | content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, snippets => 1, none => 1 }, |
1dc01b9f | 26 | { images => 1, rootdir => 1 }], |
35533c68 | 27 | format => [ { raw => 1, qcow2 => 1, vmdk => 1, subvol => 1 } , 'raw' ], |
1dc01b9f | 28 | }; |
c2fc9fbe | 29 | } |
1dc01b9f DM |
30 | |
31 | sub properties { | |
32 | return { | |
33 | path => { | |
34 | description => "File system path.", | |
35 | type => 'string', format => 'pve-storage-path', | |
36 | }, | |
b521247b WB |
37 | mkdir => { |
38 | description => "Create the directory if it doesn't exist.", | |
39 | type => 'boolean', | |
40 | default => 'yes', | |
41 | }, | |
d547f26c WB |
42 | is_mountpoint => { |
43 | description => | |
de8eff4d WB |
44 | "Assume the given path is an externally managed mountpoint " . |
45 | "and consider the storage offline if it is not mounted. ". | |
46 | "Using a boolean (yes/no) value serves as a shortcut to using the target path in this field.", | |
47 | type => 'string', | |
d547f26c WB |
48 | default => 'no', |
49 | }, | |
9edb99a5 | 50 | bwlimit => get_standard_option('bwlimit'), |
1dc01b9f DM |
51 | }; |
52 | } | |
53 | ||
54 | sub options { | |
55 | return { | |
56 | path => { fixed => 1 }, | |
b539bb46 | 57 | 'content-dirs' => { optional => 1 }, |
3353698f | 58 | nodes => { optional => 1 }, |
1dc01b9f DM |
59 | shared => { optional => 1 }, |
60 | disable => { optional => 1 }, | |
3353698f FE |
61 | maxfiles => { optional => 1 }, |
62 | 'prune-backups' => { optional => 1 }, | |
8009417d | 63 | 'max-protected-backups' => { optional => 1 }, |
1dc01b9f DM |
64 | content => { optional => 1 }, |
65 | format => { optional => 1 }, | |
b521247b | 66 | mkdir => { optional => 1 }, |
d547f26c | 67 | is_mountpoint => { optional => 1 }, |
9edb99a5 | 68 | bwlimit => { optional => 1 }, |
95ff5dbd | 69 | preallocation => { optional => 1 }, |
1dc01b9f DM |
70 | }; |
71 | } | |
72 | ||
73 | # Storage implementation | |
d547f26c WB |
74 | # |
75 | ||
76 | # NOTE: should ProcFSTools::is_mounted accept an optional cache like this? | |
77 | sub path_is_mounted { | |
78 | my ($mountpoint, $mountdata) = @_; | |
79 | ||
80 | $mountpoint = Cwd::realpath($mountpoint); # symlinks | |
81 | return 0 if !defined($mountpoint); # path does not exist | |
82 | ||
83 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; | |
84 | return 1 if grep { $_->[1] eq $mountpoint } @$mountdata; | |
85 | return undef; | |
86 | } | |
87 | ||
de8eff4d WB |
88 | sub parse_is_mountpoint { |
89 | my ($scfg) = @_; | |
90 | my $is_mp = $scfg->{is_mountpoint}; | |
91 | return undef if !defined $is_mp; | |
92 | if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) { | |
93 | return $bool ? $scfg->{path} : undef; | |
94 | } | |
95 | return $is_mp; # contains a path | |
96 | } | |
97 | ||
91d49d1d DC |
98 | # FIXME move into 'get_volume_attribute' when removing 'get_volume_notes' |
99 | my $get_volume_notes_impl = sub { | |
e9991d26 | 100 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; |
20471dfd | 101 | |
e0aa2070 FE |
102 | my ($vtype) = $class->parse_volname($volname); |
103 | return if $vtype ne 'backup'; | |
104 | ||
e9991d26 DC |
105 | my $path = $class->filesystem_path($scfg, $volname); |
106 | $path .= $class->SUPER::NOTES_EXT; | |
107 | ||
43f8112f DC |
108 | if (-f $path) { |
109 | my $data = PVE::Tools::file_get_contents($path); | |
110 | return eval { decode('UTF-8', $data, 1) } // $data; | |
111 | } | |
e9991d26 | 112 | |
20471dfd | 113 | return ''; |
91d49d1d | 114 | }; |
e9991d26 | 115 | |
f1de8281 | 116 | # FIXME remove on the next APIAGE reset. |
91d49d1d DC |
117 | # Deprecated, use get_volume_attribute instead. |
118 | sub get_volume_notes { | |
119 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
120 | return $get_volume_notes_impl->($class, $scfg, $storeid, $volname, $timeout); | |
121 | } | |
122 | ||
123 | # FIXME move into 'update_volume_attribute' when removing 'update_volume_notes' | |
124 | my $update_volume_notes_impl = sub { | |
e9991d26 | 125 | my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_; |
e9991d26 | 126 | |
20471dfd TL |
127 | my ($vtype) = $class->parse_volname($volname); |
128 | die "only backups can have notes\n" if $vtype ne 'backup'; | |
e9991d26 | 129 | |
20471dfd | 130 | my $path = $class->filesystem_path($scfg, $volname); |
e9991d26 DC |
131 | $path .= $class->SUPER::NOTES_EXT; |
132 | ||
f244e2aa | 133 | if (defined($notes) && $notes ne '') { |
43f8112f DC |
134 | my $encoded = encode('UTF-8', $notes); |
135 | PVE::Tools::file_set_contents($path, $encoded); | |
f244e2aa | 136 | } else { |
ddb32630 | 137 | unlink $path or $! == ENOENT or die "could not delete notes - $!\n"; |
f244e2aa | 138 | } |
20471dfd | 139 | return; |
91d49d1d DC |
140 | }; |
141 | ||
142 | # FIXME remove on the next APIAGE reset. | |
143 | # Deprecated, use update_volume_attribute instead. | |
144 | sub update_volume_notes { | |
145 | my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_; | |
146 | return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $notes, $timeout); | |
e9991d26 DC |
147 | } |
148 | ||
f1de8281 FE |
149 | sub get_volume_attribute { |
150 | my ($class, $scfg, $storeid, $volname, $attribute) = @_; | |
151 | ||
152 | if ($attribute eq 'notes') { | |
91d49d1d | 153 | return $get_volume_notes_impl->($class, $scfg, $storeid, $volname); |
f1de8281 FE |
154 | } |
155 | ||
56897a92 FE |
156 | my ($vtype) = $class->parse_volname($volname); |
157 | return if $vtype ne 'backup'; | |
158 | ||
159 | if ($attribute eq 'protected') { | |
160 | my $path = $class->filesystem_path($scfg, $volname); | |
161 | return -e PVE::Storage::protection_file_path($path) ? 1 : 0; | |
162 | } | |
163 | ||
f1de8281 FE |
164 | return; |
165 | } | |
166 | ||
167 | sub update_volume_attribute { | |
168 | my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_; | |
169 | ||
170 | if ($attribute eq 'notes') { | |
91d49d1d | 171 | return $update_volume_notes_impl->($class, $scfg, $storeid, $volname, $value); |
f1de8281 FE |
172 | } |
173 | ||
56897a92 FE |
174 | my ($vtype) = $class->parse_volname($volname); |
175 | die "only backups support attribute '$attribute'\n" if $vtype ne 'backup'; | |
176 | ||
177 | if ($attribute eq 'protected') { | |
178 | my $path = $class->filesystem_path($scfg, $volname); | |
179 | my $protection_path = PVE::Storage::protection_file_path($path); | |
180 | ||
181 | return if !((-e $protection_path) xor $value); # protection status already correct | |
182 | ||
183 | if ($value) { | |
184 | my $fh = IO::File->new($protection_path, O_CREAT, 0644) | |
185 | or die "unable to create protection file '$protection_path' - $!\n"; | |
186 | close($fh); | |
187 | } else { | |
188 | unlink $protection_path or $! == ENOENT | |
189 | or die "could not delete protection file '$protection_path' - $!\n"; | |
190 | } | |
191 | ||
192 | return; | |
193 | } | |
194 | ||
f1de8281 FE |
195 | die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n"; |
196 | } | |
197 | ||
d547f26c WB |
198 | sub status { |
199 | my ($class, $storeid, $scfg, $cache) = @_; | |
200 | ||
de8eff4d | 201 | if (defined(my $mp = parse_is_mountpoint($scfg))) { |
8b5ccc06 FG |
202 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
203 | if !$cache->{mountdata}; | |
d547f26c | 204 | |
de8eff4d | 205 | return undef if !path_is_mounted($mp, $cache->{mountdata}); |
8b5ccc06 | 206 | } |
d547f26c WB |
207 | |
208 | return $class->SUPER::status($storeid, $scfg, $cache); | |
209 | } | |
210 | ||
1dc01b9f DM |
211 | |
212 | sub activate_storage { | |
213 | my ($class, $storeid, $scfg, $cache) = @_; | |
214 | ||
d547f26c | 215 | my $path = $scfg->{path}; |
b521247b | 216 | if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) { |
b521247b WB |
217 | mkpath $path; |
218 | } | |
1dc01b9f | 219 | |
de8eff4d WB |
220 | my $mp = parse_is_mountpoint($scfg); |
221 | if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) { | |
d547f26c | 222 | die "unable to activate storage '$storeid' - " . |
de8eff4d | 223 | "directory is expected to be a mount point but is not mounted: '$mp'\n"; |
d547f26c WB |
224 | } |
225 | ||
c2fc9fbe | 226 | $class->SUPER::activate_storage($storeid, $scfg, $cache); |
1dc01b9f DM |
227 | } |
228 | ||
5c95e484 WB |
229 | sub check_config { |
230 | my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
231 | my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck); | |
232 | return $opts if !$create; | |
233 | if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) { | |
234 | die "illegal path for directory storage: $opts->{path}\n"; | |
235 | } | |
236 | return $opts; | |
237 | } | |
1dc01b9f DM |
238 | |
239 | 1; |