]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
add comments about LVM thin clones
[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 if ($err = $@){
591 run_command(['zfs', 'destroy', "$zfspath\@__migration__"]);
592 die $err;
593 }
594 run_command($destroy_target);
595
596 } else {
597 die "$errstr - target type $tcfg->{type} is not valid\n";
598 }
599
600 } elsif ($scfg->{type} eq 'lvmthin' || $scfg->{type} eq 'lvm') {
601
602 if (($scfg->{type} eq $tcfg->{type}) &&
603 ($tcfg->{type} eq 'lvmthin' || $tcfg->{type} eq 'lvm')) {
604
605 my (undef, $volname, $vmid) = parse_volname($cfg, $volid);
606 my $size = volume_size_info($cfg, $volid, 5);
607 my $src = path($cfg, $volid);
608 my $dst = path($cfg, $target_volid);
609
610 run_command(['/usr/bin/ssh', "root\@${target_host}",
611 'pvesm', 'alloc', $target_storeid, $vmid,
612 $target_volname, int($size/1024)]);
613
614 eval {
615 if ($tcfg->{type} eq 'lvmthin') {
616 run_command([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
617 "dd", 'conv=sparse', "of=$dst"]]);
618 } else {
619 run_command([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
620 "dd", "of=$dst"]]);
621 }
622 };
623 if (my $err = $@) {
624 run_command(['/usr/bin/ssh', "root\@${target_host}",
625 'pvesm', 'free', $target_volid]);
626 die $err;
627 }
628 } else {
629 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
630 }
631 } else {
632 die "$errstr - source type '$scfg->{type}' not implemented\n";
633 }
634 }
635
636 sub vdisk_clone {
637 my ($cfg, $volid, $vmid, $snap) = @_;
638
639 my ($storeid, $volname) = parse_volume_id($volid);
640
641 my $scfg = storage_config($cfg, $storeid);
642
643 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
644
645 activate_storage($cfg, $storeid);
646
647 # lock shared storage
648 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
649 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
650 return "$storeid:$volname";
651 });
652 }
653
654 sub vdisk_create_base {
655 my ($cfg, $volid) = @_;
656
657 my ($storeid, $volname) = parse_volume_id($volid);
658
659 my $scfg = storage_config($cfg, $storeid);
660
661 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
662
663 activate_storage($cfg, $storeid);
664
665 # lock shared storage
666 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
667 my $volname = $plugin->create_base($storeid, $scfg, $volname);
668 return "$storeid:$volname";
669 });
670 }
671
672 sub vdisk_alloc {
673 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
674
675 die "no storage ID specified\n" if !$storeid;
676
677 PVE::JSONSchema::parse_storage_id($storeid);
678
679 my $scfg = storage_config($cfg, $storeid);
680
681 die "no VMID specified\n" if !$vmid;
682
683 $vmid = parse_vmid($vmid);
684
685 my $defformat = PVE::Storage::Plugin::default_format($scfg);
686
687 $fmt = $defformat if !$fmt;
688
689 activate_storage($cfg, $storeid);
690
691 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
692
693 # lock shared storage
694 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
695 my $old_umask = umask(umask|0037);
696 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
697 my $err = $@;
698 umask $old_umask;
699 die $err if $err;
700 return "$storeid:$volname";
701 });
702 }
703
704 sub vdisk_free {
705 my ($cfg, $volid) = @_;
706
707 my ($storeid, $volname) = parse_volume_id($volid);
708 my $scfg = storage_config($cfg, $storeid);
709 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
710
711 activate_storage($cfg, $storeid);
712
713 my $cleanup_worker;
714
715 # lock shared storage
716 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
717 # LVM-thin allows deletion of still referenced base volumes!
718 die "base volume '$volname' is still in use by linked clones\n"
719 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
720
721 my (undef, undef, undef, undef, undef, $isBase, $format) =
722 $plugin->parse_volname($volname);
723 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
724 });
725
726 return if !$cleanup_worker;
727
728 my $rpcenv = PVE::RPCEnvironment::get();
729 my $authuser = $rpcenv->get_user();
730
731 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
732 }
733
734 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
735 sub template_list {
736 my ($cfg, $storeid, $tt) = @_;
737
738 die "unknown template type '$tt'\n"
739 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
740
741 my $ids = $cfg->{ids};
742
743 storage_check_enabled($cfg, $storeid) if ($storeid);
744
745 my $res = {};
746
747 # query the storage
748
749 foreach my $sid (keys %$ids) {
750 next if $storeid && $storeid ne $sid;
751
752 my $scfg = $ids->{$sid};
753 my $type = $scfg->{type};
754
755 next if !storage_check_enabled($cfg, $sid, undef, 1);
756
757 next if $tt eq 'iso' && !$scfg->{content}->{iso};
758 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
759 next if $tt eq 'backup' && !$scfg->{content}->{backup};
760
761 activate_storage($cfg, $sid);
762
763 if ($scfg->{path}) {
764 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
765
766 my $path = $plugin->get_subdir($scfg, $tt);
767
768 foreach my $fn (<$path/*>) {
769
770 my $info;
771
772 if ($tt eq 'iso') {
773 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
774
775 $info = { volid => "$sid:iso/$1", format => 'iso' };
776
777 } elsif ($tt eq 'vztmpl') {
778 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
779
780 $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
781
782 } elsif ($tt eq 'backup') {
783 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
784
785 $info = { volid => "$sid:backup/$1", format => $2 };
786 }
787
788 $info->{size} = -s $fn;
789
790 push @{$res->{$sid}}, $info;
791 }
792
793 }
794
795 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
796 }
797
798 return $res;
799 }
800
801
802 sub vdisk_list {
803 my ($cfg, $storeid, $vmid, $vollist) = @_;
804
805 my $ids = $cfg->{ids};
806
807 storage_check_enabled($cfg, $storeid) if ($storeid);
808
809 my $res = {};
810
811 # prepare/activate/refresh all storages
812
813 my $storage_list = [];
814 if ($vollist) {
815 foreach my $volid (@$vollist) {
816 my ($sid, undef) = parse_volume_id($volid);
817 next if !defined($ids->{$sid});
818 next if !storage_check_enabled($cfg, $sid, undef, 1);
819 push @$storage_list, $sid;
820 }
821 } else {
822 foreach my $sid (keys %$ids) {
823 next if $storeid && $storeid ne $sid;
824 next if !storage_check_enabled($cfg, $sid, undef, 1);
825 push @$storage_list, $sid;
826 }
827 }
828
829 my $cache = {};
830
831 activate_storage_list($cfg, $storage_list, $cache);
832
833 foreach my $sid (keys %$ids) {
834 next if $storeid && $storeid ne $sid;
835 next if !storage_check_enabled($cfg, $sid, undef, 1);
836
837 my $scfg = $ids->{$sid};
838 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
839 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
840 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
841 }
842
843 return $res;
844 }
845
846 sub volume_list {
847 my ($cfg, $storeid, $vmid, $content) = @_;
848
849 my @ctypes = qw(images vztmpl iso backup);
850
851 my $cts = $content ? [ $content ] : [ @ctypes ];
852
853 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
854
855 my $res = [];
856 foreach my $ct (@$cts) {
857 my $data;
858 if ($ct eq 'images') {
859 $data = vdisk_list($cfg, $storeid, $vmid);
860 } elsif ($ct eq 'iso' && !defined($vmid)) {
861 $data = template_list($cfg, $storeid, 'iso');
862 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
863 $data = template_list ($cfg, $storeid, 'vztmpl');
864 } elsif ($ct eq 'backup') {
865 $data = template_list ($cfg, $storeid, 'backup');
866 foreach my $item (@{$data->{$storeid}}) {
867 if (defined($vmid)) {
868 @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
869 }
870 }
871 }
872
873 next if !$data || !$data->{$storeid};
874
875 foreach my $item (@{$data->{$storeid}}) {
876 $item->{content} = $ct;
877 push @$res, $item;
878 }
879 }
880
881 return $res;
882 }
883
884 sub uevent_seqnum {
885
886 my $filename = "/sys/kernel/uevent_seqnum";
887
888 my $seqnum = 0;
889 if (my $fh = IO::File->new($filename, "r")) {
890 my $line = <$fh>;
891 if ($line =~ m/^(\d+)$/) {
892 $seqnum = int($1);
893 }
894 close ($fh);
895 }
896 return $seqnum;
897 }
898
899 sub activate_storage {
900 my ($cfg, $storeid, $cache) = @_;
901
902 $cache = {} if !$cache;
903
904 my $scfg = storage_check_enabled($cfg, $storeid);
905
906 return if $cache->{activated}->{$storeid};
907
908 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
909
910 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
911
912 if ($scfg->{base}) {
913 my ($baseid, undef) = parse_volume_id ($scfg->{base});
914 activate_storage($cfg, $baseid, $cache);
915 }
916
917 if (!$plugin->check_connection($storeid, $scfg)) {
918 die "storage '$storeid' is not online\n";
919 }
920
921 $plugin->activate_storage($storeid, $scfg, $cache);
922
923 my $newseq = uevent_seqnum ();
924
925 # only call udevsettle if there are events
926 if ($newseq > $cache->{uevent_seqnum}) {
927 my $timeout = 30;
928 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
929 $cache->{uevent_seqnum} = $newseq;
930 }
931
932 $cache->{activated}->{$storeid} = 1;
933 }
934
935 sub activate_storage_list {
936 my ($cfg, $storeid_list, $cache) = @_;
937
938 $cache = {} if !$cache;
939
940 foreach my $storeid (@$storeid_list) {
941 activate_storage($cfg, $storeid, $cache);
942 }
943 }
944
945 sub deactivate_storage {
946 my ($cfg, $storeid) = @_;
947
948 my $scfg = storage_config ($cfg, $storeid);
949 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
950
951 my $cache = {};
952 $plugin->deactivate_storage($storeid, $scfg, $cache);
953 }
954
955 sub activate_volumes {
956 my ($cfg, $vollist, $snapname) = @_;
957
958 return if !($vollist && scalar(@$vollist));
959
960 my $storagehash = {};
961 foreach my $volid (@$vollist) {
962 my ($storeid, undef) = parse_volume_id($volid);
963 $storagehash->{$storeid} = 1;
964 }
965
966 my $cache = {};
967
968 activate_storage_list($cfg, [keys %$storagehash], $cache);
969
970 foreach my $volid (@$vollist) {
971 my ($storeid, $volname) = parse_volume_id($volid);
972 my $scfg = storage_config($cfg, $storeid);
973 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
974 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
975 }
976 }
977
978 sub deactivate_volumes {
979 my ($cfg, $vollist, $snapname) = @_;
980
981 return if !($vollist && scalar(@$vollist));
982
983 my $cache = {};
984
985 my @errlist = ();
986 foreach my $volid (@$vollist) {
987 my ($storeid, $volname) = parse_volume_id($volid);
988
989 my $scfg = storage_config($cfg, $storeid);
990 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
991
992 eval {
993 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
994 };
995 if (my $err = $@) {
996 warn $err;
997 push @errlist, $volid;
998 }
999 }
1000
1001 die "volume deactivation failed: " . join(' ', @errlist)
1002 if scalar(@errlist);
1003 }
1004
1005 sub storage_info {
1006 my ($cfg, $content) = @_;
1007
1008 my $ids = $cfg->{ids};
1009
1010 my $info = {};
1011
1012 my @ctypes = PVE::Tools::split_list($content);
1013
1014 my $slist = [];
1015 foreach my $storeid (keys %$ids) {
1016
1017 next if !storage_check_enabled($cfg, $storeid, undef, 1);
1018
1019 if (defined($content)) {
1020 my $want_ctype = 0;
1021 foreach my $ctype (@ctypes) {
1022 if ($ids->{$storeid}->{content}->{$ctype}) {
1023 $want_ctype = 1;
1024 last;
1025 }
1026 }
1027 next if !$want_ctype;
1028 }
1029
1030 my $type = $ids->{$storeid}->{type};
1031
1032 $info->{$storeid} = {
1033 type => $type,
1034 total => 0,
1035 avail => 0,
1036 used => 0,
1037 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1038 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
1039 active => 0,
1040 };
1041
1042 push @$slist, $storeid;
1043 }
1044
1045 my $cache = {};
1046
1047 foreach my $storeid (keys %$ids) {
1048 my $scfg = $ids->{$storeid};
1049 next if !$info->{$storeid};
1050
1051 eval { activate_storage($cfg, $storeid, $cache); };
1052 if (my $err = $@) {
1053 warn $err;
1054 next;
1055 }
1056
1057 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1058 my ($total, $avail, $used, $active);
1059 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1060 warn $@ if $@;
1061 next if !$active;
1062 $info->{$storeid}->{total} = $total;
1063 $info->{$storeid}->{avail} = $avail;
1064 $info->{$storeid}->{used} = $used;
1065 $info->{$storeid}->{active} = $active;
1066 }
1067
1068 return $info;
1069 }
1070
1071 sub resolv_server {
1072 my ($server) = @_;
1073
1074 my ($packed_ip, $family);
1075 eval {
1076 my @res = PVE::Tools::getaddrinfo_all($server);
1077 $family = $res[0]->{family};
1078 $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
1079 };
1080 if (defined $packed_ip) {
1081 return Socket::inet_ntop($family, $packed_ip);
1082 }
1083 return undef;
1084 }
1085
1086 sub scan_nfs {
1087 my ($server_in) = @_;
1088
1089 my $server;
1090 if (!($server = resolv_server ($server_in))) {
1091 die "unable to resolve address for server '${server_in}'\n";
1092 }
1093
1094 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1095
1096 my $res = {};
1097 run_command($cmd, outfunc => sub {
1098 my $line = shift;
1099
1100 # note: howto handle white spaces in export path??
1101 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1102 $res->{$1} = $2;
1103 }
1104 });
1105
1106 return $res;
1107 }
1108
1109 sub scan_zfs {
1110
1111 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1112
1113 my $res = [];
1114 run_command($cmd, outfunc => sub {
1115 my $line = shift;
1116
1117 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1118 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1119 my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str);
1120 my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str);
1121 # ignore subvolumes generated by our ZFSPoolPlugin
1122 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1123 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1124 push @$res, { pool => $pool, size => $size, free => $size-$used };
1125 }
1126 });
1127
1128 return $res;
1129 }
1130
1131 sub resolv_portal {
1132 my ($portal, $noerr) = @_;
1133
1134 my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
1135 if ($server) {
1136 if (my $ip = resolv_server($server)) {
1137 $server = $ip;
1138 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1139 return $port ? "$server:$port" : $server;
1140 }
1141 }
1142 return undef if $noerr;
1143
1144 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
1145 }
1146
1147 # idea is from usbutils package (/usr/bin/usb-devices) script
1148 sub __scan_usb_device {
1149 my ($res, $devpath, $parent, $level) = @_;
1150
1151 return if ! -d $devpath;
1152 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1153 my $port = $level ? int($1 - 1) : 0;
1154
1155 my $busnum = int(file_read_firstline("$devpath/busnum"));
1156 my $devnum = int(file_read_firstline("$devpath/devnum"));
1157
1158 my $d = {
1159 port => $port,
1160 level => $level,
1161 busnum => $busnum,
1162 devnum => $devnum,
1163 speed => file_read_firstline("$devpath/speed"),
1164 class => hex(file_read_firstline("$devpath/bDeviceClass")),
1165 vendid => file_read_firstline("$devpath/idVendor"),
1166 prodid => file_read_firstline("$devpath/idProduct"),
1167 };
1168
1169 if ($level) {
1170 my $usbpath = $devpath;
1171 $usbpath =~ s|^.*/\d+\-||;
1172 $d->{usbpath} = $usbpath;
1173 }
1174
1175 my $product = file_read_firstline("$devpath/product");
1176 $d->{product} = $product if $product;
1177
1178 my $manu = file_read_firstline("$devpath/manufacturer");
1179 $d->{manufacturer} = $manu if $manu;
1180
1181 my $serial => file_read_firstline("$devpath/serial");
1182 $d->{serial} = $serial if $serial;
1183
1184 push @$res, $d;
1185
1186 foreach my $subdev (<$devpath/$busnum-*>) {
1187 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
1188 __scan_usb_device($res, $subdev, $devnum, $level + 1);
1189 }
1190
1191 };
1192
1193 sub scan_usb {
1194
1195 my $devlist = [];
1196
1197 foreach my $device (</sys/bus/usb/devices/usb*>) {
1198 __scan_usb_device($devlist, $device, 0, 0);
1199 }
1200
1201 return $devlist;
1202 }
1203
1204 sub scan_iscsi {
1205 my ($portal_in) = @_;
1206
1207 my $portal;
1208 if (!($portal = resolv_portal($portal_in))) {
1209 die "unable to parse/resolve portal address '${portal_in}'\n";
1210 }
1211
1212 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
1213 }
1214
1215 sub storage_default_format {
1216 my ($cfg, $storeid) = @_;
1217
1218 my $scfg = storage_config ($cfg, $storeid);
1219
1220 return PVE::Storage::Plugin::default_format($scfg);
1221 }
1222
1223 sub vgroup_is_used {
1224 my ($cfg, $vgname) = @_;
1225
1226 foreach my $storeid (keys %{$cfg->{ids}}) {
1227 my $scfg = storage_config($cfg, $storeid);
1228 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
1229 return 1;
1230 }
1231 }
1232
1233 return undef;
1234 }
1235
1236 sub target_is_used {
1237 my ($cfg, $target) = @_;
1238
1239 foreach my $storeid (keys %{$cfg->{ids}}) {
1240 my $scfg = storage_config($cfg, $storeid);
1241 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
1242 return 1;
1243 }
1244 }
1245
1246 return undef;
1247 }
1248
1249 sub volume_is_used {
1250 my ($cfg, $volid) = @_;
1251
1252 foreach my $storeid (keys %{$cfg->{ids}}) {
1253 my $scfg = storage_config($cfg, $storeid);
1254 if ($scfg->{base} && $scfg->{base} eq $volid) {
1255 return 1;
1256 }
1257 }
1258
1259 return undef;
1260 }
1261
1262 sub storage_is_used {
1263 my ($cfg, $storeid) = @_;
1264
1265 foreach my $sid (keys %{$cfg->{ids}}) {
1266 my $scfg = storage_config($cfg, $sid);
1267 next if !$scfg->{base};
1268 my ($st) = parse_volume_id($scfg->{base});
1269 return 1 if $st && $st eq $storeid;
1270 }
1271
1272 return undef;
1273 }
1274
1275 sub foreach_volid {
1276 my ($list, $func) = @_;
1277
1278 return if !$list;
1279
1280 foreach my $sid (keys %$list) {
1281 foreach my $info (@{$list->{$sid}}) {
1282 my $volid = $info->{volid};
1283 my ($sid1, $volname) = parse_volume_id($volid, 1);
1284 if ($sid1 && $sid1 eq $sid) {
1285 &$func ($volid, $sid, $info);
1286 } else {
1287 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1288 }
1289 }
1290 }
1291 }
1292
1293 sub extract_vzdump_config_tar {
1294 my ($archive, $conf_re) = @_;
1295
1296 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
1297
1298 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1299 die "unable to open file '$archive'\n";
1300
1301 my $file;
1302 while (defined($file = <$fh>)) {
1303 if ($file =~ m!$conf_re!) {
1304 $file = $1; # untaint
1305 last;
1306 }
1307 }
1308
1309 kill 15, $pid;
1310 waitpid $pid, 0;
1311 close $fh;
1312
1313 die "ERROR: archive contains no configuration file\n" if !$file;
1314 chomp $file;
1315
1316 my $raw = '';
1317 my $out = sub {
1318 my $output = shift;
1319 $raw .= "$output\n";
1320 };
1321
1322 PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out);
1323
1324 return wantarray ? ($raw, $file) : $raw;
1325 }
1326
1327 sub extract_vzdump_config_vma {
1328 my ($archive, $comp) = @_;
1329
1330 my $cmd;
1331 my $raw = '';
1332 my $out = sub {
1333 my $output = shift;
1334 $raw .= "$output\n";
1335 };
1336
1337
1338 if ($comp) {
1339 my $uncomp;
1340 if ($comp eq 'gz') {
1341 $uncomp = ["zcat", $archive];
1342 } elsif ($comp eq 'lzo') {
1343 $uncomp = ["lzop", "-d", "-c", $archive];
1344 } else {
1345 die "unknown compression method '$comp'\n";
1346 }
1347 $cmd = [$uncomp, ["vma", "config", "-"]];
1348
1349 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1350 # closed early by vma, detect this and ignore the exit code later
1351 my $broken_pipe;
1352 my $errstring;
1353 my $err = sub {
1354 my $output = shift;
1355 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1356 $broken_pipe = 1;
1357 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1358 $errstring = "Failed to extract config from VMA archive: $output\n";
1359 }
1360 };
1361
1362 # in other cases, the pipeline will exit with exit code 141
1363 # because of the broken pipe, handle / ignore this as well
1364 my $rc;
1365 eval {
1366 $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1);
1367 };
1368 my $rerr = $@;
1369
1370 # use exit code if no stderr output and not just broken pipe
1371 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1372 die "$rerr\n" if $rerr;
1373 die "config extraction failed with exit code $rc\n";
1374 }
1375 die "$errstring\n" if $errstring;
1376 } else {
1377 # simple case without compression and weird piping behaviour
1378 PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out);
1379 }
1380
1381 return wantarray ? ($raw, undef) : $raw;
1382 }
1383
1384 sub extract_vzdump_config {
1385 my ($cfg, $volid) = @_;
1386
1387 my $archive = abs_filesystem_path($cfg, $volid);
1388
1389 if ($volid =~ /\/vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1390 return extract_vzdump_config_tar($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1391 } elsif ($volid =~ /\/vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1392 my $format;
1393 my $comp;
1394 if ($7 eq 'tgz') {
1395 $format = 'tar';
1396 $comp = 'gz';
1397 } else {
1398 $format = $9;
1399 $comp = $11 if defined($11);
1400 }
1401
1402 if ($format eq 'tar') {
1403 return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!);
1404 } else {
1405 return extract_vzdump_config_vma($archive, $comp);
1406 }
1407 } else {
1408 die "cannot determine backup guest type for backup archive '$volid'\n";
1409 }
1410 }
1411
1412 # bash completion helper
1413
1414 sub complete_storage {
1415 my ($cmdname, $pname, $cvalue) = @_;
1416
1417 my $cfg = PVE::Storage::config();
1418
1419 return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ];
1420 }
1421
1422 sub complete_storage_enabled {
1423 my ($cmdname, $pname, $cvalue) = @_;
1424
1425 my $res = [];
1426
1427 my $cfg = PVE::Storage::config();
1428 foreach my $sid (keys %{$cfg->{ids}}) {
1429 next if !storage_check_enabled($cfg, $sid, undef, 1);
1430 push @$res, $sid;
1431 }
1432 return $res;
1433 }
1434
1435 sub complete_content_type {
1436 my ($cmdname, $pname, $cvalue) = @_;
1437
1438 return [qw(rootdir images vztmpl iso backup)];
1439 }
1440
1441 sub complete_volume {
1442 my ($cmdname, $pname, $cvalue) = @_;
1443
1444 my $cfg = config();
1445
1446 my $storage_list = complete_storage_enabled();
1447
1448 if ($cvalue =~ m/^([^:]+):/) {
1449 $storage_list = [ $1 ];
1450 } else {
1451 if (scalar(@$storage_list) > 1) {
1452 # only list storage IDs to avoid large listings
1453 my $res = [];
1454 foreach my $storeid (@$storage_list) {
1455 # Hack: simply return 2 artificial values, so that
1456 # completions does not finish
1457 push @$res, "$storeid:volname", "$storeid:...";
1458 }
1459 return $res;
1460 }
1461 }
1462
1463 my $res = [];
1464 foreach my $storeid (@$storage_list) {
1465 my $vollist = PVE::Storage::volume_list($cfg, $storeid);
1466
1467 foreach my $item (@$vollist) {
1468 push @$res, $item->{volid};
1469 }
1470 }
1471
1472 return $res;
1473 }
1474
1475 1;