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