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