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