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