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