]>
Commit | Line | Data |
---|---|---|
b6cf0a66 DM |
1 | package PVE::Storage; |
2 | ||
3 | use strict; | |
ffd6f2f3 | 4 | use warnings; |
dec97937 | 5 | use Data::Dumper; |
ffd6f2f3 | 6 | |
b6cf0a66 DM |
7 | use POSIX; |
8 | use IO::Select; | |
b6cf0a66 | 9 | use IO::File; |
7ba34faa | 10 | use IO::Socket::IP; |
57acd6a1 | 11 | use IPC::Open3; |
b6cf0a66 DM |
12 | use File::Basename; |
13 | use File::Path; | |
b6cf0a66 | 14 | use Cwd 'abs_path'; |
7a2d5c1a | 15 | use Socket; |
fb821c18 | 16 | use Time::Local qw(timelocal); |
b6cf0a66 | 17 | |
4dee23d3 | 18 | use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE); |
83d7192f | 19 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
2f08fb4b | 20 | use PVE::DataCenterConfig; |
be18d6da | 21 | use PVE::Exception qw(raise_param_exc raise); |
b6cf0a66 DM |
22 | use PVE::JSONSchema; |
23 | use PVE::INotify; | |
88c3abaf | 24 | use PVE::RPCEnvironment; |
65bb9859 | 25 | use PVE::SSHInfo; |
b6cf0a66 | 26 | |
1dc01b9f DM |
27 | use PVE::Storage::Plugin; |
28 | use PVE::Storage::DirPlugin; | |
29 | use PVE::Storage::LVMPlugin; | |
610798bc | 30 | use PVE::Storage::LvmThinPlugin; |
1dc01b9f | 31 | use PVE::Storage::NFSPlugin; |
d7875239 | 32 | use PVE::Storage::CIFSPlugin; |
1dc01b9f | 33 | use PVE::Storage::ISCSIPlugin; |
0509010d | 34 | use PVE::Storage::RBDPlugin; |
e34ce144 | 35 | use PVE::Storage::CephFSPlugin; |
86616554 | 36 | use PVE::Storage::ISCSIDirectPlugin; |
f4648aef | 37 | use PVE::Storage::GlusterfsPlugin; |
85fda4dd | 38 | use PVE::Storage::ZFSPoolPlugin; |
4f914e6e | 39 | use PVE::Storage::ZFSPlugin; |
271fe394 | 40 | use PVE::Storage::PBSPlugin; |
b6cf0a66 | 41 | |
af2dd59e | 42 | # Storage API version. Increment it on changes in storage API interface. |
343ca257 | 43 | use constant APIVER => 8; |
042dd4be WB |
44 | # Age is the number of versions we're backward compatible with. |
45 | # This is like having 'current=APIVER' and age='APIAGE' in libtool, | |
46 | # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html | |
343ca257 | 47 | use constant APIAGE => 7; |
4dee23d3 DP |
48 | |
49 | # load standard plugins | |
1dc01b9f DM |
50 | PVE::Storage::DirPlugin->register(); |
51 | PVE::Storage::LVMPlugin->register(); | |
610798bc | 52 | PVE::Storage::LvmThinPlugin->register(); |
1dc01b9f | 53 | PVE::Storage::NFSPlugin->register(); |
d7875239 | 54 | PVE::Storage::CIFSPlugin->register(); |
1dc01b9f | 55 | PVE::Storage::ISCSIPlugin->register(); |
0509010d | 56 | PVE::Storage::RBDPlugin->register(); |
e34ce144 | 57 | PVE::Storage::CephFSPlugin->register(); |
86616554 | 58 | PVE::Storage::ISCSIDirectPlugin->register(); |
f4648aef | 59 | PVE::Storage::GlusterfsPlugin->register(); |
85fda4dd | 60 | PVE::Storage::ZFSPoolPlugin->register(); |
4f914e6e | 61 | PVE::Storage::ZFSPlugin->register(); |
271fe394 | 62 | PVE::Storage::PBSPlugin->register(); |
4dee23d3 DP |
63 | |
64 | # load third-party plugins | |
65 | if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) { | |
66 | dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub { | |
67 | my ($file) = @_; | |
68 | my $modname = 'PVE::Storage::Custom::' . $file; | |
69 | $modname =~ s!\.pm$!!; | |
70 | $file = 'PVE/Storage/Custom/' . $file; | |
71 | ||
72 | eval { | |
73 | require $file; | |
042dd4be WB |
74 | |
75 | # Check perl interface: | |
823e8afe TL |
76 | die "not derived from PVE::Storage::Plugin\n" if !$modname->isa('PVE::Storage::Plugin'); |
77 | die "does not provide an api() method\n" if !$modname->can('api'); | |
042dd4be WB |
78 | # Check storage API version and that file is really storage plugin. |
79 | my $version = $modname->api(); | |
a0908caa | 80 | die "implements an API version newer than current ($version > " . APIVER . ")\n" |
042dd4be | 81 | if $version > APIVER; |
a0908caa TL |
82 | my $min_version = (APIVER - APIAGE); |
83 | die "API version too old, please update the plugin ($version < $min_version)\n" | |
84 | if $version < $min_version; | |
823e8afe | 85 | # all OK, do import and register (i.e., "use") |
042dd4be WB |
86 | import $file; |
87 | $modname->register(); | |
88 | ||
823e8afe | 89 | # If we got this far and the API version is not the same, make some noise: |
042dd4be WB |
90 | warn "Plugin \"$modname\" is implementing an older storage API, an upgrade is recommended\n" |
91 | if $version != APIVER; | |
4dee23d3 DP |
92 | }; |
93 | if ($@) { | |
042dd4be | 94 | warn "Error loading storage plugin \"$modname\": $@"; |
4dee23d3 DP |
95 | } |
96 | }); | |
97 | } | |
98 | ||
99 | # initialize all plugins | |
1dc01b9f | 100 | PVE::Storage::Plugin->init(); |
b6cf0a66 | 101 | |
b295e05e | 102 | our $iso_extension_re = qr/\.(?:iso|img)/i; |
4c693491 | 103 | |
1dc01b9f | 104 | # PVE::Storage utility functions |
b6cf0a66 DM |
105 | |
106 | sub config { | |
107 | return cfs_read_file("storage.cfg"); | |
108 | } | |
109 | ||
83d7192f FG |
110 | sub write_config { |
111 | my ($cfg) = @_; | |
112 | ||
113 | cfs_write_file('storage.cfg', $cfg); | |
114 | } | |
115 | ||
b6cf0a66 DM |
116 | sub lock_storage_config { |
117 | my ($code, $errmsg) = @_; | |
118 | ||
119 | cfs_lock_file("storage.cfg", undef, $code); | |
120 | my $err = $@; | |
121 | if ($err) { | |
122 | $errmsg ? die "$errmsg: $err" : die $err; | |
123 | } | |
124 | } | |
125 | ||
bbadd165 | 126 | # FIXME remove maxfiles for PVE 8.0 or PVE 9.0 |
d4e8a1f2 FE |
127 | my $convert_maxfiles_to_prune_backups = sub { |
128 | my ($scfg) = @_; | |
129 | ||
130 | return if !$scfg; | |
131 | ||
132 | my $maxfiles = delete $scfg->{maxfiles}; | |
133 | ||
134 | if (!defined($scfg->{'prune-backups'}) && defined($maxfiles)) { | |
135 | my $prune_backups; | |
136 | if ($maxfiles) { | |
137 | $prune_backups = { 'keep-last' => $maxfiles }; | |
138 | } else { # maxfiles 0 means no limit | |
139 | $prune_backups = { 'keep-all' => 1 }; | |
140 | } | |
141 | $scfg->{'prune-backups'} = PVE::JSONSchema::print_property_string( | |
142 | $prune_backups, | |
143 | 'prune-backups' | |
144 | ); | |
145 | } | |
146 | }; | |
147 | ||
b6cf0a66 DM |
148 | sub storage_config { |
149 | my ($cfg, $storeid, $noerr) = @_; | |
150 | ||
82fc923f | 151 | die "no storage ID specified\n" if !$storeid; |
1a3459ac | 152 | |
b6cf0a66 DM |
153 | my $scfg = $cfg->{ids}->{$storeid}; |
154 | ||
481f6177 | 155 | die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg); |
b6cf0a66 | 156 | |
d4e8a1f2 FE |
157 | $convert_maxfiles_to_prune_backups->($scfg); |
158 | ||
b6cf0a66 DM |
159 | return $scfg; |
160 | } | |
161 | ||
162 | sub storage_check_node { | |
163 | my ($cfg, $storeid, $node, $noerr) = @_; | |
164 | ||
1dc01b9f | 165 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
166 | |
167 | if ($scfg->{nodes}) { | |
168 | $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost'); | |
169 | if (!$scfg->{nodes}->{$node}) { | |
da156fb3 | 170 | die "storage '$storeid' is not available on node '$node'\n" if !$noerr; |
b6cf0a66 DM |
171 | return undef; |
172 | } | |
173 | } | |
174 | ||
175 | return $scfg; | |
176 | } | |
177 | ||
178 | sub storage_check_enabled { | |
179 | my ($cfg, $storeid, $node, $noerr) = @_; | |
180 | ||
1dc01b9f | 181 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
182 | |
183 | if ($scfg->{disable}) { | |
184 | die "storage '$storeid' is disabled\n" if !$noerr; | |
185 | return undef; | |
186 | } | |
187 | ||
188 | return storage_check_node($cfg, $storeid, $node, $noerr); | |
189 | } | |
190 | ||
7118dd91 DM |
191 | # storage_can_replicate: |
192 | # return true if storage supports replication | |
ffc31266 | 193 | # (volumes allocated with vdisk_alloc() has replication feature) |
7118dd91 DM |
194 | sub storage_can_replicate { |
195 | my ($cfg, $storeid, $format) = @_; | |
196 | ||
197 | my $scfg = storage_config($cfg, $storeid); | |
198 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
199 | return $plugin->storage_can_replicate($scfg, $storeid, $format); | |
200 | } | |
201 | ||
b6cf0a66 DM |
202 | sub storage_ids { |
203 | my ($cfg) = @_; | |
204 | ||
1dc01b9f | 205 | return keys %{$cfg->{ids}}; |
b6cf0a66 DM |
206 | } |
207 | ||
1dc01b9f DM |
208 | sub file_size_info { |
209 | my ($filename, $timeout) = @_; | |
b6cf0a66 | 210 | |
1dc01b9f | 211 | return PVE::Storage::Plugin::file_size_info($filename, $timeout); |
b6cf0a66 DM |
212 | } |
213 | ||
e9991d26 DC |
214 | sub get_volume_notes { |
215 | my ($cfg, $volid, $timeout) = @_; | |
216 | ||
217 | my ($storeid, $volname) = parse_volume_id($volid); | |
218 | my $scfg = storage_config($cfg, $storeid); | |
219 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
220 | ||
221 | return $plugin->get_volume_notes($scfg, $storeid, $volname, $timeout); | |
222 | } | |
223 | ||
224 | sub update_volume_notes { | |
225 | my ($cfg, $volid, $notes, $timeout) = @_; | |
226 | ||
227 | my ($storeid, $volname) = parse_volume_id($volid); | |
228 | my $scfg = storage_config($cfg, $storeid); | |
229 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
230 | ||
231 | $plugin->update_volume_notes($scfg, $storeid, $volname, $notes, $timeout); | |
232 | } | |
233 | ||
20ccac1b AD |
234 | sub volume_size_info { |
235 | my ($cfg, $volid, $timeout) = @_; | |
236 | ||
f18199e5 DM |
237 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
238 | if ($storeid) { | |
239 | my $scfg = storage_config($cfg, $storeid); | |
240 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
241 | return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout); | |
242 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
243 | return file_size_info($volid, $timeout); | |
244 | } else { | |
245 | return 0; | |
246 | } | |
20ccac1b AD |
247 | } |
248 | ||
7e6c05dc AD |
249 | sub volume_resize { |
250 | my ($cfg, $volid, $size, $running) = @_; | |
251 | ||
c7215573 FE |
252 | my $padding = (1024 - $size % 1024) % 1024; |
253 | $size = $size + $padding; | |
254 | ||
7e6c05dc AD |
255 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
256 | if ($storeid) { | |
257 | my $scfg = storage_config($cfg, $storeid); | |
258 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
259 | return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running); | |
260 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 261 | die "resize file/device '$volid' is not possible\n"; |
7e6c05dc | 262 | } else { |
f824c722 | 263 | die "unable to parse volume ID '$volid'\n"; |
7e6c05dc AD |
264 | } |
265 | } | |
266 | ||
1597f1f9 WL |
267 | sub volume_rollback_is_possible { |
268 | my ($cfg, $volid, $snap) = @_; | |
e0852ba7 | 269 | |
1597f1f9 WL |
270 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
271 | if ($storeid) { | |
272 | my $scfg = storage_config($cfg, $storeid); | |
273 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
274 | return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); | |
275 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 276 | die "snapshot rollback file/device '$volid' is not possible\n"; |
1597f1f9 | 277 | } else { |
f824c722 | 278 | die "unable to parse volume ID '$volid'\n"; |
1597f1f9 WL |
279 | } |
280 | } | |
281 | ||
db60719c | 282 | sub volume_snapshot { |
f5640e7d | 283 | my ($cfg, $volid, $snap) = @_; |
db60719c AD |
284 | |
285 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
286 | if ($storeid) { | |
287 | my $scfg = storage_config($cfg, $storeid); | |
288 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
f5640e7d | 289 | return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap); |
db60719c | 290 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 291 | die "snapshot file/device '$volid' is not possible\n"; |
db60719c | 292 | } else { |
f824c722 | 293 | die "unable to parse volume ID '$volid'\n"; |
db60719c AD |
294 | } |
295 | } | |
296 | ||
22a2a633 AD |
297 | sub volume_snapshot_rollback { |
298 | my ($cfg, $volid, $snap) = @_; | |
299 | ||
300 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
301 | if ($storeid) { | |
302 | my $scfg = storage_config($cfg, $storeid); | |
303 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b3f302c6 | 304 | $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); |
22a2a633 AD |
305 | return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap); |
306 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 307 | die "snapshot rollback file/device '$volid' is not possible\n"; |
22a2a633 | 308 | } else { |
f824c722 | 309 | die "unable to parse volume ID '$volid'\n"; |
22a2a633 AD |
310 | } |
311 | } | |
312 | ||
5753c9d1 AD |
313 | sub volume_snapshot_delete { |
314 | my ($cfg, $volid, $snap, $running) = @_; | |
315 | ||
316 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
317 | if ($storeid) { | |
318 | my $scfg = storage_config($cfg, $storeid); | |
319 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
27cc55d4 | 320 | return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running); |
5753c9d1 | 321 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 322 | die "snapshot delete file/device '$volid' is not possible\n"; |
5753c9d1 | 323 | } else { |
f824c722 | 324 | die "unable to parse volume ID '$volid'\n"; |
5753c9d1 AD |
325 | } |
326 | } | |
327 | ||
2c036838 SI |
328 | # check if a filesystem on top of a volume needs to flush its journal for |
329 | # consistency (see fsfreeze(8)) before a snapshot is taken - needed for | |
330 | # container mountpoints | |
331 | sub volume_snapshot_needs_fsfreeze { | |
332 | my ($cfg, $volid) = @_; | |
333 | ||
334 | my ($storeid, $volname) = parse_volume_id($volid); | |
335 | my $scfg = storage_config($cfg, $storeid); | |
336 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
337 | return $plugin->volume_snapshot_needs_fsfreeze(); | |
338 | } | |
339 | ||
2668adce FE |
340 | # check if a volume or snapshot supports a given feature |
341 | # $feature - one of: | |
342 | # clone - linked clone is possible | |
343 | # copy - full clone is possible | |
344 | # replicate - replication is possible | |
345 | # snapshot - taking a snapshot is possible | |
346 | # sparseinit - volume is sparsely initialized | |
347 | # template - conversion to base image is possible | |
348 | # $snap - check if the feature is supported for a given snapshot | |
349 | # $running - if the guest owning the volume is running | |
350 | # $opts - hash with further options: | |
351 | # valid_target_formats - list of formats for the target of a copy/clone | |
352 | # operation that the caller could work with. The | |
353 | # format of $volid is always considered valid and if | |
354 | # no list is specified, all formats are considered valid. | |
99473759 | 355 | sub volume_has_feature { |
e6f4eed4 | 356 | my ($cfg, $feature, $volid, $snap, $running, $opts) = @_; |
99473759 AD |
357 | |
358 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
359 | if ($storeid) { | |
360 | my $scfg = storage_config($cfg, $storeid); | |
361 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
e6f4eed4 | 362 | return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts); |
99473759 AD |
363 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
364 | return undef; | |
365 | } else { | |
366 | return undef; | |
367 | } | |
368 | } | |
369 | ||
aefe82ea | 370 | sub volume_snapshot_list { |
8b622c2d | 371 | my ($cfg, $volid) = @_; |
aefe82ea WL |
372 | |
373 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
374 | if ($storeid) { | |
375 | my $scfg = storage_config($cfg, $storeid); | |
376 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
8b622c2d | 377 | return $plugin->volume_snapshot_list($scfg, $storeid, $volname); |
aefe82ea WL |
378 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
379 | die "send file/device '$volid' is not possible\n"; | |
380 | } else { | |
381 | die "unable to parse volume ID '$volid'\n"; | |
382 | } | |
383 | # return an empty array if dataset does not exist. | |
aefe82ea WL |
384 | } |
385 | ||
1dc01b9f DM |
386 | sub get_image_dir { |
387 | my ($cfg, $storeid, $vmid) = @_; | |
b6cf0a66 | 388 | |
1dc01b9f DM |
389 | my $scfg = storage_config($cfg, $storeid); |
390 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 391 | |
1dc01b9f | 392 | my $path = $plugin->get_subdir($scfg, 'images'); |
b6cf0a66 | 393 | |
1dc01b9f | 394 | return $vmid ? "$path/$vmid" : $path; |
b6cf0a66 DM |
395 | } |
396 | ||
1dc01b9f | 397 | sub get_private_dir { |
b6cf0a66 DM |
398 | my ($cfg, $storeid, $vmid) = @_; |
399 | ||
1dc01b9f DM |
400 | my $scfg = storage_config($cfg, $storeid); |
401 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 402 | |
1dc01b9f | 403 | my $path = $plugin->get_subdir($scfg, 'rootdir'); |
d22a6133 | 404 | |
1dc01b9f | 405 | return $vmid ? "$path/$vmid" : $path; |
d22a6133 DM |
406 | } |
407 | ||
b6cf0a66 DM |
408 | sub get_iso_dir { |
409 | my ($cfg, $storeid) = @_; | |
410 | ||
1dc01b9f DM |
411 | my $scfg = storage_config($cfg, $storeid); |
412 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 413 | |
1dc01b9f | 414 | return $plugin->get_subdir($scfg, 'iso'); |
b6cf0a66 DM |
415 | } |
416 | ||
417 | sub get_vztmpl_dir { | |
418 | my ($cfg, $storeid) = @_; | |
419 | ||
1dc01b9f DM |
420 | my $scfg = storage_config($cfg, $storeid); |
421 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 422 | |
1dc01b9f | 423 | return $plugin->get_subdir($scfg, 'vztmpl'); |
b6cf0a66 DM |
424 | } |
425 | ||
568de3d1 DM |
426 | sub get_backup_dir { |
427 | my ($cfg, $storeid) = @_; | |
428 | ||
1dc01b9f DM |
429 | my $scfg = storage_config($cfg, $storeid); |
430 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 431 | |
1dc01b9f | 432 | return $plugin->get_subdir($scfg, 'backup'); |
b6cf0a66 DM |
433 | } |
434 | ||
435 | # library implementation | |
436 | ||
b6cf0a66 DM |
437 | sub parse_vmid { |
438 | my $vmid = shift; | |
439 | ||
440 | die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/; | |
441 | ||
442 | return int($vmid); | |
443 | } | |
444 | ||
787624df FG |
445 | # NOTE: basename and basevmid are always undef for LVM-thin, where the |
446 | # clone -> base reference is not encoded in the volume ID. | |
447 | # see note in PVE::Storage::LvmThinPlugin for details. | |
ec4b0dc7 AD |
448 | sub parse_volname { |
449 | my ($cfg, $volid) = @_; | |
450 | ||
451 | my ($storeid, $volname) = parse_volume_id($volid); | |
452 | ||
453 | my $scfg = storage_config($cfg, $storeid); | |
454 | ||
455 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
a6f12626 DM |
456 | |
457 | # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) | |
458 | ||
ec4b0dc7 AD |
459 | return $plugin->parse_volname($volname); |
460 | } | |
461 | ||
b6cf0a66 DM |
462 | sub parse_volume_id { |
463 | my ($volid, $noerr) = @_; | |
464 | ||
a7f3d909 | 465 | return PVE::Storage::Plugin::parse_volume_id($volid, $noerr); |
b6cf0a66 DM |
466 | } |
467 | ||
04a13668 DM |
468 | # test if we have read access to volid |
469 | sub check_volume_access { | |
470 | my ($rpcenv, $user, $cfg, $vmid, $volid) = @_; | |
471 | ||
472 | my ($sid, $volname) = parse_volume_id($volid, 1); | |
473 | if ($sid) { | |
474 | my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid); | |
475 | if ($vtype eq 'iso' || $vtype eq 'vztmpl') { | |
775fdc69 | 476 | # require at least read access to storage, (custom) templates/ISOs could be sensitive |
061b9ca6 | 477 | $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']); |
04a13668 DM |
478 | } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) { |
479 | # we are owner - allow access | |
480 | } elsif ($vtype eq 'backup' && $ownervm) { | |
481 | $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']); | |
482 | $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']); | |
483 | } else { | |
484 | # allow if we are Datastore administrator | |
485 | $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']); | |
486 | } | |
487 | } else { | |
488 | die "Only root can pass arbitrary filesystem paths." | |
489 | if $user ne 'root@pam'; | |
490 | } | |
491 | ||
492 | return undef; | |
493 | } | |
494 | ||
50853be2 FE |
495 | # NOTE: this check does not work for LVM-thin, where the clone -> base |
496 | # reference is not encoded in the volume ID. | |
497 | # see note in PVE::Storage::LvmThinPlugin for details. | |
498 | sub volume_is_base_and_used { | |
499 | my ($cfg, $volid) = @_; | |
500 | ||
501 | my ($storeid, $volname) = parse_volume_id($volid); | |
502 | my $scfg = storage_config($cfg, $storeid); | |
503 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
17fb7e42 FG |
504 | |
505 | my ($vtype, $name, $vmid, undef, undef, $isBase, undef) = | |
506 | $plugin->parse_volname($volname); | |
507 | ||
508 | if ($isBase) { | |
509 | my $vollist = $plugin->list_images($storeid, $scfg); | |
510 | foreach my $info (@$vollist) { | |
511 | my (undef, $tmpvolname) = parse_volume_id($info->{volid}); | |
512 | my $basename = undef; | |
513 | my $basevmid = undef; | |
514 | ||
515 | eval{ | |
516 | (undef, undef, undef, $basename, $basevmid) = | |
517 | $plugin->parse_volname($tmpvolname); | |
518 | }; | |
519 | ||
520 | if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) { | |
521 | return 1; | |
522 | } | |
523 | } | |
524 | } | |
525 | return 0; | |
17fb7e42 FG |
526 | } |
527 | ||
b6cf0a66 DM |
528 | # try to map a filesystem path to a volume identifier |
529 | sub path_to_volume_id { | |
530 | my ($cfg, $path) = @_; | |
531 | ||
532 | my $ids = $cfg->{ids}; | |
533 | ||
1dc01b9f | 534 | my ($sid, $volname) = parse_volume_id($path, 1); |
b6cf0a66 | 535 | if ($sid) { |
1dc01b9f | 536 | if (my $scfg = $ids->{$sid}) { |
188aca38 | 537 | if ($scfg->{path}) { |
1dc01b9f DM |
538 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
539 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
b6cf0a66 DM |
540 | return ($vtype, $path); |
541 | } | |
542 | } | |
543 | return (''); | |
544 | } | |
545 | ||
1a3459ac | 546 | # Note: abs_path() return undef if $path doesn not exist |
75d75990 DM |
547 | # for example when nfs storage is not mounted |
548 | $path = abs_path($path) || $path; | |
b6cf0a66 DM |
549 | |
550 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
551 | my $scfg = $ids->{$sid}; |
552 | next if !$scfg->{path}; | |
553 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
554 | my $imagedir = $plugin->get_subdir($scfg, 'images'); | |
555 | my $isodir = $plugin->get_subdir($scfg, 'iso'); | |
556 | my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl'); | |
557 | my $backupdir = $plugin->get_subdir($scfg, 'backup'); | |
558 | my $privatedir = $plugin->get_subdir($scfg, 'rootdir'); | |
73fcb7bf | 559 | my $snippetsdir = $plugin->get_subdir($scfg, 'snippets'); |
b6cf0a66 DM |
560 | |
561 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
562 | my $vmid = $1; | |
563 | my $name = $2; | |
fcbec654 DM |
564 | |
565 | my $vollist = $plugin->list_images($sid, $scfg, $vmid); | |
566 | foreach my $info (@$vollist) { | |
567 | my ($storeid, $volname) = parse_volume_id($info->{volid}); | |
568 | my $volpath = $plugin->path($scfg, $volname, $storeid); | |
569 | if ($volpath eq $path) { | |
570 | return ('images', $info->{volid}); | |
571 | } | |
572 | } | |
4c693491 | 573 | } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) { |
b6cf0a66 | 574 | my $name = $1; |
1a3459ac | 575 | return ('iso', "$sid:iso/$name"); |
b6cf0a66 DM |
576 | } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) { |
577 | my $name = $1; | |
578 | return ('vztmpl', "$sid:vztmpl/$name"); | |
1ac17c74 DM |
579 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { |
580 | my $vmid = $1; | |
581 | return ('rootdir', "$sid:rootdir/$vmid"); | |
277cafc0 | 582 | } elsif ($path =~ m!^$backupdir/([^/]+\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) { |
568de3d1 | 583 | my $name = $1; |
892dc992 | 584 | return ('backup', "$sid:backup/$name"); |
73fcb7bf AA |
585 | } elsif ($path =~ m!^$snippetsdir/([^/]+)$!) { |
586 | my $name = $1; | |
587 | return ('snippets', "$sid:snippets/$name"); | |
b6cf0a66 DM |
588 | } |
589 | } | |
590 | ||
591 | # can't map path to volume id | |
592 | return (''); | |
593 | } | |
594 | ||
595 | sub path { | |
207ea852 | 596 | my ($cfg, $volid, $snapname) = @_; |
b6cf0a66 | 597 | |
1dc01b9f | 598 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 599 | |
1dc01b9f | 600 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 601 | |
1dc01b9f | 602 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
207ea852 | 603 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname); |
2494896a | 604 | return wantarray ? ($path, $owner, $vtype) : $path; |
b6cf0a66 DM |
605 | } |
606 | ||
35fbb2e6 | 607 | sub abs_filesystem_path { |
4b84ad5e | 608 | my ($cfg, $volid, $allow_blockdev) = @_; |
35fbb2e6 DM |
609 | |
610 | my $path; | |
6ed43d81 TL |
611 | if (parse_volume_id ($volid, 1)) { |
612 | activate_volumes($cfg, [ $volid ]); | |
35fbb2e6 DM |
613 | $path = PVE::Storage::path($cfg, $volid); |
614 | } else { | |
4b84ad5e | 615 | if (-f $volid || ($allow_blockdev && -b $volid)) { |
35fbb2e6 DM |
616 | my $abspath = abs_path($volid); |
617 | if ($abspath && $abspath =~ m|^(/.+)$|) { | |
618 | $path = $1; # untaint any path | |
619 | } | |
620 | } | |
621 | } | |
4b84ad5e DJ |
622 | die "can't find file '$volid'\n" |
623 | if !($path && (-f $path || ($allow_blockdev && -b $path))); | |
35fbb2e6 DM |
624 | |
625 | return $path; | |
626 | } | |
627 | ||
683a3f46 FE |
628 | my $volname_for_storage = sub { |
629 | my ($cfg, $volid, $target_storeid) = @_; | |
630 | ||
631 | my (undef, $name, $vmid, undef, undef, undef, $format) = parse_volname($cfg, $volid); | |
632 | my $target_scfg = storage_config($cfg, $target_storeid); | |
633 | ||
634 | my (undef, $valid_formats) = PVE::Storage::Plugin::default_format($target_scfg); | |
635 | my $format_is_valid = grep { $_ eq $format } @$valid_formats; | |
636 | die "unsupported format '$format' for storage type $target_scfg->{type}\n" if !$format_is_valid; | |
637 | ||
638 | (my $name_without_extension = $name) =~ s/\.$format$//; | |
639 | ||
640 | if ($target_scfg->{path}) { | |
641 | return "$vmid/$name_without_extension.$format"; | |
642 | } else { | |
643 | return "$name_without_extension"; | |
644 | } | |
645 | }; | |
646 | ||
b6cf0a66 | 647 | sub storage_migrate { |
dc3655a1 FE |
648 | my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_; |
649 | ||
650 | my $base_snapshot = $opts->{base_snapshot}; | |
651 | my $snapshot = $opts->{snapshot}; | |
652 | my $ratelimit_bps = $opts->{ratelimit_bps}; | |
653 | my $insecure = $opts->{insecure}; | |
654 | my $with_snapshots = $opts->{with_snapshots} ? 1 : 0; | |
655 | my $allow_rename = $opts->{allow_rename} ? 1 : 0; | |
b6cf0a66 | 656 | |
6bf56298 | 657 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 658 | |
6bf56298 | 659 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
660 | |
661 | # no need to migrate shared content | |
a97d3ee4 | 662 | return $volid if $storeid eq $target_storeid && $scfg->{shared}; |
b6cf0a66 | 663 | |
6bf56298 | 664 | my $tcfg = storage_config($cfg, $target_storeid); |
b6cf0a66 | 665 | |
683a3f46 FE |
666 | my $target_volname; |
667 | if ($opts->{target_volname}) { | |
668 | $target_volname = $opts->{target_volname}; | |
669 | } elsif ($scfg->{type} eq $tcfg->{type}) { | |
670 | $target_volname = $volname; | |
671 | } else { | |
672 | $target_volname = $volname_for_storage->($cfg, $volid, $target_storeid); | |
673 | } | |
674 | ||
b6cf0a66 DM |
675 | my $target_volid = "${target_storeid}:${target_volname}"; |
676 | ||
4b4c580d | 677 | my $target_ip = $target_sshinfo->{ip}; |
b6cf0a66 | 678 | |
65bb9859 FG |
679 | my $ssh = PVE::SSHInfo::ssh_info_to_command($target_sshinfo); |
680 | my $ssh_base = PVE::SSHInfo::ssh_info_to_command_base($target_sshinfo); | |
47cea194 | 681 | local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base); |
b6cf0a66 | 682 | |
93d1812e FE |
683 | my @cstream; |
684 | if (defined($ratelimit_bps)) { | |
685 | @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ]); | |
686 | $logfunc->("using a bandwidth limit of $ratelimit_bps bps for transferring '$volid'") if $logfunc; | |
687 | } | |
01f7e902 | 688 | |
da72898c WB |
689 | my $migration_snapshot; |
690 | if (!defined($snapshot)) { | |
691 | if ($scfg->{type} eq 'zfspool') { | |
692 | $migration_snapshot = 1; | |
693 | $snapshot = '__migration__'; | |
1dc01b9f | 694 | } |
da72898c | 695 | } |
e0852ba7 | 696 | |
cfdffd8a | 697 | my @formats = volume_transfer_formats($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots); |
da72898c WB |
698 | die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats; |
699 | my $format = $formats[0]; | |
7459cb3d | 700 | |
228e5be9 | 701 | my $import_fn = '-'; # let pvesm import read from stdin per default |
da72898c | 702 | if ($insecure) { |
228e5be9 TL |
703 | my $net = $target_sshinfo->{network} // $target_sshinfo->{ip}; |
704 | $import_fn = "tcp://$net"; | |
da72898c | 705 | } |
7ba34faa | 706 | |
a97d3ee4 FE |
707 | my $target_apiver = 1; # if there is no apiinfo call, assume 1 |
708 | my $get_api_version = [@$ssh, 'pvesm', 'apiinfo']; | |
709 | my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; }; | |
710 | eval { run_command($get_api_version, logfunc => $match_api_version); }; | |
711 | ||
e8a7e764 | 712 | my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots]; |
cfdffd8a | 713 | my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots]; |
da72898c WB |
714 | if (defined($snapshot)) { |
715 | push @$send, '-snapshot', $snapshot | |
716 | } | |
717 | if ($migration_snapshot) { | |
718 | push @$recv, '-delete-snapshot', $snapshot; | |
719 | } | |
a97d3ee4 | 720 | push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5; |
3d621977 | 721 | |
da72898c WB |
722 | if (defined($base_snapshot)) { |
723 | # Check if the snapshot exists on the remote side: | |
724 | push @$send, '-base', $base_snapshot; | |
725 | push @$recv, '-base', $base_snapshot; | |
726 | } | |
ac191ec7 | 727 | |
a97d3ee4 FE |
728 | my $new_volid; |
729 | my $pattern = volume_imported_message(undef, 1); | |
730 | my $match_volid_and_log = sub { | |
731 | my $line = shift; | |
732 | ||
733 | $new_volid = $1 if ($line =~ $pattern); | |
734 | ||
735 | if ($logfunc) { | |
736 | chomp($line); | |
737 | $logfunc->($line); | |
738 | } | |
739 | }; | |
740 | ||
da72898c | 741 | volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot; |
5324ceff FE |
742 | |
743 | if (defined($snapshot)) { | |
744 | activate_volumes($cfg, [$volid], $snapshot); | |
745 | } else { | |
746 | activate_volumes($cfg, [$volid]); | |
747 | } | |
748 | ||
da72898c WB |
749 | eval { |
750 | if ($insecure) { | |
57acd6a1 FE |
751 | my $input = IO::File->new(); |
752 | my $info = IO::File->new(); | |
753 | open3($input, $info, $info, @{$recv}) | |
da72898c | 754 | or die "receive command failed: $!\n"; |
57acd6a1 FE |
755 | close($input); |
756 | ||
7cdc75a2 FE |
757 | my $try_ip = <$info> // ''; |
758 | my ($ip) = $try_ip =~ /^($PVE::Tools::IPRE)$/ # untaint | |
759 | or die "no tunnel IP received, got '$try_ip'\n"; | |
760 | ||
761 | my $try_port = <$info> // ''; | |
762 | my ($port) = $try_port =~ /^(\d+)$/ # untaint | |
763 | or die "no tunnel port received, got '$try_port'\n"; | |
764 | ||
da72898c WB |
765 | my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM) |
766 | or die "failed to connect to tunnel at $ip:$port\n"; | |
767 | # we won't be reading from the socket | |
768 | shutdown($socket, 0); | |
57acd6a1 FE |
769 | |
770 | eval { run_command([$send, @cstream], output => '>&'.fileno($socket), errfunc => $logfunc); }; | |
771 | my $send_error = $@; | |
772 | ||
da72898c WB |
773 | # don't close the connection entirely otherwise the receiving end |
774 | # might not get all buffered data (and fails with 'connection reset by peer') | |
775 | shutdown($socket, 1); | |
aca83310 FE |
776 | |
777 | # wait for the remote process to finish | |
a97d3ee4 FE |
778 | while (my $line = <$info>) { |
779 | $match_volid_and_log->("[$target_sshinfo->{name}] $line"); | |
aca83310 FE |
780 | } |
781 | ||
da72898c WB |
782 | # now close the socket |
783 | close($socket); | |
784 | if (!close($info)) { # does waitpid() | |
785 | die "import failed: $!\n" if $!; | |
786 | die "import failed: exit code ".($?>>8)."\n"; | |
0a29ad61 | 787 | } |
57acd6a1 FE |
788 | |
789 | die $send_error if $send_error; | |
0a29ad61 | 790 | } else { |
a97d3ee4 | 791 | run_command([$send, @cstream, $recv], logfunc => $match_volid_and_log); |
0a29ad61 | 792 | } |
a97d3ee4 FE |
793 | |
794 | die "unable to get ID of the migrated volume\n" | |
795 | if !defined($new_volid) && $target_apiver >= 5; | |
da72898c WB |
796 | }; |
797 | my $err = $@; | |
798 | warn "send/receive failed, cleaning up snapshot(s)..\n" if $err; | |
799 | if ($migration_snapshot) { | |
800 | eval { volume_snapshot_delete($cfg, $volid, $snapshot, 0) }; | |
801 | warn "could not remove source snapshot: $@\n" if $@; | |
b6cf0a66 | 802 | } |
da72898c | 803 | die $err if $err; |
a97d3ee4 FE |
804 | |
805 | return $new_volid // $target_volid; | |
b6cf0a66 DM |
806 | } |
807 | ||
2502b33b | 808 | sub vdisk_clone { |
7bbc4004 | 809 | my ($cfg, $volid, $vmid, $snap) = @_; |
1a3459ac | 810 | |
2502b33b DM |
811 | my ($storeid, $volname) = parse_volume_id($volid); |
812 | ||
813 | my $scfg = storage_config($cfg, $storeid); | |
814 | ||
815 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 816 | |
2502b33b DM |
817 | activate_storage($cfg, $storeid); |
818 | ||
819 | # lock shared storage | |
820 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
7bbc4004 | 821 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); |
2502b33b DM |
822 | return "$storeid:$volname"; |
823 | }); | |
824 | } | |
825 | ||
826 | sub vdisk_create_base { | |
827 | my ($cfg, $volid) = @_; | |
828 | ||
829 | my ($storeid, $volname) = parse_volume_id($volid); | |
830 | ||
831 | my $scfg = storage_config($cfg, $storeid); | |
832 | ||
833 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 834 | |
2502b33b DM |
835 | activate_storage($cfg, $storeid); |
836 | ||
837 | # lock shared storage | |
838 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
839 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
840 | return "$storeid:$volname"; | |
841 | }); | |
842 | } | |
843 | ||
40d69893 DM |
844 | sub map_volume { |
845 | my ($cfg, $volid, $snapname) = @_; | |
846 | ||
847 | my ($storeid, $volname) = parse_volume_id($volid); | |
848 | ||
849 | my $scfg = storage_config($cfg, $storeid); | |
850 | ||
851 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
852 | ||
853 | return $plugin->map_volume($storeid, $scfg, $volname, $snapname); | |
854 | } | |
855 | ||
856 | sub unmap_volume { | |
857 | my ($cfg, $volid, $snapname) = @_; | |
858 | ||
859 | my ($storeid, $volname) = parse_volume_id($volid); | |
860 | ||
861 | my $scfg = storage_config($cfg, $storeid); | |
862 | ||
863 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
864 | ||
865 | return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname); | |
866 | } | |
867 | ||
1dc01b9f DM |
868 | sub vdisk_alloc { |
869 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
b6cf0a66 | 870 | |
82fc923f | 871 | die "no storage ID specified\n" if !$storeid; |
b6cf0a66 | 872 | |
1dc01b9f | 873 | PVE::JSONSchema::parse_storage_id($storeid); |
b6cf0a66 | 874 | |
1dc01b9f | 875 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 876 | |
1dc01b9f | 877 | die "no VMID specified\n" if !$vmid; |
b6cf0a66 | 878 | |
1dc01b9f | 879 | $vmid = parse_vmid($vmid); |
b6cf0a66 | 880 | |
1dc01b9f | 881 | my $defformat = PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 | 882 | |
1dc01b9f | 883 | $fmt = $defformat if !$fmt; |
b6cf0a66 | 884 | |
1dc01b9f | 885 | activate_storage($cfg, $storeid); |
3af60e62 | 886 | |
1dc01b9f | 887 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 888 | |
1dc01b9f DM |
889 | # lock shared storage |
890 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
afdfbe55 WB |
891 | my $old_umask = umask(umask|0037); |
892 | my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) }; | |
893 | my $err = $@; | |
894 | umask $old_umask; | |
895 | die $err if $err; | |
1dc01b9f DM |
896 | return "$storeid:$volname"; |
897 | }); | |
b6cf0a66 DM |
898 | } |
899 | ||
1dc01b9f DM |
900 | sub vdisk_free { |
901 | my ($cfg, $volid) = @_; | |
b6cf0a66 | 902 | |
1dc01b9f | 903 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f | 904 | my $scfg = storage_config($cfg, $storeid); |
1dc01b9f | 905 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1a3459ac | 906 | |
1dc01b9f | 907 | activate_storage($cfg, $storeid); |
b6cf0a66 | 908 | |
1dc01b9f | 909 | my $cleanup_worker; |
b6cf0a66 | 910 | |
1dc01b9f DM |
911 | # lock shared storage |
912 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
787624df | 913 | # LVM-thin allows deletion of still referenced base volumes! |
17fb7e42 | 914 | die "base volume '$volname' is still in use by linked clones\n" |
50853be2 | 915 | if volume_is_base_and_used($cfg, $volid); |
32437ed2 | 916 | |
17fb7e42 | 917 | my (undef, undef, undef, undef, undef, $isBase, $format) = |
32437ed2 | 918 | $plugin->parse_volname($volname); |
35533c68 | 919 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); |
1dc01b9f | 920 | }); |
b6cf0a66 | 921 | |
1dc01b9f | 922 | return if !$cleanup_worker; |
b6cf0a66 | 923 | |
1dc01b9f DM |
924 | my $rpcenv = PVE::RPCEnvironment::get(); |
925 | my $authuser = $rpcenv->get_user(); | |
b6cf0a66 | 926 | |
1dc01b9f | 927 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); |
b6cf0a66 DM |
928 | } |
929 | ||
b6cf0a66 | 930 | sub vdisk_list { |
2c5246e1 | 931 | my ($cfg, $storeid, $vmid, $vollist, $ctype) = @_; |
b6cf0a66 DM |
932 | |
933 | my $ids = $cfg->{ids}; | |
934 | ||
935 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
936 | ||
d96b789a | 937 | my $res = $storeid ? { $storeid => [] } : {}; |
b6cf0a66 DM |
938 | |
939 | # prepare/activate/refresh all storages | |
940 | ||
b6cf0a66 DM |
941 | my $storage_list = []; |
942 | if ($vollist) { | |
943 | foreach my $volid (@$vollist) { | |
1dc01b9f DM |
944 | my ($sid, undef) = parse_volume_id($volid); |
945 | next if !defined($ids->{$sid}); | |
b6cf0a66 DM |
946 | next if !storage_check_enabled($cfg, $sid, undef, 1); |
947 | push @$storage_list, $sid; | |
b6cf0a66 DM |
948 | } |
949 | } else { | |
950 | foreach my $sid (keys %$ids) { | |
951 | next if $storeid && $storeid ne $sid; | |
952 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
c43655d2 | 953 | my $content = $ids->{$sid}->{content}; |
2c5246e1 | 954 | next if defined($ctype) && !$content->{$ctype}; |
c43655d2 | 955 | next if !($content->{rootdir} || $content->{images}); |
b6cf0a66 | 956 | push @$storage_list, $sid; |
b6cf0a66 DM |
957 | } |
958 | } | |
959 | ||
1dc01b9f | 960 | my $cache = {}; |
b6cf0a66 | 961 | |
1dc01b9f | 962 | activate_storage_list($cfg, $storage_list, $cache); |
b6cf0a66 | 963 | |
d96b789a | 964 | for my $sid ($storage_list->@*) { |
1dc01b9f | 965 | next if $storeid && $storeid ne $sid; |
b6cf0a66 | 966 | |
1dc01b9f DM |
967 | my $scfg = $ids->{$sid}; |
968 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
969 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
b6cf0a66 DM |
970 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; |
971 | } | |
972 | ||
973 | return $res; | |
974 | } | |
975 | ||
c2fc9fbe DM |
976 | sub template_list { |
977 | my ($cfg, $storeid, $tt) = @_; | |
978 | ||
979 | die "unknown template type '$tt'\n" | |
980 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets'); | |
981 | ||
982 | my $ids = $cfg->{ids}; | |
983 | ||
984 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
985 | ||
986 | my $res = {}; | |
987 | ||
988 | # query the storage | |
989 | foreach my $sid (keys %$ids) { | |
990 | next if $storeid && $storeid ne $sid; | |
991 | ||
992 | my $scfg = $ids->{$sid}; | |
993 | my $type = $scfg->{type}; | |
994 | ||
995 | next if !$scfg->{content}->{$tt}; | |
996 | ||
997 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
998 | ||
999 | $res->{$sid} = volume_list($cfg, $sid, undef, $tt); | |
1000 | } | |
1001 | ||
1002 | return $res; | |
1003 | } | |
1004 | ||
37ba0aea DM |
1005 | sub volume_list { |
1006 | my ($cfg, $storeid, $vmid, $content) = @_; | |
1007 | ||
be785439 | 1008 | my @ctypes = qw(rootdir images vztmpl iso backup snippets); |
37ba0aea DM |
1009 | |
1010 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
1011 | ||
1012 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
1013 | ||
c2fc9fbe | 1014 | $cts = [ grep { defined($scfg->{content}->{$_}) } @$cts ]; |
37ba0aea | 1015 | |
c2fc9fbe | 1016 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
37ba0aea | 1017 | |
c2fc9fbe DM |
1018 | activate_storage($cfg, $storeid); |
1019 | ||
1020 | my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts); | |
1021 | ||
1022 | @$res = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @$res; | |
37ba0aea DM |
1023 | |
1024 | return $res; | |
1025 | } | |
1026 | ||
b6cf0a66 DM |
1027 | sub uevent_seqnum { |
1028 | ||
1029 | my $filename = "/sys/kernel/uevent_seqnum"; | |
1030 | ||
1031 | my $seqnum = 0; | |
1dc01b9f | 1032 | if (my $fh = IO::File->new($filename, "r")) { |
b6cf0a66 DM |
1033 | my $line = <$fh>; |
1034 | if ($line =~ m/^(\d+)$/) { | |
1dc01b9f | 1035 | $seqnum = int($1); |
b6cf0a66 DM |
1036 | } |
1037 | close ($fh); | |
1038 | } | |
1039 | return $seqnum; | |
1040 | } | |
1041 | ||
f3d4ef46 | 1042 | sub activate_storage { |
1dc01b9f | 1043 | my ($cfg, $storeid, $cache) = @_; |
b6cf0a66 | 1044 | |
f3d4ef46 DM |
1045 | $cache = {} if !$cache; |
1046 | ||
b6cf0a66 DM |
1047 | my $scfg = storage_check_enabled($cfg, $storeid); |
1048 | ||
1dc01b9f | 1049 | return if $cache->{activated}->{$storeid}; |
b6cf0a66 | 1050 | |
1dc01b9f | 1051 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; |
b6cf0a66 | 1052 | |
1dc01b9f | 1053 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 1054 | |
1dc01b9f DM |
1055 | if ($scfg->{base}) { |
1056 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
f3d4ef46 DM |
1057 | activate_storage($cfg, $baseid, $cache); |
1058 | } | |
1059 | ||
1060 | if (!$plugin->check_connection($storeid, $scfg)) { | |
1061 | die "storage '$storeid' is not online\n"; | |
b6cf0a66 DM |
1062 | } |
1063 | ||
1dc01b9f DM |
1064 | $plugin->activate_storage($storeid, $scfg, $cache); |
1065 | ||
b6cf0a66 DM |
1066 | my $newseq = uevent_seqnum (); |
1067 | ||
1068 | # only call udevsettle if there are events | |
1dc01b9f | 1069 | if ($newseq > $cache->{uevent_seqnum}) { |
d3a5e309 | 1070 | system ("udevadm settle --timeout=30"); # ignore errors |
1dc01b9f | 1071 | $cache->{uevent_seqnum} = $newseq; |
b6cf0a66 DM |
1072 | } |
1073 | ||
1dc01b9f | 1074 | $cache->{activated}->{$storeid} = 1; |
b6cf0a66 DM |
1075 | } |
1076 | ||
1077 | sub activate_storage_list { | |
1dc01b9f | 1078 | my ($cfg, $storeid_list, $cache) = @_; |
b6cf0a66 | 1079 | |
1dc01b9f | 1080 | $cache = {} if !$cache; |
b6cf0a66 DM |
1081 | |
1082 | foreach my $storeid (@$storeid_list) { | |
f3d4ef46 | 1083 | activate_storage($cfg, $storeid, $cache); |
b6cf0a66 DM |
1084 | } |
1085 | } | |
1086 | ||
1dc01b9f DM |
1087 | sub deactivate_storage { |
1088 | my ($cfg, $storeid) = @_; | |
1089 | ||
1090 | my $scfg = storage_config ($cfg, $storeid); | |
1091 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 1092 | |
1dc01b9f DM |
1093 | my $cache = {}; |
1094 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
b6cf0a66 DM |
1095 | } |
1096 | ||
1097 | sub activate_volumes { | |
02e797b8 | 1098 | my ($cfg, $vollist, $snapname) = @_; |
6703353b DM |
1099 | |
1100 | return if !($vollist && scalar(@$vollist)); | |
1101 | ||
b6cf0a66 DM |
1102 | my $storagehash = {}; |
1103 | foreach my $volid (@$vollist) { | |
1dc01b9f | 1104 | my ($storeid, undef) = parse_volume_id($volid); |
b6cf0a66 DM |
1105 | $storagehash->{$storeid} = 1; |
1106 | } | |
1107 | ||
1dc01b9f DM |
1108 | my $cache = {}; |
1109 | ||
1110 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
b6cf0a66 DM |
1111 | |
1112 | foreach my $volid (@$vollist) { | |
5521b580 | 1113 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f DM |
1114 | my $scfg = storage_config($cfg, $storeid); |
1115 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
02e797b8 | 1116 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); |
b6cf0a66 DM |
1117 | } |
1118 | } | |
1119 | ||
1120 | sub deactivate_volumes { | |
02e797b8 | 1121 | my ($cfg, $vollist, $snapname) = @_; |
b6cf0a66 | 1122 | |
6703353b DM |
1123 | return if !($vollist && scalar(@$vollist)); |
1124 | ||
1dc01b9f DM |
1125 | my $cache = {}; |
1126 | ||
6703353b | 1127 | my @errlist = (); |
b6cf0a66 | 1128 | foreach my $volid (@$vollist) { |
1dc01b9f | 1129 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 1130 | |
1dc01b9f DM |
1131 | my $scfg = storage_config($cfg, $storeid); |
1132 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 1133 | |
1dc01b9f | 1134 | eval { |
02e797b8 | 1135 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); |
1dc01b9f DM |
1136 | }; |
1137 | if (my $err = $@) { | |
1138 | warn $err; | |
1139 | push @errlist, $volid; | |
b6cf0a66 DM |
1140 | } |
1141 | } | |
6703353b | 1142 | |
82fc923f | 1143 | die "volume deactivation failed: " . join(' ', @errlist) |
6703353b | 1144 | if scalar(@errlist); |
b6cf0a66 DM |
1145 | } |
1146 | ||
1a3459ac | 1147 | sub storage_info { |
856c54bd | 1148 | my ($cfg, $content, $includeformat) = @_; |
b6cf0a66 DM |
1149 | |
1150 | my $ids = $cfg->{ids}; | |
1151 | ||
1152 | my $info = {}; | |
ff3badd8 | 1153 | |
583c2802 | 1154 | my @ctypes = PVE::Tools::split_list($content); |
ff3badd8 | 1155 | |
b6cf0a66 DM |
1156 | my $slist = []; |
1157 | foreach my $storeid (keys %$ids) { | |
6ce4f724 | 1158 | my $storage_enabled = defined(storage_check_enabled($cfg, $storeid, undef, 1)); |
b6cf0a66 | 1159 | |
d73060be DM |
1160 | if (defined($content)) { |
1161 | my $want_ctype = 0; | |
1162 | foreach my $ctype (@ctypes) { | |
1163 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
1164 | $want_ctype = 1; | |
1165 | last; | |
1166 | } | |
583c2802 | 1167 | } |
6ce4f724 | 1168 | next if !$want_ctype || !$storage_enabled; |
583c2802 | 1169 | } |
ff3badd8 | 1170 | |
b6cf0a66 DM |
1171 | my $type = $ids->{$storeid}->{type}; |
1172 | ||
1a3459ac | 1173 | $info->{$storeid} = { |
b6cf0a66 | 1174 | type => $type, |
1a3459ac DM |
1175 | total => 0, |
1176 | avail => 0, | |
1177 | used => 0, | |
04a2e4f3 | 1178 | shared => $ids->{$storeid}->{shared} ? 1 : 0, |
1dc01b9f | 1179 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), |
b6cf0a66 | 1180 | active => 0, |
6ce4f724 | 1181 | enabled => $storage_enabled ? 1 : 0, |
b6cf0a66 DM |
1182 | }; |
1183 | ||
b6cf0a66 DM |
1184 | push @$slist, $storeid; |
1185 | } | |
1186 | ||
1dc01b9f | 1187 | my $cache = {}; |
b6cf0a66 | 1188 | |
b6cf0a66 DM |
1189 | foreach my $storeid (keys %$ids) { |
1190 | my $scfg = $ids->{$storeid}; | |
b43b073b | 1191 | |
b6cf0a66 | 1192 | next if !$info->{$storeid}; |
b43b073b | 1193 | next if !$info->{$storeid}->{enabled}; |
b6cf0a66 | 1194 | |
856c54bd DC |
1195 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1196 | if ($includeformat) { | |
1197 | my $pd = $plugin->plugindata(); | |
1198 | $info->{$storeid}->{format} = $pd->{format} | |
1199 | if $pd->{format}; | |
1200 | $info->{$storeid}->{select_existing} = $pd->{select_existing} | |
1201 | if $pd->{select_existing}; | |
1202 | } | |
1203 | ||
f3d4ef46 DM |
1204 | eval { activate_storage($cfg, $storeid, $cache); }; |
1205 | if (my $err = $@) { | |
1206 | warn $err; | |
1207 | next; | |
1208 | } | |
1209 | ||
41aacc6c | 1210 | my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); }; |
7028645e | 1211 | warn $@ if $@; |
1dc01b9f | 1212 | next if !$active; |
ff3badd8 DM |
1213 | $info->{$storeid}->{total} = int($total); |
1214 | $info->{$storeid}->{avail} = int($avail); | |
1215 | $info->{$storeid}->{used} = int($used); | |
1dc01b9f | 1216 | $info->{$storeid}->{active} = $active; |
b6cf0a66 DM |
1217 | } |
1218 | ||
1219 | return $info; | |
1220 | } | |
1221 | ||
1222 | sub resolv_server { | |
1223 | my ($server) = @_; | |
1a3459ac | 1224 | |
c67daeac WB |
1225 | my ($packed_ip, $family); |
1226 | eval { | |
1227 | my @res = PVE::Tools::getaddrinfo_all($server); | |
1228 | $family = $res[0]->{family}; | |
1229 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1230 | }; | |
b6cf0a66 | 1231 | if (defined $packed_ip) { |
ee302b1c | 1232 | return Socket::inet_ntop($family, $packed_ip); |
b6cf0a66 DM |
1233 | } |
1234 | return undef; | |
1235 | } | |
1236 | ||
1237 | sub scan_nfs { | |
1238 | my ($server_in) = @_; | |
1239 | ||
1240 | my $server; | |
1241 | if (!($server = resolv_server ($server_in))) { | |
1242 | die "unable to resolve address for server '${server_in}'\n"; | |
1243 | } | |
1244 | ||
1245 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1246 | ||
1247 | my $res = {}; | |
f81372ac | 1248 | run_command($cmd, outfunc => sub { |
b6cf0a66 DM |
1249 | my $line = shift; |
1250 | ||
1251 | # note: howto handle white spaces in export path?? | |
1252 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1253 | $res->{$1} = $2; | |
1254 | } | |
1255 | }); | |
1256 | ||
1257 | return $res; | |
1258 | } | |
1259 | ||
4cab0acd WL |
1260 | sub scan_cifs { |
1261 | my ($server_in, $user, $password, $domain) = @_; | |
1262 | ||
436773fd TL |
1263 | my $server = resolv_server($server_in); |
1264 | die "unable to resolve address for server '${server_in}'\n" if !$server; | |
4cab0acd | 1265 | |
436773fd | 1266 | # we only support Windows 2012 and newer, so just use smb3 |
4cab0acd | 1267 | my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server]; |
fd55f51e | 1268 | push @$cmd, '-W', $domain if defined($domain); |
afaa98f1 | 1269 | |
8594155a | 1270 | push @$cmd, '-N' if !defined($password); |
afaa98f1 TL |
1271 | local $ENV{USER} = $user if defined($user); |
1272 | local $ENV{PASSWD} = $password if defined($password); | |
4cab0acd WL |
1273 | |
1274 | my $res = {}; | |
be18d6da | 1275 | my $err = ''; |
4cab0acd | 1276 | run_command($cmd, |
436773fd | 1277 | noerr => 1, |
be18d6da TL |
1278 | errfunc => sub { |
1279 | $err .= "$_[0]\n" | |
1280 | }, | |
436773fd TL |
1281 | outfunc => sub { |
1282 | my $line = shift; | |
1283 | if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) { | |
1284 | $res->{$1} = $2; | |
c42d8655 TL |
1285 | } elsif ($line =~ m/(NT_STATUS_(\S+))/) { |
1286 | my $status = $1; | |
1287 | $err .= "unexpected status: $1\n" if uc($1) ne 'SUCCESS'; | |
436773fd TL |
1288 | } |
1289 | }, | |
4cab0acd | 1290 | ); |
d37729b9 TL |
1291 | # only die if we got no share, else it's just some followup check error |
1292 | # (like workgroup querying) | |
1293 | raise($err) if $err && !%$res; | |
4cab0acd WL |
1294 | |
1295 | return $res; | |
1296 | } | |
1297 | ||
584d97f6 DM |
1298 | sub scan_zfs { |
1299 | ||
3881e680 | 1300 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-Hp', '-o', 'name,avail,used']; |
584d97f6 DM |
1301 | |
1302 | my $res = []; | |
1303 | run_command($cmd, outfunc => sub { | |
1304 | my $line = shift; | |
1305 | ||
1306 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
3932390b | 1307 | my ($pool, $size_str, $used_str) = ($1, $2, $3); |
3881e680 AL |
1308 | my $size = $size_str + 0; |
1309 | my $used = $used_str + 0; | |
48e27f79 | 1310 | # ignore subvolumes generated by our ZFSPoolPlugin |
851658c3 WL |
1311 | return if $pool =~ m!/subvol-\d+-[^/]+$!; |
1312 | return if $pool =~ m!/basevol-\d+-[^/]+$!; | |
3932390b | 1313 | push @$res, { pool => $pool, size => $size, free => $size-$used }; |
584d97f6 DM |
1314 | } |
1315 | }); | |
1316 | ||
1317 | return $res; | |
1318 | } | |
1319 | ||
b6cf0a66 DM |
1320 | sub resolv_portal { |
1321 | my ($portal, $noerr) = @_; | |
1322 | ||
1689e627 WB |
1323 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); |
1324 | if ($server) { | |
b6cf0a66 DM |
1325 | if (my $ip = resolv_server($server)) { |
1326 | $server = $ip; | |
1689e627 | 1327 | $server = "[$server]" if $server =~ /^$IPV6RE$/; |
b6cf0a66 DM |
1328 | return $port ? "$server:$port" : $server; |
1329 | } | |
1330 | } | |
1331 | return undef if $noerr; | |
1332 | ||
1333 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1334 | } | |
1335 | ||
b6cf0a66 DM |
1336 | |
1337 | sub scan_iscsi { | |
1338 | my ($portal_in) = @_; | |
1339 | ||
1340 | my $portal; | |
1dc01b9f | 1341 | if (!($portal = resolv_portal($portal_in))) { |
b6cf0a66 DM |
1342 | die "unable to parse/resolve portal address '${portal_in}'\n"; |
1343 | } | |
1344 | ||
1dc01b9f | 1345 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); |
b6cf0a66 DM |
1346 | } |
1347 | ||
1348 | sub storage_default_format { | |
1349 | my ($cfg, $storeid) = @_; | |
1350 | ||
1351 | my $scfg = storage_config ($cfg, $storeid); | |
1352 | ||
1dc01b9f | 1353 | return PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 DM |
1354 | } |
1355 | ||
1356 | sub vgroup_is_used { | |
1357 | my ($cfg, $vgname) = @_; | |
1358 | ||
1359 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1360 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1361 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { |
1362 | return 1; | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | return undef; | |
1367 | } | |
1368 | ||
1369 | sub target_is_used { | |
1370 | my ($cfg, $target) = @_; | |
1371 | ||
1372 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1373 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1374 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { |
1375 | return 1; | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | return undef; | |
1380 | } | |
1381 | ||
1382 | sub volume_is_used { | |
1383 | my ($cfg, $volid) = @_; | |
1384 | ||
1385 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1386 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1387 | if ($scfg->{base} && $scfg->{base} eq $volid) { |
1388 | return 1; | |
1389 | } | |
1390 | } | |
1391 | ||
1392 | return undef; | |
1393 | } | |
1394 | ||
1395 | sub storage_is_used { | |
1396 | my ($cfg, $storeid) = @_; | |
1397 | ||
1398 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1399 | my $scfg = storage_config($cfg, $sid); |
b6cf0a66 | 1400 | next if !$scfg->{base}; |
1dc01b9f | 1401 | my ($st) = parse_volume_id($scfg->{base}); |
b6cf0a66 DM |
1402 | return 1 if $st && $st eq $storeid; |
1403 | } | |
1404 | ||
1405 | return undef; | |
1406 | } | |
1407 | ||
1408 | sub foreach_volid { | |
1409 | my ($list, $func) = @_; | |
1410 | ||
1411 | return if !$list; | |
1412 | ||
1413 | foreach my $sid (keys %$list) { | |
1414 | foreach my $info (@{$list->{$sid}}) { | |
1415 | my $volid = $info->{volid}; | |
1dc01b9f | 1416 | my ($sid1, $volname) = parse_volume_id($volid, 1); |
b6cf0a66 DM |
1417 | if ($sid1 && $sid1 eq $sid) { |
1418 | &$func ($volid, $sid, $info); | |
1419 | } else { | |
1420 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | } | |
1425 | ||
cd554b79 AA |
1426 | sub decompressor_info { |
1427 | my ($format, $comp) = @_; | |
1428 | ||
1429 | if ($format eq 'tgz' && !defined($comp)) { | |
1430 | ($format, $comp) = ('tar', 'gz'); | |
1431 | } | |
1432 | ||
1433 | my $decompressor = { | |
1434 | tar => { | |
1435 | gz => ['tar', '-z'], | |
1436 | lzo => ['tar', '--lzop'], | |
014d36db | 1437 | zst => ['tar', '--zstd'], |
cd554b79 AA |
1438 | }, |
1439 | vma => { | |
1440 | gz => ['zcat'], | |
1441 | lzo => ['lzop', '-d', '-c'], | |
014d36db | 1442 | zst => ['zstd', '-q', '-d', '-c'], |
cd554b79 AA |
1443 | }, |
1444 | }; | |
1445 | ||
1446 | die "ERROR: archive format not defined\n" | |
1447 | if !defined($decompressor->{$format}); | |
1448 | ||
1207620c TL |
1449 | my $decomp; |
1450 | $decomp = $decompressor->{$format}->{$comp} if $comp; | |
cd554b79 AA |
1451 | |
1452 | my $info = { | |
1453 | format => $format, | |
1454 | compression => $comp, | |
1455 | decompressor => $decomp, | |
1456 | }; | |
1457 | ||
1458 | return $info; | |
1459 | } | |
1460 | ||
1461 | sub archive_info { | |
1462 | my ($archive) = shift; | |
1463 | my $info; | |
1464 | ||
1465 | my $volid = basename($archive); | |
28be2a43 | 1466 | if ($volid =~ /^(vzdump-(lxc|openvz|qemu)-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)$/) { |
e34afeb1 FE |
1467 | my $filename = "$1"; # untaint |
1468 | my ($type, $format, $comp) = ($2, $3, $4); | |
fb821c18 FE |
1469 | my $format_re = defined($comp) ? "$format.$comp" : "$format"; |
1470 | $info = decompressor_info($format, $comp); | |
e34afeb1 | 1471 | $info->{filename} = $filename; |
fb821c18 FE |
1472 | $info->{type} = $type; |
1473 | ||
e34afeb1 FE |
1474 | if ($volid =~ /^(vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2}))\.${format_re}$/) { |
1475 | $info->{logfilename} = "$1.log"; | |
1476 | $info->{vmid} = int($2); | |
b1ddc54a | 1477 | $info->{ctime} = timelocal($8, $7, $6, $5, $4 - 1, $3); |
fb821c18 FE |
1478 | $info->{is_std_name} = 1; |
1479 | } else { | |
1480 | $info->{is_std_name} = 0; | |
1481 | } | |
cd554b79 | 1482 | } else { |
bf5af0fb | 1483 | die "ERROR: couldn't determine archive info from '$archive'\n"; |
cd554b79 AA |
1484 | } |
1485 | ||
1486 | return $info; | |
1487 | } | |
1488 | ||
35a39532 FE |
1489 | sub archive_remove { |
1490 | my ($archive_path) = @_; | |
1491 | ||
1492 | my $dirname = dirname($archive_path); | |
1493 | my $archive_info = eval { archive_info($archive_path) } // {}; | |
1494 | my $logfn = $archive_info->{logfilename}; | |
1495 | ||
1496 | unlink $archive_path or die "removing archive $archive_path failed: $!\n"; | |
1497 | ||
1498 | if (defined($logfn)) { | |
1499 | my $logpath = "$dirname/$logfn"; | |
1500 | if (-e $logpath) { | |
1501 | unlink $logpath or warn "removing log file $logpath failed: $!\n"; | |
1502 | } | |
1503 | } | |
1504 | } | |
1505 | ||
8898dd7b FG |
1506 | sub extract_vzdump_config_tar { |
1507 | my ($archive, $conf_re) = @_; | |
1508 | ||
1509 | die "ERROR: file '$archive' does not exist\n" if ! -f $archive; | |
1510 | ||
1511 | my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) || | |
1512 | die "unable to open file '$archive'\n"; | |
1513 | ||
1514 | my $file; | |
1515 | while (defined($file = <$fh>)) { | |
086c4bf1 | 1516 | if ($file =~ $conf_re) { |
8898dd7b FG |
1517 | $file = $1; # untaint |
1518 | last; | |
1519 | } | |
1520 | } | |
1521 | ||
1522 | kill 15, $pid; | |
1523 | waitpid $pid, 0; | |
1524 | close $fh; | |
1525 | ||
1526 | die "ERROR: archive contains no configuration file\n" if !$file; | |
1527 | chomp $file; | |
1528 | ||
1529 | my $raw = ''; | |
1530 | my $out = sub { | |
1531 | my $output = shift; | |
1532 | $raw .= "$output\n"; | |
1533 | }; | |
1534 | ||
63e89295 | 1535 | run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out); |
8898dd7b FG |
1536 | |
1537 | return wantarray ? ($raw, $file) : $raw; | |
1538 | } | |
1539 | ||
1540 | sub extract_vzdump_config_vma { | |
1541 | my ($archive, $comp) = @_; | |
1542 | ||
8898dd7b | 1543 | my $raw = ''; |
63e89295 | 1544 | my $out = sub { $raw .= "$_[0]\n"; }; |
8898dd7b | 1545 | |
cd554b79 AA |
1546 | my $info = archive_info($archive); |
1547 | $comp //= $info->{compression}; | |
1548 | my $decompressor = $info->{decompressor}; | |
1549 | ||
8898dd7b | 1550 | if ($comp) { |
63e89295 | 1551 | my $cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ]; |
8898dd7b | 1552 | |
63e89295 | 1553 | # lzop/zcat exits with 1 when the pipe is closed early by vma, detect this and ignore the exit code later |
8898dd7b FG |
1554 | my $broken_pipe; |
1555 | my $errstring; | |
1556 | my $err = sub { | |
1557 | my $output = shift; | |
014d36db | 1558 | if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/ || $output =~ m/zstd: error 70 : Write error : Broken pipe/) { |
8898dd7b FG |
1559 | $broken_pipe = 1; |
1560 | } elsif (!defined ($errstring) && $output !~ m/^\s*$/) { | |
1561 | $errstring = "Failed to extract config from VMA archive: $output\n"; | |
1562 | } | |
1563 | }; | |
1564 | ||
63e89295 | 1565 | my $rc = eval { run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1) }; |
8898dd7b FG |
1566 | my $rerr = $@; |
1567 | ||
63e89295 TL |
1568 | $broken_pipe ||= $rc == 141; # broken pipe from vma POV |
1569 | ||
1570 | if (!$errstring && !$broken_pipe && $rc != 0) { | |
8898dd7b FG |
1571 | die "$rerr\n" if $rerr; |
1572 | die "config extraction failed with exit code $rc\n"; | |
1573 | } | |
1574 | die "$errstring\n" if $errstring; | |
1575 | } else { | |
63e89295 | 1576 | run_command(["vma", "config", $archive], outfunc => $out); |
8898dd7b FG |
1577 | } |
1578 | ||
1579 | return wantarray ? ($raw, undef) : $raw; | |
1580 | } | |
1581 | ||
1582 | sub extract_vzdump_config { | |
1583 | my ($cfg, $volid) = @_; | |
1584 | ||
c855ac15 DM |
1585 | my ($storeid, $volname) = parse_volume_id($volid); |
1586 | if (defined($storeid)) { | |
1587 | my $scfg = storage_config($cfg, $storeid); | |
1588 | if ($scfg->{type} eq 'pbs') { | |
1589 | storage_check_enabled($cfg, $storeid); | |
1590 | return PVE::Storage::PBSPlugin->extract_vzdump_config($scfg, $volname, $storeid); | |
1591 | } | |
1592 | } | |
1593 | ||
8898dd7b | 1594 | my $archive = abs_filesystem_path($cfg, $volid); |
cd554b79 AA |
1595 | my $info = archive_info($archive); |
1596 | my $format = $info->{format}; | |
1597 | my $comp = $info->{compression}; | |
1598 | my $type = $info->{type}; | |
8898dd7b | 1599 | |
cd554b79 | 1600 | if ($type eq 'lxc' || $type eq 'openvz') { |
086c4bf1 | 1601 | return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!); |
cd554b79 | 1602 | } elsif ($type eq 'qemu') { |
8898dd7b FG |
1603 | if ($format eq 'tar') { |
1604 | return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!); | |
1605 | } else { | |
1606 | return extract_vzdump_config_vma($archive, $comp); | |
1607 | } | |
1608 | } else { | |
1609 | die "cannot determine backup guest type for backup archive '$volid'\n"; | |
1610 | } | |
1611 | } | |
1612 | ||
8f26b391 FE |
1613 | sub prune_backups { |
1614 | my ($cfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_; | |
1615 | ||
1616 | my $scfg = storage_config($cfg, $storeid); | |
1617 | die "storage '$storeid' does not support backups\n" if !$scfg->{content}->{backup}; | |
1618 | ||
1619 | if (!defined($keep)) { | |
1620 | die "no prune-backups options configured for storage '$storeid'\n" | |
1621 | if !defined($scfg->{'prune-backups'}); | |
1622 | $keep = PVE::JSONSchema::parse_property_string('prune-backups', $scfg->{'prune-backups'}); | |
1623 | } | |
1624 | ||
883c811f FE |
1625 | activate_storage($cfg, $storeid); |
1626 | ||
8f26b391 FE |
1627 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1628 | return $plugin->prune_backups($scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc); | |
1629 | } | |
1630 | ||
1631 | my $prune_mark = sub { | |
1632 | my ($prune_entries, $keep_count, $id_func) = @_; | |
1633 | ||
1634 | return if !$keep_count; | |
1635 | ||
1636 | my $already_included = {}; | |
1637 | my $newly_included = {}; | |
1638 | ||
1639 | foreach my $prune_entry (@{$prune_entries}) { | |
1640 | my $mark = $prune_entry->{mark}; | |
1641 | my $id = $id_func->($prune_entry->{ctime}); | |
10dfeb9e FE |
1642 | $already_included->{$id} = 1 if defined($mark) && $mark eq 'keep'; |
1643 | } | |
8f26b391 | 1644 | |
10dfeb9e FE |
1645 | foreach my $prune_entry (@{$prune_entries}) { |
1646 | my $mark = $prune_entry->{mark}; | |
1647 | my $id = $id_func->($prune_entry->{ctime}); | |
8f26b391 | 1648 | |
10dfeb9e | 1649 | next if defined($mark) || $already_included->{$id}; |
8f26b391 FE |
1650 | |
1651 | if (!$newly_included->{$id}) { | |
1652 | last if scalar(keys %{$newly_included}) >= $keep_count; | |
1653 | $newly_included->{$id} = 1; | |
1654 | $prune_entry->{mark} = 'keep'; | |
1655 | } else { | |
1656 | $prune_entry->{mark} = 'remove'; | |
1657 | } | |
1658 | } | |
1659 | }; | |
1660 | ||
1661 | sub prune_mark_backup_group { | |
1662 | my ($backup_group, $keep) = @_; | |
1663 | ||
1b87f013 FE |
1664 | my $keep_all = delete $keep->{'keep-all'}; |
1665 | ||
1666 | if ($keep_all || !scalar(grep {$_ > 0} values %{$keep})) { | |
1667 | $keep = { 'keep-all' => 1 } if $keep_all; | |
f514181d FE |
1668 | foreach my $prune_entry (@{$backup_group}) { |
1669 | $prune_entry->{mark} = 'keep'; | |
1670 | } | |
1671 | return; | |
1672 | } | |
1673 | ||
8f26b391 FE |
1674 | my $prune_list = [ sort { $b->{ctime} <=> $a->{ctime} } @{$backup_group} ]; |
1675 | ||
1676 | $prune_mark->($prune_list, $keep->{'keep-last'}, sub { | |
1677 | my ($ctime) = @_; | |
1678 | return $ctime; | |
1679 | }); | |
1680 | $prune_mark->($prune_list, $keep->{'keep-hourly'}, sub { | |
1681 | my ($ctime) = @_; | |
1682 | my (undef, undef, $hour, $day, $month, $year) = localtime($ctime); | |
1683 | return "$hour/$day/$month/$year"; | |
1684 | }); | |
1685 | $prune_mark->($prune_list, $keep->{'keep-daily'}, sub { | |
1686 | my ($ctime) = @_; | |
1687 | my (undef, undef, undef, $day, $month, $year) = localtime($ctime); | |
1688 | return "$day/$month/$year"; | |
1689 | }); | |
1690 | $prune_mark->($prune_list, $keep->{'keep-weekly'}, sub { | |
1691 | my ($ctime) = @_; | |
1692 | my ($sec, $min, $hour, $day, $month, $year) = localtime($ctime); | |
189e67ff FE |
1693 | my $iso_week = int(strftime("%V", $sec, $min, $hour, $day, $month, $year)); |
1694 | my $iso_week_year = int(strftime("%G", $sec, $min, $hour, $day, $month, $year)); | |
8f26b391 FE |
1695 | return "$iso_week/$iso_week_year"; |
1696 | }); | |
1697 | $prune_mark->($prune_list, $keep->{'keep-monthly'}, sub { | |
1698 | my ($ctime) = @_; | |
1699 | my (undef, undef, undef, undef, $month, $year) = localtime($ctime); | |
1700 | return "$month/$year"; | |
1701 | }); | |
1702 | $prune_mark->($prune_list, $keep->{'keep-yearly'}, sub { | |
1703 | my ($ctime) = @_; | |
1704 | my $year = (localtime($ctime))[5]; | |
1705 | return "$year"; | |
1706 | }); | |
1707 | ||
1708 | foreach my $prune_entry (@{$prune_list}) { | |
1709 | $prune_entry->{mark} //= 'remove'; | |
1710 | } | |
1711 | } | |
1712 | ||
47f37b53 WB |
1713 | sub volume_export { |
1714 | my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1715 | ||
1716 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1717 | die "cannot export volume '$volid'\n" if !$storeid; | |
1718 | my $scfg = storage_config($cfg, $storeid); | |
1719 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1720 | return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format, | |
1721 | $snapshot, $base_snapshot, $with_snapshots); | |
1722 | } | |
1723 | ||
1724 | sub volume_import { | |
a97d3ee4 | 1725 | my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_; |
47f37b53 WB |
1726 | |
1727 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1728 | die "cannot import into volume '$volid'\n" if !$storeid; | |
1729 | my $scfg = storage_config($cfg, $storeid); | |
1730 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1731 | return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format, | |
a97d3ee4 | 1732 | $base_snapshot, $with_snapshots, $allow_rename) // $volid; |
47f37b53 WB |
1733 | } |
1734 | ||
d390328b WB |
1735 | sub volume_export_formats { |
1736 | my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1737 | ||
1738 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1739 | return if !$storeid; | |
1740 | my $scfg = storage_config($cfg, $storeid); | |
1741 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1742 | return $plugin->volume_export_formats($scfg, $storeid, $volname, | |
ae36189d WB |
1743 | $snapshot, $base_snapshot, |
1744 | $with_snapshots); | |
d390328b WB |
1745 | } |
1746 | ||
1747 | sub volume_import_formats { | |
1748 | my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_; | |
1749 | ||
1750 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1751 | return if !$storeid; | |
1752 | my $scfg = storage_config($cfg, $storeid); | |
1753 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1754 | return $plugin->volume_import_formats($scfg, $storeid, $volname, | |
1755 | $base_snapshot, $with_snapshots); | |
1756 | } | |
1757 | ||
1758 | sub volume_transfer_formats { | |
1759 | my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1760 | my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots); | |
1761 | my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots); | |
1762 | my %import_hash = map { $_ => 1 } @import_formats; | |
1763 | my @common = grep { $import_hash{$_} } @export_formats; | |
1764 | return @common; | |
1765 | } | |
1766 | ||
a97d3ee4 FE |
1767 | sub volume_imported_message { |
1768 | my ($volid, $want_pattern) = @_; | |
1769 | ||
1770 | if ($want_pattern) { | |
1771 | return qr/successfully imported '([^']*)'$/; | |
1772 | } else { | |
1773 | return "successfully imported '$volid'\n"; | |
1774 | } | |
1775 | } | |
1776 | ||
f7621c01 DM |
1777 | # bash completion helper |
1778 | ||
1779 | sub complete_storage { | |
746e530f | 1780 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1781 | |
746e530f | 1782 | my $cfg = PVE::Storage::config(); |
180c8b02 | 1783 | |
746e530f | 1784 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; |
f7621c01 DM |
1785 | } |
1786 | ||
1787 | sub complete_storage_enabled { | |
746e530f | 1788 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1789 | |
746e530f | 1790 | my $res = []; |
f7621c01 | 1791 | |
746e530f DM |
1792 | my $cfg = PVE::Storage::config(); |
1793 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1794 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1795 | push @$res, $sid; | |
1796 | } | |
1797 | return $res; | |
f7621c01 DM |
1798 | } |
1799 | ||
98437f4c DM |
1800 | sub complete_content_type { |
1801 | my ($cmdname, $pname, $cvalue) = @_; | |
1802 | ||
7c7ae12f | 1803 | return [qw(rootdir images vztmpl iso backup snippets)]; |
98437f4c DM |
1804 | } |
1805 | ||
bf7aed26 DM |
1806 | sub complete_volume { |
1807 | my ($cmdname, $pname, $cvalue) = @_; | |
1808 | ||
1809 | my $cfg = config(); | |
1810 | ||
1811 | my $storage_list = complete_storage_enabled(); | |
1812 | ||
b70b0c58 DM |
1813 | if ($cvalue =~ m/^([^:]+):/) { |
1814 | $storage_list = [ $1 ]; | |
1815 | } else { | |
1816 | if (scalar(@$storage_list) > 1) { | |
1817 | # only list storage IDs to avoid large listings | |
1818 | my $res = []; | |
1819 | foreach my $storeid (@$storage_list) { | |
1820 | # Hack: simply return 2 artificial values, so that | |
1821 | # completions does not finish | |
1822 | push @$res, "$storeid:volname", "$storeid:..."; | |
1823 | } | |
1824 | return $res; | |
1825 | } | |
1826 | } | |
1827 | ||
bf7aed26 DM |
1828 | my $res = []; |
1829 | foreach my $storeid (@$storage_list) { | |
1830 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1831 | ||
1832 | foreach my $item (@$vollist) { | |
1833 | push @$res, $item->{volid}; | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | return $res; | |
1838 | } | |
1839 | ||
9edb99a5 WB |
1840 | # Various io-heavy operations require io/bandwidth limits which can be |
1841 | # configured on multiple levels: The global defaults in datacenter.cfg, and | |
1842 | # per-storage overrides. When we want to do a restore from storage A to storage | |
1843 | # B, we should take the smaller limit defined for storages A and B, and if no | |
1844 | # such limit was specified, use the one from datacenter.cfg. | |
1845 | sub get_bandwidth_limit { | |
1846 | my ($operation, $storage_list, $override) = @_; | |
1847 | ||
1848 | # called for each limit (global, per-storage) with the 'default' and the | |
ffc31266 | 1849 | # $operation limit and should update $override for every limit affecting |
9edb99a5 WB |
1850 | # us. |
1851 | my $use_global_limits = 0; | |
1852 | my $apply_limit = sub { | |
1853 | my ($bwlimit) = @_; | |
1854 | if (defined($bwlimit)) { | |
1855 | my $limits = PVE::JSONSchema::parse_property_string('bwlimit', $bwlimit); | |
1856 | my $limit = $limits->{$operation} // $limits->{default}; | |
1857 | if (defined($limit)) { | |
1858 | if (!$override || $limit < $override) { | |
1859 | $override = $limit; | |
1860 | } | |
1861 | return; | |
1862 | } | |
1863 | } | |
1864 | # If there was no applicable limit, try to apply the global ones. | |
1865 | $use_global_limits = 1; | |
1866 | }; | |
1867 | ||
77445e9b WB |
1868 | my ($rpcenv, $authuser); |
1869 | if (defined($override)) { | |
1870 | $rpcenv = PVE::RPCEnvironment->get(); | |
1871 | $authuser = $rpcenv->get_user(); | |
1872 | } | |
9edb99a5 WB |
1873 | |
1874 | # Apply per-storage limits - if there are storages involved. | |
074bdd35 | 1875 | if (defined($storage_list) && @$storage_list) { |
9edb99a5 WB |
1876 | my $config = config(); |
1877 | ||
1878 | # The Datastore.Allocate permission allows us to modify the per-storage | |
1879 | # limits, therefore it also allows us to override them. | |
1880 | # Since we have most likely multiple storages to check, do a quick check on | |
1881 | # the general '/storage' path to see if we can skip the checks entirely: | |
77445e9b | 1882 | return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1); |
9edb99a5 WB |
1883 | |
1884 | my %done; | |
1885 | foreach my $storage (@$storage_list) { | |
396aedff | 1886 | next if !defined($storage); |
9edb99a5 WB |
1887 | # Avoid duplicate checks: |
1888 | next if $done{$storage}; | |
1889 | $done{$storage} = 1; | |
1890 | ||
1891 | # Otherwise we may still have individual /storage/$ID permissions: | |
77445e9b | 1892 | if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) { |
9edb99a5 WB |
1893 | # And if not: apply the limits. |
1894 | my $storecfg = storage_config($config, $storage); | |
1895 | $apply_limit->($storecfg->{bwlimit}); | |
1896 | } | |
1897 | } | |
1898 | ||
1899 | # Storage limits take precedence over the datacenter defaults, so if | |
1900 | # a limit was applied: | |
1901 | return $override if !$use_global_limits; | |
1902 | } | |
1903 | ||
1904 | # Sys.Modify on '/' means we can change datacenter.cfg which contains the | |
1905 | # global default limits. | |
77445e9b | 1906 | if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) { |
9edb99a5 WB |
1907 | # So if we cannot modify global limits, apply them to our currently |
1908 | # requested override. | |
1909 | my $dc = cfs_read_file('datacenter.cfg'); | |
1910 | $apply_limit->($dc->{bwlimit}); | |
1911 | } | |
1912 | ||
1913 | return $override; | |
1914 | } | |
1915 | ||
76c1e57b | 1916 | # checks if the storage id is available and dies if not |
9280153e TL |
1917 | sub assert_sid_unused { |
1918 | my ($sid) = @_; | |
76c1e57b DC |
1919 | |
1920 | my $cfg = config(); | |
9280153e TL |
1921 | if (my $scfg = storage_config($cfg, $sid, 1)) { |
1922 | die "storage ID '$sid' already defined\n"; | |
76c1e57b DC |
1923 | } |
1924 | ||
1925 | return undef; | |
1926 | } | |
1927 | ||
b6cf0a66 | 1928 | 1; |