]>
Commit | Line | Data |
---|---|---|
1 | package PVE::Storage; | |
2 | ||
3 | use strict; | |
4 | use POSIX; | |
5 | use IO::Select; | |
6 | use IO::File; | |
7 | use File::Basename; | |
8 | use File::Path; | |
9 | use Cwd 'abs_path'; | |
10 | use Socket; | |
11 | ||
12 | use PVE::Tools qw(run_command file_read_firstline); | |
13 | use PVE::Cluster qw(cfs_read_file cfs_lock_file); | |
14 | use PVE::Exception qw(raise_param_exc); | |
15 | use PVE::JSONSchema; | |
16 | use PVE::INotify; | |
17 | use PVE::RPCEnvironment; | |
18 | ||
19 | use PVE::Storage::Plugin; | |
20 | use PVE::Storage::DirPlugin; | |
21 | use PVE::Storage::LVMPlugin; | |
22 | use PVE::Storage::NFSPlugin; | |
23 | use PVE::Storage::ISCSIPlugin; | |
24 | use PVE::Storage::RBDPlugin; | |
25 | use PVE::Storage::SheepdogPlugin; | |
26 | use PVE::Storage::ISCSIDirectPlugin; | |
27 | use PVE::Storage::NexentaPlugin; | |
28 | ||
29 | # load and initialize all plugins | |
30 | PVE::Storage::DirPlugin->register(); | |
31 | PVE::Storage::LVMPlugin->register(); | |
32 | PVE::Storage::NFSPlugin->register(); | |
33 | PVE::Storage::ISCSIPlugin->register(); | |
34 | PVE::Storage::RBDPlugin->register(); | |
35 | PVE::Storage::SheepdogPlugin->register(); | |
36 | PVE::Storage::ISCSIDirectPlugin->register(); | |
37 | PVE::Storage::NexentaPlugin->register(); | |
38 | PVE::Storage::Plugin->init(); | |
39 | ||
40 | my $UDEVADM = '/sbin/udevadm'; | |
41 | ||
42 | # PVE::Storage utility functions | |
43 | ||
44 | sub config { | |
45 | return cfs_read_file("storage.cfg"); | |
46 | } | |
47 | ||
48 | sub lock_storage_config { | |
49 | my ($code, $errmsg) = @_; | |
50 | ||
51 | cfs_lock_file("storage.cfg", undef, $code); | |
52 | my $err = $@; | |
53 | if ($err) { | |
54 | $errmsg ? die "$errmsg: $err" : die $err; | |
55 | } | |
56 | } | |
57 | ||
58 | sub storage_config { | |
59 | my ($cfg, $storeid, $noerr) = @_; | |
60 | ||
61 | die "no storage id specified\n" if !$storeid; | |
62 | ||
63 | my $scfg = $cfg->{ids}->{$storeid}; | |
64 | ||
65 | die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg); | |
66 | ||
67 | return $scfg; | |
68 | } | |
69 | ||
70 | sub storage_check_node { | |
71 | my ($cfg, $storeid, $node, $noerr) = @_; | |
72 | ||
73 | my $scfg = storage_config($cfg, $storeid); | |
74 | ||
75 | if ($scfg->{nodes}) { | |
76 | $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost'); | |
77 | if (!$scfg->{nodes}->{$node}) { | |
78 | die "storage '$storeid' is not available on node '$node'\n" if !$noerr; | |
79 | return undef; | |
80 | } | |
81 | } | |
82 | ||
83 | return $scfg; | |
84 | } | |
85 | ||
86 | sub storage_check_enabled { | |
87 | my ($cfg, $storeid, $node, $noerr) = @_; | |
88 | ||
89 | my $scfg = storage_config($cfg, $storeid); | |
90 | ||
91 | if ($scfg->{disable}) { | |
92 | die "storage '$storeid' is disabled\n" if !$noerr; | |
93 | return undef; | |
94 | } | |
95 | ||
96 | return storage_check_node($cfg, $storeid, $node, $noerr); | |
97 | } | |
98 | ||
99 | sub storage_ids { | |
100 | my ($cfg) = @_; | |
101 | ||
102 | return keys %{$cfg->{ids}}; | |
103 | } | |
104 | ||
105 | sub file_size_info { | |
106 | my ($filename, $timeout) = @_; | |
107 | ||
108 | return PVE::Storage::Plugin::file_size_info($filename, $timeout); | |
109 | } | |
110 | ||
111 | sub volume_size_info { | |
112 | my ($cfg, $volid, $timeout) = @_; | |
113 | ||
114 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
115 | if ($storeid) { | |
116 | my $scfg = storage_config($cfg, $storeid); | |
117 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
118 | return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout); | |
119 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
120 | return file_size_info($volid, $timeout); | |
121 | } else { | |
122 | return 0; | |
123 | } | |
124 | } | |
125 | ||
126 | sub volume_resize { | |
127 | my ($cfg, $volid, $size, $running) = @_; | |
128 | ||
129 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
130 | if ($storeid) { | |
131 | my $scfg = storage_config($cfg, $storeid); | |
132 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
133 | return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running); | |
134 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
135 | die "resize device is not possible"; | |
136 | } else { | |
137 | die "can't resize"; | |
138 | } | |
139 | } | |
140 | ||
141 | sub volume_snapshot { | |
142 | my ($cfg, $volid, $snap, $running) = @_; | |
143 | ||
144 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
145 | if ($storeid) { | |
146 | my $scfg = storage_config($cfg, $storeid); | |
147 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
148 | return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running); | |
149 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
150 | die "snapshot device is not possible"; | |
151 | } else { | |
152 | die "can't snapshot"; | |
153 | } | |
154 | } | |
155 | ||
156 | sub volume_snapshot_rollback { | |
157 | my ($cfg, $volid, $snap) = @_; | |
158 | ||
159 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
160 | if ($storeid) { | |
161 | my $scfg = storage_config($cfg, $storeid); | |
162 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
163 | return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap); | |
164 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
165 | die "snapshot rollback device is not possible"; | |
166 | } else { | |
167 | die "can't snapshot"; | |
168 | } | |
169 | } | |
170 | ||
171 | sub volume_snapshot_delete { | |
172 | my ($cfg, $volid, $snap, $running) = @_; | |
173 | ||
174 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
175 | if ($storeid) { | |
176 | my $scfg = storage_config($cfg, $storeid); | |
177 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
178 | return $plugin->volume_snapshot_rollback_delete($scfg, $storeid, $volname, $snap, $running); | |
179 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
180 | die "snapshot delete device is not possible"; | |
181 | } else { | |
182 | die "can't delete snapshot"; | |
183 | } | |
184 | } | |
185 | ||
186 | sub get_image_dir { | |
187 | my ($cfg, $storeid, $vmid) = @_; | |
188 | ||
189 | my $scfg = storage_config($cfg, $storeid); | |
190 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
191 | ||
192 | my $path = $plugin->get_subdir($scfg, 'images'); | |
193 | ||
194 | return $vmid ? "$path/$vmid" : $path; | |
195 | } | |
196 | ||
197 | sub get_private_dir { | |
198 | my ($cfg, $storeid, $vmid) = @_; | |
199 | ||
200 | my $scfg = storage_config($cfg, $storeid); | |
201 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
202 | ||
203 | my $path = $plugin->get_subdir($scfg, 'rootdir'); | |
204 | ||
205 | return $vmid ? "$path/$vmid" : $path; | |
206 | } | |
207 | ||
208 | sub get_iso_dir { | |
209 | my ($cfg, $storeid) = @_; | |
210 | ||
211 | my $scfg = storage_config($cfg, $storeid); | |
212 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
213 | ||
214 | return $plugin->get_subdir($scfg, 'iso'); | |
215 | } | |
216 | ||
217 | sub get_vztmpl_dir { | |
218 | my ($cfg, $storeid) = @_; | |
219 | ||
220 | my $scfg = storage_config($cfg, $storeid); | |
221 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
222 | ||
223 | return $plugin->get_subdir($scfg, 'vztmpl'); | |
224 | } | |
225 | ||
226 | sub get_backup_dir { | |
227 | my ($cfg, $storeid) = @_; | |
228 | ||
229 | my $scfg = storage_config($cfg, $storeid); | |
230 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
231 | ||
232 | return $plugin->get_subdir($scfg, 'backup'); | |
233 | } | |
234 | ||
235 | # library implementation | |
236 | ||
237 | sub parse_vmid { | |
238 | my $vmid = shift; | |
239 | ||
240 | die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/; | |
241 | ||
242 | return int($vmid); | |
243 | } | |
244 | ||
245 | PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id); | |
246 | sub parse_volume_id { | |
247 | my ($volid, $noerr) = @_; | |
248 | ||
249 | if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) { | |
250 | return wantarray ? ($1, $2) : $1; | |
251 | } | |
252 | return undef if $noerr; | |
253 | die "unable to parse volume ID '$volid'\n"; | |
254 | } | |
255 | ||
256 | # try to map a filesystem path to a volume identifier | |
257 | sub path_to_volume_id { | |
258 | my ($cfg, $path) = @_; | |
259 | ||
260 | my $ids = $cfg->{ids}; | |
261 | ||
262 | my ($sid, $volname) = parse_volume_id($path, 1); | |
263 | if ($sid) { | |
264 | if (my $scfg = $ids->{$sid}) { | |
265 | if (my $path = $scfg->{path}) { | |
266 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
267 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
268 | return ($vtype, $path); | |
269 | } | |
270 | } | |
271 | return (''); | |
272 | } | |
273 | ||
274 | # Note: abs_path() return undef if $path doesn not exist | |
275 | # for example when nfs storage is not mounted | |
276 | $path = abs_path($path) || $path; | |
277 | ||
278 | foreach my $sid (keys %$ids) { | |
279 | my $scfg = $ids->{$sid}; | |
280 | next if !$scfg->{path}; | |
281 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
282 | my $imagedir = $plugin->get_subdir($scfg, 'images'); | |
283 | my $isodir = $plugin->get_subdir($scfg, 'iso'); | |
284 | my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl'); | |
285 | my $backupdir = $plugin->get_subdir($scfg, 'backup'); | |
286 | my $privatedir = $plugin->get_subdir($scfg, 'rootdir'); | |
287 | ||
288 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
289 | my $vmid = $1; | |
290 | my $name = $2; | |
291 | return ('images', "$sid:$vmid/$name"); | |
292 | } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) { | |
293 | my $name = $1; | |
294 | return ('iso', "$sid:iso/$name"); | |
295 | } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) { | |
296 | my $name = $1; | |
297 | return ('vztmpl', "$sid:vztmpl/$name"); | |
298 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { | |
299 | my $vmid = $1; | |
300 | return ('rootdir', "$sid:rootdir/$vmid"); | |
301 | } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) { | |
302 | my $name = $1; | |
303 | return ('iso', "$sid:backup/$name"); | |
304 | } | |
305 | } | |
306 | ||
307 | # can't map path to volume id | |
308 | return (''); | |
309 | } | |
310 | ||
311 | sub path { | |
312 | my ($cfg, $volid) = @_; | |
313 | ||
314 | my ($storeid, $volname) = parse_volume_id($volid); | |
315 | ||
316 | my $scfg = storage_config($cfg, $storeid); | |
317 | ||
318 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
319 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid); | |
320 | return wantarray ? ($path, $owner, $vtype) : $path; | |
321 | } | |
322 | ||
323 | sub storage_migrate { | |
324 | my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_; | |
325 | ||
326 | my ($storeid, $volname) = parse_volume_id($volid); | |
327 | $target_volname = $volname if !$target_volname; | |
328 | ||
329 | my $scfg = storage_config($cfg, $storeid); | |
330 | ||
331 | # no need to migrate shared content | |
332 | return if $storeid eq $target_storeid && $scfg->{shared}; | |
333 | ||
334 | my $tcfg = storage_config($cfg, $target_storeid); | |
335 | ||
336 | my $target_volid = "${target_storeid}:${target_volname}"; | |
337 | ||
338 | my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'"; | |
339 | ||
340 | # blowfish is a fast block cipher, much faster then 3des | |
341 | my $sshoptions = "-c blowfish -o 'BatchMode=yes'"; | |
342 | my $ssh = "/usr/bin/ssh $sshoptions"; | |
343 | ||
344 | local $ENV{RSYNC_RSH} = $ssh; | |
345 | ||
346 | # only implemented for file system based storage | |
347 | if ($scfg->{path}) { | |
348 | if ($tcfg->{path}) { | |
349 | ||
350 | my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
351 | my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type}); | |
352 | my $src = $src_plugin->path($scfg, $volname, $storeid); | |
353 | my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid); | |
354 | ||
355 | my $dirname = dirname($dst); | |
356 | ||
357 | if ($tcfg->{shared}) { # we can do a local copy | |
358 | ||
359 | run_command(['/bin/mkdir', '-p', $dirname]); | |
360 | ||
361 | run_command(['/bin/cp', $src, $dst]); | |
362 | ||
363 | } else { | |
364 | ||
365 | run_command(['/usr/bin/ssh', "root\@${target_host}", | |
366 | '/bin/mkdir', '-p', $dirname]); | |
367 | ||
368 | # we use rsync with --sparse, so we can't use --inplace, | |
369 | # so we remove file on the target if it already exists to | |
370 | # save space | |
371 | my ($size, $format) = PVE::Storage::Plugin::file_size_info($src); | |
372 | if ($format && ($format eq 'raw') && $size) { | |
373 | run_command(['/usr/bin/ssh', "root\@${target_host}", | |
374 | 'rm', '-f', $dst], | |
375 | outfunc => sub {}); | |
376 | } | |
377 | ||
378 | my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file', | |
379 | $src, "root\@${target_host}:$dst"]; | |
380 | ||
381 | my $percent = -1; | |
382 | ||
383 | run_command($cmd, outfunc => sub { | |
384 | my $line = shift; | |
385 | ||
386 | if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) { | |
387 | if ($2 > $percent) { | |
388 | $percent = $2; | |
389 | print "rsync status: $1\n"; | |
390 | *STDOUT->flush(); | |
391 | } | |
392 | } else { | |
393 | print "$line\n"; | |
394 | *STDOUT->flush(); | |
395 | } | |
396 | }); | |
397 | } | |
398 | } else { | |
399 | die "$errstr - target type '$tcfg->{type}' not implemented\n"; | |
400 | } | |
401 | } else { | |
402 | die "$errstr - source type '$scfg->{type}' not implemented\n"; | |
403 | } | |
404 | } | |
405 | ||
406 | sub vdisk_alloc { | |
407 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
408 | ||
409 | die "no storage id specified\n" if !$storeid; | |
410 | ||
411 | PVE::JSONSchema::parse_storage_id($storeid); | |
412 | ||
413 | my $scfg = storage_config($cfg, $storeid); | |
414 | ||
415 | die "no VMID specified\n" if !$vmid; | |
416 | ||
417 | $vmid = parse_vmid($vmid); | |
418 | ||
419 | my $defformat = PVE::Storage::Plugin::default_format($scfg); | |
420 | ||
421 | $fmt = $defformat if !$fmt; | |
422 | ||
423 | activate_storage($cfg, $storeid); | |
424 | ||
425 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
426 | ||
427 | # lock shared storage | |
428 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
429 | my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size); | |
430 | return "$storeid:$volname"; | |
431 | }); | |
432 | } | |
433 | ||
434 | sub vdisk_free { | |
435 | my ($cfg, $volid) = @_; | |
436 | ||
437 | my ($storeid, $volname) = parse_volume_id($volid); | |
438 | ||
439 | my $scfg = storage_config($cfg, $storeid); | |
440 | ||
441 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
442 | ||
443 | activate_storage($cfg, $storeid); | |
444 | ||
445 | my $cleanup_worker; | |
446 | ||
447 | # lock shared storage | |
448 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
449 | my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname); | |
450 | }); | |
451 | ||
452 | return if !$cleanup_worker; | |
453 | ||
454 | my $rpcenv = PVE::RPCEnvironment::get(); | |
455 | my $authuser = $rpcenv->get_user(); | |
456 | ||
457 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); | |
458 | } | |
459 | ||
460 | #list iso or openvz template ($tt = <iso|vztmpl|backup>) | |
461 | sub template_list { | |
462 | my ($cfg, $storeid, $tt) = @_; | |
463 | ||
464 | die "unknown template type '$tt'\n" | |
465 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup'); | |
466 | ||
467 | my $ids = $cfg->{ids}; | |
468 | ||
469 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
470 | ||
471 | my $res = {}; | |
472 | ||
473 | # query the storage | |
474 | ||
475 | foreach my $sid (keys %$ids) { | |
476 | next if $storeid && $storeid ne $sid; | |
477 | ||
478 | my $scfg = $ids->{$sid}; | |
479 | my $type = $scfg->{type}; | |
480 | ||
481 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
482 | ||
483 | next if $tt eq 'iso' && !$scfg->{content}->{iso}; | |
484 | next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl}; | |
485 | next if $tt eq 'backup' && !$scfg->{content}->{backup}; | |
486 | ||
487 | activate_storage($cfg, $sid); | |
488 | ||
489 | if ($scfg->{path}) { | |
490 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
491 | ||
492 | my $path = $plugin->get_subdir($scfg, $tt); | |
493 | ||
494 | foreach my $fn (<$path/*>) { | |
495 | ||
496 | my $info; | |
497 | ||
498 | if ($tt eq 'iso') { | |
499 | next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!; | |
500 | ||
501 | $info = { volid => "$sid:iso/$1", format => 'iso' }; | |
502 | ||
503 | } elsif ($tt eq 'vztmpl') { | |
504 | next if $fn !~ m!/([^/]+\.tar\.gz)$!; | |
505 | ||
506 | $info = { volid => "$sid:vztmpl/$1", format => 'tgz' }; | |
507 | ||
508 | } elsif ($tt eq 'backup') { | |
509 | next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!; | |
510 | ||
511 | $info = { volid => "$sid:backup/$1", format => $2 }; | |
512 | } | |
513 | ||
514 | $info->{size} = -s $fn; | |
515 | ||
516 | push @{$res->{$sid}}, $info; | |
517 | } | |
518 | ||
519 | } | |
520 | ||
521 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
522 | } | |
523 | ||
524 | return $res; | |
525 | } | |
526 | ||
527 | ||
528 | sub vdisk_list { | |
529 | my ($cfg, $storeid, $vmid, $vollist) = @_; | |
530 | ||
531 | my $ids = $cfg->{ids}; | |
532 | ||
533 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
534 | ||
535 | my $res = {}; | |
536 | ||
537 | # prepare/activate/refresh all storages | |
538 | ||
539 | my $storage_list = []; | |
540 | if ($vollist) { | |
541 | foreach my $volid (@$vollist) { | |
542 | my ($sid, undef) = parse_volume_id($volid); | |
543 | next if !defined($ids->{$sid}); | |
544 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
545 | push @$storage_list, $sid; | |
546 | } | |
547 | } else { | |
548 | foreach my $sid (keys %$ids) { | |
549 | next if $storeid && $storeid ne $sid; | |
550 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
551 | push @$storage_list, $sid; | |
552 | } | |
553 | } | |
554 | ||
555 | my $cache = {}; | |
556 | ||
557 | activate_storage_list($cfg, $storage_list, $cache); | |
558 | ||
559 | foreach my $sid (keys %$ids) { | |
560 | next if $storeid && $storeid ne $sid; | |
561 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
562 | ||
563 | my $scfg = $ids->{$sid}; | |
564 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
565 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
566 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
567 | } | |
568 | ||
569 | return $res; | |
570 | } | |
571 | ||
572 | sub uevent_seqnum { | |
573 | ||
574 | my $filename = "/sys/kernel/uevent_seqnum"; | |
575 | ||
576 | my $seqnum = 0; | |
577 | if (my $fh = IO::File->new($filename, "r")) { | |
578 | my $line = <$fh>; | |
579 | if ($line =~ m/^(\d+)$/) { | |
580 | $seqnum = int($1); | |
581 | } | |
582 | close ($fh); | |
583 | } | |
584 | return $seqnum; | |
585 | } | |
586 | ||
587 | sub activate_storage { | |
588 | my ($cfg, $storeid, $cache) = @_; | |
589 | ||
590 | $cache = {} if !$cache; | |
591 | ||
592 | my $scfg = storage_check_enabled($cfg, $storeid); | |
593 | ||
594 | return if $cache->{activated}->{$storeid}; | |
595 | ||
596 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; | |
597 | ||
598 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
599 | ||
600 | if ($scfg->{base}) { | |
601 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
602 | activate_storage($cfg, $baseid, $cache); | |
603 | } | |
604 | ||
605 | if (!$plugin->check_connection($storeid, $scfg)) { | |
606 | die "storage '$storeid' is not online\n"; | |
607 | } | |
608 | ||
609 | $plugin->activate_storage($storeid, $scfg, $cache); | |
610 | ||
611 | my $newseq = uevent_seqnum (); | |
612 | ||
613 | # only call udevsettle if there are events | |
614 | if ($newseq > $cache->{uevent_seqnum}) { | |
615 | my $timeout = 30; | |
616 | system ("$UDEVADM settle --timeout=$timeout"); # ignore errors | |
617 | $cache->{uevent_seqnum} = $newseq; | |
618 | } | |
619 | ||
620 | $cache->{activated}->{$storeid} = 1; | |
621 | } | |
622 | ||
623 | sub activate_storage_list { | |
624 | my ($cfg, $storeid_list, $cache) = @_; | |
625 | ||
626 | $cache = {} if !$cache; | |
627 | ||
628 | foreach my $storeid (@$storeid_list) { | |
629 | activate_storage($cfg, $storeid, $cache); | |
630 | } | |
631 | } | |
632 | ||
633 | sub deactivate_storage { | |
634 | my ($cfg, $storeid) = @_; | |
635 | ||
636 | my $scfg = storage_config ($cfg, $storeid); | |
637 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
638 | ||
639 | my $cache = {}; | |
640 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
641 | } | |
642 | ||
643 | sub activate_volumes { | |
644 | my ($cfg, $vollist, $exclusive) = @_; | |
645 | ||
646 | return if !($vollist && scalar(@$vollist)); | |
647 | ||
648 | my $storagehash = {}; | |
649 | foreach my $volid (@$vollist) { | |
650 | my ($storeid, undef) = parse_volume_id($volid); | |
651 | $storagehash->{$storeid} = 1; | |
652 | } | |
653 | ||
654 | my $cache = {}; | |
655 | ||
656 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
657 | ||
658 | foreach my $volid (@$vollist) { | |
659 | my ($storeid, $volname) = parse_volume_id($volid); | |
660 | my $scfg = storage_config($cfg, $storeid); | |
661 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
662 | $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache); | |
663 | } | |
664 | } | |
665 | ||
666 | sub deactivate_volumes { | |
667 | my ($cfg, $vollist) = @_; | |
668 | ||
669 | return if !($vollist && scalar(@$vollist)); | |
670 | ||
671 | my $cache = {}; | |
672 | ||
673 | my @errlist = (); | |
674 | foreach my $volid (@$vollist) { | |
675 | my ($storeid, $volname) = parse_volume_id($volid); | |
676 | ||
677 | my $scfg = storage_config($cfg, $storeid); | |
678 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
679 | ||
680 | eval { | |
681 | $plugin->deactivate_volume($storeid, $scfg, $volname, $cache); | |
682 | }; | |
683 | if (my $err = $@) { | |
684 | warn $err; | |
685 | push @errlist, $volid; | |
686 | } | |
687 | } | |
688 | ||
689 | die "volume deativation failed: " . join(' ', @errlist) | |
690 | if scalar(@errlist); | |
691 | } | |
692 | ||
693 | sub storage_info { | |
694 | my ($cfg, $content) = @_; | |
695 | ||
696 | my $ids = $cfg->{ids}; | |
697 | ||
698 | my $info = {}; | |
699 | ||
700 | my $slist = []; | |
701 | foreach my $storeid (keys %$ids) { | |
702 | ||
703 | next if $content && !$ids->{$storeid}->{content}->{$content}; | |
704 | ||
705 | next if !storage_check_enabled($cfg, $storeid, undef, 1); | |
706 | ||
707 | my $type = $ids->{$storeid}->{type}; | |
708 | ||
709 | $info->{$storeid} = { | |
710 | type => $type, | |
711 | total => 0, | |
712 | avail => 0, | |
713 | used => 0, | |
714 | shared => $ids->{$storeid}->{shared} ? 1 : 0, | |
715 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), | |
716 | active => 0, | |
717 | }; | |
718 | ||
719 | push @$slist, $storeid; | |
720 | } | |
721 | ||
722 | my $cache = {}; | |
723 | ||
724 | foreach my $storeid (keys %$ids) { | |
725 | my $scfg = $ids->{$storeid}; | |
726 | next if !$info->{$storeid}; | |
727 | ||
728 | eval { activate_storage($cfg, $storeid, $cache); }; | |
729 | if (my $err = $@) { | |
730 | warn $err; | |
731 | next; | |
732 | } | |
733 | ||
734 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
735 | my ($total, $avail, $used, $active); | |
736 | eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); }; | |
737 | warn $@ if $@; | |
738 | next if !$active; | |
739 | $info->{$storeid}->{total} = $total; | |
740 | $info->{$storeid}->{avail} = $avail; | |
741 | $info->{$storeid}->{used} = $used; | |
742 | $info->{$storeid}->{active} = $active; | |
743 | } | |
744 | ||
745 | return $info; | |
746 | } | |
747 | ||
748 | sub resolv_server { | |
749 | my ($server) = @_; | |
750 | ||
751 | my $packed_ip = gethostbyname($server); | |
752 | if (defined $packed_ip) { | |
753 | return inet_ntoa($packed_ip); | |
754 | } | |
755 | return undef; | |
756 | } | |
757 | ||
758 | sub scan_nfs { | |
759 | my ($server_in) = @_; | |
760 | ||
761 | my $server; | |
762 | if (!($server = resolv_server ($server_in))) { | |
763 | die "unable to resolve address for server '${server_in}'\n"; | |
764 | } | |
765 | ||
766 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
767 | ||
768 | my $res = {}; | |
769 | run_command($cmd, outfunc => sub { | |
770 | my $line = shift; | |
771 | ||
772 | # note: howto handle white spaces in export path?? | |
773 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
774 | $res->{$1} = $2; | |
775 | } | |
776 | }); | |
777 | ||
778 | return $res; | |
779 | } | |
780 | ||
781 | sub resolv_portal { | |
782 | my ($portal, $noerr) = @_; | |
783 | ||
784 | if ($portal =~ m/^([^:]+)(:(\d+))?$/) { | |
785 | my $server = $1; | |
786 | my $port = $3; | |
787 | ||
788 | if (my $ip = resolv_server($server)) { | |
789 | $server = $ip; | |
790 | return $port ? "$server:$port" : $server; | |
791 | } | |
792 | } | |
793 | return undef if $noerr; | |
794 | ||
795 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
796 | } | |
797 | ||
798 | # idea is from usbutils package (/usr/bin/usb-devices) script | |
799 | sub __scan_usb_device { | |
800 | my ($res, $devpath, $parent, $level) = @_; | |
801 | ||
802 | return if ! -d $devpath; | |
803 | return if $level && $devpath !~ m/^.*[-.](\d+)$/; | |
804 | my $port = $level ? int($1 - 1) : 0; | |
805 | ||
806 | my $busnum = int(file_read_firstline("$devpath/busnum")); | |
807 | my $devnum = int(file_read_firstline("$devpath/devnum")); | |
808 | ||
809 | my $d = { | |
810 | port => $port, | |
811 | level => $level, | |
812 | busnum => $busnum, | |
813 | devnum => $devnum, | |
814 | speed => file_read_firstline("$devpath/speed"), | |
815 | class => hex(file_read_firstline("$devpath/bDeviceClass")), | |
816 | vendid => file_read_firstline("$devpath/idVendor"), | |
817 | prodid => file_read_firstline("$devpath/idProduct"), | |
818 | }; | |
819 | ||
820 | if ($level) { | |
821 | my $usbpath = $devpath; | |
822 | $usbpath =~ s|^.*/\d+\-||; | |
823 | $d->{usbpath} = $usbpath; | |
824 | } | |
825 | ||
826 | my $product = file_read_firstline("$devpath/product"); | |
827 | $d->{product} = $product if $product; | |
828 | ||
829 | my $manu = file_read_firstline("$devpath/manufacturer"); | |
830 | $d->{manufacturer} = $manu if $manu; | |
831 | ||
832 | my $serial => file_read_firstline("$devpath/serial"); | |
833 | $d->{serial} = $serial if $serial; | |
834 | ||
835 | push @$res, $d; | |
836 | ||
837 | foreach my $subdev (<$devpath/$busnum-*>) { | |
838 | next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; | |
839 | __scan_usb_device($res, $subdev, $devnum, $level + 1); | |
840 | } | |
841 | ||
842 | }; | |
843 | ||
844 | sub scan_usb { | |
845 | ||
846 | my $devlist = []; | |
847 | ||
848 | foreach my $device (</sys/bus/usb/devices/usb*>) { | |
849 | __scan_usb_device($devlist, $device, 0, 0); | |
850 | } | |
851 | ||
852 | return $devlist; | |
853 | } | |
854 | ||
855 | sub scan_iscsi { | |
856 | my ($portal_in) = @_; | |
857 | ||
858 | my $portal; | |
859 | if (!($portal = resolv_portal($portal_in))) { | |
860 | die "unable to parse/resolve portal address '${portal_in}'\n"; | |
861 | } | |
862 | ||
863 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); | |
864 | } | |
865 | ||
866 | sub storage_default_format { | |
867 | my ($cfg, $storeid) = @_; | |
868 | ||
869 | my $scfg = storage_config ($cfg, $storeid); | |
870 | ||
871 | return PVE::Storage::Plugin::default_format($scfg); | |
872 | } | |
873 | ||
874 | sub vgroup_is_used { | |
875 | my ($cfg, $vgname) = @_; | |
876 | ||
877 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
878 | my $scfg = storage_config($cfg, $storeid); | |
879 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { | |
880 | return 1; | |
881 | } | |
882 | } | |
883 | ||
884 | return undef; | |
885 | } | |
886 | ||
887 | sub target_is_used { | |
888 | my ($cfg, $target) = @_; | |
889 | ||
890 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
891 | my $scfg = storage_config($cfg, $storeid); | |
892 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { | |
893 | return 1; | |
894 | } | |
895 | } | |
896 | ||
897 | return undef; | |
898 | } | |
899 | ||
900 | sub volume_is_used { | |
901 | my ($cfg, $volid) = @_; | |
902 | ||
903 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
904 | my $scfg = storage_config($cfg, $storeid); | |
905 | if ($scfg->{base} && $scfg->{base} eq $volid) { | |
906 | return 1; | |
907 | } | |
908 | } | |
909 | ||
910 | return undef; | |
911 | } | |
912 | ||
913 | sub storage_is_used { | |
914 | my ($cfg, $storeid) = @_; | |
915 | ||
916 | foreach my $sid (keys %{$cfg->{ids}}) { | |
917 | my $scfg = storage_config($cfg, $sid); | |
918 | next if !$scfg->{base}; | |
919 | my ($st) = parse_volume_id($scfg->{base}); | |
920 | return 1 if $st && $st eq $storeid; | |
921 | } | |
922 | ||
923 | return undef; | |
924 | } | |
925 | ||
926 | sub foreach_volid { | |
927 | my ($list, $func) = @_; | |
928 | ||
929 | return if !$list; | |
930 | ||
931 | foreach my $sid (keys %$list) { | |
932 | foreach my $info (@{$list->{$sid}}) { | |
933 | my $volid = $info->{volid}; | |
934 | my ($sid1, $volname) = parse_volume_id($volid, 1); | |
935 | if ($sid1 && $sid1 eq $sid) { | |
936 | &$func ($volid, $sid, $info); | |
937 | } else { | |
938 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
939 | } | |
940 | } | |
941 | } | |
942 | } | |
943 | ||
944 | 1; |