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