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