16 use Time
::Local
qw(timelocal);
18 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
19 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
20 use PVE
::DataCenterConfig
;
21 use PVE
::Exception
qw(raise_param_exc raise);
24 use PVE
::RPCEnvironment
;
27 use PVE
::Storage
::Plugin
;
28 use PVE
::Storage
::DirPlugin
;
29 use PVE
::Storage
::LVMPlugin
;
30 use PVE
::Storage
::LvmThinPlugin
;
31 use PVE
::Storage
::NFSPlugin
;
32 use PVE
::Storage
::CIFSPlugin
;
33 use PVE
::Storage
::ISCSIPlugin
;
34 use PVE
::Storage
::RBDPlugin
;
35 use PVE
::Storage
::CephFSPlugin
;
36 use PVE
::Storage
::ISCSIDirectPlugin
;
37 use PVE
::Storage
::GlusterfsPlugin
;
38 use PVE
::Storage
::ZFSPoolPlugin
;
39 use PVE
::Storage
::ZFSPlugin
;
40 use PVE
::Storage
::DRBDPlugin
;
41 use PVE
::Storage
::PBSPlugin
;
43 # Storage API version. Increment it on changes in storage API interface.
44 use constant APIVER
=> 8;
45 # Age is the number of versions we're backward compatible with.
46 # This is like having 'current=APIVER' and age='APIAGE' in libtool,
47 # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
48 use constant APIAGE
=> 7;
50 # load standard plugins
51 PVE
::Storage
::DirPlugin-
>register();
52 PVE
::Storage
::LVMPlugin-
>register();
53 PVE
::Storage
::LvmThinPlugin-
>register();
54 PVE
::Storage
::NFSPlugin-
>register();
55 PVE
::Storage
::CIFSPlugin-
>register();
56 PVE
::Storage
::ISCSIPlugin-
>register();
57 PVE
::Storage
::RBDPlugin-
>register();
58 PVE
::Storage
::CephFSPlugin-
>register();
59 PVE
::Storage
::ISCSIDirectPlugin-
>register();
60 PVE
::Storage
::GlusterfsPlugin-
>register();
61 PVE
::Storage
::ZFSPoolPlugin-
>register();
62 PVE
::Storage
::ZFSPlugin-
>register();
63 PVE
::Storage
::DRBDPlugin-
>register();
64 PVE
::Storage
::PBSPlugin-
>register();
66 # load third-party plugins
67 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
68 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
70 my $modname = 'PVE::Storage::Custom::' . $file;
71 $modname =~ s!\.pm$!!;
72 $file = 'PVE/Storage/Custom/' . $file;
77 # Check perl interface:
78 die "not derived from PVE::Storage::Plugin\n"
79 if !$modname->isa('PVE::Storage::Plugin');
80 die "does not provide an api() method\n"
81 if !$modname->can('api');
82 # Check storage API version and that file is really storage plugin.
83 my $version = $modname->api();
84 die "implements an API version newer than current ($version > " . APIVER
. ")\n"
86 my $min_version = (APIVER
- APIAGE
);
87 die "API version too old, please update the plugin ($version < $min_version)\n"
88 if $version < $min_version;
92 # If we got this far and the API version is not the same, make some
94 warn "Plugin \"$modname\" is implementing an older storage API
, an upgrade
is recommended
\n"
95 if $version != APIVER;
98 warn "Error loading storage plugin
\"$modname\": $@";
103 # initialize all plugins
104 PVE
::Storage
::Plugin-
>init();
106 my $UDEVADM = '/sbin/udevadm';
108 our $iso_extension_re = qr/\.(?:iso|img)/i;
110 # PVE::Storage utility functions
113 return cfs_read_file
("storage.cfg");
119 cfs_write_file
('storage.cfg', $cfg);
122 sub lock_storage_config
{
123 my ($code, $errmsg) = @_;
125 cfs_lock_file
("storage.cfg", undef, $code);
128 $errmsg ?
die "$errmsg: $err" : die $err;
132 # FIXME remove maxfiles for PVE 7.0
133 my $convert_maxfiles_to_prune_backups = sub {
138 my $maxfiles = delete $scfg->{maxfiles
};
140 if (!defined($scfg->{'prune-backups'}) && defined($maxfiles)) {
143 $prune_backups = { 'keep-last' => $maxfiles };
144 } else { # maxfiles 0 means no limit
145 $prune_backups = { 'keep-all' => 1 };
147 $scfg->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
155 my ($cfg, $storeid, $noerr) = @_;
157 die "no storage ID specified\n" if !$storeid;
159 my $scfg = $cfg->{ids
}->{$storeid};
161 die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg);
163 $convert_maxfiles_to_prune_backups->($scfg);
168 sub storage_check_node
{
169 my ($cfg, $storeid, $node, $noerr) = @_;
171 my $scfg = storage_config
($cfg, $storeid);
173 if ($scfg->{nodes
}) {
174 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
175 if (!$scfg->{nodes
}->{$node}) {
176 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
184 sub storage_check_enabled
{
185 my ($cfg, $storeid, $node, $noerr) = @_;
187 my $scfg = storage_config
($cfg, $storeid);
189 if ($scfg->{disable
}) {
190 die "storage '$storeid' is disabled\n" if !$noerr;
194 return storage_check_node
($cfg, $storeid, $node, $noerr);
197 # storage_can_replicate:
198 # return true if storage supports replication
199 # (volumes alocated with vdisk_alloc() has replication feature)
200 sub storage_can_replicate
{
201 my ($cfg, $storeid, $format) = @_;
203 my $scfg = storage_config
($cfg, $storeid);
204 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
205 return $plugin->storage_can_replicate($scfg, $storeid, $format);
211 return keys %{$cfg->{ids
}};
215 my ($filename, $timeout) = @_;
217 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
220 sub get_volume_notes
{
221 my ($cfg, $volid, $timeout) = @_;
223 my ($storeid, $volname) = parse_volume_id
($volid);
224 my $scfg = storage_config
($cfg, $storeid);
225 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
227 return $plugin->get_volume_notes($scfg, $storeid, $volname, $timeout);
230 sub update_volume_notes
{
231 my ($cfg, $volid, $notes, $timeout) = @_;
233 my ($storeid, $volname) = parse_volume_id
($volid);
234 my $scfg = storage_config
($cfg, $storeid);
235 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
237 $plugin->update_volume_notes($scfg, $storeid, $volname, $notes, $timeout);
240 sub volume_size_info
{
241 my ($cfg, $volid, $timeout) = @_;
243 my ($storeid, $volname) = parse_volume_id
($volid, 1);
245 my $scfg = storage_config
($cfg, $storeid);
246 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
247 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
248 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
249 return file_size_info
($volid, $timeout);
256 my ($cfg, $volid, $size, $running) = @_;
258 my $padding = (1024 - $size % 1024) % 1024;
259 $size = $size + $padding;
261 my ($storeid, $volname) = parse_volume_id
($volid, 1);
263 my $scfg = storage_config
($cfg, $storeid);
264 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
265 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
266 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
267 die "resize file/device '$volid' is not possible\n";
269 die "unable to parse volume ID '$volid'\n";
273 sub volume_rollback_is_possible
{
274 my ($cfg, $volid, $snap) = @_;
276 my ($storeid, $volname) = parse_volume_id
($volid, 1);
278 my $scfg = storage_config
($cfg, $storeid);
279 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
280 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
281 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
282 die "snapshot rollback file/device '$volid' is not possible\n";
284 die "unable to parse volume ID '$volid'\n";
288 sub volume_snapshot
{
289 my ($cfg, $volid, $snap) = @_;
291 my ($storeid, $volname) = parse_volume_id
($volid, 1);
293 my $scfg = storage_config
($cfg, $storeid);
294 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
295 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
296 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
297 die "snapshot file/device '$volid' is not possible\n";
299 die "unable to parse volume ID '$volid'\n";
303 sub volume_snapshot_rollback
{
304 my ($cfg, $volid, $snap) = @_;
306 my ($storeid, $volname) = parse_volume_id
($volid, 1);
308 my $scfg = storage_config
($cfg, $storeid);
309 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
310 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
311 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
312 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
313 die "snapshot rollback file/device '$volid' is not possible\n";
315 die "unable to parse volume ID '$volid'\n";
319 sub volume_snapshot_delete
{
320 my ($cfg, $volid, $snap, $running) = @_;
322 my ($storeid, $volname) = parse_volume_id
($volid, 1);
324 my $scfg = storage_config
($cfg, $storeid);
325 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
326 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
327 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
328 die "snapshot delete file/device '$volid' is not possible\n";
330 die "unable to parse volume ID '$volid'\n";
334 # check if a filesystem on top of a volume needs to flush its journal for
335 # consistency (see fsfreeze(8)) before a snapshot is taken - needed for
336 # container mountpoints
337 sub volume_snapshot_needs_fsfreeze
{
338 my ($cfg, $volid) = @_;
340 my ($storeid, $volname) = parse_volume_id
($volid);
341 my $scfg = storage_config
($cfg, $storeid);
342 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
343 return $plugin->volume_snapshot_needs_fsfreeze();
346 # check if a volume or snapshot supports a given feature
348 # clone - linked clone is possible
349 # copy - full clone is possible
350 # replicate - replication is possible
351 # snapshot - taking a snapshot is possible
352 # sparseinit - volume is sparsely initialized
353 # template - conversion to base image is possible
354 # $snap - check if the feature is supported for a given snapshot
355 # $running - if the guest owning the volume is running
356 # $opts - hash with further options:
357 # valid_target_formats - list of formats for the target of a copy/clone
358 # operation that the caller could work with. The
359 # format of $volid is always considered valid and if
360 # no list is specified, all formats are considered valid.
361 sub volume_has_feature
{
362 my ($cfg, $feature, $volid, $snap, $running, $opts) = @_;
364 my ($storeid, $volname) = parse_volume_id
($volid, 1);
366 my $scfg = storage_config
($cfg, $storeid);
367 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
368 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts);
369 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
376 sub volume_snapshot_list
{
377 my ($cfg, $volid) = @_;
379 my ($storeid, $volname) = parse_volume_id
($volid, 1);
381 my $scfg = storage_config
($cfg, $storeid);
382 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
383 return $plugin->volume_snapshot_list($scfg, $storeid, $volname);
384 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
385 die "send file/device '$volid' is not possible\n";
387 die "unable to parse volume ID '$volid'\n";
389 # return an empty array if dataset does not exist.
393 my ($cfg, $storeid, $vmid) = @_;
395 my $scfg = storage_config
($cfg, $storeid);
396 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
398 my $path = $plugin->get_subdir($scfg, 'images');
400 return $vmid ?
"$path/$vmid" : $path;
403 sub get_private_dir
{
404 my ($cfg, $storeid, $vmid) = @_;
406 my $scfg = storage_config
($cfg, $storeid);
407 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
409 my $path = $plugin->get_subdir($scfg, 'rootdir');
411 return $vmid ?
"$path/$vmid" : $path;
415 my ($cfg, $storeid) = @_;
417 my $scfg = storage_config
($cfg, $storeid);
418 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
420 return $plugin->get_subdir($scfg, 'iso');
424 my ($cfg, $storeid) = @_;
426 my $scfg = storage_config
($cfg, $storeid);
427 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
429 return $plugin->get_subdir($scfg, 'vztmpl');
433 my ($cfg, $storeid) = @_;
435 my $scfg = storage_config
($cfg, $storeid);
436 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
438 return $plugin->get_subdir($scfg, 'backup');
441 # library implementation
446 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
451 # NOTE: basename and basevmid are always undef for LVM-thin, where the
452 # clone -> base reference is not encoded in the volume ID.
453 # see note in PVE::Storage::LvmThinPlugin for details.
455 my ($cfg, $volid) = @_;
457 my ($storeid, $volname) = parse_volume_id
($volid);
459 my $scfg = storage_config
($cfg, $storeid);
461 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
463 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
465 return $plugin->parse_volname($volname);
468 sub parse_volume_id
{
469 my ($volid, $noerr) = @_;
471 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
474 # test if we have read access to volid
475 sub check_volume_access
{
476 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
478 my ($sid, $volname) = parse_volume_id
($volid, 1);
480 my ($vtype, undef, $ownervm) = parse_volname
($cfg, $volid);
481 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
482 # require at least read access to storage, (custom) templates/ISOs could be sensitive
483 $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']);
484 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
485 # we are owner - allow access
486 } elsif ($vtype eq 'backup' && $ownervm) {
487 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
488 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
490 # allow if we are Datastore administrator
491 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
494 die "Only root can pass arbitrary filesystem paths."
495 if $user ne 'root@pam';
501 my $volume_is_base_and_used__no_lock = sub {
502 my ($scfg, $storeid, $plugin, $volname) = @_;
504 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
505 $plugin->parse_volname($volname);
508 my $vollist = $plugin->list_images($storeid, $scfg);
509 foreach my $info (@$vollist) {
510 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
511 my $basename = undef;
512 my $basevmid = undef;
515 (undef, undef, undef, $basename, $basevmid) =
516 $plugin->parse_volname($tmpvolname);
519 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
527 # NOTE: this check does not work for LVM-thin, where the clone -> base
528 # reference is not encoded in the volume ID.
529 # see note in PVE::Storage::LvmThinPlugin for details.
530 sub volume_is_base_and_used
{
531 my ($cfg, $volid) = @_;
533 my ($storeid, $volname) = parse_volume_id
($volid);
534 my $scfg = storage_config
($cfg, $storeid);
535 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
537 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
538 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
542 # try to map a filesystem path to a volume identifier
543 sub path_to_volume_id
{
544 my ($cfg, $path) = @_;
546 my $ids = $cfg->{ids
};
548 my ($sid, $volname) = parse_volume_id
($path, 1);
550 if (my $scfg = $ids->{$sid}) {
552 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
553 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
554 return ($vtype, $path);
560 # Note: abs_path() return undef if $path doesn not exist
561 # for example when nfs storage is not mounted
562 $path = abs_path
($path) || $path;
564 foreach my $sid (keys %$ids) {
565 my $scfg = $ids->{$sid};
566 next if !$scfg->{path
};
567 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
568 my $imagedir = $plugin->get_subdir($scfg, 'images');
569 my $isodir = $plugin->get_subdir($scfg, 'iso');
570 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
571 my $backupdir = $plugin->get_subdir($scfg, 'backup');
572 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
573 my $snippetsdir = $plugin->get_subdir($scfg, 'snippets');
575 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
579 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
580 foreach my $info (@$vollist) {
581 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
582 my $volpath = $plugin->path($scfg, $volname, $storeid);
583 if ($volpath eq $path) {
584 return ('images', $info->{volid
});
587 } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) {
589 return ('iso', "$sid:iso/$name");
590 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
592 return ('vztmpl', "$sid:vztmpl/$name");
593 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
595 return ('rootdir', "$sid:rootdir/$vmid");
596 } elsif ($path =~ m!^$backupdir/([^/]+\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) {
598 return ('backup', "$sid:backup/$name");
599 } elsif ($path =~ m!^$snippetsdir/([^/]+)$!) {
601 return ('snippets', "$sid:snippets/$name");
605 # can't map path to volume id
610 my ($cfg, $volid, $snapname) = @_;
612 my ($storeid, $volname) = parse_volume_id
($volid);
614 my $scfg = storage_config
($cfg, $storeid);
616 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
617 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
618 return wantarray ?
($path, $owner, $vtype) : $path;
621 sub abs_filesystem_path
{
622 my ($cfg, $volid) = @_;
625 if (parse_volume_id
($volid, 1)) {
626 activate_volumes
($cfg, [ $volid ]);
627 $path = PVE
::Storage
::path
($cfg, $volid);
630 my $abspath = abs_path
($volid);
631 if ($abspath && $abspath =~ m
|^(/.+)$|) {
632 $path = $1; # untaint any path
637 die "can't find file '$volid'\n" if !($path && -f
$path);
642 my $volname_for_storage = sub {
643 my ($cfg, $volid, $target_storeid) = @_;
645 my (undef, $name, $vmid, undef, undef, undef, $format) = parse_volname
($cfg, $volid);
646 my $target_scfg = storage_config
($cfg, $target_storeid);
648 my (undef, $valid_formats) = PVE
::Storage
::Plugin
::default_format
($target_scfg);
649 my $format_is_valid = grep { $_ eq $format } @$valid_formats;
650 die "unsupported format '$format' for storage type $target_scfg->{type}\n" if !$format_is_valid;
652 (my $name_without_extension = $name) =~ s/\.$format$//;
654 if ($target_scfg->{path
}) {
655 return "$vmid/$name_without_extension.$format";
657 return "$name_without_extension";
661 sub storage_migrate
{
662 my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_;
664 my $base_snapshot = $opts->{base_snapshot
};
665 my $snapshot = $opts->{snapshot
};
666 my $ratelimit_bps = $opts->{ratelimit_bps
};
667 my $insecure = $opts->{insecure
};
668 my $with_snapshots = $opts->{with_snapshots
} ?
1 : 0;
669 my $allow_rename = $opts->{allow_rename
} ?
1 : 0;
671 my ($storeid, $volname) = parse_volume_id
($volid);
673 my $scfg = storage_config
($cfg, $storeid);
675 # no need to migrate shared content
676 return $volid if $storeid eq $target_storeid && $scfg->{shared
};
678 my $tcfg = storage_config
($cfg, $target_storeid);
681 if ($opts->{target_volname
}) {
682 $target_volname = $opts->{target_volname
};
683 } elsif ($scfg->{type
} eq $tcfg->{type
}) {
684 $target_volname = $volname;
686 $target_volname = $volname_for_storage->($cfg, $volid, $target_storeid);
689 my $target_volid = "${target_storeid}:${target_volname}";
691 my $target_ip = $target_sshinfo->{ip
};
693 my $ssh = PVE
::SSHInfo
::ssh_info_to_command
($target_sshinfo);
694 my $ssh_base = PVE
::SSHInfo
::ssh_info_to_command_base
($target_sshinfo);
695 local $ENV{RSYNC_RSH
} = PVE
::Tools
::cmd2string
($ssh_base);
698 if (defined($ratelimit_bps)) {
699 @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ]);
700 $logfunc->("using a bandwidth limit of $ratelimit_bps bps for transferring '$volid'") if $logfunc;
703 my $migration_snapshot;
704 if (!defined($snapshot)) {
705 if ($scfg->{type
} eq 'zfspool') {
706 $migration_snapshot = 1;
707 $snapshot = '__migration__';
711 my @formats = volume_transfer_formats
($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots);
712 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
713 my $format = $formats[0];
715 my $import_fn = '-'; # let pvesm import read from stdin per default
717 my $net = $target_sshinfo->{network
} // $target_sshinfo->{ip
};
718 $import_fn = "tcp://$net";
721 my $target_apiver = 1; # if there is no apiinfo call, assume 1
722 my $get_api_version = [@$ssh, 'pvesm', 'apiinfo'];
723 my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; };
724 eval { run_command
($get_api_version, logfunc
=> $match_api_version); };
726 my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
727 my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
728 if (defined($snapshot)) {
729 push @$send, '-snapshot', $snapshot
731 if ($migration_snapshot) {
732 push @$recv, '-delete-snapshot', $snapshot;
734 push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5;
736 if (defined($base_snapshot)) {
737 # Check if the snapshot exists on the remote side:
738 push @$send, '-base', $base_snapshot;
739 push @$recv, '-base', $base_snapshot;
743 my $pattern = volume_imported_message
(undef, 1);
744 my $match_volid_and_log = sub {
747 $new_volid = $1 if ($line =~ $pattern);
755 volume_snapshot
($cfg, $volid, $snapshot) if $migration_snapshot;
757 if (defined($snapshot)) {
758 activate_volumes
($cfg, [$volid], $snapshot);
760 activate_volumes
($cfg, [$volid]);
765 my $input = IO
::File-
>new();
766 my $info = IO
::File-
>new();
767 open3
($input, $info, $info, @{$recv})
768 or die "receive command failed: $!\n";
771 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
772 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
773 my $socket = IO
::Socket
::IP-
>new(PeerHost
=> $ip, PeerPort
=> $port, Type
=> SOCK_STREAM
)
774 or die "failed to connect to tunnel at $ip:$port\n";
775 # we won't be reading from the socket
776 shutdown($socket, 0);
778 eval { run_command
([$send, @cstream], output
=> '>&'.fileno($socket), errfunc
=> $logfunc); };
781 # don't close the connection entirely otherwise the receiving end
782 # might not get all buffered data (and fails with 'connection reset by peer')
783 shutdown($socket, 1);
785 # wait for the remote process to finish
786 while (my $line = <$info>) {
787 $match_volid_and_log->("[$target_sshinfo->{name}] $line");
790 # now close the socket
792 if (!close($info)) { # does waitpid()
793 die "import failed: $!\n" if $!;
794 die "import failed: exit code ".($?>>8)."\n";
797 die $send_error if $send_error;
799 run_command
([$send, @cstream, $recv], logfunc
=> $match_volid_and_log);
802 die "unable to get ID of the migrated volume\n"
803 if !defined($new_volid) && $target_apiver >= 5;
806 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
807 if ($migration_snapshot) {
808 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
809 warn "could not remove source snapshot: $@\n" if $@;
813 return $new_volid // $target_volid;
817 my ($cfg, $volid, $vmid, $snap) = @_;
819 my ($storeid, $volname) = parse_volume_id
($volid);
821 my $scfg = storage_config
($cfg, $storeid);
823 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
825 activate_storage
($cfg, $storeid);
827 # lock shared storage
828 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
829 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
830 return "$storeid:$volname";
834 sub vdisk_create_base
{
835 my ($cfg, $volid) = @_;
837 my ($storeid, $volname) = parse_volume_id
($volid);
839 my $scfg = storage_config
($cfg, $storeid);
841 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
843 activate_storage
($cfg, $storeid);
845 # lock shared storage
846 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
847 my $volname = $plugin->create_base($storeid, $scfg, $volname);
848 return "$storeid:$volname";
853 my ($cfg, $volid, $snapname) = @_;
855 my ($storeid, $volname) = parse_volume_id
($volid);
857 my $scfg = storage_config
($cfg, $storeid);
859 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
861 return $plugin->map_volume($storeid, $scfg, $volname, $snapname);
865 my ($cfg, $volid, $snapname) = @_;
867 my ($storeid, $volname) = parse_volume_id
($volid);
869 my $scfg = storage_config
($cfg, $storeid);
871 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
873 return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
877 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
879 die "no storage ID specified\n" if !$storeid;
881 PVE
::JSONSchema
::parse_storage_id
($storeid);
883 my $scfg = storage_config
($cfg, $storeid);
885 die "no VMID specified\n" if !$vmid;
887 $vmid = parse_vmid
($vmid);
889 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
891 $fmt = $defformat if !$fmt;
893 activate_storage
($cfg, $storeid);
895 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
897 # lock shared storage
898 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
899 my $old_umask = umask(umask|0037);
900 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
904 return "$storeid:$volname";
909 my ($cfg, $volid) = @_;
911 my ($storeid, $volname) = parse_volume_id
($volid);
912 my $scfg = storage_config
($cfg, $storeid);
913 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
915 activate_storage
($cfg, $storeid);
919 # lock shared storage
920 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
921 # LVM-thin allows deletion of still referenced base volumes!
922 die "base volume '$volname' is still in use by linked clones\n"
923 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
925 my (undef, undef, undef, undef, undef, $isBase, $format) =
926 $plugin->parse_volname($volname);
927 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
930 return if !$cleanup_worker;
932 my $rpcenv = PVE
::RPCEnvironment
::get
();
933 my $authuser = $rpcenv->get_user();
935 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
939 my ($cfg, $storeid, $vmid, $vollist) = @_;
941 my $ids = $cfg->{ids
};
943 storage_check_enabled
($cfg, $storeid) if ($storeid);
947 # prepare/activate/refresh all storages
949 my $storage_list = [];
951 foreach my $volid (@$vollist) {
952 my ($sid, undef) = parse_volume_id
($volid);
953 next if !defined($ids->{$sid});
954 next if !storage_check_enabled
($cfg, $sid, undef, 1);
955 push @$storage_list, $sid;
958 foreach my $sid (keys %$ids) {
959 next if $storeid && $storeid ne $sid;
960 next if !storage_check_enabled
($cfg, $sid, undef, 1);
961 my $content = $ids->{$sid}->{content
};
962 next if !($content->{rootdir
} || $content->{images
});
963 push @$storage_list, $sid;
969 activate_storage_list
($cfg, $storage_list, $cache);
971 foreach my $sid (keys %$ids) {
972 next if $storeid && $storeid ne $sid;
973 next if !storage_check_enabled
($cfg, $sid, undef, 1);
975 my $scfg = $ids->{$sid};
976 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
977 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
978 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
985 my ($cfg, $storeid, $tt) = @_;
987 die "unknown template type '$tt'\n"
988 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets');
990 my $ids = $cfg->{ids
};
992 storage_check_enabled
($cfg, $storeid) if ($storeid);
997 foreach my $sid (keys %$ids) {
998 next if $storeid && $storeid ne $sid;
1000 my $scfg = $ids->{$sid};
1001 my $type = $scfg->{type
};
1003 next if !$scfg->{content
}->{$tt};
1005 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1007 $res->{$sid} = volume_list
($cfg, $sid, undef, $tt);
1014 my ($cfg, $storeid, $vmid, $content) = @_;
1016 my @ctypes = qw(rootdir images vztmpl iso backup snippets);
1018 my $cts = $content ?
[ $content ] : [ @ctypes ];
1020 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
1022 $cts = [ grep { defined($scfg->{content
}->{$_}) } @$cts ];
1024 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1026 activate_storage
($cfg, $storeid);
1028 my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts);
1030 @$res = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @$res;
1037 my $filename = "/sys/kernel/uevent_seqnum";
1040 if (my $fh = IO
::File-
>new($filename, "r")) {
1042 if ($line =~ m/^(\d+)$/) {
1050 sub activate_storage
{
1051 my ($cfg, $storeid, $cache) = @_;
1053 $cache = {} if !$cache;
1055 my $scfg = storage_check_enabled
($cfg, $storeid);
1057 return if $cache->{activated
}->{$storeid};
1059 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
1061 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1063 if ($scfg->{base
}) {
1064 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
1065 activate_storage
($cfg, $baseid, $cache);
1068 if (!$plugin->check_connection($storeid, $scfg)) {
1069 die "storage '$storeid' is not online\n";
1072 $plugin->activate_storage($storeid, $scfg, $cache);
1074 my $newseq = uevent_seqnum
();
1076 # only call udevsettle if there are events
1077 if ($newseq > $cache->{uevent_seqnum
}) {
1079 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1080 $cache->{uevent_seqnum
} = $newseq;
1083 $cache->{activated
}->{$storeid} = 1;
1086 sub activate_storage_list
{
1087 my ($cfg, $storeid_list, $cache) = @_;
1089 $cache = {} if !$cache;
1091 foreach my $storeid (@$storeid_list) {
1092 activate_storage
($cfg, $storeid, $cache);
1096 sub deactivate_storage
{
1097 my ($cfg, $storeid) = @_;
1099 my $scfg = storage_config
($cfg, $storeid);
1100 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1103 $plugin->deactivate_storage($storeid, $scfg, $cache);
1106 sub activate_volumes
{
1107 my ($cfg, $vollist, $snapname) = @_;
1109 return if !($vollist && scalar(@$vollist));
1111 my $storagehash = {};
1112 foreach my $volid (@$vollist) {
1113 my ($storeid, undef) = parse_volume_id
($volid);
1114 $storagehash->{$storeid} = 1;
1119 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1121 foreach my $volid (@$vollist) {
1122 my ($storeid, $volname) = parse_volume_id
($volid);
1123 my $scfg = storage_config
($cfg, $storeid);
1124 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1125 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1129 sub deactivate_volumes
{
1130 my ($cfg, $vollist, $snapname) = @_;
1132 return if !($vollist && scalar(@$vollist));
1137 foreach my $volid (@$vollist) {
1138 my ($storeid, $volname) = parse_volume_id
($volid);
1140 my $scfg = storage_config
($cfg, $storeid);
1141 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1144 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1148 push @errlist, $volid;
1152 die "volume deactivation failed: " . join(' ', @errlist)
1153 if scalar(@errlist);
1157 my ($cfg, $content, $includeformat) = @_;
1159 my $ids = $cfg->{ids
};
1163 my @ctypes = PVE
::Tools
::split_list
($content);
1166 foreach my $storeid (keys %$ids) {
1167 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1169 if (defined($content)) {
1171 foreach my $ctype (@ctypes) {
1172 if ($ids->{$storeid}->{content
}->{$ctype}) {
1177 next if !$want_ctype || !$storage_enabled;
1180 my $type = $ids->{$storeid}->{type
};
1182 $info->{$storeid} = {
1187 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1188 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1190 enabled
=> $storage_enabled ?
1 : 0,
1193 push @$slist, $storeid;
1198 foreach my $storeid (keys %$ids) {
1199 my $scfg = $ids->{$storeid};
1201 next if !$info->{$storeid};
1202 next if !$info->{$storeid}->{enabled
};
1204 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1205 if ($includeformat) {
1206 my $pd = $plugin->plugindata();
1207 $info->{$storeid}->{format
} = $pd->{format
}
1209 $info->{$storeid}->{select_existing
} = $pd->{select_existing
}
1210 if $pd->{select_existing
};
1213 eval { activate_storage
($cfg, $storeid, $cache); };
1219 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
1222 $info->{$storeid}->{total
} = int($total);
1223 $info->{$storeid}->{avail
} = int($avail);
1224 $info->{$storeid}->{used
} = int($used);
1225 $info->{$storeid}->{active
} = $active;
1234 my ($packed_ip, $family);
1236 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1237 $family = $res[0]->{family
};
1238 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1240 if (defined $packed_ip) {
1241 return Socket
::inet_ntop
($family, $packed_ip);
1247 my ($server_in) = @_;
1250 if (!($server = resolv_server
($server_in))) {
1251 die "unable to resolve address for server '${server_in}'\n";
1254 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1257 run_command
($cmd, outfunc
=> sub {
1260 # note: howto handle white spaces in export path??
1261 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1270 my ($server_in, $user, $password, $domain) = @_;
1272 my $server = resolv_server
($server_in);
1273 die "unable to resolve address for server '${server_in}'\n" if !$server;
1275 # we only support Windows 2012 and newer, so just use smb3
1276 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1277 push @$cmd, '-W', $domain if defined($domain);
1279 push @$cmd, '-N' if !defined($password);
1280 local $ENV{USER
} = $user if defined($user);
1281 local $ENV{PASSWD
} = $password if defined($password);
1292 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1294 } elsif ($line =~ m/(NT_STATUS_(\S+))/) {
1296 $err .= "unexpected status: $1\n" if uc($1) ne 'SUCCESS';
1300 # only die if we got no share, else it's just some followup check error
1301 # (like workgroup querying)
1302 raise
($err) if $err && !%$res;
1309 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-Hp', '-o', 'name,avail,used'];
1312 run_command
($cmd, outfunc
=> sub {
1315 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1316 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1317 my $size = $size_str + 0;
1318 my $used = $used_str + 0;
1319 # ignore subvolumes generated by our ZFSPoolPlugin
1320 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1321 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1322 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1330 my ($portal, $noerr) = @_;
1332 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1334 if (my $ip = resolv_server
($server)) {
1336 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1337 return $port ?
"$server:$port" : $server;
1340 return undef if $noerr;
1342 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1347 my ($portal_in) = @_;
1350 if (!($portal = resolv_portal
($portal_in))) {
1351 die "unable to parse/resolve portal address '${portal_in}'\n";
1354 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1357 sub storage_default_format
{
1358 my ($cfg, $storeid) = @_;
1360 my $scfg = storage_config
($cfg, $storeid);
1362 return PVE
::Storage
::Plugin
::default_format
($scfg);
1365 sub vgroup_is_used
{
1366 my ($cfg, $vgname) = @_;
1368 foreach my $storeid (keys %{$cfg->{ids
}}) {
1369 my $scfg = storage_config
($cfg, $storeid);
1370 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1378 sub target_is_used
{
1379 my ($cfg, $target) = @_;
1381 foreach my $storeid (keys %{$cfg->{ids
}}) {
1382 my $scfg = storage_config
($cfg, $storeid);
1383 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1391 sub volume_is_used
{
1392 my ($cfg, $volid) = @_;
1394 foreach my $storeid (keys %{$cfg->{ids
}}) {
1395 my $scfg = storage_config
($cfg, $storeid);
1396 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1404 sub storage_is_used
{
1405 my ($cfg, $storeid) = @_;
1407 foreach my $sid (keys %{$cfg->{ids
}}) {
1408 my $scfg = storage_config
($cfg, $sid);
1409 next if !$scfg->{base
};
1410 my ($st) = parse_volume_id
($scfg->{base
});
1411 return 1 if $st && $st eq $storeid;
1418 my ($list, $func) = @_;
1422 foreach my $sid (keys %$list) {
1423 foreach my $info (@{$list->{$sid}}) {
1424 my $volid = $info->{volid
};
1425 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1426 if ($sid1 && $sid1 eq $sid) {
1427 &$func ($volid, $sid, $info);
1429 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1435 sub decompressor_info
{
1436 my ($format, $comp) = @_;
1438 if ($format eq 'tgz' && !defined($comp)) {
1439 ($format, $comp) = ('tar', 'gz');
1442 my $decompressor = {
1444 gz
=> ['tar', '-z'],
1445 lzo
=> ['tar', '--lzop'],
1446 zst
=> ['tar', '--zstd'],
1450 lzo
=> ['lzop', '-d', '-c'],
1451 zst
=> ['zstd', '-q', '-d', '-c'],
1455 die "ERROR: archive format not defined\n"
1456 if !defined($decompressor->{$format});
1459 $decomp = $decompressor->{$format}->{$comp} if $comp;
1463 compression
=> $comp,
1464 decompressor
=> $decomp,
1471 my ($archive) = shift;
1474 my $volid = basename
($archive);
1475 if ($volid =~ /^(vzdump-(lxc|openvz|qemu)-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)$/) {
1476 my $filename = "$1"; # untaint
1477 my ($type, $format, $comp) = ($2, $3, $4);
1478 my $format_re = defined($comp) ?
"$format.$comp" : "$format";
1479 $info = decompressor_info
($format, $comp);
1480 $info->{filename
} = $filename;
1481 $info->{type
} = $type;
1483 if ($volid =~ /^(vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2}))\.${format_re}$/) {
1484 $info->{logfilename
} = "$1.log";
1485 $info->{vmid
} = int($2);
1486 $info->{ctime
} = timelocal
($8, $7, $6, $5, $4 - 1, $3);
1487 $info->{is_std_name
} = 1;
1489 $info->{is_std_name
} = 0;
1492 die "ERROR: couldn't determine archive info from '$archive'\n";
1498 sub archive_remove
{
1499 my ($archive_path) = @_;
1501 my $dirname = dirname
($archive_path);
1502 my $archive_info = eval { archive_info
($archive_path) } // {};
1503 my $logfn = $archive_info->{logfilename
};
1505 unlink $archive_path or die "removing archive $archive_path failed: $!\n";
1507 if (defined($logfn)) {
1508 my $logpath = "$dirname/$logfn";
1510 unlink $logpath or warn "removing log file $logpath failed: $!\n";
1515 sub extract_vzdump_config_tar
{
1516 my ($archive, $conf_re) = @_;
1518 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1520 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1521 die "unable to open file '$archive'\n";
1524 while (defined($file = <$fh>)) {
1525 if ($file =~ $conf_re) {
1526 $file = $1; # untaint
1535 die "ERROR: archive contains no configuration file\n" if !$file;
1541 $raw .= "$output\n";
1544 run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1546 return wantarray ?
($raw, $file) : $raw;
1549 sub extract_vzdump_config_vma
{
1550 my ($archive, $comp) = @_;
1553 my $out = sub { $raw .= "$_[0]\n"; };
1555 my $info = archive_info
($archive);
1556 $comp //= $info->{compression
};
1557 my $decompressor = $info->{decompressor
};
1560 my $cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ];
1562 # lzop/zcat exits with 1 when the pipe is closed early by vma, detect this and ignore the exit code later
1567 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/ || $output =~ m/zstd: error 70 : Write error : Broken pipe/) {
1569 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1570 $errstring = "Failed to extract config from VMA archive: $output\n";
1574 my $rc = eval { run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1) };
1577 $broken_pipe ||= $rc == 141; # broken pipe from vma POV
1579 if (!$errstring && !$broken_pipe && $rc != 0) {
1580 die "$rerr\n" if $rerr;
1581 die "config extraction failed with exit code $rc\n";
1583 die "$errstring\n" if $errstring;
1585 run_command
(["vma", "config", $archive], outfunc
=> $out);
1588 return wantarray ?
($raw, undef) : $raw;
1591 sub extract_vzdump_config
{
1592 my ($cfg, $volid) = @_;
1594 my ($storeid, $volname) = parse_volume_id
($volid);
1595 if (defined($storeid)) {
1596 my $scfg = storage_config
($cfg, $storeid);
1597 if ($scfg->{type
} eq 'pbs') {
1598 storage_check_enabled
($cfg, $storeid);
1599 return PVE
::Storage
::PBSPlugin-
>extract_vzdump_config($scfg, $volname, $storeid);
1603 my $archive = abs_filesystem_path
($cfg, $volid);
1604 my $info = archive_info
($archive);
1605 my $format = $info->{format
};
1606 my $comp = $info->{compression
};
1607 my $type = $info->{type
};
1609 if ($type eq 'lxc' || $type eq 'openvz') {
1610 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1611 } elsif ($type eq 'qemu') {
1612 if ($format eq 'tar') {
1613 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1615 return extract_vzdump_config_vma
($archive, $comp);
1618 die "cannot determine backup guest type for backup archive '$volid'\n";
1623 my ($cfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
1625 my $scfg = storage_config
($cfg, $storeid);
1626 die "storage '$storeid' does not support backups\n" if !$scfg->{content
}->{backup
};
1628 if (!defined($keep)) {
1629 die "no prune-backups options configured for storage '$storeid'\n"
1630 if !defined($scfg->{'prune-backups'});
1631 $keep = PVE
::JSONSchema
::parse_property_string
('prune-backups', $scfg->{'prune-backups'});
1634 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1635 return $plugin->prune_backups($scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc);
1638 my $prune_mark = sub {
1639 my ($prune_entries, $keep_count, $id_func) = @_;
1641 return if !$keep_count;
1643 my $already_included = {};
1644 my $newly_included = {};
1646 foreach my $prune_entry (@{$prune_entries}) {
1647 my $mark = $prune_entry->{mark
};
1648 my $id = $id_func->($prune_entry->{ctime
});
1649 $already_included->{$id} = 1 if defined($mark) && $mark eq 'keep';
1652 foreach my $prune_entry (@{$prune_entries}) {
1653 my $mark = $prune_entry->{mark
};
1654 my $id = $id_func->($prune_entry->{ctime
});
1656 next if defined($mark) || $already_included->{$id};
1658 if (!$newly_included->{$id}) {
1659 last if scalar(keys %{$newly_included}) >= $keep_count;
1660 $newly_included->{$id} = 1;
1661 $prune_entry->{mark
} = 'keep';
1663 $prune_entry->{mark
} = 'remove';
1668 sub prune_mark_backup_group
{
1669 my ($backup_group, $keep) = @_;
1671 my $keep_all = delete $keep->{'keep-all'};
1673 if ($keep_all || !scalar(grep {$_ > 0} values %{$keep})) {
1674 $keep = { 'keep-all' => 1 } if $keep_all;
1675 foreach my $prune_entry (@{$backup_group}) {
1676 $prune_entry->{mark
} = 'keep';
1681 my $prune_list = [ sort { $b->{ctime
} <=> $a->{ctime
} } @{$backup_group} ];
1683 $prune_mark->($prune_list, $keep->{'keep-last'}, sub {
1687 $prune_mark->($prune_list, $keep->{'keep-hourly'}, sub {
1689 my (undef, undef, $hour, $day, $month, $year) = localtime($ctime);
1690 return "$hour/$day/$month/$year";
1692 $prune_mark->($prune_list, $keep->{'keep-daily'}, sub {
1694 my (undef, undef, undef, $day, $month, $year) = localtime($ctime);
1695 return "$day/$month/$year";
1697 $prune_mark->($prune_list, $keep->{'keep-weekly'}, sub {
1699 my ($sec, $min, $hour, $day, $month, $year) = localtime($ctime);
1700 my $iso_week = int(strftime
("%V", $sec, $min, $hour, $day, $month - 1, $year - 1900));
1701 my $iso_week_year = int(strftime
("%G", $sec, $min, $hour, $day, $month - 1, $year - 1900));
1702 return "$iso_week/$iso_week_year";
1704 $prune_mark->($prune_list, $keep->{'keep-monthly'}, sub {
1706 my (undef, undef, undef, undef, $month, $year) = localtime($ctime);
1707 return "$month/$year";
1709 $prune_mark->($prune_list, $keep->{'keep-yearly'}, sub {
1711 my $year = (localtime($ctime))[5];
1715 foreach my $prune_entry (@{$prune_list}) {
1716 $prune_entry->{mark
} //= 'remove';
1721 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1723 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1724 die "cannot export volume '$volid'\n" if !$storeid;
1725 my $scfg = storage_config
($cfg, $storeid);
1726 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1727 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1728 $snapshot, $base_snapshot, $with_snapshots);
1732 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1734 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1735 die "cannot import into volume '$volid'\n" if !$storeid;
1736 my $scfg = storage_config
($cfg, $storeid);
1737 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1738 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1739 $base_snapshot, $with_snapshots, $allow_rename) // $volid;
1742 sub volume_export_formats
{
1743 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1745 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1746 return if !$storeid;
1747 my $scfg = storage_config
($cfg, $storeid);
1748 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1749 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1750 $snapshot, $base_snapshot,
1754 sub volume_import_formats
{
1755 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1757 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1758 return if !$storeid;
1759 my $scfg = storage_config
($cfg, $storeid);
1760 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1761 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1762 $base_snapshot, $with_snapshots);
1765 sub volume_transfer_formats
{
1766 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1767 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1768 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1769 my %import_hash = map { $_ => 1 } @import_formats;
1770 my @common = grep { $import_hash{$_} } @export_formats;
1774 sub volume_imported_message
{
1775 my ($volid, $want_pattern) = @_;
1777 if ($want_pattern) {
1778 return qr/successfully imported '([^']*)'$/;
1780 return "successfully imported '$volid'\n";
1784 # bash completion helper
1786 sub complete_storage
{
1787 my ($cmdname, $pname, $cvalue) = @_;
1789 my $cfg = PVE
::Storage
::config
();
1791 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1794 sub complete_storage_enabled
{
1795 my ($cmdname, $pname, $cvalue) = @_;
1799 my $cfg = PVE
::Storage
::config
();
1800 foreach my $sid (keys %{$cfg->{ids
}}) {
1801 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1807 sub complete_content_type
{
1808 my ($cmdname, $pname, $cvalue) = @_;
1810 return [qw(rootdir images vztmpl iso backup snippets)];
1813 sub complete_volume
{
1814 my ($cmdname, $pname, $cvalue) = @_;
1818 my $storage_list = complete_storage_enabled
();
1820 if ($cvalue =~ m/^([^:]+):/) {
1821 $storage_list = [ $1 ];
1823 if (scalar(@$storage_list) > 1) {
1824 # only list storage IDs to avoid large listings
1826 foreach my $storeid (@$storage_list) {
1827 # Hack: simply return 2 artificial values, so that
1828 # completions does not finish
1829 push @$res, "$storeid:volname", "$storeid:...";
1836 foreach my $storeid (@$storage_list) {
1837 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1839 foreach my $item (@$vollist) {
1840 push @$res, $item->{volid
};
1847 # Various io-heavy operations require io/bandwidth limits which can be
1848 # configured on multiple levels: The global defaults in datacenter.cfg, and
1849 # per-storage overrides. When we want to do a restore from storage A to storage
1850 # B, we should take the smaller limit defined for storages A and B, and if no
1851 # such limit was specified, use the one from datacenter.cfg.
1852 sub get_bandwidth_limit
{
1853 my ($operation, $storage_list, $override) = @_;
1855 # called for each limit (global, per-storage) with the 'default' and the
1856 # $operation limit and should udpate $override for every limit affecting
1858 my $use_global_limits = 0;
1859 my $apply_limit = sub {
1861 if (defined($bwlimit)) {
1862 my $limits = PVE
::JSONSchema
::parse_property_string
('bwlimit', $bwlimit);
1863 my $limit = $limits->{$operation} // $limits->{default};
1864 if (defined($limit)) {
1865 if (!$override || $limit < $override) {
1871 # If there was no applicable limit, try to apply the global ones.
1872 $use_global_limits = 1;
1875 my ($rpcenv, $authuser);
1876 if (defined($override)) {
1877 $rpcenv = PVE
::RPCEnvironment-
>get();
1878 $authuser = $rpcenv->get_user();
1881 # Apply per-storage limits - if there are storages involved.
1882 if (defined($storage_list) && @$storage_list) {
1883 my $config = config
();
1885 # The Datastore.Allocate permission allows us to modify the per-storage
1886 # limits, therefore it also allows us to override them.
1887 # Since we have most likely multiple storages to check, do a quick check on
1888 # the general '/storage' path to see if we can skip the checks entirely:
1889 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
1892 foreach my $storage (@$storage_list) {
1893 next if !defined($storage);
1894 # Avoid duplicate checks:
1895 next if $done{$storage};
1896 $done{$storage} = 1;
1898 # Otherwise we may still have individual /storage/$ID permissions:
1899 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
1900 # And if not: apply the limits.
1901 my $storecfg = storage_config
($config, $storage);
1902 $apply_limit->($storecfg->{bwlimit
});
1906 # Storage limits take precedence over the datacenter defaults, so if
1907 # a limit was applied:
1908 return $override if !$use_global_limits;
1911 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1912 # global default limits.
1913 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
1914 # So if we cannot modify global limits, apply them to our currently
1915 # requested override.
1916 my $dc = cfs_read_file
('datacenter.cfg');
1917 $apply_limit->($dc->{bwlimit
});
1923 # checks if the storage id is available and dies if not
1924 sub assert_sid_unused
{
1928 if (my $scfg = storage_config
($cfg, $sid, 1)) {
1929 die "storage ID '$sid' already defined\n";