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