15 use Time
::Local
qw(timelocal);
17 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
18 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
19 use PVE
::DataCenterConfig
;
20 use PVE
::Exception
qw(raise_param_exc raise);
23 use PVE
::RPCEnvironment
;
26 use PVE
::Storage
::Plugin
;
27 use PVE
::Storage
::DirPlugin
;
28 use PVE
::Storage
::LVMPlugin
;
29 use PVE
::Storage
::LvmThinPlugin
;
30 use PVE
::Storage
::NFSPlugin
;
31 use PVE
::Storage
::CIFSPlugin
;
32 use PVE
::Storage
::ISCSIPlugin
;
33 use PVE
::Storage
::RBDPlugin
;
34 use PVE
::Storage
::CephFSPlugin
;
35 use PVE
::Storage
::ISCSIDirectPlugin
;
36 use PVE
::Storage
::GlusterfsPlugin
;
37 use PVE
::Storage
::ZFSPoolPlugin
;
38 use PVE
::Storage
::ZFSPlugin
;
39 use PVE
::Storage
::DRBDPlugin
;
40 use PVE
::Storage
::PBSPlugin
;
42 # Storage API version. Icrement it on changes in storage API interface.
43 use constant APIVER
=> 5;
44 # Age is the number of versions we're backward compatible with.
45 # This is like having 'current=APIVER' and age='APIAGE' in libtool,
46 # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
47 use constant APIAGE
=> 4;
49 # load standard plugins
50 PVE
::Storage
::DirPlugin-
>register();
51 PVE
::Storage
::LVMPlugin-
>register();
52 PVE
::Storage
::LvmThinPlugin-
>register();
53 PVE
::Storage
::NFSPlugin-
>register();
54 PVE
::Storage
::CIFSPlugin-
>register();
55 PVE
::Storage
::ISCSIPlugin-
>register();
56 PVE
::Storage
::RBDPlugin-
>register();
57 PVE
::Storage
::CephFSPlugin-
>register();
58 PVE
::Storage
::ISCSIDirectPlugin-
>register();
59 PVE
::Storage
::GlusterfsPlugin-
>register();
60 PVE
::Storage
::ZFSPoolPlugin-
>register();
61 PVE
::Storage
::ZFSPlugin-
>register();
62 PVE
::Storage
::DRBDPlugin-
>register();
63 PVE
::Storage
::PBSPlugin-
>register();
65 # load third-party plugins
66 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
67 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
69 my $modname = 'PVE::Storage::Custom::' . $file;
70 $modname =~ s!\.pm$!!;
71 $file = 'PVE/Storage/Custom/' . $file;
76 # Check perl interface:
77 die "not derived from PVE::Storage::Plugin\n"
78 if !$modname->isa('PVE::Storage::Plugin');
79 die "does not provide an api() method\n"
80 if !$modname->can('api');
81 # Check storage API version and that file is really storage plugin.
82 my $version = $modname->api();
83 die "implements an API version newer than current ($version > " . APIVER
. ")\n"
85 my $min_version = (APIVER
- APIAGE
);
86 die "API version too old, please update the plugin ($version < $min_version)\n"
87 if $version < $min_version;
91 # If we got this far and the API version is not the same, make some
93 warn "Plugin \"$modname\" is implementing an older storage API
, an upgrade
is recommended
\n"
94 if $version != APIVER;
97 warn "Error loading storage plugin
\"$modname\": $@";
102 # initialize all plugins
103 PVE
::Storage
::Plugin-
>init();
105 my $UDEVADM = '/sbin/udevadm';
107 our $iso_extension_re = qr/\.(?:iso|img)/i;
109 # PVE::Storage utility functions
112 return cfs_read_file
("storage.cfg");
118 cfs_write_file
('storage.cfg', $cfg);
121 sub lock_storage_config
{
122 my ($code, $errmsg) = @_;
124 cfs_lock_file
("storage.cfg", undef, $code);
127 $errmsg ?
die "$errmsg: $err" : die $err;
132 my ($cfg, $storeid, $noerr) = @_;
134 die "no storage ID specified\n" if !$storeid;
136 my $scfg = $cfg->{ids
}->{$storeid};
138 die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg);
143 sub storage_check_node
{
144 my ($cfg, $storeid, $node, $noerr) = @_;
146 my $scfg = storage_config
($cfg, $storeid);
148 if ($scfg->{nodes
}) {
149 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
150 if (!$scfg->{nodes
}->{$node}) {
151 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
159 sub storage_check_enabled
{
160 my ($cfg, $storeid, $node, $noerr) = @_;
162 my $scfg = storage_config
($cfg, $storeid);
164 if ($scfg->{disable
}) {
165 die "storage '$storeid' is disabled\n" if !$noerr;
169 return storage_check_node
($cfg, $storeid, $node, $noerr);
172 # storage_can_replicate:
173 # return true if storage supports replication
174 # (volumes alocated with vdisk_alloc() has replication feature)
175 sub storage_can_replicate
{
176 my ($cfg, $storeid, $format) = @_;
178 my $scfg = storage_config
($cfg, $storeid);
179 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
180 return $plugin->storage_can_replicate($scfg, $storeid, $format);
186 return keys %{$cfg->{ids
}};
190 my ($filename, $timeout) = @_;
192 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
195 sub volume_size_info
{
196 my ($cfg, $volid, $timeout) = @_;
198 my ($storeid, $volname) = parse_volume_id
($volid, 1);
200 my $scfg = storage_config
($cfg, $storeid);
201 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
202 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
203 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
204 return file_size_info
($volid, $timeout);
211 my ($cfg, $volid, $size, $running) = @_;
213 my $padding = (1024 - $size % 1024) % 1024;
214 $size = $size + $padding;
216 my ($storeid, $volname) = parse_volume_id
($volid, 1);
218 my $scfg = storage_config
($cfg, $storeid);
219 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
220 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
221 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
222 die "resize file/device '$volid' is not possible\n";
224 die "unable to parse volume ID '$volid'\n";
228 sub volume_rollback_is_possible
{
229 my ($cfg, $volid, $snap) = @_;
231 my ($storeid, $volname) = parse_volume_id
($volid, 1);
233 my $scfg = storage_config
($cfg, $storeid);
234 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
235 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
236 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
237 die "snapshot rollback file/device '$volid' is not possible\n";
239 die "unable to parse volume ID '$volid'\n";
243 sub volume_snapshot
{
244 my ($cfg, $volid, $snap) = @_;
246 my ($storeid, $volname) = parse_volume_id
($volid, 1);
248 my $scfg = storage_config
($cfg, $storeid);
249 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
250 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
251 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
252 die "snapshot file/device '$volid' is not possible\n";
254 die "unable to parse volume ID '$volid'\n";
258 sub volume_snapshot_rollback
{
259 my ($cfg, $volid, $snap) = @_;
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 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
266 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
267 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
268 die "snapshot rollback file/device '$volid' is not possible\n";
270 die "unable to parse volume ID '$volid'\n";
274 sub volume_snapshot_delete
{
275 my ($cfg, $volid, $snap, $running) = @_;
277 my ($storeid, $volname) = parse_volume_id
($volid, 1);
279 my $scfg = storage_config
($cfg, $storeid);
280 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
281 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
282 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
283 die "snapshot delete file/device '$volid' is not possible\n";
285 die "unable to parse volume ID '$volid'\n";
289 # check if a volume or snapshot supports a given feature
291 # clone - linked clone is possible
292 # copy - full clone is possible
293 # replicate - replication is possible
294 # snapshot - taking a snapshot is possible
295 # sparseinit - volume is sparsely initialized
296 # template - conversion to base image is possible
297 # $snap - check if the feature is supported for a given snapshot
298 # $running - if the guest owning the volume is running
299 # $opts - hash with further options:
300 # valid_target_formats - list of formats for the target of a copy/clone
301 # operation that the caller could work with. The
302 # format of $volid is always considered valid and if
303 # no list is specified, all formats are considered valid.
304 sub volume_has_feature
{
305 my ($cfg, $feature, $volid, $snap, $running, $opts) = @_;
307 my ($storeid, $volname) = parse_volume_id
($volid, 1);
309 my $scfg = storage_config
($cfg, $storeid);
310 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
311 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts);
312 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
319 sub volume_snapshot_list
{
320 my ($cfg, $volid) = @_;
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_list($scfg, $storeid, $volname);
327 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
328 die "send file/device '$volid' is not possible\n";
330 die "unable to parse volume ID '$volid'\n";
332 # return an empty array if dataset does not exist.
336 my ($cfg, $storeid, $vmid) = @_;
338 my $scfg = storage_config
($cfg, $storeid);
339 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
341 my $path = $plugin->get_subdir($scfg, 'images');
343 return $vmid ?
"$path/$vmid" : $path;
346 sub get_private_dir
{
347 my ($cfg, $storeid, $vmid) = @_;
349 my $scfg = storage_config
($cfg, $storeid);
350 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
352 my $path = $plugin->get_subdir($scfg, 'rootdir');
354 return $vmid ?
"$path/$vmid" : $path;
358 my ($cfg, $storeid) = @_;
360 my $scfg = storage_config
($cfg, $storeid);
361 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
363 return $plugin->get_subdir($scfg, 'iso');
367 my ($cfg, $storeid) = @_;
369 my $scfg = storage_config
($cfg, $storeid);
370 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
372 return $plugin->get_subdir($scfg, 'vztmpl');
376 my ($cfg, $storeid) = @_;
378 my $scfg = storage_config
($cfg, $storeid);
379 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
381 return $plugin->get_subdir($scfg, 'backup');
384 # library implementation
389 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
394 # NOTE: basename and basevmid are always undef for LVM-thin, where the
395 # clone -> base reference is not encoded in the volume ID.
396 # see note in PVE::Storage::LvmThinPlugin for details.
398 my ($cfg, $volid) = @_;
400 my ($storeid, $volname) = parse_volume_id
($volid);
402 my $scfg = storage_config
($cfg, $storeid);
404 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
406 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
408 return $plugin->parse_volname($volname);
411 sub parse_volume_id
{
412 my ($volid, $noerr) = @_;
414 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
417 # test if we have read access to volid
418 sub check_volume_access
{
419 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
421 my ($sid, $volname) = parse_volume_id
($volid, 1);
423 my ($vtype, undef, $ownervm) = parse_volname
($cfg, $volid);
424 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
425 # require at least read access to storage, (custom) templates/ISOs could be sensitive
426 $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']);
427 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
428 # we are owner - allow access
429 } elsif ($vtype eq 'backup' && $ownervm) {
430 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
431 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
433 # allow if we are Datastore administrator
434 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
437 die "Only root can pass arbitrary filesystem paths."
438 if $user ne 'root@pam';
444 my $volume_is_base_and_used__no_lock = sub {
445 my ($scfg, $storeid, $plugin, $volname) = @_;
447 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
448 $plugin->parse_volname($volname);
451 my $vollist = $plugin->list_images($storeid, $scfg);
452 foreach my $info (@$vollist) {
453 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
454 my $basename = undef;
455 my $basevmid = undef;
458 (undef, undef, undef, $basename, $basevmid) =
459 $plugin->parse_volname($tmpvolname);
462 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
470 # NOTE: this check does not work for LVM-thin, where the clone -> base
471 # reference is not encoded in the volume ID.
472 # see note in PVE::Storage::LvmThinPlugin for details.
473 sub volume_is_base_and_used
{
474 my ($cfg, $volid) = @_;
476 my ($storeid, $volname) = parse_volume_id
($volid);
477 my $scfg = storage_config
($cfg, $storeid);
478 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
480 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
481 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
485 # try to map a filesystem path to a volume identifier
486 sub path_to_volume_id
{
487 my ($cfg, $path) = @_;
489 my $ids = $cfg->{ids
};
491 my ($sid, $volname) = parse_volume_id
($path, 1);
493 if (my $scfg = $ids->{$sid}) {
495 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
496 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
497 return ($vtype, $path);
503 # Note: abs_path() return undef if $path doesn not exist
504 # for example when nfs storage is not mounted
505 $path = abs_path
($path) || $path;
507 foreach my $sid (keys %$ids) {
508 my $scfg = $ids->{$sid};
509 next if !$scfg->{path
};
510 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
511 my $imagedir = $plugin->get_subdir($scfg, 'images');
512 my $isodir = $plugin->get_subdir($scfg, 'iso');
513 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
514 my $backupdir = $plugin->get_subdir($scfg, 'backup');
515 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
516 my $snippetsdir = $plugin->get_subdir($scfg, 'snippets');
518 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
522 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
523 foreach my $info (@$vollist) {
524 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
525 my $volpath = $plugin->path($scfg, $volname, $storeid);
526 if ($volpath eq $path) {
527 return ('images', $info->{volid
});
530 } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) {
532 return ('iso', "$sid:iso/$name");
533 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
535 return ('vztmpl', "$sid:vztmpl/$name");
536 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
538 return ('rootdir', "$sid:rootdir/$vmid");
539 } elsif ($path =~ m!^$backupdir/([^/]+\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) {
541 return ('backup', "$sid:backup/$name");
542 } elsif ($path =~ m!^$snippetsdir/([^/]+)$!) {
544 return ('snippets', "$sid:snippets/$name");
548 # can't map path to volume id
553 my ($cfg, $volid, $snapname) = @_;
555 my ($storeid, $volname) = parse_volume_id
($volid);
557 my $scfg = storage_config
($cfg, $storeid);
559 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
560 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
561 return wantarray ?
($path, $owner, $vtype) : $path;
564 sub abs_filesystem_path
{
565 my ($cfg, $volid) = @_;
568 if (parse_volume_id
($volid, 1)) {
569 activate_volumes
($cfg, [ $volid ]);
570 $path = PVE
::Storage
::path
($cfg, $volid);
573 my $abspath = abs_path
($volid);
574 if ($abspath && $abspath =~ m
|^(/.+)$|) {
575 $path = $1; # untaint any path
580 die "can't find file '$volid'\n" if !($path && -f
$path);
585 my $volname_for_storage = sub {
586 my ($cfg, $volid, $target_storeid) = @_;
588 my (undef, $name, $vmid, undef, undef, undef, $format) = parse_volname
($cfg, $volid);
589 my $target_scfg = storage_config
($cfg, $target_storeid);
591 my (undef, $valid_formats) = PVE
::Storage
::Plugin
::default_format
($target_scfg);
592 my $format_is_valid = grep { $_ eq $format } @$valid_formats;
593 die "unsupported format '$format' for storage type $target_scfg->{type}\n" if !$format_is_valid;
595 (my $name_without_extension = $name) =~ s/\.$format$//;
597 if ($target_scfg->{path
}) {
598 return "$vmid/$name_without_extension.$format";
600 return "$name_without_extension";
604 sub storage_migrate
{
605 my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_;
607 my $base_snapshot = $opts->{base_snapshot
};
608 my $snapshot = $opts->{snapshot
};
609 my $ratelimit_bps = $opts->{ratelimit_bps
};
610 my $insecure = $opts->{insecure
};
611 my $with_snapshots = $opts->{with_snapshots
} ?
1 : 0;
612 my $allow_rename = $opts->{allow_rename
} ?
1 : 0;
614 my ($storeid, $volname) = parse_volume_id
($volid);
616 my $scfg = storage_config
($cfg, $storeid);
618 # no need to migrate shared content
619 return $volid if $storeid eq $target_storeid && $scfg->{shared
};
621 my $tcfg = storage_config
($cfg, $target_storeid);
624 if ($opts->{target_volname
}) {
625 $target_volname = $opts->{target_volname
};
626 } elsif ($scfg->{type
} eq $tcfg->{type
}) {
627 $target_volname = $volname;
629 $target_volname = $volname_for_storage->($cfg, $volid, $target_storeid);
632 my $target_volid = "${target_storeid}:${target_volname}";
634 my $target_ip = $target_sshinfo->{ip
};
636 my $ssh = PVE
::SSHInfo
::ssh_info_to_command
($target_sshinfo);
637 my $ssh_base = PVE
::SSHInfo
::ssh_info_to_command_base
($target_sshinfo);
638 local $ENV{RSYNC_RSH
} = PVE
::Tools
::cmd2string
($ssh_base);
640 my @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ])
641 if defined($ratelimit_bps);
643 my $migration_snapshot;
644 if (!defined($snapshot)) {
645 if ($scfg->{type
} eq 'zfspool') {
646 $migration_snapshot = 1;
647 $snapshot = '__migration__';
651 my @formats = volume_transfer_formats
($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots);
652 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
653 my $format = $formats[0];
655 my $import_fn = '-'; # let pvesm import read from stdin per default
657 my $net = $target_sshinfo->{network
} // $target_sshinfo->{ip
};
658 $import_fn = "tcp://$net";
661 my $target_apiver = 1; # if there is no apiinfo call, assume 1
662 my $get_api_version = [@$ssh, 'pvesm', 'apiinfo'];
663 my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; };
664 eval { run_command
($get_api_version, logfunc
=> $match_api_version); };
666 my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
667 my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
668 if (defined($snapshot)) {
669 push @$send, '-snapshot', $snapshot
671 if ($migration_snapshot) {
672 push @$recv, '-delete-snapshot', $snapshot;
674 push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5;
676 if (defined($base_snapshot)) {
677 # Check if the snapshot exists on the remote side:
678 push @$send, '-base', $base_snapshot;
679 push @$recv, '-base', $base_snapshot;
683 my $pattern = volume_imported_message
(undef, 1);
684 my $match_volid_and_log = sub {
687 $new_volid = $1 if ($line =~ $pattern);
695 volume_snapshot
($cfg, $volid, $snapshot) if $migration_snapshot;
698 open(my $info, '-|', @$recv)
699 or die "receive command failed: $!\n";
700 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
701 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
702 my $socket = IO
::Socket
::IP-
>new(PeerHost
=> $ip, PeerPort
=> $port, Type
=> SOCK_STREAM
)
703 or die "failed to connect to tunnel at $ip:$port\n";
704 # we won't be reading from the socket
705 shutdown($socket, 0);
706 run_command
([$send, @cstream], output
=> '>&'.fileno($socket), errfunc
=> $logfunc);
707 # don't close the connection entirely otherwise the receiving end
708 # might not get all buffered data (and fails with 'connection reset by peer')
709 shutdown($socket, 1);
711 # wait for the remote process to finish
712 while (my $line = <$info>) {
713 $match_volid_and_log->("[$target_sshinfo->{name}] $line");
716 # now close the socket
718 if (!close($info)) { # does waitpid()
719 die "import failed: $!\n" if $!;
720 die "import failed: exit code ".($?>>8)."\n";
723 run_command
([$send, @cstream, $recv], logfunc
=> $match_volid_and_log);
726 die "unable to get ID of the migrated volume\n"
727 if !defined($new_volid) && $target_apiver >= 5;
730 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
731 if ($migration_snapshot) {
732 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
733 warn "could not remove source snapshot: $@\n" if $@;
737 return $new_volid // $target_volid;
741 my ($cfg, $volid, $vmid, $snap) = @_;
743 my ($storeid, $volname) = parse_volume_id
($volid);
745 my $scfg = storage_config
($cfg, $storeid);
747 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
749 activate_storage
($cfg, $storeid);
751 # lock shared storage
752 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
753 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
754 return "$storeid:$volname";
758 sub vdisk_create_base
{
759 my ($cfg, $volid) = @_;
761 my ($storeid, $volname) = parse_volume_id
($volid);
763 my $scfg = storage_config
($cfg, $storeid);
765 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
767 activate_storage
($cfg, $storeid);
769 # lock shared storage
770 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
771 my $volname = $plugin->create_base($storeid, $scfg, $volname);
772 return "$storeid:$volname";
777 my ($cfg, $volid, $snapname) = @_;
779 my ($storeid, $volname) = parse_volume_id
($volid);
781 my $scfg = storage_config
($cfg, $storeid);
783 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
785 return $plugin->map_volume($storeid, $scfg, $volname, $snapname);
789 my ($cfg, $volid, $snapname) = @_;
791 my ($storeid, $volname) = parse_volume_id
($volid);
793 my $scfg = storage_config
($cfg, $storeid);
795 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
797 return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
801 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
803 die "no storage ID specified\n" if !$storeid;
805 PVE
::JSONSchema
::parse_storage_id
($storeid);
807 my $scfg = storage_config
($cfg, $storeid);
809 die "no VMID specified\n" if !$vmid;
811 $vmid = parse_vmid
($vmid);
813 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
815 $fmt = $defformat if !$fmt;
817 activate_storage
($cfg, $storeid);
819 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
821 # lock shared storage
822 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
823 my $old_umask = umask(umask|0037);
824 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
828 return "$storeid:$volname";
833 my ($cfg, $volid) = @_;
835 my ($storeid, $volname) = parse_volume_id
($volid);
836 my $scfg = storage_config
($cfg, $storeid);
837 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
839 activate_storage
($cfg, $storeid);
843 # lock shared storage
844 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
845 # LVM-thin allows deletion of still referenced base volumes!
846 die "base volume '$volname' is still in use by linked clones\n"
847 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
849 my (undef, undef, undef, undef, undef, $isBase, $format) =
850 $plugin->parse_volname($volname);
851 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
854 return if !$cleanup_worker;
856 my $rpcenv = PVE
::RPCEnvironment
::get
();
857 my $authuser = $rpcenv->get_user();
859 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
863 my ($cfg, $storeid, $vmid, $vollist) = @_;
865 my $ids = $cfg->{ids
};
867 storage_check_enabled
($cfg, $storeid) if ($storeid);
871 # prepare/activate/refresh all storages
873 my $storage_list = [];
875 foreach my $volid (@$vollist) {
876 my ($sid, undef) = parse_volume_id
($volid);
877 next if !defined($ids->{$sid});
878 next if !storage_check_enabled
($cfg, $sid, undef, 1);
879 push @$storage_list, $sid;
882 foreach my $sid (keys %$ids) {
883 next if $storeid && $storeid ne $sid;
884 next if !storage_check_enabled
($cfg, $sid, undef, 1);
885 my $content = $ids->{$sid}->{content
};
886 next if !($content->{rootdir
} || $content->{images
});
887 push @$storage_list, $sid;
893 activate_storage_list
($cfg, $storage_list, $cache);
895 foreach my $sid (keys %$ids) {
896 next if $storeid && $storeid ne $sid;
897 next if !storage_check_enabled
($cfg, $sid, undef, 1);
899 my $scfg = $ids->{$sid};
900 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
901 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
902 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
909 my ($cfg, $storeid, $tt) = @_;
911 die "unknown template type '$tt'\n"
912 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets');
914 my $ids = $cfg->{ids
};
916 storage_check_enabled
($cfg, $storeid) if ($storeid);
921 foreach my $sid (keys %$ids) {
922 next if $storeid && $storeid ne $sid;
924 my $scfg = $ids->{$sid};
925 my $type = $scfg->{type
};
927 next if !$scfg->{content
}->{$tt};
929 next if !storage_check_enabled
($cfg, $sid, undef, 1);
931 $res->{$sid} = volume_list
($cfg, $sid, undef, $tt);
938 my ($cfg, $storeid, $vmid, $content) = @_;
940 my @ctypes = qw(rootdir images vztmpl iso backup snippets);
942 my $cts = $content ?
[ $content ] : [ @ctypes ];
944 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
946 $cts = [ grep { defined($scfg->{content
}->{$_}) } @$cts ];
948 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
950 activate_storage
($cfg, $storeid);
952 my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts);
954 @$res = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @$res;
961 my $filename = "/sys/kernel/uevent_seqnum";
964 if (my $fh = IO
::File-
>new($filename, "r")) {
966 if ($line =~ m/^(\d+)$/) {
974 sub activate_storage
{
975 my ($cfg, $storeid, $cache) = @_;
977 $cache = {} if !$cache;
979 my $scfg = storage_check_enabled
($cfg, $storeid);
981 return if $cache->{activated
}->{$storeid};
983 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
985 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
988 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
989 activate_storage
($cfg, $baseid, $cache);
992 if (!$plugin->check_connection($storeid, $scfg)) {
993 die "storage '$storeid' is not online\n";
996 $plugin->activate_storage($storeid, $scfg, $cache);
998 my $newseq = uevent_seqnum
();
1000 # only call udevsettle if there are events
1001 if ($newseq > $cache->{uevent_seqnum
}) {
1003 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1004 $cache->{uevent_seqnum
} = $newseq;
1007 $cache->{activated
}->{$storeid} = 1;
1010 sub activate_storage_list
{
1011 my ($cfg, $storeid_list, $cache) = @_;
1013 $cache = {} if !$cache;
1015 foreach my $storeid (@$storeid_list) {
1016 activate_storage
($cfg, $storeid, $cache);
1020 sub deactivate_storage
{
1021 my ($cfg, $storeid) = @_;
1023 my $scfg = storage_config
($cfg, $storeid);
1024 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1027 $plugin->deactivate_storage($storeid, $scfg, $cache);
1030 sub activate_volumes
{
1031 my ($cfg, $vollist, $snapname) = @_;
1033 return if !($vollist && scalar(@$vollist));
1035 my $storagehash = {};
1036 foreach my $volid (@$vollist) {
1037 my ($storeid, undef) = parse_volume_id
($volid);
1038 $storagehash->{$storeid} = 1;
1043 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1045 foreach my $volid (@$vollist) {
1046 my ($storeid, $volname) = parse_volume_id
($volid);
1047 my $scfg = storage_config
($cfg, $storeid);
1048 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1049 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1053 sub deactivate_volumes
{
1054 my ($cfg, $vollist, $snapname) = @_;
1056 return if !($vollist && scalar(@$vollist));
1061 foreach my $volid (@$vollist) {
1062 my ($storeid, $volname) = parse_volume_id
($volid);
1064 my $scfg = storage_config
($cfg, $storeid);
1065 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1068 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1072 push @errlist, $volid;
1076 die "volume deactivation failed: " . join(' ', @errlist)
1077 if scalar(@errlist);
1081 my ($cfg, $content, $includeformat) = @_;
1083 my $ids = $cfg->{ids
};
1087 my @ctypes = PVE
::Tools
::split_list
($content);
1090 foreach my $storeid (keys %$ids) {
1091 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1093 if (defined($content)) {
1095 foreach my $ctype (@ctypes) {
1096 if ($ids->{$storeid}->{content
}->{$ctype}) {
1101 next if !$want_ctype || !$storage_enabled;
1104 my $type = $ids->{$storeid}->{type
};
1106 $info->{$storeid} = {
1111 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1112 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1114 enabled
=> $storage_enabled ?
1 : 0,
1117 push @$slist, $storeid;
1122 foreach my $storeid (keys %$ids) {
1123 my $scfg = $ids->{$storeid};
1125 next if !$info->{$storeid};
1126 next if !$info->{$storeid}->{enabled
};
1128 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1129 if ($includeformat) {
1130 my $pd = $plugin->plugindata();
1131 $info->{$storeid}->{format
} = $pd->{format
}
1133 $info->{$storeid}->{select_existing
} = $pd->{select_existing
}
1134 if $pd->{select_existing
};
1137 eval { activate_storage
($cfg, $storeid, $cache); };
1143 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
1146 $info->{$storeid}->{total
} = int($total);
1147 $info->{$storeid}->{avail
} = int($avail);
1148 $info->{$storeid}->{used
} = int($used);
1149 $info->{$storeid}->{active
} = $active;
1158 my ($packed_ip, $family);
1160 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1161 $family = $res[0]->{family
};
1162 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1164 if (defined $packed_ip) {
1165 return Socket
::inet_ntop
($family, $packed_ip);
1171 my ($server_in) = @_;
1174 if (!($server = resolv_server
($server_in))) {
1175 die "unable to resolve address for server '${server_in}'\n";
1178 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1181 run_command
($cmd, outfunc
=> sub {
1184 # note: howto handle white spaces in export path??
1185 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1194 my ($server_in, $user, $password, $domain) = @_;
1196 my $server = resolv_server
($server_in);
1197 die "unable to resolve address for server '${server_in}'\n" if !$server;
1199 # we only support Windows 2012 and newer, so just use smb3
1200 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1201 push @$cmd, '-W', $domain if defined($domain);
1203 push @$cmd, '-N' if !defined($password);
1204 local $ENV{USER
} = $user if defined($user);
1205 local $ENV{PASSWD
} = $password if defined($password);
1216 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1218 } elsif ($line =~ m/(NT_STATUS_(\S+))/) {
1220 $err .= "unexpected status: $1\n" if uc($1) ne 'SUCCESS';
1224 # only die if we got no share, else it's just some followup check error
1225 # (like workgroup querying)
1226 raise
($err) if $err && !%$res;
1233 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-Hp', '-o', 'name,avail,used'];
1236 run_command
($cmd, outfunc
=> sub {
1239 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1240 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1241 my $size = $size_str + 0;
1242 my $used = $used_str + 0;
1243 # ignore subvolumes generated by our ZFSPoolPlugin
1244 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1245 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1246 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1254 my ($portal, $noerr) = @_;
1256 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1258 if (my $ip = resolv_server
($server)) {
1260 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1261 return $port ?
"$server:$port" : $server;
1264 return undef if $noerr;
1266 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1271 my ($portal_in) = @_;
1274 if (!($portal = resolv_portal
($portal_in))) {
1275 die "unable to parse/resolve portal address '${portal_in}'\n";
1278 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1281 sub storage_default_format
{
1282 my ($cfg, $storeid) = @_;
1284 my $scfg = storage_config
($cfg, $storeid);
1286 return PVE
::Storage
::Plugin
::default_format
($scfg);
1289 sub vgroup_is_used
{
1290 my ($cfg, $vgname) = @_;
1292 foreach my $storeid (keys %{$cfg->{ids
}}) {
1293 my $scfg = storage_config
($cfg, $storeid);
1294 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1302 sub target_is_used
{
1303 my ($cfg, $target) = @_;
1305 foreach my $storeid (keys %{$cfg->{ids
}}) {
1306 my $scfg = storage_config
($cfg, $storeid);
1307 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1315 sub volume_is_used
{
1316 my ($cfg, $volid) = @_;
1318 foreach my $storeid (keys %{$cfg->{ids
}}) {
1319 my $scfg = storage_config
($cfg, $storeid);
1320 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1328 sub storage_is_used
{
1329 my ($cfg, $storeid) = @_;
1331 foreach my $sid (keys %{$cfg->{ids
}}) {
1332 my $scfg = storage_config
($cfg, $sid);
1333 next if !$scfg->{base
};
1334 my ($st) = parse_volume_id
($scfg->{base
});
1335 return 1 if $st && $st eq $storeid;
1342 my ($list, $func) = @_;
1346 foreach my $sid (keys %$list) {
1347 foreach my $info (@{$list->{$sid}}) {
1348 my $volid = $info->{volid
};
1349 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1350 if ($sid1 && $sid1 eq $sid) {
1351 &$func ($volid, $sid, $info);
1353 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1359 sub decompressor_info
{
1360 my ($format, $comp) = @_;
1362 if ($format eq 'tgz' && !defined($comp)) {
1363 ($format, $comp) = ('tar', 'gz');
1366 my $decompressor = {
1368 gz
=> ['tar', '-z'],
1369 lzo
=> ['tar', '--lzop'],
1370 zst
=> ['tar', '--zstd'],
1374 lzo
=> ['lzop', '-d', '-c'],
1375 zst
=> ['zstd', '-q', '-d', '-c'],
1379 die "ERROR: archive format not defined\n"
1380 if !defined($decompressor->{$format});
1382 my $decomp = $decompressor->{$format}->{$comp} if $comp;
1386 compression
=> $comp,
1387 decompressor
=> $decomp,
1394 my ($archive) = shift;
1397 my $volid = basename
($archive);
1398 if ($volid =~ /^(vzdump-(lxc|openvz|qemu)-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)$/) {
1399 my $filename = "$1"; # untaint
1400 my ($type, $format, $comp) = ($2, $3, $4);
1401 my $format_re = defined($comp) ?
"$format.$comp" : "$format";
1402 $info = decompressor_info
($format, $comp);
1403 $info->{filename
} = $filename;
1404 $info->{type
} = $type;
1406 if ($volid =~ /^(vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2}))\.${format_re}$/) {
1407 $info->{logfilename
} = "$1.log";
1408 $info->{vmid
} = int($2);
1409 $info->{ctime
} = timelocal
($8, $7, $6, $5, $4 - 1, $3);
1410 $info->{is_std_name
} = 1;
1412 $info->{is_std_name
} = 0;
1415 die "ERROR: couldn't determine archive info from '$archive'\n";
1421 sub archive_remove
{
1422 my ($archive_path) = @_;
1424 my $dirname = dirname
($archive_path);
1425 my $archive_info = eval { archive_info
($archive_path) } // {};
1426 my $logfn = $archive_info->{logfilename
};
1428 unlink $archive_path or die "removing archive $archive_path failed: $!\n";
1430 if (defined($logfn)) {
1431 my $logpath = "$dirname/$logfn";
1433 unlink $logpath or warn "removing log file $logpath failed: $!\n";
1438 sub extract_vzdump_config_tar
{
1439 my ($archive, $conf_re) = @_;
1441 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1443 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1444 die "unable to open file '$archive'\n";
1447 while (defined($file = <$fh>)) {
1448 if ($file =~ $conf_re) {
1449 $file = $1; # untaint
1458 die "ERROR: archive contains no configuration file\n" if !$file;
1464 $raw .= "$output\n";
1467 run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1469 return wantarray ?
($raw, $file) : $raw;
1472 sub extract_vzdump_config_vma
{
1473 my ($archive, $comp) = @_;
1476 my $out = sub { $raw .= "$_[0]\n"; };
1478 my $info = archive_info
($archive);
1479 $comp //= $info->{compression
};
1480 my $decompressor = $info->{decompressor
};
1483 my $cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ];
1485 # lzop/zcat exits with 1 when the pipe is closed early by vma, detect this and ignore the exit code later
1490 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/ || $output =~ m/zstd: error 70 : Write error : Broken pipe/) {
1492 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1493 $errstring = "Failed to extract config from VMA archive: $output\n";
1497 my $rc = eval { run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1) };
1500 $broken_pipe ||= $rc == 141; # broken pipe from vma POV
1502 if (!$errstring && !$broken_pipe && $rc != 0) {
1503 die "$rerr\n" if $rerr;
1504 die "config extraction failed with exit code $rc\n";
1506 die "$errstring\n" if $errstring;
1508 run_command
(["vma", "config", $archive], outfunc
=> $out);
1511 return wantarray ?
($raw, undef) : $raw;
1514 sub extract_vzdump_config
{
1515 my ($cfg, $volid) = @_;
1517 my ($storeid, $volname) = parse_volume_id
($volid);
1518 if (defined($storeid)) {
1519 my $scfg = storage_config
($cfg, $storeid);
1520 if ($scfg->{type
} eq 'pbs') {
1521 storage_check_enabled
($cfg, $storeid);
1522 return PVE
::Storage
::PBSPlugin-
>extract_vzdump_config($scfg, $volname, $storeid);
1526 my $archive = abs_filesystem_path
($cfg, $volid);
1527 my $info = archive_info
($archive);
1528 my $format = $info->{format
};
1529 my $comp = $info->{compression
};
1530 my $type = $info->{type
};
1532 if ($type eq 'lxc' || $type eq 'openvz') {
1533 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1534 } elsif ($type eq 'qemu') {
1535 if ($format eq 'tar') {
1536 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1538 return extract_vzdump_config_vma
($archive, $comp);
1541 die "cannot determine backup guest type for backup archive '$volid'\n";
1546 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1548 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1549 die "cannot export volume '$volid'\n" if !$storeid;
1550 my $scfg = storage_config
($cfg, $storeid);
1551 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1552 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1553 $snapshot, $base_snapshot, $with_snapshots);
1557 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1559 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1560 die "cannot import into volume '$volid'\n" if !$storeid;
1561 my $scfg = storage_config
($cfg, $storeid);
1562 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1563 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1564 $base_snapshot, $with_snapshots, $allow_rename) // $volid;
1567 sub volume_export_formats
{
1568 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1570 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1571 return if !$storeid;
1572 my $scfg = storage_config
($cfg, $storeid);
1573 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1574 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1575 $snapshot, $base_snapshot,
1579 sub volume_import_formats
{
1580 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1582 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1583 return if !$storeid;
1584 my $scfg = storage_config
($cfg, $storeid);
1585 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1586 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1587 $base_snapshot, $with_snapshots);
1590 sub volume_transfer_formats
{
1591 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1592 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1593 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1594 my %import_hash = map { $_ => 1 } @import_formats;
1595 my @common = grep { $import_hash{$_} } @export_formats;
1599 sub volume_imported_message
{
1600 my ($volid, $want_pattern) = @_;
1602 if ($want_pattern) {
1603 return qr/successfully imported '([^']*)'$/;
1605 return "successfully imported '$volid'\n";
1609 # bash completion helper
1611 sub complete_storage
{
1612 my ($cmdname, $pname, $cvalue) = @_;
1614 my $cfg = PVE
::Storage
::config
();
1616 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1619 sub complete_storage_enabled
{
1620 my ($cmdname, $pname, $cvalue) = @_;
1624 my $cfg = PVE
::Storage
::config
();
1625 foreach my $sid (keys %{$cfg->{ids
}}) {
1626 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1632 sub complete_content_type
{
1633 my ($cmdname, $pname, $cvalue) = @_;
1635 return [qw(rootdir images vztmpl iso backup snippets)];
1638 sub complete_volume
{
1639 my ($cmdname, $pname, $cvalue) = @_;
1643 my $storage_list = complete_storage_enabled
();
1645 if ($cvalue =~ m/^([^:]+):/) {
1646 $storage_list = [ $1 ];
1648 if (scalar(@$storage_list) > 1) {
1649 # only list storage IDs to avoid large listings
1651 foreach my $storeid (@$storage_list) {
1652 # Hack: simply return 2 artificial values, so that
1653 # completions does not finish
1654 push @$res, "$storeid:volname", "$storeid:...";
1661 foreach my $storeid (@$storage_list) {
1662 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1664 foreach my $item (@$vollist) {
1665 push @$res, $item->{volid
};
1672 # Various io-heavy operations require io/bandwidth limits which can be
1673 # configured on multiple levels: The global defaults in datacenter.cfg, and
1674 # per-storage overrides. When we want to do a restore from storage A to storage
1675 # B, we should take the smaller limit defined for storages A and B, and if no
1676 # such limit was specified, use the one from datacenter.cfg.
1677 sub get_bandwidth_limit
{
1678 my ($operation, $storage_list, $override) = @_;
1680 # called for each limit (global, per-storage) with the 'default' and the
1681 # $operation limit and should udpate $override for every limit affecting
1683 my $use_global_limits = 0;
1684 my $apply_limit = sub {
1686 if (defined($bwlimit)) {
1687 my $limits = PVE
::JSONSchema
::parse_property_string
('bwlimit', $bwlimit);
1688 my $limit = $limits->{$operation} // $limits->{default};
1689 if (defined($limit)) {
1690 if (!$override || $limit < $override) {
1696 # If there was no applicable limit, try to apply the global ones.
1697 $use_global_limits = 1;
1700 my ($rpcenv, $authuser);
1701 if (defined($override)) {
1702 $rpcenv = PVE
::RPCEnvironment-
>get();
1703 $authuser = $rpcenv->get_user();
1706 # Apply per-storage limits - if there are storages involved.
1707 if (defined($storage_list) && @$storage_list) {
1708 my $config = config
();
1710 # The Datastore.Allocate permission allows us to modify the per-storage
1711 # limits, therefore it also allows us to override them.
1712 # Since we have most likely multiple storages to check, do a quick check on
1713 # the general '/storage' path to see if we can skip the checks entirely:
1714 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
1717 foreach my $storage (@$storage_list) {
1718 next if !defined($storage);
1719 # Avoid duplicate checks:
1720 next if $done{$storage};
1721 $done{$storage} = 1;
1723 # Otherwise we may still have individual /storage/$ID permissions:
1724 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
1725 # And if not: apply the limits.
1726 my $storecfg = storage_config
($config, $storage);
1727 $apply_limit->($storecfg->{bwlimit
});
1731 # Storage limits take precedence over the datacenter defaults, so if
1732 # a limit was applied:
1733 return $override if !$use_global_limits;
1736 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1737 # global default limits.
1738 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
1739 # So if we cannot modify global limits, apply them to our currently
1740 # requested override.
1741 my $dc = cfs_read_file
('datacenter.cfg');
1742 $apply_limit->($dc->{bwlimit
});
1748 # checks if the storage id is available and dies if not
1749 sub assert_sid_unused
{
1753 if (my $scfg = storage_config
($cfg, $sid, 1)) {
1754 die "storage ID '$sid' already defined\n";