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