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