]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
fix error message
[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 sub parse_volname {
325 my ($cfg, $volid) = @_;
326
327 my ($storeid, $volname) = parse_volume_id($volid);
328
329 my $scfg = storage_config($cfg, $storeid);
330
331 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
332
333 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
334
335 return $plugin->parse_volname($volname);
336 }
337
338 sub parse_volume_id {
339 my ($volid, $noerr) = @_;
340
341 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
342 }
343
344 sub volume_is_base {
345 my ($cfg, $volid) = @_;
346
347 my ($sid, $volname) = parse_volume_id($volid, 1);
348 return 0 if !$sid;
349
350 if (my $scfg = $cfg->{ids}->{$sid}) {
351 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
352 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
353 $plugin->parse_volname($volname);
354 return $isBase ? 1 : 0;
355 } else {
356 # stale volid with undefined storage - so we can just guess
357 if ($volid =~ m/base-/) {
358 return 1;
359 }
360 }
361
362 return 0;
363 }
364
365 # try to map a filesystem path to a volume identifier
366 sub path_to_volume_id {
367 my ($cfg, $path) = @_;
368
369 my $ids = $cfg->{ids};
370
371 my ($sid, $volname) = parse_volume_id($path, 1);
372 if ($sid) {
373 if (my $scfg = $ids->{$sid}) {
374 if ($scfg->{path}) {
375 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
376 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
377 return ($vtype, $path);
378 }
379 }
380 return ('');
381 }
382
383 # Note: abs_path() return undef if $path doesn not exist
384 # for example when nfs storage is not mounted
385 $path = abs_path($path) || $path;
386
387 foreach my $sid (keys %$ids) {
388 my $scfg = $ids->{$sid};
389 next if !$scfg->{path};
390 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
391 my $imagedir = $plugin->get_subdir($scfg, 'images');
392 my $isodir = $plugin->get_subdir($scfg, 'iso');
393 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
394 my $backupdir = $plugin->get_subdir($scfg, 'backup');
395 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
396
397 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
398 my $vmid = $1;
399 my $name = $2;
400
401 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
402 foreach my $info (@$vollist) {
403 my ($storeid, $volname) = parse_volume_id($info->{volid});
404 my $volpath = $plugin->path($scfg, $volname, $storeid);
405 if ($volpath eq $path) {
406 return ('images', $info->{volid});
407 }
408 }
409 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
410 my $name = $1;
411 return ('iso', "$sid:iso/$name");
412 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
413 my $name = $1;
414 return ('vztmpl', "$sid:vztmpl/$name");
415 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
416 my $vmid = $1;
417 return ('rootdir', "$sid:rootdir/$vmid");
418 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
419 my $name = $1;
420 return ('iso', "$sid:backup/$name");
421 }
422 }
423
424 # can't map path to volume id
425 return ('');
426 }
427
428 sub path {
429 my ($cfg, $volid, $snapname) = @_;
430
431 my ($storeid, $volname) = parse_volume_id($volid);
432
433 my $scfg = storage_config($cfg, $storeid);
434
435 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
436 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
437 return wantarray ? ($path, $owner, $vtype) : $path;
438 }
439
440 sub abs_filesystem_path {
441 my ($cfg, $volid) = @_;
442
443 my $path;
444 if (PVE::Storage::parse_volume_id ($volid, 1)) {
445 PVE::Storage::activate_volumes($cfg, [ $volid ]);
446 $path = PVE::Storage::path($cfg, $volid);
447 } else {
448 if (-f $volid) {
449 my $abspath = abs_path($volid);
450 if ($abspath && $abspath =~ m|^(/.+)$|) {
451 $path = $1; # untaint any path
452 }
453 }
454 }
455
456 die "can't find file '$volid'\n" if !($path && -f $path);
457
458 return $path;
459 }
460
461 sub storage_migrate {
462 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
463
464 my ($storeid, $volname) = parse_volume_id($volid);
465 $target_volname = $volname if !$target_volname;
466
467 my $scfg = storage_config($cfg, $storeid);
468
469 # no need to migrate shared content
470 return if $storeid eq $target_storeid && $scfg->{shared};
471
472 my $tcfg = storage_config($cfg, $target_storeid);
473
474 my $target_volid = "${target_storeid}:${target_volname}";
475
476 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
477
478 my $sshoptions = "-o 'BatchMode=yes'";
479 my $ssh = "/usr/bin/ssh $sshoptions";
480
481 local $ENV{RSYNC_RSH} = $ssh;
482
483 # only implemented for file system based storage
484 if ($scfg->{path}) {
485 if ($tcfg->{path}) {
486
487 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
488 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
489 my $src = $src_plugin->path($scfg, $volname, $storeid);
490 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
491
492 my $dirname = dirname($dst);
493
494 if ($tcfg->{shared}) { # we can do a local copy
495
496 run_command(['/bin/mkdir', '-p', $dirname]);
497
498 run_command(['/bin/cp', $src, $dst]);
499
500 } else {
501 run_command(['/usr/bin/ssh', "root\@${target_host}",
502 '/bin/mkdir', '-p', $dirname]);
503
504 # we use rsync with --sparse, so we can't use --inplace,
505 # so we remove file on the target if it already exists to
506 # save space
507 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
508 if ($format && ($format eq 'raw') && $size) {
509 run_command(['/usr/bin/ssh', "root\@${target_host}",
510 'rm', '-f', $dst],
511 outfunc => sub {});
512 }
513
514 my $cmd;
515 if ($format eq 'subvol') {
516 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
517 '-aH', '--delete', '--no-whole-file', '--inplace',
518 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
519 } else {
520 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
521 $src, "[root\@${target_host}]:$dst"];
522 }
523
524 my $percent = -1;
525
526 run_command($cmd, outfunc => sub {
527 my $line = shift;
528
529 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
530 if ($2 > $percent) {
531 $percent = $2;
532 print "rsync status: $1\n";
533 *STDOUT->flush();
534 }
535 } else {
536 print "$line\n";
537 *STDOUT->flush();
538 }
539 });
540 }
541 } else {
542 die "$errstr - target type '$tcfg->{type}' not implemented\n";
543 }
544
545 } elsif ($scfg->{type} eq 'zfspool') {
546
547 if ($tcfg->{type} eq 'zfspool') {
548
549 die "$errstr - pool on target does not have the same name as on source!"
550 if $tcfg->{pool} ne $scfg->{pool};
551
552 my (undef, $volname) = parse_volname($cfg, $volid);
553
554 my $zfspath = "$scfg->{pool}\/$volname";
555
556 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
557
558 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
559 'zfs', 'recv', $zfspath]];
560
561 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
562 run_command($snap);
563 eval{
564 run_command($send);
565 };
566 my $err;
567 if ($err = $@){
568 run_command(['zfs', 'destroy', "$zfspath\@__migration__"]);
569 die $err;
570 }
571 run_command($destroy_target);
572
573 } else {
574 die "$errstr - target type $tcfg->{type} is not valid\n";
575 }
576
577 } elsif ($scfg->{type} eq 'lvmthin' || $scfg->{type} eq 'lvm') {
578
579 if (($scfg->{type} eq $tcfg->{type}) &&
580 ($tcfg->{type} eq 'lvmthin' || $tcfg->{type} eq 'lvm')) {
581
582 my (undef, $volname, $vmid) = parse_volname($cfg, $volid);
583 my $size = volume_size_info($cfg, $volid, 5);
584 my $src = path($cfg, $volid);
585 my $dst = path($cfg, $target_volid);
586
587 run_command(['/usr/bin/ssh', "root\@${target_host}",
588 'pvesm', 'alloc', $target_storeid, $vmid,
589 $target_volname, int($size/1024)]);
590
591 eval {
592 if ($tcfg->{type} eq 'lvmthin') {
593 run_command([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
594 "dd", 'conv=sparse', "of=$dst"]]);
595 } else {
596 run_command([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
597 "dd", "of=$dst"]]);
598 }
599 };
600 if (my $err = $@) {
601 run_command(['/usr/bin/ssh', "root\@${target_host}",
602 'pvesm', 'free', $target_volid]);
603 die $err;
604 }
605 } else {
606 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
607 }
608 } else {
609 die "$errstr - source type '$scfg->{type}' not implemented\n";
610 }
611 }
612
613 sub vdisk_clone {
614 my ($cfg, $volid, $vmid, $snap) = @_;
615
616 my ($storeid, $volname) = parse_volume_id($volid);
617
618 my $scfg = storage_config($cfg, $storeid);
619
620 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
621
622 activate_storage($cfg, $storeid);
623
624 # lock shared storage
625 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
626 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
627 return "$storeid:$volname";
628 });
629 }
630
631 sub vdisk_create_base {
632 my ($cfg, $volid) = @_;
633
634 my ($storeid, $volname) = parse_volume_id($volid);
635
636 my $scfg = storage_config($cfg, $storeid);
637
638 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
639
640 activate_storage($cfg, $storeid);
641
642 # lock shared storage
643 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
644 my $volname = $plugin->create_base($storeid, $scfg, $volname);
645 return "$storeid:$volname";
646 });
647 }
648
649 sub vdisk_alloc {
650 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
651
652 die "no storage ID specified\n" if !$storeid;
653
654 PVE::JSONSchema::parse_storage_id($storeid);
655
656 my $scfg = storage_config($cfg, $storeid);
657
658 die "no VMID specified\n" if !$vmid;
659
660 $vmid = parse_vmid($vmid);
661
662 my $defformat = PVE::Storage::Plugin::default_format($scfg);
663
664 $fmt = $defformat if !$fmt;
665
666 activate_storage($cfg, $storeid);
667
668 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
669
670 # lock shared storage
671 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
672 my $old_umask = umask(umask|0037);
673 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
674 my $err = $@;
675 umask $old_umask;
676 die $err if $err;
677 return "$storeid:$volname";
678 });
679 }
680
681 sub vdisk_free {
682 my ($cfg, $volid) = @_;
683
684 my ($storeid, $volname) = parse_volume_id($volid);
685
686 my $scfg = storage_config($cfg, $storeid);
687
688 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
689
690 activate_storage($cfg, $storeid);
691
692 my $cleanup_worker;
693
694 # lock shared storage
695 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
696
697 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
698 $plugin->parse_volname($volname);
699 if ($isBase) {
700 my $vollist = $plugin->list_images($storeid, $scfg);
701 foreach my $info (@$vollist) {
702 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
703 my $basename = undef;
704 my $basevmid = undef;
705
706 eval{
707 (undef, undef, undef, $basename, $basevmid) =
708 $plugin->parse_volname($tmpvolname);
709 };
710
711 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
712 die "base volume '$volname' is still in use " .
713 "(used by '$tmpvolname')\n";
714 }
715 }
716 }
717 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
718 });
719
720 return if !$cleanup_worker;
721
722 my $rpcenv = PVE::RPCEnvironment::get();
723 my $authuser = $rpcenv->get_user();
724
725 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
726 }
727
728 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
729 sub template_list {
730 my ($cfg, $storeid, $tt) = @_;
731
732 die "unknown template type '$tt'\n"
733 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
734
735 my $ids = $cfg->{ids};
736
737 storage_check_enabled($cfg, $storeid) if ($storeid);
738
739 my $res = {};
740
741 # query the storage
742
743 foreach my $sid (keys %$ids) {
744 next if $storeid && $storeid ne $sid;
745
746 my $scfg = $ids->{$sid};
747 my $type = $scfg->{type};
748
749 next if !storage_check_enabled($cfg, $sid, undef, 1);
750
751 next if $tt eq 'iso' && !$scfg->{content}->{iso};
752 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
753 next if $tt eq 'backup' && !$scfg->{content}->{backup};
754
755 activate_storage($cfg, $sid);
756
757 if ($scfg->{path}) {
758 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
759
760 my $path = $plugin->get_subdir($scfg, $tt);
761
762 foreach my $fn (<$path/*>) {
763
764 my $info;
765
766 if ($tt eq 'iso') {
767 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
768
769 $info = { volid => "$sid:iso/$1", format => 'iso' };
770
771 } elsif ($tt eq 'vztmpl') {
772 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
773
774 $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
775
776 } elsif ($tt eq 'backup') {
777 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
778
779 $info = { volid => "$sid:backup/$1", format => $2 };
780 }
781
782 $info->{size} = -s $fn;
783
784 push @{$res->{$sid}}, $info;
785 }
786
787 }
788
789 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
790 }
791
792 return $res;
793 }
794
795
796 sub vdisk_list {
797 my ($cfg, $storeid, $vmid, $vollist) = @_;
798
799 my $ids = $cfg->{ids};
800
801 storage_check_enabled($cfg, $storeid) if ($storeid);
802
803 my $res = {};
804
805 # prepare/activate/refresh all storages
806
807 my $storage_list = [];
808 if ($vollist) {
809 foreach my $volid (@$vollist) {
810 my ($sid, undef) = parse_volume_id($volid);
811 next if !defined($ids->{$sid});
812 next if !storage_check_enabled($cfg, $sid, undef, 1);
813 push @$storage_list, $sid;
814 }
815 } else {
816 foreach my $sid (keys %$ids) {
817 next if $storeid && $storeid ne $sid;
818 next if !storage_check_enabled($cfg, $sid, undef, 1);
819 push @$storage_list, $sid;
820 }
821 }
822
823 my $cache = {};
824
825 activate_storage_list($cfg, $storage_list, $cache);
826
827 foreach my $sid (keys %$ids) {
828 next if $storeid && $storeid ne $sid;
829 next if !storage_check_enabled($cfg, $sid, undef, 1);
830
831 my $scfg = $ids->{$sid};
832 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
833 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
834 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
835 }
836
837 return $res;
838 }
839
840 sub volume_list {
841 my ($cfg, $storeid, $vmid, $content) = @_;
842
843 my @ctypes = qw(images vztmpl iso backup);
844
845 my $cts = $content ? [ $content ] : [ @ctypes ];
846
847 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
848
849 my $res = [];
850 foreach my $ct (@$cts) {
851 my $data;
852 if ($ct eq 'images') {
853 $data = vdisk_list($cfg, $storeid, $vmid);
854 } elsif ($ct eq 'iso' && !defined($vmid)) {
855 $data = template_list($cfg, $storeid, 'iso');
856 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
857 $data = template_list ($cfg, $storeid, 'vztmpl');
858 } elsif ($ct eq 'backup') {
859 $data = template_list ($cfg, $storeid, 'backup');
860 foreach my $item (@{$data->{$storeid}}) {
861 if (defined($vmid)) {
862 @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
863 }
864 }
865 }
866
867 next if !$data || !$data->{$storeid};
868
869 foreach my $item (@{$data->{$storeid}}) {
870 $item->{content} = $ct;
871 push @$res, $item;
872 }
873 }
874
875 return $res;
876 }
877
878 sub uevent_seqnum {
879
880 my $filename = "/sys/kernel/uevent_seqnum";
881
882 my $seqnum = 0;
883 if (my $fh = IO::File->new($filename, "r")) {
884 my $line = <$fh>;
885 if ($line =~ m/^(\d+)$/) {
886 $seqnum = int($1);
887 }
888 close ($fh);
889 }
890 return $seqnum;
891 }
892
893 sub activate_storage {
894 my ($cfg, $storeid, $cache) = @_;
895
896 $cache = {} if !$cache;
897
898 my $scfg = storage_check_enabled($cfg, $storeid);
899
900 return if $cache->{activated}->{$storeid};
901
902 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
903
904 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
905
906 if ($scfg->{base}) {
907 my ($baseid, undef) = parse_volume_id ($scfg->{base});
908 activate_storage($cfg, $baseid, $cache);
909 }
910
911 if (!$plugin->check_connection($storeid, $scfg)) {
912 die "storage '$storeid' is not online\n";
913 }
914
915 $plugin->activate_storage($storeid, $scfg, $cache);
916
917 my $newseq = uevent_seqnum ();
918
919 # only call udevsettle if there are events
920 if ($newseq > $cache->{uevent_seqnum}) {
921 my $timeout = 30;
922 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
923 $cache->{uevent_seqnum} = $newseq;
924 }
925
926 $cache->{activated}->{$storeid} = 1;
927 }
928
929 sub activate_storage_list {
930 my ($cfg, $storeid_list, $cache) = @_;
931
932 $cache = {} if !$cache;
933
934 foreach my $storeid (@$storeid_list) {
935 activate_storage($cfg, $storeid, $cache);
936 }
937 }
938
939 sub deactivate_storage {
940 my ($cfg, $storeid) = @_;
941
942 my $scfg = storage_config ($cfg, $storeid);
943 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
944
945 my $cache = {};
946 $plugin->deactivate_storage($storeid, $scfg, $cache);
947 }
948
949 sub activate_volumes {
950 my ($cfg, $vollist, $snapname) = @_;
951
952 return if !($vollist && scalar(@$vollist));
953
954 my $storagehash = {};
955 foreach my $volid (@$vollist) {
956 my ($storeid, undef) = parse_volume_id($volid);
957 $storagehash->{$storeid} = 1;
958 }
959
960 my $cache = {};
961
962 activate_storage_list($cfg, [keys %$storagehash], $cache);
963
964 foreach my $volid (@$vollist) {
965 my ($storeid, $volname) = parse_volume_id($volid);
966 my $scfg = storage_config($cfg, $storeid);
967 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
968 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
969 }
970 }
971
972 sub deactivate_volumes {
973 my ($cfg, $vollist, $snapname) = @_;
974
975 return if !($vollist && scalar(@$vollist));
976
977 my $cache = {};
978
979 my @errlist = ();
980 foreach my $volid (@$vollist) {
981 my ($storeid, $volname) = parse_volume_id($volid);
982
983 my $scfg = storage_config($cfg, $storeid);
984 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
985
986 eval {
987 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
988 };
989 if (my $err = $@) {
990 warn $err;
991 push @errlist, $volid;
992 }
993 }
994
995 die "volume deactivation failed: " . join(' ', @errlist)
996 if scalar(@errlist);
997 }
998
999 sub storage_info {
1000 my ($cfg, $content) = @_;
1001
1002 my $ids = $cfg->{ids};
1003
1004 my $info = {};
1005
1006 my @ctypes = PVE::Tools::split_list($content);
1007
1008 my $slist = [];
1009 foreach my $storeid (keys %$ids) {
1010
1011 next if !storage_check_enabled($cfg, $storeid, undef, 1);
1012
1013 if (defined($content)) {
1014 my $want_ctype = 0;
1015 foreach my $ctype (@ctypes) {
1016 if ($ids->{$storeid}->{content}->{$ctype}) {
1017 $want_ctype = 1;
1018 last;
1019 }
1020 }
1021 next if !$want_ctype;
1022 }
1023
1024 my $type = $ids->{$storeid}->{type};
1025
1026 $info->{$storeid} = {
1027 type => $type,
1028 total => 0,
1029 avail => 0,
1030 used => 0,
1031 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1032 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
1033 active => 0,
1034 };
1035
1036 push @$slist, $storeid;
1037 }
1038
1039 my $cache = {};
1040
1041 foreach my $storeid (keys %$ids) {
1042 my $scfg = $ids->{$storeid};
1043 next if !$info->{$storeid};
1044
1045 eval { activate_storage($cfg, $storeid, $cache); };
1046 if (my $err = $@) {
1047 warn $err;
1048 next;
1049 }
1050
1051 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1052 my ($total, $avail, $used, $active);
1053 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1054 warn $@ if $@;
1055 next if !$active;
1056 $info->{$storeid}->{total} = $total;
1057 $info->{$storeid}->{avail} = $avail;
1058 $info->{$storeid}->{used} = $used;
1059 $info->{$storeid}->{active} = $active;
1060 }
1061
1062 return $info;
1063 }
1064
1065 sub resolv_server {
1066 my ($server) = @_;
1067
1068 my ($packed_ip, $family);
1069 eval {
1070 my @res = PVE::Tools::getaddrinfo_all($server);
1071 $family = $res[0]->{family};
1072 $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
1073 };
1074 if (defined $packed_ip) {
1075 return Socket::inet_ntop($family, $packed_ip);
1076 }
1077 return undef;
1078 }
1079
1080 sub scan_nfs {
1081 my ($server_in) = @_;
1082
1083 my $server;
1084 if (!($server = resolv_server ($server_in))) {
1085 die "unable to resolve address for server '${server_in}'\n";
1086 }
1087
1088 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1089
1090 my $res = {};
1091 run_command($cmd, outfunc => sub {
1092 my $line = shift;
1093
1094 # note: howto handle white spaces in export path??
1095 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1096 $res->{$1} = $2;
1097 }
1098 });
1099
1100 return $res;
1101 }
1102
1103 sub scan_zfs {
1104
1105 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1106
1107 my $res = [];
1108 run_command($cmd, outfunc => sub {
1109 my $line = shift;
1110
1111 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1112 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1113 my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str);
1114 my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str);
1115 # ignore subvolumes generated by our ZFSPoolPlugin
1116 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1117 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1118 push @$res, { pool => $pool, size => $size, free => $size-$used };
1119 }
1120 });
1121
1122 return $res;
1123 }
1124
1125 sub resolv_portal {
1126 my ($portal, $noerr) = @_;
1127
1128 my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
1129 if ($server) {
1130 if (my $ip = resolv_server($server)) {
1131 $server = $ip;
1132 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1133 return $port ? "$server:$port" : $server;
1134 }
1135 }
1136 return undef if $noerr;
1137
1138 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
1139 }
1140
1141 # idea is from usbutils package (/usr/bin/usb-devices) script
1142 sub __scan_usb_device {
1143 my ($res, $devpath, $parent, $level) = @_;
1144
1145 return if ! -d $devpath;
1146 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1147 my $port = $level ? int($1 - 1) : 0;
1148
1149 my $busnum = int(file_read_firstline("$devpath/busnum"));
1150 my $devnum = int(file_read_firstline("$devpath/devnum"));
1151
1152 my $d = {
1153 port => $port,
1154 level => $level,
1155 busnum => $busnum,
1156 devnum => $devnum,
1157 speed => file_read_firstline("$devpath/speed"),
1158 class => hex(file_read_firstline("$devpath/bDeviceClass")),
1159 vendid => file_read_firstline("$devpath/idVendor"),
1160 prodid => file_read_firstline("$devpath/idProduct"),
1161 };
1162
1163 if ($level) {
1164 my $usbpath = $devpath;
1165 $usbpath =~ s|^.*/\d+\-||;
1166 $d->{usbpath} = $usbpath;
1167 }
1168
1169 my $product = file_read_firstline("$devpath/product");
1170 $d->{product} = $product if $product;
1171
1172 my $manu = file_read_firstline("$devpath/manufacturer");
1173 $d->{manufacturer} = $manu if $manu;
1174
1175 my $serial => file_read_firstline("$devpath/serial");
1176 $d->{serial} = $serial if $serial;
1177
1178 push @$res, $d;
1179
1180 foreach my $subdev (<$devpath/$busnum-*>) {
1181 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
1182 __scan_usb_device($res, $subdev, $devnum, $level + 1);
1183 }
1184
1185 };
1186
1187 sub scan_usb {
1188
1189 my $devlist = [];
1190
1191 foreach my $device (</sys/bus/usb/devices/usb*>) {
1192 __scan_usb_device($devlist, $device, 0, 0);
1193 }
1194
1195 return $devlist;
1196 }
1197
1198 sub scan_iscsi {
1199 my ($portal_in) = @_;
1200
1201 my $portal;
1202 if (!($portal = resolv_portal($portal_in))) {
1203 die "unable to parse/resolve portal address '${portal_in}'\n";
1204 }
1205
1206 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
1207 }
1208
1209 sub storage_default_format {
1210 my ($cfg, $storeid) = @_;
1211
1212 my $scfg = storage_config ($cfg, $storeid);
1213
1214 return PVE::Storage::Plugin::default_format($scfg);
1215 }
1216
1217 sub vgroup_is_used {
1218 my ($cfg, $vgname) = @_;
1219
1220 foreach my $storeid (keys %{$cfg->{ids}}) {
1221 my $scfg = storage_config($cfg, $storeid);
1222 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
1223 return 1;
1224 }
1225 }
1226
1227 return undef;
1228 }
1229
1230 sub target_is_used {
1231 my ($cfg, $target) = @_;
1232
1233 foreach my $storeid (keys %{$cfg->{ids}}) {
1234 my $scfg = storage_config($cfg, $storeid);
1235 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
1236 return 1;
1237 }
1238 }
1239
1240 return undef;
1241 }
1242
1243 sub volume_is_used {
1244 my ($cfg, $volid) = @_;
1245
1246 foreach my $storeid (keys %{$cfg->{ids}}) {
1247 my $scfg = storage_config($cfg, $storeid);
1248 if ($scfg->{base} && $scfg->{base} eq $volid) {
1249 return 1;
1250 }
1251 }
1252
1253 return undef;
1254 }
1255
1256 sub storage_is_used {
1257 my ($cfg, $storeid) = @_;
1258
1259 foreach my $sid (keys %{$cfg->{ids}}) {
1260 my $scfg = storage_config($cfg, $sid);
1261 next if !$scfg->{base};
1262 my ($st) = parse_volume_id($scfg->{base});
1263 return 1 if $st && $st eq $storeid;
1264 }
1265
1266 return undef;
1267 }
1268
1269 sub foreach_volid {
1270 my ($list, $func) = @_;
1271
1272 return if !$list;
1273
1274 foreach my $sid (keys %$list) {
1275 foreach my $info (@{$list->{$sid}}) {
1276 my $volid = $info->{volid};
1277 my ($sid1, $volname) = parse_volume_id($volid, 1);
1278 if ($sid1 && $sid1 eq $sid) {
1279 &$func ($volid, $sid, $info);
1280 } else {
1281 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1282 }
1283 }
1284 }
1285 }
1286
1287 sub extract_vzdump_config_tar {
1288 my ($archive, $conf_re) = @_;
1289
1290 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
1291
1292 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1293 die "unable to open file '$archive'\n";
1294
1295 my $file;
1296 while (defined($file = <$fh>)) {
1297 if ($file =~ m!$conf_re!) {
1298 $file = $1; # untaint
1299 last;
1300 }
1301 }
1302
1303 kill 15, $pid;
1304 waitpid $pid, 0;
1305 close $fh;
1306
1307 die "ERROR: archive contains no configuration file\n" if !$file;
1308 chomp $file;
1309
1310 my $raw = '';
1311 my $out = sub {
1312 my $output = shift;
1313 $raw .= "$output\n";
1314 };
1315
1316 PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out);
1317
1318 return wantarray ? ($raw, $file) : $raw;
1319 }
1320
1321 sub extract_vzdump_config_vma {
1322 my ($archive, $comp) = @_;
1323
1324 my $cmd;
1325 my $raw = '';
1326 my $out = sub {
1327 my $output = shift;
1328 $raw .= "$output\n";
1329 };
1330
1331
1332 if ($comp) {
1333 my $uncomp;
1334 if ($comp eq 'gz') {
1335 $uncomp = ["zcat", $archive];
1336 } elsif ($comp eq 'lzo') {
1337 $uncomp = ["lzop", "-d", "-c", $archive];
1338 } else {
1339 die "unknown compression method '$comp'\n";
1340 }
1341 $cmd = [$uncomp, ["vma", "config", "-"]];
1342
1343 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1344 # closed early by vma, detect this and ignore the exit code later
1345 my $broken_pipe;
1346 my $errstring;
1347 my $err = sub {
1348 my $output = shift;
1349 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1350 $broken_pipe = 1;
1351 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1352 $errstring = "Failed to extract config from VMA archive: $output\n";
1353 }
1354 };
1355
1356 # in other cases, the pipeline will exit with exit code 141
1357 # because of the broken pipe, handle / ignore this as well
1358 my $rc;
1359 eval {
1360 $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1);
1361 };
1362 my $rerr = $@;
1363
1364 # use exit code if no stderr output and not just broken pipe
1365 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1366 die "$rerr\n" if $rerr;
1367 die "config extraction failed with exit code $rc\n";
1368 }
1369 die "$errstring\n" if $errstring;
1370 } else {
1371 # simple case without compression and weird piping behaviour
1372 PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out);
1373 }
1374
1375 return wantarray ? ($raw, undef) : $raw;
1376 }
1377
1378 sub extract_vzdump_config {
1379 my ($cfg, $volid) = @_;
1380
1381 my $archive = abs_filesystem_path($cfg, $volid);
1382
1383 if ($volid =~ /\/vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1384 return extract_vzdump_config_tar($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1385 } elsif ($volid =~ /\/vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1386 my $format;
1387 my $comp;
1388 if ($7 eq 'tgz') {
1389 $format = 'tar';
1390 $comp = 'gz';
1391 } else {
1392 $format = $9;
1393 $comp = $11 if defined($11);
1394 }
1395
1396 if ($format eq 'tar') {
1397 return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!);
1398 } else {
1399 return extract_vzdump_config_vma($archive, $comp);
1400 }
1401 } else {
1402 die "cannot determine backup guest type for backup archive '$volid'\n";
1403 }
1404 }
1405
1406 # bash completion helper
1407
1408 sub complete_storage {
1409 my ($cmdname, $pname, $cvalue) = @_;
1410
1411 my $cfg = PVE::Storage::config();
1412
1413 return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ];
1414 }
1415
1416 sub complete_storage_enabled {
1417 my ($cmdname, $pname, $cvalue) = @_;
1418
1419 my $res = [];
1420
1421 my $cfg = PVE::Storage::config();
1422 foreach my $sid (keys %{$cfg->{ids}}) {
1423 next if !storage_check_enabled($cfg, $sid, undef, 1);
1424 push @$res, $sid;
1425 }
1426 return $res;
1427 }
1428
1429 sub complete_content_type {
1430 my ($cmdname, $pname, $cvalue) = @_;
1431
1432 return [qw(rootdir images vztmpl iso backup)];
1433 }
1434
1435 sub complete_volume {
1436 my ($cmdname, $pname, $cvalue) = @_;
1437
1438 my $cfg = config();
1439
1440 my $storage_list = complete_storage_enabled();
1441
1442 if ($cvalue =~ m/^([^:]+):/) {
1443 $storage_list = [ $1 ];
1444 } else {
1445 if (scalar(@$storage_list) > 1) {
1446 # only list storage IDs to avoid large listings
1447 my $res = [];
1448 foreach my $storeid (@$storage_list) {
1449 # Hack: simply return 2 artificial values, so that
1450 # completions does not finish
1451 push @$res, "$storeid:volname", "$storeid:...";
1452 }
1453 return $res;
1454 }
1455 }
1456
1457 my $res = [];
1458 foreach my $storeid (@$storage_list) {
1459 my $vollist = PVE::Storage::volume_list($cfg, $storeid);
1460
1461 foreach my $item (@$vollist) {
1462 push @$res, $item->{volid};
1463 }
1464 }
1465
1466 return $res;
1467 }
1468
1469 1;