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