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