]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
rename ZFSDirPlugin to ZFSPoolPlugin
[pve-storage.git] / PVE / Storage.pm
1 package PVE::Storage;
2
3 use strict;
4 use warnings;
5
6 use POSIX;
7 use IO::Select;
8 use IO::File;
9 use File::Basename;
10 use File::Path;
11 use Cwd 'abs_path';
12 use Socket;
13
14 use PVE::Tools qw(run_command file_read_firstline);
15 use PVE::Cluster qw(cfs_read_file cfs_lock_file);
16 use PVE::Exception qw(raise_param_exc);
17 use PVE::JSONSchema;
18 use PVE::INotify;
19 use PVE::RPCEnvironment;
20
21 use PVE::Storage::Plugin;
22 use PVE::Storage::DirPlugin;
23 use PVE::Storage::LVMPlugin;
24 use PVE::Storage::NFSPlugin;
25 use PVE::Storage::ISCSIPlugin;
26 use PVE::Storage::RBDPlugin;
27 use PVE::Storage::SheepdogPlugin;
28 use PVE::Storage::ISCSIDirectPlugin;
29 use PVE::Storage::GlusterfsPlugin;
30 use PVE::Storage::ZFSPoolPlugin;
31 use PVE::Storage::ZFSPlugin;
32
33 # load and initialize all plugins
34 PVE::Storage::DirPlugin->register();
35 PVE::Storage::LVMPlugin->register();
36 PVE::Storage::NFSPlugin->register();
37 PVE::Storage::ISCSIPlugin->register();
38 PVE::Storage::RBDPlugin->register();
39 PVE::Storage::SheepdogPlugin->register();
40 PVE::Storage::ISCSIDirectPlugin->register();
41 PVE::Storage::GlusterfsPlugin->register();
42 PVE::Storage::ZFSPoolPlugin->register();
43 PVE::Storage::ZFSPlugin->register();
44 PVE::Storage::Plugin->init();
45
46 my $UDEVADM = '/sbin/udevadm';
47
48 # PVE::Storage utility functions
49
50 sub config {
51 return cfs_read_file("storage.cfg");
52 }
53
54 sub lock_storage_config {
55 my ($code, $errmsg) = @_;
56
57 cfs_lock_file("storage.cfg", undef, $code);
58 my $err = $@;
59 if ($err) {
60 $errmsg ? die "$errmsg: $err" : die $err;
61 }
62 }
63
64 sub storage_config {
65 my ($cfg, $storeid, $noerr) = @_;
66
67 die "no storage id specified\n" if !$storeid;
68
69 my $scfg = $cfg->{ids}->{$storeid};
70
71 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
72
73 return $scfg;
74 }
75
76 sub storage_check_node {
77 my ($cfg, $storeid, $node, $noerr) = @_;
78
79 my $scfg = storage_config($cfg, $storeid);
80
81 if ($scfg->{nodes}) {
82 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
83 if (!$scfg->{nodes}->{$node}) {
84 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
85 return undef;
86 }
87 }
88
89 return $scfg;
90 }
91
92 sub storage_check_enabled {
93 my ($cfg, $storeid, $node, $noerr) = @_;
94
95 my $scfg = storage_config($cfg, $storeid);
96
97 if ($scfg->{disable}) {
98 die "storage '$storeid' is disabled\n" if !$noerr;
99 return undef;
100 }
101
102 return storage_check_node($cfg, $storeid, $node, $noerr);
103 }
104
105 sub storage_ids {
106 my ($cfg) = @_;
107
108 return keys %{$cfg->{ids}};
109 }
110
111 sub file_size_info {
112 my ($filename, $timeout) = @_;
113
114 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
115 }
116
117 sub volume_size_info {
118 my ($cfg, $volid, $timeout) = @_;
119
120 my ($storeid, $volname) = parse_volume_id($volid, 1);
121 if ($storeid) {
122 my $scfg = storage_config($cfg, $storeid);
123 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
124 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
125 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
126 return file_size_info($volid, $timeout);
127 } else {
128 return 0;
129 }
130 }
131
132 sub volume_resize {
133 my ($cfg, $volid, $size, $running) = @_;
134
135 my ($storeid, $volname) = parse_volume_id($volid, 1);
136 if ($storeid) {
137 my $scfg = storage_config($cfg, $storeid);
138 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
139 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
140 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
141 die "resize device is not possible";
142 } else {
143 die "can't resize";
144 }
145 }
146
147 sub volume_snapshot {
148 my ($cfg, $volid, $snap, $running) = @_;
149
150 my ($storeid, $volname) = parse_volume_id($volid, 1);
151 if ($storeid) {
152 my $scfg = storage_config($cfg, $storeid);
153 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
154 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running);
155 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
156 die "snapshot device is not possible";
157 } else {
158 die "can't snapshot";
159 }
160 }
161
162 sub volume_snapshot_rollback {
163 my ($cfg, $volid, $snap) = @_;
164
165 my ($storeid, $volname) = parse_volume_id($volid, 1);
166 if ($storeid) {
167 my $scfg = storage_config($cfg, $storeid);
168 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
169 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
170 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
171 die "snapshot rollback device is not possible";
172 } else {
173 die "can't snapshot";
174 }
175 }
176
177 sub volume_snapshot_delete {
178 my ($cfg, $volid, $snap, $running) = @_;
179
180 my ($storeid, $volname) = parse_volume_id($volid, 1);
181 if ($storeid) {
182 my $scfg = storage_config($cfg, $storeid);
183 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
184 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
185 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
186 die "snapshot delete device is not possible";
187 } else {
188 die "can't delete snapshot";
189 }
190 }
191
192 sub volume_has_feature {
193 my ($cfg, $feature, $volid, $snap, $running) = @_;
194
195 my ($storeid, $volname) = parse_volume_id($volid, 1);
196 if ($storeid) {
197 my $scfg = storage_config($cfg, $storeid);
198 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
199 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
200 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
201 return undef;
202 } else {
203 return undef;
204 }
205 }
206
207 sub get_image_dir {
208 my ($cfg, $storeid, $vmid) = @_;
209
210 my $scfg = storage_config($cfg, $storeid);
211 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
212
213 my $path = $plugin->get_subdir($scfg, 'images');
214
215 return $vmid ? "$path/$vmid" : $path;
216 }
217
218 sub get_private_dir {
219 my ($cfg, $storeid, $vmid) = @_;
220
221 my $scfg = storage_config($cfg, $storeid);
222 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
223
224 my $path = $plugin->get_subdir($scfg, 'rootdir');
225
226 return $vmid ? "$path/$vmid" : $path;
227 }
228
229 sub get_iso_dir {
230 my ($cfg, $storeid) = @_;
231
232 my $scfg = storage_config($cfg, $storeid);
233 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
234
235 return $plugin->get_subdir($scfg, 'iso');
236 }
237
238 sub get_vztmpl_dir {
239 my ($cfg, $storeid) = @_;
240
241 my $scfg = storage_config($cfg, $storeid);
242 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
243
244 return $plugin->get_subdir($scfg, 'vztmpl');
245 }
246
247 sub get_backup_dir {
248 my ($cfg, $storeid) = @_;
249
250 my $scfg = storage_config($cfg, $storeid);
251 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
252
253 return $plugin->get_subdir($scfg, 'backup');
254 }
255
256 # library implementation
257
258 sub parse_vmid {
259 my $vmid = shift;
260
261 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
262
263 return int($vmid);
264 }
265
266 sub parse_volname {
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 return $plugin->parse_volname($volname);
275 }
276
277 sub parse_volume_id {
278 my ($volid, $noerr) = @_;
279
280 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
281 }
282
283 sub volume_is_base {
284 my ($cfg, $volid) = @_;
285
286 my ($sid, $volname) = parse_volume_id($volid, 1);
287 return 0 if !$sid;
288
289 if (my $scfg = $cfg->{ids}->{$sid}) {
290 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
291 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
292 $plugin->parse_volname($volname);
293 return $isBase ? 1 : 0;
294 } else {
295 # stale volid with undefined storage - so we can just guess
296 if ($volid =~ m/base-/) {
297 return 1;
298 }
299 }
300
301 return 0;
302 }
303
304 # try to map a filesystem path to a volume identifier
305 sub path_to_volume_id {
306 my ($cfg, $path) = @_;
307
308 my $ids = $cfg->{ids};
309
310 my ($sid, $volname) = parse_volume_id($path, 1);
311 if ($sid) {
312 if (my $scfg = $ids->{$sid}) {
313 if ($scfg->{path}) {
314 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
315 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
316 return ($vtype, $path);
317 }
318 }
319 return ('');
320 }
321
322 # Note: abs_path() return undef if $path doesn not exist
323 # for example when nfs storage is not mounted
324 $path = abs_path($path) || $path;
325
326 foreach my $sid (keys %$ids) {
327 my $scfg = $ids->{$sid};
328 next if !$scfg->{path};
329 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
330 my $imagedir = $plugin->get_subdir($scfg, 'images');
331 my $isodir = $plugin->get_subdir($scfg, 'iso');
332 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
333 my $backupdir = $plugin->get_subdir($scfg, 'backup');
334 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
335
336 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
337 my $vmid = $1;
338 my $name = $2;
339
340 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
341 foreach my $info (@$vollist) {
342 my ($storeid, $volname) = parse_volume_id($info->{volid});
343 my $volpath = $plugin->path($scfg, $volname, $storeid);
344 if ($volpath eq $path) {
345 return ('images', $info->{volid});
346 }
347 }
348 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
349 my $name = $1;
350 return ('iso', "$sid:iso/$name");
351 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
352 my $name = $1;
353 return ('vztmpl', "$sid:vztmpl/$name");
354 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
355 my $vmid = $1;
356 return ('rootdir', "$sid:rootdir/$vmid");
357 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
358 my $name = $1;
359 return ('iso', "$sid:backup/$name");
360 }
361 }
362
363 # can't map path to volume id
364 return ('');
365 }
366
367 sub path {
368 my ($cfg, $volid, $snapname) = @_;
369
370 my ($storeid, $volname) = parse_volume_id($volid);
371
372 my $scfg = storage_config($cfg, $storeid);
373
374 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
375 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
376 return wantarray ? ($path, $owner, $vtype) : $path;
377 }
378
379 sub abs_filesystem_path {
380 my ($cfg, $volid) = @_;
381
382 my $path;
383 if (PVE::Storage::parse_volume_id ($volid, 1)) {
384 PVE::Storage::activate_volumes($cfg, [ $volid ]);
385 $path = PVE::Storage::path($cfg, $volid);
386 } else {
387 if (-f $volid) {
388 my $abspath = abs_path($volid);
389 if ($abspath && $abspath =~ m|^(/.+)$|) {
390 $path = $1; # untaint any path
391 }
392 }
393 }
394
395 die "can't find file '$volid'\n" if !($path && -f $path);
396
397 return $path;
398 }
399
400 sub storage_migrate {
401 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
402
403 my ($storeid, $volname) = parse_volume_id($volid);
404 $target_volname = $volname if !$target_volname;
405
406 my $scfg = storage_config($cfg, $storeid);
407
408 # no need to migrate shared content
409 return if $storeid eq $target_storeid && $scfg->{shared};
410
411 my $tcfg = storage_config($cfg, $target_storeid);
412
413 my $target_volid = "${target_storeid}:${target_volname}";
414
415 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
416
417 my $sshoptions = "-o 'BatchMode=yes'";
418 my $ssh = "/usr/bin/ssh $sshoptions";
419
420 local $ENV{RSYNC_RSH} = $ssh;
421
422 # only implemented for file system based storage
423 if ($scfg->{path}) {
424 if ($tcfg->{path}) {
425
426 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
427 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
428 my $src = $src_plugin->path($scfg, $volname, $storeid);
429 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
430
431 my $dirname = dirname($dst);
432
433 if ($tcfg->{shared}) { # we can do a local copy
434
435 run_command(['/bin/mkdir', '-p', $dirname]);
436
437 run_command(['/bin/cp', $src, $dst]);
438
439 } else {
440
441 run_command(['/usr/bin/ssh', "root\@${target_host}",
442 '/bin/mkdir', '-p', $dirname]);
443
444 # we use rsync with --sparse, so we can't use --inplace,
445 # so we remove file on the target if it already exists to
446 # save space
447 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
448 if ($format && ($format eq 'raw') && $size) {
449 run_command(['/usr/bin/ssh', "root\@${target_host}",
450 'rm', '-f', $dst],
451 outfunc => sub {});
452 }
453
454 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
455 $src, "root\@${target_host}:$dst"];
456
457 my $percent = -1;
458
459 run_command($cmd, outfunc => sub {
460 my $line = shift;
461
462 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
463 if ($2 > $percent) {
464 $percent = $2;
465 print "rsync status: $1\n";
466 *STDOUT->flush();
467 }
468 } else {
469 print "$line\n";
470 *STDOUT->flush();
471 }
472 });
473 }
474 } else {
475 die "$errstr - target type '$tcfg->{type}' not implemented\n";
476 }
477 } else {
478 die "$errstr - source type '$scfg->{type}' not implemented\n";
479 }
480 }
481
482 sub vdisk_clone {
483 my ($cfg, $volid, $vmid, $snap) = @_;
484
485 my ($storeid, $volname) = parse_volume_id($volid);
486
487 my $scfg = storage_config($cfg, $storeid);
488
489 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
490
491 activate_storage($cfg, $storeid);
492
493 # lock shared storage
494 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
495 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
496 return "$storeid:$volname";
497 });
498 }
499
500 sub vdisk_create_base {
501 my ($cfg, $volid) = @_;
502
503 my ($storeid, $volname) = parse_volume_id($volid);
504
505 my $scfg = storage_config($cfg, $storeid);
506
507 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
508
509 activate_storage($cfg, $storeid);
510
511 # lock shared storage
512 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
513 my $volname = $plugin->create_base($storeid, $scfg, $volname);
514 return "$storeid:$volname";
515 });
516 }
517
518 sub vdisk_alloc {
519 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
520
521 die "no storage id specified\n" if !$storeid;
522
523 PVE::JSONSchema::parse_storage_id($storeid);
524
525 my $scfg = storage_config($cfg, $storeid);
526
527 die "no VMID specified\n" if !$vmid;
528
529 $vmid = parse_vmid($vmid);
530
531 my $defformat = PVE::Storage::Plugin::default_format($scfg);
532
533 $fmt = $defformat if !$fmt;
534
535 activate_storage($cfg, $storeid);
536
537 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
538
539 # lock shared storage
540 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
541 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
542 return "$storeid:$volname";
543 });
544 }
545
546 sub vdisk_free {
547 my ($cfg, $volid) = @_;
548
549 my ($storeid, $volname) = parse_volume_id($volid);
550
551 my $scfg = storage_config($cfg, $storeid);
552
553 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
554
555 activate_storage($cfg, $storeid);
556
557 my $cleanup_worker;
558
559 # lock shared storage
560 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
561
562 my ($vtype, $name, $vmid, undef, undef, $isBase) =
563 $plugin->parse_volname($volname);
564 if ($isBase) {
565 my $vollist = $plugin->list_images($storeid, $scfg);
566 foreach my $info (@$vollist) {
567 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
568 my $basename = undef;
569 my $basevmid = undef;
570
571 eval{
572 (undef, undef, undef, $basename, $basevmid) =
573 $plugin->parse_volname($tmpvolname);
574 };
575
576 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
577 die "base volume '$volname' is still in use " .
578 "(use by '$tmpvolname')\n";
579 }
580 }
581 }
582 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
583 });
584
585 return if !$cleanup_worker;
586
587 my $rpcenv = PVE::RPCEnvironment::get();
588 my $authuser = $rpcenv->get_user();
589
590 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
591 }
592
593 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
594 sub template_list {
595 my ($cfg, $storeid, $tt) = @_;
596
597 die "unknown template type '$tt'\n"
598 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
599
600 my $ids = $cfg->{ids};
601
602 storage_check_enabled($cfg, $storeid) if ($storeid);
603
604 my $res = {};
605
606 # query the storage
607
608 foreach my $sid (keys %$ids) {
609 next if $storeid && $storeid ne $sid;
610
611 my $scfg = $ids->{$sid};
612 my $type = $scfg->{type};
613
614 next if !storage_check_enabled($cfg, $sid, undef, 1);
615
616 next if $tt eq 'iso' && !$scfg->{content}->{iso};
617 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
618 next if $tt eq 'backup' && !$scfg->{content}->{backup};
619
620 activate_storage($cfg, $sid);
621
622 if ($scfg->{path}) {
623 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
624
625 my $path = $plugin->get_subdir($scfg, $tt);
626
627 foreach my $fn (<$path/*>) {
628
629 my $info;
630
631 if ($tt eq 'iso') {
632 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
633
634 $info = { volid => "$sid:iso/$1", format => 'iso' };
635
636 } elsif ($tt eq 'vztmpl') {
637 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
638
639 $info = { volid => "$sid:vztmpl/$1", format => 'tgz' };
640
641 } elsif ($tt eq 'backup') {
642 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
643
644 $info = { volid => "$sid:backup/$1", format => $2 };
645 }
646
647 $info->{size} = -s $fn;
648
649 push @{$res->{$sid}}, $info;
650 }
651
652 }
653
654 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
655 }
656
657 return $res;
658 }
659
660
661 sub vdisk_list {
662 my ($cfg, $storeid, $vmid, $vollist) = @_;
663
664 my $ids = $cfg->{ids};
665
666 storage_check_enabled($cfg, $storeid) if ($storeid);
667
668 my $res = {};
669
670 # prepare/activate/refresh all storages
671
672 my $storage_list = [];
673 if ($vollist) {
674 foreach my $volid (@$vollist) {
675 my ($sid, undef) = parse_volume_id($volid);
676 next if !defined($ids->{$sid});
677 next if !storage_check_enabled($cfg, $sid, undef, 1);
678 push @$storage_list, $sid;
679 }
680 } else {
681 foreach my $sid (keys %$ids) {
682 next if $storeid && $storeid ne $sid;
683 next if !storage_check_enabled($cfg, $sid, undef, 1);
684 push @$storage_list, $sid;
685 }
686 }
687
688 my $cache = {};
689
690 activate_storage_list($cfg, $storage_list, $cache);
691
692 foreach my $sid (keys %$ids) {
693 next if $storeid && $storeid ne $sid;
694 next if !storage_check_enabled($cfg, $sid, undef, 1);
695
696 my $scfg = $ids->{$sid};
697 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
698 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
699 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
700 }
701
702 return $res;
703 }
704
705 sub uevent_seqnum {
706
707 my $filename = "/sys/kernel/uevent_seqnum";
708
709 my $seqnum = 0;
710 if (my $fh = IO::File->new($filename, "r")) {
711 my $line = <$fh>;
712 if ($line =~ m/^(\d+)$/) {
713 $seqnum = int($1);
714 }
715 close ($fh);
716 }
717 return $seqnum;
718 }
719
720 sub activate_storage {
721 my ($cfg, $storeid, $cache) = @_;
722
723 $cache = {} if !$cache;
724
725 my $scfg = storage_check_enabled($cfg, $storeid);
726
727 return if $cache->{activated}->{$storeid};
728
729 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
730
731 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
732
733 if ($scfg->{base}) {
734 my ($baseid, undef) = parse_volume_id ($scfg->{base});
735 activate_storage($cfg, $baseid, $cache);
736 }
737
738 if (!$plugin->check_connection($storeid, $scfg)) {
739 die "storage '$storeid' is not online\n";
740 }
741
742 $plugin->activate_storage($storeid, $scfg, $cache);
743
744 my $newseq = uevent_seqnum ();
745
746 # only call udevsettle if there are events
747 if ($newseq > $cache->{uevent_seqnum}) {
748 my $timeout = 30;
749 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
750 $cache->{uevent_seqnum} = $newseq;
751 }
752
753 $cache->{activated}->{$storeid} = 1;
754 }
755
756 sub activate_storage_list {
757 my ($cfg, $storeid_list, $cache) = @_;
758
759 $cache = {} if !$cache;
760
761 foreach my $storeid (@$storeid_list) {
762 activate_storage($cfg, $storeid, $cache);
763 }
764 }
765
766 sub deactivate_storage {
767 my ($cfg, $storeid) = @_;
768
769 my $scfg = storage_config ($cfg, $storeid);
770 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
771
772 my $cache = {};
773 $plugin->deactivate_storage($storeid, $scfg, $cache);
774 }
775
776 sub activate_volumes {
777 my ($cfg, $vollist, $exclusive) = @_;
778
779 return if !($vollist && scalar(@$vollist));
780
781 my $storagehash = {};
782 foreach my $volid (@$vollist) {
783 my ($storeid, undef) = parse_volume_id($volid);
784 $storagehash->{$storeid} = 1;
785 }
786
787 my $cache = {};
788
789 activate_storage_list($cfg, [keys %$storagehash], $cache);
790
791 foreach my $volid (@$vollist) {
792 my ($storeid, $volname) = parse_volume_id($volid);
793 my $scfg = storage_config($cfg, $storeid);
794 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
795 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
796 }
797 }
798
799 sub deactivate_volumes {
800 my ($cfg, $vollist) = @_;
801
802 return if !($vollist && scalar(@$vollist));
803
804 my $cache = {};
805
806 my @errlist = ();
807 foreach my $volid (@$vollist) {
808 my ($storeid, $volname) = parse_volume_id($volid);
809
810 my $scfg = storage_config($cfg, $storeid);
811 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
812
813 eval {
814 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
815 };
816 if (my $err = $@) {
817 warn $err;
818 push @errlist, $volid;
819 }
820 }
821
822 die "volume deativation failed: " . join(' ', @errlist)
823 if scalar(@errlist);
824 }
825
826 sub storage_info {
827 my ($cfg, $content) = @_;
828
829 my $ids = $cfg->{ids};
830
831 my $info = {};
832
833 my $slist = [];
834 foreach my $storeid (keys %$ids) {
835
836 next if $content && !$ids->{$storeid}->{content}->{$content};
837
838 next if !storage_check_enabled($cfg, $storeid, undef, 1);
839
840 my $type = $ids->{$storeid}->{type};
841
842 $info->{$storeid} = {
843 type => $type,
844 total => 0,
845 avail => 0,
846 used => 0,
847 shared => $ids->{$storeid}->{shared} ? 1 : 0,
848 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
849 active => 0,
850 };
851
852 push @$slist, $storeid;
853 }
854
855 my $cache = {};
856
857 foreach my $storeid (keys %$ids) {
858 my $scfg = $ids->{$storeid};
859 next if !$info->{$storeid};
860
861 eval { activate_storage($cfg, $storeid, $cache); };
862 if (my $err = $@) {
863 warn $err;
864 next;
865 }
866
867 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
868 my ($total, $avail, $used, $active);
869 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
870 warn $@ if $@;
871 next if !$active;
872 $info->{$storeid}->{total} = $total;
873 $info->{$storeid}->{avail} = $avail;
874 $info->{$storeid}->{used} = $used;
875 $info->{$storeid}->{active} = $active;
876 }
877
878 return $info;
879 }
880
881 sub resolv_server {
882 my ($server) = @_;
883
884 my $packed_ip = gethostbyname($server);
885 if (defined $packed_ip) {
886 return inet_ntoa($packed_ip);
887 }
888 return undef;
889 }
890
891 sub scan_nfs {
892 my ($server_in) = @_;
893
894 my $server;
895 if (!($server = resolv_server ($server_in))) {
896 die "unable to resolve address for server '${server_in}'\n";
897 }
898
899 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
900
901 my $res = {};
902 run_command($cmd, outfunc => sub {
903 my $line = shift;
904
905 # note: howto handle white spaces in export path??
906 if ($line =~ m!^(/\S+)\s+(.+)$!) {
907 $res->{$1} = $2;
908 }
909 });
910
911 return $res;
912 }
913
914 sub resolv_portal {
915 my ($portal, $noerr) = @_;
916
917 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
918 my $server = $1;
919 my $port = $3;
920
921 if (my $ip = resolv_server($server)) {
922 $server = $ip;
923 return $port ? "$server:$port" : $server;
924 }
925 }
926 return undef if $noerr;
927
928 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
929 }
930
931 # idea is from usbutils package (/usr/bin/usb-devices) script
932 sub __scan_usb_device {
933 my ($res, $devpath, $parent, $level) = @_;
934
935 return if ! -d $devpath;
936 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
937 my $port = $level ? int($1 - 1) : 0;
938
939 my $busnum = int(file_read_firstline("$devpath/busnum"));
940 my $devnum = int(file_read_firstline("$devpath/devnum"));
941
942 my $d = {
943 port => $port,
944 level => $level,
945 busnum => $busnum,
946 devnum => $devnum,
947 speed => file_read_firstline("$devpath/speed"),
948 class => hex(file_read_firstline("$devpath/bDeviceClass")),
949 vendid => file_read_firstline("$devpath/idVendor"),
950 prodid => file_read_firstline("$devpath/idProduct"),
951 };
952
953 if ($level) {
954 my $usbpath = $devpath;
955 $usbpath =~ s|^.*/\d+\-||;
956 $d->{usbpath} = $usbpath;
957 }
958
959 my $product = file_read_firstline("$devpath/product");
960 $d->{product} = $product if $product;
961
962 my $manu = file_read_firstline("$devpath/manufacturer");
963 $d->{manufacturer} = $manu if $manu;
964
965 my $serial => file_read_firstline("$devpath/serial");
966 $d->{serial} = $serial if $serial;
967
968 push @$res, $d;
969
970 foreach my $subdev (<$devpath/$busnum-*>) {
971 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
972 __scan_usb_device($res, $subdev, $devnum, $level + 1);
973 }
974
975 };
976
977 sub scan_usb {
978
979 my $devlist = [];
980
981 foreach my $device (</sys/bus/usb/devices/usb*>) {
982 __scan_usb_device($devlist, $device, 0, 0);
983 }
984
985 return $devlist;
986 }
987
988 sub scan_iscsi {
989 my ($portal_in) = @_;
990
991 my $portal;
992 if (!($portal = resolv_portal($portal_in))) {
993 die "unable to parse/resolve portal address '${portal_in}'\n";
994 }
995
996 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
997 }
998
999 sub storage_default_format {
1000 my ($cfg, $storeid) = @_;
1001
1002 my $scfg = storage_config ($cfg, $storeid);
1003
1004 return PVE::Storage::Plugin::default_format($scfg);
1005 }
1006
1007 sub vgroup_is_used {
1008 my ($cfg, $vgname) = @_;
1009
1010 foreach my $storeid (keys %{$cfg->{ids}}) {
1011 my $scfg = storage_config($cfg, $storeid);
1012 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
1013 return 1;
1014 }
1015 }
1016
1017 return undef;
1018 }
1019
1020 sub target_is_used {
1021 my ($cfg, $target) = @_;
1022
1023 foreach my $storeid (keys %{$cfg->{ids}}) {
1024 my $scfg = storage_config($cfg, $storeid);
1025 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
1026 return 1;
1027 }
1028 }
1029
1030 return undef;
1031 }
1032
1033 sub volume_is_used {
1034 my ($cfg, $volid) = @_;
1035
1036 foreach my $storeid (keys %{$cfg->{ids}}) {
1037 my $scfg = storage_config($cfg, $storeid);
1038 if ($scfg->{base} && $scfg->{base} eq $volid) {
1039 return 1;
1040 }
1041 }
1042
1043 return undef;
1044 }
1045
1046 sub storage_is_used {
1047 my ($cfg, $storeid) = @_;
1048
1049 foreach my $sid (keys %{$cfg->{ids}}) {
1050 my $scfg = storage_config($cfg, $sid);
1051 next if !$scfg->{base};
1052 my ($st) = parse_volume_id($scfg->{base});
1053 return 1 if $st && $st eq $storeid;
1054 }
1055
1056 return undef;
1057 }
1058
1059 sub foreach_volid {
1060 my ($list, $func) = @_;
1061
1062 return if !$list;
1063
1064 foreach my $sid (keys %$list) {
1065 foreach my $info (@{$list->{$sid}}) {
1066 my $volid = $info->{volid};
1067 my ($sid1, $volname) = parse_volume_id($volid, 1);
1068 if ($sid1 && $sid1 eq $sid) {
1069 &$func ($volid, $sid, $info);
1070 } else {
1071 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1072 }
1073 }
1074 }
1075 }
1076
1077 1;