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
=> 2;
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, $volid, $snapname) = @_;
677 my ($storeid, $volname) = parse_volume_id
($volid);
679 my $scfg = storage_config
($cfg, $storeid);
681 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
683 return $plugin->map_volume($storeid, $scfg, $volname, $snapname);
687 my ($cfg, $volid, $snapname) = @_;
689 my ($storeid, $volname) = parse_volume_id
($volid);
691 my $scfg = storage_config
($cfg, $storeid);
693 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
695 return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
699 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
701 die "no storage ID specified\n" if !$storeid;
703 PVE
::JSONSchema
::parse_storage_id
($storeid);
705 my $scfg = storage_config
($cfg, $storeid);
707 die "no VMID specified\n" if !$vmid;
709 $vmid = parse_vmid
($vmid);
711 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
713 $fmt = $defformat if !$fmt;
715 activate_storage
($cfg, $storeid);
717 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
719 # lock shared storage
720 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
721 my $old_umask = umask(umask|0037);
722 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
726 return "$storeid:$volname";
731 my ($cfg, $volid) = @_;
733 my ($storeid, $volname) = parse_volume_id
($volid);
734 my $scfg = storage_config
($cfg, $storeid);
735 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
737 activate_storage
($cfg, $storeid);
741 # lock shared storage
742 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
743 # LVM-thin allows deletion of still referenced base volumes!
744 die "base volume '$volname' is still in use by linked clones\n"
745 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
747 my (undef, undef, undef, undef, undef, $isBase, $format) =
748 $plugin->parse_volname($volname);
749 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
752 return if !$cleanup_worker;
754 my $rpcenv = PVE
::RPCEnvironment
::get
();
755 my $authuser = $rpcenv->get_user();
757 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
760 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
762 my ($cfg, $storeid, $tt) = @_;
764 die "unknown template type '$tt'\n"
765 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
767 my $ids = $cfg->{ids
};
769 storage_check_enabled
($cfg, $storeid) if ($storeid);
775 foreach my $sid (keys %$ids) {
776 next if $storeid && $storeid ne $sid;
778 my $scfg = $ids->{$sid};
779 my $type = $scfg->{type
};
781 next if !storage_check_enabled
($cfg, $sid, undef, 1);
783 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
784 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
785 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
787 activate_storage
($cfg, $sid);
790 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
792 my $path = $plugin->get_subdir($scfg, $tt);
794 foreach my $fn (<$path/*>) {
799 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
801 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
803 } elsif ($tt eq 'vztmpl') {
804 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
806 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
808 } elsif ($tt eq 'backup') {
809 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
811 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
814 $info->{size
} = -s
$fn;
816 push @{$res->{$sid}}, $info;
821 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
829 my ($cfg, $storeid, $vmid, $vollist) = @_;
831 my $ids = $cfg->{ids
};
833 storage_check_enabled
($cfg, $storeid) if ($storeid);
837 # prepare/activate/refresh all storages
839 my $storage_list = [];
841 foreach my $volid (@$vollist) {
842 my ($sid, undef) = parse_volume_id
($volid);
843 next if !defined($ids->{$sid});
844 next if !storage_check_enabled
($cfg, $sid, undef, 1);
845 push @$storage_list, $sid;
848 foreach my $sid (keys %$ids) {
849 next if $storeid && $storeid ne $sid;
850 next if !storage_check_enabled
($cfg, $sid, undef, 1);
851 push @$storage_list, $sid;
857 activate_storage_list
($cfg, $storage_list, $cache);
859 foreach my $sid (keys %$ids) {
860 next if $storeid && $storeid ne $sid;
861 next if !storage_check_enabled
($cfg, $sid, undef, 1);
863 my $scfg = $ids->{$sid};
864 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
865 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
866 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
873 my ($cfg, $storeid, $vmid, $content) = @_;
875 my @ctypes = qw(images vztmpl iso backup);
877 my $cts = $content ?
[ $content ] : [ @ctypes ];
879 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
882 foreach my $ct (@$cts) {
884 if ($ct eq 'images') {
885 $data = vdisk_list
($cfg, $storeid, $vmid);
886 } elsif ($ct eq 'iso' && !defined($vmid)) {
887 $data = template_list
($cfg, $storeid, 'iso');
888 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
889 $data = template_list
($cfg, $storeid, 'vztmpl');
890 } elsif ($ct eq 'backup') {
891 $data = template_list
($cfg, $storeid, 'backup');
892 foreach my $item (@{$data->{$storeid}}) {
893 if (defined($vmid)) {
894 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
899 next if !$data || !$data->{$storeid};
901 foreach my $item (@{$data->{$storeid}}) {
902 $item->{content
} = $ct;
912 my $filename = "/sys/kernel/uevent_seqnum";
915 if (my $fh = IO
::File-
>new($filename, "r")) {
917 if ($line =~ m/^(\d+)$/) {
925 sub activate_storage
{
926 my ($cfg, $storeid, $cache) = @_;
928 $cache = {} if !$cache;
930 my $scfg = storage_check_enabled
($cfg, $storeid);
932 return if $cache->{activated
}->{$storeid};
934 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
936 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
939 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
940 activate_storage
($cfg, $baseid, $cache);
943 if (!$plugin->check_connection($storeid, $scfg)) {
944 die "storage '$storeid' is not online\n";
947 $plugin->activate_storage($storeid, $scfg, $cache);
949 my $newseq = uevent_seqnum
();
951 # only call udevsettle if there are events
952 if ($newseq > $cache->{uevent_seqnum
}) {
954 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
955 $cache->{uevent_seqnum
} = $newseq;
958 $cache->{activated
}->{$storeid} = 1;
961 sub activate_storage_list
{
962 my ($cfg, $storeid_list, $cache) = @_;
964 $cache = {} if !$cache;
966 foreach my $storeid (@$storeid_list) {
967 activate_storage
($cfg, $storeid, $cache);
971 sub deactivate_storage
{
972 my ($cfg, $storeid) = @_;
974 my $scfg = storage_config
($cfg, $storeid);
975 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
978 $plugin->deactivate_storage($storeid, $scfg, $cache);
981 sub activate_volumes
{
982 my ($cfg, $vollist, $snapname) = @_;
984 return if !($vollist && scalar(@$vollist));
986 my $storagehash = {};
987 foreach my $volid (@$vollist) {
988 my ($storeid, undef) = parse_volume_id
($volid);
989 $storagehash->{$storeid} = 1;
994 activate_storage_list
($cfg, [keys %$storagehash], $cache);
996 foreach my $volid (@$vollist) {
997 my ($storeid, $volname) = parse_volume_id
($volid);
998 my $scfg = storage_config
($cfg, $storeid);
999 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1000 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1004 sub deactivate_volumes
{
1005 my ($cfg, $vollist, $snapname) = @_;
1007 return if !($vollist && scalar(@$vollist));
1012 foreach my $volid (@$vollist) {
1013 my ($storeid, $volname) = parse_volume_id
($volid);
1015 my $scfg = storage_config
($cfg, $storeid);
1016 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1019 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1023 push @errlist, $volid;
1027 die "volume deactivation failed: " . join(' ', @errlist)
1028 if scalar(@errlist);
1032 my ($cfg, $content, $includeformat) = @_;
1034 my $ids = $cfg->{ids
};
1038 my @ctypes = PVE
::Tools
::split_list
($content);
1041 foreach my $storeid (keys %$ids) {
1042 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1044 if (defined($content)) {
1046 foreach my $ctype (@ctypes) {
1047 if ($ids->{$storeid}->{content
}->{$ctype}) {
1052 next if !$want_ctype || !$storage_enabled;
1055 my $type = $ids->{$storeid}->{type
};
1057 $info->{$storeid} = {
1062 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1063 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1065 enabled
=> $storage_enabled ?
1 : 0,
1068 push @$slist, $storeid;
1073 foreach my $storeid (keys %$ids) {
1074 my $scfg = $ids->{$storeid};
1076 next if !$info->{$storeid};
1077 next if !$info->{$storeid}->{enabled
};
1079 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1080 if ($includeformat) {
1081 my $pd = $plugin->plugindata();
1082 $info->{$storeid}->{format
} = $pd->{format
}
1084 $info->{$storeid}->{select_existing
} = $pd->{select_existing
}
1085 if $pd->{select_existing
};
1088 eval { activate_storage
($cfg, $storeid, $cache); };
1094 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
1097 $info->{$storeid}->{total
} = int($total);
1098 $info->{$storeid}->{avail
} = int($avail);
1099 $info->{$storeid}->{used
} = int($used);
1100 $info->{$storeid}->{active
} = $active;
1109 my ($packed_ip, $family);
1111 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1112 $family = $res[0]->{family
};
1113 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1115 if (defined $packed_ip) {
1116 return Socket
::inet_ntop
($family, $packed_ip);
1122 my ($server_in) = @_;
1125 if (!($server = resolv_server
($server_in))) {
1126 die "unable to resolve address for server '${server_in}'\n";
1129 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1132 run_command
($cmd, outfunc
=> sub {
1135 # note: howto handle white spaces in export path??
1136 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1145 my ($server_in, $user, $password, $domain) = @_;
1148 if (!($server = resolv_server
($server_in))) {
1149 die "unable to resolve address for server '${server_in}'\n";
1152 # we support only Windows grater than 2012 cifsscan so use smb3
1153 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1154 if (defined($user)) {
1155 die "password is required" if !defined($password);
1156 push @$cmd, '-U', "$user\%$password";
1157 push @$cmd, '-W', $domain if defined($domain);
1166 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1168 } elsif ($line =~ m/(NT_STATUS_(\S*))/) {
1181 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1184 run_command
($cmd, outfunc
=> sub {
1187 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1188 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1189 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1190 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1191 # ignore subvolumes generated by our ZFSPoolPlugin
1192 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1193 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1194 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1202 my ($portal, $noerr) = @_;
1204 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1206 if (my $ip = resolv_server
($server)) {
1208 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1209 return $port ?
"$server:$port" : $server;
1212 return undef if $noerr;
1214 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1217 # idea is from usbutils package (/usr/bin/usb-devices) script
1218 sub __scan_usb_device
{
1219 my ($res, $devpath, $parent, $level) = @_;
1221 return if ! -d
$devpath;
1222 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1223 my $port = $level ?
int($1 - 1) : 0;
1225 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1226 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1233 speed
=> file_read_firstline
("$devpath/speed"),
1234 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1235 vendid
=> file_read_firstline
("$devpath/idVendor"),
1236 prodid
=> file_read_firstline
("$devpath/idProduct"),
1240 my $usbpath = $devpath;
1241 $usbpath =~ s
|^.*/\d
+\
-||;
1242 $d->{usbpath
} = $usbpath;
1245 my $product = file_read_firstline
("$devpath/product");
1246 $d->{product
} = $product if $product;
1248 my $manu = file_read_firstline
("$devpath/manufacturer");
1249 $d->{manufacturer
} = $manu if $manu;
1251 my $serial => file_read_firstline
("$devpath/serial");
1252 $d->{serial
} = $serial if $serial;
1256 foreach my $subdev (<$devpath/$busnum-*>) {
1257 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1258 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1267 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1268 __scan_usb_device
($devlist, $device, 0, 0);
1275 my ($portal_in) = @_;
1278 if (!($portal = resolv_portal
($portal_in))) {
1279 die "unable to parse/resolve portal address '${portal_in}'\n";
1282 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1285 sub storage_default_format
{
1286 my ($cfg, $storeid) = @_;
1288 my $scfg = storage_config
($cfg, $storeid);
1290 return PVE
::Storage
::Plugin
::default_format
($scfg);
1293 sub vgroup_is_used
{
1294 my ($cfg, $vgname) = @_;
1296 foreach my $storeid (keys %{$cfg->{ids
}}) {
1297 my $scfg = storage_config
($cfg, $storeid);
1298 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1306 sub target_is_used
{
1307 my ($cfg, $target) = @_;
1309 foreach my $storeid (keys %{$cfg->{ids
}}) {
1310 my $scfg = storage_config
($cfg, $storeid);
1311 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1319 sub volume_is_used
{
1320 my ($cfg, $volid) = @_;
1322 foreach my $storeid (keys %{$cfg->{ids
}}) {
1323 my $scfg = storage_config
($cfg, $storeid);
1324 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1332 sub storage_is_used
{
1333 my ($cfg, $storeid) = @_;
1335 foreach my $sid (keys %{$cfg->{ids
}}) {
1336 my $scfg = storage_config
($cfg, $sid);
1337 next if !$scfg->{base
};
1338 my ($st) = parse_volume_id
($scfg->{base
});
1339 return 1 if $st && $st eq $storeid;
1346 my ($list, $func) = @_;
1350 foreach my $sid (keys %$list) {
1351 foreach my $info (@{$list->{$sid}}) {
1352 my $volid = $info->{volid
};
1353 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1354 if ($sid1 && $sid1 eq $sid) {
1355 &$func ($volid, $sid, $info);
1357 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1363 sub extract_vzdump_config_tar
{
1364 my ($archive, $conf_re) = @_;
1366 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1368 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1369 die "unable to open file '$archive'\n";
1372 while (defined($file = <$fh>)) {
1373 if ($file =~ $conf_re) {
1374 $file = $1; # untaint
1383 die "ERROR: archive contains no configuration file\n" if !$file;
1389 $raw .= "$output\n";
1392 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1394 return wantarray ?
($raw, $file) : $raw;
1397 sub extract_vzdump_config_vma
{
1398 my ($archive, $comp) = @_;
1404 $raw .= "$output\n";
1410 if ($comp eq 'gz') {
1411 $uncomp = ["zcat", $archive];
1412 } elsif ($comp eq 'lzo') {
1413 $uncomp = ["lzop", "-d", "-c", $archive];
1415 die "unknown compression method '$comp'\n";
1417 $cmd = [$uncomp, ["vma", "config", "-"]];
1419 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1420 # closed early by vma, detect this and ignore the exit code later
1425 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1427 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1428 $errstring = "Failed to extract config from VMA archive: $output\n";
1432 # in other cases, the pipeline will exit with exit code 141
1433 # because of the broken pipe, handle / ignore this as well
1436 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1440 # use exit code if no stderr output and not just broken pipe
1441 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1442 die "$rerr\n" if $rerr;
1443 die "config extraction failed with exit code $rc\n";
1445 die "$errstring\n" if $errstring;
1447 # simple case without compression and weird piping behaviour
1448 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1451 return wantarray ?
($raw, undef) : $raw;
1454 sub extract_vzdump_config
{
1455 my ($cfg, $volid) = @_;
1457 my $archive = abs_filesystem_path
($cfg, $volid);
1459 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1460 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1461 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1469 $comp = $11 if defined($11);
1472 if ($format eq 'tar') {
1473 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1475 return extract_vzdump_config_vma
($archive, $comp);
1478 die "cannot determine backup guest type for backup archive '$volid'\n";
1483 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1485 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1486 die "cannot export volume '$volid'\n" if !$storeid;
1487 my $scfg = storage_config
($cfg, $storeid);
1488 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1489 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1490 $snapshot, $base_snapshot, $with_snapshots);
1494 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
1496 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1497 die "cannot import into volume '$volid'\n" if !$storeid;
1498 my $scfg = storage_config
($cfg, $storeid);
1499 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1500 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1501 $base_snapshot, $with_snapshots);
1504 sub volume_export_formats
{
1505 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1507 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1508 return if !$storeid;
1509 my $scfg = storage_config
($cfg, $storeid);
1510 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1511 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1512 $snapshot, $base_snapshot,
1516 sub volume_import_formats
{
1517 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1519 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1520 return if !$storeid;
1521 my $scfg = storage_config
($cfg, $storeid);
1522 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1523 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1524 $base_snapshot, $with_snapshots);
1527 sub volume_transfer_formats
{
1528 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1529 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1530 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1531 my %import_hash = map { $_ => 1 } @import_formats;
1532 my @common = grep { $import_hash{$_} } @export_formats;
1536 # bash completion helper
1538 sub complete_storage
{
1539 my ($cmdname, $pname, $cvalue) = @_;
1541 my $cfg = PVE
::Storage
::config
();
1543 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1546 sub complete_storage_enabled
{
1547 my ($cmdname, $pname, $cvalue) = @_;
1551 my $cfg = PVE
::Storage
::config
();
1552 foreach my $sid (keys %{$cfg->{ids
}}) {
1553 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1559 sub complete_content_type
{
1560 my ($cmdname, $pname, $cvalue) = @_;
1562 return [qw(rootdir images vztmpl iso backup)];
1565 sub complete_volume
{
1566 my ($cmdname, $pname, $cvalue) = @_;
1570 my $storage_list = complete_storage_enabled
();
1572 if ($cvalue =~ m/^([^:]+):/) {
1573 $storage_list = [ $1 ];
1575 if (scalar(@$storage_list) > 1) {
1576 # only list storage IDs to avoid large listings
1578 foreach my $storeid (@$storage_list) {
1579 # Hack: simply return 2 artificial values, so that
1580 # completions does not finish
1581 push @$res, "$storeid:volname", "$storeid:...";
1588 foreach my $storeid (@$storage_list) {
1589 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1591 foreach my $item (@$vollist) {
1592 push @$res, $item->{volid
};
1599 # Various io-heavy operations require io/bandwidth limits which can be
1600 # configured on multiple levels: The global defaults in datacenter.cfg, and
1601 # per-storage overrides. When we want to do a restore from storage A to storage
1602 # B, we should take the smaller limit defined for storages A and B, and if no
1603 # such limit was specified, use the one from datacenter.cfg.
1604 sub get_bandwidth_limit
{
1605 my ($operation, $storage_list, $override) = @_;
1607 # called for each limit (global, per-storage) with the 'default' and the
1608 # $operation limit and should udpate $override for every limit affecting
1610 my $use_global_limits = 0;
1611 my $apply_limit = sub {
1613 if (defined($bwlimit)) {
1614 my $limits = PVE
::JSONSchema
::parse_property_string
('bwlimit', $bwlimit);
1615 my $limit = $limits->{$operation} // $limits->{default};
1616 if (defined($limit)) {
1617 if (!$override || $limit < $override) {
1623 # If there was no applicable limit, try to apply the global ones.
1624 $use_global_limits = 1;
1627 my ($rpcenv, $authuser);
1628 if (defined($override)) {
1629 $rpcenv = PVE
::RPCEnvironment-
>get();
1630 $authuser = $rpcenv->get_user();
1633 # Apply per-storage limits - if there are storages involved.
1634 if (@$storage_list) {
1635 my $config = config
();
1637 # The Datastore.Allocate permission allows us to modify the per-storage
1638 # limits, therefore it also allows us to override them.
1639 # Since we have most likely multiple storages to check, do a quick check on
1640 # the general '/storage' path to see if we can skip the checks entirely:
1641 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
1644 foreach my $storage (@$storage_list) {
1645 # Avoid duplicate checks:
1646 next if $done{$storage};
1647 $done{$storage} = 1;
1649 # Otherwise we may still have individual /storage/$ID permissions:
1650 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
1651 # And if not: apply the limits.
1652 my $storecfg = storage_config
($config, $storage);
1653 $apply_limit->($storecfg->{bwlimit
});
1657 # Storage limits take precedence over the datacenter defaults, so if
1658 # a limit was applied:
1659 return $override if !$use_global_limits;
1662 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1663 # global default limits.
1664 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
1665 # So if we cannot modify global limits, apply them to our currently
1666 # requested override.
1667 my $dc = cfs_read_file
('datacenter.cfg');
1668 $apply_limit->($dc->{bwlimit
});
1674 # checks if the storage id is available and dies if not
1675 sub assert_sid_unused
{
1679 if (my $scfg = storage_config
($cfg, $sid, 1)) {
1680 die "storage ID '$sid' already defined\n";