]> git.proxmox.com Git - pve-storage.git/blame_incremental - PVE/Storage.pm
migrate: snapshot parameter for use with replication
[pve-storage.git] / PVE / Storage.pm
... / ...
CommitLineData
1package PVE::Storage;
2
3use strict;
4use warnings;
5use Data::Dumper;
6
7use POSIX;
8use IO::Select;
9use IO::File;
10use File::Basename;
11use File::Path;
12use Cwd 'abs_path';
13use Socket;
14
15use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
16use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
17use PVE::Exception qw(raise_param_exc);
18use PVE::JSONSchema;
19use PVE::INotify;
20use PVE::RPCEnvironment;
21
22use PVE::Storage::Plugin;
23use PVE::Storage::DirPlugin;
24use PVE::Storage::LVMPlugin;
25use PVE::Storage::LvmThinPlugin;
26use PVE::Storage::NFSPlugin;
27use PVE::Storage::ISCSIPlugin;
28use PVE::Storage::RBDPlugin;
29use PVE::Storage::SheepdogPlugin;
30use PVE::Storage::ISCSIDirectPlugin;
31use PVE::Storage::GlusterfsPlugin;
32use PVE::Storage::ZFSPoolPlugin;
33use PVE::Storage::ZFSPlugin;
34use PVE::Storage::DRBDPlugin;
35
36# Storage API version. Icrement it on changes in storage API interface.
37use constant APIVER => 1;
38
39# load standard plugins
40PVE::Storage::DirPlugin->register();
41PVE::Storage::LVMPlugin->register();
42PVE::Storage::LvmThinPlugin->register();
43PVE::Storage::NFSPlugin->register();
44PVE::Storage::ISCSIPlugin->register();
45PVE::Storage::RBDPlugin->register();
46PVE::Storage::SheepdogPlugin->register();
47PVE::Storage::ISCSIDirectPlugin->register();
48PVE::Storage::GlusterfsPlugin->register();
49PVE::Storage::ZFSPoolPlugin->register();
50PVE::Storage::ZFSPlugin->register();
51PVE::Storage::DRBDPlugin->register();
52
53# load third-party plugins
54if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) {
55 dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
56 my ($file) = @_;
57 my $modname = 'PVE::Storage::Custom::' . $file;
58 $modname =~ s!\.pm$!!;
59 $file = 'PVE/Storage/Custom/' . $file;
60
61 eval {
62 require $file;
63 };
64 if ($@) {
65 warn $@;
66 # Check storage API version and that file is really storage plugin.
67 } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER) {
68 eval {
69 import $file;
70 $modname->register();
71 };
72 warn $@ if $@;
73 } else {
74 warn "Error loading storage plugin \"$modname\" because of API version mismatch. Please, update it.\n"
75 }
76 });
77}
78
79# initialize all plugins
80PVE::Storage::Plugin->init();
81
82my $UDEVADM = '/sbin/udevadm';
83
84# PVE::Storage utility functions
85
86sub config {
87 return cfs_read_file("storage.cfg");
88}
89
90sub write_config {
91 my ($cfg) = @_;
92
93 cfs_write_file('storage.cfg', $cfg);
94}
95
96sub lock_storage_config {
97 my ($code, $errmsg) = @_;
98
99 cfs_lock_file("storage.cfg", undef, $code);
100 my $err = $@;
101 if ($err) {
102 $errmsg ? die "$errmsg: $err" : die $err;
103 }
104}
105
106sub storage_config {
107 my ($cfg, $storeid, $noerr) = @_;
108
109 die "no storage ID specified\n" if !$storeid;
110
111 my $scfg = $cfg->{ids}->{$storeid};
112
113 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
114
115 return $scfg;
116}
117
118sub storage_check_node {
119 my ($cfg, $storeid, $node, $noerr) = @_;
120
121 my $scfg = storage_config($cfg, $storeid);
122
123 if ($scfg->{nodes}) {
124 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
125 if (!$scfg->{nodes}->{$node}) {
126 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
127 return undef;
128 }
129 }
130
131 return $scfg;
132}
133
134sub storage_check_enabled {
135 my ($cfg, $storeid, $node, $noerr) = @_;
136
137 my $scfg = storage_config($cfg, $storeid);
138
139 if ($scfg->{disable}) {
140 die "storage '$storeid' is disabled\n" if !$noerr;
141 return undef;
142 }
143
144 return storage_check_node($cfg, $storeid, $node, $noerr);
145}
146
147sub storage_ids {
148 my ($cfg) = @_;
149
150 return keys %{$cfg->{ids}};
151}
152
153sub file_size_info {
154 my ($filename, $timeout) = @_;
155
156 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
157}
158
159sub volume_size_info {
160 my ($cfg, $volid, $timeout) = @_;
161
162 my ($storeid, $volname) = parse_volume_id($volid, 1);
163 if ($storeid) {
164 my $scfg = storage_config($cfg, $storeid);
165 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
166 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
167 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
168 return file_size_info($volid, $timeout);
169 } else {
170 return 0;
171 }
172}
173
174sub volume_resize {
175 my ($cfg, $volid, $size, $running) = @_;
176
177 my ($storeid, $volname) = parse_volume_id($volid, 1);
178 if ($storeid) {
179 my $scfg = storage_config($cfg, $storeid);
180 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
181 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
182 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
183 die "resize file/device '$volid' is not possible\n";
184 } else {
185 die "unable to parse volume ID '$volid'\n";
186 }
187}
188
189sub volume_rollback_is_possible {
190 my ($cfg, $volid, $snap) = @_;
191
192 my ($storeid, $volname) = parse_volume_id($volid, 1);
193 if ($storeid) {
194 my $scfg = storage_config($cfg, $storeid);
195 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
196 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
197 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
198 die "snapshot rollback file/device '$volid' is not possible\n";
199 } else {
200 die "unable to parse volume ID '$volid'\n";
201 }
202}
203
204sub volume_snapshot {
205 my ($cfg, $volid, $snap) = @_;
206
207 my ($storeid, $volname) = parse_volume_id($volid, 1);
208 if ($storeid) {
209 my $scfg = storage_config($cfg, $storeid);
210 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
211 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
212 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
213 die "snapshot file/device '$volid' is not possible\n";
214 } else {
215 die "unable to parse volume ID '$volid'\n";
216 }
217}
218
219sub volume_snapshot_rollback {
220 my ($cfg, $volid, $snap) = @_;
221
222 my ($storeid, $volname) = parse_volume_id($volid, 1);
223 if ($storeid) {
224 my $scfg = storage_config($cfg, $storeid);
225 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
226 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
227 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
228 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
229 die "snapshot rollback file/device '$volid' is not possible\n";
230 } else {
231 die "unable to parse volume ID '$volid'\n";
232 }
233}
234
235sub volume_snapshot_delete {
236 my ($cfg, $volid, $snap, $running) = @_;
237
238 my ($storeid, $volname) = parse_volume_id($volid, 1);
239 if ($storeid) {
240 my $scfg = storage_config($cfg, $storeid);
241 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
242 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
243 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
244 die "snapshot delete file/device '$volid' is not possible\n";
245 } else {
246 die "unable to parse volume ID '$volid'\n";
247 }
248}
249
250sub volume_has_feature {
251 my ($cfg, $feature, $volid, $snap, $running) = @_;
252
253 my ($storeid, $volname) = parse_volume_id($volid, 1);
254 if ($storeid) {
255 my $scfg = storage_config($cfg, $storeid);
256 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
257 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
258 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
259 return undef;
260 } else {
261 return undef;
262 }
263}
264
265sub volume_snapshot_list {
266 my ($cfg, $volid, $prefix) = @_;
267
268 my ($storeid, $volname) = parse_volume_id($volid, 1);
269 if ($storeid) {
270 my $scfg = storage_config($cfg, $storeid);
271 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
272 return $plugin->volume_snapshot_list($scfg, $storeid, $volname, $prefix);
273 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
274 die "send file/device '$volid' is not possible\n";
275 } else {
276 die "unable to parse volume ID '$volid'\n";
277 }
278 # return an empty array if dataset does not exist.
279 # youngest snap first
280}
281
282sub get_image_dir {
283 my ($cfg, $storeid, $vmid) = @_;
284
285 my $scfg = storage_config($cfg, $storeid);
286 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
287
288 my $path = $plugin->get_subdir($scfg, 'images');
289
290 return $vmid ? "$path/$vmid" : $path;
291}
292
293sub get_private_dir {
294 my ($cfg, $storeid, $vmid) = @_;
295
296 my $scfg = storage_config($cfg, $storeid);
297 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
298
299 my $path = $plugin->get_subdir($scfg, 'rootdir');
300
301 return $vmid ? "$path/$vmid" : $path;
302}
303
304sub get_iso_dir {
305 my ($cfg, $storeid) = @_;
306
307 my $scfg = storage_config($cfg, $storeid);
308 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
309
310 return $plugin->get_subdir($scfg, 'iso');
311}
312
313sub get_vztmpl_dir {
314 my ($cfg, $storeid) = @_;
315
316 my $scfg = storage_config($cfg, $storeid);
317 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
318
319 return $plugin->get_subdir($scfg, 'vztmpl');
320}
321
322sub get_backup_dir {
323 my ($cfg, $storeid) = @_;
324
325 my $scfg = storage_config($cfg, $storeid);
326 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
327
328 return $plugin->get_subdir($scfg, 'backup');
329}
330
331# library implementation
332
333sub parse_vmid {
334 my $vmid = shift;
335
336 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
337
338 return int($vmid);
339}
340
341# NOTE: basename and basevmid are always undef for LVM-thin, where the
342# clone -> base reference is not encoded in the volume ID.
343# see note in PVE::Storage::LvmThinPlugin for details.
344sub parse_volname {
345 my ($cfg, $volid) = @_;
346
347 my ($storeid, $volname) = parse_volume_id($volid);
348
349 my $scfg = storage_config($cfg, $storeid);
350
351 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
352
353 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
354
355 return $plugin->parse_volname($volname);
356}
357
358sub parse_volume_id {
359 my ($volid, $noerr) = @_;
360
361 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
362}
363
364# test if we have read access to volid
365sub check_volume_access {
366 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
367
368 my ($sid, $volname) = parse_volume_id($volid, 1);
369 if ($sid) {
370 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
371 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
372 # we simply allow access
373 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
374 # we are owner - allow access
375 } elsif ($vtype eq 'backup' && $ownervm) {
376 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
377 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
378 } else {
379 # allow if we are Datastore administrator
380 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
381 }
382 } else {
383 die "Only root can pass arbitrary filesystem paths."
384 if $user ne 'root@pam';
385 }
386
387 return undef;
388}
389
390my $volume_is_base_and_used__no_lock = sub {
391 my ($scfg, $storeid, $plugin, $volname) = @_;
392
393 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
394 $plugin->parse_volname($volname);
395
396 if ($isBase) {
397 my $vollist = $plugin->list_images($storeid, $scfg);
398 foreach my $info (@$vollist) {
399 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
400 my $basename = undef;
401 my $basevmid = undef;
402
403 eval{
404 (undef, undef, undef, $basename, $basevmid) =
405 $plugin->parse_volname($tmpvolname);
406 };
407
408 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
409 return 1;
410 }
411 }
412 }
413 return 0;
414};
415
416# NOTE: this check does not work for LVM-thin, where the clone -> base
417# reference is not encoded in the volume ID.
418# see note in PVE::Storage::LvmThinPlugin for details.
419sub volume_is_base_and_used {
420 my ($cfg, $volid) = @_;
421
422 my ($storeid, $volname) = parse_volume_id($volid);
423 my $scfg = storage_config($cfg, $storeid);
424 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
425
426 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
427 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
428 });
429}
430
431# try to map a filesystem path to a volume identifier
432sub path_to_volume_id {
433 my ($cfg, $path) = @_;
434
435 my $ids = $cfg->{ids};
436
437 my ($sid, $volname) = parse_volume_id($path, 1);
438 if ($sid) {
439 if (my $scfg = $ids->{$sid}) {
440 if ($scfg->{path}) {
441 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
442 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
443 return ($vtype, $path);
444 }
445 }
446 return ('');
447 }
448
449 # Note: abs_path() return undef if $path doesn not exist
450 # for example when nfs storage is not mounted
451 $path = abs_path($path) || $path;
452
453 foreach my $sid (keys %$ids) {
454 my $scfg = $ids->{$sid};
455 next if !$scfg->{path};
456 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
457 my $imagedir = $plugin->get_subdir($scfg, 'images');
458 my $isodir = $plugin->get_subdir($scfg, 'iso');
459 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
460 my $backupdir = $plugin->get_subdir($scfg, 'backup');
461 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
462
463 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
464 my $vmid = $1;
465 my $name = $2;
466
467 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
468 foreach my $info (@$vollist) {
469 my ($storeid, $volname) = parse_volume_id($info->{volid});
470 my $volpath = $plugin->path($scfg, $volname, $storeid);
471 if ($volpath eq $path) {
472 return ('images', $info->{volid});
473 }
474 }
475 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
476 my $name = $1;
477 return ('iso', "$sid:iso/$name");
478 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
479 my $name = $1;
480 return ('vztmpl', "$sid:vztmpl/$name");
481 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
482 my $vmid = $1;
483 return ('rootdir', "$sid:rootdir/$vmid");
484 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
485 my $name = $1;
486 return ('iso', "$sid:backup/$name");
487 }
488 }
489
490 # can't map path to volume id
491 return ('');
492}
493
494sub path {
495 my ($cfg, $volid, $snapname) = @_;
496
497 my ($storeid, $volname) = parse_volume_id($volid);
498
499 my $scfg = storage_config($cfg, $storeid);
500
501 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
502 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
503 return wantarray ? ($path, $owner, $vtype) : $path;
504}
505
506sub abs_filesystem_path {
507 my ($cfg, $volid) = @_;
508
509 my $path;
510 if (PVE::Storage::parse_volume_id ($volid, 1)) {
511 PVE::Storage::activate_volumes($cfg, [ $volid ]);
512 $path = PVE::Storage::path($cfg, $volid);
513 } else {
514 if (-f $volid) {
515 my $abspath = abs_path($volid);
516 if ($abspath && $abspath =~ m|^(/.+)$|) {
517 $path = $1; # untaint any path
518 }
519 }
520 }
521
522 die "can't find file '$volid'\n" if !($path && -f $path);
523
524 return $path;
525}
526
527sub storage_migrate {
528 my ($cfg, $volid, $target_host, $target_storeid, $target_volname, $base_snapshot, $snapshot) = @_;
529
530 my ($storeid, $volname) = parse_volume_id($volid);
531 $target_volname = $volname if !$target_volname;
532
533 my $scfg = storage_config($cfg, $storeid);
534
535 # no need to migrate shared content
536 return if $storeid eq $target_storeid && $scfg->{shared};
537
538 my $tcfg = storage_config($cfg, $target_storeid);
539
540 my $target_volid = "${target_storeid}:${target_volname}";
541
542 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
543
544 my $sshoptions = "-o 'BatchMode=yes'";
545 my $ssh = "/usr/bin/ssh $sshoptions";
546
547 local $ENV{RSYNC_RSH} = $ssh;
548
549 my $no_incremental = sub {
550 my ($type) = @_;
551 die "incremental migration not supported on storage type $type\n"
552 if defined($base_snapshot);
553 };
554 my $no_snapshot = sub {
555 my ($type) = @_;
556 # $snapshot is currently only used by replication
557 die "replicating storage migration not supported on storage type $type\n"
558 if defined($snapshot);
559 };
560
561 # only implemented for file system based storage
562 if ($scfg->{path}) {
563 $no_incremental->($scfg->{type});
564 $no_snapshot->($scfg->{type});
565
566 if ($tcfg->{path}) {
567 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
568 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
569 my $src = $src_plugin->path($scfg, $volname, $storeid);
570 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
571
572 my $dirname = dirname($dst);
573
574 if ($tcfg->{shared}) { # we can do a local copy
575
576 run_command(['/bin/mkdir', '-p', $dirname]);
577
578 run_command(['/bin/cp', $src, $dst]);
579
580 } else {
581 run_command(['/usr/bin/ssh', "root\@${target_host}",
582 '/bin/mkdir', '-p', $dirname]);
583
584 # we use rsync with --sparse, so we can't use --inplace,
585 # so we remove file on the target if it already exists to
586 # save space
587 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
588 if ($format && ($format eq 'raw') && $size) {
589 run_command(['/usr/bin/ssh', "root\@${target_host}",
590 'rm', '-f', $dst],
591 outfunc => sub {});
592 }
593
594 my $cmd;
595 if ($format eq 'subvol') {
596 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
597 '-aH', '--delete', '--no-whole-file', '--inplace',
598 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
599 } else {
600 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
601 $src, "[root\@${target_host}]:$dst"];
602 }
603
604 my $percent = -1;
605
606 run_command($cmd, outfunc => sub {
607 my $line = shift;
608
609 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
610 if ($2 > $percent) {
611 $percent = $2;
612 print "rsync status: $1\n";
613 *STDOUT->flush();
614 }
615 } else {
616 print "$line\n";
617 *STDOUT->flush();
618 }
619 });
620 }
621 } else {
622 die "$errstr - target type '$tcfg->{type}' not implemented\n";
623 }
624
625 } elsif ($scfg->{type} eq 'zfspool') {
626
627 if ($tcfg->{type} eq 'zfspool') {
628
629 die "$errstr - pool on target does not have the same name as on source!"
630 if $tcfg->{pool} ne $scfg->{pool};
631
632 my $snapname = $snapshot // '__migration__';
633
634 my (undef, $volname) = parse_volname($cfg, $volid);
635 my $zfspath = "$scfg->{pool}\/$volname";
636
637 my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapname, $base_snapshot, 1);
638 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
639 my $format = $formats[0];
640
641 my $send = ['pvesm', 'export', $volid, $format, '-', '-snapshot', $snapname, '-with-snapshots', '1'];
642 my $recv = ['ssh', "root\@$target_host", '--', 'pvesm', 'import', $volid, $format, '-', '-with-snapshots', '1'];
643 my $free = ['ssh', "root\@$target_host", '--', 'pvesm', 'free', $volid, '-snapshot', $snapname]
644 if !defined($snapshot);
645
646 if (defined($base_snapshot)) {
647 # Check if the snapshot exists on the remote side:
648 push @$send, '-base', $base_snapshot;
649 push @$recv, '-base', $base_snapshot;
650 }
651
652 volume_snapshot($cfg, $volid, $snapname);
653 eval {
654 run_command([$send, $recv]);
655 };
656 my $err = $@;
657 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
658 if (!defined($snapshot)) {
659 eval { volume_snapshot_delete($cfg, $volid, $snapname, 0) };
660 warn "could not remove source snapshot: $@\n" if $@;
661 eval { run_command($free) };
662 warn "could not remove target snapshot: $@\n" if $@;
663 }
664 die $err if $err;
665 } else {
666 die "$errstr - target type $tcfg->{type} is not valid\n";
667 }
668
669 } elsif ($scfg->{type} eq 'lvmthin' || $scfg->{type} eq 'lvm') {
670 $no_incremental->($scfg->{type});
671 $no_snapshot->($scfg->{type});
672
673 if (($scfg->{type} eq $tcfg->{type}) &&
674 ($tcfg->{type} eq 'lvmthin' || $tcfg->{type} eq 'lvm')) {
675
676 my (undef, $volname, $vmid) = parse_volname($cfg, $volid);
677 my $size = volume_size_info($cfg, $volid, 5);
678 my $src = path($cfg, $volid);
679 my $dst = path($cfg, $target_volid);
680
681 run_command(['/usr/bin/ssh', "root\@${target_host}",
682 'pvesm', 'alloc', $target_storeid, $vmid,
683 $target_volname, int($size/1024)]);
684
685 eval {
686 if ($tcfg->{type} eq 'lvmthin') {
687 run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
688 "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
689 } else {
690 run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
691 "dd", "of=$dst", "bs=4k"]]);
692 }
693 };
694 if (my $err = $@) {
695 run_command(['/usr/bin/ssh', "root\@${target_host}",
696 'pvesm', 'free', $target_volid]);
697 die $err;
698 }
699 } else {
700 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
701 }
702 } else {
703 die "$errstr - source type '$scfg->{type}' not implemented\n";
704 }
705}
706
707sub vdisk_clone {
708 my ($cfg, $volid, $vmid, $snap) = @_;
709
710 my ($storeid, $volname) = parse_volume_id($volid);
711
712 my $scfg = storage_config($cfg, $storeid);
713
714 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
715
716 activate_storage($cfg, $storeid);
717
718 # lock shared storage
719 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
720 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
721 return "$storeid:$volname";
722 });
723}
724
725sub vdisk_create_base {
726 my ($cfg, $volid) = @_;
727
728 my ($storeid, $volname) = parse_volume_id($volid);
729
730 my $scfg = storage_config($cfg, $storeid);
731
732 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
733
734 activate_storage($cfg, $storeid);
735
736 # lock shared storage
737 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
738 my $volname = $plugin->create_base($storeid, $scfg, $volname);
739 return "$storeid:$volname";
740 });
741}
742
743sub vdisk_alloc {
744 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
745
746 die "no storage ID specified\n" if !$storeid;
747
748 PVE::JSONSchema::parse_storage_id($storeid);
749
750 my $scfg = storage_config($cfg, $storeid);
751
752 die "no VMID specified\n" if !$vmid;
753
754 $vmid = parse_vmid($vmid);
755
756 my $defformat = PVE::Storage::Plugin::default_format($scfg);
757
758 $fmt = $defformat if !$fmt;
759
760 activate_storage($cfg, $storeid);
761
762 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
763
764 # lock shared storage
765 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
766 my $old_umask = umask(umask|0037);
767 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
768 my $err = $@;
769 umask $old_umask;
770 die $err if $err;
771 return "$storeid:$volname";
772 });
773}
774
775sub vdisk_free {
776 my ($cfg, $volid) = @_;
777
778 my ($storeid, $volname) = parse_volume_id($volid);
779 my $scfg = storage_config($cfg, $storeid);
780 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
781
782 activate_storage($cfg, $storeid);
783
784 my $cleanup_worker;
785
786 # lock shared storage
787 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
788 # LVM-thin allows deletion of still referenced base volumes!
789 die "base volume '$volname' is still in use by linked clones\n"
790 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
791
792 my (undef, undef, undef, undef, undef, $isBase, $format) =
793 $plugin->parse_volname($volname);
794 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
795 });
796
797 return if !$cleanup_worker;
798
799 my $rpcenv = PVE::RPCEnvironment::get();
800 my $authuser = $rpcenv->get_user();
801
802 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
803}
804
805#list iso or openvz template ($tt = <iso|vztmpl|backup>)
806sub template_list {
807 my ($cfg, $storeid, $tt) = @_;
808
809 die "unknown template type '$tt'\n"
810 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
811
812 my $ids = $cfg->{ids};
813
814 storage_check_enabled($cfg, $storeid) if ($storeid);
815
816 my $res = {};
817
818 # query the storage
819
820 foreach my $sid (keys %$ids) {
821 next if $storeid && $storeid ne $sid;
822
823 my $scfg = $ids->{$sid};
824 my $type = $scfg->{type};
825
826 next if !storage_check_enabled($cfg, $sid, undef, 1);
827
828 next if $tt eq 'iso' && !$scfg->{content}->{iso};
829 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
830 next if $tt eq 'backup' && !$scfg->{content}->{backup};
831
832 activate_storage($cfg, $sid);
833
834 if ($scfg->{path}) {
835 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
836
837 my $path = $plugin->get_subdir($scfg, $tt);
838
839 foreach my $fn (<$path/*>) {
840
841 my $info;
842
843 if ($tt eq 'iso') {
844 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
845
846 $info = { volid => "$sid:iso/$1", format => 'iso' };
847
848 } elsif ($tt eq 'vztmpl') {
849 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
850
851 $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
852
853 } elsif ($tt eq 'backup') {
854 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
855
856 $info = { volid => "$sid:backup/$1", format => $2 };
857 }
858
859 $info->{size} = -s $fn;
860
861 push @{$res->{$sid}}, $info;
862 }
863
864 }
865
866 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
867 }
868
869 return $res;
870}
871
872
873sub vdisk_list {
874 my ($cfg, $storeid, $vmid, $vollist) = @_;
875
876 my $ids = $cfg->{ids};
877
878 storage_check_enabled($cfg, $storeid) if ($storeid);
879
880 my $res = {};
881
882 # prepare/activate/refresh all storages
883
884 my $storage_list = [];
885 if ($vollist) {
886 foreach my $volid (@$vollist) {
887 my ($sid, undef) = parse_volume_id($volid);
888 next if !defined($ids->{$sid});
889 next if !storage_check_enabled($cfg, $sid, undef, 1);
890 push @$storage_list, $sid;
891 }
892 } else {
893 foreach my $sid (keys %$ids) {
894 next if $storeid && $storeid ne $sid;
895 next if !storage_check_enabled($cfg, $sid, undef, 1);
896 push @$storage_list, $sid;
897 }
898 }
899
900 my $cache = {};
901
902 activate_storage_list($cfg, $storage_list, $cache);
903
904 foreach my $sid (keys %$ids) {
905 next if $storeid && $storeid ne $sid;
906 next if !storage_check_enabled($cfg, $sid, undef, 1);
907
908 my $scfg = $ids->{$sid};
909 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
910 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
911 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
912 }
913
914 return $res;
915}
916
917sub volume_list {
918 my ($cfg, $storeid, $vmid, $content) = @_;
919
920 my @ctypes = qw(images vztmpl iso backup);
921
922 my $cts = $content ? [ $content ] : [ @ctypes ];
923
924 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
925
926 my $res = [];
927 foreach my $ct (@$cts) {
928 my $data;
929 if ($ct eq 'images') {
930 $data = vdisk_list($cfg, $storeid, $vmid);
931 } elsif ($ct eq 'iso' && !defined($vmid)) {
932 $data = template_list($cfg, $storeid, 'iso');
933 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
934 $data = template_list ($cfg, $storeid, 'vztmpl');
935 } elsif ($ct eq 'backup') {
936 $data = template_list ($cfg, $storeid, 'backup');
937 foreach my $item (@{$data->{$storeid}}) {
938 if (defined($vmid)) {
939 @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
940 }
941 }
942 }
943
944 next if !$data || !$data->{$storeid};
945
946 foreach my $item (@{$data->{$storeid}}) {
947 $item->{content} = $ct;
948 push @$res, $item;
949 }
950 }
951
952 return $res;
953}
954
955sub uevent_seqnum {
956
957 my $filename = "/sys/kernel/uevent_seqnum";
958
959 my $seqnum = 0;
960 if (my $fh = IO::File->new($filename, "r")) {
961 my $line = <$fh>;
962 if ($line =~ m/^(\d+)$/) {
963 $seqnum = int($1);
964 }
965 close ($fh);
966 }
967 return $seqnum;
968}
969
970sub activate_storage {
971 my ($cfg, $storeid, $cache) = @_;
972
973 $cache = {} if !$cache;
974
975 my $scfg = storage_check_enabled($cfg, $storeid);
976
977 return if $cache->{activated}->{$storeid};
978
979 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
980
981 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
982
983 if ($scfg->{base}) {
984 my ($baseid, undef) = parse_volume_id ($scfg->{base});
985 activate_storage($cfg, $baseid, $cache);
986 }
987
988 if (!$plugin->check_connection($storeid, $scfg)) {
989 die "storage '$storeid' is not online\n";
990 }
991
992 $plugin->activate_storage($storeid, $scfg, $cache);
993
994 my $newseq = uevent_seqnum ();
995
996 # only call udevsettle if there are events
997 if ($newseq > $cache->{uevent_seqnum}) {
998 my $timeout = 30;
999 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1000 $cache->{uevent_seqnum} = $newseq;
1001 }
1002
1003 $cache->{activated}->{$storeid} = 1;
1004}
1005
1006sub activate_storage_list {
1007 my ($cfg, $storeid_list, $cache) = @_;
1008
1009 $cache = {} if !$cache;
1010
1011 foreach my $storeid (@$storeid_list) {
1012 activate_storage($cfg, $storeid, $cache);
1013 }
1014}
1015
1016sub deactivate_storage {
1017 my ($cfg, $storeid) = @_;
1018
1019 my $scfg = storage_config ($cfg, $storeid);
1020 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1021
1022 my $cache = {};
1023 $plugin->deactivate_storage($storeid, $scfg, $cache);
1024}
1025
1026sub activate_volumes {
1027 my ($cfg, $vollist, $snapname) = @_;
1028
1029 return if !($vollist && scalar(@$vollist));
1030
1031 my $storagehash = {};
1032 foreach my $volid (@$vollist) {
1033 my ($storeid, undef) = parse_volume_id($volid);
1034 $storagehash->{$storeid} = 1;
1035 }
1036
1037 my $cache = {};
1038
1039 activate_storage_list($cfg, [keys %$storagehash], $cache);
1040
1041 foreach my $volid (@$vollist) {
1042 my ($storeid, $volname) = parse_volume_id($volid);
1043 my $scfg = storage_config($cfg, $storeid);
1044 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1045 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1046 }
1047}
1048
1049sub deactivate_volumes {
1050 my ($cfg, $vollist, $snapname) = @_;
1051
1052 return if !($vollist && scalar(@$vollist));
1053
1054 my $cache = {};
1055
1056 my @errlist = ();
1057 foreach my $volid (@$vollist) {
1058 my ($storeid, $volname) = parse_volume_id($volid);
1059
1060 my $scfg = storage_config($cfg, $storeid);
1061 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1062
1063 eval {
1064 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1065 };
1066 if (my $err = $@) {
1067 warn $err;
1068 push @errlist, $volid;
1069 }
1070 }
1071
1072 die "volume deactivation failed: " . join(' ', @errlist)
1073 if scalar(@errlist);
1074}
1075
1076sub storage_info {
1077 my ($cfg, $content) = @_;
1078
1079 my $ids = $cfg->{ids};
1080
1081 my $info = {};
1082
1083 my @ctypes = PVE::Tools::split_list($content);
1084
1085 my $slist = [];
1086 foreach my $storeid (keys %$ids) {
1087
1088 next if !storage_check_enabled($cfg, $storeid, undef, 1);
1089
1090 if (defined($content)) {
1091 my $want_ctype = 0;
1092 foreach my $ctype (@ctypes) {
1093 if ($ids->{$storeid}->{content}->{$ctype}) {
1094 $want_ctype = 1;
1095 last;
1096 }
1097 }
1098 next if !$want_ctype;
1099 }
1100
1101 my $type = $ids->{$storeid}->{type};
1102
1103 $info->{$storeid} = {
1104 type => $type,
1105 total => 0,
1106 avail => 0,
1107 used => 0,
1108 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1109 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
1110 active => 0,
1111 };
1112
1113 push @$slist, $storeid;
1114 }
1115
1116 my $cache = {};
1117
1118 foreach my $storeid (keys %$ids) {
1119 my $scfg = $ids->{$storeid};
1120 next if !$info->{$storeid};
1121
1122 eval { activate_storage($cfg, $storeid, $cache); };
1123 if (my $err = $@) {
1124 warn $err;
1125 next;
1126 }
1127
1128 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1129 my ($total, $avail, $used, $active);
1130 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1131 warn $@ if $@;
1132 next if !$active;
1133 $info->{$storeid}->{total} = int($total);
1134 $info->{$storeid}->{avail} = int($avail);
1135 $info->{$storeid}->{used} = int($used);
1136 $info->{$storeid}->{active} = $active;
1137 }
1138
1139 return $info;
1140}
1141
1142sub resolv_server {
1143 my ($server) = @_;
1144
1145 my ($packed_ip, $family);
1146 eval {
1147 my @res = PVE::Tools::getaddrinfo_all($server);
1148 $family = $res[0]->{family};
1149 $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
1150 };
1151 if (defined $packed_ip) {
1152 return Socket::inet_ntop($family, $packed_ip);
1153 }
1154 return undef;
1155}
1156
1157sub scan_nfs {
1158 my ($server_in) = @_;
1159
1160 my $server;
1161 if (!($server = resolv_server ($server_in))) {
1162 die "unable to resolve address for server '${server_in}'\n";
1163 }
1164
1165 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1166
1167 my $res = {};
1168 run_command($cmd, outfunc => sub {
1169 my $line = shift;
1170
1171 # note: howto handle white spaces in export path??
1172 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1173 $res->{$1} = $2;
1174 }
1175 });
1176
1177 return $res;
1178}
1179
1180sub scan_zfs {
1181
1182 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1183
1184 my $res = [];
1185 run_command($cmd, outfunc => sub {
1186 my $line = shift;
1187
1188 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1189 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1190 my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str);
1191 my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str);
1192 # ignore subvolumes generated by our ZFSPoolPlugin
1193 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1194 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1195 push @$res, { pool => $pool, size => $size, free => $size-$used };
1196 }
1197 });
1198
1199 return $res;
1200}
1201
1202sub resolv_portal {
1203 my ($portal, $noerr) = @_;
1204
1205 my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
1206 if ($server) {
1207 if (my $ip = resolv_server($server)) {
1208 $server = $ip;
1209 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1210 return $port ? "$server:$port" : $server;
1211 }
1212 }
1213 return undef if $noerr;
1214
1215 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
1216}
1217
1218# idea is from usbutils package (/usr/bin/usb-devices) script
1219sub __scan_usb_device {
1220 my ($res, $devpath, $parent, $level) = @_;
1221
1222 return if ! -d $devpath;
1223 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1224 my $port = $level ? int($1 - 1) : 0;
1225
1226 my $busnum = int(file_read_firstline("$devpath/busnum"));
1227 my $devnum = int(file_read_firstline("$devpath/devnum"));
1228
1229 my $d = {
1230 port => $port,
1231 level => $level,
1232 busnum => $busnum,
1233 devnum => $devnum,
1234 speed => file_read_firstline("$devpath/speed"),
1235 class => hex(file_read_firstline("$devpath/bDeviceClass")),
1236 vendid => file_read_firstline("$devpath/idVendor"),
1237 prodid => file_read_firstline("$devpath/idProduct"),
1238 };
1239
1240 if ($level) {
1241 my $usbpath = $devpath;
1242 $usbpath =~ s|^.*/\d+\-||;
1243 $d->{usbpath} = $usbpath;
1244 }
1245
1246 my $product = file_read_firstline("$devpath/product");
1247 $d->{product} = $product if $product;
1248
1249 my $manu = file_read_firstline("$devpath/manufacturer");
1250 $d->{manufacturer} = $manu if $manu;
1251
1252 my $serial => file_read_firstline("$devpath/serial");
1253 $d->{serial} = $serial if $serial;
1254
1255 push @$res, $d;
1256
1257 foreach my $subdev (<$devpath/$busnum-*>) {
1258 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
1259 __scan_usb_device($res, $subdev, $devnum, $level + 1);
1260 }
1261
1262};
1263
1264sub scan_usb {
1265
1266 my $devlist = [];
1267
1268 foreach my $device (</sys/bus/usb/devices/usb*>) {
1269 __scan_usb_device($devlist, $device, 0, 0);
1270 }
1271
1272 return $devlist;
1273}
1274
1275sub scan_iscsi {
1276 my ($portal_in) = @_;
1277
1278 my $portal;
1279 if (!($portal = resolv_portal($portal_in))) {
1280 die "unable to parse/resolve portal address '${portal_in}'\n";
1281 }
1282
1283 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
1284}
1285
1286sub storage_default_format {
1287 my ($cfg, $storeid) = @_;
1288
1289 my $scfg = storage_config ($cfg, $storeid);
1290
1291 return PVE::Storage::Plugin::default_format($scfg);
1292}
1293
1294sub vgroup_is_used {
1295 my ($cfg, $vgname) = @_;
1296
1297 foreach my $storeid (keys %{$cfg->{ids}}) {
1298 my $scfg = storage_config($cfg, $storeid);
1299 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
1300 return 1;
1301 }
1302 }
1303
1304 return undef;
1305}
1306
1307sub target_is_used {
1308 my ($cfg, $target) = @_;
1309
1310 foreach my $storeid (keys %{$cfg->{ids}}) {
1311 my $scfg = storage_config($cfg, $storeid);
1312 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
1313 return 1;
1314 }
1315 }
1316
1317 return undef;
1318}
1319
1320sub volume_is_used {
1321 my ($cfg, $volid) = @_;
1322
1323 foreach my $storeid (keys %{$cfg->{ids}}) {
1324 my $scfg = storage_config($cfg, $storeid);
1325 if ($scfg->{base} && $scfg->{base} eq $volid) {
1326 return 1;
1327 }
1328 }
1329
1330 return undef;
1331}
1332
1333sub storage_is_used {
1334 my ($cfg, $storeid) = @_;
1335
1336 foreach my $sid (keys %{$cfg->{ids}}) {
1337 my $scfg = storage_config($cfg, $sid);
1338 next if !$scfg->{base};
1339 my ($st) = parse_volume_id($scfg->{base});
1340 return 1 if $st && $st eq $storeid;
1341 }
1342
1343 return undef;
1344}
1345
1346sub foreach_volid {
1347 my ($list, $func) = @_;
1348
1349 return if !$list;
1350
1351 foreach my $sid (keys %$list) {
1352 foreach my $info (@{$list->{$sid}}) {
1353 my $volid = $info->{volid};
1354 my ($sid1, $volname) = parse_volume_id($volid, 1);
1355 if ($sid1 && $sid1 eq $sid) {
1356 &$func ($volid, $sid, $info);
1357 } else {
1358 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1359 }
1360 }
1361 }
1362}
1363
1364sub extract_vzdump_config_tar {
1365 my ($archive, $conf_re) = @_;
1366
1367 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
1368
1369 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1370 die "unable to open file '$archive'\n";
1371
1372 my $file;
1373 while (defined($file = <$fh>)) {
1374 if ($file =~ $conf_re) {
1375 $file = $1; # untaint
1376 last;
1377 }
1378 }
1379
1380 kill 15, $pid;
1381 waitpid $pid, 0;
1382 close $fh;
1383
1384 die "ERROR: archive contains no configuration file\n" if !$file;
1385 chomp $file;
1386
1387 my $raw = '';
1388 my $out = sub {
1389 my $output = shift;
1390 $raw .= "$output\n";
1391 };
1392
1393 PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out);
1394
1395 return wantarray ? ($raw, $file) : $raw;
1396}
1397
1398sub extract_vzdump_config_vma {
1399 my ($archive, $comp) = @_;
1400
1401 my $cmd;
1402 my $raw = '';
1403 my $out = sub {
1404 my $output = shift;
1405 $raw .= "$output\n";
1406 };
1407
1408
1409 if ($comp) {
1410 my $uncomp;
1411 if ($comp eq 'gz') {
1412 $uncomp = ["zcat", $archive];
1413 } elsif ($comp eq 'lzo') {
1414 $uncomp = ["lzop", "-d", "-c", $archive];
1415 } else {
1416 die "unknown compression method '$comp'\n";
1417 }
1418 $cmd = [$uncomp, ["vma", "config", "-"]];
1419
1420 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1421 # closed early by vma, detect this and ignore the exit code later
1422 my $broken_pipe;
1423 my $errstring;
1424 my $err = sub {
1425 my $output = shift;
1426 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1427 $broken_pipe = 1;
1428 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1429 $errstring = "Failed to extract config from VMA archive: $output\n";
1430 }
1431 };
1432
1433 # in other cases, the pipeline will exit with exit code 141
1434 # because of the broken pipe, handle / ignore this as well
1435 my $rc;
1436 eval {
1437 $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1);
1438 };
1439 my $rerr = $@;
1440
1441 # use exit code if no stderr output and not just broken pipe
1442 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1443 die "$rerr\n" if $rerr;
1444 die "config extraction failed with exit code $rc\n";
1445 }
1446 die "$errstring\n" if $errstring;
1447 } else {
1448 # simple case without compression and weird piping behaviour
1449 PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out);
1450 }
1451
1452 return wantarray ? ($raw, undef) : $raw;
1453}
1454
1455sub extract_vzdump_config {
1456 my ($cfg, $volid) = @_;
1457
1458 my $archive = abs_filesystem_path($cfg, $volid);
1459
1460 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1461 return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1462 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1463 my $format;
1464 my $comp;
1465 if ($7 eq 'tgz') {
1466 $format = 'tar';
1467 $comp = 'gz';
1468 } else {
1469 $format = $9;
1470 $comp = $11 if defined($11);
1471 }
1472
1473 if ($format eq 'tar') {
1474 return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!);
1475 } else {
1476 return extract_vzdump_config_vma($archive, $comp);
1477 }
1478 } else {
1479 die "cannot determine backup guest type for backup archive '$volid'\n";
1480 }
1481}
1482
1483sub volume_export {
1484 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1485
1486 my ($storeid, $volname) = parse_volume_id($volid, 1);
1487 die "cannot export volume '$volid'\n" if !$storeid;
1488 my $scfg = storage_config($cfg, $storeid);
1489 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1490 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1491 $snapshot, $base_snapshot, $with_snapshots);
1492}
1493
1494sub volume_import {
1495 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
1496
1497 my ($storeid, $volname) = parse_volume_id($volid, 1);
1498 die "cannot import into volume '$volid'\n" if !$storeid;
1499 my $scfg = storage_config($cfg, $storeid);
1500 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1501 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1502 $base_snapshot, $with_snapshots);
1503}
1504
1505sub volume_export_formats {
1506 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1507
1508 my ($storeid, $volname) = parse_volume_id($volid, 1);
1509 return if !$storeid;
1510 my $scfg = storage_config($cfg, $storeid);
1511 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1512 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1513 $base_snapshot, $with_snapshots);
1514}
1515
1516sub volume_import_formats {
1517 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1518
1519 my ($storeid, $volname) = parse_volume_id($volid, 1);
1520 return if !$storeid;
1521 my $scfg = storage_config($cfg, $storeid);
1522 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1523 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1524 $base_snapshot, $with_snapshots);
1525}
1526
1527sub volume_transfer_formats {
1528 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1529 my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1530 my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1531 my %import_hash = map { $_ => 1 } @import_formats;
1532 my @common = grep { $import_hash{$_} } @export_formats;
1533 return @common;
1534}
1535
1536# bash completion helper
1537
1538sub complete_storage {
1539 my ($cmdname, $pname, $cvalue) = @_;
1540
1541 my $cfg = PVE::Storage::config();
1542
1543 return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ];
1544}
1545
1546sub complete_storage_enabled {
1547 my ($cmdname, $pname, $cvalue) = @_;
1548
1549 my $res = [];
1550
1551 my $cfg = PVE::Storage::config();
1552 foreach my $sid (keys %{$cfg->{ids}}) {
1553 next if !storage_check_enabled($cfg, $sid, undef, 1);
1554 push @$res, $sid;
1555 }
1556 return $res;
1557}
1558
1559sub complete_content_type {
1560 my ($cmdname, $pname, $cvalue) = @_;
1561
1562 return [qw(rootdir images vztmpl iso backup)];
1563}
1564
1565sub complete_volume {
1566 my ($cmdname, $pname, $cvalue) = @_;
1567
1568 my $cfg = config();
1569
1570 my $storage_list = complete_storage_enabled();
1571
1572 if ($cvalue =~ m/^([^:]+):/) {
1573 $storage_list = [ $1 ];
1574 } else {
1575 if (scalar(@$storage_list) > 1) {
1576 # only list storage IDs to avoid large listings
1577 my $res = [];
1578 foreach my $storeid (@$storage_list) {
1579 # Hack: simply return 2 artificial values, so that
1580 # completions does not finish
1581 push @$res, "$storeid:volname", "$storeid:...";
1582 }
1583 return $res;
1584 }
1585 }
1586
1587 my $res = [];
1588 foreach my $storeid (@$storage_list) {
1589 my $vollist = PVE::Storage::volume_list($cfg, $storeid);
1590
1591 foreach my $item (@$vollist) {
1592 push @$res, $item->{volid};
1593 }
1594 }
1595
1596 return $res;
1597}
1598
15991;