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