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