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