16 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
17 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
18 use PVE
::DataCenterConfig
;
19 use PVE
::Exception
qw(raise_param_exc);
22 use PVE
::RPCEnvironment
;
25 use PVE
::Storage
::Plugin
;
26 use PVE
::Storage
::DirPlugin
;
27 use PVE
::Storage
::LVMPlugin
;
28 use PVE
::Storage
::LvmThinPlugin
;
29 use PVE
::Storage
::NFSPlugin
;
30 use PVE
::Storage
::CIFSPlugin
;
31 use PVE
::Storage
::ISCSIPlugin
;
32 use PVE
::Storage
::RBDPlugin
;
33 use PVE
::Storage
::CephFSPlugin
;
34 use PVE
::Storage
::ISCSIDirectPlugin
;
35 use PVE
::Storage
::GlusterfsPlugin
;
36 use PVE
::Storage
::ZFSPoolPlugin
;
37 use PVE
::Storage
::ZFSPlugin
;
38 use PVE
::Storage
::DRBDPlugin
;
39 use PVE
::Storage
::PBSPlugin
;
41 # Storage API version. Icrement it on changes in storage API interface.
42 use constant APIVER
=> 5;
43 # Age is the number of versions we're backward compatible with.
44 # This is like having 'current=APIVER' and age='APIAGE' in libtool,
45 # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
46 use constant APIAGE
=> 4;
48 # load standard plugins
49 PVE
::Storage
::DirPlugin-
>register();
50 PVE
::Storage
::LVMPlugin-
>register();
51 PVE
::Storage
::LvmThinPlugin-
>register();
52 PVE
::Storage
::NFSPlugin-
>register();
53 PVE
::Storage
::CIFSPlugin-
>register();
54 PVE
::Storage
::ISCSIPlugin-
>register();
55 PVE
::Storage
::RBDPlugin-
>register();
56 PVE
::Storage
::CephFSPlugin-
>register();
57 PVE
::Storage
::ISCSIDirectPlugin-
>register();
58 PVE
::Storage
::GlusterfsPlugin-
>register();
59 PVE
::Storage
::ZFSPoolPlugin-
>register();
60 PVE
::Storage
::ZFSPlugin-
>register();
61 PVE
::Storage
::DRBDPlugin-
>register();
62 PVE
::Storage
::PBSPlugin-
>register();
64 # load third-party plugins
65 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
66 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
68 my $modname = 'PVE::Storage::Custom::' . $file;
69 $modname =~ s!\.pm$!!;
70 $file = 'PVE/Storage/Custom/' . $file;
75 # Check perl interface:
76 die "not derived from PVE::Storage::Plugin\n"
77 if !$modname->isa('PVE::Storage::Plugin');
78 die "does not provide an api() method\n"
79 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;
90 # If we got this far and the API version is not the same, make some
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 my $UDEVADM = '/sbin/udevadm';
106 our $iso_extension_re = qr/\.(?:iso|img)/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;
131 my ($cfg, $storeid, $noerr) = @_;
133 die "no storage ID specified\n" if !$storeid;
135 my $scfg = $cfg->{ids
}->{$storeid};
137 die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg);
142 sub storage_check_node
{
143 my ($cfg, $storeid, $node, $noerr) = @_;
145 my $scfg = storage_config
($cfg, $storeid);
147 if ($scfg->{nodes
}) {
148 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
149 if (!$scfg->{nodes
}->{$node}) {
150 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
158 sub storage_check_enabled
{
159 my ($cfg, $storeid, $node, $noerr) = @_;
161 my $scfg = storage_config
($cfg, $storeid);
163 if ($scfg->{disable
}) {
164 die "storage '$storeid' is disabled\n" if !$noerr;
168 return storage_check_node
($cfg, $storeid, $node, $noerr);
171 # storage_can_replicate:
172 # return true if storage supports replication
173 # (volumes alocated with vdisk_alloc() has replication feature)
174 sub storage_can_replicate
{
175 my ($cfg, $storeid, $format) = @_;
177 my $scfg = storage_config
($cfg, $storeid);
178 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
179 return $plugin->storage_can_replicate($scfg, $storeid, $format);
185 return keys %{$cfg->{ids
}};
189 my ($filename, $timeout) = @_;
191 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
194 sub volume_size_info
{
195 my ($cfg, $volid, $timeout) = @_;
197 my ($storeid, $volname) = parse_volume_id
($volid, 1);
199 my $scfg = storage_config
($cfg, $storeid);
200 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
201 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
202 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
203 return file_size_info
($volid, $timeout);
210 my ($cfg, $volid, $size, $running) = @_;
212 my $padding = (1024 - $size % 1024) % 1024;
213 $size = $size + $padding;
215 my ($storeid, $volname) = parse_volume_id
($volid, 1);
217 my $scfg = storage_config
($cfg, $storeid);
218 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
219 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
220 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
221 die "resize file/device '$volid' is not possible\n";
223 die "unable to parse volume ID '$volid'\n";
227 sub volume_rollback_is_possible
{
228 my ($cfg, $volid, $snap) = @_;
230 my ($storeid, $volname) = parse_volume_id
($volid, 1);
232 my $scfg = storage_config
($cfg, $storeid);
233 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
234 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
235 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
236 die "snapshot rollback file/device '$volid' is not possible\n";
238 die "unable to parse volume ID '$volid'\n";
242 sub volume_snapshot
{
243 my ($cfg, $volid, $snap) = @_;
245 my ($storeid, $volname) = parse_volume_id
($volid, 1);
247 my $scfg = storage_config
($cfg, $storeid);
248 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
249 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
250 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
251 die "snapshot file/device '$volid' is not possible\n";
253 die "unable to parse volume ID '$volid'\n";
257 sub volume_snapshot_rollback
{
258 my ($cfg, $volid, $snap) = @_;
260 my ($storeid, $volname) = parse_volume_id
($volid, 1);
262 my $scfg = storage_config
($cfg, $storeid);
263 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
264 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
265 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
266 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
267 die "snapshot rollback file/device '$volid' is not possible\n";
269 die "unable to parse volume ID '$volid'\n";
273 sub volume_snapshot_delete
{
274 my ($cfg, $volid, $snap, $running) = @_;
276 my ($storeid, $volname) = parse_volume_id
($volid, 1);
278 my $scfg = storage_config
($cfg, $storeid);
279 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
280 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
281 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
282 die "snapshot delete file/device '$volid' is not possible\n";
284 die "unable to parse volume ID '$volid'\n";
288 sub volume_has_feature
{
289 my ($cfg, $feature, $volid, $snap, $running, $opts) = @_;
291 my ($storeid, $volname) = parse_volume_id
($volid, 1);
293 my $scfg = storage_config
($cfg, $storeid);
294 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
295 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts);
296 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
303 sub volume_snapshot_list
{
304 my ($cfg, $volid) = @_;
306 my ($storeid, $volname) = parse_volume_id
($volid, 1);
308 my $scfg = storage_config
($cfg, $storeid);
309 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
310 return $plugin->volume_snapshot_list($scfg, $storeid, $volname);
311 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
312 die "send file/device '$volid' is not possible\n";
314 die "unable to parse volume ID '$volid'\n";
316 # return an empty array if dataset does not exist.
320 my ($cfg, $storeid, $vmid) = @_;
322 my $scfg = storage_config
($cfg, $storeid);
323 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
325 my $path = $plugin->get_subdir($scfg, 'images');
327 return $vmid ?
"$path/$vmid" : $path;
330 sub get_private_dir
{
331 my ($cfg, $storeid, $vmid) = @_;
333 my $scfg = storage_config
($cfg, $storeid);
334 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
336 my $path = $plugin->get_subdir($scfg, 'rootdir');
338 return $vmid ?
"$path/$vmid" : $path;
342 my ($cfg, $storeid) = @_;
344 my $scfg = storage_config
($cfg, $storeid);
345 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
347 return $plugin->get_subdir($scfg, 'iso');
351 my ($cfg, $storeid) = @_;
353 my $scfg = storage_config
($cfg, $storeid);
354 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
356 return $plugin->get_subdir($scfg, 'vztmpl');
360 my ($cfg, $storeid) = @_;
362 my $scfg = storage_config
($cfg, $storeid);
363 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
365 return $plugin->get_subdir($scfg, 'backup');
368 # library implementation
373 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
378 # NOTE: basename and basevmid are always undef for LVM-thin, where the
379 # clone -> base reference is not encoded in the volume ID.
380 # see note in PVE::Storage::LvmThinPlugin for details.
382 my ($cfg, $volid) = @_;
384 my ($storeid, $volname) = parse_volume_id
($volid);
386 my $scfg = storage_config
($cfg, $storeid);
388 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
390 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
392 return $plugin->parse_volname($volname);
395 sub parse_volume_id
{
396 my ($volid, $noerr) = @_;
398 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
401 # test if we have read access to volid
402 sub check_volume_access
{
403 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
405 my ($sid, $volname) = parse_volume_id
($volid, 1);
407 my ($vtype, undef, $ownervm) = parse_volname
($cfg, $volid);
408 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
409 # require at least read access to storage, (custom) templates/ISOs could be sensitive
410 $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']);
411 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
412 # we are owner - allow access
413 } elsif ($vtype eq 'backup' && $ownervm) {
414 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
415 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
417 # allow if we are Datastore administrator
418 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
421 die "Only root can pass arbitrary filesystem paths."
422 if $user ne 'root@pam';
428 my $volume_is_base_and_used__no_lock = sub {
429 my ($scfg, $storeid, $plugin, $volname) = @_;
431 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
432 $plugin->parse_volname($volname);
435 my $vollist = $plugin->list_images($storeid, $scfg);
436 foreach my $info (@$vollist) {
437 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
438 my $basename = undef;
439 my $basevmid = undef;
442 (undef, undef, undef, $basename, $basevmid) =
443 $plugin->parse_volname($tmpvolname);
446 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
454 # NOTE: this check does not work for LVM-thin, where the clone -> base
455 # reference is not encoded in the volume ID.
456 # see note in PVE::Storage::LvmThinPlugin for details.
457 sub volume_is_base_and_used
{
458 my ($cfg, $volid) = @_;
460 my ($storeid, $volname) = parse_volume_id
($volid);
461 my $scfg = storage_config
($cfg, $storeid);
462 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
464 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
465 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
469 # try to map a filesystem path to a volume identifier
470 sub path_to_volume_id
{
471 my ($cfg, $path) = @_;
473 my $ids = $cfg->{ids
};
475 my ($sid, $volname) = parse_volume_id
($path, 1);
477 if (my $scfg = $ids->{$sid}) {
479 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
480 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
481 return ($vtype, $path);
487 # Note: abs_path() return undef if $path doesn not exist
488 # for example when nfs storage is not mounted
489 $path = abs_path
($path) || $path;
491 foreach my $sid (keys %$ids) {
492 my $scfg = $ids->{$sid};
493 next if !$scfg->{path
};
494 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
495 my $imagedir = $plugin->get_subdir($scfg, 'images');
496 my $isodir = $plugin->get_subdir($scfg, 'iso');
497 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
498 my $backupdir = $plugin->get_subdir($scfg, 'backup');
499 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
501 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
505 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
506 foreach my $info (@$vollist) {
507 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
508 my $volpath = $plugin->path($scfg, $volname, $storeid);
509 if ($volpath eq $path) {
510 return ('images', $info->{volid
});
513 } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) {
515 return ('iso', "$sid:iso/$name");
516 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
518 return ('vztmpl', "$sid:vztmpl/$name");
519 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
521 return ('rootdir', "$sid:rootdir/$vmid");
522 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
524 return ('iso', "$sid:backup/$name");
528 # can't map path to volume id
533 my ($cfg, $volid, $snapname) = @_;
535 my ($storeid, $volname) = parse_volume_id
($volid);
537 my $scfg = storage_config
($cfg, $storeid);
539 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
540 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
541 return wantarray ?
($path, $owner, $vtype) : $path;
544 sub abs_filesystem_path
{
545 my ($cfg, $volid) = @_;
548 if (parse_volume_id
($volid, 1)) {
549 activate_volumes
($cfg, [ $volid ]);
550 $path = PVE
::Storage
::path
($cfg, $volid);
553 my $abspath = abs_path
($volid);
554 if ($abspath && $abspath =~ m
|^(/.+)$|) {
555 $path = $1; # untaint any path
560 die "can't find file '$volid'\n" if !($path && -f
$path);
565 sub storage_migrate
{
566 my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_;
568 my $base_snapshot = $opts->{base_snapshot
};
569 my $snapshot = $opts->{snapshot
};
570 my $ratelimit_bps = $opts->{ratelimit_bps
};
571 my $insecure = $opts->{insecure
};
572 my $with_snapshots = $opts->{with_snapshots
} ?
1 : 0;
573 my $allow_rename = $opts->{allow_rename
} ?
1 : 0;
575 my ($storeid, $volname) = parse_volume_id
($volid);
576 my $target_volname = $opts->{target_volname
} || $volname;
578 my $scfg = storage_config
($cfg, $storeid);
580 # no need to migrate shared content
581 return $volid if $storeid eq $target_storeid && $scfg->{shared
};
583 my $tcfg = storage_config
($cfg, $target_storeid);
585 my $vtype = (parse_volname
($cfg, $volid))[0];
587 die "content type '$vtype' is not available on storage '$target_storeid'\n"
588 if !$tcfg->{content
}->{$vtype};
590 my $target_volid = "${target_storeid}:${target_volname}";
592 my $target_ip = $target_sshinfo->{ip
};
594 my $ssh = PVE
::SSHInfo
::ssh_info_to_command
($target_sshinfo);
595 my $ssh_base = PVE
::SSHInfo
::ssh_info_to_command_base
($target_sshinfo);
596 local $ENV{RSYNC_RSH
} = PVE
::Tools
::cmd2string
($ssh_base);
598 my @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ])
599 if defined($ratelimit_bps);
601 my $migration_snapshot;
602 if (!defined($snapshot)) {
603 if ($scfg->{type
} eq 'zfspool') {
604 $migration_snapshot = 1;
605 $snapshot = '__migration__';
609 my @formats = volume_transfer_formats
($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots);
610 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
611 my $format = $formats[0];
613 my $import_fn = '-'; # let pvesm import read from stdin per default
615 my $net = $target_sshinfo->{network
} // $target_sshinfo->{ip
};
616 $import_fn = "tcp://$net";
619 my $target_apiver = 1; # if there is no apiinfo call, assume 1
620 my $get_api_version = [@$ssh, 'pvesm', 'apiinfo'];
621 my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; };
622 eval { run_command
($get_api_version, logfunc
=> $match_api_version); };
624 my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
625 my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
626 if (defined($snapshot)) {
627 push @$send, '-snapshot', $snapshot
629 if ($migration_snapshot) {
630 push @$recv, '-delete-snapshot', $snapshot;
632 push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5;
634 if (defined($base_snapshot)) {
635 # Check if the snapshot exists on the remote side:
636 push @$send, '-base', $base_snapshot;
637 push @$recv, '-base', $base_snapshot;
641 my $pattern = volume_imported_message
(undef, 1);
642 my $match_volid_and_log = sub {
645 $new_volid = $1 if ($line =~ $pattern);
653 volume_snapshot
($cfg, $volid, $snapshot) if $migration_snapshot;
656 open(my $info, '-|', @$recv)
657 or die "receive command failed: $!\n";
658 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
659 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
660 my $socket = IO
::Socket
::IP-
>new(PeerHost
=> $ip, PeerPort
=> $port, Type
=> SOCK_STREAM
)
661 or die "failed to connect to tunnel at $ip:$port\n";
662 # we won't be reading from the socket
663 shutdown($socket, 0);
664 run_command
([$send, @cstream], output
=> '>&'.fileno($socket), errfunc
=> $logfunc);
665 # don't close the connection entirely otherwise the receiving end
666 # might not get all buffered data (and fails with 'connection reset by peer')
667 shutdown($socket, 1);
669 # wait for the remote process to finish
670 while (my $line = <$info>) {
671 $match_volid_and_log->("[$target_sshinfo->{name}] $line");
674 # now close the socket
676 if (!close($info)) { # does waitpid()
677 die "import failed: $!\n" if $!;
678 die "import failed: exit code ".($?>>8)."\n";
681 run_command
([$send, @cstream, $recv], logfunc
=> $match_volid_and_log);
684 die "unable to get ID of the migrated volume\n"
685 if !defined($new_volid) && $target_apiver >= 5;
688 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
689 if ($migration_snapshot) {
690 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
691 warn "could not remove source snapshot: $@\n" if $@;
695 return $new_volid // $target_volid;
699 my ($cfg, $volid, $vmid, $snap) = @_;
701 my ($storeid, $volname) = parse_volume_id
($volid);
703 my $scfg = storage_config
($cfg, $storeid);
705 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
707 activate_storage
($cfg, $storeid);
709 # lock shared storage
710 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
711 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
712 return "$storeid:$volname";
716 sub vdisk_create_base
{
717 my ($cfg, $volid) = @_;
719 my ($storeid, $volname) = parse_volume_id
($volid);
721 my $scfg = storage_config
($cfg, $storeid);
723 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
725 activate_storage
($cfg, $storeid);
727 # lock shared storage
728 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
729 my $volname = $plugin->create_base($storeid, $scfg, $volname);
730 return "$storeid:$volname";
735 my ($cfg, $volid, $snapname) = @_;
737 my ($storeid, $volname) = parse_volume_id
($volid);
739 my $scfg = storage_config
($cfg, $storeid);
741 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
743 return $plugin->map_volume($storeid, $scfg, $volname, $snapname);
747 my ($cfg, $volid, $snapname) = @_;
749 my ($storeid, $volname) = parse_volume_id
($volid);
751 my $scfg = storage_config
($cfg, $storeid);
753 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
755 return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
759 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
761 die "no storage ID specified\n" if !$storeid;
763 PVE
::JSONSchema
::parse_storage_id
($storeid);
765 my $scfg = storage_config
($cfg, $storeid);
767 die "no VMID specified\n" if !$vmid;
769 $vmid = parse_vmid
($vmid);
771 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
773 $fmt = $defformat if !$fmt;
775 activate_storage
($cfg, $storeid);
777 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
779 # lock shared storage
780 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
781 my $old_umask = umask(umask|0037);
782 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
786 return "$storeid:$volname";
791 my ($cfg, $volid) = @_;
793 my ($storeid, $volname) = parse_volume_id
($volid);
794 my $scfg = storage_config
($cfg, $storeid);
795 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
797 activate_storage
($cfg, $storeid);
801 # lock shared storage
802 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
803 # LVM-thin allows deletion of still referenced base volumes!
804 die "base volume '$volname' is still in use by linked clones\n"
805 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
807 my (undef, undef, undef, undef, undef, $isBase, $format) =
808 $plugin->parse_volname($volname);
809 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
812 return if !$cleanup_worker;
814 my $rpcenv = PVE
::RPCEnvironment
::get
();
815 my $authuser = $rpcenv->get_user();
817 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
821 my ($cfg, $storeid, $vmid, $vollist) = @_;
823 my $ids = $cfg->{ids
};
825 storage_check_enabled
($cfg, $storeid) if ($storeid);
829 # prepare/activate/refresh all storages
831 my $storage_list = [];
833 foreach my $volid (@$vollist) {
834 my ($sid, undef) = parse_volume_id
($volid);
835 next if !defined($ids->{$sid});
836 next if !storage_check_enabled
($cfg, $sid, undef, 1);
837 push @$storage_list, $sid;
840 foreach my $sid (keys %$ids) {
841 next if $storeid && $storeid ne $sid;
842 next if !storage_check_enabled
($cfg, $sid, undef, 1);
843 push @$storage_list, $sid;
849 activate_storage_list
($cfg, $storage_list, $cache);
851 foreach my $sid (keys %$ids) {
852 next if $storeid && $storeid ne $sid;
853 next if !storage_check_enabled
($cfg, $sid, undef, 1);
855 my $scfg = $ids->{$sid};
856 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
857 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
858 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
865 my ($cfg, $storeid, $tt) = @_;
867 die "unknown template type '$tt'\n"
868 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets');
870 my $ids = $cfg->{ids
};
872 storage_check_enabled
($cfg, $storeid) if ($storeid);
877 foreach my $sid (keys %$ids) {
878 next if $storeid && $storeid ne $sid;
880 my $scfg = $ids->{$sid};
881 my $type = $scfg->{type
};
883 next if !$scfg->{content
}->{$tt};
885 next if !storage_check_enabled
($cfg, $sid, undef, 1);
887 $res->{$sid} = volume_list
($cfg, $sid, undef, $tt);
894 my ($cfg, $storeid, $vmid, $content) = @_;
896 my @ctypes = qw(rootdir images vztmpl iso backup snippets);
898 my $cts = $content ?
[ $content ] : [ @ctypes ];
900 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
902 $cts = [ grep { defined($scfg->{content
}->{$_}) } @$cts ];
904 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
906 activate_storage
($cfg, $storeid);
908 my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts);
910 @$res = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @$res;
917 my $filename = "/sys/kernel/uevent_seqnum";
920 if (my $fh = IO
::File-
>new($filename, "r")) {
922 if ($line =~ m/^(\d+)$/) {
930 sub activate_storage
{
931 my ($cfg, $storeid, $cache) = @_;
933 $cache = {} if !$cache;
935 my $scfg = storage_check_enabled
($cfg, $storeid);
937 return if $cache->{activated
}->{$storeid};
939 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
941 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
944 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
945 activate_storage
($cfg, $baseid, $cache);
948 if (!$plugin->check_connection($storeid, $scfg)) {
949 die "storage '$storeid' is not online\n";
952 $plugin->activate_storage($storeid, $scfg, $cache);
954 my $newseq = uevent_seqnum
();
956 # only call udevsettle if there are events
957 if ($newseq > $cache->{uevent_seqnum
}) {
959 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
960 $cache->{uevent_seqnum
} = $newseq;
963 $cache->{activated
}->{$storeid} = 1;
966 sub activate_storage_list
{
967 my ($cfg, $storeid_list, $cache) = @_;
969 $cache = {} if !$cache;
971 foreach my $storeid (@$storeid_list) {
972 activate_storage
($cfg, $storeid, $cache);
976 sub deactivate_storage
{
977 my ($cfg, $storeid) = @_;
979 my $scfg = storage_config
($cfg, $storeid);
980 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
983 $plugin->deactivate_storage($storeid, $scfg, $cache);
986 sub activate_volumes
{
987 my ($cfg, $vollist, $snapname) = @_;
989 return if !($vollist && scalar(@$vollist));
991 my $storagehash = {};
992 foreach my $volid (@$vollist) {
993 my ($storeid, undef) = parse_volume_id
($volid);
994 $storagehash->{$storeid} = 1;
999 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1001 foreach my $volid (@$vollist) {
1002 my ($storeid, $volname) = parse_volume_id
($volid);
1003 my $scfg = storage_config
($cfg, $storeid);
1004 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1005 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1009 sub deactivate_volumes
{
1010 my ($cfg, $vollist, $snapname) = @_;
1012 return if !($vollist && scalar(@$vollist));
1017 foreach my $volid (@$vollist) {
1018 my ($storeid, $volname) = parse_volume_id
($volid);
1020 my $scfg = storage_config
($cfg, $storeid);
1021 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1024 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1028 push @errlist, $volid;
1032 die "volume deactivation failed: " . join(' ', @errlist)
1033 if scalar(@errlist);
1037 my ($cfg, $content, $includeformat) = @_;
1039 my $ids = $cfg->{ids
};
1043 my @ctypes = PVE
::Tools
::split_list
($content);
1046 foreach my $storeid (keys %$ids) {
1047 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1049 if (defined($content)) {
1051 foreach my $ctype (@ctypes) {
1052 if ($ids->{$storeid}->{content
}->{$ctype}) {
1057 next if !$want_ctype || !$storage_enabled;
1060 my $type = $ids->{$storeid}->{type
};
1062 $info->{$storeid} = {
1067 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1068 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1070 enabled
=> $storage_enabled ?
1 : 0,
1073 push @$slist, $storeid;
1078 foreach my $storeid (keys %$ids) {
1079 my $scfg = $ids->{$storeid};
1081 next if !$info->{$storeid};
1082 next if !$info->{$storeid}->{enabled
};
1084 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1085 if ($includeformat) {
1086 my $pd = $plugin->plugindata();
1087 $info->{$storeid}->{format
} = $pd->{format
}
1089 $info->{$storeid}->{select_existing
} = $pd->{select_existing
}
1090 if $pd->{select_existing
};
1093 eval { activate_storage
($cfg, $storeid, $cache); };
1099 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
1102 $info->{$storeid}->{total
} = int($total);
1103 $info->{$storeid}->{avail
} = int($avail);
1104 $info->{$storeid}->{used
} = int($used);
1105 $info->{$storeid}->{active
} = $active;
1114 my ($packed_ip, $family);
1116 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1117 $family = $res[0]->{family
};
1118 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1120 if (defined $packed_ip) {
1121 return Socket
::inet_ntop
($family, $packed_ip);
1127 my ($server_in) = @_;
1130 if (!($server = resolv_server
($server_in))) {
1131 die "unable to resolve address for server '${server_in}'\n";
1134 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1137 run_command
($cmd, outfunc
=> sub {
1140 # note: howto handle white spaces in export path??
1141 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1150 my ($server_in, $user, $password, $domain) = @_;
1153 if (!($server = resolv_server
($server_in))) {
1154 die "unable to resolve address for server '${server_in}'\n";
1157 # we support only Windows grater than 2012 cifsscan so use smb3
1158 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1159 if (defined($user)) {
1160 die "password is required" if !defined($password);
1161 push @$cmd, '-U', "$user\%$password";
1162 push @$cmd, '-W', $domain if defined($domain);
1171 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1173 } elsif ($line =~ m/(NT_STATUS_(\S*))/) {
1186 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1189 run_command
($cmd, outfunc
=> sub {
1192 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1193 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1194 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1195 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1196 # ignore subvolumes generated by our ZFSPoolPlugin
1197 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1198 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1199 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1207 my ($portal, $noerr) = @_;
1209 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1211 if (my $ip = resolv_server
($server)) {
1213 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1214 return $port ?
"$server:$port" : $server;
1217 return undef if $noerr;
1219 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1224 my ($portal_in) = @_;
1227 if (!($portal = resolv_portal
($portal_in))) {
1228 die "unable to parse/resolve portal address '${portal_in}'\n";
1231 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1234 sub storage_default_format
{
1235 my ($cfg, $storeid) = @_;
1237 my $scfg = storage_config
($cfg, $storeid);
1239 return PVE
::Storage
::Plugin
::default_format
($scfg);
1242 sub vgroup_is_used
{
1243 my ($cfg, $vgname) = @_;
1245 foreach my $storeid (keys %{$cfg->{ids
}}) {
1246 my $scfg = storage_config
($cfg, $storeid);
1247 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1255 sub target_is_used
{
1256 my ($cfg, $target) = @_;
1258 foreach my $storeid (keys %{$cfg->{ids
}}) {
1259 my $scfg = storage_config
($cfg, $storeid);
1260 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1268 sub volume_is_used
{
1269 my ($cfg, $volid) = @_;
1271 foreach my $storeid (keys %{$cfg->{ids
}}) {
1272 my $scfg = storage_config
($cfg, $storeid);
1273 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1281 sub storage_is_used
{
1282 my ($cfg, $storeid) = @_;
1284 foreach my $sid (keys %{$cfg->{ids
}}) {
1285 my $scfg = storage_config
($cfg, $sid);
1286 next if !$scfg->{base
};
1287 my ($st) = parse_volume_id
($scfg->{base
});
1288 return 1 if $st && $st eq $storeid;
1295 my ($list, $func) = @_;
1299 foreach my $sid (keys %$list) {
1300 foreach my $info (@{$list->{$sid}}) {
1301 my $volid = $info->{volid
};
1302 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1303 if ($sid1 && $sid1 eq $sid) {
1304 &$func ($volid, $sid, $info);
1306 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1312 sub extract_vzdump_config_tar
{
1313 my ($archive, $conf_re) = @_;
1315 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1317 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1318 die "unable to open file '$archive'\n";
1321 while (defined($file = <$fh>)) {
1322 if ($file =~ $conf_re) {
1323 $file = $1; # untaint
1332 die "ERROR: archive contains no configuration file\n" if !$file;
1338 $raw .= "$output\n";
1341 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1343 return wantarray ?
($raw, $file) : $raw;
1346 sub extract_vzdump_config_vma
{
1347 my ($archive, $comp) = @_;
1353 $raw .= "$output\n";
1359 if ($comp eq 'gz') {
1360 $uncomp = ["zcat", $archive];
1361 } elsif ($comp eq 'lzo') {
1362 $uncomp = ["lzop", "-d", "-c", $archive];
1364 die "unknown compression method '$comp'\n";
1366 $cmd = [$uncomp, ["vma", "config", "-"]];
1368 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1369 # closed early by vma, detect this and ignore the exit code later
1374 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1376 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1377 $errstring = "Failed to extract config from VMA archive: $output\n";
1381 # in other cases, the pipeline will exit with exit code 141
1382 # because of the broken pipe, handle / ignore this as well
1385 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1389 # use exit code if no stderr output and not just broken pipe
1390 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1391 die "$rerr\n" if $rerr;
1392 die "config extraction failed with exit code $rc\n";
1394 die "$errstring\n" if $errstring;
1396 # simple case without compression and weird piping behaviour
1397 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1400 return wantarray ?
($raw, undef) : $raw;
1403 sub extract_vzdump_config
{
1404 my ($cfg, $volid) = @_;
1406 my ($storeid, $volname) = parse_volume_id
($volid);
1407 if (defined($storeid)) {
1408 my $scfg = storage_config
($cfg, $storeid);
1409 if ($scfg->{type
} eq 'pbs') {
1410 storage_check_enabled
($cfg, $storeid);
1411 return PVE
::Storage
::PBSPlugin-
>extract_vzdump_config($scfg, $volname, $storeid);
1415 my $archive = abs_filesystem_path
($cfg, $volid);
1417 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1418 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1419 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1427 $comp = $11 if defined($11);
1430 if ($format eq 'tar') {
1431 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1433 return extract_vzdump_config_vma
($archive, $comp);
1436 die "cannot determine backup guest type for backup archive '$volid'\n";
1441 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1443 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1444 die "cannot export volume '$volid'\n" if !$storeid;
1445 my $scfg = storage_config
($cfg, $storeid);
1446 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1447 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1448 $snapshot, $base_snapshot, $with_snapshots);
1452 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1454 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1455 die "cannot import into volume '$volid'\n" if !$storeid;
1456 my $scfg = storage_config
($cfg, $storeid);
1457 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1458 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1459 $base_snapshot, $with_snapshots, $allow_rename) // $volid;
1462 sub volume_export_formats
{
1463 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1465 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1466 return if !$storeid;
1467 my $scfg = storage_config
($cfg, $storeid);
1468 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1469 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1470 $snapshot, $base_snapshot,
1474 sub volume_import_formats
{
1475 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1477 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1478 return if !$storeid;
1479 my $scfg = storage_config
($cfg, $storeid);
1480 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1481 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1482 $base_snapshot, $with_snapshots);
1485 sub volume_transfer_formats
{
1486 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1487 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1488 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1489 my %import_hash = map { $_ => 1 } @import_formats;
1490 my @common = grep { $import_hash{$_} } @export_formats;
1494 sub volume_imported_message
{
1495 my ($volid, $want_pattern) = @_;
1497 if ($want_pattern) {
1498 return qr/successfully imported '([^']*)'$/;
1500 return "successfully imported '$volid'\n";
1504 # bash completion helper
1506 sub complete_storage
{
1507 my ($cmdname, $pname, $cvalue) = @_;
1509 my $cfg = PVE
::Storage
::config
();
1511 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1514 sub complete_storage_enabled
{
1515 my ($cmdname, $pname, $cvalue) = @_;
1519 my $cfg = PVE
::Storage
::config
();
1520 foreach my $sid (keys %{$cfg->{ids
}}) {
1521 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1527 sub complete_content_type
{
1528 my ($cmdname, $pname, $cvalue) = @_;
1530 return [qw(rootdir images vztmpl iso backup snippets)];
1533 sub complete_volume
{
1534 my ($cmdname, $pname, $cvalue) = @_;
1538 my $storage_list = complete_storage_enabled
();
1540 if ($cvalue =~ m/^([^:]+):/) {
1541 $storage_list = [ $1 ];
1543 if (scalar(@$storage_list) > 1) {
1544 # only list storage IDs to avoid large listings
1546 foreach my $storeid (@$storage_list) {
1547 # Hack: simply return 2 artificial values, so that
1548 # completions does not finish
1549 push @$res, "$storeid:volname", "$storeid:...";
1556 foreach my $storeid (@$storage_list) {
1557 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1559 foreach my $item (@$vollist) {
1560 push @$res, $item->{volid
};
1567 # Various io-heavy operations require io/bandwidth limits which can be
1568 # configured on multiple levels: The global defaults in datacenter.cfg, and
1569 # per-storage overrides. When we want to do a restore from storage A to storage
1570 # B, we should take the smaller limit defined for storages A and B, and if no
1571 # such limit was specified, use the one from datacenter.cfg.
1572 sub get_bandwidth_limit
{
1573 my ($operation, $storage_list, $override) = @_;
1575 # called for each limit (global, per-storage) with the 'default' and the
1576 # $operation limit and should udpate $override for every limit affecting
1578 my $use_global_limits = 0;
1579 my $apply_limit = sub {
1581 if (defined($bwlimit)) {
1582 my $limits = PVE
::JSONSchema
::parse_property_string
('bwlimit', $bwlimit);
1583 my $limit = $limits->{$operation} // $limits->{default};
1584 if (defined($limit)) {
1585 if (!$override || $limit < $override) {
1591 # If there was no applicable limit, try to apply the global ones.
1592 $use_global_limits = 1;
1595 my ($rpcenv, $authuser);
1596 if (defined($override)) {
1597 $rpcenv = PVE
::RPCEnvironment-
>get();
1598 $authuser = $rpcenv->get_user();
1601 # Apply per-storage limits - if there are storages involved.
1602 if (defined($storage_list) && @$storage_list) {
1603 my $config = config
();
1605 # The Datastore.Allocate permission allows us to modify the per-storage
1606 # limits, therefore it also allows us to override them.
1607 # Since we have most likely multiple storages to check, do a quick check on
1608 # the general '/storage' path to see if we can skip the checks entirely:
1609 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
1612 foreach my $storage (@$storage_list) {
1613 next if !defined($storage);
1614 # Avoid duplicate checks:
1615 next if $done{$storage};
1616 $done{$storage} = 1;
1618 # Otherwise we may still have individual /storage/$ID permissions:
1619 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
1620 # And if not: apply the limits.
1621 my $storecfg = storage_config
($config, $storage);
1622 $apply_limit->($storecfg->{bwlimit
});
1626 # Storage limits take precedence over the datacenter defaults, so if
1627 # a limit was applied:
1628 return $override if !$use_global_limits;
1631 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1632 # global default limits.
1633 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
1634 # So if we cannot modify global limits, apply them to our currently
1635 # requested override.
1636 my $dc = cfs_read_file
('datacenter.cfg');
1637 $apply_limit->($dc->{bwlimit
});
1643 # checks if the storage id is available and dies if not
1644 sub assert_sid_unused
{
1648 if (my $scfg = storage_config
($cfg, $sid, 1)) {
1649 die "storage ID '$sid' already defined\n";