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