15 use PVE
::Tools
qw(run_command file_read_firstline);
16 use PVE
::Cluster
qw(cfs_read_file cfs_lock_file);
17 use PVE
::Exception
qw(raise_param_exc);
20 use PVE
::RPCEnvironment
;
22 use PVE
::Storage
::Plugin
;
23 use PVE
::Storage
::DirPlugin
;
24 use PVE
::Storage
::LVMPlugin
;
25 use PVE
::Storage
::NFSPlugin
;
26 use PVE
::Storage
::ISCSIPlugin
;
27 use PVE
::Storage
::RBDPlugin
;
28 use PVE
::Storage
::SheepdogPlugin
;
29 use PVE
::Storage
::ISCSIDirectPlugin
;
30 use PVE
::Storage
::GlusterfsPlugin
;
31 use PVE
::Storage
::ZFSPoolPlugin
;
32 use PVE
::Storage
::ZFSPlugin
;
33 use PVE
::Storage
::DRBDPlugin
;
35 # load and initialize all plugins
36 PVE
::Storage
::DirPlugin-
>register();
37 PVE
::Storage
::LVMPlugin-
>register();
38 PVE
::Storage
::NFSPlugin-
>register();
39 PVE
::Storage
::ISCSIPlugin-
>register();
40 PVE
::Storage
::RBDPlugin-
>register();
41 PVE
::Storage
::SheepdogPlugin-
>register();
42 PVE
::Storage
::ISCSIDirectPlugin-
>register();
43 PVE
::Storage
::GlusterfsPlugin-
>register();
44 PVE
::Storage
::ZFSPoolPlugin-
>register();
45 PVE
::Storage
::ZFSPlugin-
>register();
46 PVE
::Storage
::DRBDPlugin-
>register();
47 PVE
::Storage
::Plugin-
>init();
49 my $UDEVADM = '/sbin/udevadm';
51 # PVE::Storage utility functions
54 return cfs_read_file
("storage.cfg");
57 sub lock_storage_config
{
58 my ($code, $errmsg) = @_;
60 cfs_lock_file
("storage.cfg", undef, $code);
63 $errmsg ?
die "$errmsg: $err" : die $err;
68 my ($cfg, $storeid, $noerr) = @_;
70 die "no storage id specified\n" if !$storeid;
72 my $scfg = $cfg->{ids
}->{$storeid};
74 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
79 sub storage_check_node
{
80 my ($cfg, $storeid, $node, $noerr) = @_;
82 my $scfg = storage_config
($cfg, $storeid);
85 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
86 if (!$scfg->{nodes
}->{$node}) {
87 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
95 sub storage_check_enabled
{
96 my ($cfg, $storeid, $node, $noerr) = @_;
98 my $scfg = storage_config
($cfg, $storeid);
100 if ($scfg->{disable
}) {
101 die "storage '$storeid' is disabled\n" if !$noerr;
105 return storage_check_node
($cfg, $storeid, $node, $noerr);
111 return keys %{$cfg->{ids
}};
115 my ($filename, $timeout) = @_;
117 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
120 sub volume_size_info
{
121 my ($cfg, $volid, $timeout) = @_;
123 my ($storeid, $volname) = parse_volume_id
($volid, 1);
125 my $scfg = storage_config
($cfg, $storeid);
126 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
127 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
128 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
129 return file_size_info
($volid, $timeout);
136 my ($cfg, $volid, $size, $running) = @_;
138 my ($storeid, $volname) = parse_volume_id
($volid, 1);
140 my $scfg = storage_config
($cfg, $storeid);
141 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
142 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
143 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
144 die "resize file/device '$volid' is not possible\n";
146 die "unable to parse volume ID '$volid'\n";
150 sub volume_rollback_is_possible
{
151 my ($cfg, $volid, $snap) = @_;
153 my ($storeid, $volname) = parse_volume_id
($volid, 1);
155 my $scfg = storage_config
($cfg, $storeid);
156 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
157 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
158 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
159 die "snapshot rollback file/device '$volid' is not possible\n";
161 die "unable to parse volume ID '$volid'\n";
165 sub volume_snapshot
{
166 my ($cfg, $volid, $snap) = @_;
168 my ($storeid, $volname) = parse_volume_id
($volid, 1);
170 my $scfg = storage_config
($cfg, $storeid);
171 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
172 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
173 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
174 die "snapshot file/device '$volid' is not possible\n";
176 die "unable to parse volume ID '$volid'\n";
180 sub volume_snapshot_rollback
{
181 my ($cfg, $volid, $snap) = @_;
183 my ($storeid, $volname) = parse_volume_id
($volid, 1);
185 my $scfg = storage_config
($cfg, $storeid);
186 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
187 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
188 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
189 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
190 die "snapshot rollback file/device '$volid' is not possible\n";
192 die "unable to parse volume ID '$volid'\n";
196 sub volume_snapshot_delete
{
197 my ($cfg, $volid, $snap, $running) = @_;
199 my ($storeid, $volname) = parse_volume_id
($volid, 1);
201 my $scfg = storage_config
($cfg, $storeid);
202 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
203 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
204 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
205 die "snapshot delete file/device '$volid' is not possible\n";
207 die "unable to parse volume ID '$volid'\n";
211 sub volume_has_feature
{
212 my ($cfg, $feature, $volid, $snap, $running) = @_;
214 my ($storeid, $volname) = parse_volume_id
($volid, 1);
216 my $scfg = storage_config
($cfg, $storeid);
217 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
218 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
219 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
227 my ($cfg, $storeid, $vmid) = @_;
229 my $scfg = storage_config
($cfg, $storeid);
230 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
232 my $path = $plugin->get_subdir($scfg, 'images');
234 return $vmid ?
"$path/$vmid" : $path;
237 sub get_private_dir
{
238 my ($cfg, $storeid, $vmid) = @_;
240 my $scfg = storage_config
($cfg, $storeid);
241 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
243 my $path = $plugin->get_subdir($scfg, 'rootdir');
245 return $vmid ?
"$path/$vmid" : $path;
249 my ($cfg, $storeid) = @_;
251 my $scfg = storage_config
($cfg, $storeid);
252 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
254 return $plugin->get_subdir($scfg, 'iso');
258 my ($cfg, $storeid) = @_;
260 my $scfg = storage_config
($cfg, $storeid);
261 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
263 return $plugin->get_subdir($scfg, 'vztmpl');
267 my ($cfg, $storeid) = @_;
269 my $scfg = storage_config
($cfg, $storeid);
270 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
272 return $plugin->get_subdir($scfg, 'backup');
275 # library implementation
280 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
286 my ($cfg, $volid) = @_;
288 my ($storeid, $volname) = parse_volume_id
($volid);
290 my $scfg = storage_config
($cfg, $storeid);
292 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
293 return $plugin->parse_volname($volname);
296 sub parse_volume_id
{
297 my ($volid, $noerr) = @_;
299 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
303 my ($cfg, $volid) = @_;
305 my ($sid, $volname) = parse_volume_id
($volid, 1);
308 if (my $scfg = $cfg->{ids
}->{$sid}) {
309 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
310 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
311 $plugin->parse_volname($volname);
312 return $isBase ?
1 : 0;
314 # stale volid with undefined storage - so we can just guess
315 if ($volid =~ m/base-/) {
323 # try to map a filesystem path to a volume identifier
324 sub path_to_volume_id
{
325 my ($cfg, $path) = @_;
327 my $ids = $cfg->{ids
};
329 my ($sid, $volname) = parse_volume_id
($path, 1);
331 if (my $scfg = $ids->{$sid}) {
333 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
334 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
335 return ($vtype, $path);
341 # Note: abs_path() return undef if $path doesn not exist
342 # for example when nfs storage is not mounted
343 $path = abs_path
($path) || $path;
345 foreach my $sid (keys %$ids) {
346 my $scfg = $ids->{$sid};
347 next if !$scfg->{path
};
348 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
349 my $imagedir = $plugin->get_subdir($scfg, 'images');
350 my $isodir = $plugin->get_subdir($scfg, 'iso');
351 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
352 my $backupdir = $plugin->get_subdir($scfg, 'backup');
353 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
355 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
359 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
360 foreach my $info (@$vollist) {
361 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
362 my $volpath = $plugin->path($scfg, $volname, $storeid);
363 if ($volpath eq $path) {
364 return ('images', $info->{volid
});
367 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
369 return ('iso', "$sid:iso/$name");
370 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
372 return ('vztmpl', "$sid:vztmpl/$name");
373 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
375 return ('rootdir', "$sid:rootdir/$vmid");
376 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
378 return ('iso', "$sid:backup/$name");
382 # can't map path to volume id
387 my ($cfg, $volid, $snapname) = @_;
389 my ($storeid, $volname) = parse_volume_id
($volid);
391 my $scfg = storage_config
($cfg, $storeid);
393 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
394 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
395 return wantarray ?
($path, $owner, $vtype) : $path;
398 sub abs_filesystem_path
{
399 my ($cfg, $volid) = @_;
402 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
403 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
404 $path = PVE
::Storage
::path
($cfg, $volid);
407 my $abspath = abs_path
($volid);
408 if ($abspath && $abspath =~ m
|^(/.+)$|) {
409 $path = $1; # untaint any path
414 die "can't find file '$volid'\n" if !($path && -f
$path);
419 sub storage_migrate
{
420 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
422 my ($storeid, $volname) = parse_volume_id
($volid);
423 $target_volname = $volname if !$target_volname;
425 my $scfg = storage_config
($cfg, $storeid);
427 # no need to migrate shared content
428 return if $storeid eq $target_storeid && $scfg->{shared
};
430 my $tcfg = storage_config
($cfg, $target_storeid);
432 my $target_volid = "${target_storeid}:${target_volname}";
434 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
436 my $sshoptions = "-o 'BatchMode=yes'";
437 my $ssh = "/usr/bin/ssh $sshoptions";
439 local $ENV{RSYNC_RSH
} = $ssh;
441 # only implemented for file system based storage
445 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
446 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
447 my $src = $src_plugin->path($scfg, $volname, $storeid);
448 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
450 my $dirname = dirname
($dst);
452 if ($tcfg->{shared
}) { # we can do a local copy
454 run_command
(['/bin/mkdir', '-p', $dirname]);
456 run_command
(['/bin/cp', $src, $dst]);
460 run_command
(['/usr/bin/ssh', "root\@${target_host}",
461 '/bin/mkdir', '-p', $dirname]);
463 # we use rsync with --sparse, so we can't use --inplace,
464 # so we remove file on the target if it already exists to
466 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
467 if ($format && ($format eq 'raw') && $size) {
468 run_command
(['/usr/bin/ssh', "root\@${target_host}",
473 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
474 $src, "root\@${target_host}:$dst"];
478 run_command
($cmd, outfunc
=> sub {
481 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
484 print "rsync status: $1\n";
494 die "$errstr - target type '$tcfg->{type}' not implemented\n";
497 } elsif ($scfg->{type
} eq 'zfspool') {
499 if ($tcfg->{type
} eq 'zfspool') {
501 die "$errstr - pool on target has not same name as source!"
502 if $tcfg->{pool
} ne $scfg->{pool
};
504 my (undef, $volname) = parse_volname
($cfg, $volid);
506 my $zfspath = "$scfg->{pool}\/$volname";
508 my $snap = "zfs snapshot $zfspath\@__migration__";
510 my $send = "zfs send -v $zfspath\@__migration__ \| ssh root\@$target_host zfs recv $zfspath";
512 my $destroy_target = "ssh root\@$target_host zfs destroy $zfspath\@__migration__";
519 run_command
("zfs destroy $zfspath\@__migration__");
522 run_command
($destroy_target);
525 die "$errstr - target type $tcfg->{type} is not valid\n";
528 die "$errstr - source type '$scfg->{type}' not implemented\n";
533 my ($cfg, $volid, $vmid, $snap) = @_;
535 my ($storeid, $volname) = parse_volume_id
($volid);
537 my $scfg = storage_config
($cfg, $storeid);
539 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
541 activate_storage
($cfg, $storeid);
543 # lock shared storage
544 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
545 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
546 return "$storeid:$volname";
550 sub vdisk_create_base
{
551 my ($cfg, $volid) = @_;
553 my ($storeid, $volname) = parse_volume_id
($volid);
555 my $scfg = storage_config
($cfg, $storeid);
557 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
559 activate_storage
($cfg, $storeid);
561 # lock shared storage
562 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
563 my $volname = $plugin->create_base($storeid, $scfg, $volname);
564 return "$storeid:$volname";
569 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
571 die "no storage id specified\n" if !$storeid;
573 PVE
::JSONSchema
::parse_storage_id
($storeid);
575 my $scfg = storage_config
($cfg, $storeid);
577 die "no VMID specified\n" if !$vmid;
579 $vmid = parse_vmid
($vmid);
581 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
583 $fmt = $defformat if !$fmt;
585 activate_storage
($cfg, $storeid);
587 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
589 # lock shared storage
590 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
591 my $old_umask = umask(umask|0037);
592 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
596 return "$storeid:$volname";
601 my ($cfg, $volid) = @_;
603 my ($storeid, $volname) = parse_volume_id
($volid);
605 my $scfg = storage_config
($cfg, $storeid);
607 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
609 activate_storage
($cfg, $storeid);
613 # lock shared storage
614 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
616 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
617 $plugin->parse_volname($volname);
619 my $vollist = $plugin->list_images($storeid, $scfg);
620 foreach my $info (@$vollist) {
621 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
622 my $basename = undef;
623 my $basevmid = undef;
626 (undef, undef, undef, $basename, $basevmid) =
627 $plugin->parse_volname($tmpvolname);
630 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
631 die "base volume '$volname' is still in use " .
632 "(use by '$tmpvolname')\n";
636 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
639 return if !$cleanup_worker;
641 my $rpcenv = PVE
::RPCEnvironment
::get
();
642 my $authuser = $rpcenv->get_user();
644 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
647 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
649 my ($cfg, $storeid, $tt) = @_;
651 die "unknown template type '$tt'\n"
652 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
654 my $ids = $cfg->{ids
};
656 storage_check_enabled
($cfg, $storeid) if ($storeid);
662 foreach my $sid (keys %$ids) {
663 next if $storeid && $storeid ne $sid;
665 my $scfg = $ids->{$sid};
666 my $type = $scfg->{type
};
668 next if !storage_check_enabled
($cfg, $sid, undef, 1);
670 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
671 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
672 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
674 activate_storage
($cfg, $sid);
677 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
679 my $path = $plugin->get_subdir($scfg, $tt);
681 foreach my $fn (<$path/*>) {
686 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
688 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
690 } elsif ($tt eq 'vztmpl') {
691 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
693 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
695 } elsif ($tt eq 'backup') {
696 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
698 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
701 $info->{size
} = -s
$fn;
703 push @{$res->{$sid}}, $info;
708 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
716 my ($cfg, $storeid, $vmid, $vollist) = @_;
718 my $ids = $cfg->{ids
};
720 storage_check_enabled
($cfg, $storeid) if ($storeid);
724 # prepare/activate/refresh all storages
726 my $storage_list = [];
728 foreach my $volid (@$vollist) {
729 my ($sid, undef) = parse_volume_id
($volid);
730 next if !defined($ids->{$sid});
731 next if !storage_check_enabled
($cfg, $sid, undef, 1);
732 push @$storage_list, $sid;
735 foreach my $sid (keys %$ids) {
736 next if $storeid && $storeid ne $sid;
737 next if !storage_check_enabled
($cfg, $sid, undef, 1);
738 push @$storage_list, $sid;
744 activate_storage_list
($cfg, $storage_list, $cache);
746 foreach my $sid (keys %$ids) {
747 next if $storeid && $storeid ne $sid;
748 next if !storage_check_enabled
($cfg, $sid, undef, 1);
750 my $scfg = $ids->{$sid};
751 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
752 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
753 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
761 my $filename = "/sys/kernel/uevent_seqnum";
764 if (my $fh = IO
::File-
>new($filename, "r")) {
766 if ($line =~ m/^(\d+)$/) {
774 sub activate_storage
{
775 my ($cfg, $storeid, $cache) = @_;
777 $cache = {} if !$cache;
779 my $scfg = storage_check_enabled
($cfg, $storeid);
781 return if $cache->{activated
}->{$storeid};
783 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
785 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
788 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
789 activate_storage
($cfg, $baseid, $cache);
792 if (!$plugin->check_connection($storeid, $scfg)) {
793 die "storage '$storeid' is not online\n";
796 $plugin->activate_storage($storeid, $scfg, $cache);
798 my $newseq = uevent_seqnum
();
800 # only call udevsettle if there are events
801 if ($newseq > $cache->{uevent_seqnum
}) {
803 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
804 $cache->{uevent_seqnum
} = $newseq;
807 $cache->{activated
}->{$storeid} = 1;
810 sub activate_storage_list
{
811 my ($cfg, $storeid_list, $cache) = @_;
813 $cache = {} if !$cache;
815 foreach my $storeid (@$storeid_list) {
816 activate_storage
($cfg, $storeid, $cache);
820 sub deactivate_storage
{
821 my ($cfg, $storeid) = @_;
823 my $scfg = storage_config
($cfg, $storeid);
824 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
827 $plugin->deactivate_storage($storeid, $scfg, $cache);
830 sub activate_volumes
{
831 my ($cfg, $vollist, $exclusive) = @_;
833 return if !($vollist && scalar(@$vollist));
835 my $storagehash = {};
836 foreach my $volid (@$vollist) {
837 my ($storeid, undef) = parse_volume_id
($volid);
838 $storagehash->{$storeid} = 1;
843 activate_storage_list
($cfg, [keys %$storagehash], $cache);
845 foreach my $volid (@$vollist) {
846 my ($storeid, $volname) = parse_volume_id
($volid);
847 my $scfg = storage_config
($cfg, $storeid);
848 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
849 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
853 sub deactivate_volumes
{
854 my ($cfg, $vollist) = @_;
856 return if !($vollist && scalar(@$vollist));
861 foreach my $volid (@$vollist) {
862 my ($storeid, $volname) = parse_volume_id
($volid);
864 my $scfg = storage_config
($cfg, $storeid);
865 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
868 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
872 push @errlist, $volid;
876 die "volume deativation failed: " . join(' ', @errlist)
881 my ($cfg, $content) = @_;
883 my $ids = $cfg->{ids
};
887 my @ctypes = PVE
::Tools
::split_list
($content);
890 foreach my $storeid (keys %$ids) {
892 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
894 if (defined($content)) {
896 foreach my $ctype (@ctypes) {
897 if ($ids->{$storeid}->{content
}->{$ctype}) {
902 next if !$want_ctype;
905 my $type = $ids->{$storeid}->{type
};
907 $info->{$storeid} = {
912 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
913 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
917 push @$slist, $storeid;
922 foreach my $storeid (keys %$ids) {
923 my $scfg = $ids->{$storeid};
924 next if !$info->{$storeid};
926 eval { activate_storage
($cfg, $storeid, $cache); };
932 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
933 my ($total, $avail, $used, $active);
934 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
937 $info->{$storeid}->{total
} = $total;
938 $info->{$storeid}->{avail
} = $avail;
939 $info->{$storeid}->{used
} = $used;
940 $info->{$storeid}->{active
} = $active;
949 my ($packed_ip, $family);
951 my @res = PVE
::Tools
::getaddrinfo_all
($server);
952 $family = $res[0]->{family
};
953 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
955 if (defined $packed_ip) {
956 return Socket
::inet_ntop
($family, $packed_ip);
962 my ($server_in) = @_;
965 if (!($server = resolv_server
($server_in))) {
966 die "unable to resolve address for server '${server_in}'\n";
969 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
972 run_command
($cmd, outfunc
=> sub {
975 # note: howto handle white spaces in export path??
976 if ($line =~ m!^(/\S+)\s+(.+)$!) {
986 my $cmd = ['zpool', 'list', '-H', '-o', 'name,size,free'];
989 run_command
($cmd, outfunc
=> sub {
992 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
993 my ($pool, $size_str, $free_str) = ($1, $2, $3);
994 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
995 my $free = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($free_str);
996 push @$res, { pool
=> $pool, size
=> $size, free
=> $free };
1004 my ($portal, $noerr) = @_;
1006 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
1010 if (my $ip = resolv_server
($server)) {
1012 return $port ?
"$server:$port" : $server;
1015 return undef if $noerr;
1017 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1020 # idea is from usbutils package (/usr/bin/usb-devices) script
1021 sub __scan_usb_device
{
1022 my ($res, $devpath, $parent, $level) = @_;
1024 return if ! -d
$devpath;
1025 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1026 my $port = $level ?
int($1 - 1) : 0;
1028 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1029 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1036 speed
=> file_read_firstline
("$devpath/speed"),
1037 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1038 vendid
=> file_read_firstline
("$devpath/idVendor"),
1039 prodid
=> file_read_firstline
("$devpath/idProduct"),
1043 my $usbpath = $devpath;
1044 $usbpath =~ s
|^.*/\d
+\
-||;
1045 $d->{usbpath
} = $usbpath;
1048 my $product = file_read_firstline
("$devpath/product");
1049 $d->{product
} = $product if $product;
1051 my $manu = file_read_firstline
("$devpath/manufacturer");
1052 $d->{manufacturer
} = $manu if $manu;
1054 my $serial => file_read_firstline
("$devpath/serial");
1055 $d->{serial
} = $serial if $serial;
1059 foreach my $subdev (<$devpath/$busnum-*>) {
1060 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1061 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1070 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1071 __scan_usb_device
($devlist, $device, 0, 0);
1078 my ($portal_in) = @_;
1081 if (!($portal = resolv_portal
($portal_in))) {
1082 die "unable to parse/resolve portal address '${portal_in}'\n";
1085 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1088 sub storage_default_format
{
1089 my ($cfg, $storeid) = @_;
1091 my $scfg = storage_config
($cfg, $storeid);
1093 return PVE
::Storage
::Plugin
::default_format
($scfg);
1096 sub vgroup_is_used
{
1097 my ($cfg, $vgname) = @_;
1099 foreach my $storeid (keys %{$cfg->{ids
}}) {
1100 my $scfg = storage_config
($cfg, $storeid);
1101 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1109 sub target_is_used
{
1110 my ($cfg, $target) = @_;
1112 foreach my $storeid (keys %{$cfg->{ids
}}) {
1113 my $scfg = storage_config
($cfg, $storeid);
1114 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1122 sub volume_is_used
{
1123 my ($cfg, $volid) = @_;
1125 foreach my $storeid (keys %{$cfg->{ids
}}) {
1126 my $scfg = storage_config
($cfg, $storeid);
1127 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1135 sub storage_is_used
{
1136 my ($cfg, $storeid) = @_;
1138 foreach my $sid (keys %{$cfg->{ids
}}) {
1139 my $scfg = storage_config
($cfg, $sid);
1140 next if !$scfg->{base
};
1141 my ($st) = parse_volume_id
($scfg->{base
});
1142 return 1 if $st && $st eq $storeid;
1149 my ($list, $func) = @_;
1153 foreach my $sid (keys %$list) {
1154 foreach my $info (@{$list->{$sid}}) {
1155 my $volid = $info->{volid
};
1156 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1157 if ($sid1 && $sid1 eq $sid) {
1158 &$func ($volid, $sid, $info);
1160 warn "detected strange volid '$volid' in volume list for '$sid'\n";