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
::ZFSPoolPlugin
;
31 use PVE
::Storage
::ZFSPlugin
;
32 use PVE
::Storage
::DRBDPlugin
;
34 # load and initialize all plugins
35 PVE
::Storage
::DirPlugin-
>register();
36 PVE
::Storage
::LVMPlugin-
>register();
37 PVE
::Storage
::NFSPlugin-
>register();
38 PVE
::Storage
::ISCSIPlugin-
>register();
39 PVE
::Storage
::RBDPlugin-
>register();
40 PVE
::Storage
::SheepdogPlugin-
>register();
41 PVE
::Storage
::ISCSIDirectPlugin-
>register();
42 PVE
::Storage
::GlusterfsPlugin-
>register();
43 PVE
::Storage
::ZFSPoolPlugin-
>register();
44 PVE
::Storage
::ZFSPlugin-
>register();
45 PVE
::Storage
::DRBDPlugin-
>register();
46 PVE
::Storage
::Plugin-
>init();
48 my $UDEVADM = '/sbin/udevadm';
50 # PVE::Storage utility functions
53 return cfs_read_file
("storage.cfg");
56 sub lock_storage_config
{
57 my ($code, $errmsg) = @_;
59 cfs_lock_file
("storage.cfg", undef, $code);
62 $errmsg ?
die "$errmsg: $err" : die $err;
67 my ($cfg, $storeid, $noerr) = @_;
69 die "no storage id specified\n" if !$storeid;
71 my $scfg = $cfg->{ids
}->{$storeid};
73 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
78 sub storage_check_node
{
79 my ($cfg, $storeid, $node, $noerr) = @_;
81 my $scfg = storage_config
($cfg, $storeid);
84 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
85 if (!$scfg->{nodes
}->{$node}) {
86 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
94 sub storage_check_enabled
{
95 my ($cfg, $storeid, $node, $noerr) = @_;
97 my $scfg = storage_config
($cfg, $storeid);
99 if ($scfg->{disable
}) {
100 die "storage '$storeid' is disabled\n" if !$noerr;
104 return storage_check_node
($cfg, $storeid, $node, $noerr);
110 return keys %{$cfg->{ids
}};
114 my ($filename, $timeout) = @_;
116 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
119 sub volume_size_info
{
120 my ($cfg, $volid, $timeout) = @_;
122 my ($storeid, $volname) = parse_volume_id
($volid, 1);
124 my $scfg = storage_config
($cfg, $storeid);
125 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
126 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
127 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
128 return file_size_info
($volid, $timeout);
135 my ($cfg, $volid, $size, $running) = @_;
137 my ($storeid, $volname) = parse_volume_id
($volid, 1);
139 my $scfg = storage_config
($cfg, $storeid);
140 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
141 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
142 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
143 die "resize file/device '$volid' is not possible\n";
145 die "unable to parse volume ID '$volid'\n";
149 sub volume_rollback_is_possible
{
150 my ($cfg, $volid, $snap) = @_;
152 my ($storeid, $volname) = parse_volume_id
($volid, 1);
154 my $scfg = storage_config
($cfg, $storeid);
155 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
156 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
157 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
158 die "snapshot rollback file/device '$volid' is not possible\n";
160 die "unable to parse volume ID '$volid'\n";
164 sub volume_snapshot
{
165 my ($cfg, $volid, $snap) = @_;
167 my ($storeid, $volname) = parse_volume_id
($volid, 1);
169 my $scfg = storage_config
($cfg, $storeid);
170 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
171 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
172 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
173 die "snapshot file/device '$volid' is not possible\n";
175 die "unable to parse volume ID '$volid'\n";
179 sub volume_snapshot_rollback
{
180 my ($cfg, $volid, $snap) = @_;
182 my ($storeid, $volname) = parse_volume_id
($volid, 1);
184 my $scfg = storage_config
($cfg, $storeid);
185 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
186 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
187 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
188 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
189 die "snapshot rollback file/device '$volid' is not possible\n";
191 die "unable to parse volume ID '$volid'\n";
195 sub volume_snapshot_delete
{
196 my ($cfg, $volid, $snap, $running) = @_;
198 my ($storeid, $volname) = parse_volume_id
($volid, 1);
200 my $scfg = storage_config
($cfg, $storeid);
201 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
202 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
203 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
204 die "snapshot delete file/device '$volid' is not possible\n";
206 die "unable to parse volume ID '$volid'\n";
210 sub volume_has_feature
{
211 my ($cfg, $feature, $volid, $snap, $running) = @_;
213 my ($storeid, $volname) = parse_volume_id
($volid, 1);
215 my $scfg = storage_config
($cfg, $storeid);
216 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
217 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
218 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
226 my ($cfg, $storeid, $vmid) = @_;
228 my $scfg = storage_config
($cfg, $storeid);
229 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
231 my $path = $plugin->get_subdir($scfg, 'images');
233 return $vmid ?
"$path/$vmid" : $path;
236 sub get_private_dir
{
237 my ($cfg, $storeid, $vmid) = @_;
239 my $scfg = storage_config
($cfg, $storeid);
240 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
242 my $path = $plugin->get_subdir($scfg, 'rootdir');
244 return $vmid ?
"$path/$vmid" : $path;
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, 'iso');
257 my ($cfg, $storeid) = @_;
259 my $scfg = storage_config
($cfg, $storeid);
260 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
262 return $plugin->get_subdir($scfg, 'vztmpl');
266 my ($cfg, $storeid) = @_;
268 my $scfg = storage_config
($cfg, $storeid);
269 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
271 return $plugin->get_subdir($scfg, 'backup');
274 # library implementation
279 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
285 my ($cfg, $volid) = @_;
287 my ($storeid, $volname) = parse_volume_id
($volid);
289 my $scfg = storage_config
($cfg, $storeid);
291 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
292 return $plugin->parse_volname($volname);
295 sub parse_volume_id
{
296 my ($volid, $noerr) = @_;
298 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
302 my ($cfg, $volid) = @_;
304 my ($sid, $volname) = parse_volume_id
($volid, 1);
307 if (my $scfg = $cfg->{ids
}->{$sid}) {
308 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
309 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
310 $plugin->parse_volname($volname);
311 return $isBase ?
1 : 0;
313 # stale volid with undefined storage - so we can just guess
314 if ($volid =~ m/base-/) {
322 # try to map a filesystem path to a volume identifier
323 sub path_to_volume_id
{
324 my ($cfg, $path) = @_;
326 my $ids = $cfg->{ids
};
328 my ($sid, $volname) = parse_volume_id
($path, 1);
330 if (my $scfg = $ids->{$sid}) {
332 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
333 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
334 return ($vtype, $path);
340 # Note: abs_path() return undef if $path doesn not exist
341 # for example when nfs storage is not mounted
342 $path = abs_path
($path) || $path;
344 foreach my $sid (keys %$ids) {
345 my $scfg = $ids->{$sid};
346 next if !$scfg->{path
};
347 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
348 my $imagedir = $plugin->get_subdir($scfg, 'images');
349 my $isodir = $plugin->get_subdir($scfg, 'iso');
350 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
351 my $backupdir = $plugin->get_subdir($scfg, 'backup');
352 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
354 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
358 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
359 foreach my $info (@$vollist) {
360 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
361 my $volpath = $plugin->path($scfg, $volname, $storeid);
362 if ($volpath eq $path) {
363 return ('images', $info->{volid
});
366 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
368 return ('iso', "$sid:iso/$name");
369 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
371 return ('vztmpl', "$sid:vztmpl/$name");
372 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
374 return ('rootdir', "$sid:rootdir/$vmid");
375 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
377 return ('iso', "$sid:backup/$name");
381 # can't map path to volume id
386 my ($cfg, $volid, $snapname) = @_;
388 my ($storeid, $volname) = parse_volume_id
($volid);
390 my $scfg = storage_config
($cfg, $storeid);
392 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
393 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
394 return wantarray ?
($path, $owner, $vtype) : $path;
397 sub abs_filesystem_path
{
398 my ($cfg, $volid) = @_;
401 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
402 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
403 $path = PVE
::Storage
::path
($cfg, $volid);
406 my $abspath = abs_path
($volid);
407 if ($abspath && $abspath =~ m
|^(/.+)$|) {
408 $path = $1; # untaint any path
413 die "can't find file '$volid'\n" if !($path && -f
$path);
418 sub storage_migrate
{
419 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
421 my ($storeid, $volname) = parse_volume_id
($volid);
422 $target_volname = $volname if !$target_volname;
424 my $scfg = storage_config
($cfg, $storeid);
426 # no need to migrate shared content
427 return if $storeid eq $target_storeid && $scfg->{shared
};
429 my $tcfg = storage_config
($cfg, $target_storeid);
431 my $target_volid = "${target_storeid}:${target_volname}";
433 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
435 my $sshoptions = "-o 'BatchMode=yes'";
436 my $ssh = "/usr/bin/ssh $sshoptions";
438 local $ENV{RSYNC_RSH
} = $ssh;
440 # only implemented for file system based storage
444 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
445 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
446 my $src = $src_plugin->path($scfg, $volname, $storeid);
447 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
449 my $dirname = dirname
($dst);
451 if ($tcfg->{shared
}) { # we can do a local copy
453 run_command
(['/bin/mkdir', '-p', $dirname]);
455 run_command
(['/bin/cp', $src, $dst]);
459 run_command
(['/usr/bin/ssh', "root\@${target_host}",
460 '/bin/mkdir', '-p', $dirname]);
462 # we use rsync with --sparse, so we can't use --inplace,
463 # so we remove file on the target if it already exists to
465 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
466 if ($format && ($format eq 'raw') && $size) {
467 run_command
(['/usr/bin/ssh', "root\@${target_host}",
472 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
473 $src, "root\@${target_host}:$dst"];
477 run_command
($cmd, outfunc
=> sub {
480 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
483 print "rsync status: $1\n";
493 die "$errstr - target type '$tcfg->{type}' not implemented\n";
496 } elsif ($scfg->{type
} eq 'zfspool') {
498 if ($tcfg->{type
} eq 'zfspool') {
500 die "$errstr - pool on target has not same name as source!"
501 if $tcfg->{pool
} ne $scfg->{pool
};
503 my (undef, $volname) = parse_volname
($cfg, $volid);
505 my $zfspath = "$scfg->{pool}\/$volname";
507 my $snap = "zfs snapshot $zfspath\@__migration__";
509 my $send = "zfs send -v $zfspath\@__migration__ \| ssh root\@$target_host zfs recv $zfspath";
511 my $destroy_target = "ssh root\@$target_host zfs destroy $zfspath\@__migration__";
518 run_command
("zfs destroy $zfspath\@__migration__");
521 run_command
($destroy_target);
524 die "$errstr - target type $tcfg->{type} is not valid\n";
527 die "$errstr - source type '$scfg->{type}' not implemented\n";
532 my ($cfg, $volid, $vmid, $snap) = @_;
534 my ($storeid, $volname) = parse_volume_id
($volid);
536 my $scfg = storage_config
($cfg, $storeid);
538 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
540 activate_storage
($cfg, $storeid);
542 # lock shared storage
543 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
544 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
545 return "$storeid:$volname";
549 sub vdisk_create_base
{
550 my ($cfg, $volid) = @_;
552 my ($storeid, $volname) = parse_volume_id
($volid);
554 my $scfg = storage_config
($cfg, $storeid);
556 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
558 activate_storage
($cfg, $storeid);
560 # lock shared storage
561 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
562 my $volname = $plugin->create_base($storeid, $scfg, $volname);
563 return "$storeid:$volname";
568 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
570 die "no storage id specified\n" if !$storeid;
572 PVE
::JSONSchema
::parse_storage_id
($storeid);
574 my $scfg = storage_config
($cfg, $storeid);
576 die "no VMID specified\n" if !$vmid;
578 $vmid = parse_vmid
($vmid);
580 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
582 $fmt = $defformat if !$fmt;
584 activate_storage
($cfg, $storeid);
586 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
588 # lock shared storage
589 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
590 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
591 return "$storeid:$volname";
596 my ($cfg, $volid) = @_;
598 my ($storeid, $volname) = parse_volume_id
($volid);
600 my $scfg = storage_config
($cfg, $storeid);
602 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
604 activate_storage
($cfg, $storeid);
608 # lock shared storage
609 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
611 my ($vtype, $name, $vmid, undef, undef, $isBase) =
612 $plugin->parse_volname($volname);
614 my $vollist = $plugin->list_images($storeid, $scfg);
615 foreach my $info (@$vollist) {
616 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
617 my $basename = undef;
618 my $basevmid = undef;
621 (undef, undef, undef, $basename, $basevmid) =
622 $plugin->parse_volname($tmpvolname);
625 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
626 die "base volume '$volname' is still in use " .
627 "(use by '$tmpvolname')\n";
631 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
634 return if !$cleanup_worker;
636 my $rpcenv = PVE
::RPCEnvironment
::get
();
637 my $authuser = $rpcenv->get_user();
639 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
642 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
644 my ($cfg, $storeid, $tt) = @_;
646 die "unknown template type '$tt'\n"
647 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
649 my $ids = $cfg->{ids
};
651 storage_check_enabled
($cfg, $storeid) if ($storeid);
657 foreach my $sid (keys %$ids) {
658 next if $storeid && $storeid ne $sid;
660 my $scfg = $ids->{$sid};
661 my $type = $scfg->{type
};
663 next if !storage_check_enabled
($cfg, $sid, undef, 1);
665 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
666 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
667 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
669 activate_storage
($cfg, $sid);
672 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
674 my $path = $plugin->get_subdir($scfg, $tt);
676 foreach my $fn (<$path/*>) {
681 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
683 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
685 } elsif ($tt eq 'vztmpl') {
686 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
688 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
690 } elsif ($tt eq 'backup') {
691 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
693 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
696 $info->{size
} = -s
$fn;
698 push @{$res->{$sid}}, $info;
703 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
711 my ($cfg, $storeid, $vmid, $vollist) = @_;
713 my $ids = $cfg->{ids
};
715 storage_check_enabled
($cfg, $storeid) if ($storeid);
719 # prepare/activate/refresh all storages
721 my $storage_list = [];
723 foreach my $volid (@$vollist) {
724 my ($sid, undef) = parse_volume_id
($volid);
725 next if !defined($ids->{$sid});
726 next if !storage_check_enabled
($cfg, $sid, undef, 1);
727 push @$storage_list, $sid;
730 foreach my $sid (keys %$ids) {
731 next if $storeid && $storeid ne $sid;
732 next if !storage_check_enabled
($cfg, $sid, undef, 1);
733 push @$storage_list, $sid;
739 activate_storage_list
($cfg, $storage_list, $cache);
741 foreach my $sid (keys %$ids) {
742 next if $storeid && $storeid ne $sid;
743 next if !storage_check_enabled
($cfg, $sid, undef, 1);
745 my $scfg = $ids->{$sid};
746 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
747 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
748 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
756 my $filename = "/sys/kernel/uevent_seqnum";
759 if (my $fh = IO
::File-
>new($filename, "r")) {
761 if ($line =~ m/^(\d+)$/) {
769 sub activate_storage
{
770 my ($cfg, $storeid, $cache) = @_;
772 $cache = {} if !$cache;
774 my $scfg = storage_check_enabled
($cfg, $storeid);
776 return if $cache->{activated
}->{$storeid};
778 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
780 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
783 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
784 activate_storage
($cfg, $baseid, $cache);
787 if (!$plugin->check_connection($storeid, $scfg)) {
788 die "storage '$storeid' is not online\n";
791 $plugin->activate_storage($storeid, $scfg, $cache);
793 my $newseq = uevent_seqnum
();
795 # only call udevsettle if there are events
796 if ($newseq > $cache->{uevent_seqnum
}) {
798 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
799 $cache->{uevent_seqnum
} = $newseq;
802 $cache->{activated
}->{$storeid} = 1;
805 sub activate_storage_list
{
806 my ($cfg, $storeid_list, $cache) = @_;
808 $cache = {} if !$cache;
810 foreach my $storeid (@$storeid_list) {
811 activate_storage
($cfg, $storeid, $cache);
815 sub deactivate_storage
{
816 my ($cfg, $storeid) = @_;
818 my $scfg = storage_config
($cfg, $storeid);
819 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
822 $plugin->deactivate_storage($storeid, $scfg, $cache);
825 sub activate_volumes
{
826 my ($cfg, $vollist, $exclusive) = @_;
828 return if !($vollist && scalar(@$vollist));
830 my $storagehash = {};
831 foreach my $volid (@$vollist) {
832 my ($storeid, undef) = parse_volume_id
($volid);
833 $storagehash->{$storeid} = 1;
838 activate_storage_list
($cfg, [keys %$storagehash], $cache);
840 foreach my $volid (@$vollist) {
841 my ($storeid, $volname) = parse_volume_id
($volid);
842 my $scfg = storage_config
($cfg, $storeid);
843 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
844 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
848 sub deactivate_volumes
{
849 my ($cfg, $vollist) = @_;
851 return if !($vollist && scalar(@$vollist));
856 foreach my $volid (@$vollist) {
857 my ($storeid, $volname) = parse_volume_id
($volid);
859 my $scfg = storage_config
($cfg, $storeid);
860 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
863 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
867 push @errlist, $volid;
871 die "volume deativation failed: " . join(' ', @errlist)
876 my ($cfg, $content) = @_;
878 my $ids = $cfg->{ids
};
883 foreach my $storeid (keys %$ids) {
885 next if $content && !$ids->{$storeid}->{content
}->{$content};
887 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
889 my $type = $ids->{$storeid}->{type
};
891 $info->{$storeid} = {
896 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
897 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
901 push @$slist, $storeid;
906 foreach my $storeid (keys %$ids) {
907 my $scfg = $ids->{$storeid};
908 next if !$info->{$storeid};
910 eval { activate_storage
($cfg, $storeid, $cache); };
916 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
917 my ($total, $avail, $used, $active);
918 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
921 $info->{$storeid}->{total
} = $total;
922 $info->{$storeid}->{avail
} = $avail;
923 $info->{$storeid}->{used
} = $used;
924 $info->{$storeid}->{active
} = $active;
933 my ($packed_ip, $family);
935 my @res = PVE
::Tools
::getaddrinfo_all
($server);
936 $family = $res[0]->{family
};
937 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
939 if (defined $packed_ip) {
940 return Socket
::inet_ntop
($family, $packed_ip);
946 my ($server_in) = @_;
949 if (!($server = resolv_server
($server_in))) {
950 die "unable to resolve address for server '${server_in}'\n";
953 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
956 run_command
($cmd, outfunc
=> sub {
959 # note: howto handle white spaces in export path??
960 if ($line =~ m!^(/\S+)\s+(.+)$!) {
970 my $cmd = ['zpool', 'list', '-H', '-o', 'name,size,free'];
973 run_command
($cmd, outfunc
=> sub {
976 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
977 my ($pool, $size_str, $free_str) = ($1, $2, $3);
978 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
979 my $free = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($free_str);
980 push @$res, { pool
=> $pool, size
=> $size, free
=> $free };
988 my ($portal, $noerr) = @_;
990 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
994 if (my $ip = resolv_server
($server)) {
996 return $port ?
"$server:$port" : $server;
999 return undef if $noerr;
1001 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1004 # idea is from usbutils package (/usr/bin/usb-devices) script
1005 sub __scan_usb_device
{
1006 my ($res, $devpath, $parent, $level) = @_;
1008 return if ! -d
$devpath;
1009 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1010 my $port = $level ?
int($1 - 1) : 0;
1012 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1013 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1020 speed
=> file_read_firstline
("$devpath/speed"),
1021 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1022 vendid
=> file_read_firstline
("$devpath/idVendor"),
1023 prodid
=> file_read_firstline
("$devpath/idProduct"),
1027 my $usbpath = $devpath;
1028 $usbpath =~ s
|^.*/\d
+\
-||;
1029 $d->{usbpath
} = $usbpath;
1032 my $product = file_read_firstline
("$devpath/product");
1033 $d->{product
} = $product if $product;
1035 my $manu = file_read_firstline
("$devpath/manufacturer");
1036 $d->{manufacturer
} = $manu if $manu;
1038 my $serial => file_read_firstline
("$devpath/serial");
1039 $d->{serial
} = $serial if $serial;
1043 foreach my $subdev (<$devpath/$busnum-*>) {
1044 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1045 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1054 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1055 __scan_usb_device
($devlist, $device, 0, 0);
1062 my ($portal_in) = @_;
1065 if (!($portal = resolv_portal
($portal_in))) {
1066 die "unable to parse/resolve portal address '${portal_in}'\n";
1069 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1072 sub storage_default_format
{
1073 my ($cfg, $storeid) = @_;
1075 my $scfg = storage_config
($cfg, $storeid);
1077 return PVE
::Storage
::Plugin
::default_format
($scfg);
1080 sub vgroup_is_used
{
1081 my ($cfg, $vgname) = @_;
1083 foreach my $storeid (keys %{$cfg->{ids
}}) {
1084 my $scfg = storage_config
($cfg, $storeid);
1085 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1093 sub target_is_used
{
1094 my ($cfg, $target) = @_;
1096 foreach my $storeid (keys %{$cfg->{ids
}}) {
1097 my $scfg = storage_config
($cfg, $storeid);
1098 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1106 sub volume_is_used
{
1107 my ($cfg, $volid) = @_;
1109 foreach my $storeid (keys %{$cfg->{ids
}}) {
1110 my $scfg = storage_config
($cfg, $storeid);
1111 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1119 sub storage_is_used
{
1120 my ($cfg, $storeid) = @_;
1122 foreach my $sid (keys %{$cfg->{ids
}}) {
1123 my $scfg = storage_config
($cfg, $sid);
1124 next if !$scfg->{base
};
1125 my ($st) = parse_volume_id
($scfg->{base
});
1126 return 1 if $st && $st eq $storeid;
1133 my ($list, $func) = @_;
1137 foreach my $sid (keys %$list) {
1138 foreach my $info (@{$list->{$sid}}) {
1139 my $volid = $info->{volid
};
1140 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1141 if ($sid1 && $sid1 eq $sid) {
1142 &$func ($volid, $sid, $info);
1144 warn "detected strange volid '$volid' in volume list for '$sid'\n";