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