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