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