16 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
17 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
18 use PVE
::Exception
qw(raise_param_exc);
21 use PVE
::RPCEnvironment
;
23 use PVE
::Storage
::Plugin
;
24 use PVE
::Storage
::DirPlugin
;
25 use PVE
::Storage
::LVMPlugin
;
26 use PVE
::Storage
::LvmThinPlugin
;
27 use PVE
::Storage
::NFSPlugin
;
28 use PVE
::Storage
::CIFSPlugin
;
29 use PVE
::Storage
::ISCSIPlugin
;
30 use PVE
::Storage
::RBDPlugin
;
31 use PVE
::Storage
::CephFSPlugin
;
32 use PVE
::Storage
::SheepdogPlugin
;
33 use PVE
::Storage
::ISCSIDirectPlugin
;
34 use PVE
::Storage
::GlusterfsPlugin
;
35 use PVE
::Storage
::ZFSPoolPlugin
;
36 use PVE
::Storage
::ZFSPlugin
;
37 use PVE
::Storage
::DRBDPlugin
;
39 # Storage API version. Icrement it on changes in storage API interface.
40 use constant APIVER
=> 1;
42 # load standard plugins
43 PVE
::Storage
::DirPlugin-
>register();
44 PVE
::Storage
::LVMPlugin-
>register();
45 PVE
::Storage
::LvmThinPlugin-
>register();
46 PVE
::Storage
::NFSPlugin-
>register();
47 PVE
::Storage
::CIFSPlugin-
>register();
48 PVE
::Storage
::ISCSIPlugin-
>register();
49 PVE
::Storage
::RBDPlugin-
>register();
50 PVE
::Storage
::CephFSPlugin-
>register();
51 PVE
::Storage
::SheepdogPlugin-
>register();
52 PVE
::Storage
::ISCSIDirectPlugin-
>register();
53 PVE
::Storage
::GlusterfsPlugin-
>register();
54 PVE
::Storage
::ZFSPoolPlugin-
>register();
55 PVE
::Storage
::ZFSPlugin-
>register();
56 PVE
::Storage
::DRBDPlugin-
>register();
58 # load third-party plugins
59 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
60 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
62 my $modname = 'PVE::Storage::Custom::' . $file;
63 $modname =~ s!\.pm$!!;
64 $file = 'PVE/Storage/Custom/' . $file;
71 # Check storage API version and that file is really storage plugin.
72 } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER
) {
79 warn "Error loading storage plugin \"$modname\" because of API version mismatch
. Please
, update it
.\n"
84 # initialize all plugins
85 PVE::Storage::Plugin->init();
87 my $UDEVADM = '/sbin/udevadm';
89 # PVE::Storage utility functions
92 return cfs_read_file("storage
.cfg
");
98 cfs_write_file('storage.cfg', $cfg);
101 sub lock_storage_config {
102 my ($code, $errmsg) = @_;
104 cfs_lock_file("storage
.cfg
", undef, $code);
107 $errmsg ? die "$errmsg: $err" : die $err;
112 my ($cfg, $storeid, $noerr) = @_;
114 die "no storage ID specified
\n" if !$storeid;
116 my $scfg = $cfg->{ids}->{$storeid};
118 die "storage
'$storeid' does not exists\n" if (!$noerr && !$scfg);
123 sub storage_check_node {
124 my ($cfg, $storeid, $node, $noerr) = @_;
126 my $scfg = storage_config($cfg, $storeid);
128 if ($scfg->{nodes}) {
129 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
130 if (!$scfg->{nodes}->{$node}) {
131 die "storage
'$storeid' is not available on node
'$node'\n" if !$noerr;
139 sub storage_check_enabled {
140 my ($cfg, $storeid, $node, $noerr) = @_;
142 my $scfg = storage_config($cfg, $storeid);
144 if ($scfg->{disable}) {
145 die "storage
'$storeid' is disabled
\n" if !$noerr;
149 return storage_check_node($cfg, $storeid, $node, $noerr);
152 # storage_can_replicate:
153 # return true if storage supports replication
154 # (volumes alocated with vdisk_alloc() has replication feature)
155 sub storage_can_replicate {
156 my ($cfg, $storeid, $format) = @_;
158 my $scfg = storage_config($cfg, $storeid);
159 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
160 return $plugin->storage_can_replicate($scfg, $storeid, $format);
166 return keys %{$cfg->{ids}};
170 my ($filename, $timeout) = @_;
172 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
175 sub volume_size_info {
176 my ($cfg, $volid, $timeout) = @_;
178 my ($storeid, $volname) = parse_volume_id($volid, 1);
180 my $scfg = storage_config($cfg, $storeid);
181 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
182 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
183 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
184 return file_size_info($volid, $timeout);
191 my ($cfg, $volid, $size, $running) = @_;
193 my ($storeid, $volname) = parse_volume_id($volid, 1);
195 my $scfg = storage_config($cfg, $storeid);
196 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
197 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
198 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
199 die "resize file
/device
'$volid' is not possible
\n";
201 die "unable to parse volume ID
'$volid'\n";
205 sub volume_rollback_is_possible {
206 my ($cfg, $volid, $snap) = @_;
208 my ($storeid, $volname) = parse_volume_id($volid, 1);
210 my $scfg = storage_config($cfg, $storeid);
211 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
212 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
213 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
214 die "snapshot rollback file
/device
'$volid' is not possible
\n";
216 die "unable to parse volume ID
'$volid'\n";
220 sub volume_snapshot {
221 my ($cfg, $volid, $snap) = @_;
223 my ($storeid, $volname) = parse_volume_id($volid, 1);
225 my $scfg = storage_config($cfg, $storeid);
226 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
227 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
228 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
229 die "snapshot file
/device
'$volid' is not possible
\n";
231 die "unable to parse volume ID
'$volid'\n";
235 sub volume_snapshot_rollback {
236 my ($cfg, $volid, $snap) = @_;
238 my ($storeid, $volname) = parse_volume_id($volid, 1);
240 my $scfg = storage_config($cfg, $storeid);
241 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
242 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
243 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
244 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
245 die "snapshot rollback file
/device
'$volid' is not possible
\n";
247 die "unable to parse volume ID
'$volid'\n";
251 sub volume_snapshot_delete {
252 my ($cfg, $volid, $snap, $running) = @_;
254 my ($storeid, $volname) = parse_volume_id($volid, 1);
256 my $scfg = storage_config($cfg, $storeid);
257 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
258 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
259 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
260 die "snapshot
delete file
/device
'$volid' is not possible
\n";
262 die "unable to parse volume ID
'$volid'\n";
266 sub volume_has_feature {
267 my ($cfg, $feature, $volid, $snap, $running) = @_;
269 my ($storeid, $volname) = parse_volume_id($volid, 1);
271 my $scfg = storage_config($cfg, $storeid);
272 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
273 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
274 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
281 sub volume_snapshot_list {
282 my ($cfg, $volid) = @_;
284 my ($storeid, $volname) = parse_volume_id($volid, 1);
286 my $scfg = storage_config($cfg, $storeid);
287 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
288 return $plugin->volume_snapshot_list($scfg, $storeid, $volname);
289 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
290 die "send file
/device
'$volid' is not possible
\n";
292 die "unable to parse volume ID
'$volid'\n";
294 # return an empty array if dataset does not exist.
298 my ($cfg, $storeid, $vmid) = @_;
300 my $scfg = storage_config($cfg, $storeid);
301 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
303 my $path = $plugin->get_subdir($scfg, 'images');
305 return $vmid ? "$path/$vmid" : $path;
308 sub get_private_dir {
309 my ($cfg, $storeid, $vmid) = @_;
311 my $scfg = storage_config($cfg, $storeid);
312 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
314 my $path = $plugin->get_subdir($scfg, 'rootdir');
316 return $vmid ? "$path/$vmid" : $path;
320 my ($cfg, $storeid) = @_;
322 my $scfg = storage_config($cfg, $storeid);
323 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
325 return $plugin->get_subdir($scfg, 'iso');
329 my ($cfg, $storeid) = @_;
331 my $scfg = storage_config($cfg, $storeid);
332 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
334 return $plugin->get_subdir($scfg, 'vztmpl');
338 my ($cfg, $storeid) = @_;
340 my $scfg = storage_config($cfg, $storeid);
341 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
343 return $plugin->get_subdir($scfg, 'backup');
346 # library implementation
351 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
356 # NOTE: basename and basevmid are always undef for LVM-thin, where the
357 # clone -> base reference is not encoded in the volume ID.
358 # see note in PVE::Storage::LvmThinPlugin for details.
360 my ($cfg, $volid) = @_;
362 my ($storeid, $volname) = parse_volume_id($volid);
364 my $scfg = storage_config($cfg, $storeid);
366 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
368 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
370 return $plugin->parse_volname($volname);
373 sub parse_volume_id {
374 my ($volid, $noerr) = @_;
376 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
379 # test if we have read access to volid
380 sub check_volume_access {
381 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
383 my ($sid, $volname) = parse_volume_id($volid, 1);
385 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
386 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
387 # we simply allow access
388 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
389 # we are owner - allow access
390 } elsif ($vtype eq 'backup' && $ownervm) {
391 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
392 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
394 # allow if we are Datastore administrator
395 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
398 die "Only root can pass arbitrary filesystem paths
."
399 if $user ne 'root@pam';
405 my $volume_is_base_and_used__no_lock = sub {
406 my ($scfg, $storeid, $plugin, $volname) = @_;
408 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
409 $plugin->parse_volname($volname);
412 my $vollist = $plugin->list_images($storeid, $scfg);
413 foreach my $info (@$vollist) {
414 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
415 my $basename = undef;
416 my $basevmid = undef;
419 (undef, undef, undef, $basename, $basevmid) =
420 $plugin->parse_volname($tmpvolname);
423 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
431 # NOTE: this check does not work for LVM-thin, where the clone -> base
432 # reference is not encoded in the volume ID.
433 # see note in PVE::Storage::LvmThinPlugin for details.
434 sub volume_is_base_and_used {
435 my ($cfg, $volid) = @_;
437 my ($storeid, $volname) = parse_volume_id($volid);
438 my $scfg = storage_config($cfg, $storeid);
439 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
441 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
442 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
446 # try to map a filesystem path to a volume identifier
447 sub path_to_volume_id {
448 my ($cfg, $path) = @_;
450 my $ids = $cfg->{ids};
452 my ($sid, $volname) = parse_volume_id($path, 1);
454 if (my $scfg = $ids->{$sid}) {
456 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
457 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
458 return ($vtype, $path);
464 # Note: abs_path() return undef if $path doesn not exist
465 # for example when nfs storage is not mounted
466 $path = abs_path($path) || $path;
468 foreach my $sid (keys %$ids) {
469 my $scfg = $ids->{$sid};
470 next if !$scfg->{path};
471 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
472 my $imagedir = $plugin->get_subdir($scfg, 'images');
473 my $isodir = $plugin->get_subdir($scfg, 'iso');
474 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
475 my $backupdir = $plugin->get_subdir($scfg, 'backup');
476 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
478 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
482 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
483 foreach my $info (@$vollist) {
484 my ($storeid, $volname) = parse_volume_id($info->{volid});
485 my $volpath = $plugin->path($scfg, $volname, $storeid);
486 if ($volpath eq $path) {
487 return ('images', $info->{volid});
490 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
492 return ('iso', "$sid:iso
/$name");
493 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
495 return ('vztmpl', "$sid:vztmpl
/$name");
496 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
498 return ('rootdir', "$sid:rootdir
/$vmid");
499 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
501 return ('iso', "$sid:backup
/$name");
505 # can't map path to volume id
510 my ($cfg, $volid, $snapname) = @_;
512 my ($storeid, $volname) = parse_volume_id($volid);
514 my $scfg = storage_config($cfg, $storeid);
516 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
517 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
518 return wantarray ? ($path, $owner, $vtype) : $path;
521 sub abs_filesystem_path {
522 my ($cfg, $volid) = @_;
525 if (PVE::Storage::parse_volume_id ($volid, 1)) {
526 PVE::Storage::activate_volumes($cfg, [ $volid ]);
527 $path = PVE::Storage::path($cfg, $volid);
530 my $abspath = abs_path($volid);
531 if ($abspath && $abspath =~ m|^(/.+)$|) {
532 $path = $1; # untaint any path
537 die "can
't find file '$volid'\n" if !($path && -f $path);
542 sub storage_migrate {
543 my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots, $logfunc) = @_;
545 my ($storeid, $volname) = parse_volume_id($volid);
546 $target_volname = $volname if !$target_volname;
548 my $scfg = storage_config($cfg, $storeid);
550 # no need to migrate shared content
551 return if $storeid eq $target_storeid && $scfg->{shared};
553 my $tcfg = storage_config($cfg, $target_storeid);
555 my $target_volid = "${target_storeid}:${target_volname}";
557 my $target_ip = $target_sshinfo->{ip};
558 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_sshinfo->{name
}'";
560 my $ssh = PVE::Cluster::ssh_info_to_command($target_sshinfo);
561 my $ssh_base = PVE::Cluster::ssh_info_to_command_base($target_sshinfo);
562 local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base);
564 my @cstream = ([ '/usr/bin
/cstream
', '-t
', $ratelimit_bps ])
565 if defined($ratelimit_bps);
567 my $migration_snapshot;
568 if (!defined($snapshot)) {
569 if ($scfg->{type} eq 'zfspool
') {
570 $migration_snapshot = 1;
571 $snapshot = '__migration__
';
575 my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots);
576 die "cannot migrate from storage type '$scfg->{type
}' to '$tcfg->{type
}'\n" if !@formats;
577 my $format = $formats[0];
581 @insecurecmd = ('pvecm
', 'mtunnel
', '-run-command
', 1);
582 if (my $network = $target_sshinfo->{network}) {
583 push @insecurecmd, '-migration_network
', $network;
587 $with_snapshots = $with_snapshots ? 1 : 0; # sanitize for passing as cli parameter
588 my $send = ['pvesm
', 'export
', $volid, $format, '-', '-with-snapshots
', $with_snapshots];
589 my $recv = [@$ssh, @insecurecmd, '--', 'pvesm
', 'import
', $volid, $format, '-', '-with-snapshots
', $with_snapshots];
590 if (defined($snapshot)) {
591 push @$send, '-snapshot
', $snapshot
593 if ($migration_snapshot) {
594 push @$recv, '-delete-snapshot
', $snapshot;
597 if (defined($base_snapshot)) {
598 # Check if the snapshot exists on the remote side:
599 push @$send, '-base
', $base_snapshot;
600 push @$recv, '-base
', $base_snapshot;
603 volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot;
606 open(my $info, '-|', @$recv)
607 or die "receive command failed: $!\n";
608 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
609 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
610 my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM)
611 or die "failed to connect to tunnel at $ip:$port\n";
612 # we won't be reading from the
socket
613 shutdown($socket, 0);
614 run_command
([$send, @cstream], output
=> '>&'.fileno($socket));
615 # don't close the connection entirely otherwise the receiving end
616 # might not get all buffered data (and fails with 'connection reset by peer')
617 shutdown($socket, 1);
618 1 while <$info>; # wait for the remote process to finish
619 # now close the socket
621 if (!close($info)) { # does waitpid()
622 die "import failed: $!\n" if $!;
623 die "import failed: exit code ".($?>>8)."\n";
626 run_command
([$send, @cstream, $recv], logfunc
=> $logfunc);
630 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
631 if ($migration_snapshot) {
632 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
633 warn "could not remove source snapshot: $@\n" if $@;
639 my ($cfg, $volid, $vmid, $snap) = @_;
641 my ($storeid, $volname) = parse_volume_id
($volid);
643 my $scfg = storage_config
($cfg, $storeid);
645 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
647 activate_storage
($cfg, $storeid);
649 # lock shared storage
650 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
651 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
652 return "$storeid:$volname";
656 sub vdisk_create_base
{
657 my ($cfg, $volid) = @_;
659 my ($storeid, $volname) = parse_volume_id
($volid);
661 my $scfg = storage_config
($cfg, $storeid);
663 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
665 activate_storage
($cfg, $storeid);
667 # lock shared storage
668 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
669 my $volname = $plugin->create_base($storeid, $scfg, $volname);
670 return "$storeid:$volname";
675 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
677 die "no storage ID specified\n" if !$storeid;
679 PVE
::JSONSchema
::parse_storage_id
($storeid);
681 my $scfg = storage_config
($cfg, $storeid);
683 die "no VMID specified\n" if !$vmid;
685 $vmid = parse_vmid
($vmid);
687 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
689 $fmt = $defformat if !$fmt;
691 activate_storage
($cfg, $storeid);
693 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
695 # lock shared storage
696 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
697 my $old_umask = umask(umask|0037);
698 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
702 return "$storeid:$volname";
707 my ($cfg, $volid) = @_;
709 my ($storeid, $volname) = parse_volume_id
($volid);
710 my $scfg = storage_config
($cfg, $storeid);
711 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
713 activate_storage
($cfg, $storeid);
717 # lock shared storage
718 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
719 # LVM-thin allows deletion of still referenced base volumes!
720 die "base volume '$volname' is still in use by linked clones\n"
721 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
723 my (undef, undef, undef, undef, undef, $isBase, $format) =
724 $plugin->parse_volname($volname);
725 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
728 return if !$cleanup_worker;
730 my $rpcenv = PVE
::RPCEnvironment
::get
();
731 my $authuser = $rpcenv->get_user();
733 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
736 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
738 my ($cfg, $storeid, $tt) = @_;
740 die "unknown template type '$tt'\n"
741 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
743 my $ids = $cfg->{ids
};
745 storage_check_enabled
($cfg, $storeid) if ($storeid);
751 foreach my $sid (keys %$ids) {
752 next if $storeid && $storeid ne $sid;
754 my $scfg = $ids->{$sid};
755 my $type = $scfg->{type
};
757 next if !storage_check_enabled
($cfg, $sid, undef, 1);
759 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
760 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
761 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
763 activate_storage
($cfg, $sid);
766 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
768 my $path = $plugin->get_subdir($scfg, $tt);
770 foreach my $fn (<$path/*>) {
775 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
777 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
779 } elsif ($tt eq 'vztmpl') {
780 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
782 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
784 } elsif ($tt eq 'backup') {
785 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
787 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
790 $info->{size
} = -s
$fn;
792 push @{$res->{$sid}}, $info;
797 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
805 my ($cfg, $storeid, $vmid, $vollist) = @_;
807 my $ids = $cfg->{ids
};
809 storage_check_enabled
($cfg, $storeid) if ($storeid);
813 # prepare/activate/refresh all storages
815 my $storage_list = [];
817 foreach my $volid (@$vollist) {
818 my ($sid, undef) = parse_volume_id
($volid);
819 next if !defined($ids->{$sid});
820 next if !storage_check_enabled
($cfg, $sid, undef, 1);
821 push @$storage_list, $sid;
824 foreach my $sid (keys %$ids) {
825 next if $storeid && $storeid ne $sid;
826 next if !storage_check_enabled
($cfg, $sid, undef, 1);
827 push @$storage_list, $sid;
833 activate_storage_list
($cfg, $storage_list, $cache);
835 foreach my $sid (keys %$ids) {
836 next if $storeid && $storeid ne $sid;
837 next if !storage_check_enabled
($cfg, $sid, undef, 1);
839 my $scfg = $ids->{$sid};
840 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
841 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
842 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
849 my ($cfg, $storeid, $vmid, $content) = @_;
851 my @ctypes = qw(images vztmpl iso backup);
853 my $cts = $content ?
[ $content ] : [ @ctypes ];
855 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
858 foreach my $ct (@$cts) {
860 if ($ct eq 'images') {
861 $data = vdisk_list
($cfg, $storeid, $vmid);
862 } elsif ($ct eq 'iso' && !defined($vmid)) {
863 $data = template_list
($cfg, $storeid, 'iso');
864 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
865 $data = template_list
($cfg, $storeid, 'vztmpl');
866 } elsif ($ct eq 'backup') {
867 $data = template_list
($cfg, $storeid, 'backup');
868 foreach my $item (@{$data->{$storeid}}) {
869 if (defined($vmid)) {
870 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
875 next if !$data || !$data->{$storeid};
877 foreach my $item (@{$data->{$storeid}}) {
878 $item->{content
} = $ct;
888 my $filename = "/sys/kernel/uevent_seqnum";
891 if (my $fh = IO
::File-
>new($filename, "r")) {
893 if ($line =~ m/^(\d+)$/) {
901 sub activate_storage
{
902 my ($cfg, $storeid, $cache) = @_;
904 $cache = {} if !$cache;
906 my $scfg = storage_check_enabled
($cfg, $storeid);
908 return if $cache->{activated
}->{$storeid};
910 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
912 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
915 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
916 activate_storage
($cfg, $baseid, $cache);
919 if (!$plugin->check_connection($storeid, $scfg)) {
920 die "storage '$storeid' is not online\n";
923 $plugin->activate_storage($storeid, $scfg, $cache);
925 my $newseq = uevent_seqnum
();
927 # only call udevsettle if there are events
928 if ($newseq > $cache->{uevent_seqnum
}) {
930 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
931 $cache->{uevent_seqnum
} = $newseq;
934 $cache->{activated
}->{$storeid} = 1;
937 sub activate_storage_list
{
938 my ($cfg, $storeid_list, $cache) = @_;
940 $cache = {} if !$cache;
942 foreach my $storeid (@$storeid_list) {
943 activate_storage
($cfg, $storeid, $cache);
947 sub deactivate_storage
{
948 my ($cfg, $storeid) = @_;
950 my $scfg = storage_config
($cfg, $storeid);
951 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
954 $plugin->deactivate_storage($storeid, $scfg, $cache);
957 sub activate_volumes
{
958 my ($cfg, $vollist, $snapname) = @_;
960 return if !($vollist && scalar(@$vollist));
962 my $storagehash = {};
963 foreach my $volid (@$vollist) {
964 my ($storeid, undef) = parse_volume_id
($volid);
965 $storagehash->{$storeid} = 1;
970 activate_storage_list
($cfg, [keys %$storagehash], $cache);
972 foreach my $volid (@$vollist) {
973 my ($storeid, $volname) = parse_volume_id
($volid);
974 my $scfg = storage_config
($cfg, $storeid);
975 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
976 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
980 sub deactivate_volumes
{
981 my ($cfg, $vollist, $snapname) = @_;
983 return if !($vollist && scalar(@$vollist));
988 foreach my $volid (@$vollist) {
989 my ($storeid, $volname) = parse_volume_id
($volid);
991 my $scfg = storage_config
($cfg, $storeid);
992 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
995 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
999 push @errlist, $volid;
1003 die "volume deactivation failed: " . join(' ', @errlist)
1004 if scalar(@errlist);
1008 my ($cfg, $content, $includeformat) = @_;
1010 my $ids = $cfg->{ids
};
1014 my @ctypes = PVE
::Tools
::split_list
($content);
1017 foreach my $storeid (keys %$ids) {
1018 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1020 if (defined($content)) {
1022 foreach my $ctype (@ctypes) {
1023 if ($ids->{$storeid}->{content
}->{$ctype}) {
1028 next if !$want_ctype || !$storage_enabled;
1031 my $type = $ids->{$storeid}->{type
};
1033 $info->{$storeid} = {
1038 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1039 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1041 enabled
=> $storage_enabled ?
1 : 0,
1044 push @$slist, $storeid;
1049 foreach my $storeid (keys %$ids) {
1050 my $scfg = $ids->{$storeid};
1052 next if !$info->{$storeid};
1053 next if !$info->{$storeid}->{enabled
};
1055 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1056 if ($includeformat) {
1057 my $pd = $plugin->plugindata();
1058 $info->{$storeid}->{format
} = $pd->{format
}
1060 $info->{$storeid}->{select_existing
} = $pd->{select_existing
}
1061 if $pd->{select_existing
};
1064 eval { activate_storage
($cfg, $storeid, $cache); };
1070 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
1073 $info->{$storeid}->{total
} = int($total);
1074 $info->{$storeid}->{avail
} = int($avail);
1075 $info->{$storeid}->{used
} = int($used);
1076 $info->{$storeid}->{active
} = $active;
1085 my ($packed_ip, $family);
1087 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1088 $family = $res[0]->{family
};
1089 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1091 if (defined $packed_ip) {
1092 return Socket
::inet_ntop
($family, $packed_ip);
1098 my ($server_in) = @_;
1101 if (!($server = resolv_server
($server_in))) {
1102 die "unable to resolve address for server '${server_in}'\n";
1105 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1108 run_command
($cmd, outfunc
=> sub {
1111 # note: howto handle white spaces in export path??
1112 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1121 my ($server_in, $user, $password, $domain) = @_;
1124 if (!($server = resolv_server
($server_in))) {
1125 die "unable to resolve address for server '${server_in}'\n";
1128 # we support only Windows grater than 2012 cifsscan so use smb3
1129 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1130 if (defined($user)) {
1131 die "password is required" if !defined($password);
1132 push @$cmd, '-U', "$user\%$password";
1133 push @$cmd, '-W', $domain if defined($domain);
1142 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1144 } elsif ($line =~ m/(NT_STATUS_(\S*))/) {
1157 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1160 run_command
($cmd, outfunc
=> sub {
1163 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1164 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1165 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1166 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1167 # ignore subvolumes generated by our ZFSPoolPlugin
1168 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1169 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1170 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1178 my ($portal, $noerr) = @_;
1180 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1182 if (my $ip = resolv_server
($server)) {
1184 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1185 return $port ?
"$server:$port" : $server;
1188 return undef if $noerr;
1190 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1193 # idea is from usbutils package (/usr/bin/usb-devices) script
1194 sub __scan_usb_device
{
1195 my ($res, $devpath, $parent, $level) = @_;
1197 return if ! -d
$devpath;
1198 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1199 my $port = $level ?
int($1 - 1) : 0;
1201 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1202 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1209 speed
=> file_read_firstline
("$devpath/speed"),
1210 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1211 vendid
=> file_read_firstline
("$devpath/idVendor"),
1212 prodid
=> file_read_firstline
("$devpath/idProduct"),
1216 my $usbpath = $devpath;
1217 $usbpath =~ s
|^.*/\d
+\
-||;
1218 $d->{usbpath
} = $usbpath;
1221 my $product = file_read_firstline
("$devpath/product");
1222 $d->{product
} = $product if $product;
1224 my $manu = file_read_firstline
("$devpath/manufacturer");
1225 $d->{manufacturer
} = $manu if $manu;
1227 my $serial => file_read_firstline
("$devpath/serial");
1228 $d->{serial
} = $serial if $serial;
1232 foreach my $subdev (<$devpath/$busnum-*>) {
1233 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1234 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1243 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1244 __scan_usb_device
($devlist, $device, 0, 0);
1251 my ($portal_in) = @_;
1254 if (!($portal = resolv_portal
($portal_in))) {
1255 die "unable to parse/resolve portal address '${portal_in}'\n";
1258 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1261 sub storage_default_format
{
1262 my ($cfg, $storeid) = @_;
1264 my $scfg = storage_config
($cfg, $storeid);
1266 return PVE
::Storage
::Plugin
::default_format
($scfg);
1269 sub vgroup_is_used
{
1270 my ($cfg, $vgname) = @_;
1272 foreach my $storeid (keys %{$cfg->{ids
}}) {
1273 my $scfg = storage_config
($cfg, $storeid);
1274 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1282 sub target_is_used
{
1283 my ($cfg, $target) = @_;
1285 foreach my $storeid (keys %{$cfg->{ids
}}) {
1286 my $scfg = storage_config
($cfg, $storeid);
1287 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1295 sub volume_is_used
{
1296 my ($cfg, $volid) = @_;
1298 foreach my $storeid (keys %{$cfg->{ids
}}) {
1299 my $scfg = storage_config
($cfg, $storeid);
1300 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1308 sub storage_is_used
{
1309 my ($cfg, $storeid) = @_;
1311 foreach my $sid (keys %{$cfg->{ids
}}) {
1312 my $scfg = storage_config
($cfg, $sid);
1313 next if !$scfg->{base
};
1314 my ($st) = parse_volume_id
($scfg->{base
});
1315 return 1 if $st && $st eq $storeid;
1322 my ($list, $func) = @_;
1326 foreach my $sid (keys %$list) {
1327 foreach my $info (@{$list->{$sid}}) {
1328 my $volid = $info->{volid
};
1329 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1330 if ($sid1 && $sid1 eq $sid) {
1331 &$func ($volid, $sid, $info);
1333 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1339 sub extract_vzdump_config_tar
{
1340 my ($archive, $conf_re) = @_;
1342 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1344 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1345 die "unable to open file '$archive'\n";
1348 while (defined($file = <$fh>)) {
1349 if ($file =~ $conf_re) {
1350 $file = $1; # untaint
1359 die "ERROR: archive contains no configuration file\n" if !$file;
1365 $raw .= "$output\n";
1368 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1370 return wantarray ?
($raw, $file) : $raw;
1373 sub extract_vzdump_config_vma
{
1374 my ($archive, $comp) = @_;
1380 $raw .= "$output\n";
1386 if ($comp eq 'gz') {
1387 $uncomp = ["zcat", $archive];
1388 } elsif ($comp eq 'lzo') {
1389 $uncomp = ["lzop", "-d", "-c", $archive];
1391 die "unknown compression method '$comp'\n";
1393 $cmd = [$uncomp, ["vma", "config", "-"]];
1395 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1396 # closed early by vma, detect this and ignore the exit code later
1401 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1403 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1404 $errstring = "Failed to extract config from VMA archive: $output\n";
1408 # in other cases, the pipeline will exit with exit code 141
1409 # because of the broken pipe, handle / ignore this as well
1412 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1416 # use exit code if no stderr output and not just broken pipe
1417 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1418 die "$rerr\n" if $rerr;
1419 die "config extraction failed with exit code $rc\n";
1421 die "$errstring\n" if $errstring;
1423 # simple case without compression and weird piping behaviour
1424 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1427 return wantarray ?
($raw, undef) : $raw;
1430 sub extract_vzdump_config
{
1431 my ($cfg, $volid) = @_;
1433 my $archive = abs_filesystem_path
($cfg, $volid);
1435 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1436 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1437 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1445 $comp = $11 if defined($11);
1448 if ($format eq 'tar') {
1449 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1451 return extract_vzdump_config_vma
($archive, $comp);
1454 die "cannot determine backup guest type for backup archive '$volid'\n";
1459 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1461 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1462 die "cannot export volume '$volid'\n" if !$storeid;
1463 my $scfg = storage_config
($cfg, $storeid);
1464 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1465 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1466 $snapshot, $base_snapshot, $with_snapshots);
1470 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
1472 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1473 die "cannot import into volume '$volid'\n" if !$storeid;
1474 my $scfg = storage_config
($cfg, $storeid);
1475 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1476 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1477 $base_snapshot, $with_snapshots);
1480 sub volume_export_formats
{
1481 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1483 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1484 return if !$storeid;
1485 my $scfg = storage_config
($cfg, $storeid);
1486 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1487 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1488 $snapshot, $base_snapshot,
1492 sub volume_import_formats
{
1493 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1495 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1496 return if !$storeid;
1497 my $scfg = storage_config
($cfg, $storeid);
1498 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1499 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1500 $base_snapshot, $with_snapshots);
1503 sub volume_transfer_formats
{
1504 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1505 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1506 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1507 my %import_hash = map { $_ => 1 } @import_formats;
1508 my @common = grep { $import_hash{$_} } @export_formats;
1512 # bash completion helper
1514 sub complete_storage
{
1515 my ($cmdname, $pname, $cvalue) = @_;
1517 my $cfg = PVE
::Storage
::config
();
1519 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1522 sub complete_storage_enabled
{
1523 my ($cmdname, $pname, $cvalue) = @_;
1527 my $cfg = PVE
::Storage
::config
();
1528 foreach my $sid (keys %{$cfg->{ids
}}) {
1529 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1535 sub complete_content_type
{
1536 my ($cmdname, $pname, $cvalue) = @_;
1538 return [qw(rootdir images vztmpl iso backup)];
1541 sub complete_volume
{
1542 my ($cmdname, $pname, $cvalue) = @_;
1546 my $storage_list = complete_storage_enabled
();
1548 if ($cvalue =~ m/^([^:]+):/) {
1549 $storage_list = [ $1 ];
1551 if (scalar(@$storage_list) > 1) {
1552 # only list storage IDs to avoid large listings
1554 foreach my $storeid (@$storage_list) {
1555 # Hack: simply return 2 artificial values, so that
1556 # completions does not finish
1557 push @$res, "$storeid:volname", "$storeid:...";
1564 foreach my $storeid (@$storage_list) {
1565 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1567 foreach my $item (@$vollist) {
1568 push @$res, $item->{volid
};
1575 # Various io-heavy operations require io/bandwidth limits which can be
1576 # configured on multiple levels: The global defaults in datacenter.cfg, and
1577 # per-storage overrides. When we want to do a restore from storage A to storage
1578 # B, we should take the smaller limit defined for storages A and B, and if no
1579 # such limit was specified, use the one from datacenter.cfg.
1580 sub get_bandwidth_limit
{
1581 my ($operation, $storage_list, $override) = @_;
1583 # called for each limit (global, per-storage) with the 'default' and the
1584 # $operation limit and should udpate $override for every limit affecting
1586 my $use_global_limits = 0;
1587 my $apply_limit = sub {
1589 if (defined($bwlimit)) {
1590 my $limits = PVE
::JSONSchema
::parse_property_string
('bwlimit', $bwlimit);
1591 my $limit = $limits->{$operation} // $limits->{default};
1592 if (defined($limit)) {
1593 if (!$override || $limit < $override) {
1599 # If there was no applicable limit, try to apply the global ones.
1600 $use_global_limits = 1;
1603 my ($rpcenv, $authuser);
1604 if (defined($override)) {
1605 $rpcenv = PVE
::RPCEnvironment-
>get();
1606 $authuser = $rpcenv->get_user();
1609 # Apply per-storage limits - if there are storages involved.
1610 if (@$storage_list) {
1611 my $config = config
();
1613 # The Datastore.Allocate permission allows us to modify the per-storage
1614 # limits, therefore it also allows us to override them.
1615 # Since we have most likely multiple storages to check, do a quick check on
1616 # the general '/storage' path to see if we can skip the checks entirely:
1617 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
1620 foreach my $storage (@$storage_list) {
1621 # Avoid duplicate checks:
1622 next if $done{$storage};
1623 $done{$storage} = 1;
1625 # Otherwise we may still have individual /storage/$ID permissions:
1626 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
1627 # And if not: apply the limits.
1628 my $storecfg = storage_config
($config, $storage);
1629 $apply_limit->($storecfg->{bwlimit
});
1633 # Storage limits take precedence over the datacenter defaults, so if
1634 # a limit was applied:
1635 return $override if !$use_global_limits;
1638 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1639 # global default limits.
1640 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
1641 # So if we cannot modify global limits, apply them to our currently
1642 # requested override.
1643 my $dc = cfs_read_file
('datacenter.cfg');
1644 $apply_limit->($dc->{bwlimit
});
1650 # checks if the storage id is available and dies if not
1651 sub assert_sid_unused
{
1655 if (my $scfg = storage_config
($cfg, $sid, 1)) {
1656 die "storage ID '$sid' already defined\n";