]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
update version to 2.0-19
[pve-storage.git] / PVE / Storage.pm
1 package PVE::Storage;
2
3 use strict;
4 use POSIX;
5 use IO::Select;
6 use IO::File;
7 use File::Basename;
8 use File::Path;
9 use Cwd 'abs_path';
10
11 use PVE::Tools qw(run_command file_read_firstline);
12 use PVE::Cluster qw(cfs_read_file cfs_lock_file);
13 use PVE::Exception qw(raise_param_exc);
14 use PVE::JSONSchema;
15 use PVE::INotify;
16 use PVE::RPCEnvironment;
17
18 use PVE::Storage::Plugin;
19 use PVE::Storage::DirPlugin;
20 use PVE::Storage::LVMPlugin;
21 use PVE::Storage::NFSPlugin;
22 use PVE::Storage::ISCSIPlugin;
23
24 # load and initialize all plugins
25 PVE::Storage::DirPlugin->register();
26 PVE::Storage::LVMPlugin->register();
27 PVE::Storage::NFSPlugin->register();
28 PVE::Storage::ISCSIPlugin->register();
29 PVE::Storage::Plugin->init();
30
31 my $UDEVADM = '/sbin/udevadm';
32
33 # PVE::Storage utility functions
34
35 sub config {
36 return cfs_read_file("storage.cfg");
37 }
38
39 sub lock_storage_config {
40 my ($code, $errmsg) = @_;
41
42 cfs_lock_file("storage.cfg", undef, $code);
43 my $err = $@;
44 if ($err) {
45 $errmsg ? die "$errmsg: $err" : die $err;
46 }
47 }
48
49 sub storage_config {
50 my ($cfg, $storeid, $noerr) = @_;
51
52 die "no storage id specified\n" if !$storeid;
53
54 my $scfg = $cfg->{ids}->{$storeid};
55
56 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
57
58 return $scfg;
59 }
60
61 sub storage_check_node {
62 my ($cfg, $storeid, $node, $noerr) = @_;
63
64 my $scfg = storage_config($cfg, $storeid);
65
66 if ($scfg->{nodes}) {
67 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
68 if (!$scfg->{nodes}->{$node}) {
69 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
70 return undef;
71 }
72 }
73
74 return $scfg;
75 }
76
77 sub storage_check_enabled {
78 my ($cfg, $storeid, $node, $noerr) = @_;
79
80 my $scfg = storage_config($cfg, $storeid);
81
82 if ($scfg->{disable}) {
83 die "storage '$storeid' is disabled\n" if !$noerr;
84 return undef;
85 }
86
87 return storage_check_node($cfg, $storeid, $node, $noerr);
88 }
89
90 sub storage_ids {
91 my ($cfg) = @_;
92
93 return keys %{$cfg->{ids}};
94 }
95
96 sub file_size_info {
97 my ($filename, $timeout) = @_;
98
99 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
100 }
101
102 sub get_image_dir {
103 my ($cfg, $storeid, $vmid) = @_;
104
105 my $scfg = storage_config($cfg, $storeid);
106 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
107
108 my $path = $plugin->get_subdir($scfg, 'images');
109
110 return $vmid ? "$path/$vmid" : $path;
111 }
112
113 sub get_private_dir {
114 my ($cfg, $storeid, $vmid) = @_;
115
116 my $scfg = storage_config($cfg, $storeid);
117 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
118
119 my $path = $plugin->get_subdir($scfg, 'rootdir');
120
121 return $vmid ? "$path/$vmid" : $path;
122 }
123
124 sub get_iso_dir {
125 my ($cfg, $storeid) = @_;
126
127 my $scfg = storage_config($cfg, $storeid);
128 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
129
130 return $plugin->get_subdir($scfg, 'iso');
131 }
132
133 sub get_vztmpl_dir {
134 my ($cfg, $storeid) = @_;
135
136 my $scfg = storage_config($cfg, $storeid);
137 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
138
139 return $plugin->get_subdir($scfg, 'vztmpl');
140 }
141
142 sub get_backup_dir {
143 my ($cfg, $storeid) = @_;
144
145 my $scfg = storage_config($cfg, $storeid);
146 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
147
148 return $plugin->get_subdir($scfg, 'backup');
149 }
150
151 # library implementation
152
153 sub parse_vmid {
154 my $vmid = shift;
155
156 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
157
158 return int($vmid);
159 }
160
161 PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
162 sub parse_volume_id {
163 my ($volid, $noerr) = @_;
164
165 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
166 return wantarray ? ($1, $2) : $1;
167 }
168 return undef if $noerr;
169 die "unable to parse volume ID '$volid'\n";
170 }
171
172 # try to map a filesystem path to a volume identifier
173 sub path_to_volume_id {
174 my ($cfg, $path) = @_;
175
176 my $ids = $cfg->{ids};
177
178 my ($sid, $volname) = parse_volume_id($path, 1);
179 if ($sid) {
180 if (my $scfg = $ids->{$sid}) {
181 if (my $path = $scfg->{path}) {
182 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
183 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
184 return ($vtype, $path);
185 }
186 }
187 return ('');
188 }
189
190 # Note: abs_path() return undef if $path doesn not exist
191 # for example when nfs storage is not mounted
192 $path = abs_path($path) || $path;
193
194 foreach my $sid (keys %$ids) {
195 my $scfg = $ids->{$sid};
196 next if !$scfg->{path};
197 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
198 my $imagedir = $plugin->get_subdir($scfg, 'images');
199 my $isodir = $plugin->get_subdir($scfg, 'iso');
200 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
201 my $backupdir = $plugin->get_subdir($scfg, 'backup');
202 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
203
204 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
205 my $vmid = $1;
206 my $name = $2;
207 return ('images', "$sid:$vmid/$name");
208 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
209 my $name = $1;
210 return ('iso', "$sid:iso/$name");
211 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
212 my $name = $1;
213 return ('vztmpl', "$sid:vztmpl/$name");
214 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
215 my $vmid = $1;
216 return ('rootdir', "$sid:rootdir/$vmid");
217 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
218 my $name = $1;
219 return ('iso', "$sid:backup/$name");
220 }
221 }
222
223 # can't map path to volume id
224 return ('');
225 }
226
227 sub path {
228 my ($cfg, $volid) = @_;
229
230 my ($storeid, $volname) = parse_volume_id($volid);
231
232 my $scfg = storage_config($cfg, $storeid);
233
234 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
235 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname);
236 return wantarray ? ($path, $owner, $vtype) : $path;
237 }
238
239 sub storage_migrate {
240 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
241
242 my ($storeid, $volname) = parse_volume_id ($volid);
243 $target_volname = $volname if !$target_volname;
244
245 my $scfg = storage_config ($cfg, $storeid);
246
247 # no need to migrate shared content
248 return if $storeid eq $target_storeid && $scfg->{shared};
249
250 my $tcfg = storage_config ($cfg, $target_storeid);
251
252 my $target_volid = "${target_storeid}:${target_volname}";
253
254 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
255
256 # blowfish is a fast block cipher, much faster then 3des
257 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
258 my $ssh = "/usr/bin/ssh $sshoptions";
259
260 local $ENV{RSYNC_RSH} = $ssh;
261
262 # only implemented for file system based storage
263 if ($scfg->{path}) {
264 if ($tcfg->{path}) {
265
266 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
267 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
268 my $src = $src_plugin->path($scfg, $volid);
269 my $dst = $dst_plugin->path($tcfg, $target_volid);
270
271 my $dirname = dirname($dst);
272
273 if ($tcfg->{shared}) { # we can do a local copy
274
275 run_command(['/bin/mkdir', '-p', $dirname]);
276
277 run_command(['/bin/cp', $src, $dst]);
278
279 } else {
280
281 run_command(['/usr/bin/ssh', "root\@${target_host}",
282 '/bin/mkdir', '-p', $dirname]);
283
284 # we use rsync with --sparse, so we can't use --inplace,
285 # so we remove file on the target if it already exists to
286 # save space
287 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
288 if ($format && ($format eq 'raw') && $size) {
289 run_command(['/usr/bin/ssh', "root\@${target_host}",
290 'rm', '-f', $dst],
291 outfunc => sub {});
292 }
293
294 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
295 $src, "root\@${target_host}:$dst"];
296
297 my $percent = -1;
298
299 run_command($cmd, outfunc => sub {
300 my $line = shift;
301
302 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
303 if ($2 > $percent) {
304 $percent = $2;
305 print "rsync status: $1\n";
306 *STDOUT->flush();
307 }
308 } else {
309 print "$line\n";
310 *STDOUT->flush();
311 }
312 });
313 }
314 } else {
315 die "$errstr - target type '$tcfg->{type}' not implemented\n";
316 }
317 } else {
318 die "$errstr - source type '$scfg->{type}' not implemented\n";
319 }
320 }
321
322 sub vdisk_alloc {
323 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
324
325 die "no storage id specified\n" if !$storeid;
326
327 PVE::JSONSchema::parse_storage_id($storeid);
328
329 my $scfg = storage_config($cfg, $storeid);
330
331 die "no VMID specified\n" if !$vmid;
332
333 $vmid = parse_vmid($vmid);
334
335 my $defformat = PVE::Storage::Plugin::default_format($scfg);
336
337 $fmt = $defformat if !$fmt;
338
339 activate_storage($cfg, $storeid);
340
341 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
342
343 # lock shared storage
344 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
345 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
346 return "$storeid:$volname";
347 });
348 }
349
350 sub vdisk_free {
351 my ($cfg, $volid) = @_;
352
353 my ($storeid, $volname) = parse_volume_id($volid);
354
355 my $scfg = storage_config($cfg, $storeid);
356
357 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
358
359 activate_storage($cfg, $storeid);
360
361 my $cleanup_worker;
362
363 # lock shared storage
364 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
365 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
366 });
367
368 return if !$cleanup_worker;
369
370 my $rpcenv = PVE::RPCEnvironment::get();
371 my $authuser = $rpcenv->get_user();
372
373 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
374 }
375
376 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
377 sub template_list {
378 my ($cfg, $storeid, $tt) = @_;
379
380 die "unknown template type '$tt'\n"
381 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
382
383 my $ids = $cfg->{ids};
384
385 storage_check_enabled($cfg, $storeid) if ($storeid);
386
387 my $res = {};
388
389 # query the storage
390
391 foreach my $sid (keys %$ids) {
392 next if $storeid && $storeid ne $sid;
393
394 my $scfg = $ids->{$sid};
395 my $type = $scfg->{type};
396
397 next if !storage_check_enabled($cfg, $sid, undef, 1);
398
399 next if $tt eq 'iso' && !$scfg->{content}->{iso};
400 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
401 next if $tt eq 'backup' && !$scfg->{content}->{backup};
402
403 activate_storage($cfg, $sid);
404
405 if ($scfg->{path}) {
406 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
407
408 my $path = $plugin->get_subdir($scfg, $tt);
409
410 foreach my $fn (<$path/*>) {
411
412 my $info;
413
414 if ($tt eq 'iso') {
415 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
416
417 $info = { volid => "$sid:iso/$1", format => 'iso' };
418
419 } elsif ($tt eq 'vztmpl') {
420 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
421
422 $info = { volid => "$sid:vztmpl/$1", format => 'tgz' };
423
424 } elsif ($tt eq 'backup') {
425 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
426
427 $info = { volid => "$sid:backup/$1", format => $2 };
428 }
429
430 $info->{size} = -s $fn;
431
432 push @{$res->{$sid}}, $info;
433 }
434
435 }
436
437 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
438 }
439
440 return $res;
441 }
442
443 sub vdisk_list {
444 my ($cfg, $storeid, $vmid, $vollist) = @_;
445
446 my $ids = $cfg->{ids};
447
448 storage_check_enabled($cfg, $storeid) if ($storeid);
449
450 my $res = {};
451
452 # prepare/activate/refresh all storages
453
454 my $storage_list = [];
455 if ($vollist) {
456 foreach my $volid (@$vollist) {
457 my ($sid, undef) = parse_volume_id($volid);
458 next if !defined($ids->{$sid});
459 next if !storage_check_enabled($cfg, $sid, undef, 1);
460 push @$storage_list, $sid;
461 }
462 } else {
463 foreach my $sid (keys %$ids) {
464 next if $storeid && $storeid ne $sid;
465 next if !storage_check_enabled($cfg, $sid, undef, 1);
466 push @$storage_list, $sid;
467 }
468 }
469
470 my $cache = {};
471
472 activate_storage_list($cfg, $storage_list, $cache);
473
474 foreach my $sid (keys %$ids) {
475 next if $storeid && $storeid ne $sid;
476 next if !storage_check_enabled($cfg, $sid, undef, 1);
477
478 my $scfg = $ids->{$sid};
479 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
480 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
481 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
482 }
483
484 return $res;
485 }
486
487 sub uevent_seqnum {
488
489 my $filename = "/sys/kernel/uevent_seqnum";
490
491 my $seqnum = 0;
492 if (my $fh = IO::File->new($filename, "r")) {
493 my $line = <$fh>;
494 if ($line =~ m/^(\d+)$/) {
495 $seqnum = int($1);
496 }
497 close ($fh);
498 }
499 return $seqnum;
500 }
501
502 sub __activate_storage_full {
503 my ($cfg, $storeid, $cache) = @_;
504
505 my $scfg = storage_check_enabled($cfg, $storeid);
506
507 return if $cache->{activated}->{$storeid};
508
509 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
510
511 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
512
513 if ($scfg->{base}) {
514 my ($baseid, undef) = parse_volume_id ($scfg->{base});
515 __activate_storage_full ($cfg, $baseid, $cache);
516 }
517
518 $plugin->activate_storage($storeid, $scfg, $cache);
519
520 my $newseq = uevent_seqnum ();
521
522 # only call udevsettle if there are events
523 if ($newseq > $cache->{uevent_seqnum}) {
524 my $timeout = 30;
525 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
526 $cache->{uevent_seqnum} = $newseq;
527 }
528
529 $cache->{activated}->{$storeid} = 1;
530 }
531
532 sub activate_storage_list {
533 my ($cfg, $storeid_list, $cache) = @_;
534
535 $cache = {} if !$cache;
536
537 foreach my $storeid (@$storeid_list) {
538 __activate_storage_full ($cfg, $storeid, $cache);
539 }
540 }
541
542 sub activate_storage {
543 my ($cfg, $storeid) = @_;
544
545 my $cache = {};
546
547 __activate_storage_full ($cfg, $storeid, $cache);
548 }
549
550
551 sub deactivate_storage {
552 my ($cfg, $storeid) = @_;
553
554 my $scfg = storage_config ($cfg, $storeid);
555 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
556
557 my $cache = {};
558 $plugin->deactivate_storage($storeid, $scfg, $cache);
559 }
560
561 sub activate_volumes {
562 my ($cfg, $vollist, $exclusive) = @_;
563
564 return if !($vollist && scalar(@$vollist));
565
566 my $storagehash = {};
567 foreach my $volid (@$vollist) {
568 my ($storeid, undef) = parse_volume_id($volid);
569 $storagehash->{$storeid} = 1;
570 }
571
572 my $cache = {};
573
574 activate_storage_list($cfg, [keys %$storagehash], $cache);
575
576 foreach my $volid (@$vollist) {
577 my ($storeid, $volname) = parse_volume_id ($volid);
578 my $scfg = storage_config($cfg, $storeid);
579 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
580 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
581 }
582 }
583
584 sub deactivate_volumes {
585 my ($cfg, $vollist) = @_;
586
587 return if !($vollist && scalar(@$vollist));
588
589 my $cache = {};
590
591 my @errlist = ();
592 foreach my $volid (@$vollist) {
593 my ($storeid, $volname) = parse_volume_id($volid);
594
595 my $scfg = storage_config($cfg, $storeid);
596 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
597
598 eval {
599 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
600 };
601 if (my $err = $@) {
602 warn $err;
603 push @errlist, $volid;
604 }
605 }
606
607 die "volume deativation failed: " . join(' ', @errlist)
608 if scalar(@errlist);
609 }
610
611 sub storage_info {
612 my ($cfg, $content) = @_;
613
614 my $ids = $cfg->{ids};
615
616 my $info = {};
617
618 my $slist = [];
619 foreach my $storeid (keys %$ids) {
620
621 next if $content && !$ids->{$storeid}->{content}->{$content};
622
623 next if !storage_check_enabled($cfg, $storeid, undef, 1);
624
625 my $type = $ids->{$storeid}->{type};
626
627 $info->{$storeid} = {
628 type => $type,
629 total => 0,
630 avail => 0,
631 used => 0,
632 shared => $ids->{$storeid}->{shared} ? 1 : 0,
633 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
634 active => 0,
635 };
636
637 push @$slist, $storeid;
638 }
639
640 my $cache = {};
641
642 eval { activate_storage_list($cfg, $slist, $cache); };
643
644 foreach my $storeid (keys %$ids) {
645 my $scfg = $ids->{$storeid};
646 next if !$info->{$storeid};
647
648 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
649 my ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache);
650 next if !$active;
651 $info->{$storeid}->{total} = $total;
652 $info->{$storeid}->{avail} = $avail;
653 $info->{$storeid}->{used} = $used;
654 $info->{$storeid}->{active} = $active;
655 }
656
657 return $info;
658 }
659
660 sub resolv_server {
661 my ($server) = @_;
662
663 my $packed_ip = gethostbyname($server);
664 if (defined $packed_ip) {
665 return inet_ntoa($packed_ip);
666 }
667 return undef;
668 }
669
670 sub scan_nfs {
671 my ($server_in) = @_;
672
673 my $server;
674 if (!($server = resolv_server ($server_in))) {
675 die "unable to resolve address for server '${server_in}'\n";
676 }
677
678 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
679
680 my $res = {};
681 run_command($cmd, outfunc => sub {
682 my $line = shift;
683
684 # note: howto handle white spaces in export path??
685 if ($line =~ m!^(/\S+)\s+(.+)$!) {
686 $res->{$1} = $2;
687 }
688 });
689
690 return $res;
691 }
692
693 sub resolv_portal {
694 my ($portal, $noerr) = @_;
695
696 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
697 my $server = $1;
698 my $port = $3;
699
700 if (my $ip = resolv_server($server)) {
701 $server = $ip;
702 return $port ? "$server:$port" : $server;
703 }
704 }
705 return undef if $noerr;
706
707 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
708 }
709
710 # idea is from usbutils package (/usr/bin/usb-devices) script
711 sub __scan_usb_device {
712 my ($res, $devpath, $parent, $level) = @_;
713
714 return if ! -d $devpath;
715 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
716 my $port = $level ? int($1 - 1) : 0;
717
718 my $busnum = int(file_read_firstline("$devpath/busnum"));
719 my $devnum = int(file_read_firstline("$devpath/devnum"));
720
721 my $d = {
722 port => $port,
723 level => $level,
724 busnum => $busnum,
725 devnum => $devnum,
726 speed => file_read_firstline("$devpath/speed"),
727 class => hex(file_read_firstline("$devpath/bDeviceClass")),
728 vendid => file_read_firstline("$devpath/idVendor"),
729 prodid => file_read_firstline("$devpath/idProduct"),
730 };
731
732 if ($level) {
733 my $usbpath = $devpath;
734 $usbpath =~ s|^.*/\d+\-||;
735 $d->{usbpath} = $usbpath;
736 }
737
738 my $product = file_read_firstline("$devpath/product");
739 $d->{product} = $product if $product;
740
741 my $manu = file_read_firstline("$devpath/manufacturer");
742 $d->{manufacturer} = $manu if $manu;
743
744 my $serial => file_read_firstline("$devpath/serial");
745 $d->{serial} = $serial if $serial;
746
747 push @$res, $d;
748
749 foreach my $subdev (<$devpath/$busnum-*>) {
750 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
751 __scan_usb_device($res, $subdev, $devnum, $level + 1);
752 }
753
754 };
755
756 sub scan_usb {
757
758 my $devlist = [];
759
760 foreach my $device (</sys/bus/usb/devices/usb*>) {
761 __scan_usb_device($devlist, $device, 0, 0);
762 }
763
764 return $devlist;
765 }
766
767 sub scan_iscsi {
768 my ($portal_in) = @_;
769
770 my $portal;
771 if (!($portal = resolv_portal($portal_in))) {
772 die "unable to parse/resolve portal address '${portal_in}'\n";
773 }
774
775 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
776 }
777
778 sub storage_default_format {
779 my ($cfg, $storeid) = @_;
780
781 my $scfg = storage_config ($cfg, $storeid);
782
783 return PVE::Storage::Plugin::default_format($scfg);
784 }
785
786 sub vgroup_is_used {
787 my ($cfg, $vgname) = @_;
788
789 foreach my $storeid (keys %{$cfg->{ids}}) {
790 my $scfg = storage_config($cfg, $storeid);
791 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
792 return 1;
793 }
794 }
795
796 return undef;
797 }
798
799 sub target_is_used {
800 my ($cfg, $target) = @_;
801
802 foreach my $storeid (keys %{$cfg->{ids}}) {
803 my $scfg = storage_config($cfg, $storeid);
804 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
805 return 1;
806 }
807 }
808
809 return undef;
810 }
811
812 sub volume_is_used {
813 my ($cfg, $volid) = @_;
814
815 foreach my $storeid (keys %{$cfg->{ids}}) {
816 my $scfg = storage_config($cfg, $storeid);
817 if ($scfg->{base} && $scfg->{base} eq $volid) {
818 return 1;
819 }
820 }
821
822 return undef;
823 }
824
825 sub storage_is_used {
826 my ($cfg, $storeid) = @_;
827
828 foreach my $sid (keys %{$cfg->{ids}}) {
829 my $scfg = storage_config($cfg, $sid);
830 next if !$scfg->{base};
831 my ($st) = parse_volume_id($scfg->{base});
832 return 1 if $st && $st eq $storeid;
833 }
834
835 return undef;
836 }
837
838 sub foreach_volid {
839 my ($list, $func) = @_;
840
841 return if !$list;
842
843 foreach my $sid (keys %$list) {
844 foreach my $info (@{$list->{$sid}}) {
845 my $volid = $info->{volid};
846 my ($sid1, $volname) = parse_volume_id($volid, 1);
847 if ($sid1 && $sid1 eq $sid) {
848 &$func ($volid, $sid, $info);
849 } else {
850 warn "detected strange volid '$volid' in volume list for '$sid'\n";
851 }
852 }
853 }
854 }
855
856 1;