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