]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Storage; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
7 | use POSIX; | |
8 | use IO::Select; | |
9 | use IO::File; | |
10 | use IO::Socket::IP; | |
11 | use IPC::Open3; | |
12 | use File::Basename; | |
13 | use File::Path; | |
14 | use Cwd 'abs_path'; | |
15 | use Socket; | |
16 | use Time::Local qw(timelocal); | |
17 | ||
18 | use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE); | |
19 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); | |
20 | use PVE::DataCenterConfig; | |
21 | use PVE::Exception qw(raise_param_exc raise); | |
22 | use PVE::JSONSchema; | |
23 | use PVE::INotify; | |
24 | use PVE::RPCEnvironment; | |
25 | use PVE::SSHInfo; | |
26 | ||
27 | use PVE::Storage::Plugin; | |
28 | use PVE::Storage::DirPlugin; | |
29 | use PVE::Storage::LVMPlugin; | |
30 | use PVE::Storage::LvmThinPlugin; | |
31 | use PVE::Storage::NFSPlugin; | |
32 | use PVE::Storage::CIFSPlugin; | |
33 | use PVE::Storage::ISCSIPlugin; | |
34 | use PVE::Storage::RBDPlugin; | |
35 | use PVE::Storage::CephFSPlugin; | |
36 | use PVE::Storage::ISCSIDirectPlugin; | |
37 | use PVE::Storage::GlusterfsPlugin; | |
38 | use PVE::Storage::ZFSPoolPlugin; | |
39 | use PVE::Storage::ZFSPlugin; | |
40 | use PVE::Storage::PBSPlugin; | |
41 | ||
42 | # Storage API version. Increment it on changes in storage API interface. | |
43 | use constant APIVER => 8; | |
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 | |
47 | use constant APIAGE => 7; | |
48 | ||
49 | # load standard plugins | |
50 | PVE::Storage::DirPlugin->register(); | |
51 | PVE::Storage::LVMPlugin->register(); | |
52 | PVE::Storage::LvmThinPlugin->register(); | |
53 | PVE::Storage::NFSPlugin->register(); | |
54 | PVE::Storage::CIFSPlugin->register(); | |
55 | PVE::Storage::ISCSIPlugin->register(); | |
56 | PVE::Storage::RBDPlugin->register(); | |
57 | PVE::Storage::CephFSPlugin->register(); | |
58 | PVE::Storage::ISCSIDirectPlugin->register(); | |
59 | PVE::Storage::GlusterfsPlugin->register(); | |
60 | PVE::Storage::ZFSPoolPlugin->register(); | |
61 | PVE::Storage::ZFSPlugin->register(); | |
62 | PVE::Storage::PBSPlugin->register(); | |
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; | |
74 | ||
75 | # Check perl interface: | |
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'); | |
78 | # Check storage API version and that file is really storage plugin. | |
79 | my $version = $modname->api(); | |
80 | die "implements an API version newer than current ($version > " . APIVER . ")\n" | |
81 | if $version > APIVER; | |
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; | |
85 | # all OK, do import and register (i.e., "use") | |
86 | import $file; | |
87 | $modname->register(); | |
88 | ||
89 | # If we got this far and the API version is not the same, make some noise: | |
90 | warn "Plugin \"$modname\" is implementing an older storage API, an upgrade is recommended\n" | |
91 | if $version != APIVER; | |
92 | }; | |
93 | if ($@) { | |
94 | warn "Error loading storage plugin \"$modname\": $@"; | |
95 | } | |
96 | }); | |
97 | } | |
98 | ||
99 | # initialize all plugins | |
100 | PVE::Storage::Plugin->init(); | |
101 | ||
102 | our $iso_extension_re = qr/\.(?:iso|img)/i; | |
103 | ||
104 | # PVE::Storage utility functions | |
105 | ||
106 | sub config { | |
107 | return cfs_read_file("storage.cfg"); | |
108 | } | |
109 | ||
110 | sub write_config { | |
111 | my ($cfg) = @_; | |
112 | ||
113 | cfs_write_file('storage.cfg', $cfg); | |
114 | } | |
115 | ||
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 | ||
126 | # FIXME remove maxfiles for PVE 8.0 or PVE 9.0 | |
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 | ||
148 | sub storage_config { | |
149 | my ($cfg, $storeid, $noerr) = @_; | |
150 | ||
151 | die "no storage ID specified\n" if !$storeid; | |
152 | ||
153 | my $scfg = $cfg->{ids}->{$storeid}; | |
154 | ||
155 | die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg); | |
156 | ||
157 | $convert_maxfiles_to_prune_backups->($scfg); | |
158 | ||
159 | return $scfg; | |
160 | } | |
161 | ||
162 | sub storage_check_node { | |
163 | my ($cfg, $storeid, $node, $noerr) = @_; | |
164 | ||
165 | my $scfg = storage_config($cfg, $storeid); | |
166 | ||
167 | if ($scfg->{nodes}) { | |
168 | $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost'); | |
169 | if (!$scfg->{nodes}->{$node}) { | |
170 | die "storage '$storeid' is not available on node '$node'\n" if !$noerr; | |
171 | return undef; | |
172 | } | |
173 | } | |
174 | ||
175 | return $scfg; | |
176 | } | |
177 | ||
178 | sub storage_check_enabled { | |
179 | my ($cfg, $storeid, $node, $noerr) = @_; | |
180 | ||
181 | my $scfg = storage_config($cfg, $storeid); | |
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 | ||
191 | # storage_can_replicate: | |
192 | # return true if storage supports replication | |
193 | # (volumes alocated with vdisk_alloc() has replication feature) | |
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 | ||
202 | sub storage_ids { | |
203 | my ($cfg) = @_; | |
204 | ||
205 | return keys %{$cfg->{ids}}; | |
206 | } | |
207 | ||
208 | sub file_size_info { | |
209 | my ($filename, $timeout) = @_; | |
210 | ||
211 | return PVE::Storage::Plugin::file_size_info($filename, $timeout); | |
212 | } | |
213 | ||
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 | ||
234 | sub volume_size_info { | |
235 | my ($cfg, $volid, $timeout) = @_; | |
236 | ||
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 | } | |
247 | } | |
248 | ||
249 | sub volume_resize { | |
250 | my ($cfg, $volid, $size, $running) = @_; | |
251 | ||
252 | my $padding = (1024 - $size % 1024) % 1024; | |
253 | $size = $size + $padding; | |
254 | ||
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) { | |
261 | die "resize file/device '$volid' is not possible\n"; | |
262 | } else { | |
263 | die "unable to parse volume ID '$volid'\n"; | |
264 | } | |
265 | } | |
266 | ||
267 | sub volume_rollback_is_possible { | |
268 | my ($cfg, $volid, $snap) = @_; | |
269 | ||
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) { | |
276 | die "snapshot rollback file/device '$volid' is not possible\n"; | |
277 | } else { | |
278 | die "unable to parse volume ID '$volid'\n"; | |
279 | } | |
280 | } | |
281 | ||
282 | sub volume_snapshot { | |
283 | my ($cfg, $volid, $snap) = @_; | |
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}); | |
289 | return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap); | |
290 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
291 | die "snapshot file/device '$volid' is not possible\n"; | |
292 | } else { | |
293 | die "unable to parse volume ID '$volid'\n"; | |
294 | } | |
295 | } | |
296 | ||
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}); | |
304 | $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); | |
305 | return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap); | |
306 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
307 | die "snapshot rollback file/device '$volid' is not possible\n"; | |
308 | } else { | |
309 | die "unable to parse volume ID '$volid'\n"; | |
310 | } | |
311 | } | |
312 | ||
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}); | |
320 | return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running); | |
321 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
322 | die "snapshot delete file/device '$volid' is not possible\n"; | |
323 | } else { | |
324 | die "unable to parse volume ID '$volid'\n"; | |
325 | } | |
326 | } | |
327 | ||
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 | ||
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. | |
355 | sub volume_has_feature { | |
356 | my ($cfg, $feature, $volid, $snap, $running, $opts) = @_; | |
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}); | |
362 | return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts); | |
363 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
364 | return undef; | |
365 | } else { | |
366 | return undef; | |
367 | } | |
368 | } | |
369 | ||
370 | sub volume_snapshot_list { | |
371 | my ($cfg, $volid) = @_; | |
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}); | |
377 | return $plugin->volume_snapshot_list($scfg, $storeid, $volname); | |
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. | |
384 | } | |
385 | ||
386 | sub get_image_dir { | |
387 | my ($cfg, $storeid, $vmid) = @_; | |
388 | ||
389 | my $scfg = storage_config($cfg, $storeid); | |
390 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
391 | ||
392 | my $path = $plugin->get_subdir($scfg, 'images'); | |
393 | ||
394 | return $vmid ? "$path/$vmid" : $path; | |
395 | } | |
396 | ||
397 | sub get_private_dir { | |
398 | my ($cfg, $storeid, $vmid) = @_; | |
399 | ||
400 | my $scfg = storage_config($cfg, $storeid); | |
401 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
402 | ||
403 | my $path = $plugin->get_subdir($scfg, 'rootdir'); | |
404 | ||
405 | return $vmid ? "$path/$vmid" : $path; | |
406 | } | |
407 | ||
408 | sub get_iso_dir { | |
409 | my ($cfg, $storeid) = @_; | |
410 | ||
411 | my $scfg = storage_config($cfg, $storeid); | |
412 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
413 | ||
414 | return $plugin->get_subdir($scfg, 'iso'); | |
415 | } | |
416 | ||
417 | sub get_vztmpl_dir { | |
418 | my ($cfg, $storeid) = @_; | |
419 | ||
420 | my $scfg = storage_config($cfg, $storeid); | |
421 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
422 | ||
423 | return $plugin->get_subdir($scfg, 'vztmpl'); | |
424 | } | |
425 | ||
426 | sub get_backup_dir { | |
427 | my ($cfg, $storeid) = @_; | |
428 | ||
429 | my $scfg = storage_config($cfg, $storeid); | |
430 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
431 | ||
432 | return $plugin->get_subdir($scfg, 'backup'); | |
433 | } | |
434 | ||
435 | # library implementation | |
436 | ||
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 | ||
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. | |
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}); | |
456 | ||
457 | # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) | |
458 | ||
459 | return $plugin->parse_volname($volname); | |
460 | } | |
461 | ||
462 | sub parse_volume_id { | |
463 | my ($volid, $noerr) = @_; | |
464 | ||
465 | return PVE::Storage::Plugin::parse_volume_id($volid, $noerr); | |
466 | } | |
467 | ||
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') { | |
476 | # require at least read access to storage, (custom) templates/ISOs could be sensitive | |
477 | $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']); | |
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 | ||
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}); | |
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; | |
526 | } | |
527 | ||
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 | ||
534 | my ($sid, $volname) = parse_volume_id($path, 1); | |
535 | if ($sid) { | |
536 | if (my $scfg = $ids->{$sid}) { | |
537 | if ($scfg->{path}) { | |
538 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
539 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
540 | return ($vtype, $path); | |
541 | } | |
542 | } | |
543 | return (''); | |
544 | } | |
545 | ||
546 | # Note: abs_path() return undef if $path doesn not exist | |
547 | # for example when nfs storage is not mounted | |
548 | $path = abs_path($path) || $path; | |
549 | ||
550 | foreach my $sid (keys %$ids) { | |
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'); | |
559 | my $snippetsdir = $plugin->get_subdir($scfg, 'snippets'); | |
560 | ||
561 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
562 | my $vmid = $1; | |
563 | my $name = $2; | |
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 | } | |
573 | } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) { | |
574 | my $name = $1; | |
575 | return ('iso', "$sid:iso/$name"); | |
576 | } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) { | |
577 | my $name = $1; | |
578 | return ('vztmpl', "$sid:vztmpl/$name"); | |
579 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { | |
580 | my $vmid = $1; | |
581 | return ('rootdir', "$sid:rootdir/$vmid"); | |
582 | } elsif ($path =~ m!^$backupdir/([^/]+\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) { | |
583 | my $name = $1; | |
584 | return ('backup', "$sid:backup/$name"); | |
585 | } elsif ($path =~ m!^$snippetsdir/([^/]+)$!) { | |
586 | my $name = $1; | |
587 | return ('snippets', "$sid:snippets/$name"); | |
588 | } | |
589 | } | |
590 | ||
591 | # can't map path to volume id | |
592 | return (''); | |
593 | } | |
594 | ||
595 | sub path { | |
596 | my ($cfg, $volid, $snapname) = @_; | |
597 | ||
598 | my ($storeid, $volname) = parse_volume_id($volid); | |
599 | ||
600 | my $scfg = storage_config($cfg, $storeid); | |
601 | ||
602 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
603 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname); | |
604 | return wantarray ? ($path, $owner, $vtype) : $path; | |
605 | } | |
606 | ||
607 | sub abs_filesystem_path { | |
608 | my ($cfg, $volid, $allow_blockdev) = @_; | |
609 | ||
610 | my $path; | |
611 | if (parse_volume_id ($volid, 1)) { | |
612 | activate_volumes($cfg, [ $volid ]); | |
613 | $path = PVE::Storage::path($cfg, $volid); | |
614 | } else { | |
615 | if (-f $volid || ($allow_blockdev && -b $volid)) { | |
616 | my $abspath = abs_path($volid); | |
617 | if ($abspath && $abspath =~ m|^(/.+)$|) { | |
618 | $path = $1; # untaint any path | |
619 | } | |
620 | } | |
621 | } | |
622 | die "can't find file '$volid'\n" | |
623 | if !($path && (-f $path || ($allow_blockdev && -b $path))); | |
624 | ||
625 | return $path; | |
626 | } | |
627 | ||
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 | ||
647 | sub storage_migrate { | |
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; | |
656 | ||
657 | my ($storeid, $volname) = parse_volume_id($volid); | |
658 | ||
659 | my $scfg = storage_config($cfg, $storeid); | |
660 | ||
661 | # no need to migrate shared content | |
662 | return $volid if $storeid eq $target_storeid && $scfg->{shared}; | |
663 | ||
664 | my $tcfg = storage_config($cfg, $target_storeid); | |
665 | ||
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 | ||
675 | my $target_volid = "${target_storeid}:${target_volname}"; | |
676 | ||
677 | my $target_ip = $target_sshinfo->{ip}; | |
678 | ||
679 | my $ssh = PVE::SSHInfo::ssh_info_to_command($target_sshinfo); | |
680 | my $ssh_base = PVE::SSHInfo::ssh_info_to_command_base($target_sshinfo); | |
681 | local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base); | |
682 | ||
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 | } | |
688 | ||
689 | my $migration_snapshot; | |
690 | if (!defined($snapshot)) { | |
691 | if ($scfg->{type} eq 'zfspool') { | |
692 | $migration_snapshot = 1; | |
693 | $snapshot = '__migration__'; | |
694 | } | |
695 | } | |
696 | ||
697 | my @formats = volume_transfer_formats($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots); | |
698 | die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats; | |
699 | my $format = $formats[0]; | |
700 | ||
701 | my $import_fn = '-'; # let pvesm import read from stdin per default | |
702 | if ($insecure) { | |
703 | my $net = $target_sshinfo->{network} // $target_sshinfo->{ip}; | |
704 | $import_fn = "tcp://$net"; | |
705 | } | |
706 | ||
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 | ||
712 | my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots]; | |
713 | my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots]; | |
714 | if (defined($snapshot)) { | |
715 | push @$send, '-snapshot', $snapshot | |
716 | } | |
717 | if ($migration_snapshot) { | |
718 | push @$recv, '-delete-snapshot', $snapshot; | |
719 | } | |
720 | push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5; | |
721 | ||
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 | } | |
727 | ||
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 | ||
741 | volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot; | |
742 | ||
743 | if (defined($snapshot)) { | |
744 | activate_volumes($cfg, [$volid], $snapshot); | |
745 | } else { | |
746 | activate_volumes($cfg, [$volid]); | |
747 | } | |
748 | ||
749 | eval { | |
750 | if ($insecure) { | |
751 | my $input = IO::File->new(); | |
752 | my $info = IO::File->new(); | |
753 | open3($input, $info, $info, @{$recv}) | |
754 | or die "receive command failed: $!\n"; | |
755 | close($input); | |
756 | ||
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 | ||
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); | |
769 | ||
770 | eval { run_command([$send, @cstream], output => '>&'.fileno($socket), errfunc => $logfunc); }; | |
771 | my $send_error = $@; | |
772 | ||
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); | |
776 | ||
777 | # wait for the remote process to finish | |
778 | while (my $line = <$info>) { | |
779 | $match_volid_and_log->("[$target_sshinfo->{name}] $line"); | |
780 | } | |
781 | ||
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"; | |
787 | } | |
788 | ||
789 | die $send_error if $send_error; | |
790 | } else { | |
791 | run_command([$send, @cstream, $recv], logfunc => $match_volid_and_log); | |
792 | } | |
793 | ||
794 | die "unable to get ID of the migrated volume\n" | |
795 | if !defined($new_volid) && $target_apiver >= 5; | |
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 $@; | |
802 | } | |
803 | die $err if $err; | |
804 | ||
805 | return $new_volid // $target_volid; | |
806 | } | |
807 | ||
808 | sub vdisk_clone { | |
809 | my ($cfg, $volid, $vmid, $snap) = @_; | |
810 | ||
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}); | |
816 | ||
817 | activate_storage($cfg, $storeid); | |
818 | ||
819 | # lock shared storage | |
820 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
821 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); | |
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}); | |
834 | ||
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 | ||
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 | ||
868 | sub vdisk_alloc { | |
869 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
870 | ||
871 | die "no storage ID specified\n" if !$storeid; | |
872 | ||
873 | PVE::JSONSchema::parse_storage_id($storeid); | |
874 | ||
875 | my $scfg = storage_config($cfg, $storeid); | |
876 | ||
877 | die "no VMID specified\n" if !$vmid; | |
878 | ||
879 | $vmid = parse_vmid($vmid); | |
880 | ||
881 | my $defformat = PVE::Storage::Plugin::default_format($scfg); | |
882 | ||
883 | $fmt = $defformat if !$fmt; | |
884 | ||
885 | activate_storage($cfg, $storeid); | |
886 | ||
887 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
888 | ||
889 | # lock shared storage | |
890 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
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; | |
896 | return "$storeid:$volname"; | |
897 | }); | |
898 | } | |
899 | ||
900 | sub vdisk_free { | |
901 | my ($cfg, $volid) = @_; | |
902 | ||
903 | my ($storeid, $volname) = parse_volume_id($volid); | |
904 | my $scfg = storage_config($cfg, $storeid); | |
905 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
906 | ||
907 | activate_storage($cfg, $storeid); | |
908 | ||
909 | my $cleanup_worker; | |
910 | ||
911 | # lock shared storage | |
912 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
913 | # LVM-thin allows deletion of still referenced base volumes! | |
914 | die "base volume '$volname' is still in use by linked clones\n" | |
915 | if volume_is_base_and_used($cfg, $volid); | |
916 | ||
917 | my (undef, undef, undef, undef, undef, $isBase, $format) = | |
918 | $plugin->parse_volname($volname); | |
919 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); | |
920 | }); | |
921 | ||
922 | return if !$cleanup_worker; | |
923 | ||
924 | my $rpcenv = PVE::RPCEnvironment::get(); | |
925 | my $authuser = $rpcenv->get_user(); | |
926 | ||
927 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); | |
928 | } | |
929 | ||
930 | sub vdisk_list { | |
931 | my ($cfg, $storeid, $vmid, $vollist, $ctype) = @_; | |
932 | ||
933 | my $ids = $cfg->{ids}; | |
934 | ||
935 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
936 | ||
937 | my $res = {}; | |
938 | ||
939 | # prepare/activate/refresh all storages | |
940 | ||
941 | my $storage_list = []; | |
942 | if ($vollist) { | |
943 | foreach my $volid (@$vollist) { | |
944 | my ($sid, undef) = parse_volume_id($volid); | |
945 | next if !defined($ids->{$sid}); | |
946 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
947 | push @$storage_list, $sid; | |
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); | |
953 | my $content = $ids->{$sid}->{content}; | |
954 | next if defined($ctype) && !$content->{$ctype}; | |
955 | next if !($content->{rootdir} || $content->{images}); | |
956 | push @$storage_list, $sid; | |
957 | } | |
958 | } | |
959 | ||
960 | my $cache = {}; | |
961 | ||
962 | activate_storage_list($cfg, $storage_list, $cache); | |
963 | ||
964 | # FIXME PVE 7.0: only scan storages with the correct content types | |
965 | my $scan = defined($ctype) ? $storage_list : [ keys %{$ids} ]; | |
966 | ||
967 | foreach my $sid (@{$scan}) { | |
968 | next if $storeid && $storeid ne $sid; | |
969 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
970 | ||
971 | my $scfg = $ids->{$sid}; | |
972 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
973 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
974 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
975 | } | |
976 | ||
977 | return $res; | |
978 | } | |
979 | ||
980 | sub template_list { | |
981 | my ($cfg, $storeid, $tt) = @_; | |
982 | ||
983 | die "unknown template type '$tt'\n" | |
984 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets'); | |
985 | ||
986 | my $ids = $cfg->{ids}; | |
987 | ||
988 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
989 | ||
990 | my $res = {}; | |
991 | ||
992 | # query the storage | |
993 | foreach my $sid (keys %$ids) { | |
994 | next if $storeid && $storeid ne $sid; | |
995 | ||
996 | my $scfg = $ids->{$sid}; | |
997 | my $type = $scfg->{type}; | |
998 | ||
999 | next if !$scfg->{content}->{$tt}; | |
1000 | ||
1001 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1002 | ||
1003 | $res->{$sid} = volume_list($cfg, $sid, undef, $tt); | |
1004 | } | |
1005 | ||
1006 | return $res; | |
1007 | } | |
1008 | ||
1009 | sub volume_list { | |
1010 | my ($cfg, $storeid, $vmid, $content) = @_; | |
1011 | ||
1012 | my @ctypes = qw(rootdir images vztmpl iso backup snippets); | |
1013 | ||
1014 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
1015 | ||
1016 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
1017 | ||
1018 | $cts = [ grep { defined($scfg->{content}->{$_}) } @$cts ]; | |
1019 | ||
1020 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1021 | ||
1022 | activate_storage($cfg, $storeid); | |
1023 | ||
1024 | my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts); | |
1025 | ||
1026 | @$res = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @$res; | |
1027 | ||
1028 | return $res; | |
1029 | } | |
1030 | ||
1031 | sub uevent_seqnum { | |
1032 | ||
1033 | my $filename = "/sys/kernel/uevent_seqnum"; | |
1034 | ||
1035 | my $seqnum = 0; | |
1036 | if (my $fh = IO::File->new($filename, "r")) { | |
1037 | my $line = <$fh>; | |
1038 | if ($line =~ m/^(\d+)$/) { | |
1039 | $seqnum = int($1); | |
1040 | } | |
1041 | close ($fh); | |
1042 | } | |
1043 | return $seqnum; | |
1044 | } | |
1045 | ||
1046 | sub activate_storage { | |
1047 | my ($cfg, $storeid, $cache) = @_; | |
1048 | ||
1049 | $cache = {} if !$cache; | |
1050 | ||
1051 | my $scfg = storage_check_enabled($cfg, $storeid); | |
1052 | ||
1053 | return if $cache->{activated}->{$storeid}; | |
1054 | ||
1055 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; | |
1056 | ||
1057 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1058 | ||
1059 | if ($scfg->{base}) { | |
1060 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
1061 | activate_storage($cfg, $baseid, $cache); | |
1062 | } | |
1063 | ||
1064 | if (!$plugin->check_connection($storeid, $scfg)) { | |
1065 | die "storage '$storeid' is not online\n"; | |
1066 | } | |
1067 | ||
1068 | $plugin->activate_storage($storeid, $scfg, $cache); | |
1069 | ||
1070 | my $newseq = uevent_seqnum (); | |
1071 | ||
1072 | # only call udevsettle if there are events | |
1073 | if ($newseq > $cache->{uevent_seqnum}) { | |
1074 | system ("udevadm settle --timeout=30"); # ignore errors | |
1075 | $cache->{uevent_seqnum} = $newseq; | |
1076 | } | |
1077 | ||
1078 | $cache->{activated}->{$storeid} = 1; | |
1079 | } | |
1080 | ||
1081 | sub activate_storage_list { | |
1082 | my ($cfg, $storeid_list, $cache) = @_; | |
1083 | ||
1084 | $cache = {} if !$cache; | |
1085 | ||
1086 | foreach my $storeid (@$storeid_list) { | |
1087 | activate_storage($cfg, $storeid, $cache); | |
1088 | } | |
1089 | } | |
1090 | ||
1091 | sub deactivate_storage { | |
1092 | my ($cfg, $storeid) = @_; | |
1093 | ||
1094 | my $scfg = storage_config ($cfg, $storeid); | |
1095 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1096 | ||
1097 | my $cache = {}; | |
1098 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
1099 | } | |
1100 | ||
1101 | sub activate_volumes { | |
1102 | my ($cfg, $vollist, $snapname) = @_; | |
1103 | ||
1104 | return if !($vollist && scalar(@$vollist)); | |
1105 | ||
1106 | my $storagehash = {}; | |
1107 | foreach my $volid (@$vollist) { | |
1108 | my ($storeid, undef) = parse_volume_id($volid); | |
1109 | $storagehash->{$storeid} = 1; | |
1110 | } | |
1111 | ||
1112 | my $cache = {}; | |
1113 | ||
1114 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
1115 | ||
1116 | foreach my $volid (@$vollist) { | |
1117 | my ($storeid, $volname) = parse_volume_id($volid); | |
1118 | my $scfg = storage_config($cfg, $storeid); | |
1119 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1120 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | sub deactivate_volumes { | |
1125 | my ($cfg, $vollist, $snapname) = @_; | |
1126 | ||
1127 | return if !($vollist && scalar(@$vollist)); | |
1128 | ||
1129 | my $cache = {}; | |
1130 | ||
1131 | my @errlist = (); | |
1132 | foreach my $volid (@$vollist) { | |
1133 | my ($storeid, $volname) = parse_volume_id($volid); | |
1134 | ||
1135 | my $scfg = storage_config($cfg, $storeid); | |
1136 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1137 | ||
1138 | eval { | |
1139 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); | |
1140 | }; | |
1141 | if (my $err = $@) { | |
1142 | warn $err; | |
1143 | push @errlist, $volid; | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | die "volume deactivation failed: " . join(' ', @errlist) | |
1148 | if scalar(@errlist); | |
1149 | } | |
1150 | ||
1151 | sub storage_info { | |
1152 | my ($cfg, $content, $includeformat) = @_; | |
1153 | ||
1154 | my $ids = $cfg->{ids}; | |
1155 | ||
1156 | my $info = {}; | |
1157 | ||
1158 | my @ctypes = PVE::Tools::split_list($content); | |
1159 | ||
1160 | my $slist = []; | |
1161 | foreach my $storeid (keys %$ids) { | |
1162 | my $storage_enabled = defined(storage_check_enabled($cfg, $storeid, undef, 1)); | |
1163 | ||
1164 | if (defined($content)) { | |
1165 | my $want_ctype = 0; | |
1166 | foreach my $ctype (@ctypes) { | |
1167 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
1168 | $want_ctype = 1; | |
1169 | last; | |
1170 | } | |
1171 | } | |
1172 | next if !$want_ctype || !$storage_enabled; | |
1173 | } | |
1174 | ||
1175 | my $type = $ids->{$storeid}->{type}; | |
1176 | ||
1177 | $info->{$storeid} = { | |
1178 | type => $type, | |
1179 | total => 0, | |
1180 | avail => 0, | |
1181 | used => 0, | |
1182 | shared => $ids->{$storeid}->{shared} ? 1 : 0, | |
1183 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), | |
1184 | active => 0, | |
1185 | enabled => $storage_enabled ? 1 : 0, | |
1186 | }; | |
1187 | ||
1188 | push @$slist, $storeid; | |
1189 | } | |
1190 | ||
1191 | my $cache = {}; | |
1192 | ||
1193 | foreach my $storeid (keys %$ids) { | |
1194 | my $scfg = $ids->{$storeid}; | |
1195 | ||
1196 | next if !$info->{$storeid}; | |
1197 | next if !$info->{$storeid}->{enabled}; | |
1198 | ||
1199 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1200 | if ($includeformat) { | |
1201 | my $pd = $plugin->plugindata(); | |
1202 | $info->{$storeid}->{format} = $pd->{format} | |
1203 | if $pd->{format}; | |
1204 | $info->{$storeid}->{select_existing} = $pd->{select_existing} | |
1205 | if $pd->{select_existing}; | |
1206 | } | |
1207 | ||
1208 | eval { activate_storage($cfg, $storeid, $cache); }; | |
1209 | if (my $err = $@) { | |
1210 | warn $err; | |
1211 | next; | |
1212 | } | |
1213 | ||
1214 | my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); }; | |
1215 | warn $@ if $@; | |
1216 | next if !$active; | |
1217 | $info->{$storeid}->{total} = int($total); | |
1218 | $info->{$storeid}->{avail} = int($avail); | |
1219 | $info->{$storeid}->{used} = int($used); | |
1220 | $info->{$storeid}->{active} = $active; | |
1221 | } | |
1222 | ||
1223 | return $info; | |
1224 | } | |
1225 | ||
1226 | sub resolv_server { | |
1227 | my ($server) = @_; | |
1228 | ||
1229 | my ($packed_ip, $family); | |
1230 | eval { | |
1231 | my @res = PVE::Tools::getaddrinfo_all($server); | |
1232 | $family = $res[0]->{family}; | |
1233 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1234 | }; | |
1235 | if (defined $packed_ip) { | |
1236 | return Socket::inet_ntop($family, $packed_ip); | |
1237 | } | |
1238 | return undef; | |
1239 | } | |
1240 | ||
1241 | sub scan_nfs { | |
1242 | my ($server_in) = @_; | |
1243 | ||
1244 | my $server; | |
1245 | if (!($server = resolv_server ($server_in))) { | |
1246 | die "unable to resolve address for server '${server_in}'\n"; | |
1247 | } | |
1248 | ||
1249 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1250 | ||
1251 | my $res = {}; | |
1252 | run_command($cmd, outfunc => sub { | |
1253 | my $line = shift; | |
1254 | ||
1255 | # note: howto handle white spaces in export path?? | |
1256 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1257 | $res->{$1} = $2; | |
1258 | } | |
1259 | }); | |
1260 | ||
1261 | return $res; | |
1262 | } | |
1263 | ||
1264 | sub scan_cifs { | |
1265 | my ($server_in, $user, $password, $domain) = @_; | |
1266 | ||
1267 | my $server = resolv_server($server_in); | |
1268 | die "unable to resolve address for server '${server_in}'\n" if !$server; | |
1269 | ||
1270 | # we only support Windows 2012 and newer, so just use smb3 | |
1271 | my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server]; | |
1272 | push @$cmd, '-W', $domain if defined($domain); | |
1273 | ||
1274 | push @$cmd, '-N' if !defined($password); | |
1275 | local $ENV{USER} = $user if defined($user); | |
1276 | local $ENV{PASSWD} = $password if defined($password); | |
1277 | ||
1278 | my $res = {}; | |
1279 | my $err = ''; | |
1280 | run_command($cmd, | |
1281 | noerr => 1, | |
1282 | errfunc => sub { | |
1283 | $err .= "$_[0]\n" | |
1284 | }, | |
1285 | outfunc => sub { | |
1286 | my $line = shift; | |
1287 | if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) { | |
1288 | $res->{$1} = $2; | |
1289 | } elsif ($line =~ m/(NT_STATUS_(\S+))/) { | |
1290 | my $status = $1; | |
1291 | $err .= "unexpected status: $1\n" if uc($1) ne 'SUCCESS'; | |
1292 | } | |
1293 | }, | |
1294 | ); | |
1295 | # only die if we got no share, else it's just some followup check error | |
1296 | # (like workgroup querying) | |
1297 | raise($err) if $err && !%$res; | |
1298 | ||
1299 | return $res; | |
1300 | } | |
1301 | ||
1302 | sub scan_zfs { | |
1303 | ||
1304 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-Hp', '-o', 'name,avail,used']; | |
1305 | ||
1306 | my $res = []; | |
1307 | run_command($cmd, outfunc => sub { | |
1308 | my $line = shift; | |
1309 | ||
1310 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
1311 | my ($pool, $size_str, $used_str) = ($1, $2, $3); | |
1312 | my $size = $size_str + 0; | |
1313 | my $used = $used_str + 0; | |
1314 | # ignore subvolumes generated by our ZFSPoolPlugin | |
1315 | return if $pool =~ m!/subvol-\d+-[^/]+$!; | |
1316 | return if $pool =~ m!/basevol-\d+-[^/]+$!; | |
1317 | push @$res, { pool => $pool, size => $size, free => $size-$used }; | |
1318 | } | |
1319 | }); | |
1320 | ||
1321 | return $res; | |
1322 | } | |
1323 | ||
1324 | sub resolv_portal { | |
1325 | my ($portal, $noerr) = @_; | |
1326 | ||
1327 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); | |
1328 | if ($server) { | |
1329 | if (my $ip = resolv_server($server)) { | |
1330 | $server = $ip; | |
1331 | $server = "[$server]" if $server =~ /^$IPV6RE$/; | |
1332 | return $port ? "$server:$port" : $server; | |
1333 | } | |
1334 | } | |
1335 | return undef if $noerr; | |
1336 | ||
1337 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1338 | } | |
1339 | ||
1340 | ||
1341 | sub scan_iscsi { | |
1342 | my ($portal_in) = @_; | |
1343 | ||
1344 | my $portal; | |
1345 | if (!($portal = resolv_portal($portal_in))) { | |
1346 | die "unable to parse/resolve portal address '${portal_in}'\n"; | |
1347 | } | |
1348 | ||
1349 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); | |
1350 | } | |
1351 | ||
1352 | sub storage_default_format { | |
1353 | my ($cfg, $storeid) = @_; | |
1354 | ||
1355 | my $scfg = storage_config ($cfg, $storeid); | |
1356 | ||
1357 | return PVE::Storage::Plugin::default_format($scfg); | |
1358 | } | |
1359 | ||
1360 | sub vgroup_is_used { | |
1361 | my ($cfg, $vgname) = @_; | |
1362 | ||
1363 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1364 | my $scfg = storage_config($cfg, $storeid); | |
1365 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { | |
1366 | return 1; | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | return undef; | |
1371 | } | |
1372 | ||
1373 | sub target_is_used { | |
1374 | my ($cfg, $target) = @_; | |
1375 | ||
1376 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1377 | my $scfg = storage_config($cfg, $storeid); | |
1378 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { | |
1379 | return 1; | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | return undef; | |
1384 | } | |
1385 | ||
1386 | sub volume_is_used { | |
1387 | my ($cfg, $volid) = @_; | |
1388 | ||
1389 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1390 | my $scfg = storage_config($cfg, $storeid); | |
1391 | if ($scfg->{base} && $scfg->{base} eq $volid) { | |
1392 | return 1; | |
1393 | } | |
1394 | } | |
1395 | ||
1396 | return undef; | |
1397 | } | |
1398 | ||
1399 | sub storage_is_used { | |
1400 | my ($cfg, $storeid) = @_; | |
1401 | ||
1402 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1403 | my $scfg = storage_config($cfg, $sid); | |
1404 | next if !$scfg->{base}; | |
1405 | my ($st) = parse_volume_id($scfg->{base}); | |
1406 | return 1 if $st && $st eq $storeid; | |
1407 | } | |
1408 | ||
1409 | return undef; | |
1410 | } | |
1411 | ||
1412 | sub foreach_volid { | |
1413 | my ($list, $func) = @_; | |
1414 | ||
1415 | return if !$list; | |
1416 | ||
1417 | foreach my $sid (keys %$list) { | |
1418 | foreach my $info (@{$list->{$sid}}) { | |
1419 | my $volid = $info->{volid}; | |
1420 | my ($sid1, $volname) = parse_volume_id($volid, 1); | |
1421 | if ($sid1 && $sid1 eq $sid) { | |
1422 | &$func ($volid, $sid, $info); | |
1423 | } else { | |
1424 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1425 | } | |
1426 | } | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | sub decompressor_info { | |
1431 | my ($format, $comp) = @_; | |
1432 | ||
1433 | if ($format eq 'tgz' && !defined($comp)) { | |
1434 | ($format, $comp) = ('tar', 'gz'); | |
1435 | } | |
1436 | ||
1437 | my $decompressor = { | |
1438 | tar => { | |
1439 | gz => ['tar', '-z'], | |
1440 | lzo => ['tar', '--lzop'], | |
1441 | zst => ['tar', '--zstd'], | |
1442 | }, | |
1443 | vma => { | |
1444 | gz => ['zcat'], | |
1445 | lzo => ['lzop', '-d', '-c'], | |
1446 | zst => ['zstd', '-q', '-d', '-c'], | |
1447 | }, | |
1448 | }; | |
1449 | ||
1450 | die "ERROR: archive format not defined\n" | |
1451 | if !defined($decompressor->{$format}); | |
1452 | ||
1453 | my $decomp; | |
1454 | $decomp = $decompressor->{$format}->{$comp} if $comp; | |
1455 | ||
1456 | my $info = { | |
1457 | format => $format, | |
1458 | compression => $comp, | |
1459 | decompressor => $decomp, | |
1460 | }; | |
1461 | ||
1462 | return $info; | |
1463 | } | |
1464 | ||
1465 | sub archive_info { | |
1466 | my ($archive) = shift; | |
1467 | my $info; | |
1468 | ||
1469 | my $volid = basename($archive); | |
1470 | if ($volid =~ /^(vzdump-(lxc|openvz|qemu)-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)$/) { | |
1471 | my $filename = "$1"; # untaint | |
1472 | my ($type, $format, $comp) = ($2, $3, $4); | |
1473 | my $format_re = defined($comp) ? "$format.$comp" : "$format"; | |
1474 | $info = decompressor_info($format, $comp); | |
1475 | $info->{filename} = $filename; | |
1476 | $info->{type} = $type; | |
1477 | ||
1478 | if ($volid =~ /^(vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2}))\.${format_re}$/) { | |
1479 | $info->{logfilename} = "$1.log"; | |
1480 | $info->{vmid} = int($2); | |
1481 | $info->{ctime} = timelocal($8, $7, $6, $5, $4 - 1, $3); | |
1482 | $info->{is_std_name} = 1; | |
1483 | } else { | |
1484 | $info->{is_std_name} = 0; | |
1485 | } | |
1486 | } else { | |
1487 | die "ERROR: couldn't determine archive info from '$archive'\n"; | |
1488 | } | |
1489 | ||
1490 | return $info; | |
1491 | } | |
1492 | ||
1493 | sub archive_remove { | |
1494 | my ($archive_path) = @_; | |
1495 | ||
1496 | my $dirname = dirname($archive_path); | |
1497 | my $archive_info = eval { archive_info($archive_path) } // {}; | |
1498 | my $logfn = $archive_info->{logfilename}; | |
1499 | ||
1500 | unlink $archive_path or die "removing archive $archive_path failed: $!\n"; | |
1501 | ||
1502 | if (defined($logfn)) { | |
1503 | my $logpath = "$dirname/$logfn"; | |
1504 | if (-e $logpath) { | |
1505 | unlink $logpath or warn "removing log file $logpath failed: $!\n"; | |
1506 | } | |
1507 | } | |
1508 | } | |
1509 | ||
1510 | sub extract_vzdump_config_tar { | |
1511 | my ($archive, $conf_re) = @_; | |
1512 | ||
1513 | die "ERROR: file '$archive' does not exist\n" if ! -f $archive; | |
1514 | ||
1515 | my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) || | |
1516 | die "unable to open file '$archive'\n"; | |
1517 | ||
1518 | my $file; | |
1519 | while (defined($file = <$fh>)) { | |
1520 | if ($file =~ $conf_re) { | |
1521 | $file = $1; # untaint | |
1522 | last; | |
1523 | } | |
1524 | } | |
1525 | ||
1526 | kill 15, $pid; | |
1527 | waitpid $pid, 0; | |
1528 | close $fh; | |
1529 | ||
1530 | die "ERROR: archive contains no configuration file\n" if !$file; | |
1531 | chomp $file; | |
1532 | ||
1533 | my $raw = ''; | |
1534 | my $out = sub { | |
1535 | my $output = shift; | |
1536 | $raw .= "$output\n"; | |
1537 | }; | |
1538 | ||
1539 | run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out); | |
1540 | ||
1541 | return wantarray ? ($raw, $file) : $raw; | |
1542 | } | |
1543 | ||
1544 | sub extract_vzdump_config_vma { | |
1545 | my ($archive, $comp) = @_; | |
1546 | ||
1547 | my $raw = ''; | |
1548 | my $out = sub { $raw .= "$_[0]\n"; }; | |
1549 | ||
1550 | my $info = archive_info($archive); | |
1551 | $comp //= $info->{compression}; | |
1552 | my $decompressor = $info->{decompressor}; | |
1553 | ||
1554 | if ($comp) { | |
1555 | my $cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ]; | |
1556 | ||
1557 | # lzop/zcat exits with 1 when the pipe is closed early by vma, detect this and ignore the exit code later | |
1558 | my $broken_pipe; | |
1559 | my $errstring; | |
1560 | my $err = sub { | |
1561 | my $output = shift; | |
1562 | if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/ || $output =~ m/zstd: error 70 : Write error : Broken pipe/) { | |
1563 | $broken_pipe = 1; | |
1564 | } elsif (!defined ($errstring) && $output !~ m/^\s*$/) { | |
1565 | $errstring = "Failed to extract config from VMA archive: $output\n"; | |
1566 | } | |
1567 | }; | |
1568 | ||
1569 | my $rc = eval { run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1) }; | |
1570 | my $rerr = $@; | |
1571 | ||
1572 | $broken_pipe ||= $rc == 141; # broken pipe from vma POV | |
1573 | ||
1574 | if (!$errstring && !$broken_pipe && $rc != 0) { | |
1575 | die "$rerr\n" if $rerr; | |
1576 | die "config extraction failed with exit code $rc\n"; | |
1577 | } | |
1578 | die "$errstring\n" if $errstring; | |
1579 | } else { | |
1580 | run_command(["vma", "config", $archive], outfunc => $out); | |
1581 | } | |
1582 | ||
1583 | return wantarray ? ($raw, undef) : $raw; | |
1584 | } | |
1585 | ||
1586 | sub extract_vzdump_config { | |
1587 | my ($cfg, $volid) = @_; | |
1588 | ||
1589 | my ($storeid, $volname) = parse_volume_id($volid); | |
1590 | if (defined($storeid)) { | |
1591 | my $scfg = storage_config($cfg, $storeid); | |
1592 | if ($scfg->{type} eq 'pbs') { | |
1593 | storage_check_enabled($cfg, $storeid); | |
1594 | return PVE::Storage::PBSPlugin->extract_vzdump_config($scfg, $volname, $storeid); | |
1595 | } | |
1596 | } | |
1597 | ||
1598 | my $archive = abs_filesystem_path($cfg, $volid); | |
1599 | my $info = archive_info($archive); | |
1600 | my $format = $info->{format}; | |
1601 | my $comp = $info->{compression}; | |
1602 | my $type = $info->{type}; | |
1603 | ||
1604 | if ($type eq 'lxc' || $type eq 'openvz') { | |
1605 | return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!); | |
1606 | } elsif ($type eq 'qemu') { | |
1607 | if ($format eq 'tar') { | |
1608 | return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!); | |
1609 | } else { | |
1610 | return extract_vzdump_config_vma($archive, $comp); | |
1611 | } | |
1612 | } else { | |
1613 | die "cannot determine backup guest type for backup archive '$volid'\n"; | |
1614 | } | |
1615 | } | |
1616 | ||
1617 | sub prune_backups { | |
1618 | my ($cfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_; | |
1619 | ||
1620 | my $scfg = storage_config($cfg, $storeid); | |
1621 | die "storage '$storeid' does not support backups\n" if !$scfg->{content}->{backup}; | |
1622 | ||
1623 | if (!defined($keep)) { | |
1624 | die "no prune-backups options configured for storage '$storeid'\n" | |
1625 | if !defined($scfg->{'prune-backups'}); | |
1626 | $keep = PVE::JSONSchema::parse_property_string('prune-backups', $scfg->{'prune-backups'}); | |
1627 | } | |
1628 | ||
1629 | activate_storage($cfg, $storeid); | |
1630 | ||
1631 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1632 | return $plugin->prune_backups($scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc); | |
1633 | } | |
1634 | ||
1635 | my $prune_mark = sub { | |
1636 | my ($prune_entries, $keep_count, $id_func) = @_; | |
1637 | ||
1638 | return if !$keep_count; | |
1639 | ||
1640 | my $already_included = {}; | |
1641 | my $newly_included = {}; | |
1642 | ||
1643 | foreach my $prune_entry (@{$prune_entries}) { | |
1644 | my $mark = $prune_entry->{mark}; | |
1645 | my $id = $id_func->($prune_entry->{ctime}); | |
1646 | $already_included->{$id} = 1 if defined($mark) && $mark eq 'keep'; | |
1647 | } | |
1648 | ||
1649 | foreach my $prune_entry (@{$prune_entries}) { | |
1650 | my $mark = $prune_entry->{mark}; | |
1651 | my $id = $id_func->($prune_entry->{ctime}); | |
1652 | ||
1653 | next if defined($mark) || $already_included->{$id}; | |
1654 | ||
1655 | if (!$newly_included->{$id}) { | |
1656 | last if scalar(keys %{$newly_included}) >= $keep_count; | |
1657 | $newly_included->{$id} = 1; | |
1658 | $prune_entry->{mark} = 'keep'; | |
1659 | } else { | |
1660 | $prune_entry->{mark} = 'remove'; | |
1661 | } | |
1662 | } | |
1663 | }; | |
1664 | ||
1665 | sub prune_mark_backup_group { | |
1666 | my ($backup_group, $keep) = @_; | |
1667 | ||
1668 | my $keep_all = delete $keep->{'keep-all'}; | |
1669 | ||
1670 | if ($keep_all || !scalar(grep {$_ > 0} values %{$keep})) { | |
1671 | $keep = { 'keep-all' => 1 } if $keep_all; | |
1672 | foreach my $prune_entry (@{$backup_group}) { | |
1673 | $prune_entry->{mark} = 'keep'; | |
1674 | } | |
1675 | return; | |
1676 | } | |
1677 | ||
1678 | my $prune_list = [ sort { $b->{ctime} <=> $a->{ctime} } @{$backup_group} ]; | |
1679 | ||
1680 | $prune_mark->($prune_list, $keep->{'keep-last'}, sub { | |
1681 | my ($ctime) = @_; | |
1682 | return $ctime; | |
1683 | }); | |
1684 | $prune_mark->($prune_list, $keep->{'keep-hourly'}, sub { | |
1685 | my ($ctime) = @_; | |
1686 | my (undef, undef, $hour, $day, $month, $year) = localtime($ctime); | |
1687 | return "$hour/$day/$month/$year"; | |
1688 | }); | |
1689 | $prune_mark->($prune_list, $keep->{'keep-daily'}, sub { | |
1690 | my ($ctime) = @_; | |
1691 | my (undef, undef, undef, $day, $month, $year) = localtime($ctime); | |
1692 | return "$day/$month/$year"; | |
1693 | }); | |
1694 | $prune_mark->($prune_list, $keep->{'keep-weekly'}, sub { | |
1695 | my ($ctime) = @_; | |
1696 | my ($sec, $min, $hour, $day, $month, $year) = localtime($ctime); | |
1697 | my $iso_week = int(strftime("%V", $sec, $min, $hour, $day, $month, $year)); | |
1698 | my $iso_week_year = int(strftime("%G", $sec, $min, $hour, $day, $month, $year)); | |
1699 | return "$iso_week/$iso_week_year"; | |
1700 | }); | |
1701 | $prune_mark->($prune_list, $keep->{'keep-monthly'}, sub { | |
1702 | my ($ctime) = @_; | |
1703 | my (undef, undef, undef, undef, $month, $year) = localtime($ctime); | |
1704 | return "$month/$year"; | |
1705 | }); | |
1706 | $prune_mark->($prune_list, $keep->{'keep-yearly'}, sub { | |
1707 | my ($ctime) = @_; | |
1708 | my $year = (localtime($ctime))[5]; | |
1709 | return "$year"; | |
1710 | }); | |
1711 | ||
1712 | foreach my $prune_entry (@{$prune_list}) { | |
1713 | $prune_entry->{mark} //= 'remove'; | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | sub volume_export { | |
1718 | my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1719 | ||
1720 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1721 | die "cannot export volume '$volid'\n" if !$storeid; | |
1722 | my $scfg = storage_config($cfg, $storeid); | |
1723 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1724 | return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format, | |
1725 | $snapshot, $base_snapshot, $with_snapshots); | |
1726 | } | |
1727 | ||
1728 | sub volume_import { | |
1729 | my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_; | |
1730 | ||
1731 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1732 | die "cannot import into volume '$volid'\n" if !$storeid; | |
1733 | my $scfg = storage_config($cfg, $storeid); | |
1734 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1735 | return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format, | |
1736 | $base_snapshot, $with_snapshots, $allow_rename) // $volid; | |
1737 | } | |
1738 | ||
1739 | sub volume_export_formats { | |
1740 | my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1741 | ||
1742 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1743 | return if !$storeid; | |
1744 | my $scfg = storage_config($cfg, $storeid); | |
1745 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1746 | return $plugin->volume_export_formats($scfg, $storeid, $volname, | |
1747 | $snapshot, $base_snapshot, | |
1748 | $with_snapshots); | |
1749 | } | |
1750 | ||
1751 | sub volume_import_formats { | |
1752 | my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_; | |
1753 | ||
1754 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1755 | return if !$storeid; | |
1756 | my $scfg = storage_config($cfg, $storeid); | |
1757 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1758 | return $plugin->volume_import_formats($scfg, $storeid, $volname, | |
1759 | $base_snapshot, $with_snapshots); | |
1760 | } | |
1761 | ||
1762 | sub volume_transfer_formats { | |
1763 | my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1764 | my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots); | |
1765 | my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots); | |
1766 | my %import_hash = map { $_ => 1 } @import_formats; | |
1767 | my @common = grep { $import_hash{$_} } @export_formats; | |
1768 | return @common; | |
1769 | } | |
1770 | ||
1771 | sub volume_imported_message { | |
1772 | my ($volid, $want_pattern) = @_; | |
1773 | ||
1774 | if ($want_pattern) { | |
1775 | return qr/successfully imported '([^']*)'$/; | |
1776 | } else { | |
1777 | return "successfully imported '$volid'\n"; | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | # bash completion helper | |
1782 | ||
1783 | sub complete_storage { | |
1784 | my ($cmdname, $pname, $cvalue) = @_; | |
1785 | ||
1786 | my $cfg = PVE::Storage::config(); | |
1787 | ||
1788 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; | |
1789 | } | |
1790 | ||
1791 | sub complete_storage_enabled { | |
1792 | my ($cmdname, $pname, $cvalue) = @_; | |
1793 | ||
1794 | my $res = []; | |
1795 | ||
1796 | my $cfg = PVE::Storage::config(); | |
1797 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1798 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1799 | push @$res, $sid; | |
1800 | } | |
1801 | return $res; | |
1802 | } | |
1803 | ||
1804 | sub complete_content_type { | |
1805 | my ($cmdname, $pname, $cvalue) = @_; | |
1806 | ||
1807 | return [qw(rootdir images vztmpl iso backup snippets)]; | |
1808 | } | |
1809 | ||
1810 | sub complete_volume { | |
1811 | my ($cmdname, $pname, $cvalue) = @_; | |
1812 | ||
1813 | my $cfg = config(); | |
1814 | ||
1815 | my $storage_list = complete_storage_enabled(); | |
1816 | ||
1817 | if ($cvalue =~ m/^([^:]+):/) { | |
1818 | $storage_list = [ $1 ]; | |
1819 | } else { | |
1820 | if (scalar(@$storage_list) > 1) { | |
1821 | # only list storage IDs to avoid large listings | |
1822 | my $res = []; | |
1823 | foreach my $storeid (@$storage_list) { | |
1824 | # Hack: simply return 2 artificial values, so that | |
1825 | # completions does not finish | |
1826 | push @$res, "$storeid:volname", "$storeid:..."; | |
1827 | } | |
1828 | return $res; | |
1829 | } | |
1830 | } | |
1831 | ||
1832 | my $res = []; | |
1833 | foreach my $storeid (@$storage_list) { | |
1834 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1835 | ||
1836 | foreach my $item (@$vollist) { | |
1837 | push @$res, $item->{volid}; | |
1838 | } | |
1839 | } | |
1840 | ||
1841 | return $res; | |
1842 | } | |
1843 | ||
1844 | # Various io-heavy operations require io/bandwidth limits which can be | |
1845 | # configured on multiple levels: The global defaults in datacenter.cfg, and | |
1846 | # per-storage overrides. When we want to do a restore from storage A to storage | |
1847 | # B, we should take the smaller limit defined for storages A and B, and if no | |
1848 | # such limit was specified, use the one from datacenter.cfg. | |
1849 | sub get_bandwidth_limit { | |
1850 | my ($operation, $storage_list, $override) = @_; | |
1851 | ||
1852 | # called for each limit (global, per-storage) with the 'default' and the | |
1853 | # $operation limit and should udpate $override for every limit affecting | |
1854 | # us. | |
1855 | my $use_global_limits = 0; | |
1856 | my $apply_limit = sub { | |
1857 | my ($bwlimit) = @_; | |
1858 | if (defined($bwlimit)) { | |
1859 | my $limits = PVE::JSONSchema::parse_property_string('bwlimit', $bwlimit); | |
1860 | my $limit = $limits->{$operation} // $limits->{default}; | |
1861 | if (defined($limit)) { | |
1862 | if (!$override || $limit < $override) { | |
1863 | $override = $limit; | |
1864 | } | |
1865 | return; | |
1866 | } | |
1867 | } | |
1868 | # If there was no applicable limit, try to apply the global ones. | |
1869 | $use_global_limits = 1; | |
1870 | }; | |
1871 | ||
1872 | my ($rpcenv, $authuser); | |
1873 | if (defined($override)) { | |
1874 | $rpcenv = PVE::RPCEnvironment->get(); | |
1875 | $authuser = $rpcenv->get_user(); | |
1876 | } | |
1877 | ||
1878 | # Apply per-storage limits - if there are storages involved. | |
1879 | if (defined($storage_list) && @$storage_list) { | |
1880 | my $config = config(); | |
1881 | ||
1882 | # The Datastore.Allocate permission allows us to modify the per-storage | |
1883 | # limits, therefore it also allows us to override them. | |
1884 | # Since we have most likely multiple storages to check, do a quick check on | |
1885 | # the general '/storage' path to see if we can skip the checks entirely: | |
1886 | return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1); | |
1887 | ||
1888 | my %done; | |
1889 | foreach my $storage (@$storage_list) { | |
1890 | next if !defined($storage); | |
1891 | # Avoid duplicate checks: | |
1892 | next if $done{$storage}; | |
1893 | $done{$storage} = 1; | |
1894 | ||
1895 | # Otherwise we may still have individual /storage/$ID permissions: | |
1896 | if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) { | |
1897 | # And if not: apply the limits. | |
1898 | my $storecfg = storage_config($config, $storage); | |
1899 | $apply_limit->($storecfg->{bwlimit}); | |
1900 | } | |
1901 | } | |
1902 | ||
1903 | # Storage limits take precedence over the datacenter defaults, so if | |
1904 | # a limit was applied: | |
1905 | return $override if !$use_global_limits; | |
1906 | } | |
1907 | ||
1908 | # Sys.Modify on '/' means we can change datacenter.cfg which contains the | |
1909 | # global default limits. | |
1910 | if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) { | |
1911 | # So if we cannot modify global limits, apply them to our currently | |
1912 | # requested override. | |
1913 | my $dc = cfs_read_file('datacenter.cfg'); | |
1914 | $apply_limit->($dc->{bwlimit}); | |
1915 | } | |
1916 | ||
1917 | return $override; | |
1918 | } | |
1919 | ||
1920 | # checks if the storage id is available and dies if not | |
1921 | sub assert_sid_unused { | |
1922 | my ($sid) = @_; | |
1923 | ||
1924 | my $cfg = config(); | |
1925 | if (my $scfg = storage_config($cfg, $sid, 1)) { | |
1926 | die "storage ID '$sid' already defined\n"; | |
1927 | } | |
1928 | ||
1929 | return undef; | |
1930 | } | |
1931 | ||
1932 | 1; |