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