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