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