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