]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
b38990a3ba5e524618310049de1b91c5367c0400
12 use PVE
::Tools
qw(run_command file_read_firstline);
13 use PVE
::Cluster
qw(cfs_read_file cfs_lock_file);
14 use PVE
::Exception
qw(raise_param_exc);
17 use PVE
::RPCEnvironment
;
19 use PVE
::Storage
::Plugin
;
20 use PVE
::Storage
::DirPlugin
;
21 use PVE
::Storage
::LVMPlugin
;
22 use PVE
::Storage
::NFSPlugin
;
23 use PVE
::Storage
::ISCSIPlugin
;
24 use PVE
::Storage
::RBDPlugin
;
25 use PVE
::Storage
::SheepdogPlugin
;
26 use PVE
::Storage
::ISCSIDirectPlugin
;
27 use PVE
::Storage
::NexentaPlugin
;
28 use PVE
::Storage
::GlusterfsPlugin
;
30 # load and initialize all plugins
31 PVE
::Storage
::DirPlugin-
>register();
32 PVE
::Storage
::LVMPlugin-
>register();
33 PVE
::Storage
::NFSPlugin-
>register();
34 PVE
::Storage
::ISCSIPlugin-
>register();
35 PVE
::Storage
::RBDPlugin-
>register();
36 PVE
::Storage
::SheepdogPlugin-
>register();
37 PVE
::Storage
::ISCSIDirectPlugin-
>register();
38 PVE
::Storage
::NexentaPlugin-
>register();
39 PVE
::Storage
::GlusterfsPlugin-
>register();
40 PVE
::Storage
::Plugin-
>init();
42 my $UDEVADM = '/sbin/udevadm';
44 # PVE::Storage utility functions
47 return cfs_read_file
("storage.cfg");
50 sub lock_storage_config
{
51 my ($code, $errmsg) = @_;
53 cfs_lock_file
("storage.cfg", undef, $code);
56 $errmsg ?
die "$errmsg: $err" : die $err;
61 my ($cfg, $storeid, $noerr) = @_;
63 die "no storage id specified\n" if !$storeid;
65 my $scfg = $cfg->{ids
}->{$storeid};
67 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
72 sub storage_check_node
{
73 my ($cfg, $storeid, $node, $noerr) = @_;
75 my $scfg = storage_config
($cfg, $storeid);
78 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
79 if (!$scfg->{nodes
}->{$node}) {
80 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
88 sub storage_check_enabled
{
89 my ($cfg, $storeid, $node, $noerr) = @_;
91 my $scfg = storage_config
($cfg, $storeid);
93 if ($scfg->{disable
}) {
94 die "storage '$storeid' is disabled\n" if !$noerr;
98 return storage_check_node
($cfg, $storeid, $node, $noerr);
104 return keys %{$cfg->{ids
}};
108 my ($filename, $timeout) = @_;
110 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
113 sub volume_size_info
{
114 my ($cfg, $volid, $timeout) = @_;
116 my ($storeid, $volname) = parse_volume_id
($volid, 1);
118 my $scfg = storage_config
($cfg, $storeid);
119 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
120 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
121 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
122 return file_size_info
($volid, $timeout);
129 my ($cfg, $volid, $size, $running) = @_;
131 my ($storeid, $volname) = parse_volume_id
($volid, 1);
133 my $scfg = storage_config
($cfg, $storeid);
134 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
135 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
136 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
137 die "resize device is not possible";
143 sub volume_snapshot
{
144 my ($cfg, $volid, $snap, $running) = @_;
146 my ($storeid, $volname) = parse_volume_id
($volid, 1);
148 my $scfg = storage_config
($cfg, $storeid);
149 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
150 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running);
151 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
152 die "snapshot device is not possible";
154 die "can't snapshot";
158 sub volume_snapshot_rollback
{
159 my ($cfg, $volid, $snap) = @_;
161 my ($storeid, $volname) = parse_volume_id
($volid, 1);
163 my $scfg = storage_config
($cfg, $storeid);
164 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
165 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
166 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
167 die "snapshot rollback device is not possible";
169 die "can't snapshot";
173 sub volume_snapshot_delete
{
174 my ($cfg, $volid, $snap, $running) = @_;
176 my ($storeid, $volname) = parse_volume_id
($volid, 1);
178 my $scfg = storage_config
($cfg, $storeid);
179 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
180 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
181 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
182 die "snapshot delete device is not possible";
184 die "can't delete snapshot";
188 sub volume_has_feature
{
189 my ($cfg, $feature, $volid, $snap, $running) = @_;
191 my ($storeid, $volname) = parse_volume_id
($volid, 1);
193 my $scfg = storage_config
($cfg, $storeid);
194 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
195 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
196 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
204 my ($cfg, $storeid, $vmid) = @_;
206 my $scfg = storage_config
($cfg, $storeid);
207 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
209 my $path = $plugin->get_subdir($scfg, 'images');
211 return $vmid ?
"$path/$vmid" : $path;
214 sub get_private_dir
{
215 my ($cfg, $storeid, $vmid) = @_;
217 my $scfg = storage_config
($cfg, $storeid);
218 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
220 my $path = $plugin->get_subdir($scfg, 'rootdir');
222 return $vmid ?
"$path/$vmid" : $path;
226 my ($cfg, $storeid) = @_;
228 my $scfg = storage_config
($cfg, $storeid);
229 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
231 return $plugin->get_subdir($scfg, 'iso');
235 my ($cfg, $storeid) = @_;
237 my $scfg = storage_config
($cfg, $storeid);
238 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
240 return $plugin->get_subdir($scfg, 'vztmpl');
244 my ($cfg, $storeid) = @_;
246 my $scfg = storage_config
($cfg, $storeid);
247 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
249 return $plugin->get_subdir($scfg, 'backup');
252 # library implementation
257 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
263 my ($cfg, $volid) = @_;
265 my ($storeid, $volname) = parse_volume_id
($volid);
267 my $scfg = storage_config
($cfg, $storeid);
269 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
270 return $plugin->parse_volname($volname);
273 sub parse_volume_id
{
274 my ($volid, $noerr) = @_;
276 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
280 my ($cfg, $volid) = @_;
282 my ($sid, $volname) = parse_volume_id
($volid, 1);
285 if (my $scfg = $cfg->{ids
}->{$sid}) {
286 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
287 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
288 $plugin->parse_volname($volname);
289 return $isBase ?
1 : 0;
291 # stale volid with undefined storage - so we can just guess
292 if ($volid =~ m/base-/) {
300 # try to map a filesystem path to a volume identifier
301 sub path_to_volume_id
{
302 my ($cfg, $path) = @_;
304 my $ids = $cfg->{ids
};
306 my ($sid, $volname) = parse_volume_id
($path, 1);
308 if (my $scfg = $ids->{$sid}) {
310 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
311 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
312 return ($vtype, $path);
318 # Note: abs_path() return undef if $path doesn not exist
319 # for example when nfs storage is not mounted
320 $path = abs_path
($path) || $path;
322 foreach my $sid (keys %$ids) {
323 my $scfg = $ids->{$sid};
324 next if !$scfg->{path
};
325 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
326 my $imagedir = $plugin->get_subdir($scfg, 'images');
327 my $isodir = $plugin->get_subdir($scfg, 'iso');
328 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
329 my $backupdir = $plugin->get_subdir($scfg, 'backup');
330 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
332 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
336 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
337 foreach my $info (@$vollist) {
338 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
339 my $volpath = $plugin->path($scfg, $volname, $storeid);
340 if ($volpath eq $path) {
341 return ('images', $info->{volid
});
344 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
346 return ('iso', "$sid:iso/$name");
347 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
349 return ('vztmpl', "$sid:vztmpl/$name");
350 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
352 return ('rootdir', "$sid:rootdir/$vmid");
353 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
355 return ('iso', "$sid:backup/$name");
359 # can't map path to volume id
364 my ($cfg, $volid, $snapname) = @_;
366 my ($storeid, $volname) = parse_volume_id
($volid);
368 my $scfg = storage_config
($cfg, $storeid);
370 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
371 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
372 return wantarray ?
($path, $owner, $vtype) : $path;
375 sub storage_migrate
{
376 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
378 my ($storeid, $volname) = parse_volume_id
($volid);
379 $target_volname = $volname if !$target_volname;
381 my $scfg = storage_config
($cfg, $storeid);
383 # no need to migrate shared content
384 return if $storeid eq $target_storeid && $scfg->{shared
};
386 my $tcfg = storage_config
($cfg, $target_storeid);
388 my $target_volid = "${target_storeid}:${target_volname}";
390 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
392 my $sshoptions = "-o 'BatchMode=yes'";
393 my $ssh = "/usr/bin/ssh $sshoptions";
395 local $ENV{RSYNC_RSH
} = $ssh;
397 # only implemented for file system based storage
401 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
402 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
403 my $src = $src_plugin->path($scfg, $volname, $storeid);
404 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
406 my $dirname = dirname
($dst);
408 if ($tcfg->{shared
}) { # we can do a local copy
410 run_command
(['/bin/mkdir', '-p', $dirname]);
412 run_command
(['/bin/cp', $src, $dst]);
416 run_command
(['/usr/bin/ssh', "root\@${target_host}",
417 '/bin/mkdir', '-p', $dirname]);
419 # we use rsync with --sparse, so we can't use --inplace,
420 # so we remove file on the target if it already exists to
422 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
423 if ($format && ($format eq 'raw') && $size) {
424 run_command
(['/usr/bin/ssh', "root\@${target_host}",
429 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
430 $src, "root\@${target_host}:$dst"];
434 run_command
($cmd, outfunc
=> sub {
437 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
440 print "rsync status: $1\n";
450 die "$errstr - target type '$tcfg->{type}' not implemented\n";
453 die "$errstr - source type '$scfg->{type}' not implemented\n";
458 my ($cfg, $volid, $vmid) = @_;
460 my ($storeid, $volname) = parse_volume_id
($volid);
462 my $scfg = storage_config
($cfg, $storeid);
464 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
466 activate_storage
($cfg, $storeid);
468 # lock shared storage
469 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
470 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid);
471 return "$storeid:$volname";
475 sub vdisk_create_base
{
476 my ($cfg, $volid) = @_;
478 my ($storeid, $volname) = parse_volume_id
($volid);
480 my $scfg = storage_config
($cfg, $storeid);
482 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
484 activate_storage
($cfg, $storeid);
486 # lock shared storage
487 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
488 my $volname = $plugin->create_base($storeid, $scfg, $volname);
489 return "$storeid:$volname";
494 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
496 die "no storage id specified\n" if !$storeid;
498 PVE
::JSONSchema
::parse_storage_id
($storeid);
500 my $scfg = storage_config
($cfg, $storeid);
502 die "no VMID specified\n" if !$vmid;
504 $vmid = parse_vmid
($vmid);
506 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
508 $fmt = $defformat if !$fmt;
510 activate_storage
($cfg, $storeid);
512 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
514 # lock shared storage
515 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
516 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
517 return "$storeid:$volname";
522 my ($cfg, $volid) = @_;
524 my ($storeid, $volname) = parse_volume_id
($volid);
526 my $scfg = storage_config
($cfg, $storeid);
528 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
530 activate_storage
($cfg, $storeid);
534 # lock shared storage
535 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
537 my ($vtype, $name, $vmid, undef, undef, $isBase) =
538 $plugin->parse_volname($volname);
540 my $vollist = $plugin->list_images($storeid, $scfg);
541 foreach my $info (@$vollist) {
542 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
543 my $basename = undef;
544 my $basevmid = undef;
547 (undef, undef, undef, $basename, $basevmid) =
548 $plugin->parse_volname($tmpvolname);
551 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
552 die "base volume '$volname' is still in use " .
553 "(use by '$tmpvolname')\n";
557 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
560 return if !$cleanup_worker;
562 my $rpcenv = PVE
::RPCEnvironment
::get
();
563 my $authuser = $rpcenv->get_user();
565 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
568 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
570 my ($cfg, $storeid, $tt) = @_;
572 die "unknown template type '$tt'\n"
573 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
575 my $ids = $cfg->{ids
};
577 storage_check_enabled
($cfg, $storeid) if ($storeid);
583 foreach my $sid (keys %$ids) {
584 next if $storeid && $storeid ne $sid;
586 my $scfg = $ids->{$sid};
587 my $type = $scfg->{type
};
589 next if !storage_check_enabled
($cfg, $sid, undef, 1);
591 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
592 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
593 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
595 activate_storage
($cfg, $sid);
598 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
600 my $path = $plugin->get_subdir($scfg, $tt);
602 foreach my $fn (<$path/*>) {
607 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
609 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
611 } elsif ($tt eq 'vztmpl') {
612 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
614 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
616 } elsif ($tt eq 'backup') {
617 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
619 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
622 $info->{size
} = -s
$fn;
624 push @{$res->{$sid}}, $info;
629 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
637 my ($cfg, $storeid, $vmid, $vollist) = @_;
639 my $ids = $cfg->{ids
};
641 storage_check_enabled
($cfg, $storeid) if ($storeid);
645 # prepare/activate/refresh all storages
647 my $storage_list = [];
649 foreach my $volid (@$vollist) {
650 my ($sid, undef) = parse_volume_id
($volid);
651 next if !defined($ids->{$sid});
652 next if !storage_check_enabled
($cfg, $sid, undef, 1);
653 push @$storage_list, $sid;
656 foreach my $sid (keys %$ids) {
657 next if $storeid && $storeid ne $sid;
658 next if !storage_check_enabled
($cfg, $sid, undef, 1);
659 push @$storage_list, $sid;
665 activate_storage_list
($cfg, $storage_list, $cache);
667 foreach my $sid (keys %$ids) {
668 next if $storeid && $storeid ne $sid;
669 next if !storage_check_enabled
($cfg, $sid, undef, 1);
671 my $scfg = $ids->{$sid};
672 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
673 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
674 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
682 my $filename = "/sys/kernel/uevent_seqnum";
685 if (my $fh = IO
::File-
>new($filename, "r")) {
687 if ($line =~ m/^(\d+)$/) {
695 sub activate_storage
{
696 my ($cfg, $storeid, $cache) = @_;
698 $cache = {} if !$cache;
700 my $scfg = storage_check_enabled
($cfg, $storeid);
702 return if $cache->{activated
}->{$storeid};
704 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
706 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
709 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
710 activate_storage
($cfg, $baseid, $cache);
713 if (!$plugin->check_connection($storeid, $scfg)) {
714 die "storage '$storeid' is not online\n";
717 $plugin->activate_storage($storeid, $scfg, $cache);
719 my $newseq = uevent_seqnum
();
721 # only call udevsettle if there are events
722 if ($newseq > $cache->{uevent_seqnum
}) {
724 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
725 $cache->{uevent_seqnum
} = $newseq;
728 $cache->{activated
}->{$storeid} = 1;
731 sub activate_storage_list
{
732 my ($cfg, $storeid_list, $cache) = @_;
734 $cache = {} if !$cache;
736 foreach my $storeid (@$storeid_list) {
737 activate_storage
($cfg, $storeid, $cache);
741 sub deactivate_storage
{
742 my ($cfg, $storeid) = @_;
744 my $scfg = storage_config
($cfg, $storeid);
745 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
748 $plugin->deactivate_storage($storeid, $scfg, $cache);
751 sub activate_volumes
{
752 my ($cfg, $vollist, $exclusive) = @_;
754 return if !($vollist && scalar(@$vollist));
756 my $storagehash = {};
757 foreach my $volid (@$vollist) {
758 my ($storeid, undef) = parse_volume_id
($volid);
759 $storagehash->{$storeid} = 1;
764 activate_storage_list
($cfg, [keys %$storagehash], $cache);
766 foreach my $volid (@$vollist) {
767 my ($storeid, $volname) = parse_volume_id
($volid);
768 my $scfg = storage_config
($cfg, $storeid);
769 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
770 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
774 sub deactivate_volumes
{
775 my ($cfg, $vollist) = @_;
777 return if !($vollist && scalar(@$vollist));
782 foreach my $volid (@$vollist) {
783 my ($storeid, $volname) = parse_volume_id
($volid);
785 my $scfg = storage_config
($cfg, $storeid);
786 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
789 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
793 push @errlist, $volid;
797 die "volume deativation failed: " . join(' ', @errlist)
802 my ($cfg, $content) = @_;
804 my $ids = $cfg->{ids
};
809 foreach my $storeid (keys %$ids) {
811 next if $content && !$ids->{$storeid}->{content
}->{$content};
813 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
815 my $type = $ids->{$storeid}->{type
};
817 $info->{$storeid} = {
822 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
823 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
827 push @$slist, $storeid;
832 foreach my $storeid (keys %$ids) {
833 my $scfg = $ids->{$storeid};
834 next if !$info->{$storeid};
836 eval { activate_storage
($cfg, $storeid, $cache); };
842 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
843 my ($total, $avail, $used, $active);
844 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
847 $info->{$storeid}->{total
} = $total;
848 $info->{$storeid}->{avail
} = $avail;
849 $info->{$storeid}->{used
} = $used;
850 $info->{$storeid}->{active
} = $active;
859 my $packed_ip = gethostbyname($server);
860 if (defined $packed_ip) {
861 return inet_ntoa
($packed_ip);
867 my ($server_in) = @_;
870 if (!($server = resolv_server
($server_in))) {
871 die "unable to resolve address for server '${server_in}'\n";
874 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
877 run_command
($cmd, outfunc
=> sub {
880 # note: howto handle white spaces in export path??
881 if ($line =~ m!^(/\S+)\s+(.+)$!) {
890 my ($portal, $noerr) = @_;
892 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
896 if (my $ip = resolv_server
($server)) {
898 return $port ?
"$server:$port" : $server;
901 return undef if $noerr;
903 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
906 # idea is from usbutils package (/usr/bin/usb-devices) script
907 sub __scan_usb_device
{
908 my ($res, $devpath, $parent, $level) = @_;
910 return if ! -d
$devpath;
911 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
912 my $port = $level ?
int($1 - 1) : 0;
914 my $busnum = int(file_read_firstline
("$devpath/busnum"));
915 my $devnum = int(file_read_firstline
("$devpath/devnum"));
922 speed
=> file_read_firstline
("$devpath/speed"),
923 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
924 vendid
=> file_read_firstline
("$devpath/idVendor"),
925 prodid
=> file_read_firstline
("$devpath/idProduct"),
929 my $usbpath = $devpath;
930 $usbpath =~ s
|^.*/\d
+\
-||;
931 $d->{usbpath
} = $usbpath;
934 my $product = file_read_firstline
("$devpath/product");
935 $d->{product
} = $product if $product;
937 my $manu = file_read_firstline
("$devpath/manufacturer");
938 $d->{manufacturer
} = $manu if $manu;
940 my $serial => file_read_firstline
("$devpath/serial");
941 $d->{serial
} = $serial if $serial;
945 foreach my $subdev (<$devpath/$busnum-*>) {
946 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
947 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
956 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
957 __scan_usb_device
($devlist, $device, 0, 0);
964 my ($portal_in) = @_;
967 if (!($portal = resolv_portal
($portal_in))) {
968 die "unable to parse/resolve portal address '${portal_in}'\n";
971 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
974 sub storage_default_format
{
975 my ($cfg, $storeid) = @_;
977 my $scfg = storage_config
($cfg, $storeid);
979 return PVE
::Storage
::Plugin
::default_format
($scfg);
983 my ($cfg, $vgname) = @_;
985 foreach my $storeid (keys %{$cfg->{ids
}}) {
986 my $scfg = storage_config
($cfg, $storeid);
987 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
996 my ($cfg, $target) = @_;
998 foreach my $storeid (keys %{$cfg->{ids
}}) {
999 my $scfg = storage_config
($cfg, $storeid);
1000 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1008 sub volume_is_used
{
1009 my ($cfg, $volid) = @_;
1011 foreach my $storeid (keys %{$cfg->{ids
}}) {
1012 my $scfg = storage_config
($cfg, $storeid);
1013 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1021 sub storage_is_used
{
1022 my ($cfg, $storeid) = @_;
1024 foreach my $sid (keys %{$cfg->{ids
}}) {
1025 my $scfg = storage_config
($cfg, $sid);
1026 next if !$scfg->{base
};
1027 my ($st) = parse_volume_id
($scfg->{base
});
1028 return 1 if $st && $st eq $storeid;
1035 my ($list, $func) = @_;
1039 foreach my $sid (keys %$list) {
1040 foreach my $info (@{$list->{$sid}}) {
1041 my $volid = $info->{volid
};
1042 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1043 if ($sid1 && $sid1 eq $sid) {
1044 &$func ($volid, $sid, $info);
1046 warn "detected strange volid '$volid' in volume list for '$sid'\n";