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