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