]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
3d01e37997bc8728ccf6b73baf7390c3105e3806
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+$/;
262 sub parse_volume_id
{
263 my ($volid, $noerr) = @_;
265 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
269 my ($cfg, $volid) = @_;
271 my ($sid, $volname) = parse_volume_id
($volid, 1);
274 if (my $scfg = $cfg->{ids
}->{$sid}) {
275 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
276 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
277 $plugin->parse_volname($volname);
278 return $isBase ?
1 : 0;
280 # stale volid with undefined storage - so we can just guess
281 if ($volid =~ m/base-/) {
289 # try to map a filesystem path to a volume identifier
290 sub path_to_volume_id
{
291 my ($cfg, $path) = @_;
293 my $ids = $cfg->{ids
};
295 my ($sid, $volname) = parse_volume_id
($path, 1);
297 if (my $scfg = $ids->{$sid}) {
299 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
300 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
301 return ($vtype, $path);
307 # Note: abs_path() return undef if $path doesn not exist
308 # for example when nfs storage is not mounted
309 $path = abs_path
($path) || $path;
311 foreach my $sid (keys %$ids) {
312 my $scfg = $ids->{$sid};
313 next if !$scfg->{path
};
314 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
315 my $imagedir = $plugin->get_subdir($scfg, 'images');
316 my $isodir = $plugin->get_subdir($scfg, 'iso');
317 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
318 my $backupdir = $plugin->get_subdir($scfg, 'backup');
319 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
321 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
325 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
326 foreach my $info (@$vollist) {
327 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
328 my $volpath = $plugin->path($scfg, $volname, $storeid);
329 if ($volpath eq $path) {
330 return ('images', $info->{volid
});
333 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
335 return ('iso', "$sid:iso/$name");
336 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
338 return ('vztmpl', "$sid:vztmpl/$name");
339 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
341 return ('rootdir', "$sid:rootdir/$vmid");
342 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
344 return ('iso', "$sid:backup/$name");
348 # can't map path to volume id
353 my ($cfg, $volid, $snapname) = @_;
355 my ($storeid, $volname) = parse_volume_id
($volid);
357 my $scfg = storage_config
($cfg, $storeid);
359 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
360 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
361 return wantarray ?
($path, $owner, $vtype) : $path;
364 sub storage_migrate
{
365 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
367 my ($storeid, $volname) = parse_volume_id
($volid);
368 $target_volname = $volname if !$target_volname;
370 my $scfg = storage_config
($cfg, $storeid);
372 # no need to migrate shared content
373 return if $storeid eq $target_storeid && $scfg->{shared
};
375 my $tcfg = storage_config
($cfg, $target_storeid);
377 my $target_volid = "${target_storeid}:${target_volname}";
379 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
381 my $sshoptions = "-o 'BatchMode=yes'";
382 my $ssh = "/usr/bin/ssh $sshoptions";
384 local $ENV{RSYNC_RSH
} = $ssh;
386 # only implemented for file system based storage
390 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
391 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
392 my $src = $src_plugin->path($scfg, $volname, $storeid);
393 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
395 my $dirname = dirname
($dst);
397 if ($tcfg->{shared
}) { # we can do a local copy
399 run_command
(['/bin/mkdir', '-p', $dirname]);
401 run_command
(['/bin/cp', $src, $dst]);
405 run_command
(['/usr/bin/ssh', "root\@${target_host}",
406 '/bin/mkdir', '-p', $dirname]);
408 # we use rsync with --sparse, so we can't use --inplace,
409 # so we remove file on the target if it already exists to
411 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
412 if ($format && ($format eq 'raw') && $size) {
413 run_command
(['/usr/bin/ssh', "root\@${target_host}",
418 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
419 $src, "root\@${target_host}:$dst"];
423 run_command
($cmd, outfunc
=> sub {
426 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
429 print "rsync status: $1\n";
439 die "$errstr - target type '$tcfg->{type}' not implemented\n";
442 die "$errstr - source type '$scfg->{type}' not implemented\n";
447 my ($cfg, $volid, $vmid) = @_;
449 my ($storeid, $volname) = parse_volume_id
($volid);
451 my $scfg = storage_config
($cfg, $storeid);
453 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
455 activate_storage
($cfg, $storeid);
457 # lock shared storage
458 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
459 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid);
460 return "$storeid:$volname";
464 sub vdisk_create_base
{
465 my ($cfg, $volid) = @_;
467 my ($storeid, $volname) = parse_volume_id
($volid);
469 my $scfg = storage_config
($cfg, $storeid);
471 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
473 activate_storage
($cfg, $storeid);
475 # lock shared storage
476 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
477 my $volname = $plugin->create_base($storeid, $scfg, $volname);
478 return "$storeid:$volname";
483 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
485 die "no storage id specified\n" if !$storeid;
487 PVE
::JSONSchema
::parse_storage_id
($storeid);
489 my $scfg = storage_config
($cfg, $storeid);
491 die "no VMID specified\n" if !$vmid;
493 $vmid = parse_vmid
($vmid);
495 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
497 $fmt = $defformat if !$fmt;
499 activate_storage
($cfg, $storeid);
501 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
503 # lock shared storage
504 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
505 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
506 return "$storeid:$volname";
511 my ($cfg, $volid) = @_;
513 my ($storeid, $volname) = parse_volume_id
($volid);
515 my $scfg = storage_config
($cfg, $storeid);
517 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
519 activate_storage
($cfg, $storeid);
523 # lock shared storage
524 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
526 my ($vtype, $name, $vmid, undef, undef, $isBase) =
527 $plugin->parse_volname($volname);
529 my $vollist = $plugin->list_images($storeid, $scfg);
530 foreach my $info (@$vollist) {
531 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
532 my $basename = undef;
533 my $basevmid = undef;
536 (undef, undef, undef, $basename, $basevmid) =
537 $plugin->parse_volname($tmpvolname);
540 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
541 die "base volume '$volname' is still in use " .
542 "(use by '$tmpvolname')\n";
546 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
549 return if !$cleanup_worker;
551 my $rpcenv = PVE
::RPCEnvironment
::get
();
552 my $authuser = $rpcenv->get_user();
554 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
557 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
559 my ($cfg, $storeid, $tt) = @_;
561 die "unknown template type '$tt'\n"
562 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
564 my $ids = $cfg->{ids
};
566 storage_check_enabled
($cfg, $storeid) if ($storeid);
572 foreach my $sid (keys %$ids) {
573 next if $storeid && $storeid ne $sid;
575 my $scfg = $ids->{$sid};
576 my $type = $scfg->{type
};
578 next if !storage_check_enabled
($cfg, $sid, undef, 1);
580 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
581 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
582 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
584 activate_storage
($cfg, $sid);
587 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
589 my $path = $plugin->get_subdir($scfg, $tt);
591 foreach my $fn (<$path/*>) {
596 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
598 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
600 } elsif ($tt eq 'vztmpl') {
601 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
603 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
605 } elsif ($tt eq 'backup') {
606 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
608 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
611 $info->{size
} = -s
$fn;
613 push @{$res->{$sid}}, $info;
618 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
626 my ($cfg, $storeid, $vmid, $vollist) = @_;
628 my $ids = $cfg->{ids
};
630 storage_check_enabled
($cfg, $storeid) if ($storeid);
634 # prepare/activate/refresh all storages
636 my $storage_list = [];
638 foreach my $volid (@$vollist) {
639 my ($sid, undef) = parse_volume_id
($volid);
640 next if !defined($ids->{$sid});
641 next if !storage_check_enabled
($cfg, $sid, undef, 1);
642 push @$storage_list, $sid;
645 foreach my $sid (keys %$ids) {
646 next if $storeid && $storeid ne $sid;
647 next if !storage_check_enabled
($cfg, $sid, undef, 1);
648 push @$storage_list, $sid;
654 activate_storage_list
($cfg, $storage_list, $cache);
656 foreach my $sid (keys %$ids) {
657 next if $storeid && $storeid ne $sid;
658 next if !storage_check_enabled
($cfg, $sid, undef, 1);
660 my $scfg = $ids->{$sid};
661 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
662 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
663 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
671 my $filename = "/sys/kernel/uevent_seqnum";
674 if (my $fh = IO
::File-
>new($filename, "r")) {
676 if ($line =~ m/^(\d+)$/) {
684 sub activate_storage
{
685 my ($cfg, $storeid, $cache) = @_;
687 $cache = {} if !$cache;
689 my $scfg = storage_check_enabled
($cfg, $storeid);
691 return if $cache->{activated
}->{$storeid};
693 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
695 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
698 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
699 activate_storage
($cfg, $baseid, $cache);
702 if (!$plugin->check_connection($storeid, $scfg)) {
703 die "storage '$storeid' is not online\n";
706 $plugin->activate_storage($storeid, $scfg, $cache);
708 my $newseq = uevent_seqnum
();
710 # only call udevsettle if there are events
711 if ($newseq > $cache->{uevent_seqnum
}) {
713 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
714 $cache->{uevent_seqnum
} = $newseq;
717 $cache->{activated
}->{$storeid} = 1;
720 sub activate_storage_list
{
721 my ($cfg, $storeid_list, $cache) = @_;
723 $cache = {} if !$cache;
725 foreach my $storeid (@$storeid_list) {
726 activate_storage
($cfg, $storeid, $cache);
730 sub deactivate_storage
{
731 my ($cfg, $storeid) = @_;
733 my $scfg = storage_config
($cfg, $storeid);
734 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
737 $plugin->deactivate_storage($storeid, $scfg, $cache);
740 sub activate_volumes
{
741 my ($cfg, $vollist, $exclusive) = @_;
743 return if !($vollist && scalar(@$vollist));
745 my $storagehash = {};
746 foreach my $volid (@$vollist) {
747 my ($storeid, undef) = parse_volume_id
($volid);
748 $storagehash->{$storeid} = 1;
753 activate_storage_list
($cfg, [keys %$storagehash], $cache);
755 foreach my $volid (@$vollist) {
756 my ($storeid, $volname) = parse_volume_id
($volid);
757 my $scfg = storage_config
($cfg, $storeid);
758 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
759 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
763 sub deactivate_volumes
{
764 my ($cfg, $vollist) = @_;
766 return if !($vollist && scalar(@$vollist));
771 foreach my $volid (@$vollist) {
772 my ($storeid, $volname) = parse_volume_id
($volid);
774 my $scfg = storage_config
($cfg, $storeid);
775 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
778 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
782 push @errlist, $volid;
786 die "volume deativation failed: " . join(' ', @errlist)
791 my ($cfg, $content) = @_;
793 my $ids = $cfg->{ids
};
798 foreach my $storeid (keys %$ids) {
800 next if $content && !$ids->{$storeid}->{content
}->{$content};
802 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
804 my $type = $ids->{$storeid}->{type
};
806 $info->{$storeid} = {
811 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
812 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
816 push @$slist, $storeid;
821 foreach my $storeid (keys %$ids) {
822 my $scfg = $ids->{$storeid};
823 next if !$info->{$storeid};
825 eval { activate_storage
($cfg, $storeid, $cache); };
831 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
832 my ($total, $avail, $used, $active);
833 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
836 $info->{$storeid}->{total
} = $total;
837 $info->{$storeid}->{avail
} = $avail;
838 $info->{$storeid}->{used
} = $used;
839 $info->{$storeid}->{active
} = $active;
848 my $packed_ip = gethostbyname($server);
849 if (defined $packed_ip) {
850 return inet_ntoa
($packed_ip);
856 my ($server_in) = @_;
859 if (!($server = resolv_server
($server_in))) {
860 die "unable to resolve address for server '${server_in}'\n";
863 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
866 run_command
($cmd, outfunc
=> sub {
869 # note: howto handle white spaces in export path??
870 if ($line =~ m!^(/\S+)\s+(.+)$!) {
879 my ($portal, $noerr) = @_;
881 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
885 if (my $ip = resolv_server
($server)) {
887 return $port ?
"$server:$port" : $server;
890 return undef if $noerr;
892 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
895 # idea is from usbutils package (/usr/bin/usb-devices) script
896 sub __scan_usb_device
{
897 my ($res, $devpath, $parent, $level) = @_;
899 return if ! -d
$devpath;
900 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
901 my $port = $level ?
int($1 - 1) : 0;
903 my $busnum = int(file_read_firstline
("$devpath/busnum"));
904 my $devnum = int(file_read_firstline
("$devpath/devnum"));
911 speed
=> file_read_firstline
("$devpath/speed"),
912 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
913 vendid
=> file_read_firstline
("$devpath/idVendor"),
914 prodid
=> file_read_firstline
("$devpath/idProduct"),
918 my $usbpath = $devpath;
919 $usbpath =~ s
|^.*/\d
+\
-||;
920 $d->{usbpath
} = $usbpath;
923 my $product = file_read_firstline
("$devpath/product");
924 $d->{product
} = $product if $product;
926 my $manu = file_read_firstline
("$devpath/manufacturer");
927 $d->{manufacturer
} = $manu if $manu;
929 my $serial => file_read_firstline
("$devpath/serial");
930 $d->{serial
} = $serial if $serial;
934 foreach my $subdev (<$devpath/$busnum-*>) {
935 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
936 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
945 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
946 __scan_usb_device
($devlist, $device, 0, 0);
953 my ($portal_in) = @_;
956 if (!($portal = resolv_portal
($portal_in))) {
957 die "unable to parse/resolve portal address '${portal_in}'\n";
960 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
963 sub storage_default_format
{
964 my ($cfg, $storeid) = @_;
966 my $scfg = storage_config
($cfg, $storeid);
968 return PVE
::Storage
::Plugin
::default_format
($scfg);
972 my ($cfg, $vgname) = @_;
974 foreach my $storeid (keys %{$cfg->{ids
}}) {
975 my $scfg = storage_config
($cfg, $storeid);
976 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
985 my ($cfg, $target) = @_;
987 foreach my $storeid (keys %{$cfg->{ids
}}) {
988 my $scfg = storage_config
($cfg, $storeid);
989 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
998 my ($cfg, $volid) = @_;
1000 foreach my $storeid (keys %{$cfg->{ids
}}) {
1001 my $scfg = storage_config
($cfg, $storeid);
1002 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1010 sub storage_is_used
{
1011 my ($cfg, $storeid) = @_;
1013 foreach my $sid (keys %{$cfg->{ids
}}) {
1014 my $scfg = storage_config
($cfg, $sid);
1015 next if !$scfg->{base
};
1016 my ($st) = parse_volume_id
($scfg->{base
});
1017 return 1 if $st && $st eq $storeid;
1024 my ($list, $func) = @_;
1028 foreach my $sid (keys %$list) {
1029 foreach my $info (@{$list->{$sid}}) {
1030 my $volid = $info->{volid
};
1031 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1032 if ($sid1 && $sid1 eq $sid) {
1033 &$func ($volid, $sid, $info);
1035 warn "detected strange volid '$volid' in volume list for '$sid'\n";