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
::PBSPlugin
;
41 use PVE
::Storage
::BTRFSPlugin
;
43 # Storage API version. Increment it on changes in storage API interface.
44 use constant APIVER
=> 9;
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
=> 0;
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
::PBSPlugin-
>register();
64 PVE
::Storage
::BTRFSPlugin-
>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" if !$modname->isa('PVE::Storage::Plugin');
79 die "does not provide an api() method\n" if !$modname->can('api');
80 # Check storage API version and that file is really storage plugin.
81 my $version = $modname->api();
82 die "implements an API version newer than current ($version > " . APIVER
. ")\n"
84 my $min_version = (APIVER
- APIAGE
);
85 die "API version too old, please update the plugin ($version < $min_version)\n"
86 if $version < $min_version;
87 # all OK, do import and register (i.e., "use")
91 # If we got this far and the API version is not the same, make some noise:
92 warn "Plugin \"$modname\" is implementing an older storage API
, an upgrade
is recommended
\n"
93 if $version != APIVER;
96 warn "Error loading storage plugin
\"$modname\": $@";
101 # initialize all plugins
102 PVE
::Storage
::Plugin-
>init();
104 our $iso_extension_re = qr/\.(?:iso|img)/i;
106 our $vztmpl_extension_re = qr/\.tar\.(gz|xz|zst)/i;
108 # PVE::Storage utility functions
111 return cfs_read_file
("storage.cfg");
117 cfs_write_file
('storage.cfg', $cfg);
120 sub lock_storage_config
{
121 my ($code, $errmsg) = @_;
123 cfs_lock_file
("storage.cfg", undef, $code);
126 $errmsg ?
die "$errmsg: $err" : die $err;
130 # FIXME remove maxfiles for PVE 8.0 or PVE 9.0
131 my $convert_maxfiles_to_prune_backups = sub {
136 my $maxfiles = delete $scfg->{maxfiles
};
138 if (!defined($scfg->{'prune-backups'}) && defined($maxfiles)) {
141 $prune_backups = { 'keep-last' => $maxfiles };
142 } else { # maxfiles 0 means no limit
143 $prune_backups = { 'keep-all' => 1 };
145 $scfg->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
153 my ($cfg, $storeid, $noerr) = @_;
155 die "no storage ID specified\n" if !$storeid;
157 my $scfg = $cfg->{ids
}->{$storeid};
159 die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg);
161 $convert_maxfiles_to_prune_backups->($scfg);
166 sub storage_check_node
{
167 my ($cfg, $storeid, $node, $noerr) = @_;
169 my $scfg = storage_config
($cfg, $storeid);
171 if ($scfg->{nodes
}) {
172 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
173 if (!$scfg->{nodes
}->{$node}) {
174 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
182 sub storage_check_enabled
{
183 my ($cfg, $storeid, $node, $noerr) = @_;
185 my $scfg = storage_config
($cfg, $storeid);
187 if ($scfg->{disable
}) {
188 die "storage '$storeid' is disabled\n" if !$noerr;
192 return storage_check_node
($cfg, $storeid, $node, $noerr);
195 # storage_can_replicate:
196 # return true if storage supports replication
197 # (volumes allocated with vdisk_alloc() has replication feature)
198 sub storage_can_replicate
{
199 my ($cfg, $storeid, $format) = @_;
201 my $scfg = storage_config
($cfg, $storeid);
202 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
203 return $plugin->storage_can_replicate($scfg, $storeid, $format);
209 return keys %{$cfg->{ids
}};
213 my ($filename, $timeout) = @_;
215 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
218 sub get_volume_notes
{
219 my ($cfg, $volid, $timeout) = @_;
221 my ($storeid, $volname) = parse_volume_id
($volid);
222 my $scfg = storage_config
($cfg, $storeid);
223 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
225 return $plugin->get_volume_notes($scfg, $storeid, $volname, $timeout);
228 sub update_volume_notes
{
229 my ($cfg, $volid, $notes, $timeout) = @_;
231 my ($storeid, $volname) = parse_volume_id
($volid);
232 my $scfg = storage_config
($cfg, $storeid);
233 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
235 $plugin->update_volume_notes($scfg, $storeid, $volname, $notes, $timeout);
238 sub volume_size_info
{
239 my ($cfg, $volid, $timeout) = @_;
241 my ($storeid, $volname) = parse_volume_id
($volid, 1);
243 my $scfg = storage_config
($cfg, $storeid);
244 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
245 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
246 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
247 return file_size_info
($volid, $timeout);
254 my ($cfg, $volid, $size, $running) = @_;
256 my $padding = (1024 - $size % 1024) % 1024;
257 $size = $size + $padding;
259 my ($storeid, $volname) = parse_volume_id
($volid, 1);
261 my $scfg = storage_config
($cfg, $storeid);
262 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
263 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
264 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
265 die "resize file/device '$volid' is not possible\n";
267 die "unable to parse volume ID '$volid'\n";
271 sub volume_rollback_is_possible
{
272 my ($cfg, $volid, $snap, $blockers) = @_;
274 my ($storeid, $volname) = parse_volume_id
($volid, 1);
276 my $scfg = storage_config
($cfg, $storeid);
277 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
278 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap, $blockers);
279 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
280 die "snapshot rollback file/device '$volid' is not possible\n";
282 die "unable to parse volume ID '$volid'\n";
286 sub volume_snapshot
{
287 my ($cfg, $volid, $snap) = @_;
289 my ($storeid, $volname) = parse_volume_id
($volid, 1);
291 my $scfg = storage_config
($cfg, $storeid);
292 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
293 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
294 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
295 die "snapshot file/device '$volid' is not possible\n";
297 die "unable to parse volume ID '$volid'\n";
301 sub volume_snapshot_rollback
{
302 my ($cfg, $volid, $snap) = @_;
304 my ($storeid, $volname) = parse_volume_id
($volid, 1);
306 my $scfg = storage_config
($cfg, $storeid);
307 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
308 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
309 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
310 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
311 die "snapshot rollback file/device '$volid' is not possible\n";
313 die "unable to parse volume ID '$volid'\n";
317 sub volume_snapshot_delete
{
318 my ($cfg, $volid, $snap, $running) = @_;
320 my ($storeid, $volname) = parse_volume_id
($volid, 1);
322 my $scfg = storage_config
($cfg, $storeid);
323 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
324 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
325 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
326 die "snapshot delete file/device '$volid' is not possible\n";
328 die "unable to parse volume ID '$volid'\n";
332 # check if a filesystem on top of a volume needs to flush its journal for
333 # consistency (see fsfreeze(8)) before a snapshot is taken - needed for
334 # container mountpoints
335 sub volume_snapshot_needs_fsfreeze
{
336 my ($cfg, $volid) = @_;
338 my ($storeid, $volname) = parse_volume_id
($volid);
339 my $scfg = storage_config
($cfg, $storeid);
340 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
341 return $plugin->volume_snapshot_needs_fsfreeze();
344 # check if a volume or snapshot supports a given feature
346 # clone - linked clone is possible
347 # copy - full clone is possible
348 # replicate - replication is possible
349 # snapshot - taking a snapshot is possible
350 # sparseinit - volume is sparsely initialized
351 # template - conversion to base image is possible
352 # $snap - check if the feature is supported for a given snapshot
353 # $running - if the guest owning the volume is running
354 # $opts - hash with further options:
355 # valid_target_formats - list of formats for the target of a copy/clone
356 # operation that the caller could work with. The
357 # format of $volid is always considered valid and if
358 # no list is specified, all formats are considered valid.
359 sub volume_has_feature
{
360 my ($cfg, $feature, $volid, $snap, $running, $opts) = @_;
362 my ($storeid, $volname) = parse_volume_id
($volid, 1);
364 my $scfg = storage_config
($cfg, $storeid);
365 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
366 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts);
367 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
374 sub volume_snapshot_info
{
375 my ($cfg, $volid) = @_;
377 my ($storeid, $volname) = parse_volume_id
($volid);
378 my $scfg = storage_config
($cfg, $storeid);
379 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
380 return $plugin->volume_snapshot_info($scfg, $storeid, $volname);
384 my ($cfg, $storeid, $vmid) = @_;
386 my $scfg = storage_config
($cfg, $storeid);
387 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
389 my $path = $plugin->get_subdir($scfg, 'images');
391 return $vmid ?
"$path/$vmid" : $path;
394 sub get_private_dir
{
395 my ($cfg, $storeid, $vmid) = @_;
397 my $scfg = storage_config
($cfg, $storeid);
398 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
400 my $path = $plugin->get_subdir($scfg, 'rootdir');
402 return $vmid ?
"$path/$vmid" : $path;
406 my ($cfg, $storeid) = @_;
408 my $scfg = storage_config
($cfg, $storeid);
409 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
411 return $plugin->get_subdir($scfg, 'iso');
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, 'vztmpl');
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, 'backup');
432 # library implementation
437 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
442 # NOTE: basename and basevmid are always undef for LVM-thin, where the
443 # clone -> base reference is not encoded in the volume ID.
444 # see note in PVE::Storage::LvmThinPlugin for details.
446 my ($cfg, $volid) = @_;
448 my ($storeid, $volname) = parse_volume_id
($volid);
450 my $scfg = storage_config
($cfg, $storeid);
452 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
454 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
456 return $plugin->parse_volname($volname);
459 sub parse_volume_id
{
460 my ($volid, $noerr) = @_;
462 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
465 # test if we have read access to volid
466 sub check_volume_access
{
467 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
469 my ($sid, $volname) = parse_volume_id
($volid, 1);
471 my ($vtype, undef, $ownervm) = parse_volname
($cfg, $volid);
472 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
473 # require at least read access to storage, (custom) templates/ISOs could be sensitive
474 $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']);
475 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
476 # we are owner - allow access
477 } elsif ($vtype eq 'backup' && $ownervm) {
478 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
479 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
481 # allow if we are Datastore administrator
482 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
485 die "Only root can pass arbitrary filesystem paths."
486 if $user ne 'root@pam';
492 # NOTE: this check does not work for LVM-thin, where the clone -> base
493 # reference is not encoded in the volume ID.
494 # see note in PVE::Storage::LvmThinPlugin for details.
495 sub volume_is_base_and_used
{
496 my ($cfg, $volid) = @_;
498 my ($storeid, $volname) = parse_volume_id
($volid);
499 my $scfg = storage_config
($cfg, $storeid);
500 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
502 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
503 $plugin->parse_volname($volname);
506 my $vollist = $plugin->list_images($storeid, $scfg);
507 foreach my $info (@$vollist) {
508 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
509 my $basename = undef;
510 my $basevmid = undef;
513 (undef, undef, undef, $basename, $basevmid) =
514 $plugin->parse_volname($tmpvolname);
517 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
525 # try to map a filesystem path to a volume identifier
526 sub path_to_volume_id
{
527 my ($cfg, $path) = @_;
529 my $ids = $cfg->{ids
};
531 my ($sid, $volname) = parse_volume_id
($path, 1);
533 if (my $scfg = $ids->{$sid}) {
535 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
536 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
537 return ($vtype, $path);
543 # Note: abs_path() return undef if $path doesn not exist
544 # for example when nfs storage is not mounted
545 $path = abs_path
($path) || $path;
547 foreach my $sid (keys %$ids) {
548 my $scfg = $ids->{$sid};
549 next if !$scfg->{path
};
550 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
551 my $imagedir = $plugin->get_subdir($scfg, 'images');
552 my $isodir = $plugin->get_subdir($scfg, 'iso');
553 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
554 my $backupdir = $plugin->get_subdir($scfg, 'backup');
555 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
556 my $snippetsdir = $plugin->get_subdir($scfg, 'snippets');
558 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
562 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
563 foreach my $info (@$vollist) {
564 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
565 my $volpath = $plugin->path($scfg, $volname, $storeid);
566 if ($volpath eq $path) {
567 return ('images', $info->{volid
});
570 } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) {
572 return ('iso', "$sid:iso/$name");
573 } elsif ($path =~ m!^$tmpldir/([^/]+$vztmpl_extension_re)$!) {
575 return ('vztmpl', "$sid:vztmpl/$name");
576 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
578 return ('rootdir', "$sid:rootdir/$vmid");
579 } elsif ($path =~ m!^$backupdir/([^/]+\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) {
581 return ('backup', "$sid:backup/$name");
582 } elsif ($path =~ m!^$snippetsdir/([^/]+)$!) {
584 return ('snippets', "$sid:snippets/$name");
588 # can't map path to volume id
593 my ($cfg, $volid, $snapname) = @_;
595 my ($storeid, $volname) = parse_volume_id
($volid);
597 my $scfg = storage_config
($cfg, $storeid);
599 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
600 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
601 return wantarray ?
($path, $owner, $vtype) : $path;
604 sub abs_filesystem_path
{
605 my ($cfg, $volid, $allow_blockdev) = @_;
608 if (parse_volume_id
($volid, 1)) {
609 activate_volumes
($cfg, [ $volid ]);
610 $path = PVE
::Storage
::path
($cfg, $volid);
612 if (-f
$volid || ($allow_blockdev && -b
$volid)) {
613 my $abspath = abs_path
($volid);
614 if ($abspath && $abspath =~ m
|^(/.+)$|) {
615 $path = $1; # untaint any path
619 die "can't find file '$volid'\n"
620 if !($path && (-f
$path || ($allow_blockdev && -b
$path)));
625 my $volname_for_storage = sub {
626 my ($cfg, $volid, $target_storeid) = @_;
628 my (undef, $name, $vmid, undef, undef, undef, $format) = parse_volname
($cfg, $volid);
629 my $target_scfg = storage_config
($cfg, $target_storeid);
631 my (undef, $valid_formats) = PVE
::Storage
::Plugin
::default_format
($target_scfg);
632 my $format_is_valid = grep { $_ eq $format } @$valid_formats;
633 die "unsupported format '$format' for storage type $target_scfg->{type}\n" if !$format_is_valid;
635 (my $name_without_extension = $name) =~ s/\.$format$//;
637 if ($target_scfg->{path
}) {
638 return "$vmid/$name_without_extension.$format";
640 return "$name_without_extension";
644 sub storage_migrate
{
645 my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_;
647 my $base_snapshot = $opts->{base_snapshot
};
648 my $snapshot = $opts->{snapshot
};
649 my $ratelimit_bps = $opts->{ratelimit_bps
};
650 my $insecure = $opts->{insecure
};
651 my $with_snapshots = $opts->{with_snapshots
} ?
1 : 0;
652 my $allow_rename = $opts->{allow_rename
} ?
1 : 0;
654 my ($storeid, $volname) = parse_volume_id
($volid);
656 my $scfg = storage_config
($cfg, $storeid);
658 # no need to migrate shared content
659 return $volid if $storeid eq $target_storeid && $scfg->{shared
};
661 my $tcfg = storage_config
($cfg, $target_storeid);
664 if ($opts->{target_volname
}) {
665 $target_volname = $opts->{target_volname
};
666 } elsif ($scfg->{type
} eq $tcfg->{type
}) {
667 $target_volname = $volname;
669 $target_volname = $volname_for_storage->($cfg, $volid, $target_storeid);
672 my $target_volid = "${target_storeid}:${target_volname}";
674 my $target_ip = $target_sshinfo->{ip
};
676 my $ssh = PVE
::SSHInfo
::ssh_info_to_command
($target_sshinfo);
677 my $ssh_base = PVE
::SSHInfo
::ssh_info_to_command_base
($target_sshinfo);
678 local $ENV{RSYNC_RSH
} = PVE
::Tools
::cmd2string
($ssh_base);
681 if (defined($ratelimit_bps)) {
682 @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ]);
683 $logfunc->("using a bandwidth limit of $ratelimit_bps bps for transferring '$volid'") if $logfunc;
686 my $migration_snapshot;
687 if (!defined($snapshot)) {
688 if ($scfg->{type
} eq 'zfspool' || $scfg->{type
} eq 'btrfs') {
689 $migration_snapshot = 1;
690 $snapshot = '__migration__';
694 my @formats = volume_transfer_formats
($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots);
695 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
696 my $format = $formats[0];
698 my $import_fn = '-'; # let pvesm import read from stdin per default
700 my $net = $target_sshinfo->{network
} // $target_sshinfo->{ip
};
701 $import_fn = "tcp://$net";
704 my $target_apiver = 1; # if there is no apiinfo call, assume 1
705 my $get_api_version = [@$ssh, 'pvesm', 'apiinfo'];
706 my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; };
707 eval { run_command
($get_api_version, logfunc
=> $match_api_version); };
709 my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
710 my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
711 if (defined($snapshot)) {
712 push @$send, '-snapshot', $snapshot;
713 push @$recv, '-snapshot', $snapshot;
715 if ($migration_snapshot) {
716 push @$recv, '-delete-snapshot', $snapshot;
718 push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5;
720 if (defined($base_snapshot)) {
721 # Check if the snapshot exists on the remote side:
722 push @$send, '-base', $base_snapshot;
723 push @$recv, '-base', $base_snapshot if $target_apiver >= 9;
727 my $pattern = volume_imported_message
(undef, 1);
728 my $match_volid_and_log = sub {
731 $new_volid = $1 if ($line =~ $pattern);
739 volume_snapshot
($cfg, $volid, $snapshot) if $migration_snapshot;
741 if (defined($snapshot)) {
742 activate_volumes
($cfg, [$volid], $snapshot);
744 activate_volumes
($cfg, [$volid]);
749 my $input = IO
::File-
>new();
750 my $info = IO
::File-
>new();
751 open3
($input, $info, $info, @{$recv})
752 or die "receive command failed: $!\n";
755 my $try_ip = <$info> // '';
756 my ($ip) = $try_ip =~ /^($PVE::Tools::IPRE)$/ # untaint
757 or die "no tunnel IP received, got '$try_ip'\n";
759 my $try_port = <$info> // '';
760 my ($port) = $try_port =~ /^(\d+)$/ # untaint
761 or die "no tunnel port received, got '$try_port'\n";
763 my $socket = IO
::Socket
::IP-
>new(PeerHost
=> $ip, PeerPort
=> $port, Type
=> SOCK_STREAM
)
764 or die "failed to connect to tunnel at $ip:$port\n";
765 # we won't be reading from the socket
766 shutdown($socket, 0);
768 eval { run_command
([$send, @cstream], output
=> '>&'.fileno($socket), errfunc
=> $logfunc); };
771 # don't close the connection entirely otherwise the receiving end
772 # might not get all buffered data (and fails with 'connection reset by peer')
773 shutdown($socket, 1);
775 # wait for the remote process to finish
776 while (my $line = <$info>) {
777 $match_volid_and_log->("[$target_sshinfo->{name}] $line");
780 # now close the socket
782 if (!close($info)) { # does waitpid()
783 die "import failed: $!\n" if $!;
784 die "import failed: exit code ".($?>>8)."\n";
787 die $send_error if $send_error;
789 run_command
([$send, @cstream, $recv], logfunc
=> $match_volid_and_log);
792 die "unable to get ID of the migrated volume\n"
793 if !defined($new_volid) && $target_apiver >= 5;
796 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
797 if ($migration_snapshot) {
798 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
799 warn "could not remove source snapshot: $@\n" if $@;
803 return $new_volid // $target_volid;
807 my ($cfg, $volid, $vmid, $snap) = @_;
809 my ($storeid, $volname) = parse_volume_id
($volid);
811 my $scfg = storage_config
($cfg, $storeid);
813 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
815 activate_storage
($cfg, $storeid);
817 # lock shared storage
818 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
819 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
820 return "$storeid:$volname";
824 sub vdisk_create_base
{
825 my ($cfg, $volid) = @_;
827 my ($storeid, $volname) = parse_volume_id
($volid);
829 my $scfg = storage_config
($cfg, $storeid);
831 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
833 activate_storage
($cfg, $storeid);
835 # lock shared storage
836 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
837 my $volname = $plugin->create_base($storeid, $scfg, $volname);
838 return "$storeid:$volname";
843 my ($cfg, $volid, $snapname) = @_;
845 my ($storeid, $volname) = parse_volume_id
($volid);
847 my $scfg = storage_config
($cfg, $storeid);
849 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
851 return $plugin->map_volume($storeid, $scfg, $volname, $snapname);
855 my ($cfg, $volid, $snapname) = @_;
857 my ($storeid, $volname) = parse_volume_id
($volid);
859 my $scfg = storage_config
($cfg, $storeid);
861 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
863 return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
867 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
869 die "no storage ID specified\n" if !$storeid;
871 PVE
::JSONSchema
::parse_storage_id
($storeid);
873 my $scfg = storage_config
($cfg, $storeid);
875 die "no VMID specified\n" if !$vmid;
877 $vmid = parse_vmid
($vmid);
879 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
881 $fmt = $defformat if !$fmt;
883 activate_storage
($cfg, $storeid);
885 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
887 # lock shared storage
888 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
889 my $old_umask = umask(umask|0037);
890 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
894 return "$storeid:$volname";
899 my ($cfg, $volid) = @_;
901 my ($storeid, $volname) = parse_volume_id
($volid);
902 my $scfg = storage_config
($cfg, $storeid);
903 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
905 activate_storage
($cfg, $storeid);
909 # lock shared storage
910 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
911 # LVM-thin allows deletion of still referenced base volumes!
912 die "base volume '$volname' is still in use by linked clones\n"
913 if volume_is_base_and_used
($cfg, $volid);
915 my (undef, undef, undef, undef, undef, $isBase, $format) =
916 $plugin->parse_volname($volname);
917 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
920 return if !$cleanup_worker;
922 my $rpcenv = PVE
::RPCEnvironment
::get
();
923 my $authuser = $rpcenv->get_user();
925 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
929 my ($cfg, $storeid, $vmid, $vollist, $ctype) = @_;
931 my $ids = $cfg->{ids
};
933 storage_check_enabled
($cfg, $storeid) if ($storeid);
935 my $res = $storeid ?
{ $storeid => [] } : {};
937 # prepare/activate/refresh all storages
939 my $storage_list = [];
941 foreach my $volid (@$vollist) {
942 my ($sid, undef) = parse_volume_id
($volid);
943 next if !defined($ids->{$sid});
944 next if !storage_check_enabled
($cfg, $sid, undef, 1);
945 push @$storage_list, $sid;
948 foreach my $sid (keys %$ids) {
949 next if $storeid && $storeid ne $sid;
950 next if !storage_check_enabled
($cfg, $sid, undef, 1);
951 my $content = $ids->{$sid}->{content
};
952 next if defined($ctype) && !$content->{$ctype};
953 next if !($content->{rootdir
} || $content->{images
});
954 push @$storage_list, $sid;
960 activate_storage_list
($cfg, $storage_list, $cache);
962 for my $sid ($storage_list->@*) {
963 next if $storeid && $storeid ne $sid;
965 my $scfg = $ids->{$sid};
966 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
967 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
968 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
975 my ($cfg, $storeid, $tt) = @_;
977 die "unknown template type '$tt'\n"
978 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets');
980 my $ids = $cfg->{ids
};
982 storage_check_enabled
($cfg, $storeid) if ($storeid);
987 foreach my $sid (keys %$ids) {
988 next if $storeid && $storeid ne $sid;
990 my $scfg = $ids->{$sid};
991 my $type = $scfg->{type
};
993 next if !$scfg->{content
}->{$tt};
995 next if !storage_check_enabled
($cfg, $sid, undef, 1);
997 $res->{$sid} = volume_list
($cfg, $sid, undef, $tt);
1004 my ($cfg, $storeid, $vmid, $content) = @_;
1006 my @ctypes = qw(rootdir images vztmpl iso backup snippets);
1008 my $cts = $content ?
[ $content ] : [ @ctypes ];
1010 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
1012 $cts = [ grep { defined($scfg->{content
}->{$_}) } @$cts ];
1014 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1016 activate_storage
($cfg, $storeid);
1018 my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts);
1020 @$res = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @$res;
1027 my $filename = "/sys/kernel/uevent_seqnum";
1030 if (my $fh = IO
::File-
>new($filename, "r")) {
1032 if ($line =~ m/^(\d+)$/) {
1040 sub activate_storage
{
1041 my ($cfg, $storeid, $cache) = @_;
1043 $cache = {} if !$cache;
1045 my $scfg = storage_check_enabled
($cfg, $storeid);
1047 return if $cache->{activated
}->{$storeid};
1049 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
1051 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1053 if ($scfg->{base
}) {
1054 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
1055 activate_storage
($cfg, $baseid, $cache);
1058 if (!$plugin->check_connection($storeid, $scfg)) {
1059 die "storage '$storeid' is not online\n";
1062 $plugin->activate_storage($storeid, $scfg, $cache);
1064 my $newseq = uevent_seqnum
();
1066 # only call udevsettle if there are events
1067 if ($newseq > $cache->{uevent_seqnum
}) {
1068 system ("udevadm settle --timeout=30"); # ignore errors
1069 $cache->{uevent_seqnum
} = $newseq;
1072 $cache->{activated
}->{$storeid} = 1;
1075 sub activate_storage_list
{
1076 my ($cfg, $storeid_list, $cache) = @_;
1078 $cache = {} if !$cache;
1080 foreach my $storeid (@$storeid_list) {
1081 activate_storage
($cfg, $storeid, $cache);
1085 sub deactivate_storage
{
1086 my ($cfg, $storeid) = @_;
1088 my $scfg = storage_config
($cfg, $storeid);
1089 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1092 $plugin->deactivate_storage($storeid, $scfg, $cache);
1095 sub activate_volumes
{
1096 my ($cfg, $vollist, $snapname) = @_;
1098 return if !($vollist && scalar(@$vollist));
1100 my $storagehash = {};
1101 foreach my $volid (@$vollist) {
1102 my ($storeid, undef) = parse_volume_id
($volid);
1103 $storagehash->{$storeid} = 1;
1108 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1110 foreach my $volid (@$vollist) {
1111 my ($storeid, $volname) = parse_volume_id
($volid);
1112 my $scfg = storage_config
($cfg, $storeid);
1113 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1114 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1118 sub deactivate_volumes
{
1119 my ($cfg, $vollist, $snapname) = @_;
1121 return if !($vollist && scalar(@$vollist));
1126 foreach my $volid (@$vollist) {
1127 my ($storeid, $volname) = parse_volume_id
($volid);
1129 my $scfg = storage_config
($cfg, $storeid);
1130 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1133 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1137 push @errlist, $volid;
1141 die "volume deactivation failed: " . join(' ', @errlist)
1142 if scalar(@errlist);
1146 my ($cfg, $content, $includeformat) = @_;
1148 my $ids = $cfg->{ids
};
1152 my @ctypes = PVE
::Tools
::split_list
($content);
1155 foreach my $storeid (keys %$ids) {
1156 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1158 if (defined($content)) {
1160 foreach my $ctype (@ctypes) {
1161 if ($ids->{$storeid}->{content
}->{$ctype}) {
1166 next if !$want_ctype || !$storage_enabled;
1169 my $type = $ids->{$storeid}->{type
};
1171 $info->{$storeid} = {
1176 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1177 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1179 enabled
=> $storage_enabled ?
1 : 0,
1182 push @$slist, $storeid;
1187 foreach my $storeid (keys %$ids) {
1188 my $scfg = $ids->{$storeid};
1190 next if !$info->{$storeid};
1191 next if !$info->{$storeid}->{enabled
};
1193 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1194 if ($includeformat) {
1195 my $pd = $plugin->plugindata();
1196 $info->{$storeid}->{format
} = $pd->{format
}
1198 $info->{$storeid}->{select_existing
} = $pd->{select_existing
}
1199 if $pd->{select_existing
};
1202 eval { activate_storage
($cfg, $storeid, $cache); };
1208 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
1211 $info->{$storeid}->{total
} = int($total);
1212 $info->{$storeid}->{avail
} = int($avail);
1213 $info->{$storeid}->{used
} = int($used);
1214 $info->{$storeid}->{active
} = $active;
1223 my ($packed_ip, $family);
1225 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1226 $family = $res[0]->{family
};
1227 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1229 if (defined $packed_ip) {
1230 return Socket
::inet_ntop
($family, $packed_ip);
1236 my ($server_in) = @_;
1239 if (!($server = resolv_server
($server_in))) {
1240 die "unable to resolve address for server '${server_in}'\n";
1243 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1246 run_command
($cmd, outfunc
=> sub {
1249 # note: howto handle white spaces in export path??
1250 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1259 my ($server_in, $user, $password, $domain) = @_;
1261 my $server = resolv_server
($server_in);
1262 die "unable to resolve address for server '${server_in}'\n" if !$server;
1264 # we only support Windows 2012 and newer, so just use smb3
1265 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1266 push @$cmd, '-W', $domain if defined($domain);
1268 push @$cmd, '-N' if !defined($password);
1269 local $ENV{USER
} = $user if defined($user);
1270 local $ENV{PASSWD
} = $password if defined($password);
1281 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1283 } elsif ($line =~ m/(NT_STATUS_(\S+))/) {
1285 $err .= "unexpected status: $1\n" if uc($1) ne 'SUCCESS';
1289 # only die if we got no share, else it's just some followup check error
1290 # (like workgroup querying)
1291 raise
($err) if $err && !%$res;
1298 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-Hp', '-o', 'name,avail,used'];
1301 run_command
($cmd, outfunc
=> sub {
1304 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1305 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1306 my $size = $size_str + 0;
1307 my $used = $used_str + 0;
1308 # ignore subvolumes generated by our ZFSPoolPlugin
1309 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1310 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1311 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1319 my ($portal, $noerr) = @_;
1321 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1323 if (my $ip = resolv_server
($server)) {
1325 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1326 return $port ?
"$server:$port" : $server;
1329 return undef if $noerr;
1331 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1336 my ($portal_in) = @_;
1339 if (!($portal = resolv_portal
($portal_in))) {
1340 die "unable to parse/resolve portal address '${portal_in}'\n";
1343 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1346 sub storage_default_format
{
1347 my ($cfg, $storeid) = @_;
1349 my $scfg = storage_config
($cfg, $storeid);
1351 return PVE
::Storage
::Plugin
::default_format
($scfg);
1354 sub vgroup_is_used
{
1355 my ($cfg, $vgname) = @_;
1357 foreach my $storeid (keys %{$cfg->{ids
}}) {
1358 my $scfg = storage_config
($cfg, $storeid);
1359 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1367 sub target_is_used
{
1368 my ($cfg, $target) = @_;
1370 foreach my $storeid (keys %{$cfg->{ids
}}) {
1371 my $scfg = storage_config
($cfg, $storeid);
1372 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1380 sub volume_is_used
{
1381 my ($cfg, $volid) = @_;
1383 foreach my $storeid (keys %{$cfg->{ids
}}) {
1384 my $scfg = storage_config
($cfg, $storeid);
1385 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1393 sub storage_is_used
{
1394 my ($cfg, $storeid) = @_;
1396 foreach my $sid (keys %{$cfg->{ids
}}) {
1397 my $scfg = storage_config
($cfg, $sid);
1398 next if !$scfg->{base
};
1399 my ($st) = parse_volume_id
($scfg->{base
});
1400 return 1 if $st && $st eq $storeid;
1407 my ($list, $func) = @_;
1411 foreach my $sid (keys %$list) {
1412 foreach my $info (@{$list->{$sid}}) {
1413 my $volid = $info->{volid
};
1414 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1415 if ($sid1 && $sid1 eq $sid) {
1416 &$func ($volid, $sid, $info);
1418 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1424 sub decompressor_info
{
1425 my ($format, $comp) = @_;
1427 if ($format eq 'tgz' && !defined($comp)) {
1428 ($format, $comp) = ('tar', 'gz');
1431 my $decompressor = {
1433 gz
=> ['tar', '-z'],
1434 lzo
=> ['tar', '--lzop'],
1435 zst
=> ['tar', '--zstd'],
1439 lzo
=> ['lzop', '-d', '-c'],
1440 zst
=> ['zstd', '-q', '-d', '-c'],
1444 die "ERROR: archive format not defined\n"
1445 if !defined($decompressor->{$format});
1448 $decomp = $decompressor->{$format}->{$comp} if $comp;
1452 compression
=> $comp,
1453 decompressor
=> $decomp,
1460 my ($archive) = shift;
1463 my $volid = basename
($archive);
1464 if ($volid =~ /^(vzdump-(lxc|openvz|qemu)-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)$/) {
1465 my $filename = "$1"; # untaint
1466 my ($type, $format, $comp) = ($2, $3, $4);
1467 my $format_re = defined($comp) ?
"$format.$comp" : "$format";
1468 $info = decompressor_info
($format, $comp);
1469 $info->{filename
} = $filename;
1470 $info->{type
} = $type;
1472 if ($volid =~ /^(vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2}))\.${format_re}$/) {
1473 $info->{logfilename
} = "$1.log";
1474 $info->{vmid
} = int($2);
1475 $info->{ctime
} = timelocal
($8, $7, $6, $5, $4 - 1, $3);
1476 $info->{is_std_name
} = 1;
1478 $info->{is_std_name
} = 0;
1481 die "ERROR: couldn't determine archive info from '$archive'\n";
1487 sub archive_remove
{
1488 my ($archive_path) = @_;
1490 my $dirname = dirname
($archive_path);
1491 my $archive_info = eval { archive_info
($archive_path) } // {};
1492 my $logfn = $archive_info->{logfilename
};
1494 unlink $archive_path or die "removing archive $archive_path failed: $!\n";
1496 if (defined($logfn)) {
1497 my $logpath = "$dirname/$logfn";
1499 unlink $logpath or warn "removing log file $logpath failed: $!\n";
1504 sub extract_vzdump_config_tar
{
1505 my ($archive, $conf_re) = @_;
1507 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1509 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1510 die "unable to open file '$archive'\n";
1513 while (defined($file = <$fh>)) {
1514 if ($file =~ $conf_re) {
1515 $file = $1; # untaint
1524 die "ERROR: archive contains no configuration file\n" if !$file;
1530 $raw .= "$output\n";
1533 run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1535 return wantarray ?
($raw, $file) : $raw;
1538 sub extract_vzdump_config_vma
{
1539 my ($archive, $comp) = @_;
1542 my $out = sub { $raw .= "$_[0]\n"; };
1544 my $info = archive_info
($archive);
1545 $comp //= $info->{compression
};
1546 my $decompressor = $info->{decompressor
};
1549 my $cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ];
1551 # lzop/zcat exits with 1 when the pipe is closed early by vma, detect this and ignore the exit code later
1556 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/ || $output =~ m/zstd: error 70 : Write error.*Broken pipe/) {
1558 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1559 $errstring = "Failed to extract config from VMA archive: $output\n";
1563 my $rc = eval { run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1) };
1566 $broken_pipe ||= $rc == 141; # broken pipe from vma POV
1568 if (!$errstring && !$broken_pipe && $rc != 0) {
1569 die "$rerr\n" if $rerr;
1570 die "config extraction failed with exit code $rc\n";
1572 die "$errstring\n" if $errstring;
1574 run_command
(["vma", "config", $archive], outfunc
=> $out);
1577 return wantarray ?
($raw, undef) : $raw;
1580 sub extract_vzdump_config
{
1581 my ($cfg, $volid) = @_;
1583 my ($storeid, $volname) = parse_volume_id
($volid);
1584 if (defined($storeid)) {
1585 my $scfg = storage_config
($cfg, $storeid);
1586 if ($scfg->{type
} eq 'pbs') {
1587 storage_check_enabled
($cfg, $storeid);
1588 return PVE
::Storage
::PBSPlugin-
>extract_vzdump_config($scfg, $volname, $storeid);
1592 my $archive = abs_filesystem_path
($cfg, $volid);
1593 my $info = archive_info
($archive);
1594 my $format = $info->{format
};
1595 my $comp = $info->{compression
};
1596 my $type = $info->{type
};
1598 if ($type eq 'lxc' || $type eq 'openvz') {
1599 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1600 } elsif ($type eq 'qemu') {
1601 if ($format eq 'tar') {
1602 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1604 return extract_vzdump_config_vma
($archive, $comp);
1607 die "cannot determine backup guest type for backup archive '$volid'\n";
1612 my ($cfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
1614 my $scfg = storage_config
($cfg, $storeid);
1615 die "storage '$storeid' does not support backups\n" if !$scfg->{content
}->{backup
};
1617 if (!defined($keep)) {
1618 die "no prune-backups options configured for storage '$storeid'\n"
1619 if !defined($scfg->{'prune-backups'});
1620 $keep = PVE
::JSONSchema
::parse_property_string
('prune-backups', $scfg->{'prune-backups'});
1623 activate_storage
($cfg, $storeid);
1625 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1626 return $plugin->prune_backups($scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc);
1629 my $prune_mark = sub {
1630 my ($prune_entries, $keep_count, $id_func) = @_;
1632 return if !$keep_count;
1634 my $already_included = {};
1635 my $newly_included = {};
1637 foreach my $prune_entry (@{$prune_entries}) {
1638 my $mark = $prune_entry->{mark
};
1639 my $id = $id_func->($prune_entry->{ctime
});
1640 $already_included->{$id} = 1 if defined($mark) && $mark eq 'keep';
1643 foreach my $prune_entry (@{$prune_entries}) {
1644 my $mark = $prune_entry->{mark
};
1645 my $id = $id_func->($prune_entry->{ctime
});
1647 next if defined($mark) || $already_included->{$id};
1649 if (!$newly_included->{$id}) {
1650 last if scalar(keys %{$newly_included}) >= $keep_count;
1651 $newly_included->{$id} = 1;
1652 $prune_entry->{mark
} = 'keep';
1654 $prune_entry->{mark
} = 'remove';
1659 sub prune_mark_backup_group
{
1660 my ($backup_group, $keep) = @_;
1662 my @positive_opts = grep { $_ ne 'keep-all' && $keep->{$_} > 0 } keys $keep->%*;
1664 if ($keep->{'keep-all'} || scalar(@positive_opts) == 0) {
1665 foreach my $prune_entry (@{$backup_group}) {
1666 $prune_entry->{mark
} = 'keep';
1671 my $prune_list = [ sort { $b->{ctime
} <=> $a->{ctime
} } @{$backup_group} ];
1673 $prune_mark->($prune_list, $keep->{'keep-last'}, sub {
1677 $prune_mark->($prune_list, $keep->{'keep-hourly'}, sub {
1679 my (undef, undef, $hour, $day, $month, $year) = localtime($ctime);
1680 return "$hour/$day/$month/$year";
1682 $prune_mark->($prune_list, $keep->{'keep-daily'}, sub {
1684 my (undef, undef, undef, $day, $month, $year) = localtime($ctime);
1685 return "$day/$month/$year";
1687 $prune_mark->($prune_list, $keep->{'keep-weekly'}, sub {
1689 my ($sec, $min, $hour, $day, $month, $year) = localtime($ctime);
1690 my $iso_week = int(strftime
("%V", $sec, $min, $hour, $day, $month, $year));
1691 my $iso_week_year = int(strftime
("%G", $sec, $min, $hour, $day, $month, $year));
1692 return "$iso_week/$iso_week_year";
1694 $prune_mark->($prune_list, $keep->{'keep-monthly'}, sub {
1696 my (undef, undef, undef, undef, $month, $year) = localtime($ctime);
1697 return "$month/$year";
1699 $prune_mark->($prune_list, $keep->{'keep-yearly'}, sub {
1701 my $year = (localtime($ctime))[5];
1705 foreach my $prune_entry (@{$prune_list}) {
1706 $prune_entry->{mark
} //= 'remove';
1710 sub volume_export
: prototype($$$$$$$) {
1711 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1713 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1714 die "cannot export volume '$volid'\n" if !$storeid;
1715 my $scfg = storage_config
($cfg, $storeid);
1716 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1717 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1718 $snapshot, $base_snapshot, $with_snapshots);
1721 sub volume_import
: prototype($$$$$$$$) {
1722 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1724 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1725 die "cannot import into volume '$volid'\n" if !$storeid;
1726 my $scfg = storage_config
($cfg, $storeid);
1727 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1728 return $plugin->volume_import(
1741 sub volume_export_formats
: prototype($$$$$) {
1742 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1744 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1745 return if !$storeid;
1746 my $scfg = storage_config
($cfg, $storeid);
1747 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1748 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1749 $snapshot, $base_snapshot,
1753 sub volume_import_formats
: prototype($$$$$) {
1754 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1756 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1757 return if !$storeid;
1758 my $scfg = storage_config
($cfg, $storeid);
1759 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1760 return $plugin->volume_import_formats(
1770 sub volume_transfer_formats
{
1771 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1772 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1773 my @import_formats = volume_import_formats
($cfg, $dst_volid, $snapshot, $base_snapshot, $with_snapshots);
1774 my %import_hash = map { $_ => 1 } @import_formats;
1775 my @common = grep { $import_hash{$_} } @export_formats;
1779 sub volume_imported_message
{
1780 my ($volid, $want_pattern) = @_;
1782 if ($want_pattern) {
1783 return qr/successfully imported '([^']*)'$/;
1785 return "successfully imported '$volid'\n";
1789 # bash completion helper
1791 sub complete_storage
{
1792 my ($cmdname, $pname, $cvalue) = @_;
1794 my $cfg = PVE
::Storage
::config
();
1796 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1799 sub complete_storage_enabled
{
1800 my ($cmdname, $pname, $cvalue) = @_;
1804 my $cfg = PVE
::Storage
::config
();
1805 foreach my $sid (keys %{$cfg->{ids
}}) {
1806 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1812 sub complete_content_type
{
1813 my ($cmdname, $pname, $cvalue) = @_;
1815 return [qw(rootdir images vztmpl iso backup snippets)];
1818 sub complete_volume
{
1819 my ($cmdname, $pname, $cvalue) = @_;
1823 my $storage_list = complete_storage_enabled
();
1825 if ($cvalue =~ m/^([^:]+):/) {
1826 $storage_list = [ $1 ];
1828 if (scalar(@$storage_list) > 1) {
1829 # only list storage IDs to avoid large listings
1831 foreach my $storeid (@$storage_list) {
1832 # Hack: simply return 2 artificial values, so that
1833 # completions does not finish
1834 push @$res, "$storeid:volname", "$storeid:...";
1841 foreach my $storeid (@$storage_list) {
1842 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1844 foreach my $item (@$vollist) {
1845 push @$res, $item->{volid
};
1852 # Various io-heavy operations require io/bandwidth limits which can be
1853 # configured on multiple levels: The global defaults in datacenter.cfg, and
1854 # per-storage overrides. When we want to do a restore from storage A to storage
1855 # B, we should take the smaller limit defined for storages A and B, and if no
1856 # such limit was specified, use the one from datacenter.cfg.
1857 sub get_bandwidth_limit
{
1858 my ($operation, $storage_list, $override) = @_;
1860 # called for each limit (global, per-storage) with the 'default' and the
1861 # $operation limit and should update $override for every limit affecting
1863 my $use_global_limits = 0;
1864 my $apply_limit = sub {
1866 if (defined($bwlimit)) {
1867 my $limits = PVE
::JSONSchema
::parse_property_string
('bwlimit', $bwlimit);
1868 my $limit = $limits->{$operation} // $limits->{default};
1869 if (defined($limit)) {
1870 if (!$override || $limit < $override) {
1876 # If there was no applicable limit, try to apply the global ones.
1877 $use_global_limits = 1;
1880 my ($rpcenv, $authuser);
1881 if (defined($override)) {
1882 $rpcenv = PVE
::RPCEnvironment-
>get();
1883 $authuser = $rpcenv->get_user();
1886 # Apply per-storage limits - if there are storages involved.
1887 if (defined($storage_list) && @$storage_list) {
1888 my $config = config
();
1890 # The Datastore.Allocate permission allows us to modify the per-storage
1891 # limits, therefore it also allows us to override them.
1892 # Since we have most likely multiple storages to check, do a quick check on
1893 # the general '/storage' path to see if we can skip the checks entirely:
1894 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
1897 foreach my $storage (@$storage_list) {
1898 next if !defined($storage);
1899 # Avoid duplicate checks:
1900 next if $done{$storage};
1901 $done{$storage} = 1;
1903 # Otherwise we may still have individual /storage/$ID permissions:
1904 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
1905 # And if not: apply the limits.
1906 my $storecfg = storage_config
($config, $storage);
1907 $apply_limit->($storecfg->{bwlimit
});
1911 # Storage limits take precedence over the datacenter defaults, so if
1912 # a limit was applied:
1913 return $override if !$use_global_limits;
1916 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1917 # global default limits.
1918 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
1919 # So if we cannot modify global limits, apply them to our currently
1920 # requested override.
1921 my $dc = cfs_read_file
('datacenter.cfg');
1922 $apply_limit->($dc->{bwlimit
});
1928 # checks if the storage id is available and dies if not
1929 sub assert_sid_unused
{
1933 if (my $scfg = storage_config
($cfg, $sid, 1)) {
1934 die "storage ID '$sid' already defined\n";
1940 # removes leading/trailing spaces and (back)slashes completely
1941 # substitutes every non-ASCII-alphanumerical char with '_', except '_.-'
1942 sub normalize_content_filename
{
1943 my ($filename) = @_;
1946 $filename =~ s/^.*[\/\\]//;
1947 $filename =~ s/[^a-zA-Z0-9_.-]/_/g;