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