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