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