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