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