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