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