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 $old_umask = umask(umask|0037);
591 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
595 return "$storeid:$volname";
600 my ($cfg, $volid) = @_;
602 my ($storeid, $volname) = parse_volume_id
($volid);
604 my $scfg = storage_config
($cfg, $storeid);
606 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
608 activate_storage
($cfg, $storeid);
612 # lock shared storage
613 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
615 my ($vtype, $name, $vmid, undef, undef, $isBase) =
616 $plugin->parse_volname($volname);
618 my $vollist = $plugin->list_images($storeid, $scfg);
619 foreach my $info (@$vollist) {
620 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
621 my $basename = undef;
622 my $basevmid = undef;
625 (undef, undef, undef, $basename, $basevmid) =
626 $plugin->parse_volname($tmpvolname);
629 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
630 die "base volume '$volname' is still in use " .
631 "(use by '$tmpvolname')\n";
635 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
638 return if !$cleanup_worker;
640 my $rpcenv = PVE
::RPCEnvironment
::get
();
641 my $authuser = $rpcenv->get_user();
643 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
646 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
648 my ($cfg, $storeid, $tt) = @_;
650 die "unknown template type '$tt'\n"
651 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
653 my $ids = $cfg->{ids
};
655 storage_check_enabled
($cfg, $storeid) if ($storeid);
661 foreach my $sid (keys %$ids) {
662 next if $storeid && $storeid ne $sid;
664 my $scfg = $ids->{$sid};
665 my $type = $scfg->{type
};
667 next if !storage_check_enabled
($cfg, $sid, undef, 1);
669 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
670 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
671 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
673 activate_storage
($cfg, $sid);
676 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
678 my $path = $plugin->get_subdir($scfg, $tt);
680 foreach my $fn (<$path/*>) {
685 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
687 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
689 } elsif ($tt eq 'vztmpl') {
690 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
692 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
694 } elsif ($tt eq 'backup') {
695 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
697 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
700 $info->{size
} = -s
$fn;
702 push @{$res->{$sid}}, $info;
707 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
715 my ($cfg, $storeid, $vmid, $vollist) = @_;
717 my $ids = $cfg->{ids
};
719 storage_check_enabled
($cfg, $storeid) if ($storeid);
723 # prepare/activate/refresh all storages
725 my $storage_list = [];
727 foreach my $volid (@$vollist) {
728 my ($sid, undef) = parse_volume_id
($volid);
729 next if !defined($ids->{$sid});
730 next if !storage_check_enabled
($cfg, $sid, undef, 1);
731 push @$storage_list, $sid;
734 foreach my $sid (keys %$ids) {
735 next if $storeid && $storeid ne $sid;
736 next if !storage_check_enabled
($cfg, $sid, undef, 1);
737 push @$storage_list, $sid;
743 activate_storage_list
($cfg, $storage_list, $cache);
745 foreach my $sid (keys %$ids) {
746 next if $storeid && $storeid ne $sid;
747 next if !storage_check_enabled
($cfg, $sid, undef, 1);
749 my $scfg = $ids->{$sid};
750 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
751 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
752 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
760 my $filename = "/sys/kernel/uevent_seqnum";
763 if (my $fh = IO
::File-
>new($filename, "r")) {
765 if ($line =~ m/^(\d+)$/) {
773 sub activate_storage
{
774 my ($cfg, $storeid, $cache) = @_;
776 $cache = {} if !$cache;
778 my $scfg = storage_check_enabled
($cfg, $storeid);
780 return if $cache->{activated
}->{$storeid};
782 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
784 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
787 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
788 activate_storage
($cfg, $baseid, $cache);
791 if (!$plugin->check_connection($storeid, $scfg)) {
792 die "storage '$storeid' is not online\n";
795 $plugin->activate_storage($storeid, $scfg, $cache);
797 my $newseq = uevent_seqnum
();
799 # only call udevsettle if there are events
800 if ($newseq > $cache->{uevent_seqnum
}) {
802 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
803 $cache->{uevent_seqnum
} = $newseq;
806 $cache->{activated
}->{$storeid} = 1;
809 sub activate_storage_list
{
810 my ($cfg, $storeid_list, $cache) = @_;
812 $cache = {} if !$cache;
814 foreach my $storeid (@$storeid_list) {
815 activate_storage
($cfg, $storeid, $cache);
819 sub deactivate_storage
{
820 my ($cfg, $storeid) = @_;
822 my $scfg = storage_config
($cfg, $storeid);
823 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
826 $plugin->deactivate_storage($storeid, $scfg, $cache);
829 sub activate_volumes
{
830 my ($cfg, $vollist, $exclusive) = @_;
832 return if !($vollist && scalar(@$vollist));
834 my $storagehash = {};
835 foreach my $volid (@$vollist) {
836 my ($storeid, undef) = parse_volume_id
($volid);
837 $storagehash->{$storeid} = 1;
842 activate_storage_list
($cfg, [keys %$storagehash], $cache);
844 foreach my $volid (@$vollist) {
845 my ($storeid, $volname) = parse_volume_id
($volid);
846 my $scfg = storage_config
($cfg, $storeid);
847 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
848 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
852 sub deactivate_volumes
{
853 my ($cfg, $vollist) = @_;
855 return if !($vollist && scalar(@$vollist));
860 foreach my $volid (@$vollist) {
861 my ($storeid, $volname) = parse_volume_id
($volid);
863 my $scfg = storage_config
($cfg, $storeid);
864 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
867 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
871 push @errlist, $volid;
875 die "volume deativation failed: " . join(' ', @errlist)
880 my ($cfg, $content) = @_;
882 my $ids = $cfg->{ids
};
887 foreach my $storeid (keys %$ids) {
889 next if $content && !$ids->{$storeid}->{content
}->{$content};
891 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
893 my $type = $ids->{$storeid}->{type
};
895 $info->{$storeid} = {
900 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
901 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
905 push @$slist, $storeid;
910 foreach my $storeid (keys %$ids) {
911 my $scfg = $ids->{$storeid};
912 next if !$info->{$storeid};
914 eval { activate_storage
($cfg, $storeid, $cache); };
920 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
921 my ($total, $avail, $used, $active);
922 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
925 $info->{$storeid}->{total
} = $total;
926 $info->{$storeid}->{avail
} = $avail;
927 $info->{$storeid}->{used
} = $used;
928 $info->{$storeid}->{active
} = $active;
937 my ($packed_ip, $family);
939 my @res = PVE
::Tools
::getaddrinfo_all
($server);
940 $family = $res[0]->{family
};
941 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
943 if (defined $packed_ip) {
944 return Socket
::inet_ntop
($family, $packed_ip);
950 my ($server_in) = @_;
953 if (!($server = resolv_server
($server_in))) {
954 die "unable to resolve address for server '${server_in}'\n";
957 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
960 run_command
($cmd, outfunc
=> sub {
963 # note: howto handle white spaces in export path??
964 if ($line =~ m!^(/\S+)\s+(.+)$!) {
974 my $cmd = ['zpool', 'list', '-H', '-o', 'name,size,free'];
977 run_command
($cmd, outfunc
=> sub {
980 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
981 my ($pool, $size_str, $free_str) = ($1, $2, $3);
982 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
983 my $free = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($free_str);
984 push @$res, { pool
=> $pool, size
=> $size, free
=> $free };
992 my ($portal, $noerr) = @_;
994 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
998 if (my $ip = resolv_server
($server)) {
1000 return $port ?
"$server:$port" : $server;
1003 return undef if $noerr;
1005 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1008 # idea is from usbutils package (/usr/bin/usb-devices) script
1009 sub __scan_usb_device
{
1010 my ($res, $devpath, $parent, $level) = @_;
1012 return if ! -d
$devpath;
1013 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1014 my $port = $level ?
int($1 - 1) : 0;
1016 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1017 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1024 speed
=> file_read_firstline
("$devpath/speed"),
1025 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1026 vendid
=> file_read_firstline
("$devpath/idVendor"),
1027 prodid
=> file_read_firstline
("$devpath/idProduct"),
1031 my $usbpath = $devpath;
1032 $usbpath =~ s
|^.*/\d
+\
-||;
1033 $d->{usbpath
} = $usbpath;
1036 my $product = file_read_firstline
("$devpath/product");
1037 $d->{product
} = $product if $product;
1039 my $manu = file_read_firstline
("$devpath/manufacturer");
1040 $d->{manufacturer
} = $manu if $manu;
1042 my $serial => file_read_firstline
("$devpath/serial");
1043 $d->{serial
} = $serial if $serial;
1047 foreach my $subdev (<$devpath/$busnum-*>) {
1048 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1049 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1058 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1059 __scan_usb_device
($devlist, $device, 0, 0);
1066 my ($portal_in) = @_;
1069 if (!($portal = resolv_portal
($portal_in))) {
1070 die "unable to parse/resolve portal address '${portal_in}'\n";
1073 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1076 sub storage_default_format
{
1077 my ($cfg, $storeid) = @_;
1079 my $scfg = storage_config
($cfg, $storeid);
1081 return PVE
::Storage
::Plugin
::default_format
($scfg);
1084 sub vgroup_is_used
{
1085 my ($cfg, $vgname) = @_;
1087 foreach my $storeid (keys %{$cfg->{ids
}}) {
1088 my $scfg = storage_config
($cfg, $storeid);
1089 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1097 sub target_is_used
{
1098 my ($cfg, $target) = @_;
1100 foreach my $storeid (keys %{$cfg->{ids
}}) {
1101 my $scfg = storage_config
($cfg, $storeid);
1102 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1110 sub volume_is_used
{
1111 my ($cfg, $volid) = @_;
1113 foreach my $storeid (keys %{$cfg->{ids
}}) {
1114 my $scfg = storage_config
($cfg, $storeid);
1115 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1123 sub storage_is_used
{
1124 my ($cfg, $storeid) = @_;
1126 foreach my $sid (keys %{$cfg->{ids
}}) {
1127 my $scfg = storage_config
($cfg, $sid);
1128 next if !$scfg->{base
};
1129 my ($st) = parse_volume_id
($scfg->{base
});
1130 return 1 if $st && $st eq $storeid;
1137 my ($list, $func) = @_;
1141 foreach my $sid (keys %$list) {
1142 foreach my $info (@{$list->{$sid}}) {
1143 my $volid = $info->{volid
};
1144 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1145 if ($sid1 && $sid1 eq $sid) {
1146 &$func ($volid, $sid, $info);
1148 warn "detected strange volid '$volid' in volume list for '$sid'\n";