]>
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 }, | |
3353698f | 57 | nodes => { optional => 1 }, |
1dc01b9f DM |
58 | shared => { optional => 1 }, |
59 | disable => { optional => 1 }, | |
3353698f FE |
60 | maxfiles => { optional => 1 }, |
61 | 'prune-backups' => { optional => 1 }, | |
8009417d | 62 | 'max-protected-backups' => { optional => 1 }, |
1dc01b9f DM |
63 | content => { optional => 1 }, |
64 | format => { optional => 1 }, | |
b521247b | 65 | mkdir => { optional => 1 }, |
d547f26c | 66 | is_mountpoint => { optional => 1 }, |
9edb99a5 | 67 | bwlimit => { optional => 1 }, |
95ff5dbd | 68 | preallocation => { optional => 1 }, |
1dc01b9f DM |
69 | }; |
70 | } | |
71 | ||
72 | # Storage implementation | |
d547f26c WB |
73 | # |
74 | ||
75 | # NOTE: should ProcFSTools::is_mounted accept an optional cache like this? | |
76 | sub path_is_mounted { | |
77 | my ($mountpoint, $mountdata) = @_; | |
78 | ||
79 | $mountpoint = Cwd::realpath($mountpoint); # symlinks | |
80 | return 0 if !defined($mountpoint); # path does not exist | |
81 | ||
82 | $mountdata = PVE::ProcFSTools::parse_proc_mounts() if !$mountdata; | |
83 | return 1 if grep { $_->[1] eq $mountpoint } @$mountdata; | |
84 | return undef; | |
85 | } | |
86 | ||
de8eff4d WB |
87 | sub parse_is_mountpoint { |
88 | my ($scfg) = @_; | |
89 | my $is_mp = $scfg->{is_mountpoint}; | |
90 | return undef if !defined $is_mp; | |
91 | if (defined(my $bool = PVE::JSONSchema::parse_boolean($is_mp))) { | |
92 | return $bool ? $scfg->{path} : undef; | |
93 | } | |
94 | return $is_mp; # contains a path | |
95 | } | |
96 | ||
f1de8281 FE |
97 | # FIXME remove on the next APIAGE reset. |
98 | # Deprecated, use get_volume_attribute instead. | |
e9991d26 DC |
99 | sub get_volume_notes { |
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 ''; |
e9991d26 DC |
114 | } |
115 | ||
f1de8281 FE |
116 | # FIXME remove on the next APIAGE reset. |
117 | # Deprecated, use update_volume_attribute instead. | |
e9991d26 DC |
118 | sub update_volume_notes { |
119 | my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_; | |
e9991d26 | 120 | |
20471dfd TL |
121 | my ($vtype) = $class->parse_volname($volname); |
122 | die "only backups can have notes\n" if $vtype ne 'backup'; | |
e9991d26 | 123 | |
20471dfd | 124 | my $path = $class->filesystem_path($scfg, $volname); |
e9991d26 DC |
125 | $path .= $class->SUPER::NOTES_EXT; |
126 | ||
f244e2aa | 127 | if (defined($notes) && $notes ne '') { |
43f8112f DC |
128 | my $encoded = encode('UTF-8', $notes); |
129 | PVE::Tools::file_set_contents($path, $encoded); | |
f244e2aa | 130 | } else { |
ddb32630 | 131 | unlink $path or $! == ENOENT or die "could not delete notes - $!\n"; |
f244e2aa | 132 | } |
20471dfd | 133 | return; |
e9991d26 DC |
134 | } |
135 | ||
f1de8281 FE |
136 | sub get_volume_attribute { |
137 | my ($class, $scfg, $storeid, $volname, $attribute) = @_; | |
138 | ||
139 | if ($attribute eq 'notes') { | |
140 | return $class->get_volume_notes($scfg, $storeid, $volname); | |
141 | } | |
142 | ||
56897a92 FE |
143 | my ($vtype) = $class->parse_volname($volname); |
144 | return if $vtype ne 'backup'; | |
145 | ||
146 | if ($attribute eq 'protected') { | |
147 | my $path = $class->filesystem_path($scfg, $volname); | |
148 | return -e PVE::Storage::protection_file_path($path) ? 1 : 0; | |
149 | } | |
150 | ||
f1de8281 FE |
151 | return; |
152 | } | |
153 | ||
154 | sub update_volume_attribute { | |
155 | my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_; | |
156 | ||
157 | if ($attribute eq 'notes') { | |
158 | return $class->update_volume_notes($scfg, $storeid, $volname, $value); | |
159 | } | |
160 | ||
56897a92 FE |
161 | my ($vtype) = $class->parse_volname($volname); |
162 | die "only backups support attribute '$attribute'\n" if $vtype ne 'backup'; | |
163 | ||
164 | if ($attribute eq 'protected') { | |
165 | my $path = $class->filesystem_path($scfg, $volname); | |
166 | my $protection_path = PVE::Storage::protection_file_path($path); | |
167 | ||
168 | return if !((-e $protection_path) xor $value); # protection status already correct | |
169 | ||
170 | if ($value) { | |
171 | my $fh = IO::File->new($protection_path, O_CREAT, 0644) | |
172 | or die "unable to create protection file '$protection_path' - $!\n"; | |
173 | close($fh); | |
174 | } else { | |
175 | unlink $protection_path or $! == ENOENT | |
176 | or die "could not delete protection file '$protection_path' - $!\n"; | |
177 | } | |
178 | ||
179 | return; | |
180 | } | |
181 | ||
f1de8281 FE |
182 | die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n"; |
183 | } | |
184 | ||
d547f26c WB |
185 | sub status { |
186 | my ($class, $storeid, $scfg, $cache) = @_; | |
187 | ||
de8eff4d | 188 | if (defined(my $mp = parse_is_mountpoint($scfg))) { |
8b5ccc06 FG |
189 | $cache->{mountdata} = PVE::ProcFSTools::parse_proc_mounts() |
190 | if !$cache->{mountdata}; | |
d547f26c | 191 | |
de8eff4d | 192 | return undef if !path_is_mounted($mp, $cache->{mountdata}); |
8b5ccc06 | 193 | } |
d547f26c WB |
194 | |
195 | return $class->SUPER::status($storeid, $scfg, $cache); | |
196 | } | |
197 | ||
1dc01b9f DM |
198 | |
199 | sub activate_storage { | |
200 | my ($class, $storeid, $scfg, $cache) = @_; | |
201 | ||
d547f26c | 202 | my $path = $scfg->{path}; |
b521247b | 203 | if (!defined($scfg->{mkdir}) || $scfg->{mkdir}) { |
b521247b WB |
204 | mkpath $path; |
205 | } | |
1dc01b9f | 206 | |
de8eff4d WB |
207 | my $mp = parse_is_mountpoint($scfg); |
208 | if (defined($mp) && !path_is_mounted($mp, $cache->{mountdata})) { | |
d547f26c | 209 | die "unable to activate storage '$storeid' - " . |
de8eff4d | 210 | "directory is expected to be a mount point but is not mounted: '$mp'\n"; |
d547f26c WB |
211 | } |
212 | ||
c2fc9fbe | 213 | $class->SUPER::activate_storage($storeid, $scfg, $cache); |
1dc01b9f DM |
214 | } |
215 | ||
5c95e484 WB |
216 | sub check_config { |
217 | my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_; | |
218 | my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck); | |
219 | return $opts if !$create; | |
220 | if ($opts->{path} !~ m@^/[-/a-zA-Z0-9_.]+$@) { | |
221 | die "illegal path for directory storage: $opts->{path}\n"; | |
222 | } | |
223 | return $opts; | |
224 | } | |
1dc01b9f DM |
225 | |
226 | 1; |