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