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