]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
380fbdf094e6eac8f0127d32ddf83a162a4743de
14 use PVE
::Tools
qw(run_command file_read_firstline);
15 use PVE
::Cluster
qw(cfs_read_file cfs_lock_file);
16 use PVE
::Exception
qw(raise_param_exc);
19 use PVE
::RPCEnvironment
;
21 use PVE
::Storage
::Plugin
;
22 use PVE
::Storage
::DirPlugin
;
23 use PVE
::Storage
::LVMPlugin
;
24 use PVE
::Storage
::NFSPlugin
;
25 use PVE
::Storage
::ISCSIPlugin
;
26 use PVE
::Storage
::RBDPlugin
;
27 use PVE
::Storage
::SheepdogPlugin
;
28 use PVE
::Storage
::ISCSIDirectPlugin
;
29 use PVE
::Storage
::GlusterfsPlugin
;
30 use PVE
::Storage
::ZFSDirPlugin
;
31 use PVE
::Storage
::ZFSPlugin
;
33 # load and initialize all plugins
34 PVE
::Storage
::DirPlugin-
>register();
35 PVE
::Storage
::LVMPlugin-
>register();
36 PVE
::Storage
::NFSPlugin-
>register();
37 PVE
::Storage
::ISCSIPlugin-
>register();
38 PVE
::Storage
::RBDPlugin-
>register();
39 PVE
::Storage
::SheepdogPlugin-
>register();
40 PVE
::Storage
::ISCSIDirectPlugin-
>register();
41 PVE
::Storage
::GlusterfsPlugin-
>register();
42 PVE
::Storage
::ZFSDirPlugin-
>register();
43 PVE
::Storage
::ZFSPlugin-
>register();
44 PVE
::Storage
::Plugin-
>init();
46 my $UDEVADM = '/sbin/udevadm';
48 # PVE::Storage utility functions
51 return cfs_read_file
("storage.cfg");
54 sub lock_storage_config
{
55 my ($code, $errmsg) = @_;
57 cfs_lock_file
("storage.cfg", undef, $code);
60 $errmsg ?
die "$errmsg: $err" : die $err;
65 my ($cfg, $storeid, $noerr) = @_;
67 die "no storage id specified\n" if !$storeid;
69 my $scfg = $cfg->{ids
}->{$storeid};
71 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
76 sub storage_check_node
{
77 my ($cfg, $storeid, $node, $noerr) = @_;
79 my $scfg = storage_config
($cfg, $storeid);
82 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
83 if (!$scfg->{nodes
}->{$node}) {
84 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
92 sub storage_check_enabled
{
93 my ($cfg, $storeid, $node, $noerr) = @_;
95 my $scfg = storage_config
($cfg, $storeid);
97 if ($scfg->{disable
}) {
98 die "storage '$storeid' is disabled\n" if !$noerr;
102 return storage_check_node
($cfg, $storeid, $node, $noerr);
108 return keys %{$cfg->{ids
}};
112 my ($filename, $timeout) = @_;
114 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
117 sub volume_size_info
{
118 my ($cfg, $volid, $timeout) = @_;
120 my ($storeid, $volname) = parse_volume_id
($volid, 1);
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);
133 my ($cfg, $volid, $size, $running) = @_;
135 my ($storeid, $volname) = parse_volume_id
($volid, 1);
137 my $scfg = storage_config
($cfg, $storeid);
138 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
139 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
140 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
141 die "resize device is not possible";
147 sub volume_snapshot
{
148 my ($cfg, $volid, $snap, $running) = @_;
150 my ($storeid, $volname) = parse_volume_id
($volid, 1);
152 my $scfg = storage_config
($cfg, $storeid);
153 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
154 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running);
155 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
156 die "snapshot device is not possible";
158 die "can't snapshot";
162 sub volume_snapshot_rollback
{
163 my ($cfg, $volid, $snap) = @_;
165 my ($storeid, $volname) = parse_volume_id
($volid, 1);
167 my $scfg = storage_config
($cfg, $storeid);
168 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
169 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
170 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
171 die "snapshot rollback device is not possible";
173 die "can't snapshot";
177 sub volume_snapshot_delete
{
178 my ($cfg, $volid, $snap, $running) = @_;
180 my ($storeid, $volname) = parse_volume_id
($volid, 1);
182 my $scfg = storage_config
($cfg, $storeid);
183 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
184 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
185 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
186 die "snapshot delete device is not possible";
188 die "can't delete snapshot";
192 sub volume_has_feature
{
193 my ($cfg, $feature, $volid, $snap, $running) = @_;
195 my ($storeid, $volname) = parse_volume_id
($volid, 1);
197 my $scfg = storage_config
($cfg, $storeid);
198 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
199 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
200 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
208 my ($cfg, $storeid, $vmid) = @_;
210 my $scfg = storage_config
($cfg, $storeid);
211 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
213 my $path = $plugin->get_subdir($scfg, 'images');
215 return $vmid ?
"$path/$vmid" : $path;
218 sub get_private_dir
{
219 my ($cfg, $storeid, $vmid) = @_;
221 my $scfg = storage_config
($cfg, $storeid);
222 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
224 my $path = $plugin->get_subdir($scfg, 'rootdir');
226 return $vmid ?
"$path/$vmid" : $path;
230 my ($cfg, $storeid) = @_;
232 my $scfg = storage_config
($cfg, $storeid);
233 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
235 return $plugin->get_subdir($scfg, 'iso');
239 my ($cfg, $storeid) = @_;
241 my $scfg = storage_config
($cfg, $storeid);
242 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
244 return $plugin->get_subdir($scfg, 'vztmpl');
248 my ($cfg, $storeid) = @_;
250 my $scfg = storage_config
($cfg, $storeid);
251 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
253 return $plugin->get_subdir($scfg, 'backup');
256 # library implementation
261 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
267 my ($cfg, $volid) = @_;
269 my ($storeid, $volname) = parse_volume_id
($volid);
271 my $scfg = storage_config
($cfg, $storeid);
273 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
274 return $plugin->parse_volname($volname);
277 sub parse_volume_id
{
278 my ($volid, $noerr) = @_;
280 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
284 my ($cfg, $volid) = @_;
286 my ($sid, $volname) = parse_volume_id
($volid, 1);
289 if (my $scfg = $cfg->{ids
}->{$sid}) {
290 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
291 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
292 $plugin->parse_volname($volname);
293 return $isBase ?
1 : 0;
295 # stale volid with undefined storage - so we can just guess
296 if ($volid =~ m/base-/) {
304 # try to map a filesystem path to a volume identifier
305 sub path_to_volume_id
{
306 my ($cfg, $path) = @_;
308 my $ids = $cfg->{ids
};
310 my ($sid, $volname) = parse_volume_id
($path, 1);
312 if (my $scfg = $ids->{$sid}) {
314 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
315 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
316 return ($vtype, $path);
322 # Note: abs_path() return undef if $path doesn not exist
323 # for example when nfs storage is not mounted
324 $path = abs_path
($path) || $path;
326 foreach my $sid (keys %$ids) {
327 my $scfg = $ids->{$sid};
328 next if !$scfg->{path
};
329 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
330 my $imagedir = $plugin->get_subdir($scfg, 'images');
331 my $isodir = $plugin->get_subdir($scfg, 'iso');
332 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
333 my $backupdir = $plugin->get_subdir($scfg, 'backup');
334 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
336 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
340 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
341 foreach my $info (@$vollist) {
342 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
343 my $volpath = $plugin->path($scfg, $volname, $storeid);
344 if ($volpath eq $path) {
345 return ('images', $info->{volid
});
348 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
350 return ('iso', "$sid:iso/$name");
351 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
353 return ('vztmpl', "$sid:vztmpl/$name");
354 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
356 return ('rootdir', "$sid:rootdir/$vmid");
357 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
359 return ('iso', "$sid:backup/$name");
363 # can't map path to volume id
368 my ($cfg, $volid, $snapname) = @_;
370 my ($storeid, $volname) = parse_volume_id
($volid);
372 my $scfg = storage_config
($cfg, $storeid);
374 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
375 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
376 return wantarray ?
($path, $owner, $vtype) : $path;
379 sub abs_filesystem_path
{
380 my ($cfg, $volid) = @_;
383 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
384 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
385 $path = PVE
::Storage
::path
($cfg, $volid);
388 my $abspath = abs_path
($volid);
389 if ($abspath && $abspath =~ m
|^(/.+)$|) {
390 $path = $1; # untaint any path
395 die "can't find file '$volid'\n" if !($path && -f
$path);
400 sub storage_migrate
{
401 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
403 my ($storeid, $volname) = parse_volume_id
($volid);
404 $target_volname = $volname if !$target_volname;
406 my $scfg = storage_config
($cfg, $storeid);
408 # no need to migrate shared content
409 return if $storeid eq $target_storeid && $scfg->{shared
};
411 my $tcfg = storage_config
($cfg, $target_storeid);
413 my $target_volid = "${target_storeid}:${target_volname}";
415 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
417 my $sshoptions = "-o 'BatchMode=yes'";
418 my $ssh = "/usr/bin/ssh $sshoptions";
420 local $ENV{RSYNC_RSH
} = $ssh;
422 # only implemented for file system based storage
426 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
427 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
428 my $src = $src_plugin->path($scfg, $volname, $storeid);
429 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
431 my $dirname = dirname
($dst);
433 if ($tcfg->{shared
}) { # we can do a local copy
435 run_command
(['/bin/mkdir', '-p', $dirname]);
437 run_command
(['/bin/cp', $src, $dst]);
441 run_command
(['/usr/bin/ssh', "root\@${target_host}",
442 '/bin/mkdir', '-p', $dirname]);
444 # we use rsync with --sparse, so we can't use --inplace,
445 # so we remove file on the target if it already exists to
447 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
448 if ($format && ($format eq 'raw') && $size) {
449 run_command
(['/usr/bin/ssh', "root\@${target_host}",
454 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
455 $src, "root\@${target_host}:$dst"];
459 run_command
($cmd, outfunc
=> sub {
462 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
465 print "rsync status: $1\n";
475 die "$errstr - target type '$tcfg->{type}' not implemented\n";
478 die "$errstr - source type '$scfg->{type}' not implemented\n";
483 my ($cfg, $volid, $vmid, $snap) = @_;
485 my ($storeid, $volname) = parse_volume_id
($volid);
487 my $scfg = storage_config
($cfg, $storeid);
489 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
491 activate_storage
($cfg, $storeid);
493 # lock shared storage
494 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
495 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
496 return "$storeid:$volname";
500 sub vdisk_create_base
{
501 my ($cfg, $volid) = @_;
503 my ($storeid, $volname) = parse_volume_id
($volid);
505 my $scfg = storage_config
($cfg, $storeid);
507 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
509 activate_storage
($cfg, $storeid);
511 # lock shared storage
512 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
513 my $volname = $plugin->create_base($storeid, $scfg, $volname);
514 return "$storeid:$volname";
519 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
521 die "no storage id specified\n" if !$storeid;
523 PVE
::JSONSchema
::parse_storage_id
($storeid);
525 my $scfg = storage_config
($cfg, $storeid);
527 die "no VMID specified\n" if !$vmid;
529 $vmid = parse_vmid
($vmid);
531 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
533 $fmt = $defformat if !$fmt;
535 activate_storage
($cfg, $storeid);
537 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
539 # lock shared storage
540 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
541 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
542 return "$storeid:$volname";
547 my ($cfg, $volid) = @_;
549 my ($storeid, $volname) = parse_volume_id
($volid);
551 my $scfg = storage_config
($cfg, $storeid);
553 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
555 activate_storage
($cfg, $storeid);
559 # lock shared storage
560 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
562 my ($vtype, $name, $vmid, undef, undef, $isBase) =
563 $plugin->parse_volname($volname);
565 my $vollist = $plugin->list_images($storeid, $scfg);
566 foreach my $info (@$vollist) {
567 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
568 my $basename = undef;
569 my $basevmid = undef;
572 (undef, undef, undef, $basename, $basevmid) =
573 $plugin->parse_volname($tmpvolname);
576 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
577 die "base volume '$volname' is still in use " .
578 "(use by '$tmpvolname')\n";
582 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
585 return if !$cleanup_worker;
587 my $rpcenv = PVE
::RPCEnvironment
::get
();
588 my $authuser = $rpcenv->get_user();
590 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
593 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
595 my ($cfg, $storeid, $tt) = @_;
597 die "unknown template type '$tt'\n"
598 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
600 my $ids = $cfg->{ids
};
602 storage_check_enabled
($cfg, $storeid) if ($storeid);
608 foreach my $sid (keys %$ids) {
609 next if $storeid && $storeid ne $sid;
611 my $scfg = $ids->{$sid};
612 my $type = $scfg->{type
};
614 next if !storage_check_enabled
($cfg, $sid, undef, 1);
616 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
617 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
618 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
620 activate_storage
($cfg, $sid);
623 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
625 my $path = $plugin->get_subdir($scfg, $tt);
627 foreach my $fn (<$path/*>) {
632 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
634 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
636 } elsif ($tt eq 'vztmpl') {
637 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
639 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
641 } elsif ($tt eq 'backup') {
642 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
644 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
647 $info->{size
} = -s
$fn;
649 push @{$res->{$sid}}, $info;
654 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
662 my ($cfg, $storeid, $vmid, $vollist) = @_;
664 my $ids = $cfg->{ids
};
666 storage_check_enabled
($cfg, $storeid) if ($storeid);
670 # prepare/activate/refresh all storages
672 my $storage_list = [];
674 foreach my $volid (@$vollist) {
675 my ($sid, undef) = parse_volume_id
($volid);
676 next if !defined($ids->{$sid});
677 next if !storage_check_enabled
($cfg, $sid, undef, 1);
678 push @$storage_list, $sid;
681 foreach my $sid (keys %$ids) {
682 next if $storeid && $storeid ne $sid;
683 next if !storage_check_enabled
($cfg, $sid, undef, 1);
684 push @$storage_list, $sid;
690 activate_storage_list
($cfg, $storage_list, $cache);
692 foreach my $sid (keys %$ids) {
693 next if $storeid && $storeid ne $sid;
694 next if !storage_check_enabled
($cfg, $sid, undef, 1);
696 my $scfg = $ids->{$sid};
697 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
698 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
699 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
707 my $filename = "/sys/kernel/uevent_seqnum";
710 if (my $fh = IO
::File-
>new($filename, "r")) {
712 if ($line =~ m/^(\d+)$/) {
720 sub activate_storage
{
721 my ($cfg, $storeid, $cache) = @_;
723 $cache = {} if !$cache;
725 my $scfg = storage_check_enabled
($cfg, $storeid);
727 return if $cache->{activated
}->{$storeid};
729 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
731 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
734 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
735 activate_storage
($cfg, $baseid, $cache);
738 if (!$plugin->check_connection($storeid, $scfg)) {
739 die "storage '$storeid' is not online\n";
742 $plugin->activate_storage($storeid, $scfg, $cache);
744 my $newseq = uevent_seqnum
();
746 # only call udevsettle if there are events
747 if ($newseq > $cache->{uevent_seqnum
}) {
749 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
750 $cache->{uevent_seqnum
} = $newseq;
753 $cache->{activated
}->{$storeid} = 1;
756 sub activate_storage_list
{
757 my ($cfg, $storeid_list, $cache) = @_;
759 $cache = {} if !$cache;
761 foreach my $storeid (@$storeid_list) {
762 activate_storage
($cfg, $storeid, $cache);
766 sub deactivate_storage
{
767 my ($cfg, $storeid) = @_;
769 my $scfg = storage_config
($cfg, $storeid);
770 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
773 $plugin->deactivate_storage($storeid, $scfg, $cache);
776 sub activate_volumes
{
777 my ($cfg, $vollist, $exclusive) = @_;
779 return if !($vollist && scalar(@$vollist));
781 my $storagehash = {};
782 foreach my $volid (@$vollist) {
783 my ($storeid, undef) = parse_volume_id
($volid);
784 $storagehash->{$storeid} = 1;
789 activate_storage_list
($cfg, [keys %$storagehash], $cache);
791 foreach my $volid (@$vollist) {
792 my ($storeid, $volname) = parse_volume_id
($volid);
793 my $scfg = storage_config
($cfg, $storeid);
794 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
795 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
799 sub deactivate_volumes
{
800 my ($cfg, $vollist) = @_;
802 return if !($vollist && scalar(@$vollist));
807 foreach my $volid (@$vollist) {
808 my ($storeid, $volname) = parse_volume_id
($volid);
810 my $scfg = storage_config
($cfg, $storeid);
811 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
814 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
818 push @errlist, $volid;
822 die "volume deativation failed: " . join(' ', @errlist)
827 my ($cfg, $content) = @_;
829 my $ids = $cfg->{ids
};
834 foreach my $storeid (keys %$ids) {
836 next if $content && !$ids->{$storeid}->{content
}->{$content};
838 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
840 my $type = $ids->{$storeid}->{type
};
842 $info->{$storeid} = {
847 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
848 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
852 push @$slist, $storeid;
857 foreach my $storeid (keys %$ids) {
858 my $scfg = $ids->{$storeid};
859 next if !$info->{$storeid};
861 eval { activate_storage
($cfg, $storeid, $cache); };
867 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
868 my ($total, $avail, $used, $active);
869 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
872 $info->{$storeid}->{total
} = $total;
873 $info->{$storeid}->{avail
} = $avail;
874 $info->{$storeid}->{used
} = $used;
875 $info->{$storeid}->{active
} = $active;
884 my $packed_ip = gethostbyname($server);
885 if (defined $packed_ip) {
886 return inet_ntoa
($packed_ip);
892 my ($server_in) = @_;
895 if (!($server = resolv_server
($server_in))) {
896 die "unable to resolve address for server '${server_in}'\n";
899 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
902 run_command
($cmd, outfunc
=> sub {
905 # note: howto handle white spaces in export path??
906 if ($line =~ m!^(/\S+)\s+(.+)$!) {
915 my ($portal, $noerr) = @_;
917 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
921 if (my $ip = resolv_server
($server)) {
923 return $port ?
"$server:$port" : $server;
926 return undef if $noerr;
928 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
931 # idea is from usbutils package (/usr/bin/usb-devices) script
932 sub __scan_usb_device
{
933 my ($res, $devpath, $parent, $level) = @_;
935 return if ! -d
$devpath;
936 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
937 my $port = $level ?
int($1 - 1) : 0;
939 my $busnum = int(file_read_firstline
("$devpath/busnum"));
940 my $devnum = int(file_read_firstline
("$devpath/devnum"));
947 speed
=> file_read_firstline
("$devpath/speed"),
948 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
949 vendid
=> file_read_firstline
("$devpath/idVendor"),
950 prodid
=> file_read_firstline
("$devpath/idProduct"),
954 my $usbpath = $devpath;
955 $usbpath =~ s
|^.*/\d
+\
-||;
956 $d->{usbpath
} = $usbpath;
959 my $product = file_read_firstline
("$devpath/product");
960 $d->{product
} = $product if $product;
962 my $manu = file_read_firstline
("$devpath/manufacturer");
963 $d->{manufacturer
} = $manu if $manu;
965 my $serial => file_read_firstline
("$devpath/serial");
966 $d->{serial
} = $serial if $serial;
970 foreach my $subdev (<$devpath/$busnum-*>) {
971 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
972 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
981 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
982 __scan_usb_device
($devlist, $device, 0, 0);
989 my ($portal_in) = @_;
992 if (!($portal = resolv_portal
($portal_in))) {
993 die "unable to parse/resolve portal address '${portal_in}'\n";
996 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
999 sub storage_default_format
{
1000 my ($cfg, $storeid) = @_;
1002 my $scfg = storage_config
($cfg, $storeid);
1004 return PVE
::Storage
::Plugin
::default_format
($scfg);
1007 sub vgroup_is_used
{
1008 my ($cfg, $vgname) = @_;
1010 foreach my $storeid (keys %{$cfg->{ids
}}) {
1011 my $scfg = storage_config
($cfg, $storeid);
1012 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1020 sub target_is_used
{
1021 my ($cfg, $target) = @_;
1023 foreach my $storeid (keys %{$cfg->{ids
}}) {
1024 my $scfg = storage_config
($cfg, $storeid);
1025 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1033 sub volume_is_used
{
1034 my ($cfg, $volid) = @_;
1036 foreach my $storeid (keys %{$cfg->{ids
}}) {
1037 my $scfg = storage_config
($cfg, $storeid);
1038 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1046 sub storage_is_used
{
1047 my ($cfg, $storeid) = @_;
1049 foreach my $sid (keys %{$cfg->{ids
}}) {
1050 my $scfg = storage_config
($cfg, $sid);
1051 next if !$scfg->{base
};
1052 my ($st) = parse_volume_id
($scfg->{base
});
1053 return 1 if $st && $st eq $storeid;
1060 my ($list, $func) = @_;
1064 foreach my $sid (keys %$list) {
1065 foreach my $info (@{$list->{$sid}}) {
1066 my $volid = $info->{volid
};
1067 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1068 if ($sid1 && $sid1 eq $sid) {
1069 &$func ($volid, $sid, $info);
1071 warn "detected strange volid '$volid' in volume list for '$sid'\n";