]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
683c920ba1259d45236f41890348aa80b6def1c9
[pve-storage.git] / PVE / Storage.pm
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 File::Basename;
11 use File::Path;
12 use Cwd 'abs_path';
13 use Socket;
14
15 use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
16 use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
17 use PVE::Exception qw(raise_param_exc);
18 use PVE::JSONSchema;
19 use PVE::INotify;
20 use PVE::RPCEnvironment;
21
22 use PVE::Storage::Plugin;
23 use PVE::Storage::DirPlugin;
24 use PVE::Storage::LVMPlugin;
25 use PVE::Storage::LvmThinPlugin;
26 use PVE::Storage::NFSPlugin;
27 use PVE::Storage::ISCSIPlugin;
28 use PVE::Storage::RBDPlugin;
29 use PVE::Storage::SheepdogPlugin;
30 use PVE::Storage::ISCSIDirectPlugin;
31 use PVE::Storage::GlusterfsPlugin;
32 use PVE::Storage::ZFSPoolPlugin;
33 use PVE::Storage::ZFSPlugin;
34 use PVE::Storage::DRBDPlugin;
35
36 # Storage API version. Icrement it on changes in storage API interface.
37 use constant APIVER => 1;
38
39 # load standard plugins
40 PVE::Storage::DirPlugin->register();
41 PVE::Storage::LVMPlugin->register();
42 PVE::Storage::LvmThinPlugin->register();
43 PVE::Storage::NFSPlugin->register();
44 PVE::Storage::ISCSIPlugin->register();
45 PVE::Storage::RBDPlugin->register();
46 PVE::Storage::SheepdogPlugin->register();
47 PVE::Storage::ISCSIDirectPlugin->register();
48 PVE::Storage::GlusterfsPlugin->register();
49 PVE::Storage::ZFSPoolPlugin->register();
50 PVE::Storage::ZFSPlugin->register();
51 PVE::Storage::DRBDPlugin->register();
52
53 # load third-party plugins
54 if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) {
55 dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
56 my ($file) = @_;
57 my $modname = 'PVE::Storage::Custom::' . $file;
58 $modname =~ s!\.pm$!!;
59 $file = 'PVE/Storage/Custom/' . $file;
60
61 eval {
62 require $file;
63 };
64 if ($@) {
65 warn $@;
66 # Check storage API version and that file is really storage plugin.
67 } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER) {
68 eval {
69 import $file;
70 $modname->register();
71 };
72 warn $@ if $@;
73 } else {
74 warn "Error loading storage plugin \"$modname\" because of API version mismatch. Please, update it.\n"
75 }
76 });
77 }
78
79 # initialize all plugins
80 PVE::Storage::Plugin->init();
81
82 my $UDEVADM = '/sbin/udevadm';
83
84 # PVE::Storage utility functions
85
86 sub config {
87 return cfs_read_file("storage.cfg");
88 }
89
90 sub write_config {
91 my ($cfg) = @_;
92
93 cfs_write_file('storage.cfg', $cfg);
94 }
95
96 sub lock_storage_config {
97 my ($code, $errmsg) = @_;
98
99 cfs_lock_file("storage.cfg", undef, $code);
100 my $err = $@;
101 if ($err) {
102 $errmsg ? die "$errmsg: $err" : die $err;
103 }
104 }
105
106 sub storage_config {
107 my ($cfg, $storeid, $noerr) = @_;
108
109 die "no storage ID specified\n" if !$storeid;
110
111 my $scfg = $cfg->{ids}->{$storeid};
112
113 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
114
115 return $scfg;
116 }
117
118 sub storage_check_node {
119 my ($cfg, $storeid, $node, $noerr) = @_;
120
121 my $scfg = storage_config($cfg, $storeid);
122
123 if ($scfg->{nodes}) {
124 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
125 if (!$scfg->{nodes}->{$node}) {
126 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
127 return undef;
128 }
129 }
130
131 return $scfg;
132 }
133
134 sub storage_check_enabled {
135 my ($cfg, $storeid, $node, $noerr) = @_;
136
137 my $scfg = storage_config($cfg, $storeid);
138
139 if ($scfg->{disable}) {
140 die "storage '$storeid' is disabled\n" if !$noerr;
141 return undef;
142 }
143
144 return storage_check_node($cfg, $storeid, $node, $noerr);
145 }
146
147 sub storage_ids {
148 my ($cfg) = @_;
149
150 return keys %{$cfg->{ids}};
151 }
152
153 sub file_size_info {
154 my ($filename, $timeout) = @_;
155
156 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
157 }
158
159 sub volume_size_info {
160 my ($cfg, $volid, $timeout) = @_;
161
162 my ($storeid, $volname) = parse_volume_id($volid, 1);
163 if ($storeid) {
164 my $scfg = storage_config($cfg, $storeid);
165 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
166 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
167 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
168 return file_size_info($volid, $timeout);
169 } else {
170 return 0;
171 }
172 }
173
174 sub volume_resize {
175 my ($cfg, $volid, $size, $running) = @_;
176
177 my ($storeid, $volname) = parse_volume_id($volid, 1);
178 if ($storeid) {
179 my $scfg = storage_config($cfg, $storeid);
180 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
181 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
182 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
183 die "resize file/device '$volid' is not possible\n";
184 } else {
185 die "unable to parse volume ID '$volid'\n";
186 }
187 }
188
189 sub volume_rollback_is_possible {
190 my ($cfg, $volid, $snap) = @_;
191
192 my ($storeid, $volname) = parse_volume_id($volid, 1);
193 if ($storeid) {
194 my $scfg = storage_config($cfg, $storeid);
195 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
196 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
197 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
198 die "snapshot rollback file/device '$volid' is not possible\n";
199 } else {
200 die "unable to parse volume ID '$volid'\n";
201 }
202 }
203
204 sub volume_snapshot {
205 my ($cfg, $volid, $snap) = @_;
206
207 my ($storeid, $volname) = parse_volume_id($volid, 1);
208 if ($storeid) {
209 my $scfg = storage_config($cfg, $storeid);
210 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
211 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
212 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
213 die "snapshot file/device '$volid' is not possible\n";
214 } else {
215 die "unable to parse volume ID '$volid'\n";
216 }
217 }
218
219 sub volume_snapshot_rollback {
220 my ($cfg, $volid, $snap) = @_;
221
222 my ($storeid, $volname) = parse_volume_id($volid, 1);
223 if ($storeid) {
224 my $scfg = storage_config($cfg, $storeid);
225 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
226 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
227 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
228 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
229 die "snapshot rollback file/device '$volid' is not possible\n";
230 } else {
231 die "unable to parse volume ID '$volid'\n";
232 }
233 }
234
235 sub volume_snapshot_delete {
236 my ($cfg, $volid, $snap, $running) = @_;
237
238 my ($storeid, $volname) = parse_volume_id($volid, 1);
239 if ($storeid) {
240 my $scfg = storage_config($cfg, $storeid);
241 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
242 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
243 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
244 die "snapshot delete file/device '$volid' is not possible\n";
245 } else {
246 die "unable to parse volume ID '$volid'\n";
247 }
248 }
249
250 sub volume_has_feature {
251 my ($cfg, $feature, $volid, $snap, $running) = @_;
252
253 my ($storeid, $volname) = parse_volume_id($volid, 1);
254 if ($storeid) {
255 my $scfg = storage_config($cfg, $storeid);
256 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
257 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
258 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
259 return undef;
260 } else {
261 return undef;
262 }
263 }
264
265 sub get_image_dir {
266 my ($cfg, $storeid, $vmid) = @_;
267
268 my $scfg = storage_config($cfg, $storeid);
269 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
270
271 my $path = $plugin->get_subdir($scfg, 'images');
272
273 return $vmid ? "$path/$vmid" : $path;
274 }
275
276 sub get_private_dir {
277 my ($cfg, $storeid, $vmid) = @_;
278
279 my $scfg = storage_config($cfg, $storeid);
280 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
281
282 my $path = $plugin->get_subdir($scfg, 'rootdir');
283
284 return $vmid ? "$path/$vmid" : $path;
285 }
286
287 sub get_iso_dir {
288 my ($cfg, $storeid) = @_;
289
290 my $scfg = storage_config($cfg, $storeid);
291 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
292
293 return $plugin->get_subdir($scfg, 'iso');
294 }
295
296 sub get_vztmpl_dir {
297 my ($cfg, $storeid) = @_;
298
299 my $scfg = storage_config($cfg, $storeid);
300 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
301
302 return $plugin->get_subdir($scfg, 'vztmpl');
303 }
304
305 sub get_backup_dir {
306 my ($cfg, $storeid) = @_;
307
308 my $scfg = storage_config($cfg, $storeid);
309 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
310
311 return $plugin->get_subdir($scfg, 'backup');
312 }
313
314 # library implementation
315
316 sub parse_vmid {
317 my $vmid = shift;
318
319 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
320
321 return int($vmid);
322 }
323
324 # NOTE: basename and basevmid are always undef for LVM-thin, where the
325 # clone -> base reference is not encoded in the volume ID.
326 # see note in PVE::Storage::LvmThinPlugin for details.
327 sub parse_volname {
328 my ($cfg, $volid) = @_;
329
330 my ($storeid, $volname) = parse_volume_id($volid);
331
332 my $scfg = storage_config($cfg, $storeid);
333
334 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
335
336 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
337
338 return $plugin->parse_volname($volname);
339 }
340
341 sub parse_volume_id {
342 my ($volid, $noerr) = @_;
343
344 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
345 }
346
347 my $volume_is_base_and_used__no_lock = sub {
348 my ($scfg, $storeid, $plugin, $volname) = @_;
349
350 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
351 $plugin->parse_volname($volname);
352
353 if ($isBase) {
354 my $vollist = $plugin->list_images($storeid, $scfg);
355 foreach my $info (@$vollist) {
356 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
357 my $basename = undef;
358 my $basevmid = undef;
359
360 eval{
361 (undef, undef, undef, $basename, $basevmid) =
362 $plugin->parse_volname($tmpvolname);
363 };
364
365 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
366 return 1;
367 }
368 }
369 }
370 return 0;
371 };
372
373 # NOTE: this check does not work for LVM-thin, where the clone -> base
374 # reference is not encoded in the volume ID.
375 # see note in PVE::Storage::LvmThinPlugin for details.
376 sub volume_is_base_and_used {
377 my ($cfg, $volid) = @_;
378
379 my ($storeid, $volname) = parse_volume_id($volid);
380 my $scfg = storage_config($cfg, $storeid);
381 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
382
383 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
384 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
385 });
386 }
387
388 # try to map a filesystem path to a volume identifier
389 sub path_to_volume_id {
390 my ($cfg, $path) = @_;
391
392 my $ids = $cfg->{ids};
393
394 my ($sid, $volname) = parse_volume_id($path, 1);
395 if ($sid) {
396 if (my $scfg = $ids->{$sid}) {
397 if ($scfg->{path}) {
398 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
399 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
400 return ($vtype, $path);
401 }
402 }
403 return ('');
404 }
405
406 # Note: abs_path() return undef if $path doesn not exist
407 # for example when nfs storage is not mounted
408 $path = abs_path($path) || $path;
409
410 foreach my $sid (keys %$ids) {
411 my $scfg = $ids->{$sid};
412 next if !$scfg->{path};
413 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
414 my $imagedir = $plugin->get_subdir($scfg, 'images');
415 my $isodir = $plugin->get_subdir($scfg, 'iso');
416 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
417 my $backupdir = $plugin->get_subdir($scfg, 'backup');
418 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
419
420 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
421 my $vmid = $1;
422 my $name = $2;
423
424 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
425 foreach my $info (@$vollist) {
426 my ($storeid, $volname) = parse_volume_id($info->{volid});
427 my $volpath = $plugin->path($scfg, $volname, $storeid);
428 if ($volpath eq $path) {
429 return ('images', $info->{volid});
430 }
431 }
432 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
433 my $name = $1;
434 return ('iso', "$sid:iso/$name");
435 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
436 my $name = $1;
437 return ('vztmpl', "$sid:vztmpl/$name");
438 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
439 my $vmid = $1;
440 return ('rootdir', "$sid:rootdir/$vmid");
441 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
442 my $name = $1;
443 return ('iso', "$sid:backup/$name");
444 }
445 }
446
447 # can't map path to volume id
448 return ('');
449 }
450
451 sub path {
452 my ($cfg, $volid, $snapname) = @_;
453
454 my ($storeid, $volname) = parse_volume_id($volid);
455
456 my $scfg = storage_config($cfg, $storeid);
457
458 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
459 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
460 return wantarray ? ($path, $owner, $vtype) : $path;
461 }
462
463 sub abs_filesystem_path {
464 my ($cfg, $volid) = @_;
465
466 my $path;
467 if (PVE::Storage::parse_volume_id ($volid, 1)) {
468 PVE::Storage::activate_volumes($cfg, [ $volid ]);
469 $path = PVE::Storage::path($cfg, $volid);
470 } else {
471 if (-f $volid) {
472 my $abspath = abs_path($volid);
473 if ($abspath && $abspath =~ m|^(/.+)$|) {
474 $path = $1; # untaint any path
475 }
476 }
477 }
478
479 die "can't find file '$volid'\n" if !($path && -f $path);
480
481 return $path;
482 }
483
484 sub storage_migrate {
485 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
486
487 my ($storeid, $volname) = parse_volume_id($volid);
488 $target_volname = $volname if !$target_volname;
489
490 my $scfg = storage_config($cfg, $storeid);
491
492 # no need to migrate shared content
493 return if $storeid eq $target_storeid && $scfg->{shared};
494
495 my $tcfg = storage_config($cfg, $target_storeid);
496
497 my $target_volid = "${target_storeid}:${target_volname}";
498
499 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
500
501 my $sshoptions = "-o 'BatchMode=yes'";
502 my $ssh = "/usr/bin/ssh $sshoptions";
503
504 local $ENV{RSYNC_RSH} = $ssh;
505
506 # only implemented for file system based storage
507 if ($scfg->{path}) {
508 if ($tcfg->{path}) {
509
510 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
511 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
512 my $src = $src_plugin->path($scfg, $volname, $storeid);
513 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
514
515 my $dirname = dirname($dst);
516
517 if ($tcfg->{shared}) { # we can do a local copy
518
519 run_command(['/bin/mkdir', '-p', $dirname]);
520
521 run_command(['/bin/cp', $src, $dst]);
522
523 } else {
524 run_command(['/usr/bin/ssh', "root\@${target_host}",
525 '/bin/mkdir', '-p', $dirname]);
526
527 # we use rsync with --sparse, so we can't use --inplace,
528 # so we remove file on the target if it already exists to
529 # save space
530 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
531 if ($format && ($format eq 'raw') && $size) {
532 run_command(['/usr/bin/ssh', "root\@${target_host}",
533 'rm', '-f', $dst],
534 outfunc => sub {});
535 }
536
537 my $cmd;
538 if ($format eq 'subvol') {
539 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
540 '-aH', '--delete', '--no-whole-file', '--inplace',
541 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
542 } else {
543 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
544 $src, "[root\@${target_host}]:$dst"];
545 }
546
547 my $percent = -1;
548
549 run_command($cmd, outfunc => sub {
550 my $line = shift;
551
552 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
553 if ($2 > $percent) {
554 $percent = $2;
555 print "rsync status: $1\n";
556 *STDOUT->flush();
557 }
558 } else {
559 print "$line\n";
560 *STDOUT->flush();
561 }
562 });
563 }
564 } else {
565 die "$errstr - target type '$tcfg->{type}' not implemented\n";
566 }
567
568 } elsif ($scfg->{type} eq 'zfspool') {
569
570 if ($tcfg->{type} eq 'zfspool') {
571
572 die "$errstr - pool on target does not have the same name as on source!"
573 if $tcfg->{pool} ne $scfg->{pool};
574
575 my (undef, $volname) = parse_volname($cfg, $volid);
576
577 my $zfspath = "$scfg->{pool}\/$volname";
578
579 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
580
581 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
582 'zfs', 'recv', $zfspath]];
583
584 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
585 run_command($snap);
586 eval{
587 run_command($send);
588 };
589 my $err = $@;
590 warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err;
591 eval { run_command(['zfs', 'destroy', "$zfspath\@__migration__"]); };
592 warn "could not remove source snapshot: $@\n" if $@;
593 eval { run_command($destroy_target); };
594 warn "could not remove target snapshot: $@\n" if $@;
595 die $err if $err;
596
597 } else {
598 die "$errstr - target type $tcfg->{type} is not valid\n";
599 }
600
601 } elsif ($scfg->{type} eq 'lvmthin' || $scfg->{type} eq 'lvm') {
602
603 if (($scfg->{type} eq $tcfg->{type}) &&
604 ($tcfg->{type} eq 'lvmthin' || $tcfg->{type} eq 'lvm')) {
605
606 my (undef, $volname, $vmid) = parse_volname($cfg, $volid);
607 my $size = volume_size_info($cfg, $volid, 5);
608 my $src = path($cfg, $volid);
609 my $dst = path($cfg, $target_volid);
610
611 run_command(['/usr/bin/ssh', "root\@${target_host}",
612 'pvesm', 'alloc', $target_storeid, $vmid,
613 $target_volname, int($size/1024)]);
614
615 eval {
616 if ($tcfg->{type} eq 'lvmthin') {
617 run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
618 "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
619 } else {
620 run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
621 "dd", "of=$dst", "bs=4k"]]);
622 }
623 };
624 if (my $err = $@) {
625 run_command(['/usr/bin/ssh', "root\@${target_host}",
626 'pvesm', 'free', $target_volid]);
627 die $err;
628 }
629 } else {
630 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
631 }
632 } else {
633 die "$errstr - source type '$scfg->{type}' not implemented\n";
634 }
635 }
636
637 sub vdisk_clone {
638 my ($cfg, $volid, $vmid, $snap) = @_;
639
640 my ($storeid, $volname) = parse_volume_id($volid);
641
642 my $scfg = storage_config($cfg, $storeid);
643
644 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
645
646 activate_storage($cfg, $storeid);
647
648 # lock shared storage
649 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
650 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
651 return "$storeid:$volname";
652 });
653 }
654
655 sub vdisk_create_base {
656 my ($cfg, $volid) = @_;
657
658 my ($storeid, $volname) = parse_volume_id($volid);
659
660 my $scfg = storage_config($cfg, $storeid);
661
662 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
663
664 activate_storage($cfg, $storeid);
665
666 # lock shared storage
667 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
668 my $volname = $plugin->create_base($storeid, $scfg, $volname);
669 return "$storeid:$volname";
670 });
671 }
672
673 sub vdisk_alloc {
674 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
675
676 die "no storage ID specified\n" if !$storeid;
677
678 PVE::JSONSchema::parse_storage_id($storeid);
679
680 my $scfg = storage_config($cfg, $storeid);
681
682 die "no VMID specified\n" if !$vmid;
683
684 $vmid = parse_vmid($vmid);
685
686 my $defformat = PVE::Storage::Plugin::default_format($scfg);
687
688 $fmt = $defformat if !$fmt;
689
690 activate_storage($cfg, $storeid);
691
692 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
693
694 # lock shared storage
695 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
696 my $old_umask = umask(umask|0037);
697 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
698 my $err = $@;
699 umask $old_umask;
700 die $err if $err;
701 return "$storeid:$volname";
702 });
703 }
704
705 sub vdisk_free {
706 my ($cfg, $volid) = @_;
707
708 my ($storeid, $volname) = parse_volume_id($volid);
709 my $scfg = storage_config($cfg, $storeid);
710 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
711
712 activate_storage($cfg, $storeid);
713
714 my $cleanup_worker;
715
716 # lock shared storage
717 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
718 # LVM-thin allows deletion of still referenced base volumes!
719 die "base volume '$volname' is still in use by linked clones\n"
720 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
721
722 my (undef, undef, undef, undef, undef, $isBase, $format) =
723 $plugin->parse_volname($volname);
724 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
725 });
726
727 return if !$cleanup_worker;
728
729 my $rpcenv = PVE::RPCEnvironment::get();
730 my $authuser = $rpcenv->get_user();
731
732 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
733 }
734
735 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
736 sub template_list {
737 my ($cfg, $storeid, $tt) = @_;
738
739 die "unknown template type '$tt'\n"
740 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
741
742 my $ids = $cfg->{ids};
743
744 storage_check_enabled($cfg, $storeid) if ($storeid);
745
746 my $res = {};
747
748 # query the storage
749
750 foreach my $sid (keys %$ids) {
751 next if $storeid && $storeid ne $sid;
752
753 my $scfg = $ids->{$sid};
754 my $type = $scfg->{type};
755
756 next if !storage_check_enabled($cfg, $sid, undef, 1);
757
758 next if $tt eq 'iso' && !$scfg->{content}->{iso};
759 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
760 next if $tt eq 'backup' && !$scfg->{content}->{backup};
761
762 activate_storage($cfg, $sid);
763
764 if ($scfg->{path}) {
765 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
766
767 my $path = $plugin->get_subdir($scfg, $tt);
768
769 foreach my $fn (<$path/*>) {
770
771 my $info;
772
773 if ($tt eq 'iso') {
774 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
775
776 $info = { volid => "$sid:iso/$1", format => 'iso' };
777
778 } elsif ($tt eq 'vztmpl') {
779 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
780
781 $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
782
783 } elsif ($tt eq 'backup') {
784 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
785
786 $info = { volid => "$sid:backup/$1", format => $2 };
787 }
788
789 $info->{size} = -s $fn;
790
791 push @{$res->{$sid}}, $info;
792 }
793
794 }
795
796 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
797 }
798
799 return $res;
800 }
801
802
803 sub vdisk_list {
804 my ($cfg, $storeid, $vmid, $vollist) = @_;
805
806 my $ids = $cfg->{ids};
807
808 storage_check_enabled($cfg, $storeid) if ($storeid);
809
810 my $res = {};
811
812 # prepare/activate/refresh all storages
813
814 my $storage_list = [];
815 if ($vollist) {
816 foreach my $volid (@$vollist) {
817 my ($sid, undef) = parse_volume_id($volid);
818 next if !defined($ids->{$sid});
819 next if !storage_check_enabled($cfg, $sid, undef, 1);
820 push @$storage_list, $sid;
821 }
822 } else {
823 foreach my $sid (keys %$ids) {
824 next if $storeid && $storeid ne $sid;
825 next if !storage_check_enabled($cfg, $sid, undef, 1);
826 push @$storage_list, $sid;
827 }
828 }
829
830 my $cache = {};
831
832 activate_storage_list($cfg, $storage_list, $cache);
833
834 foreach my $sid (keys %$ids) {
835 next if $storeid && $storeid ne $sid;
836 next if !storage_check_enabled($cfg, $sid, undef, 1);
837
838 my $scfg = $ids->{$sid};
839 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
840 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
841 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
842 }
843
844 return $res;
845 }
846
847 sub volume_list {
848 my ($cfg, $storeid, $vmid, $content) = @_;
849
850 my @ctypes = qw(images vztmpl iso backup);
851
852 my $cts = $content ? [ $content ] : [ @ctypes ];
853
854 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
855
856 my $res = [];
857 foreach my $ct (@$cts) {
858 my $data;
859 if ($ct eq 'images') {
860 $data = vdisk_list($cfg, $storeid, $vmid);
861 } elsif ($ct eq 'iso' && !defined($vmid)) {
862 $data = template_list($cfg, $storeid, 'iso');
863 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
864 $data = template_list ($cfg, $storeid, 'vztmpl');
865 } elsif ($ct eq 'backup') {
866 $data = template_list ($cfg, $storeid, 'backup');
867 foreach my $item (@{$data->{$storeid}}) {
868 if (defined($vmid)) {
869 @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
870 }
871 }
872 }
873
874 next if !$data || !$data->{$storeid};
875
876 foreach my $item (@{$data->{$storeid}}) {
877 $item->{content} = $ct;
878 push @$res, $item;
879 }
880 }
881
882 return $res;
883 }
884
885 sub uevent_seqnum {
886
887 my $filename = "/sys/kernel/uevent_seqnum";
888
889 my $seqnum = 0;
890 if (my $fh = IO::File->new($filename, "r")) {
891 my $line = <$fh>;
892 if ($line =~ m/^(\d+)$/) {
893 $seqnum = int($1);
894 }
895 close ($fh);
896 }
897 return $seqnum;
898 }
899
900 sub activate_storage {
901 my ($cfg, $storeid, $cache) = @_;
902
903 $cache = {} if !$cache;
904
905 my $scfg = storage_check_enabled($cfg, $storeid);
906
907 return if $cache->{activated}->{$storeid};
908
909 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
910
911 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
912
913 if ($scfg->{base}) {
914 my ($baseid, undef) = parse_volume_id ($scfg->{base});
915 activate_storage($cfg, $baseid, $cache);
916 }
917
918 if (!$plugin->check_connection($storeid, $scfg)) {
919 die "storage '$storeid' is not online\n";
920 }
921
922 $plugin->activate_storage($storeid, $scfg, $cache);
923
924 my $newseq = uevent_seqnum ();
925
926 # only call udevsettle if there are events
927 if ($newseq > $cache->{uevent_seqnum}) {
928 my $timeout = 30;
929 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
930 $cache->{uevent_seqnum} = $newseq;
931 }
932
933 $cache->{activated}->{$storeid} = 1;
934 }
935
936 sub activate_storage_list {
937 my ($cfg, $storeid_list, $cache) = @_;
938
939 $cache = {} if !$cache;
940
941 foreach my $storeid (@$storeid_list) {
942 activate_storage($cfg, $storeid, $cache);
943 }
944 }
945
946 sub deactivate_storage {
947 my ($cfg, $storeid) = @_;
948
949 my $scfg = storage_config ($cfg, $storeid);
950 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
951
952 my $cache = {};
953 $plugin->deactivate_storage($storeid, $scfg, $cache);
954 }
955
956 sub activate_volumes {
957 my ($cfg, $vollist, $snapname) = @_;
958
959 return if !($vollist && scalar(@$vollist));
960
961 my $storagehash = {};
962 foreach my $volid (@$vollist) {
963 my ($storeid, undef) = parse_volume_id($volid);
964 $storagehash->{$storeid} = 1;
965 }
966
967 my $cache = {};
968
969 activate_storage_list($cfg, [keys %$storagehash], $cache);
970
971 foreach my $volid (@$vollist) {
972 my ($storeid, $volname) = parse_volume_id($volid);
973 my $scfg = storage_config($cfg, $storeid);
974 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
975 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
976 }
977 }
978
979 sub deactivate_volumes {
980 my ($cfg, $vollist, $snapname) = @_;
981
982 return if !($vollist && scalar(@$vollist));
983
984 my $cache = {};
985
986 my @errlist = ();
987 foreach my $volid (@$vollist) {
988 my ($storeid, $volname) = parse_volume_id($volid);
989
990 my $scfg = storage_config($cfg, $storeid);
991 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
992
993 eval {
994 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
995 };
996 if (my $err = $@) {
997 warn $err;
998 push @errlist, $volid;
999 }
1000 }
1001
1002 die "volume deactivation failed: " . join(' ', @errlist)
1003 if scalar(@errlist);
1004 }
1005
1006 sub storage_info {
1007 my ($cfg, $content) = @_;
1008
1009 my $ids = $cfg->{ids};
1010
1011 my $info = {};
1012
1013 my @ctypes = PVE::Tools::split_list($content);
1014
1015 my $slist = [];
1016 foreach my $storeid (keys %$ids) {
1017
1018 next if !storage_check_enabled($cfg, $storeid, undef, 1);
1019
1020 if (defined($content)) {
1021 my $want_ctype = 0;
1022 foreach my $ctype (@ctypes) {
1023 if ($ids->{$storeid}->{content}->{$ctype}) {
1024 $want_ctype = 1;
1025 last;
1026 }
1027 }
1028 next if !$want_ctype;
1029 }
1030
1031 my $type = $ids->{$storeid}->{type};
1032
1033 $info->{$storeid} = {
1034 type => $type,
1035 total => 0,
1036 avail => 0,
1037 used => 0,
1038 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1039 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
1040 active => 0,
1041 };
1042
1043 push @$slist, $storeid;
1044 }
1045
1046 my $cache = {};
1047
1048 foreach my $storeid (keys %$ids) {
1049 my $scfg = $ids->{$storeid};
1050 next if !$info->{$storeid};
1051
1052 eval { activate_storage($cfg, $storeid, $cache); };
1053 if (my $err = $@) {
1054 warn $err;
1055 next;
1056 }
1057
1058 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1059 my ($total, $avail, $used, $active);
1060 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1061 warn $@ if $@;
1062 next if !$active;
1063 $info->{$storeid}->{total} = int($total);
1064 $info->{$storeid}->{avail} = int($avail);
1065 $info->{$storeid}->{used} = int($used);
1066 $info->{$storeid}->{active} = $active;
1067 }
1068
1069 return $info;
1070 }
1071
1072 sub resolv_server {
1073 my ($server) = @_;
1074
1075 my ($packed_ip, $family);
1076 eval {
1077 my @res = PVE::Tools::getaddrinfo_all($server);
1078 $family = $res[0]->{family};
1079 $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
1080 };
1081 if (defined $packed_ip) {
1082 return Socket::inet_ntop($family, $packed_ip);
1083 }
1084 return undef;
1085 }
1086
1087 sub scan_nfs {
1088 my ($server_in) = @_;
1089
1090 my $server;
1091 if (!($server = resolv_server ($server_in))) {
1092 die "unable to resolve address for server '${server_in}'\n";
1093 }
1094
1095 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1096
1097 my $res = {};
1098 run_command($cmd, outfunc => sub {
1099 my $line = shift;
1100
1101 # note: howto handle white spaces in export path??
1102 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1103 $res->{$1} = $2;
1104 }
1105 });
1106
1107 return $res;
1108 }
1109
1110 sub scan_zfs {
1111
1112 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1113
1114 my $res = [];
1115 run_command($cmd, outfunc => sub {
1116 my $line = shift;
1117
1118 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1119 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1120 my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str);
1121 my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str);
1122 # ignore subvolumes generated by our ZFSPoolPlugin
1123 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1124 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1125 push @$res, { pool => $pool, size => $size, free => $size-$used };
1126 }
1127 });
1128
1129 return $res;
1130 }
1131
1132 sub resolv_portal {
1133 my ($portal, $noerr) = @_;
1134
1135 my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
1136 if ($server) {
1137 if (my $ip = resolv_server($server)) {
1138 $server = $ip;
1139 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1140 return $port ? "$server:$port" : $server;
1141 }
1142 }
1143 return undef if $noerr;
1144
1145 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
1146 }
1147
1148 # idea is from usbutils package (/usr/bin/usb-devices) script
1149 sub __scan_usb_device {
1150 my ($res, $devpath, $parent, $level) = @_;
1151
1152 return if ! -d $devpath;
1153 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1154 my $port = $level ? int($1 - 1) : 0;
1155
1156 my $busnum = int(file_read_firstline("$devpath/busnum"));
1157 my $devnum = int(file_read_firstline("$devpath/devnum"));
1158
1159 my $d = {
1160 port => $port,
1161 level => $level,
1162 busnum => $busnum,
1163 devnum => $devnum,
1164 speed => file_read_firstline("$devpath/speed"),
1165 class => hex(file_read_firstline("$devpath/bDeviceClass")),
1166 vendid => file_read_firstline("$devpath/idVendor"),
1167 prodid => file_read_firstline("$devpath/idProduct"),
1168 };
1169
1170 if ($level) {
1171 my $usbpath = $devpath;
1172 $usbpath =~ s|^.*/\d+\-||;
1173 $d->{usbpath} = $usbpath;
1174 }
1175
1176 my $product = file_read_firstline("$devpath/product");
1177 $d->{product} = $product if $product;
1178
1179 my $manu = file_read_firstline("$devpath/manufacturer");
1180 $d->{manufacturer} = $manu if $manu;
1181
1182 my $serial => file_read_firstline("$devpath/serial");
1183 $d->{serial} = $serial if $serial;
1184
1185 push @$res, $d;
1186
1187 foreach my $subdev (<$devpath/$busnum-*>) {
1188 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
1189 __scan_usb_device($res, $subdev, $devnum, $level + 1);
1190 }
1191
1192 };
1193
1194 sub scan_usb {
1195
1196 my $devlist = [];
1197
1198 foreach my $device (</sys/bus/usb/devices/usb*>) {
1199 __scan_usb_device($devlist, $device, 0, 0);
1200 }
1201
1202 return $devlist;
1203 }
1204
1205 sub scan_iscsi {
1206 my ($portal_in) = @_;
1207
1208 my $portal;
1209 if (!($portal = resolv_portal($portal_in))) {
1210 die "unable to parse/resolve portal address '${portal_in}'\n";
1211 }
1212
1213 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
1214 }
1215
1216 sub storage_default_format {
1217 my ($cfg, $storeid) = @_;
1218
1219 my $scfg = storage_config ($cfg, $storeid);
1220
1221 return PVE::Storage::Plugin::default_format($scfg);
1222 }
1223
1224 sub vgroup_is_used {
1225 my ($cfg, $vgname) = @_;
1226
1227 foreach my $storeid (keys %{$cfg->{ids}}) {
1228 my $scfg = storage_config($cfg, $storeid);
1229 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
1230 return 1;
1231 }
1232 }
1233
1234 return undef;
1235 }
1236
1237 sub target_is_used {
1238 my ($cfg, $target) = @_;
1239
1240 foreach my $storeid (keys %{$cfg->{ids}}) {
1241 my $scfg = storage_config($cfg, $storeid);
1242 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
1243 return 1;
1244 }
1245 }
1246
1247 return undef;
1248 }
1249
1250 sub volume_is_used {
1251 my ($cfg, $volid) = @_;
1252
1253 foreach my $storeid (keys %{$cfg->{ids}}) {
1254 my $scfg = storage_config($cfg, $storeid);
1255 if ($scfg->{base} && $scfg->{base} eq $volid) {
1256 return 1;
1257 }
1258 }
1259
1260 return undef;
1261 }
1262
1263 sub storage_is_used {
1264 my ($cfg, $storeid) = @_;
1265
1266 foreach my $sid (keys %{$cfg->{ids}}) {
1267 my $scfg = storage_config($cfg, $sid);
1268 next if !$scfg->{base};
1269 my ($st) = parse_volume_id($scfg->{base});
1270 return 1 if $st && $st eq $storeid;
1271 }
1272
1273 return undef;
1274 }
1275
1276 sub foreach_volid {
1277 my ($list, $func) = @_;
1278
1279 return if !$list;
1280
1281 foreach my $sid (keys %$list) {
1282 foreach my $info (@{$list->{$sid}}) {
1283 my $volid = $info->{volid};
1284 my ($sid1, $volname) = parse_volume_id($volid, 1);
1285 if ($sid1 && $sid1 eq $sid) {
1286 &$func ($volid, $sid, $info);
1287 } else {
1288 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1289 }
1290 }
1291 }
1292 }
1293
1294 sub extract_vzdump_config_tar {
1295 my ($archive, $conf_re) = @_;
1296
1297 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
1298
1299 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1300 die "unable to open file '$archive'\n";
1301
1302 my $file;
1303 while (defined($file = <$fh>)) {
1304 if ($file =~ m!$conf_re!) {
1305 $file = $1; # untaint
1306 last;
1307 }
1308 }
1309
1310 kill 15, $pid;
1311 waitpid $pid, 0;
1312 close $fh;
1313
1314 die "ERROR: archive contains no configuration file\n" if !$file;
1315 chomp $file;
1316
1317 my $raw = '';
1318 my $out = sub {
1319 my $output = shift;
1320 $raw .= "$output\n";
1321 };
1322
1323 PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out);
1324
1325 return wantarray ? ($raw, $file) : $raw;
1326 }
1327
1328 sub extract_vzdump_config_vma {
1329 my ($archive, $comp) = @_;
1330
1331 my $cmd;
1332 my $raw = '';
1333 my $out = sub {
1334 my $output = shift;
1335 $raw .= "$output\n";
1336 };
1337
1338
1339 if ($comp) {
1340 my $uncomp;
1341 if ($comp eq 'gz') {
1342 $uncomp = ["zcat", $archive];
1343 } elsif ($comp eq 'lzo') {
1344 $uncomp = ["lzop", "-d", "-c", $archive];
1345 } else {
1346 die "unknown compression method '$comp'\n";
1347 }
1348 $cmd = [$uncomp, ["vma", "config", "-"]];
1349
1350 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1351 # closed early by vma, detect this and ignore the exit code later
1352 my $broken_pipe;
1353 my $errstring;
1354 my $err = sub {
1355 my $output = shift;
1356 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1357 $broken_pipe = 1;
1358 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1359 $errstring = "Failed to extract config from VMA archive: $output\n";
1360 }
1361 };
1362
1363 # in other cases, the pipeline will exit with exit code 141
1364 # because of the broken pipe, handle / ignore this as well
1365 my $rc;
1366 eval {
1367 $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1);
1368 };
1369 my $rerr = $@;
1370
1371 # use exit code if no stderr output and not just broken pipe
1372 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1373 die "$rerr\n" if $rerr;
1374 die "config extraction failed with exit code $rc\n";
1375 }
1376 die "$errstring\n" if $errstring;
1377 } else {
1378 # simple case without compression and weird piping behaviour
1379 PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out);
1380 }
1381
1382 return wantarray ? ($raw, undef) : $raw;
1383 }
1384
1385 sub extract_vzdump_config {
1386 my ($cfg, $volid) = @_;
1387
1388 my $archive = abs_filesystem_path($cfg, $volid);
1389
1390 if ($volid =~ /\/vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1391 return extract_vzdump_config_tar($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1392 } elsif ($volid =~ /\/vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1393 my $format;
1394 my $comp;
1395 if ($7 eq 'tgz') {
1396 $format = 'tar';
1397 $comp = 'gz';
1398 } else {
1399 $format = $9;
1400 $comp = $11 if defined($11);
1401 }
1402
1403 if ($format eq 'tar') {
1404 return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!);
1405 } else {
1406 return extract_vzdump_config_vma($archive, $comp);
1407 }
1408 } else {
1409 die "cannot determine backup guest type for backup archive '$volid'\n";
1410 }
1411 }
1412
1413 # bash completion helper
1414
1415 sub complete_storage {
1416 my ($cmdname, $pname, $cvalue) = @_;
1417
1418 my $cfg = PVE::Storage::config();
1419
1420 return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ];
1421 }
1422
1423 sub complete_storage_enabled {
1424 my ($cmdname, $pname, $cvalue) = @_;
1425
1426 my $res = [];
1427
1428 my $cfg = PVE::Storage::config();
1429 foreach my $sid (keys %{$cfg->{ids}}) {
1430 next if !storage_check_enabled($cfg, $sid, undef, 1);
1431 push @$res, $sid;
1432 }
1433 return $res;
1434 }
1435
1436 sub complete_content_type {
1437 my ($cmdname, $pname, $cvalue) = @_;
1438
1439 return [qw(rootdir images vztmpl iso backup)];
1440 }
1441
1442 sub complete_volume {
1443 my ($cmdname, $pname, $cvalue) = @_;
1444
1445 my $cfg = config();
1446
1447 my $storage_list = complete_storage_enabled();
1448
1449 if ($cvalue =~ m/^([^:]+):/) {
1450 $storage_list = [ $1 ];
1451 } else {
1452 if (scalar(@$storage_list) > 1) {
1453 # only list storage IDs to avoid large listings
1454 my $res = [];
1455 foreach my $storeid (@$storage_list) {
1456 # Hack: simply return 2 artificial values, so that
1457 # completions does not finish
1458 push @$res, "$storeid:volname", "$storeid:...";
1459 }
1460 return $res;
1461 }
1462 }
1463
1464 my $res = [];
1465 foreach my $storeid (@$storage_list) {
1466 my $vollist = PVE::Storage::volume_list($cfg, $storeid);
1467
1468 foreach my $item (@$vollist) {
1469 push @$res, $item->{volid};
1470 }
1471 }
1472
1473 return $res;
1474 }
1475
1476 1;