]>
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::ISCSIDirectPlugin; | |
33 | use PVE::Storage::GlusterfsPlugin; | |
34 | use PVE::Storage::ZFSPoolPlugin; | |
35 | use PVE::Storage::ZFSPlugin; | |
36 | use PVE::Storage::DRBDPlugin; | |
37 | ||
38 | # Storage API version. Icrement it on changes in storage API interface. | |
39 | use constant APIVER => 3; | |
40 | # Age is the number of versions we're backward compatible with. | |
41 | # This is like having 'current=APIVER' and age='APIAGE' in libtool, | |
42 | # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html | |
43 | use constant APIAGE => 2; | |
44 | ||
45 | # load standard plugins | |
46 | PVE::Storage::DirPlugin->register(); | |
47 | PVE::Storage::LVMPlugin->register(); | |
48 | PVE::Storage::LvmThinPlugin->register(); | |
49 | PVE::Storage::NFSPlugin->register(); | |
50 | PVE::Storage::CIFSPlugin->register(); | |
51 | PVE::Storage::ISCSIPlugin->register(); | |
52 | PVE::Storage::RBDPlugin->register(); | |
53 | PVE::Storage::CephFSPlugin->register(); | |
54 | PVE::Storage::ISCSIDirectPlugin->register(); | |
55 | PVE::Storage::GlusterfsPlugin->register(); | |
56 | PVE::Storage::ZFSPoolPlugin->register(); | |
57 | PVE::Storage::ZFSPlugin->register(); | |
58 | PVE::Storage::DRBDPlugin->register(); | |
59 | ||
60 | # load third-party plugins | |
61 | if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) { | |
62 | dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub { | |
63 | my ($file) = @_; | |
64 | my $modname = 'PVE::Storage::Custom::' . $file; | |
65 | $modname =~ s!\.pm$!!; | |
66 | $file = 'PVE/Storage/Custom/' . $file; | |
67 | ||
68 | eval { | |
69 | require $file; | |
70 | ||
71 | # Check perl interface: | |
72 | die "not derived from PVE::Storage::Plugin\n" | |
73 | if !$modname->isa('PVE::Storage::Plugin'); | |
74 | die "does not provide an api() method\n" | |
75 | if !$modname->can('api'); | |
76 | # Check storage API version and that file is really storage plugin. | |
77 | my $version = $modname->api(); | |
78 | die "implements an API version newer than current ($version > " . APIVER . ")\n" | |
79 | if $version > APIVER; | |
80 | my $min_version = (APIVER - APIAGE); | |
81 | die "API version too old, please update the plugin ($version < $min_version)\n" | |
82 | if $version < $min_version; | |
83 | import $file; | |
84 | $modname->register(); | |
85 | ||
86 | # If we got this far and the API version is not the same, make some | |
87 | # noise: | |
88 | warn "Plugin \"$modname\" is implementing an older storage API, an upgrade is recommended\n" | |
89 | if $version != APIVER; | |
90 | }; | |
91 | if ($@) { | |
92 | warn "Error loading storage plugin \"$modname\": $@"; | |
93 | } | |
94 | }); | |
95 | } | |
96 | ||
97 | # initialize all plugins | |
98 | PVE::Storage::Plugin->init(); | |
99 | ||
100 | my $UDEVADM = '/sbin/udevadm'; | |
101 | ||
102 | our $iso_extension_re = qr/\.(?:iso|img)/i; | |
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/([^/]+$iso_extension_re)$!) { | |
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, $target_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 $import_fn = '-'; # let pvesm import read from stdin per default | |
596 | if ($insecure) { | |
597 | my $net = $target_sshinfo->{network} // $target_sshinfo->{ip}; | |
598 | $import_fn = "tcp://$net"; | |
599 | } | |
600 | ||
601 | $with_snapshots = $with_snapshots ? 1 : 0; # sanitize for passing as cli parameter | |
602 | my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots]; | |
603 | my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots]; | |
604 | if (defined($snapshot)) { | |
605 | push @$send, '-snapshot', $snapshot | |
606 | } | |
607 | if ($migration_snapshot) { | |
608 | push @$recv, '-delete-snapshot', $snapshot; | |
609 | } | |
610 | ||
611 | if (defined($base_snapshot)) { | |
612 | # Check if the snapshot exists on the remote side: | |
613 | push @$send, '-base', $base_snapshot; | |
614 | push @$recv, '-base', $base_snapshot; | |
615 | } | |
616 | ||
617 | volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot; | |
618 | eval { | |
619 | if ($insecure) { | |
620 | open(my $info, '-|', @$recv) | |
621 | or die "receive command failed: $!\n"; | |
622 | my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n"; | |
623 | my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n"; | |
624 | my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM) | |
625 | or die "failed to connect to tunnel at $ip:$port\n"; | |
626 | # we won't be reading from the socket | |
627 | shutdown($socket, 0); | |
628 | run_command([$send, @cstream], output => '>&'.fileno($socket)); | |
629 | # don't close the connection entirely otherwise the receiving end | |
630 | # might not get all buffered data (and fails with 'connection reset by peer') | |
631 | shutdown($socket, 1); | |
632 | 1 while <$info>; # wait for the remote process to finish | |
633 | # now close the socket | |
634 | close($socket); | |
635 | if (!close($info)) { # does waitpid() | |
636 | die "import failed: $!\n" if $!; | |
637 | die "import failed: exit code ".($?>>8)."\n"; | |
638 | } | |
639 | } else { | |
640 | run_command([$send, @cstream, $recv], logfunc => $logfunc); | |
641 | } | |
642 | }; | |
643 | my $err = $@; | |
644 | warn "send/receive failed, cleaning up snapshot(s)..\n" if $err; | |
645 | if ($migration_snapshot) { | |
646 | eval { volume_snapshot_delete($cfg, $volid, $snapshot, 0) }; | |
647 | warn "could not remove source snapshot: $@\n" if $@; | |
648 | } | |
649 | die $err if $err; | |
650 | } | |
651 | ||
652 | sub vdisk_clone { | |
653 | my ($cfg, $volid, $vmid, $snap) = @_; | |
654 | ||
655 | my ($storeid, $volname) = parse_volume_id($volid); | |
656 | ||
657 | my $scfg = storage_config($cfg, $storeid); | |
658 | ||
659 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
660 | ||
661 | activate_storage($cfg, $storeid); | |
662 | ||
663 | # lock shared storage | |
664 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
665 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); | |
666 | return "$storeid:$volname"; | |
667 | }); | |
668 | } | |
669 | ||
670 | sub vdisk_create_base { | |
671 | my ($cfg, $volid) = @_; | |
672 | ||
673 | my ($storeid, $volname) = parse_volume_id($volid); | |
674 | ||
675 | my $scfg = storage_config($cfg, $storeid); | |
676 | ||
677 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
678 | ||
679 | activate_storage($cfg, $storeid); | |
680 | ||
681 | # lock shared storage | |
682 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
683 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
684 | return "$storeid:$volname"; | |
685 | }); | |
686 | } | |
687 | ||
688 | sub map_volume { | |
689 | my ($cfg, $volid, $snapname) = @_; | |
690 | ||
691 | my ($storeid, $volname) = parse_volume_id($volid); | |
692 | ||
693 | my $scfg = storage_config($cfg, $storeid); | |
694 | ||
695 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
696 | ||
697 | return $plugin->map_volume($storeid, $scfg, $volname, $snapname); | |
698 | } | |
699 | ||
700 | sub unmap_volume { | |
701 | my ($cfg, $volid, $snapname) = @_; | |
702 | ||
703 | my ($storeid, $volname) = parse_volume_id($volid); | |
704 | ||
705 | my $scfg = storage_config($cfg, $storeid); | |
706 | ||
707 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
708 | ||
709 | return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname); | |
710 | } | |
711 | ||
712 | sub vdisk_alloc { | |
713 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
714 | ||
715 | die "no storage ID specified\n" if !$storeid; | |
716 | ||
717 | PVE::JSONSchema::parse_storage_id($storeid); | |
718 | ||
719 | my $scfg = storage_config($cfg, $storeid); | |
720 | ||
721 | die "no VMID specified\n" if !$vmid; | |
722 | ||
723 | $vmid = parse_vmid($vmid); | |
724 | ||
725 | my $defformat = PVE::Storage::Plugin::default_format($scfg); | |
726 | ||
727 | $fmt = $defformat if !$fmt; | |
728 | ||
729 | activate_storage($cfg, $storeid); | |
730 | ||
731 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
732 | ||
733 | # lock shared storage | |
734 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
735 | my $old_umask = umask(umask|0037); | |
736 | my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) }; | |
737 | my $err = $@; | |
738 | umask $old_umask; | |
739 | die $err if $err; | |
740 | return "$storeid:$volname"; | |
741 | }); | |
742 | } | |
743 | ||
744 | sub vdisk_free { | |
745 | my ($cfg, $volid) = @_; | |
746 | ||
747 | my ($storeid, $volname) = parse_volume_id($volid); | |
748 | my $scfg = storage_config($cfg, $storeid); | |
749 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
750 | ||
751 | activate_storage($cfg, $storeid); | |
752 | ||
753 | my $cleanup_worker; | |
754 | ||
755 | # lock shared storage | |
756 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
757 | # LVM-thin allows deletion of still referenced base volumes! | |
758 | die "base volume '$volname' is still in use by linked clones\n" | |
759 | if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname); | |
760 | ||
761 | my (undef, undef, undef, undef, undef, $isBase, $format) = | |
762 | $plugin->parse_volname($volname); | |
763 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); | |
764 | }); | |
765 | ||
766 | return if !$cleanup_worker; | |
767 | ||
768 | my $rpcenv = PVE::RPCEnvironment::get(); | |
769 | my $authuser = $rpcenv->get_user(); | |
770 | ||
771 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); | |
772 | } | |
773 | ||
774 | sub vdisk_list { | |
775 | my ($cfg, $storeid, $vmid, $vollist) = @_; | |
776 | ||
777 | my $ids = $cfg->{ids}; | |
778 | ||
779 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
780 | ||
781 | my $res = {}; | |
782 | ||
783 | # prepare/activate/refresh all storages | |
784 | ||
785 | my $storage_list = []; | |
786 | if ($vollist) { | |
787 | foreach my $volid (@$vollist) { | |
788 | my ($sid, undef) = parse_volume_id($volid); | |
789 | next if !defined($ids->{$sid}); | |
790 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
791 | push @$storage_list, $sid; | |
792 | } | |
793 | } else { | |
794 | foreach my $sid (keys %$ids) { | |
795 | next if $storeid && $storeid ne $sid; | |
796 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
797 | push @$storage_list, $sid; | |
798 | } | |
799 | } | |
800 | ||
801 | my $cache = {}; | |
802 | ||
803 | activate_storage_list($cfg, $storage_list, $cache); | |
804 | ||
805 | foreach my $sid (keys %$ids) { | |
806 | next if $storeid && $storeid ne $sid; | |
807 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
808 | ||
809 | my $scfg = $ids->{$sid}; | |
810 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
811 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
812 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
813 | } | |
814 | ||
815 | return $res; | |
816 | } | |
817 | ||
818 | sub template_list { | |
819 | my ($cfg, $storeid, $tt) = @_; | |
820 | ||
821 | die "unknown template type '$tt'\n" | |
822 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets'); | |
823 | ||
824 | my $ids = $cfg->{ids}; | |
825 | ||
826 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
827 | ||
828 | my $res = {}; | |
829 | ||
830 | # query the storage | |
831 | foreach my $sid (keys %$ids) { | |
832 | next if $storeid && $storeid ne $sid; | |
833 | ||
834 | my $scfg = $ids->{$sid}; | |
835 | my $type = $scfg->{type}; | |
836 | ||
837 | next if !$scfg->{content}->{$tt}; | |
838 | ||
839 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
840 | ||
841 | $res->{$sid} = volume_list($cfg, $sid, undef, $tt); | |
842 | } | |
843 | ||
844 | return $res; | |
845 | } | |
846 | ||
847 | sub volume_list { | |
848 | my ($cfg, $storeid, $vmid, $content) = @_; | |
849 | ||
850 | my @ctypes = qw(rootdir images vztmpl iso backup snippets); | |
851 | ||
852 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
853 | ||
854 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
855 | ||
856 | $cts = [ grep { defined($scfg->{content}->{$_}) } @$cts ]; | |
857 | ||
858 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
859 | ||
860 | activate_storage($cfg, $storeid); | |
861 | ||
862 | my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts); | |
863 | ||
864 | @$res = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @$res; | |
865 | ||
866 | return $res; | |
867 | } | |
868 | ||
869 | sub uevent_seqnum { | |
870 | ||
871 | my $filename = "/sys/kernel/uevent_seqnum"; | |
872 | ||
873 | my $seqnum = 0; | |
874 | if (my $fh = IO::File->new($filename, "r")) { | |
875 | my $line = <$fh>; | |
876 | if ($line =~ m/^(\d+)$/) { | |
877 | $seqnum = int($1); | |
878 | } | |
879 | close ($fh); | |
880 | } | |
881 | return $seqnum; | |
882 | } | |
883 | ||
884 | sub activate_storage { | |
885 | my ($cfg, $storeid, $cache) = @_; | |
886 | ||
887 | $cache = {} if !$cache; | |
888 | ||
889 | my $scfg = storage_check_enabled($cfg, $storeid); | |
890 | ||
891 | return if $cache->{activated}->{$storeid}; | |
892 | ||
893 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; | |
894 | ||
895 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
896 | ||
897 | if ($scfg->{base}) { | |
898 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
899 | activate_storage($cfg, $baseid, $cache); | |
900 | } | |
901 | ||
902 | if (!$plugin->check_connection($storeid, $scfg)) { | |
903 | die "storage '$storeid' is not online\n"; | |
904 | } | |
905 | ||
906 | $plugin->activate_storage($storeid, $scfg, $cache); | |
907 | ||
908 | my $newseq = uevent_seqnum (); | |
909 | ||
910 | # only call udevsettle if there are events | |
911 | if ($newseq > $cache->{uevent_seqnum}) { | |
912 | my $timeout = 30; | |
913 | system ("$UDEVADM settle --timeout=$timeout"); # ignore errors | |
914 | $cache->{uevent_seqnum} = $newseq; | |
915 | } | |
916 | ||
917 | $cache->{activated}->{$storeid} = 1; | |
918 | } | |
919 | ||
920 | sub activate_storage_list { | |
921 | my ($cfg, $storeid_list, $cache) = @_; | |
922 | ||
923 | $cache = {} if !$cache; | |
924 | ||
925 | foreach my $storeid (@$storeid_list) { | |
926 | activate_storage($cfg, $storeid, $cache); | |
927 | } | |
928 | } | |
929 | ||
930 | sub deactivate_storage { | |
931 | my ($cfg, $storeid) = @_; | |
932 | ||
933 | my $scfg = storage_config ($cfg, $storeid); | |
934 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
935 | ||
936 | my $cache = {}; | |
937 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
938 | } | |
939 | ||
940 | sub activate_volumes { | |
941 | my ($cfg, $vollist, $snapname) = @_; | |
942 | ||
943 | return if !($vollist && scalar(@$vollist)); | |
944 | ||
945 | my $storagehash = {}; | |
946 | foreach my $volid (@$vollist) { | |
947 | my ($storeid, undef) = parse_volume_id($volid); | |
948 | $storagehash->{$storeid} = 1; | |
949 | } | |
950 | ||
951 | my $cache = {}; | |
952 | ||
953 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
954 | ||
955 | foreach my $volid (@$vollist) { | |
956 | my ($storeid, $volname) = parse_volume_id($volid); | |
957 | my $scfg = storage_config($cfg, $storeid); | |
958 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
959 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); | |
960 | } | |
961 | } | |
962 | ||
963 | sub deactivate_volumes { | |
964 | my ($cfg, $vollist, $snapname) = @_; | |
965 | ||
966 | return if !($vollist && scalar(@$vollist)); | |
967 | ||
968 | my $cache = {}; | |
969 | ||
970 | my @errlist = (); | |
971 | foreach my $volid (@$vollist) { | |
972 | my ($storeid, $volname) = parse_volume_id($volid); | |
973 | ||
974 | my $scfg = storage_config($cfg, $storeid); | |
975 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
976 | ||
977 | eval { | |
978 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); | |
979 | }; | |
980 | if (my $err = $@) { | |
981 | warn $err; | |
982 | push @errlist, $volid; | |
983 | } | |
984 | } | |
985 | ||
986 | die "volume deactivation failed: " . join(' ', @errlist) | |
987 | if scalar(@errlist); | |
988 | } | |
989 | ||
990 | sub storage_info { | |
991 | my ($cfg, $content, $includeformat) = @_; | |
992 | ||
993 | my $ids = $cfg->{ids}; | |
994 | ||
995 | my $info = {}; | |
996 | ||
997 | my @ctypes = PVE::Tools::split_list($content); | |
998 | ||
999 | my $slist = []; | |
1000 | foreach my $storeid (keys %$ids) { | |
1001 | my $storage_enabled = defined(storage_check_enabled($cfg, $storeid, undef, 1)); | |
1002 | ||
1003 | if (defined($content)) { | |
1004 | my $want_ctype = 0; | |
1005 | foreach my $ctype (@ctypes) { | |
1006 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
1007 | $want_ctype = 1; | |
1008 | last; | |
1009 | } | |
1010 | } | |
1011 | next if !$want_ctype || !$storage_enabled; | |
1012 | } | |
1013 | ||
1014 | my $type = $ids->{$storeid}->{type}; | |
1015 | ||
1016 | $info->{$storeid} = { | |
1017 | type => $type, | |
1018 | total => 0, | |
1019 | avail => 0, | |
1020 | used => 0, | |
1021 | shared => $ids->{$storeid}->{shared} ? 1 : 0, | |
1022 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), | |
1023 | active => 0, | |
1024 | enabled => $storage_enabled ? 1 : 0, | |
1025 | }; | |
1026 | ||
1027 | push @$slist, $storeid; | |
1028 | } | |
1029 | ||
1030 | my $cache = {}; | |
1031 | ||
1032 | foreach my $storeid (keys %$ids) { | |
1033 | my $scfg = $ids->{$storeid}; | |
1034 | ||
1035 | next if !$info->{$storeid}; | |
1036 | next if !$info->{$storeid}->{enabled}; | |
1037 | ||
1038 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1039 | if ($includeformat) { | |
1040 | my $pd = $plugin->plugindata(); | |
1041 | $info->{$storeid}->{format} = $pd->{format} | |
1042 | if $pd->{format}; | |
1043 | $info->{$storeid}->{select_existing} = $pd->{select_existing} | |
1044 | if $pd->{select_existing}; | |
1045 | } | |
1046 | ||
1047 | eval { activate_storage($cfg, $storeid, $cache); }; | |
1048 | if (my $err = $@) { | |
1049 | warn $err; | |
1050 | next; | |
1051 | } | |
1052 | ||
1053 | my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); }; | |
1054 | warn $@ if $@; | |
1055 | next if !$active; | |
1056 | $info->{$storeid}->{total} = int($total); | |
1057 | $info->{$storeid}->{avail} = int($avail); | |
1058 | $info->{$storeid}->{used} = int($used); | |
1059 | $info->{$storeid}->{active} = $active; | |
1060 | } | |
1061 | ||
1062 | return $info; | |
1063 | } | |
1064 | ||
1065 | sub resolv_server { | |
1066 | my ($server) = @_; | |
1067 | ||
1068 | my ($packed_ip, $family); | |
1069 | eval { | |
1070 | my @res = PVE::Tools::getaddrinfo_all($server); | |
1071 | $family = $res[0]->{family}; | |
1072 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1073 | }; | |
1074 | if (defined $packed_ip) { | |
1075 | return Socket::inet_ntop($family, $packed_ip); | |
1076 | } | |
1077 | return undef; | |
1078 | } | |
1079 | ||
1080 | sub scan_nfs { | |
1081 | my ($server_in) = @_; | |
1082 | ||
1083 | my $server; | |
1084 | if (!($server = resolv_server ($server_in))) { | |
1085 | die "unable to resolve address for server '${server_in}'\n"; | |
1086 | } | |
1087 | ||
1088 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1089 | ||
1090 | my $res = {}; | |
1091 | run_command($cmd, outfunc => sub { | |
1092 | my $line = shift; | |
1093 | ||
1094 | # note: howto handle white spaces in export path?? | |
1095 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1096 | $res->{$1} = $2; | |
1097 | } | |
1098 | }); | |
1099 | ||
1100 | return $res; | |
1101 | } | |
1102 | ||
1103 | sub scan_cifs { | |
1104 | my ($server_in, $user, $password, $domain) = @_; | |
1105 | ||
1106 | my $server; | |
1107 | if (!($server = resolv_server ($server_in))) { | |
1108 | die "unable to resolve address for server '${server_in}'\n"; | |
1109 | } | |
1110 | ||
1111 | # we support only Windows grater than 2012 cifsscan so use smb3 | |
1112 | my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server]; | |
1113 | if (defined($user)) { | |
1114 | die "password is required" if !defined($password); | |
1115 | push @$cmd, '-U', "$user\%$password"; | |
1116 | push @$cmd, '-W', $domain if defined($domain); | |
1117 | } else { | |
1118 | push @$cmd, '-N'; | |
1119 | } | |
1120 | ||
1121 | my $res = {}; | |
1122 | run_command($cmd, | |
1123 | outfunc => sub { | |
1124 | my $line = shift; | |
1125 | if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) { | |
1126 | $res->{$1} = $2; | |
1127 | } elsif ($line =~ m/(NT_STATUS_(\S*))/) { | |
1128 | $res->{$1} = ''; | |
1129 | } | |
1130 | }, | |
1131 | errfunc => sub {}, | |
1132 | noerr => 1 | |
1133 | ); | |
1134 | ||
1135 | return $res; | |
1136 | } | |
1137 | ||
1138 | sub scan_zfs { | |
1139 | ||
1140 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used']; | |
1141 | ||
1142 | my $res = []; | |
1143 | run_command($cmd, outfunc => sub { | |
1144 | my $line = shift; | |
1145 | ||
1146 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
1147 | my ($pool, $size_str, $used_str) = ($1, $2, $3); | |
1148 | my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str); | |
1149 | my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str); | |
1150 | # ignore subvolumes generated by our ZFSPoolPlugin | |
1151 | return if $pool =~ m!/subvol-\d+-[^/]+$!; | |
1152 | return if $pool =~ m!/basevol-\d+-[^/]+$!; | |
1153 | push @$res, { pool => $pool, size => $size, free => $size-$used }; | |
1154 | } | |
1155 | }); | |
1156 | ||
1157 | return $res; | |
1158 | } | |
1159 | ||
1160 | sub resolv_portal { | |
1161 | my ($portal, $noerr) = @_; | |
1162 | ||
1163 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); | |
1164 | if ($server) { | |
1165 | if (my $ip = resolv_server($server)) { | |
1166 | $server = $ip; | |
1167 | $server = "[$server]" if $server =~ /^$IPV6RE$/; | |
1168 | return $port ? "$server:$port" : $server; | |
1169 | } | |
1170 | } | |
1171 | return undef if $noerr; | |
1172 | ||
1173 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1174 | } | |
1175 | ||
1176 | ||
1177 | sub scan_iscsi { | |
1178 | my ($portal_in) = @_; | |
1179 | ||
1180 | my $portal; | |
1181 | if (!($portal = resolv_portal($portal_in))) { | |
1182 | die "unable to parse/resolve portal address '${portal_in}'\n"; | |
1183 | } | |
1184 | ||
1185 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); | |
1186 | } | |
1187 | ||
1188 | sub storage_default_format { | |
1189 | my ($cfg, $storeid) = @_; | |
1190 | ||
1191 | my $scfg = storage_config ($cfg, $storeid); | |
1192 | ||
1193 | return PVE::Storage::Plugin::default_format($scfg); | |
1194 | } | |
1195 | ||
1196 | sub vgroup_is_used { | |
1197 | my ($cfg, $vgname) = @_; | |
1198 | ||
1199 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1200 | my $scfg = storage_config($cfg, $storeid); | |
1201 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { | |
1202 | return 1; | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | return undef; | |
1207 | } | |
1208 | ||
1209 | sub target_is_used { | |
1210 | my ($cfg, $target) = @_; | |
1211 | ||
1212 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1213 | my $scfg = storage_config($cfg, $storeid); | |
1214 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { | |
1215 | return 1; | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | return undef; | |
1220 | } | |
1221 | ||
1222 | sub volume_is_used { | |
1223 | my ($cfg, $volid) = @_; | |
1224 | ||
1225 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1226 | my $scfg = storage_config($cfg, $storeid); | |
1227 | if ($scfg->{base} && $scfg->{base} eq $volid) { | |
1228 | return 1; | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | return undef; | |
1233 | } | |
1234 | ||
1235 | sub storage_is_used { | |
1236 | my ($cfg, $storeid) = @_; | |
1237 | ||
1238 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1239 | my $scfg = storage_config($cfg, $sid); | |
1240 | next if !$scfg->{base}; | |
1241 | my ($st) = parse_volume_id($scfg->{base}); | |
1242 | return 1 if $st && $st eq $storeid; | |
1243 | } | |
1244 | ||
1245 | return undef; | |
1246 | } | |
1247 | ||
1248 | sub foreach_volid { | |
1249 | my ($list, $func) = @_; | |
1250 | ||
1251 | return if !$list; | |
1252 | ||
1253 | foreach my $sid (keys %$list) { | |
1254 | foreach my $info (@{$list->{$sid}}) { | |
1255 | my $volid = $info->{volid}; | |
1256 | my ($sid1, $volname) = parse_volume_id($volid, 1); | |
1257 | if ($sid1 && $sid1 eq $sid) { | |
1258 | &$func ($volid, $sid, $info); | |
1259 | } else { | |
1260 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1261 | } | |
1262 | } | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | sub extract_vzdump_config_tar { | |
1267 | my ($archive, $conf_re) = @_; | |
1268 | ||
1269 | die "ERROR: file '$archive' does not exist\n" if ! -f $archive; | |
1270 | ||
1271 | my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) || | |
1272 | die "unable to open file '$archive'\n"; | |
1273 | ||
1274 | my $file; | |
1275 | while (defined($file = <$fh>)) { | |
1276 | if ($file =~ $conf_re) { | |
1277 | $file = $1; # untaint | |
1278 | last; | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | kill 15, $pid; | |
1283 | waitpid $pid, 0; | |
1284 | close $fh; | |
1285 | ||
1286 | die "ERROR: archive contains no configuration file\n" if !$file; | |
1287 | chomp $file; | |
1288 | ||
1289 | my $raw = ''; | |
1290 | my $out = sub { | |
1291 | my $output = shift; | |
1292 | $raw .= "$output\n"; | |
1293 | }; | |
1294 | ||
1295 | PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out); | |
1296 | ||
1297 | return wantarray ? ($raw, $file) : $raw; | |
1298 | } | |
1299 | ||
1300 | sub extract_vzdump_config_vma { | |
1301 | my ($archive, $comp) = @_; | |
1302 | ||
1303 | my $cmd; | |
1304 | my $raw = ''; | |
1305 | my $out = sub { | |
1306 | my $output = shift; | |
1307 | $raw .= "$output\n"; | |
1308 | }; | |
1309 | ||
1310 | ||
1311 | if ($comp) { | |
1312 | my $uncomp; | |
1313 | if ($comp eq 'gz') { | |
1314 | $uncomp = ["zcat", $archive]; | |
1315 | } elsif ($comp eq 'lzo') { | |
1316 | $uncomp = ["lzop", "-d", "-c", $archive]; | |
1317 | } else { | |
1318 | die "unknown compression method '$comp'\n"; | |
1319 | } | |
1320 | $cmd = [$uncomp, ["vma", "config", "-"]]; | |
1321 | ||
1322 | # in some cases, lzop/zcat exits with 1 when its stdout pipe is | |
1323 | # closed early by vma, detect this and ignore the exit code later | |
1324 | my $broken_pipe; | |
1325 | my $errstring; | |
1326 | my $err = sub { | |
1327 | my $output = shift; | |
1328 | if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) { | |
1329 | $broken_pipe = 1; | |
1330 | } elsif (!defined ($errstring) && $output !~ m/^\s*$/) { | |
1331 | $errstring = "Failed to extract config from VMA archive: $output\n"; | |
1332 | } | |
1333 | }; | |
1334 | ||
1335 | # in other cases, the pipeline will exit with exit code 141 | |
1336 | # because of the broken pipe, handle / ignore this as well | |
1337 | my $rc; | |
1338 | eval { | |
1339 | $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1); | |
1340 | }; | |
1341 | my $rerr = $@; | |
1342 | ||
1343 | # use exit code if no stderr output and not just broken pipe | |
1344 | if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) { | |
1345 | die "$rerr\n" if $rerr; | |
1346 | die "config extraction failed with exit code $rc\n"; | |
1347 | } | |
1348 | die "$errstring\n" if $errstring; | |
1349 | } else { | |
1350 | # simple case without compression and weird piping behaviour | |
1351 | PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out); | |
1352 | } | |
1353 | ||
1354 | return wantarray ? ($raw, undef) : $raw; | |
1355 | } | |
1356 | ||
1357 | sub extract_vzdump_config { | |
1358 | my ($cfg, $volid) = @_; | |
1359 | ||
1360 | my $archive = abs_filesystem_path($cfg, $volid); | |
1361 | ||
1362 | if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) { | |
1363 | return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!); | |
1364 | } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) { | |
1365 | my $format; | |
1366 | my $comp; | |
1367 | if ($7 eq 'tgz') { | |
1368 | $format = 'tar'; | |
1369 | $comp = 'gz'; | |
1370 | } else { | |
1371 | $format = $9; | |
1372 | $comp = $11 if defined($11); | |
1373 | } | |
1374 | ||
1375 | if ($format eq 'tar') { | |
1376 | return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!); | |
1377 | } else { | |
1378 | return extract_vzdump_config_vma($archive, $comp); | |
1379 | } | |
1380 | } else { | |
1381 | die "cannot determine backup guest type for backup archive '$volid'\n"; | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | sub volume_export { | |
1386 | my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1387 | ||
1388 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1389 | die "cannot export volume '$volid'\n" if !$storeid; | |
1390 | my $scfg = storage_config($cfg, $storeid); | |
1391 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1392 | return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format, | |
1393 | $snapshot, $base_snapshot, $with_snapshots); | |
1394 | } | |
1395 | ||
1396 | sub volume_import { | |
1397 | my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_; | |
1398 | ||
1399 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1400 | die "cannot import into volume '$volid'\n" if !$storeid; | |
1401 | my $scfg = storage_config($cfg, $storeid); | |
1402 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1403 | return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format, | |
1404 | $base_snapshot, $with_snapshots); | |
1405 | } | |
1406 | ||
1407 | sub volume_export_formats { | |
1408 | my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1409 | ||
1410 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1411 | return if !$storeid; | |
1412 | my $scfg = storage_config($cfg, $storeid); | |
1413 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1414 | return $plugin->volume_export_formats($scfg, $storeid, $volname, | |
1415 | $snapshot, $base_snapshot, | |
1416 | $with_snapshots); | |
1417 | } | |
1418 | ||
1419 | sub volume_import_formats { | |
1420 | my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_; | |
1421 | ||
1422 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1423 | return if !$storeid; | |
1424 | my $scfg = storage_config($cfg, $storeid); | |
1425 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1426 | return $plugin->volume_import_formats($scfg, $storeid, $volname, | |
1427 | $base_snapshot, $with_snapshots); | |
1428 | } | |
1429 | ||
1430 | sub volume_transfer_formats { | |
1431 | my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1432 | my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots); | |
1433 | my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots); | |
1434 | my %import_hash = map { $_ => 1 } @import_formats; | |
1435 | my @common = grep { $import_hash{$_} } @export_formats; | |
1436 | return @common; | |
1437 | } | |
1438 | ||
1439 | # bash completion helper | |
1440 | ||
1441 | sub complete_storage { | |
1442 | my ($cmdname, $pname, $cvalue) = @_; | |
1443 | ||
1444 | my $cfg = PVE::Storage::config(); | |
1445 | ||
1446 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; | |
1447 | } | |
1448 | ||
1449 | sub complete_storage_enabled { | |
1450 | my ($cmdname, $pname, $cvalue) = @_; | |
1451 | ||
1452 | my $res = []; | |
1453 | ||
1454 | my $cfg = PVE::Storage::config(); | |
1455 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1456 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1457 | push @$res, $sid; | |
1458 | } | |
1459 | return $res; | |
1460 | } | |
1461 | ||
1462 | sub complete_content_type { | |
1463 | my ($cmdname, $pname, $cvalue) = @_; | |
1464 | ||
1465 | return [qw(rootdir images vztmpl iso backup snippets)]; | |
1466 | } | |
1467 | ||
1468 | sub complete_volume { | |
1469 | my ($cmdname, $pname, $cvalue) = @_; | |
1470 | ||
1471 | my $cfg = config(); | |
1472 | ||
1473 | my $storage_list = complete_storage_enabled(); | |
1474 | ||
1475 | if ($cvalue =~ m/^([^:]+):/) { | |
1476 | $storage_list = [ $1 ]; | |
1477 | } else { | |
1478 | if (scalar(@$storage_list) > 1) { | |
1479 | # only list storage IDs to avoid large listings | |
1480 | my $res = []; | |
1481 | foreach my $storeid (@$storage_list) { | |
1482 | # Hack: simply return 2 artificial values, so that | |
1483 | # completions does not finish | |
1484 | push @$res, "$storeid:volname", "$storeid:..."; | |
1485 | } | |
1486 | return $res; | |
1487 | } | |
1488 | } | |
1489 | ||
1490 | my $res = []; | |
1491 | foreach my $storeid (@$storage_list) { | |
1492 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1493 | ||
1494 | foreach my $item (@$vollist) { | |
1495 | push @$res, $item->{volid}; | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | return $res; | |
1500 | } | |
1501 | ||
1502 | # Various io-heavy operations require io/bandwidth limits which can be | |
1503 | # configured on multiple levels: The global defaults in datacenter.cfg, and | |
1504 | # per-storage overrides. When we want to do a restore from storage A to storage | |
1505 | # B, we should take the smaller limit defined for storages A and B, and if no | |
1506 | # such limit was specified, use the one from datacenter.cfg. | |
1507 | sub get_bandwidth_limit { | |
1508 | my ($operation, $storage_list, $override) = @_; | |
1509 | ||
1510 | # called for each limit (global, per-storage) with the 'default' and the | |
1511 | # $operation limit and should udpate $override for every limit affecting | |
1512 | # us. | |
1513 | my $use_global_limits = 0; | |
1514 | my $apply_limit = sub { | |
1515 | my ($bwlimit) = @_; | |
1516 | if (defined($bwlimit)) { | |
1517 | my $limits = PVE::JSONSchema::parse_property_string('bwlimit', $bwlimit); | |
1518 | my $limit = $limits->{$operation} // $limits->{default}; | |
1519 | if (defined($limit)) { | |
1520 | if (!$override || $limit < $override) { | |
1521 | $override = $limit; | |
1522 | } | |
1523 | return; | |
1524 | } | |
1525 | } | |
1526 | # If there was no applicable limit, try to apply the global ones. | |
1527 | $use_global_limits = 1; | |
1528 | }; | |
1529 | ||
1530 | my ($rpcenv, $authuser); | |
1531 | if (defined($override)) { | |
1532 | $rpcenv = PVE::RPCEnvironment->get(); | |
1533 | $authuser = $rpcenv->get_user(); | |
1534 | } | |
1535 | ||
1536 | # Apply per-storage limits - if there are storages involved. | |
1537 | if (defined($storage_list) && @$storage_list) { | |
1538 | my $config = config(); | |
1539 | ||
1540 | # The Datastore.Allocate permission allows us to modify the per-storage | |
1541 | # limits, therefore it also allows us to override them. | |
1542 | # Since we have most likely multiple storages to check, do a quick check on | |
1543 | # the general '/storage' path to see if we can skip the checks entirely: | |
1544 | return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1); | |
1545 | ||
1546 | my %done; | |
1547 | foreach my $storage (@$storage_list) { | |
1548 | next if !defined($storage); | |
1549 | # Avoid duplicate checks: | |
1550 | next if $done{$storage}; | |
1551 | $done{$storage} = 1; | |
1552 | ||
1553 | # Otherwise we may still have individual /storage/$ID permissions: | |
1554 | if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) { | |
1555 | # And if not: apply the limits. | |
1556 | my $storecfg = storage_config($config, $storage); | |
1557 | $apply_limit->($storecfg->{bwlimit}); | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | # Storage limits take precedence over the datacenter defaults, so if | |
1562 | # a limit was applied: | |
1563 | return $override if !$use_global_limits; | |
1564 | } | |
1565 | ||
1566 | # Sys.Modify on '/' means we can change datacenter.cfg which contains the | |
1567 | # global default limits. | |
1568 | if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) { | |
1569 | # So if we cannot modify global limits, apply them to our currently | |
1570 | # requested override. | |
1571 | my $dc = cfs_read_file('datacenter.cfg'); | |
1572 | $apply_limit->($dc->{bwlimit}); | |
1573 | } | |
1574 | ||
1575 | return $override; | |
1576 | } | |
1577 | ||
1578 | # checks if the storage id is available and dies if not | |
1579 | sub assert_sid_unused { | |
1580 | my ($sid) = @_; | |
1581 | ||
1582 | my $cfg = config(); | |
1583 | if (my $scfg = storage_config($cfg, $sid, 1)) { | |
1584 | die "storage ID '$sid' already defined\n"; | |
1585 | } | |
1586 | ||
1587 | return undef; | |
1588 | } | |
1589 | ||
1590 | 1; |