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