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;
41 # Age is the number of versions we're backward compatible with.
42 # This is like having 'current=APIVER' and age='APIAGE' in libtool,
43 # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
44 use constant APIAGE
=> 1;
46 # load standard plugins
47 PVE
::Storage
::DirPlugin-
>register();
48 PVE
::Storage
::LVMPlugin-
>register();
49 PVE
::Storage
::LvmThinPlugin-
>register();
50 PVE
::Storage
::NFSPlugin-
>register();
51 PVE
::Storage
::CIFSPlugin-
>register();
52 PVE
::Storage
::ISCSIPlugin-
>register();
53 PVE
::Storage
::RBDPlugin-
>register();
54 PVE
::Storage
::CephFSPlugin-
>register();
55 PVE
::Storage
::SheepdogPlugin-
>register();
56 PVE
::Storage
::ISCSIDirectPlugin-
>register();
57 PVE
::Storage
::GlusterfsPlugin-
>register();
58 PVE
::Storage
::ZFSPoolPlugin-
>register();
59 PVE
::Storage
::ZFSPlugin-
>register();
60 PVE
::Storage
::DRBDPlugin-
>register();
62 # load third-party plugins
63 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
64 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
66 my $modname = 'PVE::Storage::Custom::' . $file;
67 $modname =~ s!\.pm$!!;
68 $file = 'PVE/Storage/Custom/' . $file;
73 # Check perl interface:
74 die "not derived from PVE::Storage::Plugin\n"
75 if !$modname->isa('PVE::Storage::Plugin');
76 die "does not provide an api() method\n"
77 if !$modname->can('api');
78 # Check storage API version and that file is really storage plugin.
79 my $version = $modname->api();
80 die "implements an API version newer than current\n"
82 die "API version too old, pluse update the plugin\n"
83 if $version < (APIVER-APIAGE
);
87 # If we got this far and the API version is not the same, make some
89 warn "Plugin \"$modname\" is implementing an older storage API
, an upgrade
is recommended
\n"
90 if $version != APIVER;
93 warn "Error loading storage plugin
\"$modname\": $@";
98 # initialize all plugins
99 PVE
::Storage
::Plugin-
>init();
101 my $UDEVADM = '/sbin/udevadm';
103 # PVE::Storage utility functions
106 return cfs_read_file
("storage.cfg");
112 cfs_write_file
('storage.cfg', $cfg);
115 sub lock_storage_config
{
116 my ($code, $errmsg) = @_;
118 cfs_lock_file
("storage.cfg", undef, $code);
121 $errmsg ?
die "$errmsg: $err" : die $err;
126 my ($cfg, $storeid, $noerr) = @_;
128 die "no storage ID specified\n" if !$storeid;
130 my $scfg = $cfg->{ids
}->{$storeid};
132 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
137 sub storage_check_node
{
138 my ($cfg, $storeid, $node, $noerr) = @_;
140 my $scfg = storage_config
($cfg, $storeid);
142 if ($scfg->{nodes
}) {
143 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
144 if (!$scfg->{nodes
}->{$node}) {
145 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
153 sub storage_check_enabled
{
154 my ($cfg, $storeid, $node, $noerr) = @_;
156 my $scfg = storage_config
($cfg, $storeid);
158 if ($scfg->{disable
}) {
159 die "storage '$storeid' is disabled\n" if !$noerr;
163 return storage_check_node
($cfg, $storeid, $node, $noerr);
166 # storage_can_replicate:
167 # return true if storage supports replication
168 # (volumes alocated with vdisk_alloc() has replication feature)
169 sub storage_can_replicate
{
170 my ($cfg, $storeid, $format) = @_;
172 my $scfg = storage_config
($cfg, $storeid);
173 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
174 return $plugin->storage_can_replicate($scfg, $storeid, $format);
180 return keys %{$cfg->{ids
}};
184 my ($filename, $timeout) = @_;
186 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
189 sub volume_size_info
{
190 my ($cfg, $volid, $timeout) = @_;
192 my ($storeid, $volname) = parse_volume_id
($volid, 1);
194 my $scfg = storage_config
($cfg, $storeid);
195 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
196 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
197 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
198 return file_size_info
($volid, $timeout);
205 my ($cfg, $volid, $size, $running) = @_;
207 my ($storeid, $volname) = parse_volume_id
($volid, 1);
209 my $scfg = storage_config
($cfg, $storeid);
210 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
211 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
212 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
213 die "resize file/device '$volid' is not possible\n";
215 die "unable to parse volume ID '$volid'\n";
219 sub volume_rollback_is_possible
{
220 my ($cfg, $volid, $snap) = @_;
222 my ($storeid, $volname) = parse_volume_id
($volid, 1);
224 my $scfg = storage_config
($cfg, $storeid);
225 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
226 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
227 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
228 die "snapshot rollback file/device '$volid' is not possible\n";
230 die "unable to parse volume ID '$volid'\n";
234 sub volume_snapshot
{
235 my ($cfg, $volid, $snap) = @_;
237 my ($storeid, $volname) = parse_volume_id
($volid, 1);
239 my $scfg = storage_config
($cfg, $storeid);
240 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
241 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
242 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
243 die "snapshot file/device '$volid' is not possible\n";
245 die "unable to parse volume ID '$volid'\n";
249 sub volume_snapshot_rollback
{
250 my ($cfg, $volid, $snap) = @_;
252 my ($storeid, $volname) = parse_volume_id
($volid, 1);
254 my $scfg = storage_config
($cfg, $storeid);
255 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
256 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
257 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
258 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
259 die "snapshot rollback file/device '$volid' is not possible\n";
261 die "unable to parse volume ID '$volid'\n";
265 sub volume_snapshot_delete
{
266 my ($cfg, $volid, $snap, $running) = @_;
268 my ($storeid, $volname) = parse_volume_id
($volid, 1);
270 my $scfg = storage_config
($cfg, $storeid);
271 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
272 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
273 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
274 die "snapshot delete file/device '$volid' is not possible\n";
276 die "unable to parse volume ID '$volid'\n";
280 sub volume_has_feature
{
281 my ($cfg, $feature, $volid, $snap, $running) = @_;
283 my ($storeid, $volname) = parse_volume_id
($volid, 1);
285 my $scfg = storage_config
($cfg, $storeid);
286 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
287 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
288 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
295 sub volume_snapshot_list
{
296 my ($cfg, $volid) = @_;
298 my ($storeid, $volname) = parse_volume_id
($volid, 1);
300 my $scfg = storage_config
($cfg, $storeid);
301 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
302 return $plugin->volume_snapshot_list($scfg, $storeid, $volname);
303 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
304 die "send file/device '$volid' is not possible\n";
306 die "unable to parse volume ID '$volid'\n";
308 # return an empty array if dataset does not exist.
312 my ($cfg, $storeid, $vmid) = @_;
314 my $scfg = storage_config
($cfg, $storeid);
315 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
317 my $path = $plugin->get_subdir($scfg, 'images');
319 return $vmid ?
"$path/$vmid" : $path;
322 sub get_private_dir
{
323 my ($cfg, $storeid, $vmid) = @_;
325 my $scfg = storage_config
($cfg, $storeid);
326 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
328 my $path = $plugin->get_subdir($scfg, 'rootdir');
330 return $vmid ?
"$path/$vmid" : $path;
334 my ($cfg, $storeid) = @_;
336 my $scfg = storage_config
($cfg, $storeid);
337 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
339 return $plugin->get_subdir($scfg, 'iso');
343 my ($cfg, $storeid) = @_;
345 my $scfg = storage_config
($cfg, $storeid);
346 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
348 return $plugin->get_subdir($scfg, 'vztmpl');
352 my ($cfg, $storeid) = @_;
354 my $scfg = storage_config
($cfg, $storeid);
355 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
357 return $plugin->get_subdir($scfg, 'backup');
360 # library implementation
365 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
370 # NOTE: basename and basevmid are always undef for LVM-thin, where the
371 # clone -> base reference is not encoded in the volume ID.
372 # see note in PVE::Storage::LvmThinPlugin for details.
374 my ($cfg, $volid) = @_;
376 my ($storeid, $volname) = parse_volume_id
($volid);
378 my $scfg = storage_config
($cfg, $storeid);
380 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
382 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
384 return $plugin->parse_volname($volname);
387 sub parse_volume_id
{
388 my ($volid, $noerr) = @_;
390 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
393 # test if we have read access to volid
394 sub check_volume_access
{
395 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
397 my ($sid, $volname) = parse_volume_id
($volid, 1);
399 my ($vtype, undef, $ownervm) = parse_volname
($cfg, $volid);
400 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
401 # we simply allow access
402 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
403 # we are owner - allow access
404 } elsif ($vtype eq 'backup' && $ownervm) {
405 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
406 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
408 # allow if we are Datastore administrator
409 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
412 die "Only root can pass arbitrary filesystem paths."
413 if $user ne 'root@pam';
419 my $volume_is_base_and_used__no_lock = sub {
420 my ($scfg, $storeid, $plugin, $volname) = @_;
422 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
423 $plugin->parse_volname($volname);
426 my $vollist = $plugin->list_images($storeid, $scfg);
427 foreach my $info (@$vollist) {
428 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
429 my $basename = undef;
430 my $basevmid = undef;
433 (undef, undef, undef, $basename, $basevmid) =
434 $plugin->parse_volname($tmpvolname);
437 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
445 # NOTE: this check does not work for LVM-thin, where the clone -> base
446 # reference is not encoded in the volume ID.
447 # see note in PVE::Storage::LvmThinPlugin for details.
448 sub volume_is_base_and_used
{
449 my ($cfg, $volid) = @_;
451 my ($storeid, $volname) = parse_volume_id
($volid);
452 my $scfg = storage_config
($cfg, $storeid);
453 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
455 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
456 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
460 # try to map a filesystem path to a volume identifier
461 sub path_to_volume_id
{
462 my ($cfg, $path) = @_;
464 my $ids = $cfg->{ids
};
466 my ($sid, $volname) = parse_volume_id
($path, 1);
468 if (my $scfg = $ids->{$sid}) {
470 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
471 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
472 return ($vtype, $path);
478 # Note: abs_path() return undef if $path doesn not exist
479 # for example when nfs storage is not mounted
480 $path = abs_path
($path) || $path;
482 foreach my $sid (keys %$ids) {
483 my $scfg = $ids->{$sid};
484 next if !$scfg->{path
};
485 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
486 my $imagedir = $plugin->get_subdir($scfg, 'images');
487 my $isodir = $plugin->get_subdir($scfg, 'iso');
488 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
489 my $backupdir = $plugin->get_subdir($scfg, 'backup');
490 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
492 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
496 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
497 foreach my $info (@$vollist) {
498 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
499 my $volpath = $plugin->path($scfg, $volname, $storeid);
500 if ($volpath eq $path) {
501 return ('images', $info->{volid
});
504 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
506 return ('iso', "$sid:iso/$name");
507 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
509 return ('vztmpl', "$sid:vztmpl/$name");
510 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
512 return ('rootdir', "$sid:rootdir/$vmid");
513 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
515 return ('iso', "$sid:backup/$name");
519 # can't map path to volume id
524 my ($cfg, $volid, $snapname) = @_;
526 my ($storeid, $volname) = parse_volume_id
($volid);
528 my $scfg = storage_config
($cfg, $storeid);
530 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
531 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
532 return wantarray ?
($path, $owner, $vtype) : $path;
535 sub abs_filesystem_path
{
536 my ($cfg, $volid) = @_;
539 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
540 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
541 $path = PVE
::Storage
::path
($cfg, $volid);
544 my $abspath = abs_path
($volid);
545 if ($abspath && $abspath =~ m
|^(/.+)$|) {
546 $path = $1; # untaint any path
551 die "can't find file '$volid'\n" if !($path && -f
$path);
556 sub storage_migrate
{
557 my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots, $logfunc) = @_;
559 my ($storeid, $volname) = parse_volume_id
($volid);
560 $target_volname = $volname if !$target_volname;
562 my $scfg = storage_config
($cfg, $storeid);
564 # no need to migrate shared content
565 return if $storeid eq $target_storeid && $scfg->{shared
};
567 my $tcfg = storage_config
($cfg, $target_storeid);
569 my $target_volid = "${target_storeid}:${target_volname}";
571 my $target_ip = $target_sshinfo->{ip
};
572 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_sshinfo->{name}'";
574 my $ssh = PVE
::Cluster
::ssh_info_to_command
($target_sshinfo);
575 my $ssh_base = PVE
::Cluster
::ssh_info_to_command_base
($target_sshinfo);
576 local $ENV{RSYNC_RSH
} = PVE
::Tools
::cmd2string
($ssh_base);
578 my @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ])
579 if defined($ratelimit_bps);
581 my $migration_snapshot;
582 if (!defined($snapshot)) {
583 if ($scfg->{type
} eq 'zfspool') {
584 $migration_snapshot = 1;
585 $snapshot = '__migration__';
589 my @formats = volume_transfer_formats
($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots);
590 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
591 my $format = $formats[0];
595 @insecurecmd = ('pvecm', 'mtunnel', '-run-command', 1);
596 if (my $network = $target_sshinfo->{network
}) {
597 push @insecurecmd, '-migration_network', $network;
601 $with_snapshots = $with_snapshots ?
1 : 0; # sanitize for passing as cli parameter
602 my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
603 my $recv = [@$ssh, @insecurecmd, '--', 'pvesm', 'import', $volid, $format, '-', '-with-snapshots', $with_snapshots];
604 if (defined($snapshot)) {
605 push @$send, '-snapshot', $snapshot
607 if ($migration_snapshot) {
608 push @$recv, '-delete-snapshot', $snapshot;
611 if (defined($base_snapshot)) {
612 # Check if the snapshot exists on the remote side:
613 push @$send, '-base', $base_snapshot;
614 push @$recv, '-base', $base_snapshot;
617 volume_snapshot
($cfg, $volid, $snapshot) if $migration_snapshot;
620 open(my $info, '-|', @$recv)
621 or die "receive command failed: $!\n";
622 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
623 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
624 my $socket = IO
::Socket
::IP-
>new(PeerHost
=> $ip, PeerPort
=> $port, Type
=> SOCK_STREAM
)
625 or die "failed to connect to tunnel at $ip:$port\n";
626 # we won't be reading from the socket
627 shutdown($socket, 0);
628 run_command
([$send, @cstream], output
=> '>&'.fileno($socket));
629 # don't close the connection entirely otherwise the receiving end
630 # might not get all buffered data (and fails with 'connection reset by peer')
631 shutdown($socket, 1);
632 1 while <$info>; # wait for the remote process to finish
633 # now close the socket
635 if (!close($info)) { # does waitpid()
636 die "import failed: $!\n" if $!;
637 die "import failed: exit code ".($?>>8)."\n";
640 run_command
([$send, @cstream, $recv], logfunc
=> $logfunc);
644 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
645 if ($migration_snapshot) {
646 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
647 warn "could not remove source snapshot: $@\n" if $@;
653 my ($cfg, $volid, $vmid, $snap) = @_;
655 my ($storeid, $volname) = parse_volume_id
($volid);
657 my $scfg = storage_config
($cfg, $storeid);
659 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
661 activate_storage
($cfg, $storeid);
663 # lock shared storage
664 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
665 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
666 return "$storeid:$volname";
670 sub vdisk_create_base
{
671 my ($cfg, $volid) = @_;
673 my ($storeid, $volname) = parse_volume_id
($volid);
675 my $scfg = storage_config
($cfg, $storeid);
677 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
679 activate_storage
($cfg, $storeid);
681 # lock shared storage
682 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
683 my $volname = $plugin->create_base($storeid, $scfg, $volname);
684 return "$storeid:$volname";
689 my ($cfg, $volid, $snapname) = @_;
691 my ($storeid, $volname) = parse_volume_id
($volid);
693 my $scfg = storage_config
($cfg, $storeid);
695 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
697 return $plugin->map_volume($storeid, $scfg, $volname, $snapname);
701 my ($cfg, $volid, $snapname) = @_;
703 my ($storeid, $volname) = parse_volume_id
($volid);
705 my $scfg = storage_config
($cfg, $storeid);
707 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
709 return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
713 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
715 die "no storage ID specified\n" if !$storeid;
717 PVE
::JSONSchema
::parse_storage_id
($storeid);
719 my $scfg = storage_config
($cfg, $storeid);
721 die "no VMID specified\n" if !$vmid;
723 $vmid = parse_vmid
($vmid);
725 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
727 $fmt = $defformat if !$fmt;
729 activate_storage
($cfg, $storeid);
731 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
733 # lock shared storage
734 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
735 my $old_umask = umask(umask|0037);
736 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
740 return "$storeid:$volname";
745 my ($cfg, $volid) = @_;
747 my ($storeid, $volname) = parse_volume_id
($volid);
748 my $scfg = storage_config
($cfg, $storeid);
749 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
751 activate_storage
($cfg, $storeid);
755 # lock shared storage
756 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
757 # LVM-thin allows deletion of still referenced base volumes!
758 die "base volume '$volname' is still in use by linked clones\n"
759 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
761 my (undef, undef, undef, undef, undef, $isBase, $format) =
762 $plugin->parse_volname($volname);
763 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
766 return if !$cleanup_worker;
768 my $rpcenv = PVE
::RPCEnvironment
::get
();
769 my $authuser = $rpcenv->get_user();
771 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
774 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
776 my ($cfg, $storeid, $tt) = @_;
778 die "unknown template type '$tt'\n"
779 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
781 my $ids = $cfg->{ids
};
783 storage_check_enabled
($cfg, $storeid) if ($storeid);
789 foreach my $sid (keys %$ids) {
790 next if $storeid && $storeid ne $sid;
792 my $scfg = $ids->{$sid};
793 my $type = $scfg->{type
};
795 next if !storage_check_enabled
($cfg, $sid, undef, 1);
797 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
798 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
799 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
801 activate_storage
($cfg, $sid);
804 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
806 my $path = $plugin->get_subdir($scfg, $tt);
808 foreach my $fn (<$path/*>) {
813 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
815 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
817 } elsif ($tt eq 'vztmpl') {
818 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
820 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
822 } elsif ($tt eq 'backup') {
823 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
825 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
828 $info->{size
} = -s
$fn;
830 push @{$res->{$sid}}, $info;
835 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
843 my ($cfg, $storeid, $vmid, $vollist) = @_;
845 my $ids = $cfg->{ids
};
847 storage_check_enabled
($cfg, $storeid) if ($storeid);
851 # prepare/activate/refresh all storages
853 my $storage_list = [];
855 foreach my $volid (@$vollist) {
856 my ($sid, undef) = parse_volume_id
($volid);
857 next if !defined($ids->{$sid});
858 next if !storage_check_enabled
($cfg, $sid, undef, 1);
859 push @$storage_list, $sid;
862 foreach my $sid (keys %$ids) {
863 next if $storeid && $storeid ne $sid;
864 next if !storage_check_enabled
($cfg, $sid, undef, 1);
865 push @$storage_list, $sid;
871 activate_storage_list
($cfg, $storage_list, $cache);
873 foreach my $sid (keys %$ids) {
874 next if $storeid && $storeid ne $sid;
875 next if !storage_check_enabled
($cfg, $sid, undef, 1);
877 my $scfg = $ids->{$sid};
878 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
879 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
880 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
887 my ($cfg, $storeid, $vmid, $content) = @_;
889 my @ctypes = qw(images vztmpl iso backup);
891 my $cts = $content ?
[ $content ] : [ @ctypes ];
893 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
896 foreach my $ct (@$cts) {
898 if ($ct eq 'images') {
899 $data = vdisk_list
($cfg, $storeid, $vmid);
900 } elsif ($ct eq 'iso' && !defined($vmid)) {
901 $data = template_list
($cfg, $storeid, 'iso');
902 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
903 $data = template_list
($cfg, $storeid, 'vztmpl');
904 } elsif ($ct eq 'backup') {
905 $data = template_list
($cfg, $storeid, 'backup');
906 foreach my $item (@{$data->{$storeid}}) {
907 if (defined($vmid)) {
908 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
913 next if !$data || !$data->{$storeid};
915 foreach my $item (@{$data->{$storeid}}) {
916 $item->{content
} = $ct;
926 my $filename = "/sys/kernel/uevent_seqnum";
929 if (my $fh = IO
::File-
>new($filename, "r")) {
931 if ($line =~ m/^(\d+)$/) {
939 sub activate_storage
{
940 my ($cfg, $storeid, $cache) = @_;
942 $cache = {} if !$cache;
944 my $scfg = storage_check_enabled
($cfg, $storeid);
946 return if $cache->{activated
}->{$storeid};
948 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
950 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
953 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
954 activate_storage
($cfg, $baseid, $cache);
957 if (!$plugin->check_connection($storeid, $scfg)) {
958 die "storage '$storeid' is not online\n";
961 $plugin->activate_storage($storeid, $scfg, $cache);
963 my $newseq = uevent_seqnum
();
965 # only call udevsettle if there are events
966 if ($newseq > $cache->{uevent_seqnum
}) {
968 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
969 $cache->{uevent_seqnum
} = $newseq;
972 $cache->{activated
}->{$storeid} = 1;
975 sub activate_storage_list
{
976 my ($cfg, $storeid_list, $cache) = @_;
978 $cache = {} if !$cache;
980 foreach my $storeid (@$storeid_list) {
981 activate_storage
($cfg, $storeid, $cache);
985 sub deactivate_storage
{
986 my ($cfg, $storeid) = @_;
988 my $scfg = storage_config
($cfg, $storeid);
989 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
992 $plugin->deactivate_storage($storeid, $scfg, $cache);
995 sub activate_volumes
{
996 my ($cfg, $vollist, $snapname) = @_;
998 return if !($vollist && scalar(@$vollist));
1000 my $storagehash = {};
1001 foreach my $volid (@$vollist) {
1002 my ($storeid, undef) = parse_volume_id
($volid);
1003 $storagehash->{$storeid} = 1;
1008 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1010 foreach my $volid (@$vollist) {
1011 my ($storeid, $volname) = parse_volume_id
($volid);
1012 my $scfg = storage_config
($cfg, $storeid);
1013 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1014 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1018 sub deactivate_volumes
{
1019 my ($cfg, $vollist, $snapname) = @_;
1021 return if !($vollist && scalar(@$vollist));
1026 foreach my $volid (@$vollist) {
1027 my ($storeid, $volname) = parse_volume_id
($volid);
1029 my $scfg = storage_config
($cfg, $storeid);
1030 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1033 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1037 push @errlist, $volid;
1041 die "volume deactivation failed: " . join(' ', @errlist)
1042 if scalar(@errlist);
1046 my ($cfg, $content, $includeformat) = @_;
1048 my $ids = $cfg->{ids
};
1052 my @ctypes = PVE
::Tools
::split_list
($content);
1055 foreach my $storeid (keys %$ids) {
1056 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1058 if (defined($content)) {
1060 foreach my $ctype (@ctypes) {
1061 if ($ids->{$storeid}->{content
}->{$ctype}) {
1066 next if !$want_ctype || !$storage_enabled;
1069 my $type = $ids->{$storeid}->{type
};
1071 $info->{$storeid} = {
1076 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1077 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1079 enabled
=> $storage_enabled ?
1 : 0,
1082 push @$slist, $storeid;
1087 foreach my $storeid (keys %$ids) {
1088 my $scfg = $ids->{$storeid};
1090 next if !$info->{$storeid};
1091 next if !$info->{$storeid}->{enabled
};
1093 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1094 if ($includeformat) {
1095 my $pd = $plugin->plugindata();
1096 $info->{$storeid}->{format
} = $pd->{format
}
1098 $info->{$storeid}->{select_existing
} = $pd->{select_existing
}
1099 if $pd->{select_existing
};
1102 eval { activate_storage
($cfg, $storeid, $cache); };
1108 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
1111 $info->{$storeid}->{total
} = int($total);
1112 $info->{$storeid}->{avail
} = int($avail);
1113 $info->{$storeid}->{used
} = int($used);
1114 $info->{$storeid}->{active
} = $active;
1123 my ($packed_ip, $family);
1125 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1126 $family = $res[0]->{family
};
1127 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1129 if (defined $packed_ip) {
1130 return Socket
::inet_ntop
($family, $packed_ip);
1136 my ($server_in) = @_;
1139 if (!($server = resolv_server
($server_in))) {
1140 die "unable to resolve address for server '${server_in}'\n";
1143 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1146 run_command
($cmd, outfunc
=> sub {
1149 # note: howto handle white spaces in export path??
1150 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1159 my ($server_in, $user, $password, $domain) = @_;
1162 if (!($server = resolv_server
($server_in))) {
1163 die "unable to resolve address for server '${server_in}'\n";
1166 # we support only Windows grater than 2012 cifsscan so use smb3
1167 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1168 if (defined($user)) {
1169 die "password is required" if !defined($password);
1170 push @$cmd, '-U', "$user\%$password";
1171 push @$cmd, '-W', $domain if defined($domain);
1180 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1182 } elsif ($line =~ m/(NT_STATUS_(\S*))/) {
1195 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1198 run_command
($cmd, outfunc
=> sub {
1201 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1202 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1203 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1204 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1205 # ignore subvolumes generated by our ZFSPoolPlugin
1206 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1207 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1208 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1216 my ($portal, $noerr) = @_;
1218 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1220 if (my $ip = resolv_server
($server)) {
1222 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1223 return $port ?
"$server:$port" : $server;
1226 return undef if $noerr;
1228 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1231 # idea is from usbutils package (/usr/bin/usb-devices) script
1232 sub __scan_usb_device
{
1233 my ($res, $devpath, $parent, $level) = @_;
1235 return if ! -d
$devpath;
1236 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1237 my $port = $level ?
int($1 - 1) : 0;
1239 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1240 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1247 speed
=> file_read_firstline
("$devpath/speed"),
1248 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1249 vendid
=> file_read_firstline
("$devpath/idVendor"),
1250 prodid
=> file_read_firstline
("$devpath/idProduct"),
1254 my $usbpath = $devpath;
1255 $usbpath =~ s
|^.*/\d
+\
-||;
1256 $d->{usbpath
} = $usbpath;
1259 my $product = file_read_firstline
("$devpath/product");
1260 $d->{product
} = $product if $product;
1262 my $manu = file_read_firstline
("$devpath/manufacturer");
1263 $d->{manufacturer
} = $manu if $manu;
1265 my $serial => file_read_firstline
("$devpath/serial");
1266 $d->{serial
} = $serial if $serial;
1270 foreach my $subdev (<$devpath/$busnum-*>) {
1271 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1272 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1281 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1282 __scan_usb_device
($devlist, $device, 0, 0);
1289 my ($portal_in) = @_;
1292 if (!($portal = resolv_portal
($portal_in))) {
1293 die "unable to parse/resolve portal address '${portal_in}'\n";
1296 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1299 sub storage_default_format
{
1300 my ($cfg, $storeid) = @_;
1302 my $scfg = storage_config
($cfg, $storeid);
1304 return PVE
::Storage
::Plugin
::default_format
($scfg);
1307 sub vgroup_is_used
{
1308 my ($cfg, $vgname) = @_;
1310 foreach my $storeid (keys %{$cfg->{ids
}}) {
1311 my $scfg = storage_config
($cfg, $storeid);
1312 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1320 sub target_is_used
{
1321 my ($cfg, $target) = @_;
1323 foreach my $storeid (keys %{$cfg->{ids
}}) {
1324 my $scfg = storage_config
($cfg, $storeid);
1325 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1333 sub volume_is_used
{
1334 my ($cfg, $volid) = @_;
1336 foreach my $storeid (keys %{$cfg->{ids
}}) {
1337 my $scfg = storage_config
($cfg, $storeid);
1338 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1346 sub storage_is_used
{
1347 my ($cfg, $storeid) = @_;
1349 foreach my $sid (keys %{$cfg->{ids
}}) {
1350 my $scfg = storage_config
($cfg, $sid);
1351 next if !$scfg->{base
};
1352 my ($st) = parse_volume_id
($scfg->{base
});
1353 return 1 if $st && $st eq $storeid;
1360 my ($list, $func) = @_;
1364 foreach my $sid (keys %$list) {
1365 foreach my $info (@{$list->{$sid}}) {
1366 my $volid = $info->{volid
};
1367 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1368 if ($sid1 && $sid1 eq $sid) {
1369 &$func ($volid, $sid, $info);
1371 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1377 sub extract_vzdump_config_tar
{
1378 my ($archive, $conf_re) = @_;
1380 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1382 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1383 die "unable to open file '$archive'\n";
1386 while (defined($file = <$fh>)) {
1387 if ($file =~ $conf_re) {
1388 $file = $1; # untaint
1397 die "ERROR: archive contains no configuration file\n" if !$file;
1403 $raw .= "$output\n";
1406 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1408 return wantarray ?
($raw, $file) : $raw;
1411 sub extract_vzdump_config_vma
{
1412 my ($archive, $comp) = @_;
1418 $raw .= "$output\n";
1424 if ($comp eq 'gz') {
1425 $uncomp = ["zcat", $archive];
1426 } elsif ($comp eq 'lzo') {
1427 $uncomp = ["lzop", "-d", "-c", $archive];
1429 die "unknown compression method '$comp'\n";
1431 $cmd = [$uncomp, ["vma", "config", "-"]];
1433 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1434 # closed early by vma, detect this and ignore the exit code later
1439 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1441 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1442 $errstring = "Failed to extract config from VMA archive: $output\n";
1446 # in other cases, the pipeline will exit with exit code 141
1447 # because of the broken pipe, handle / ignore this as well
1450 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1454 # use exit code if no stderr output and not just broken pipe
1455 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1456 die "$rerr\n" if $rerr;
1457 die "config extraction failed with exit code $rc\n";
1459 die "$errstring\n" if $errstring;
1461 # simple case without compression and weird piping behaviour
1462 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1465 return wantarray ?
($raw, undef) : $raw;
1468 sub extract_vzdump_config
{
1469 my ($cfg, $volid) = @_;
1471 my $archive = abs_filesystem_path
($cfg, $volid);
1473 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1474 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1475 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1483 $comp = $11 if defined($11);
1486 if ($format eq 'tar') {
1487 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1489 return extract_vzdump_config_vma
($archive, $comp);
1492 die "cannot determine backup guest type for backup archive '$volid'\n";
1497 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1499 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1500 die "cannot export volume '$volid'\n" if !$storeid;
1501 my $scfg = storage_config
($cfg, $storeid);
1502 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1503 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1504 $snapshot, $base_snapshot, $with_snapshots);
1508 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
1510 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1511 die "cannot import into volume '$volid'\n" if !$storeid;
1512 my $scfg = storage_config
($cfg, $storeid);
1513 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1514 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1515 $base_snapshot, $with_snapshots);
1518 sub volume_export_formats
{
1519 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1521 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1522 return if !$storeid;
1523 my $scfg = storage_config
($cfg, $storeid);
1524 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1525 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1526 $snapshot, $base_snapshot,
1530 sub volume_import_formats
{
1531 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1533 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1534 return if !$storeid;
1535 my $scfg = storage_config
($cfg, $storeid);
1536 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1537 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1538 $base_snapshot, $with_snapshots);
1541 sub volume_transfer_formats
{
1542 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1543 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1544 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1545 my %import_hash = map { $_ => 1 } @import_formats;
1546 my @common = grep { $import_hash{$_} } @export_formats;
1550 # bash completion helper
1552 sub complete_storage
{
1553 my ($cmdname, $pname, $cvalue) = @_;
1555 my $cfg = PVE
::Storage
::config
();
1557 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1560 sub complete_storage_enabled
{
1561 my ($cmdname, $pname, $cvalue) = @_;
1565 my $cfg = PVE
::Storage
::config
();
1566 foreach my $sid (keys %{$cfg->{ids
}}) {
1567 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1573 sub complete_content_type
{
1574 my ($cmdname, $pname, $cvalue) = @_;
1576 return [qw(rootdir images vztmpl iso backup)];
1579 sub complete_volume
{
1580 my ($cmdname, $pname, $cvalue) = @_;
1584 my $storage_list = complete_storage_enabled
();
1586 if ($cvalue =~ m/^([^:]+):/) {
1587 $storage_list = [ $1 ];
1589 if (scalar(@$storage_list) > 1) {
1590 # only list storage IDs to avoid large listings
1592 foreach my $storeid (@$storage_list) {
1593 # Hack: simply return 2 artificial values, so that
1594 # completions does not finish
1595 push @$res, "$storeid:volname", "$storeid:...";
1602 foreach my $storeid (@$storage_list) {
1603 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1605 foreach my $item (@$vollist) {
1606 push @$res, $item->{volid
};
1613 # Various io-heavy operations require io/bandwidth limits which can be
1614 # configured on multiple levels: The global defaults in datacenter.cfg, and
1615 # per-storage overrides. When we want to do a restore from storage A to storage
1616 # B, we should take the smaller limit defined for storages A and B, and if no
1617 # such limit was specified, use the one from datacenter.cfg.
1618 sub get_bandwidth_limit
{
1619 my ($operation, $storage_list, $override) = @_;
1621 # called for each limit (global, per-storage) with the 'default' and the
1622 # $operation limit and should udpate $override for every limit affecting
1624 my $use_global_limits = 0;
1625 my $apply_limit = sub {
1627 if (defined($bwlimit)) {
1628 my $limits = PVE
::JSONSchema
::parse_property_string
('bwlimit', $bwlimit);
1629 my $limit = $limits->{$operation} // $limits->{default};
1630 if (defined($limit)) {
1631 if (!$override || $limit < $override) {
1637 # If there was no applicable limit, try to apply the global ones.
1638 $use_global_limits = 1;
1641 my ($rpcenv, $authuser);
1642 if (defined($override)) {
1643 $rpcenv = PVE
::RPCEnvironment-
>get();
1644 $authuser = $rpcenv->get_user();
1647 # Apply per-storage limits - if there are storages involved.
1648 if (@$storage_list) {
1649 my $config = config
();
1651 # The Datastore.Allocate permission allows us to modify the per-storage
1652 # limits, therefore it also allows us to override them.
1653 # Since we have most likely multiple storages to check, do a quick check on
1654 # the general '/storage' path to see if we can skip the checks entirely:
1655 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
1658 foreach my $storage (@$storage_list) {
1659 # Avoid duplicate checks:
1660 next if $done{$storage};
1661 $done{$storage} = 1;
1663 # Otherwise we may still have individual /storage/$ID permissions:
1664 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
1665 # And if not: apply the limits.
1666 my $storecfg = storage_config
($config, $storage);
1667 $apply_limit->($storecfg->{bwlimit
});
1671 # Storage limits take precedence over the datacenter defaults, so if
1672 # a limit was applied:
1673 return $override if !$use_global_limits;
1676 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1677 # global default limits.
1678 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
1679 # So if we cannot modify global limits, apply them to our currently
1680 # requested override.
1681 my $dc = cfs_read_file
('datacenter.cfg');
1682 $apply_limit->($dc->{bwlimit
});
1688 # checks if the storage id is available and dies if not
1689 sub assert_sid_unused
{
1693 if (my $scfg = storage_config
($cfg, $sid, 1)) {
1694 die "storage ID '$sid' already defined\n";