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