]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage.pm
doc: Clarify that pvesm export is mostly internal
[pve-storage.git] / PVE / Storage.pm
CommitLineData
b6cf0a66
DM
1package PVE::Storage;
2
3use strict;
ffd6f2f3 4use warnings;
dec97937 5use Data::Dumper;
ffd6f2f3 6
b6cf0a66
DM
7use POSIX;
8use IO::Select;
b6cf0a66 9use IO::File;
7ba34faa 10use IO::Socket::IP;
b6cf0a66
DM
11use File::Basename;
12use File::Path;
b6cf0a66 13use Cwd 'abs_path';
7a2d5c1a 14use Socket;
fb821c18 15use Time::Local qw(timelocal);
b6cf0a66 16
4dee23d3 17use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
83d7192f 18use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file);
2f08fb4b 19use PVE::DataCenterConfig;
b6cf0a66
DM
20use PVE::Exception qw(raise_param_exc);
21use PVE::JSONSchema;
22use PVE::INotify;
88c3abaf 23use PVE::RPCEnvironment;
65bb9859 24use PVE::SSHInfo;
b6cf0a66 25
1dc01b9f
DM
26use PVE::Storage::Plugin;
27use PVE::Storage::DirPlugin;
28use PVE::Storage::LVMPlugin;
610798bc 29use PVE::Storage::LvmThinPlugin;
1dc01b9f 30use PVE::Storage::NFSPlugin;
d7875239 31use PVE::Storage::CIFSPlugin;
1dc01b9f 32use PVE::Storage::ISCSIPlugin;
0509010d 33use PVE::Storage::RBDPlugin;
e34ce144 34use PVE::Storage::CephFSPlugin;
86616554 35use PVE::Storage::ISCSIDirectPlugin;
f4648aef 36use PVE::Storage::GlusterfsPlugin;
85fda4dd 37use PVE::Storage::ZFSPoolPlugin;
4f914e6e 38use PVE::Storage::ZFSPlugin;
14770890 39use PVE::Storage::DRBDPlugin;
271fe394 40use PVE::Storage::PBSPlugin;
b6cf0a66 41
4dee23d3 42# Storage API version. Icrement it on changes in storage API interface.
a97d3ee4 43use constant APIVER => 5;
042dd4be
WB
44# Age is the number of versions we're backward compatible with.
45# This is like having 'current=APIVER' and age='APIAGE' in libtool,
46# see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
a97d3ee4 47use constant APIAGE => 4;
4dee23d3
DP
48
49# load standard plugins
1dc01b9f
DM
50PVE::Storage::DirPlugin->register();
51PVE::Storage::LVMPlugin->register();
610798bc 52PVE::Storage::LvmThinPlugin->register();
1dc01b9f 53PVE::Storage::NFSPlugin->register();
d7875239 54PVE::Storage::CIFSPlugin->register();
1dc01b9f 55PVE::Storage::ISCSIPlugin->register();
0509010d 56PVE::Storage::RBDPlugin->register();
e34ce144 57PVE::Storage::CephFSPlugin->register();
86616554 58PVE::Storage::ISCSIDirectPlugin->register();
f4648aef 59PVE::Storage::GlusterfsPlugin->register();
85fda4dd 60PVE::Storage::ZFSPoolPlugin->register();
4f914e6e 61PVE::Storage::ZFSPlugin->register();
14770890 62PVE::Storage::DRBDPlugin->register();
271fe394 63PVE::Storage::PBSPlugin->register();
4dee23d3
DP
64
65# load third-party plugins
66if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) {
67 dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
68 my ($file) = @_;
69 my $modname = 'PVE::Storage::Custom::' . $file;
70 $modname =~ s!\.pm$!!;
71 $file = 'PVE/Storage/Custom/' . $file;
72
73 eval {
74 require $file;
042dd4be
WB
75
76 # Check perl interface:
77 die "not derived from PVE::Storage::Plugin\n"
78 if !$modname->isa('PVE::Storage::Plugin');
79 die "does not provide an api() method\n"
80 if !$modname->can('api');
81 # Check storage API version and that file is really storage plugin.
82 my $version = $modname->api();
a0908caa 83 die "implements an API version newer than current ($version > " . APIVER . ")\n"
042dd4be 84 if $version > APIVER;
a0908caa
TL
85 my $min_version = (APIVER - APIAGE);
86 die "API version too old, please update the plugin ($version < $min_version)\n"
87 if $version < $min_version;
042dd4be
WB
88 import $file;
89 $modname->register();
90
91 # If we got this far and the API version is not the same, make some
92 # noise:
93 warn "Plugin \"$modname\" is implementing an older storage API, an upgrade is recommended\n"
94 if $version != APIVER;
4dee23d3
DP
95 };
96 if ($@) {
042dd4be 97 warn "Error loading storage plugin \"$modname\": $@";
4dee23d3
DP
98 }
99 });
100}
101
102# initialize all plugins
1dc01b9f 103PVE::Storage::Plugin->init();
b6cf0a66 104
1dc01b9f 105my $UDEVADM = '/sbin/udevadm';
b6cf0a66 106
b295e05e 107our $iso_extension_re = qr/\.(?:iso|img)/i;
4c693491 108
1dc01b9f 109# PVE::Storage utility functions
b6cf0a66
DM
110
111sub config {
112 return cfs_read_file("storage.cfg");
113}
114
83d7192f
FG
115sub write_config {
116 my ($cfg) = @_;
117
118 cfs_write_file('storage.cfg', $cfg);
119}
120
b6cf0a66
DM
121sub lock_storage_config {
122 my ($code, $errmsg) = @_;
123
124 cfs_lock_file("storage.cfg", undef, $code);
125 my $err = $@;
126 if ($err) {
127 $errmsg ? die "$errmsg: $err" : die $err;
128 }
129}
130
b6cf0a66
DM
131sub storage_config {
132 my ($cfg, $storeid, $noerr) = @_;
133
82fc923f 134 die "no storage ID specified\n" if !$storeid;
1a3459ac 135
b6cf0a66
DM
136 my $scfg = $cfg->{ids}->{$storeid};
137
481f6177 138 die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg);
b6cf0a66
DM
139
140 return $scfg;
141}
142
143sub storage_check_node {
144 my ($cfg, $storeid, $node, $noerr) = @_;
145
1dc01b9f 146 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
147
148 if ($scfg->{nodes}) {
149 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
150 if (!$scfg->{nodes}->{$node}) {
da156fb3 151 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
b6cf0a66
DM
152 return undef;
153 }
154 }
155
156 return $scfg;
157}
158
159sub storage_check_enabled {
160 my ($cfg, $storeid, $node, $noerr) = @_;
161
1dc01b9f 162 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
163
164 if ($scfg->{disable}) {
165 die "storage '$storeid' is disabled\n" if !$noerr;
166 return undef;
167 }
168
169 return storage_check_node($cfg, $storeid, $node, $noerr);
170}
171
7118dd91
DM
172# storage_can_replicate:
173# return true if storage supports replication
174# (volumes alocated with vdisk_alloc() has replication feature)
175sub storage_can_replicate {
176 my ($cfg, $storeid, $format) = @_;
177
178 my $scfg = storage_config($cfg, $storeid);
179 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
180 return $plugin->storage_can_replicate($scfg, $storeid, $format);
181}
182
b6cf0a66
DM
183sub storage_ids {
184 my ($cfg) = @_;
185
1dc01b9f 186 return keys %{$cfg->{ids}};
b6cf0a66
DM
187}
188
1dc01b9f
DM
189sub file_size_info {
190 my ($filename, $timeout) = @_;
b6cf0a66 191
1dc01b9f 192 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
b6cf0a66
DM
193}
194
20ccac1b
AD
195sub volume_size_info {
196 my ($cfg, $volid, $timeout) = @_;
197
f18199e5
DM
198 my ($storeid, $volname) = parse_volume_id($volid, 1);
199 if ($storeid) {
200 my $scfg = storage_config($cfg, $storeid);
201 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
202 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
203 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
204 return file_size_info($volid, $timeout);
205 } else {
206 return 0;
207 }
20ccac1b
AD
208}
209
7e6c05dc
AD
210sub volume_resize {
211 my ($cfg, $volid, $size, $running) = @_;
212
c7215573
FE
213 my $padding = (1024 - $size % 1024) % 1024;
214 $size = $size + $padding;
215
7e6c05dc
AD
216 my ($storeid, $volname) = parse_volume_id($volid, 1);
217 if ($storeid) {
218 my $scfg = storage_config($cfg, $storeid);
219 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
220 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
221 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
f824c722 222 die "resize file/device '$volid' is not possible\n";
7e6c05dc 223 } else {
f824c722 224 die "unable to parse volume ID '$volid'\n";
7e6c05dc
AD
225 }
226}
227
1597f1f9
WL
228sub volume_rollback_is_possible {
229 my ($cfg, $volid, $snap) = @_;
e0852ba7 230
1597f1f9
WL
231 my ($storeid, $volname) = parse_volume_id($volid, 1);
232 if ($storeid) {
233 my $scfg = storage_config($cfg, $storeid);
234 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
235 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
236 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
f824c722 237 die "snapshot rollback file/device '$volid' is not possible\n";
1597f1f9 238 } else {
f824c722 239 die "unable to parse volume ID '$volid'\n";
1597f1f9
WL
240 }
241}
242
db60719c 243sub volume_snapshot {
f5640e7d 244 my ($cfg, $volid, $snap) = @_;
db60719c
AD
245
246 my ($storeid, $volname) = parse_volume_id($volid, 1);
247 if ($storeid) {
248 my $scfg = storage_config($cfg, $storeid);
249 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
f5640e7d 250 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
db60719c 251 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
f824c722 252 die "snapshot file/device '$volid' is not possible\n";
db60719c 253 } else {
f824c722 254 die "unable to parse volume ID '$volid'\n";
db60719c
AD
255 }
256}
257
22a2a633
AD
258sub volume_snapshot_rollback {
259 my ($cfg, $volid, $snap) = @_;
260
261 my ($storeid, $volname) = parse_volume_id($volid, 1);
262 if ($storeid) {
263 my $scfg = storage_config($cfg, $storeid);
264 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b3f302c6 265 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
22a2a633
AD
266 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
267 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
f824c722 268 die "snapshot rollback file/device '$volid' is not possible\n";
22a2a633 269 } else {
f824c722 270 die "unable to parse volume ID '$volid'\n";
22a2a633
AD
271 }
272}
273
5753c9d1
AD
274sub volume_snapshot_delete {
275 my ($cfg, $volid, $snap, $running) = @_;
276
277 my ($storeid, $volname) = parse_volume_id($volid, 1);
278 if ($storeid) {
279 my $scfg = storage_config($cfg, $storeid);
280 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
27cc55d4 281 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
5753c9d1 282 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
f824c722 283 die "snapshot delete file/device '$volid' is not possible\n";
5753c9d1 284 } else {
f824c722 285 die "unable to parse volume ID '$volid'\n";
5753c9d1
AD
286 }
287}
288
2668adce
FE
289# check if a volume or snapshot supports a given feature
290# $feature - one of:
291# clone - linked clone is possible
292# copy - full clone is possible
293# replicate - replication is possible
294# snapshot - taking a snapshot is possible
295# sparseinit - volume is sparsely initialized
296# template - conversion to base image is possible
297# $snap - check if the feature is supported for a given snapshot
298# $running - if the guest owning the volume is running
299# $opts - hash with further options:
300# valid_target_formats - list of formats for the target of a copy/clone
301# operation that the caller could work with. The
302# format of $volid is always considered valid and if
303# no list is specified, all formats are considered valid.
99473759 304sub volume_has_feature {
e6f4eed4 305 my ($cfg, $feature, $volid, $snap, $running, $opts) = @_;
99473759
AD
306
307 my ($storeid, $volname) = parse_volume_id($volid, 1);
308 if ($storeid) {
309 my $scfg = storage_config($cfg, $storeid);
310 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
e6f4eed4 311 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts);
99473759
AD
312 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
313 return undef;
314 } else {
315 return undef;
316 }
317}
318
aefe82ea 319sub volume_snapshot_list {
8b622c2d 320 my ($cfg, $volid) = @_;
aefe82ea
WL
321
322 my ($storeid, $volname) = parse_volume_id($volid, 1);
323 if ($storeid) {
324 my $scfg = storage_config($cfg, $storeid);
325 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
8b622c2d 326 return $plugin->volume_snapshot_list($scfg, $storeid, $volname);
aefe82ea
WL
327 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
328 die "send file/device '$volid' is not possible\n";
329 } else {
330 die "unable to parse volume ID '$volid'\n";
331 }
332 # return an empty array if dataset does not exist.
aefe82ea
WL
333}
334
1dc01b9f
DM
335sub get_image_dir {
336 my ($cfg, $storeid, $vmid) = @_;
b6cf0a66 337
1dc01b9f
DM
338 my $scfg = storage_config($cfg, $storeid);
339 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 340
1dc01b9f 341 my $path = $plugin->get_subdir($scfg, 'images');
b6cf0a66 342
1dc01b9f 343 return $vmid ? "$path/$vmid" : $path;
b6cf0a66
DM
344}
345
1dc01b9f 346sub get_private_dir {
b6cf0a66
DM
347 my ($cfg, $storeid, $vmid) = @_;
348
1dc01b9f
DM
349 my $scfg = storage_config($cfg, $storeid);
350 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 351
1dc01b9f 352 my $path = $plugin->get_subdir($scfg, 'rootdir');
d22a6133 353
1dc01b9f 354 return $vmid ? "$path/$vmid" : $path;
d22a6133
DM
355}
356
b6cf0a66
DM
357sub get_iso_dir {
358 my ($cfg, $storeid) = @_;
359
1dc01b9f
DM
360 my $scfg = storage_config($cfg, $storeid);
361 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 362
1dc01b9f 363 return $plugin->get_subdir($scfg, 'iso');
b6cf0a66
DM
364}
365
366sub get_vztmpl_dir {
367 my ($cfg, $storeid) = @_;
368
1dc01b9f
DM
369 my $scfg = storage_config($cfg, $storeid);
370 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 371
1dc01b9f 372 return $plugin->get_subdir($scfg, 'vztmpl');
b6cf0a66
DM
373}
374
568de3d1
DM
375sub get_backup_dir {
376 my ($cfg, $storeid) = @_;
377
1dc01b9f
DM
378 my $scfg = storage_config($cfg, $storeid);
379 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 380
1dc01b9f 381 return $plugin->get_subdir($scfg, 'backup');
b6cf0a66
DM
382}
383
384# library implementation
385
b6cf0a66
DM
386sub parse_vmid {
387 my $vmid = shift;
388
389 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
390
391 return int($vmid);
392}
393
787624df
FG
394# NOTE: basename and basevmid are always undef for LVM-thin, where the
395# clone -> base reference is not encoded in the volume ID.
396# see note in PVE::Storage::LvmThinPlugin for details.
ec4b0dc7
AD
397sub parse_volname {
398 my ($cfg, $volid) = @_;
399
400 my ($storeid, $volname) = parse_volume_id($volid);
401
402 my $scfg = storage_config($cfg, $storeid);
403
404 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
a6f12626
DM
405
406 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
407
ec4b0dc7
AD
408 return $plugin->parse_volname($volname);
409}
410
b6cf0a66
DM
411sub parse_volume_id {
412 my ($volid, $noerr) = @_;
413
a7f3d909 414 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
b6cf0a66
DM
415}
416
04a13668
DM
417# test if we have read access to volid
418sub check_volume_access {
419 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
420
421 my ($sid, $volname) = parse_volume_id($volid, 1);
422 if ($sid) {
423 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
424 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
775fdc69 425 # require at least read access to storage, (custom) templates/ISOs could be sensitive
061b9ca6 426 $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']);
04a13668
DM
427 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
428 # we are owner - allow access
429 } elsif ($vtype eq 'backup' && $ownervm) {
430 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
431 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
432 } else {
433 # allow if we are Datastore administrator
434 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
435 }
436 } else {
437 die "Only root can pass arbitrary filesystem paths."
438 if $user ne 'root@pam';
439 }
440
441 return undef;
442}
443
17fb7e42
FG
444my $volume_is_base_and_used__no_lock = sub {
445 my ($scfg, $storeid, $plugin, $volname) = @_;
446
447 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
448 $plugin->parse_volname($volname);
449
450 if ($isBase) {
451 my $vollist = $plugin->list_images($storeid, $scfg);
452 foreach my $info (@$vollist) {
453 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
454 my $basename = undef;
455 my $basevmid = undef;
456
457 eval{
458 (undef, undef, undef, $basename, $basevmid) =
459 $plugin->parse_volname($tmpvolname);
460 };
461
462 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
463 return 1;
464 }
465 }
466 }
467 return 0;
468};
469
787624df
FG
470# NOTE: this check does not work for LVM-thin, where the clone -> base
471# reference is not encoded in the volume ID.
472# see note in PVE::Storage::LvmThinPlugin for details.
17fb7e42
FG
473sub volume_is_base_and_used {
474 my ($cfg, $volid) = @_;
475
476 my ($storeid, $volname) = parse_volume_id($volid);
477 my $scfg = storage_config($cfg, $storeid);
478 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
479
480 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
481 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
482 });
483}
484
b6cf0a66
DM
485# try to map a filesystem path to a volume identifier
486sub path_to_volume_id {
487 my ($cfg, $path) = @_;
488
489 my $ids = $cfg->{ids};
490
1dc01b9f 491 my ($sid, $volname) = parse_volume_id($path, 1);
b6cf0a66 492 if ($sid) {
1dc01b9f 493 if (my $scfg = $ids->{$sid}) {
188aca38 494 if ($scfg->{path}) {
1dc01b9f
DM
495 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
496 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
b6cf0a66
DM
497 return ($vtype, $path);
498 }
499 }
500 return ('');
501 }
502
1a3459ac 503 # Note: abs_path() return undef if $path doesn not exist
75d75990
DM
504 # for example when nfs storage is not mounted
505 $path = abs_path($path) || $path;
b6cf0a66
DM
506
507 foreach my $sid (keys %$ids) {
1dc01b9f
DM
508 my $scfg = $ids->{$sid};
509 next if !$scfg->{path};
510 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
511 my $imagedir = $plugin->get_subdir($scfg, 'images');
512 my $isodir = $plugin->get_subdir($scfg, 'iso');
513 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
514 my $backupdir = $plugin->get_subdir($scfg, 'backup');
515 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
73fcb7bf 516 my $snippetsdir = $plugin->get_subdir($scfg, 'snippets');
b6cf0a66
DM
517
518 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
519 my $vmid = $1;
520 my $name = $2;
fcbec654
DM
521
522 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
523 foreach my $info (@$vollist) {
524 my ($storeid, $volname) = parse_volume_id($info->{volid});
525 my $volpath = $plugin->path($scfg, $volname, $storeid);
526 if ($volpath eq $path) {
527 return ('images', $info->{volid});
528 }
529 }
4c693491 530 } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) {
b6cf0a66 531 my $name = $1;
1a3459ac 532 return ('iso', "$sid:iso/$name");
b6cf0a66
DM
533 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
534 my $name = $1;
535 return ('vztmpl', "$sid:vztmpl/$name");
1ac17c74
DM
536 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
537 my $vmid = $1;
538 return ('rootdir', "$sid:rootdir/$vmid");
277cafc0 539 } elsif ($path =~ m!^$backupdir/([^/]+\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) {
568de3d1 540 my $name = $1;
892dc992 541 return ('backup', "$sid:backup/$name");
73fcb7bf
AA
542 } elsif ($path =~ m!^$snippetsdir/([^/]+)$!) {
543 my $name = $1;
544 return ('snippets', "$sid:snippets/$name");
b6cf0a66
DM
545 }
546 }
547
548 # can't map path to volume id
549 return ('');
550}
551
552sub path {
207ea852 553 my ($cfg, $volid, $snapname) = @_;
b6cf0a66 554
1dc01b9f 555 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 556
1dc01b9f 557 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 558
1dc01b9f 559 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
207ea852 560 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
2494896a 561 return wantarray ? ($path, $owner, $vtype) : $path;
b6cf0a66
DM
562}
563
35fbb2e6
DM
564sub abs_filesystem_path {
565 my ($cfg, $volid) = @_;
566
567 my $path;
6ed43d81
TL
568 if (parse_volume_id ($volid, 1)) {
569 activate_volumes($cfg, [ $volid ]);
35fbb2e6
DM
570 $path = PVE::Storage::path($cfg, $volid);
571 } else {
572 if (-f $volid) {
573 my $abspath = abs_path($volid);
574 if ($abspath && $abspath =~ m|^(/.+)$|) {
575 $path = $1; # untaint any path
576 }
577 }
578 }
579
580 die "can't find file '$volid'\n" if !($path && -f $path);
581
582 return $path;
583}
584
683a3f46
FE
585my $volname_for_storage = sub {
586 my ($cfg, $volid, $target_storeid) = @_;
587
588 my (undef, $name, $vmid, undef, undef, undef, $format) = parse_volname($cfg, $volid);
589 my $target_scfg = storage_config($cfg, $target_storeid);
590
591 my (undef, $valid_formats) = PVE::Storage::Plugin::default_format($target_scfg);
592 my $format_is_valid = grep { $_ eq $format } @$valid_formats;
593 die "unsupported format '$format' for storage type $target_scfg->{type}\n" if !$format_is_valid;
594
595 (my $name_without_extension = $name) =~ s/\.$format$//;
596
597 if ($target_scfg->{path}) {
598 return "$vmid/$name_without_extension.$format";
599 } else {
600 return "$name_without_extension";
601 }
602};
603
b6cf0a66 604sub storage_migrate {
dc3655a1
FE
605 my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_;
606
607 my $base_snapshot = $opts->{base_snapshot};
608 my $snapshot = $opts->{snapshot};
609 my $ratelimit_bps = $opts->{ratelimit_bps};
610 my $insecure = $opts->{insecure};
611 my $with_snapshots = $opts->{with_snapshots} ? 1 : 0;
612 my $allow_rename = $opts->{allow_rename} ? 1 : 0;
b6cf0a66 613
6bf56298 614 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 615
6bf56298 616 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
617
618 # no need to migrate shared content
a97d3ee4 619 return $volid if $storeid eq $target_storeid && $scfg->{shared};
b6cf0a66 620
6bf56298 621 my $tcfg = storage_config($cfg, $target_storeid);
b6cf0a66 622
95015dbb
FE
623 my $vtype = (parse_volname($cfg, $volid))[0];
624
625 die "content type '$vtype' is not available on storage '$target_storeid'\n"
626 if !$tcfg->{content}->{$vtype};
627
683a3f46
FE
628 my $target_volname;
629 if ($opts->{target_volname}) {
630 $target_volname = $opts->{target_volname};
631 } elsif ($scfg->{type} eq $tcfg->{type}) {
632 $target_volname = $volname;
633 } else {
634 $target_volname = $volname_for_storage->($cfg, $volid, $target_storeid);
635 }
636
b6cf0a66
DM
637 my $target_volid = "${target_storeid}:${target_volname}";
638
4b4c580d 639 my $target_ip = $target_sshinfo->{ip};
b6cf0a66 640
65bb9859
FG
641 my $ssh = PVE::SSHInfo::ssh_info_to_command($target_sshinfo);
642 my $ssh_base = PVE::SSHInfo::ssh_info_to_command_base($target_sshinfo);
47cea194 643 local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base);
b6cf0a66 644
01f7e902
WB
645 my @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ])
646 if defined($ratelimit_bps);
647
da72898c
WB
648 my $migration_snapshot;
649 if (!defined($snapshot)) {
650 if ($scfg->{type} eq 'zfspool') {
651 $migration_snapshot = 1;
652 $snapshot = '__migration__';
1dc01b9f 653 }
da72898c 654 }
e0852ba7 655
cfdffd8a 656 my @formats = volume_transfer_formats($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots);
da72898c
WB
657 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
658 my $format = $formats[0];
7459cb3d 659
228e5be9 660 my $import_fn = '-'; # let pvesm import read from stdin per default
da72898c 661 if ($insecure) {
228e5be9
TL
662 my $net = $target_sshinfo->{network} // $target_sshinfo->{ip};
663 $import_fn = "tcp://$net";
da72898c 664 }
7ba34faa 665
a97d3ee4
FE
666 my $target_apiver = 1; # if there is no apiinfo call, assume 1
667 my $get_api_version = [@$ssh, 'pvesm', 'apiinfo'];
668 my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; };
669 eval { run_command($get_api_version, logfunc => $match_api_version); };
670
e8a7e764 671 my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots];
cfdffd8a 672 my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots];
da72898c
WB
673 if (defined($snapshot)) {
674 push @$send, '-snapshot', $snapshot
675 }
676 if ($migration_snapshot) {
677 push @$recv, '-delete-snapshot', $snapshot;
678 }
a97d3ee4 679 push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5;
3d621977 680
da72898c
WB
681 if (defined($base_snapshot)) {
682 # Check if the snapshot exists on the remote side:
683 push @$send, '-base', $base_snapshot;
684 push @$recv, '-base', $base_snapshot;
685 }
ac191ec7 686
a97d3ee4
FE
687 my $new_volid;
688 my $pattern = volume_imported_message(undef, 1);
689 my $match_volid_and_log = sub {
690 my $line = shift;
691
692 $new_volid = $1 if ($line =~ $pattern);
693
694 if ($logfunc) {
695 chomp($line);
696 $logfunc->($line);
697 }
698 };
699
da72898c
WB
700 volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot;
701 eval {
702 if ($insecure) {
703 open(my $info, '-|', @$recv)
704 or die "receive command failed: $!\n";
705 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
706 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
707 my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM)
708 or die "failed to connect to tunnel at $ip:$port\n";
709 # we won't be reading from the socket
710 shutdown($socket, 0);
8e55b4f2 711 run_command([$send, @cstream], output => '>&'.fileno($socket), errfunc => $logfunc);
da72898c
WB
712 # don't close the connection entirely otherwise the receiving end
713 # might not get all buffered data (and fails with 'connection reset by peer')
714 shutdown($socket, 1);
aca83310
FE
715
716 # wait for the remote process to finish
a97d3ee4
FE
717 while (my $line = <$info>) {
718 $match_volid_and_log->("[$target_sshinfo->{name}] $line");
aca83310
FE
719 }
720
da72898c
WB
721 # now close the socket
722 close($socket);
723 if (!close($info)) { # does waitpid()
724 die "import failed: $!\n" if $!;
725 die "import failed: exit code ".($?>>8)."\n";
0a29ad61
WL
726 }
727 } else {
a97d3ee4 728 run_command([$send, @cstream, $recv], logfunc => $match_volid_and_log);
0a29ad61 729 }
a97d3ee4
FE
730
731 die "unable to get ID of the migrated volume\n"
732 if !defined($new_volid) && $target_apiver >= 5;
da72898c
WB
733 };
734 my $err = $@;
735 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
736 if ($migration_snapshot) {
737 eval { volume_snapshot_delete($cfg, $volid, $snapshot, 0) };
738 warn "could not remove source snapshot: $@\n" if $@;
b6cf0a66 739 }
da72898c 740 die $err if $err;
a97d3ee4
FE
741
742 return $new_volid // $target_volid;
b6cf0a66
DM
743}
744
2502b33b 745sub vdisk_clone {
7bbc4004 746 my ($cfg, $volid, $vmid, $snap) = @_;
1a3459ac 747
2502b33b
DM
748 my ($storeid, $volname) = parse_volume_id($volid);
749
750 my $scfg = storage_config($cfg, $storeid);
751
752 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 753
2502b33b
DM
754 activate_storage($cfg, $storeid);
755
756 # lock shared storage
757 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
7bbc4004 758 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
2502b33b
DM
759 return "$storeid:$volname";
760 });
761}
762
763sub vdisk_create_base {
764 my ($cfg, $volid) = @_;
765
766 my ($storeid, $volname) = parse_volume_id($volid);
767
768 my $scfg = storage_config($cfg, $storeid);
769
770 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 771
2502b33b
DM
772 activate_storage($cfg, $storeid);
773
774 # lock shared storage
775 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
776 my $volname = $plugin->create_base($storeid, $scfg, $volname);
777 return "$storeid:$volname";
778 });
779}
780
40d69893
DM
781sub map_volume {
782 my ($cfg, $volid, $snapname) = @_;
783
784 my ($storeid, $volname) = parse_volume_id($volid);
785
786 my $scfg = storage_config($cfg, $storeid);
787
788 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
789
790 return $plugin->map_volume($storeid, $scfg, $volname, $snapname);
791}
792
793sub unmap_volume {
794 my ($cfg, $volid, $snapname) = @_;
795
796 my ($storeid, $volname) = parse_volume_id($volid);
797
798 my $scfg = storage_config($cfg, $storeid);
799
800 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
801
802 return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
803}
804
1dc01b9f
DM
805sub vdisk_alloc {
806 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
b6cf0a66 807
82fc923f 808 die "no storage ID specified\n" if !$storeid;
b6cf0a66 809
1dc01b9f 810 PVE::JSONSchema::parse_storage_id($storeid);
b6cf0a66 811
1dc01b9f 812 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 813
1dc01b9f 814 die "no VMID specified\n" if !$vmid;
b6cf0a66 815
1dc01b9f 816 $vmid = parse_vmid($vmid);
b6cf0a66 817
1dc01b9f 818 my $defformat = PVE::Storage::Plugin::default_format($scfg);
b6cf0a66 819
1dc01b9f 820 $fmt = $defformat if !$fmt;
b6cf0a66 821
1dc01b9f 822 activate_storage($cfg, $storeid);
3af60e62 823
1dc01b9f 824 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 825
1dc01b9f
DM
826 # lock shared storage
827 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
afdfbe55
WB
828 my $old_umask = umask(umask|0037);
829 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
830 my $err = $@;
831 umask $old_umask;
832 die $err if $err;
1dc01b9f
DM
833 return "$storeid:$volname";
834 });
b6cf0a66
DM
835}
836
1dc01b9f
DM
837sub vdisk_free {
838 my ($cfg, $volid) = @_;
b6cf0a66 839
1dc01b9f 840 my ($storeid, $volname) = parse_volume_id($volid);
1dc01b9f 841 my $scfg = storage_config($cfg, $storeid);
1dc01b9f 842 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 843
1dc01b9f 844 activate_storage($cfg, $storeid);
b6cf0a66 845
1dc01b9f 846 my $cleanup_worker;
b6cf0a66 847
1dc01b9f
DM
848 # lock shared storage
849 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
787624df 850 # LVM-thin allows deletion of still referenced base volumes!
17fb7e42
FG
851 die "base volume '$volname' is still in use by linked clones\n"
852 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
32437ed2 853
17fb7e42 854 my (undef, undef, undef, undef, undef, $isBase, $format) =
32437ed2 855 $plugin->parse_volname($volname);
35533c68 856 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
1dc01b9f 857 });
b6cf0a66 858
1dc01b9f 859 return if !$cleanup_worker;
b6cf0a66 860
1dc01b9f
DM
861 my $rpcenv = PVE::RPCEnvironment::get();
862 my $authuser = $rpcenv->get_user();
b6cf0a66 863
1dc01b9f 864 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
b6cf0a66
DM
865}
866
b6cf0a66
DM
867sub vdisk_list {
868 my ($cfg, $storeid, $vmid, $vollist) = @_;
869
870 my $ids = $cfg->{ids};
871
872 storage_check_enabled($cfg, $storeid) if ($storeid);
873
874 my $res = {};
875
876 # prepare/activate/refresh all storages
877
b6cf0a66
DM
878 my $storage_list = [];
879 if ($vollist) {
880 foreach my $volid (@$vollist) {
1dc01b9f
DM
881 my ($sid, undef) = parse_volume_id($volid);
882 next if !defined($ids->{$sid});
b6cf0a66
DM
883 next if !storage_check_enabled($cfg, $sid, undef, 1);
884 push @$storage_list, $sid;
b6cf0a66
DM
885 }
886 } else {
887 foreach my $sid (keys %$ids) {
888 next if $storeid && $storeid ne $sid;
889 next if !storage_check_enabled($cfg, $sid, undef, 1);
890 push @$storage_list, $sid;
b6cf0a66
DM
891 }
892 }
893
1dc01b9f 894 my $cache = {};
b6cf0a66 895
1dc01b9f 896 activate_storage_list($cfg, $storage_list, $cache);
b6cf0a66
DM
897
898 foreach my $sid (keys %$ids) {
1dc01b9f
DM
899 next if $storeid && $storeid ne $sid;
900 next if !storage_check_enabled($cfg, $sid, undef, 1);
b6cf0a66 901
1dc01b9f
DM
902 my $scfg = $ids->{$sid};
903 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
904 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
b6cf0a66
DM
905 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
906 }
907
908 return $res;
909}
910
c2fc9fbe
DM
911sub template_list {
912 my ($cfg, $storeid, $tt) = @_;
913
914 die "unknown template type '$tt'\n"
915 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets');
916
917 my $ids = $cfg->{ids};
918
919 storage_check_enabled($cfg, $storeid) if ($storeid);
920
921 my $res = {};
922
923 # query the storage
924 foreach my $sid (keys %$ids) {
925 next if $storeid && $storeid ne $sid;
926
927 my $scfg = $ids->{$sid};
928 my $type = $scfg->{type};
929
930 next if !$scfg->{content}->{$tt};
931
932 next if !storage_check_enabled($cfg, $sid, undef, 1);
933
934 $res->{$sid} = volume_list($cfg, $sid, undef, $tt);
935 }
936
937 return $res;
938}
939
37ba0aea
DM
940sub volume_list {
941 my ($cfg, $storeid, $vmid, $content) = @_;
942
be785439 943 my @ctypes = qw(rootdir images vztmpl iso backup snippets);
37ba0aea
DM
944
945 my $cts = $content ? [ $content ] : [ @ctypes ];
946
947 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
948
c2fc9fbe 949 $cts = [ grep { defined($scfg->{content}->{$_}) } @$cts ];
37ba0aea 950
c2fc9fbe 951 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
37ba0aea 952
c2fc9fbe
DM
953 activate_storage($cfg, $storeid);
954
955 my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts);
956
957 @$res = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @$res;
37ba0aea
DM
958
959 return $res;
960}
961
b6cf0a66
DM
962sub uevent_seqnum {
963
964 my $filename = "/sys/kernel/uevent_seqnum";
965
966 my $seqnum = 0;
1dc01b9f 967 if (my $fh = IO::File->new($filename, "r")) {
b6cf0a66
DM
968 my $line = <$fh>;
969 if ($line =~ m/^(\d+)$/) {
1dc01b9f 970 $seqnum = int($1);
b6cf0a66
DM
971 }
972 close ($fh);
973 }
974 return $seqnum;
975}
976
f3d4ef46 977sub activate_storage {
1dc01b9f 978 my ($cfg, $storeid, $cache) = @_;
b6cf0a66 979
f3d4ef46
DM
980 $cache = {} if !$cache;
981
b6cf0a66
DM
982 my $scfg = storage_check_enabled($cfg, $storeid);
983
1dc01b9f 984 return if $cache->{activated}->{$storeid};
b6cf0a66 985
1dc01b9f 986 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
b6cf0a66 987
1dc01b9f 988 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 989
1dc01b9f
DM
990 if ($scfg->{base}) {
991 my ($baseid, undef) = parse_volume_id ($scfg->{base});
f3d4ef46
DM
992 activate_storage($cfg, $baseid, $cache);
993 }
994
995 if (!$plugin->check_connection($storeid, $scfg)) {
996 die "storage '$storeid' is not online\n";
b6cf0a66
DM
997 }
998
1dc01b9f
DM
999 $plugin->activate_storage($storeid, $scfg, $cache);
1000
b6cf0a66
DM
1001 my $newseq = uevent_seqnum ();
1002
1003 # only call udevsettle if there are events
1dc01b9f 1004 if ($newseq > $cache->{uevent_seqnum}) {
b6cf0a66
DM
1005 my $timeout = 30;
1006 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1dc01b9f 1007 $cache->{uevent_seqnum} = $newseq;
b6cf0a66
DM
1008 }
1009
1dc01b9f 1010 $cache->{activated}->{$storeid} = 1;
b6cf0a66
DM
1011}
1012
1013sub activate_storage_list {
1dc01b9f 1014 my ($cfg, $storeid_list, $cache) = @_;
b6cf0a66 1015
1dc01b9f 1016 $cache = {} if !$cache;
b6cf0a66
DM
1017
1018 foreach my $storeid (@$storeid_list) {
f3d4ef46 1019 activate_storage($cfg, $storeid, $cache);
b6cf0a66
DM
1020 }
1021}
1022
1dc01b9f
DM
1023sub deactivate_storage {
1024 my ($cfg, $storeid) = @_;
1025
1026 my $scfg = storage_config ($cfg, $storeid);
1027 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 1028
1dc01b9f
DM
1029 my $cache = {};
1030 $plugin->deactivate_storage($storeid, $scfg, $cache);
b6cf0a66
DM
1031}
1032
1033sub activate_volumes {
02e797b8 1034 my ($cfg, $vollist, $snapname) = @_;
6703353b
DM
1035
1036 return if !($vollist && scalar(@$vollist));
1037
b6cf0a66
DM
1038 my $storagehash = {};
1039 foreach my $volid (@$vollist) {
1dc01b9f 1040 my ($storeid, undef) = parse_volume_id($volid);
b6cf0a66
DM
1041 $storagehash->{$storeid} = 1;
1042 }
1043
1dc01b9f
DM
1044 my $cache = {};
1045
1046 activate_storage_list($cfg, [keys %$storagehash], $cache);
b6cf0a66
DM
1047
1048 foreach my $volid (@$vollist) {
5521b580 1049 my ($storeid, $volname) = parse_volume_id($volid);
1dc01b9f
DM
1050 my $scfg = storage_config($cfg, $storeid);
1051 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
02e797b8 1052 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
b6cf0a66
DM
1053 }
1054}
1055
1056sub deactivate_volumes {
02e797b8 1057 my ($cfg, $vollist, $snapname) = @_;
b6cf0a66 1058
6703353b
DM
1059 return if !($vollist && scalar(@$vollist));
1060
1dc01b9f
DM
1061 my $cache = {};
1062
6703353b 1063 my @errlist = ();
b6cf0a66 1064 foreach my $volid (@$vollist) {
1dc01b9f 1065 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 1066
1dc01b9f
DM
1067 my $scfg = storage_config($cfg, $storeid);
1068 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 1069
1dc01b9f 1070 eval {
02e797b8 1071 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1dc01b9f
DM
1072 };
1073 if (my $err = $@) {
1074 warn $err;
1075 push @errlist, $volid;
b6cf0a66
DM
1076 }
1077 }
6703353b 1078
82fc923f 1079 die "volume deactivation failed: " . join(' ', @errlist)
6703353b 1080 if scalar(@errlist);
b6cf0a66
DM
1081}
1082
1a3459ac 1083sub storage_info {
856c54bd 1084 my ($cfg, $content, $includeformat) = @_;
b6cf0a66
DM
1085
1086 my $ids = $cfg->{ids};
1087
1088 my $info = {};
ff3badd8 1089
583c2802 1090 my @ctypes = PVE::Tools::split_list($content);
ff3badd8 1091
b6cf0a66
DM
1092 my $slist = [];
1093 foreach my $storeid (keys %$ids) {
6ce4f724 1094 my $storage_enabled = defined(storage_check_enabled($cfg, $storeid, undef, 1));
b6cf0a66 1095
d73060be
DM
1096 if (defined($content)) {
1097 my $want_ctype = 0;
1098 foreach my $ctype (@ctypes) {
1099 if ($ids->{$storeid}->{content}->{$ctype}) {
1100 $want_ctype = 1;
1101 last;
1102 }
583c2802 1103 }
6ce4f724 1104 next if !$want_ctype || !$storage_enabled;
583c2802 1105 }
ff3badd8 1106
b6cf0a66
DM
1107 my $type = $ids->{$storeid}->{type};
1108
1a3459ac 1109 $info->{$storeid} = {
b6cf0a66 1110 type => $type,
1a3459ac
DM
1111 total => 0,
1112 avail => 0,
1113 used => 0,
04a2e4f3 1114 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1dc01b9f 1115 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
b6cf0a66 1116 active => 0,
6ce4f724 1117 enabled => $storage_enabled ? 1 : 0,
b6cf0a66
DM
1118 };
1119
b6cf0a66
DM
1120 push @$slist, $storeid;
1121 }
1122
1dc01b9f 1123 my $cache = {};
b6cf0a66 1124
b6cf0a66
DM
1125 foreach my $storeid (keys %$ids) {
1126 my $scfg = $ids->{$storeid};
b43b073b 1127
b6cf0a66 1128 next if !$info->{$storeid};
b43b073b 1129 next if !$info->{$storeid}->{enabled};
b6cf0a66 1130
856c54bd
DC
1131 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1132 if ($includeformat) {
1133 my $pd = $plugin->plugindata();
1134 $info->{$storeid}->{format} = $pd->{format}
1135 if $pd->{format};
1136 $info->{$storeid}->{select_existing} = $pd->{select_existing}
1137 if $pd->{select_existing};
1138 }
1139
f3d4ef46
DM
1140 eval { activate_storage($cfg, $storeid, $cache); };
1141 if (my $err = $@) {
1142 warn $err;
1143 next;
1144 }
1145
41aacc6c 1146 my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); };
7028645e 1147 warn $@ if $@;
1dc01b9f 1148 next if !$active;
ff3badd8
DM
1149 $info->{$storeid}->{total} = int($total);
1150 $info->{$storeid}->{avail} = int($avail);
1151 $info->{$storeid}->{used} = int($used);
1dc01b9f 1152 $info->{$storeid}->{active} = $active;
b6cf0a66
DM
1153 }
1154
1155 return $info;
1156}
1157
1158sub resolv_server {
1159 my ($server) = @_;
1a3459ac 1160
c67daeac
WB
1161 my ($packed_ip, $family);
1162 eval {
1163 my @res = PVE::Tools::getaddrinfo_all($server);
1164 $family = $res[0]->{family};
1165 $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
1166 };
b6cf0a66 1167 if (defined $packed_ip) {
ee302b1c 1168 return Socket::inet_ntop($family, $packed_ip);
b6cf0a66
DM
1169 }
1170 return undef;
1171}
1172
1173sub scan_nfs {
1174 my ($server_in) = @_;
1175
1176 my $server;
1177 if (!($server = resolv_server ($server_in))) {
1178 die "unable to resolve address for server '${server_in}'\n";
1179 }
1180
1181 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1182
1183 my $res = {};
f81372ac 1184 run_command($cmd, outfunc => sub {
b6cf0a66
DM
1185 my $line = shift;
1186
1187 # note: howto handle white spaces in export path??
1188 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1189 $res->{$1} = $2;
1190 }
1191 });
1192
1193 return $res;
1194}
1195
4cab0acd
WL
1196sub scan_cifs {
1197 my ($server_in, $user, $password, $domain) = @_;
1198
1199 my $server;
1200 if (!($server = resolv_server ($server_in))) {
1201 die "unable to resolve address for server '${server_in}'\n";
1202 }
1203
1204 # we support only Windows grater than 2012 cifsscan so use smb3
1205 my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server];
1206 if (defined($user)) {
1207 die "password is required" if !defined($password);
1208 push @$cmd, '-U', "$user\%$password";
1209 push @$cmd, '-W', $domain if defined($domain);
1210 } else {
1211 push @$cmd, '-N';
1212 }
1213
1214 my $res = {};
1215 run_command($cmd,
1216 outfunc => sub {
1217 my $line = shift;
1218 if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) {
1219 $res->{$1} = $2;
1220 } elsif ($line =~ m/(NT_STATUS_(\S*))/) {
1221 $res->{$1} = '';
1222 }
1223 },
1224 errfunc => sub {},
1225 noerr => 1
1226 );
1227
1228 return $res;
1229}
1230
584d97f6
DM
1231sub scan_zfs {
1232
3881e680 1233 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-Hp', '-o', 'name,avail,used'];
584d97f6
DM
1234
1235 my $res = [];
1236 run_command($cmd, outfunc => sub {
1237 my $line = shift;
1238
1239 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
3932390b 1240 my ($pool, $size_str, $used_str) = ($1, $2, $3);
3881e680
AL
1241 my $size = $size_str + 0;
1242 my $used = $used_str + 0;
48e27f79 1243 # ignore subvolumes generated by our ZFSPoolPlugin
851658c3
WL
1244 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1245 return if $pool =~ m!/basevol-\d+-[^/]+$!;
3932390b 1246 push @$res, { pool => $pool, size => $size, free => $size-$used };
584d97f6
DM
1247 }
1248 });
1249
1250 return $res;
1251}
1252
b6cf0a66
DM
1253sub resolv_portal {
1254 my ($portal, $noerr) = @_;
1255
1689e627
WB
1256 my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
1257 if ($server) {
b6cf0a66
DM
1258 if (my $ip = resolv_server($server)) {
1259 $server = $ip;
1689e627 1260 $server = "[$server]" if $server =~ /^$IPV6RE$/;
b6cf0a66
DM
1261 return $port ? "$server:$port" : $server;
1262 }
1263 }
1264 return undef if $noerr;
1265
1266 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
1267}
1268
b6cf0a66
DM
1269
1270sub scan_iscsi {
1271 my ($portal_in) = @_;
1272
1273 my $portal;
1dc01b9f 1274 if (!($portal = resolv_portal($portal_in))) {
b6cf0a66
DM
1275 die "unable to parse/resolve portal address '${portal_in}'\n";
1276 }
1277
1dc01b9f 1278 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
b6cf0a66
DM
1279}
1280
1281sub storage_default_format {
1282 my ($cfg, $storeid) = @_;
1283
1284 my $scfg = storage_config ($cfg, $storeid);
1285
1dc01b9f 1286 return PVE::Storage::Plugin::default_format($scfg);
b6cf0a66
DM
1287}
1288
1289sub vgroup_is_used {
1290 my ($cfg, $vgname) = @_;
1291
1292 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 1293 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
1294 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
1295 return 1;
1296 }
1297 }
1298
1299 return undef;
1300}
1301
1302sub target_is_used {
1303 my ($cfg, $target) = @_;
1304
1305 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 1306 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
1307 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
1308 return 1;
1309 }
1310 }
1311
1312 return undef;
1313}
1314
1315sub volume_is_used {
1316 my ($cfg, $volid) = @_;
1317
1318 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 1319 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
1320 if ($scfg->{base} && $scfg->{base} eq $volid) {
1321 return 1;
1322 }
1323 }
1324
1325 return undef;
1326}
1327
1328sub storage_is_used {
1329 my ($cfg, $storeid) = @_;
1330
1331 foreach my $sid (keys %{$cfg->{ids}}) {
1dc01b9f 1332 my $scfg = storage_config($cfg, $sid);
b6cf0a66 1333 next if !$scfg->{base};
1dc01b9f 1334 my ($st) = parse_volume_id($scfg->{base});
b6cf0a66
DM
1335 return 1 if $st && $st eq $storeid;
1336 }
1337
1338 return undef;
1339}
1340
1341sub foreach_volid {
1342 my ($list, $func) = @_;
1343
1344 return if !$list;
1345
1346 foreach my $sid (keys %$list) {
1347 foreach my $info (@{$list->{$sid}}) {
1348 my $volid = $info->{volid};
1dc01b9f 1349 my ($sid1, $volname) = parse_volume_id($volid, 1);
b6cf0a66
DM
1350 if ($sid1 && $sid1 eq $sid) {
1351 &$func ($volid, $sid, $info);
1352 } else {
1353 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1354 }
1355 }
1356 }
1357}
1358
cd554b79
AA
1359sub decompressor_info {
1360 my ($format, $comp) = @_;
1361
1362 if ($format eq 'tgz' && !defined($comp)) {
1363 ($format, $comp) = ('tar', 'gz');
1364 }
1365
1366 my $decompressor = {
1367 tar => {
1368 gz => ['tar', '-z'],
1369 lzo => ['tar', '--lzop'],
014d36db 1370 zst => ['tar', '--zstd'],
cd554b79
AA
1371 },
1372 vma => {
1373 gz => ['zcat'],
1374 lzo => ['lzop', '-d', '-c'],
014d36db 1375 zst => ['zstd', '-q', '-d', '-c'],
cd554b79
AA
1376 },
1377 };
1378
1379 die "ERROR: archive format not defined\n"
1380 if !defined($decompressor->{$format});
1381
1382 my $decomp = $decompressor->{$format}->{$comp} if $comp;
1383
1384 my $info = {
1385 format => $format,
1386 compression => $comp,
1387 decompressor => $decomp,
1388 };
1389
1390 return $info;
1391}
1392
1393sub archive_info {
1394 my ($archive) = shift;
1395 my $info;
1396
1397 my $volid = basename($archive);
fb821c18
FE
1398 if ($volid =~ /^vzdump-(lxc|openvz|qemu)-\d+-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?$/) {
1399 my ($type, $format, $comp) = ($1, $2, $3);
1400 my $format_re = defined($comp) ? "$format.$comp" : "$format";
1401 $info = decompressor_info($format, $comp);
1402 $info->{type} = $type;
1403
1404 if ($volid =~ /^vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.${format_re}$/) {
1405 $info->{vmid} = int($1);
1406 $info->{ctime} = timelocal($7, $6, $5, $4, $3 - 1, $2 - 1900);
1407 $info->{is_std_name} = 1;
1408 } else {
1409 $info->{is_std_name} = 0;
1410 }
cd554b79 1411 } else {
bf5af0fb 1412 die "ERROR: couldn't determine archive info from '$archive'\n";
cd554b79
AA
1413 }
1414
1415 return $info;
1416}
1417
8898dd7b
FG
1418sub extract_vzdump_config_tar {
1419 my ($archive, $conf_re) = @_;
1420
1421 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
1422
1423 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1424 die "unable to open file '$archive'\n";
1425
1426 my $file;
1427 while (defined($file = <$fh>)) {
086c4bf1 1428 if ($file =~ $conf_re) {
8898dd7b
FG
1429 $file = $1; # untaint
1430 last;
1431 }
1432 }
1433
1434 kill 15, $pid;
1435 waitpid $pid, 0;
1436 close $fh;
1437
1438 die "ERROR: archive contains no configuration file\n" if !$file;
1439 chomp $file;
1440
1441 my $raw = '';
1442 my $out = sub {
1443 my $output = shift;
1444 $raw .= "$output\n";
1445 };
1446
63e89295 1447 run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out);
8898dd7b
FG
1448
1449 return wantarray ? ($raw, $file) : $raw;
1450}
1451
1452sub extract_vzdump_config_vma {
1453 my ($archive, $comp) = @_;
1454
8898dd7b 1455 my $raw = '';
63e89295 1456 my $out = sub { $raw .= "$_[0]\n"; };
8898dd7b 1457
cd554b79
AA
1458 my $info = archive_info($archive);
1459 $comp //= $info->{compression};
1460 my $decompressor = $info->{decompressor};
1461
8898dd7b 1462 if ($comp) {
63e89295 1463 my $cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ];
8898dd7b 1464
63e89295 1465 # lzop/zcat exits with 1 when the pipe is closed early by vma, detect this and ignore the exit code later
8898dd7b
FG
1466 my $broken_pipe;
1467 my $errstring;
1468 my $err = sub {
1469 my $output = shift;
014d36db 1470 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/ || $output =~ m/zstd: error 70 : Write error : Broken pipe/) {
8898dd7b
FG
1471 $broken_pipe = 1;
1472 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1473 $errstring = "Failed to extract config from VMA archive: $output\n";
1474 }
1475 };
1476
63e89295 1477 my $rc = eval { run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1) };
8898dd7b
FG
1478 my $rerr = $@;
1479
63e89295
TL
1480 $broken_pipe ||= $rc == 141; # broken pipe from vma POV
1481
1482 if (!$errstring && !$broken_pipe && $rc != 0) {
8898dd7b
FG
1483 die "$rerr\n" if $rerr;
1484 die "config extraction failed with exit code $rc\n";
1485 }
1486 die "$errstring\n" if $errstring;
1487 } else {
63e89295 1488 run_command(["vma", "config", $archive], outfunc => $out);
8898dd7b
FG
1489 }
1490
1491 return wantarray ? ($raw, undef) : $raw;
1492}
1493
1494sub extract_vzdump_config {
1495 my ($cfg, $volid) = @_;
1496
c855ac15
DM
1497 my ($storeid, $volname) = parse_volume_id($volid);
1498 if (defined($storeid)) {
1499 my $scfg = storage_config($cfg, $storeid);
1500 if ($scfg->{type} eq 'pbs') {
1501 storage_check_enabled($cfg, $storeid);
1502 return PVE::Storage::PBSPlugin->extract_vzdump_config($scfg, $volname, $storeid);
1503 }
1504 }
1505
8898dd7b 1506 my $archive = abs_filesystem_path($cfg, $volid);
cd554b79
AA
1507 my $info = archive_info($archive);
1508 my $format = $info->{format};
1509 my $comp = $info->{compression};
1510 my $type = $info->{type};
8898dd7b 1511
cd554b79 1512 if ($type eq 'lxc' || $type eq 'openvz') {
086c4bf1 1513 return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
cd554b79 1514 } elsif ($type eq 'qemu') {
8898dd7b
FG
1515 if ($format eq 'tar') {
1516 return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!);
1517 } else {
1518 return extract_vzdump_config_vma($archive, $comp);
1519 }
1520 } else {
1521 die "cannot determine backup guest type for backup archive '$volid'\n";
1522 }
1523}
1524
47f37b53
WB
1525sub volume_export {
1526 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1527
1528 my ($storeid, $volname) = parse_volume_id($volid, 1);
1529 die "cannot export volume '$volid'\n" if !$storeid;
1530 my $scfg = storage_config($cfg, $storeid);
1531 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1532 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1533 $snapshot, $base_snapshot, $with_snapshots);
1534}
1535
1536sub volume_import {
a97d3ee4 1537 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
47f37b53
WB
1538
1539 my ($storeid, $volname) = parse_volume_id($volid, 1);
1540 die "cannot import into volume '$volid'\n" if !$storeid;
1541 my $scfg = storage_config($cfg, $storeid);
1542 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1543 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
a97d3ee4 1544 $base_snapshot, $with_snapshots, $allow_rename) // $volid;
47f37b53
WB
1545}
1546
d390328b
WB
1547sub volume_export_formats {
1548 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1549
1550 my ($storeid, $volname) = parse_volume_id($volid, 1);
1551 return if !$storeid;
1552 my $scfg = storage_config($cfg, $storeid);
1553 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1554 return $plugin->volume_export_formats($scfg, $storeid, $volname,
ae36189d
WB
1555 $snapshot, $base_snapshot,
1556 $with_snapshots);
d390328b
WB
1557}
1558
1559sub volume_import_formats {
1560 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1561
1562 my ($storeid, $volname) = parse_volume_id($volid, 1);
1563 return if !$storeid;
1564 my $scfg = storage_config($cfg, $storeid);
1565 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1566 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1567 $base_snapshot, $with_snapshots);
1568}
1569
1570sub volume_transfer_formats {
1571 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1572 my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1573 my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1574 my %import_hash = map { $_ => 1 } @import_formats;
1575 my @common = grep { $import_hash{$_} } @export_formats;
1576 return @common;
1577}
1578
a97d3ee4
FE
1579sub volume_imported_message {
1580 my ($volid, $want_pattern) = @_;
1581
1582 if ($want_pattern) {
1583 return qr/successfully imported '([^']*)'$/;
1584 } else {
1585 return "successfully imported '$volid'\n";
1586 }
1587}
1588
f7621c01
DM
1589# bash completion helper
1590
1591sub complete_storage {
746e530f 1592 my ($cmdname, $pname, $cvalue) = @_;
f7621c01 1593
746e530f 1594 my $cfg = PVE::Storage::config();
180c8b02 1595
746e530f 1596 return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ];
f7621c01
DM
1597}
1598
1599sub complete_storage_enabled {
746e530f 1600 my ($cmdname, $pname, $cvalue) = @_;
f7621c01 1601
746e530f 1602 my $res = [];
f7621c01 1603
746e530f
DM
1604 my $cfg = PVE::Storage::config();
1605 foreach my $sid (keys %{$cfg->{ids}}) {
1606 next if !storage_check_enabled($cfg, $sid, undef, 1);
1607 push @$res, $sid;
1608 }
1609 return $res;
f7621c01
DM
1610}
1611
98437f4c
DM
1612sub complete_content_type {
1613 my ($cmdname, $pname, $cvalue) = @_;
1614
7c7ae12f 1615 return [qw(rootdir images vztmpl iso backup snippets)];
98437f4c
DM
1616}
1617
bf7aed26
DM
1618sub complete_volume {
1619 my ($cmdname, $pname, $cvalue) = @_;
1620
1621 my $cfg = config();
1622
1623 my $storage_list = complete_storage_enabled();
1624
b70b0c58
DM
1625 if ($cvalue =~ m/^([^:]+):/) {
1626 $storage_list = [ $1 ];
1627 } else {
1628 if (scalar(@$storage_list) > 1) {
1629 # only list storage IDs to avoid large listings
1630 my $res = [];
1631 foreach my $storeid (@$storage_list) {
1632 # Hack: simply return 2 artificial values, so that
1633 # completions does not finish
1634 push @$res, "$storeid:volname", "$storeid:...";
1635 }
1636 return $res;
1637 }
1638 }
1639
bf7aed26
DM
1640 my $res = [];
1641 foreach my $storeid (@$storage_list) {
1642 my $vollist = PVE::Storage::volume_list($cfg, $storeid);
1643
1644 foreach my $item (@$vollist) {
1645 push @$res, $item->{volid};
1646 }
1647 }
1648
1649 return $res;
1650}
1651
9edb99a5
WB
1652# Various io-heavy operations require io/bandwidth limits which can be
1653# configured on multiple levels: The global defaults in datacenter.cfg, and
1654# per-storage overrides. When we want to do a restore from storage A to storage
1655# B, we should take the smaller limit defined for storages A and B, and if no
1656# such limit was specified, use the one from datacenter.cfg.
1657sub get_bandwidth_limit {
1658 my ($operation, $storage_list, $override) = @_;
1659
1660 # called for each limit (global, per-storage) with the 'default' and the
1661 # $operation limit and should udpate $override for every limit affecting
1662 # us.
1663 my $use_global_limits = 0;
1664 my $apply_limit = sub {
1665 my ($bwlimit) = @_;
1666 if (defined($bwlimit)) {
1667 my $limits = PVE::JSONSchema::parse_property_string('bwlimit', $bwlimit);
1668 my $limit = $limits->{$operation} // $limits->{default};
1669 if (defined($limit)) {
1670 if (!$override || $limit < $override) {
1671 $override = $limit;
1672 }
1673 return;
1674 }
1675 }
1676 # If there was no applicable limit, try to apply the global ones.
1677 $use_global_limits = 1;
1678 };
1679
77445e9b
WB
1680 my ($rpcenv, $authuser);
1681 if (defined($override)) {
1682 $rpcenv = PVE::RPCEnvironment->get();
1683 $authuser = $rpcenv->get_user();
1684 }
9edb99a5
WB
1685
1686 # Apply per-storage limits - if there are storages involved.
074bdd35 1687 if (defined($storage_list) && @$storage_list) {
9edb99a5
WB
1688 my $config = config();
1689
1690 # The Datastore.Allocate permission allows us to modify the per-storage
1691 # limits, therefore it also allows us to override them.
1692 # Since we have most likely multiple storages to check, do a quick check on
1693 # the general '/storage' path to see if we can skip the checks entirely:
77445e9b 1694 return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1);
9edb99a5
WB
1695
1696 my %done;
1697 foreach my $storage (@$storage_list) {
396aedff 1698 next if !defined($storage);
9edb99a5
WB
1699 # Avoid duplicate checks:
1700 next if $done{$storage};
1701 $done{$storage} = 1;
1702
1703 # Otherwise we may still have individual /storage/$ID permissions:
77445e9b 1704 if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) {
9edb99a5
WB
1705 # And if not: apply the limits.
1706 my $storecfg = storage_config($config, $storage);
1707 $apply_limit->($storecfg->{bwlimit});
1708 }
1709 }
1710
1711 # Storage limits take precedence over the datacenter defaults, so if
1712 # a limit was applied:
1713 return $override if !$use_global_limits;
1714 }
1715
1716 # Sys.Modify on '/' means we can change datacenter.cfg which contains the
1717 # global default limits.
77445e9b 1718 if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) {
9edb99a5
WB
1719 # So if we cannot modify global limits, apply them to our currently
1720 # requested override.
1721 my $dc = cfs_read_file('datacenter.cfg');
1722 $apply_limit->($dc->{bwlimit});
1723 }
1724
1725 return $override;
1726}
1727
76c1e57b 1728# checks if the storage id is available and dies if not
9280153e
TL
1729sub assert_sid_unused {
1730 my ($sid) = @_;
76c1e57b
DC
1731
1732 my $cfg = config();
9280153e
TL
1733 if (my $scfg = storage_config($cfg, $sid, 1)) {
1734 die "storage ID '$sid' already defined\n";
76c1e57b
DC
1735 }
1736
1737 return undef;
1738}
1739
b6cf0a66 17401;