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
;
33 # load and initialize all plugins
34 PVE
::Storage
::DirPlugin-
>register();
35 PVE
::Storage
::LVMPlugin-
>register();
36 PVE
::Storage
::NFSPlugin-
>register();
37 PVE
::Storage
::ISCSIPlugin-
>register();
38 PVE
::Storage
::RBDPlugin-
>register();
39 PVE
::Storage
::SheepdogPlugin-
>register();
40 PVE
::Storage
::ISCSIDirectPlugin-
>register();
41 PVE
::Storage
::GlusterfsPlugin-
>register();
42 PVE
::Storage
::ZFSPoolPlugin-
>register();
43 PVE
::Storage
::ZFSPlugin-
>register();
44 PVE
::Storage
::Plugin-
>init();
46 my $UDEVADM = '/sbin/udevadm';
48 # PVE::Storage utility functions
51 return cfs_read_file
("storage.cfg");
54 sub lock_storage_config
{
55 my ($code, $errmsg) = @_;
57 cfs_lock_file
("storage.cfg", undef, $code);
60 $errmsg ?
die "$errmsg: $err" : die $err;
65 my ($cfg, $storeid, $noerr) = @_;
67 die "no storage id specified\n" if !$storeid;
69 my $scfg = $cfg->{ids
}->{$storeid};
71 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
76 sub storage_check_node
{
77 my ($cfg, $storeid, $node, $noerr) = @_;
79 my $scfg = storage_config
($cfg, $storeid);
82 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
83 if (!$scfg->{nodes
}->{$node}) {
84 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
92 sub storage_check_enabled
{
93 my ($cfg, $storeid, $node, $noerr) = @_;
95 my $scfg = storage_config
($cfg, $storeid);
97 if ($scfg->{disable
}) {
98 die "storage '$storeid' is disabled\n" if !$noerr;
102 return storage_check_node
($cfg, $storeid, $node, $noerr);
108 return keys %{$cfg->{ids
}};
112 my ($filename, $timeout) = @_;
114 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
117 sub volume_size_info
{
118 my ($cfg, $volid, $timeout) = @_;
120 my ($storeid, $volname) = parse_volume_id
($volid, 1);
122 my $scfg = storage_config
($cfg, $storeid);
123 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
124 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
125 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
126 return file_size_info
($volid, $timeout);
133 my ($cfg, $volid, $size, $running) = @_;
135 my ($storeid, $volname) = parse_volume_id
($volid, 1);
137 my $scfg = storage_config
($cfg, $storeid);
138 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
139 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
140 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
141 die "resize file/device '$volid' is not possible\n";
143 die "unable to parse volume ID '$volid'\n";
147 sub volume_rollback_is_possible
{
148 my ($cfg, $volid, $snap) = @_;
150 my ($storeid, $volname) = parse_volume_id
($volid, 1);
152 my $scfg = storage_config
($cfg, $storeid);
153 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
154 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
155 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
156 die "snapshot rollback file/device '$volid' is not possible\n";
158 die "unable to parse volume ID '$volid'\n";
162 sub volume_snapshot
{
163 my ($cfg, $volid, $snap, $running) = @_;
165 my ($storeid, $volname) = parse_volume_id
($volid, 1);
167 my $scfg = storage_config
($cfg, $storeid);
168 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
169 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running);
170 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
171 die "snapshot file/device '$volid' is not possible\n";
173 die "unable to parse volume ID '$volid'\n";
177 sub volume_snapshot_rollback
{
178 my ($cfg, $volid, $snap) = @_;
180 my ($storeid, $volname) = parse_volume_id
($volid, 1);
182 my $scfg = storage_config
($cfg, $storeid);
183 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
184 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
185 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
186 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
187 die "snapshot rollback file/device '$volid' is not possible\n";
189 die "unable to parse volume ID '$volid'\n";
193 sub volume_snapshot_delete
{
194 my ($cfg, $volid, $snap, $running) = @_;
196 my ($storeid, $volname) = parse_volume_id
($volid, 1);
198 my $scfg = storage_config
($cfg, $storeid);
199 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
200 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
201 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
202 die "snapshot delete file/device '$volid' is not possible\n";
204 die "unable to parse volume ID '$volid'\n";
208 sub volume_has_feature
{
209 my ($cfg, $feature, $volid, $snap, $running) = @_;
211 my ($storeid, $volname) = parse_volume_id
($volid, 1);
213 my $scfg = storage_config
($cfg, $storeid);
214 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
215 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
216 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
224 my ($cfg, $storeid, $vmid) = @_;
226 my $scfg = storage_config
($cfg, $storeid);
227 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
229 my $path = $plugin->get_subdir($scfg, 'images');
231 return $vmid ?
"$path/$vmid" : $path;
234 sub get_private_dir
{
235 my ($cfg, $storeid, $vmid) = @_;
237 my $scfg = storage_config
($cfg, $storeid);
238 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
240 my $path = $plugin->get_subdir($scfg, 'rootdir');
242 return $vmid ?
"$path/$vmid" : $path;
246 my ($cfg, $storeid) = @_;
248 my $scfg = storage_config
($cfg, $storeid);
249 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
251 return $plugin->get_subdir($scfg, 'iso');
255 my ($cfg, $storeid) = @_;
257 my $scfg = storage_config
($cfg, $storeid);
258 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
260 return $plugin->get_subdir($scfg, 'vztmpl');
264 my ($cfg, $storeid) = @_;
266 my $scfg = storage_config
($cfg, $storeid);
267 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
269 return $plugin->get_subdir($scfg, 'backup');
272 # library implementation
277 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
283 my ($cfg, $volid) = @_;
285 my ($storeid, $volname) = parse_volume_id
($volid);
287 my $scfg = storage_config
($cfg, $storeid);
289 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
290 return $plugin->parse_volname($volname);
293 sub parse_volume_id
{
294 my ($volid, $noerr) = @_;
296 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
300 my ($cfg, $volid) = @_;
302 my ($sid, $volname) = parse_volume_id
($volid, 1);
305 if (my $scfg = $cfg->{ids
}->{$sid}) {
306 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
307 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
308 $plugin->parse_volname($volname);
309 return $isBase ?
1 : 0;
311 # stale volid with undefined storage - so we can just guess
312 if ($volid =~ m/base-/) {
320 # try to map a filesystem path to a volume identifier
321 sub path_to_volume_id
{
322 my ($cfg, $path) = @_;
324 my $ids = $cfg->{ids
};
326 my ($sid, $volname) = parse_volume_id
($path, 1);
328 if (my $scfg = $ids->{$sid}) {
330 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
331 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
332 return ($vtype, $path);
338 # Note: abs_path() return undef if $path doesn not exist
339 # for example when nfs storage is not mounted
340 $path = abs_path
($path) || $path;
342 foreach my $sid (keys %$ids) {
343 my $scfg = $ids->{$sid};
344 next if !$scfg->{path
};
345 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
346 my $imagedir = $plugin->get_subdir($scfg, 'images');
347 my $isodir = $plugin->get_subdir($scfg, 'iso');
348 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
349 my $backupdir = $plugin->get_subdir($scfg, 'backup');
350 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
352 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
356 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
357 foreach my $info (@$vollist) {
358 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
359 my $volpath = $plugin->path($scfg, $volname, $storeid);
360 if ($volpath eq $path) {
361 return ('images', $info->{volid
});
364 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
366 return ('iso', "$sid:iso/$name");
367 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
369 return ('vztmpl', "$sid:vztmpl/$name");
370 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
372 return ('rootdir', "$sid:rootdir/$vmid");
373 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
375 return ('iso', "$sid:backup/$name");
379 # can't map path to volume id
384 my ($cfg, $volid, $snapname) = @_;
386 my ($storeid, $volname) = parse_volume_id
($volid);
388 my $scfg = storage_config
($cfg, $storeid);
390 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
391 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
392 return wantarray ?
($path, $owner, $vtype) : $path;
395 sub abs_filesystem_path
{
396 my ($cfg, $volid) = @_;
399 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
400 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
401 $path = PVE
::Storage
::path
($cfg, $volid);
404 my $abspath = abs_path
($volid);
405 if ($abspath && $abspath =~ m
|^(/.+)$|) {
406 $path = $1; # untaint any path
411 die "can't find file '$volid'\n" if !($path && -f
$path);
416 sub storage_migrate
{
417 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
419 my ($storeid, $volname) = parse_volume_id
($volid);
420 $target_volname = $volname if !$target_volname;
422 my $scfg = storage_config
($cfg, $storeid);
424 # no need to migrate shared content
425 return if $storeid eq $target_storeid && $scfg->{shared
};
427 my $tcfg = storage_config
($cfg, $target_storeid);
429 my $target_volid = "${target_storeid}:${target_volname}";
431 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
433 my $sshoptions = "-o 'BatchMode=yes'";
434 my $ssh = "/usr/bin/ssh $sshoptions";
436 local $ENV{RSYNC_RSH
} = $ssh;
438 # only implemented for file system based storage
442 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
443 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
444 my $src = $src_plugin->path($scfg, $volname, $storeid);
445 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
447 my $dirname = dirname
($dst);
449 if ($tcfg->{shared
}) { # we can do a local copy
451 run_command
(['/bin/mkdir', '-p', $dirname]);
453 run_command
(['/bin/cp', $src, $dst]);
457 run_command
(['/usr/bin/ssh', "root\@${target_host}",
458 '/bin/mkdir', '-p', $dirname]);
460 # we use rsync with --sparse, so we can't use --inplace,
461 # so we remove file on the target if it already exists to
463 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
464 if ($format && ($format eq 'raw') && $size) {
465 run_command
(['/usr/bin/ssh', "root\@${target_host}",
470 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
471 $src, "root\@${target_host}:$dst"];
475 run_command
($cmd, outfunc
=> sub {
478 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
481 print "rsync status: $1\n";
491 die "$errstr - target type '$tcfg->{type}' not implemented\n";
494 die "$errstr - source type '$scfg->{type}' not implemented\n";
499 my ($cfg, $volid, $vmid, $snap) = @_;
501 my ($storeid, $volname) = parse_volume_id
($volid);
503 my $scfg = storage_config
($cfg, $storeid);
505 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
507 activate_storage
($cfg, $storeid);
509 # lock shared storage
510 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
511 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
512 return "$storeid:$volname";
516 sub vdisk_create_base
{
517 my ($cfg, $volid) = @_;
519 my ($storeid, $volname) = parse_volume_id
($volid);
521 my $scfg = storage_config
($cfg, $storeid);
523 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
525 activate_storage
($cfg, $storeid);
527 # lock shared storage
528 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
529 my $volname = $plugin->create_base($storeid, $scfg, $volname);
530 return "$storeid:$volname";
535 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
537 die "no storage id specified\n" if !$storeid;
539 PVE
::JSONSchema
::parse_storage_id
($storeid);
541 my $scfg = storage_config
($cfg, $storeid);
543 die "no VMID specified\n" if !$vmid;
545 $vmid = parse_vmid
($vmid);
547 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
549 $fmt = $defformat if !$fmt;
551 activate_storage
($cfg, $storeid);
553 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
555 # lock shared storage
556 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
557 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
558 return "$storeid:$volname";
563 my ($cfg, $volid) = @_;
565 my ($storeid, $volname) = parse_volume_id
($volid);
567 my $scfg = storage_config
($cfg, $storeid);
569 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
571 activate_storage
($cfg, $storeid);
575 # lock shared storage
576 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
578 my ($vtype, $name, $vmid, undef, undef, $isBase) =
579 $plugin->parse_volname($volname);
581 my $vollist = $plugin->list_images($storeid, $scfg);
582 foreach my $info (@$vollist) {
583 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
584 my $basename = undef;
585 my $basevmid = undef;
588 (undef, undef, undef, $basename, $basevmid) =
589 $plugin->parse_volname($tmpvolname);
592 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
593 die "base volume '$volname' is still in use " .
594 "(use by '$tmpvolname')\n";
598 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
601 return if !$cleanup_worker;
603 my $rpcenv = PVE
::RPCEnvironment
::get
();
604 my $authuser = $rpcenv->get_user();
606 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
609 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
611 my ($cfg, $storeid, $tt) = @_;
613 die "unknown template type '$tt'\n"
614 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
616 my $ids = $cfg->{ids
};
618 storage_check_enabled
($cfg, $storeid) if ($storeid);
624 foreach my $sid (keys %$ids) {
625 next if $storeid && $storeid ne $sid;
627 my $scfg = $ids->{$sid};
628 my $type = $scfg->{type
};
630 next if !storage_check_enabled
($cfg, $sid, undef, 1);
632 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
633 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
634 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
636 activate_storage
($cfg, $sid);
639 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
641 my $path = $plugin->get_subdir($scfg, $tt);
643 foreach my $fn (<$path/*>) {
648 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
650 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
652 } elsif ($tt eq 'vztmpl') {
653 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
655 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
657 } elsif ($tt eq 'backup') {
658 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
660 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
663 $info->{size
} = -s
$fn;
665 push @{$res->{$sid}}, $info;
670 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
678 my ($cfg, $storeid, $vmid, $vollist) = @_;
680 my $ids = $cfg->{ids
};
682 storage_check_enabled
($cfg, $storeid) if ($storeid);
686 # prepare/activate/refresh all storages
688 my $storage_list = [];
690 foreach my $volid (@$vollist) {
691 my ($sid, undef) = parse_volume_id
($volid);
692 next if !defined($ids->{$sid});
693 next if !storage_check_enabled
($cfg, $sid, undef, 1);
694 push @$storage_list, $sid;
697 foreach my $sid (keys %$ids) {
698 next if $storeid && $storeid ne $sid;
699 next if !storage_check_enabled
($cfg, $sid, undef, 1);
700 push @$storage_list, $sid;
706 activate_storage_list
($cfg, $storage_list, $cache);
708 foreach my $sid (keys %$ids) {
709 next if $storeid && $storeid ne $sid;
710 next if !storage_check_enabled
($cfg, $sid, undef, 1);
712 my $scfg = $ids->{$sid};
713 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
714 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
715 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
723 my $filename = "/sys/kernel/uevent_seqnum";
726 if (my $fh = IO
::File-
>new($filename, "r")) {
728 if ($line =~ m/^(\d+)$/) {
736 sub activate_storage
{
737 my ($cfg, $storeid, $cache) = @_;
739 $cache = {} if !$cache;
741 my $scfg = storage_check_enabled
($cfg, $storeid);
743 return if $cache->{activated
}->{$storeid};
745 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
747 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
750 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
751 activate_storage
($cfg, $baseid, $cache);
754 if (!$plugin->check_connection($storeid, $scfg)) {
755 die "storage '$storeid' is not online\n";
758 $plugin->activate_storage($storeid, $scfg, $cache);
760 my $newseq = uevent_seqnum
();
762 # only call udevsettle if there are events
763 if ($newseq > $cache->{uevent_seqnum
}) {
765 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
766 $cache->{uevent_seqnum
} = $newseq;
769 $cache->{activated
}->{$storeid} = 1;
772 sub activate_storage_list
{
773 my ($cfg, $storeid_list, $cache) = @_;
775 $cache = {} if !$cache;
777 foreach my $storeid (@$storeid_list) {
778 activate_storage
($cfg, $storeid, $cache);
782 sub deactivate_storage
{
783 my ($cfg, $storeid) = @_;
785 my $scfg = storage_config
($cfg, $storeid);
786 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
789 $plugin->deactivate_storage($storeid, $scfg, $cache);
792 sub activate_volumes
{
793 my ($cfg, $vollist, $exclusive) = @_;
795 return if !($vollist && scalar(@$vollist));
797 my $storagehash = {};
798 foreach my $volid (@$vollist) {
799 my ($storeid, undef) = parse_volume_id
($volid);
800 $storagehash->{$storeid} = 1;
805 activate_storage_list
($cfg, [keys %$storagehash], $cache);
807 foreach my $volid (@$vollist) {
808 my ($storeid, $volname) = parse_volume_id
($volid);
809 my $scfg = storage_config
($cfg, $storeid);
810 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
811 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
815 sub deactivate_volumes
{
816 my ($cfg, $vollist) = @_;
818 return if !($vollist && scalar(@$vollist));
823 foreach my $volid (@$vollist) {
824 my ($storeid, $volname) = parse_volume_id
($volid);
826 my $scfg = storage_config
($cfg, $storeid);
827 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
830 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
834 push @errlist, $volid;
838 die "volume deativation failed: " . join(' ', @errlist)
843 my ($cfg, $content) = @_;
845 my $ids = $cfg->{ids
};
850 foreach my $storeid (keys %$ids) {
852 next if $content && !$ids->{$storeid}->{content
}->{$content};
854 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
856 my $type = $ids->{$storeid}->{type
};
858 $info->{$storeid} = {
863 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
864 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
868 push @$slist, $storeid;
873 foreach my $storeid (keys %$ids) {
874 my $scfg = $ids->{$storeid};
875 next if !$info->{$storeid};
877 eval { activate_storage
($cfg, $storeid, $cache); };
883 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
884 my ($total, $avail, $used, $active);
885 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
888 $info->{$storeid}->{total
} = $total;
889 $info->{$storeid}->{avail
} = $avail;
890 $info->{$storeid}->{used
} = $used;
891 $info->{$storeid}->{active
} = $active;
900 my $packed_ip = gethostbyname($server);
901 if (defined $packed_ip) {
902 return inet_ntoa
($packed_ip);
908 my ($server_in) = @_;
911 if (!($server = resolv_server
($server_in))) {
912 die "unable to resolve address for server '${server_in}'\n";
915 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
918 run_command
($cmd, outfunc
=> sub {
921 # note: howto handle white spaces in export path??
922 if ($line =~ m!^(/\S+)\s+(.+)$!) {
932 my $cmd = ['zpool', 'list', '-H', '-o', 'name,size,free'];
935 run_command
($cmd, outfunc
=> sub {
938 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
939 my ($pool, $size_str, $free_str) = ($1, $2, $3);
940 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
941 my $free = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($free_str);
942 push @$res, { pool
=> $pool, size
=> $size, free
=> $free };
950 my ($portal, $noerr) = @_;
952 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
956 if (my $ip = resolv_server
($server)) {
958 return $port ?
"$server:$port" : $server;
961 return undef if $noerr;
963 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
966 # idea is from usbutils package (/usr/bin/usb-devices) script
967 sub __scan_usb_device
{
968 my ($res, $devpath, $parent, $level) = @_;
970 return if ! -d
$devpath;
971 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
972 my $port = $level ?
int($1 - 1) : 0;
974 my $busnum = int(file_read_firstline
("$devpath/busnum"));
975 my $devnum = int(file_read_firstline
("$devpath/devnum"));
982 speed
=> file_read_firstline
("$devpath/speed"),
983 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
984 vendid
=> file_read_firstline
("$devpath/idVendor"),
985 prodid
=> file_read_firstline
("$devpath/idProduct"),
989 my $usbpath = $devpath;
990 $usbpath =~ s
|^.*/\d
+\
-||;
991 $d->{usbpath
} = $usbpath;
994 my $product = file_read_firstline
("$devpath/product");
995 $d->{product
} = $product if $product;
997 my $manu = file_read_firstline
("$devpath/manufacturer");
998 $d->{manufacturer
} = $manu if $manu;
1000 my $serial => file_read_firstline
("$devpath/serial");
1001 $d->{serial
} = $serial if $serial;
1005 foreach my $subdev (<$devpath/$busnum-*>) {
1006 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1007 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1016 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1017 __scan_usb_device
($devlist, $device, 0, 0);
1024 my ($portal_in) = @_;
1027 if (!($portal = resolv_portal
($portal_in))) {
1028 die "unable to parse/resolve portal address '${portal_in}'\n";
1031 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1034 sub storage_default_format
{
1035 my ($cfg, $storeid) = @_;
1037 my $scfg = storage_config
($cfg, $storeid);
1039 return PVE
::Storage
::Plugin
::default_format
($scfg);
1042 sub vgroup_is_used
{
1043 my ($cfg, $vgname) = @_;
1045 foreach my $storeid (keys %{$cfg->{ids
}}) {
1046 my $scfg = storage_config
($cfg, $storeid);
1047 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1055 sub target_is_used
{
1056 my ($cfg, $target) = @_;
1058 foreach my $storeid (keys %{$cfg->{ids
}}) {
1059 my $scfg = storage_config
($cfg, $storeid);
1060 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1068 sub volume_is_used
{
1069 my ($cfg, $volid) = @_;
1071 foreach my $storeid (keys %{$cfg->{ids
}}) {
1072 my $scfg = storage_config
($cfg, $storeid);
1073 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1081 sub storage_is_used
{
1082 my ($cfg, $storeid) = @_;
1084 foreach my $sid (keys %{$cfg->{ids
}}) {
1085 my $scfg = storage_config
($cfg, $sid);
1086 next if !$scfg->{base
};
1087 my ($st) = parse_volume_id
($scfg->{base
});
1088 return 1 if $st && $st eq $storeid;
1095 my ($list, $func) = @_;
1099 foreach my $sid (keys %$list) {
1100 foreach my $info (@{$list->{$sid}}) {
1101 my $volid = $info->{volid
};
1102 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1103 if ($sid1 && $sid1 eq $sid) {
1104 &$func ($volid, $sid, $info);
1106 warn "detected strange volid '$volid' in volume list for '$sid'\n";