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 device is not possible";
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 device $volid is not possible";
158 die "can't parse volume id";
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 device is not possible";
173 die "can't snapshot";
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 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
185 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
186 die "snapshot rollback device is not possible";
188 die "can't snapshot";
192 sub volume_snapshot_delete
{
193 my ($cfg, $volid, $snap, $running) = @_;
195 my ($storeid, $volname) = parse_volume_id
($volid, 1);
197 my $scfg = storage_config
($cfg, $storeid);
198 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
199 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
200 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
201 die "snapshot delete device is not possible";
203 die "can't delete snapshot";
207 sub volume_has_feature
{
208 my ($cfg, $feature, $volid, $snap, $running) = @_;
210 my ($storeid, $volname) = parse_volume_id
($volid, 1);
212 my $scfg = storage_config
($cfg, $storeid);
213 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
214 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
215 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
223 my ($cfg, $storeid, $vmid) = @_;
225 my $scfg = storage_config
($cfg, $storeid);
226 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
228 my $path = $plugin->get_subdir($scfg, 'images');
230 return $vmid ?
"$path/$vmid" : $path;
233 sub get_private_dir
{
234 my ($cfg, $storeid, $vmid) = @_;
236 my $scfg = storage_config
($cfg, $storeid);
237 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
239 my $path = $plugin->get_subdir($scfg, 'rootdir');
241 return $vmid ?
"$path/$vmid" : $path;
245 my ($cfg, $storeid) = @_;
247 my $scfg = storage_config
($cfg, $storeid);
248 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
250 return $plugin->get_subdir($scfg, 'iso');
254 my ($cfg, $storeid) = @_;
256 my $scfg = storage_config
($cfg, $storeid);
257 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
259 return $plugin->get_subdir($scfg, 'vztmpl');
263 my ($cfg, $storeid) = @_;
265 my $scfg = storage_config
($cfg, $storeid);
266 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
268 return $plugin->get_subdir($scfg, 'backup');
271 # library implementation
276 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
282 my ($cfg, $volid) = @_;
284 my ($storeid, $volname) = parse_volume_id
($volid);
286 my $scfg = storage_config
($cfg, $storeid);
288 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
289 return $plugin->parse_volname($volname);
292 sub parse_volume_id
{
293 my ($volid, $noerr) = @_;
295 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
299 my ($cfg, $volid) = @_;
301 my ($sid, $volname) = parse_volume_id
($volid, 1);
304 if (my $scfg = $cfg->{ids
}->{$sid}) {
305 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
306 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
307 $plugin->parse_volname($volname);
308 return $isBase ?
1 : 0;
310 # stale volid with undefined storage - so we can just guess
311 if ($volid =~ m/base-/) {
319 # try to map a filesystem path to a volume identifier
320 sub path_to_volume_id
{
321 my ($cfg, $path) = @_;
323 my $ids = $cfg->{ids
};
325 my ($sid, $volname) = parse_volume_id
($path, 1);
327 if (my $scfg = $ids->{$sid}) {
329 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
330 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
331 return ($vtype, $path);
337 # Note: abs_path() return undef if $path doesn not exist
338 # for example when nfs storage is not mounted
339 $path = abs_path
($path) || $path;
341 foreach my $sid (keys %$ids) {
342 my $scfg = $ids->{$sid};
343 next if !$scfg->{path
};
344 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
345 my $imagedir = $plugin->get_subdir($scfg, 'images');
346 my $isodir = $plugin->get_subdir($scfg, 'iso');
347 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
348 my $backupdir = $plugin->get_subdir($scfg, 'backup');
349 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
351 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
355 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
356 foreach my $info (@$vollist) {
357 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
358 my $volpath = $plugin->path($scfg, $volname, $storeid);
359 if ($volpath eq $path) {
360 return ('images', $info->{volid
});
363 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
365 return ('iso', "$sid:iso/$name");
366 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
368 return ('vztmpl', "$sid:vztmpl/$name");
369 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
371 return ('rootdir', "$sid:rootdir/$vmid");
372 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
374 return ('iso', "$sid:backup/$name");
378 # can't map path to volume id
383 my ($cfg, $volid, $snapname) = @_;
385 my ($storeid, $volname) = parse_volume_id
($volid);
387 my $scfg = storage_config
($cfg, $storeid);
389 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
390 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
391 return wantarray ?
($path, $owner, $vtype) : $path;
394 sub abs_filesystem_path
{
395 my ($cfg, $volid) = @_;
398 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
399 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
400 $path = PVE
::Storage
::path
($cfg, $volid);
403 my $abspath = abs_path
($volid);
404 if ($abspath && $abspath =~ m
|^(/.+)$|) {
405 $path = $1; # untaint any path
410 die "can't find file '$volid'\n" if !($path && -f
$path);
415 sub storage_migrate
{
416 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
418 my ($storeid, $volname) = parse_volume_id
($volid);
419 $target_volname = $volname if !$target_volname;
421 my $scfg = storage_config
($cfg, $storeid);
423 # no need to migrate shared content
424 return if $storeid eq $target_storeid && $scfg->{shared
};
426 my $tcfg = storage_config
($cfg, $target_storeid);
428 my $target_volid = "${target_storeid}:${target_volname}";
430 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
432 my $sshoptions = "-o 'BatchMode=yes'";
433 my $ssh = "/usr/bin/ssh $sshoptions";
435 local $ENV{RSYNC_RSH
} = $ssh;
437 # only implemented for file system based storage
441 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
442 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
443 my $src = $src_plugin->path($scfg, $volname, $storeid);
444 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
446 my $dirname = dirname
($dst);
448 if ($tcfg->{shared
}) { # we can do a local copy
450 run_command
(['/bin/mkdir', '-p', $dirname]);
452 run_command
(['/bin/cp', $src, $dst]);
456 run_command
(['/usr/bin/ssh', "root\@${target_host}",
457 '/bin/mkdir', '-p', $dirname]);
459 # we use rsync with --sparse, so we can't use --inplace,
460 # so we remove file on the target if it already exists to
462 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
463 if ($format && ($format eq 'raw') && $size) {
464 run_command
(['/usr/bin/ssh', "root\@${target_host}",
469 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
470 $src, "root\@${target_host}:$dst"];
474 run_command
($cmd, outfunc
=> sub {
477 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
480 print "rsync status: $1\n";
490 die "$errstr - target type '$tcfg->{type}' not implemented\n";
493 die "$errstr - source type '$scfg->{type}' not implemented\n";
498 my ($cfg, $volid, $vmid, $snap) = @_;
500 my ($storeid, $volname) = parse_volume_id
($volid);
502 my $scfg = storage_config
($cfg, $storeid);
504 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
506 activate_storage
($cfg, $storeid);
508 # lock shared storage
509 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
510 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
511 return "$storeid:$volname";
515 sub vdisk_create_base
{
516 my ($cfg, $volid) = @_;
518 my ($storeid, $volname) = parse_volume_id
($volid);
520 my $scfg = storage_config
($cfg, $storeid);
522 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
524 activate_storage
($cfg, $storeid);
526 # lock shared storage
527 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
528 my $volname = $plugin->create_base($storeid, $scfg, $volname);
529 return "$storeid:$volname";
534 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
536 die "no storage id specified\n" if !$storeid;
538 PVE
::JSONSchema
::parse_storage_id
($storeid);
540 my $scfg = storage_config
($cfg, $storeid);
542 die "no VMID specified\n" if !$vmid;
544 $vmid = parse_vmid
($vmid);
546 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
548 $fmt = $defformat if !$fmt;
550 activate_storage
($cfg, $storeid);
552 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
554 # lock shared storage
555 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
556 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
557 return "$storeid:$volname";
562 my ($cfg, $volid) = @_;
564 my ($storeid, $volname) = parse_volume_id
($volid);
566 my $scfg = storage_config
($cfg, $storeid);
568 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
570 activate_storage
($cfg, $storeid);
574 # lock shared storage
575 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
577 my ($vtype, $name, $vmid, undef, undef, $isBase) =
578 $plugin->parse_volname($volname);
580 my $vollist = $plugin->list_images($storeid, $scfg);
581 foreach my $info (@$vollist) {
582 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
583 my $basename = undef;
584 my $basevmid = undef;
587 (undef, undef, undef, $basename, $basevmid) =
588 $plugin->parse_volname($tmpvolname);
591 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
592 die "base volume '$volname' is still in use " .
593 "(use by '$tmpvolname')\n";
597 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase);
600 return if !$cleanup_worker;
602 my $rpcenv = PVE
::RPCEnvironment
::get
();
603 my $authuser = $rpcenv->get_user();
605 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
608 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
610 my ($cfg, $storeid, $tt) = @_;
612 die "unknown template type '$tt'\n"
613 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
615 my $ids = $cfg->{ids
};
617 storage_check_enabled
($cfg, $storeid) if ($storeid);
623 foreach my $sid (keys %$ids) {
624 next if $storeid && $storeid ne $sid;
626 my $scfg = $ids->{$sid};
627 my $type = $scfg->{type
};
629 next if !storage_check_enabled
($cfg, $sid, undef, 1);
631 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
632 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
633 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
635 activate_storage
($cfg, $sid);
638 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
640 my $path = $plugin->get_subdir($scfg, $tt);
642 foreach my $fn (<$path/*>) {
647 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
649 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
651 } elsif ($tt eq 'vztmpl') {
652 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
654 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
656 } elsif ($tt eq 'backup') {
657 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
659 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
662 $info->{size
} = -s
$fn;
664 push @{$res->{$sid}}, $info;
669 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
677 my ($cfg, $storeid, $vmid, $vollist) = @_;
679 my $ids = $cfg->{ids
};
681 storage_check_enabled
($cfg, $storeid) if ($storeid);
685 # prepare/activate/refresh all storages
687 my $storage_list = [];
689 foreach my $volid (@$vollist) {
690 my ($sid, undef) = parse_volume_id
($volid);
691 next if !defined($ids->{$sid});
692 next if !storage_check_enabled
($cfg, $sid, undef, 1);
693 push @$storage_list, $sid;
696 foreach my $sid (keys %$ids) {
697 next if $storeid && $storeid ne $sid;
698 next if !storage_check_enabled
($cfg, $sid, undef, 1);
699 push @$storage_list, $sid;
705 activate_storage_list
($cfg, $storage_list, $cache);
707 foreach my $sid (keys %$ids) {
708 next if $storeid && $storeid ne $sid;
709 next if !storage_check_enabled
($cfg, $sid, undef, 1);
711 my $scfg = $ids->{$sid};
712 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
713 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
714 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
722 my $filename = "/sys/kernel/uevent_seqnum";
725 if (my $fh = IO
::File-
>new($filename, "r")) {
727 if ($line =~ m/^(\d+)$/) {
735 sub activate_storage
{
736 my ($cfg, $storeid, $cache) = @_;
738 $cache = {} if !$cache;
740 my $scfg = storage_check_enabled
($cfg, $storeid);
742 return if $cache->{activated
}->{$storeid};
744 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
746 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
749 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
750 activate_storage
($cfg, $baseid, $cache);
753 if (!$plugin->check_connection($storeid, $scfg)) {
754 die "storage '$storeid' is not online\n";
757 $plugin->activate_storage($storeid, $scfg, $cache);
759 my $newseq = uevent_seqnum
();
761 # only call udevsettle if there are events
762 if ($newseq > $cache->{uevent_seqnum
}) {
764 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
765 $cache->{uevent_seqnum
} = $newseq;
768 $cache->{activated
}->{$storeid} = 1;
771 sub activate_storage_list
{
772 my ($cfg, $storeid_list, $cache) = @_;
774 $cache = {} if !$cache;
776 foreach my $storeid (@$storeid_list) {
777 activate_storage
($cfg, $storeid, $cache);
781 sub deactivate_storage
{
782 my ($cfg, $storeid) = @_;
784 my $scfg = storage_config
($cfg, $storeid);
785 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
788 $plugin->deactivate_storage($storeid, $scfg, $cache);
791 sub activate_volumes
{
792 my ($cfg, $vollist, $exclusive) = @_;
794 return if !($vollist && scalar(@$vollist));
796 my $storagehash = {};
797 foreach my $volid (@$vollist) {
798 my ($storeid, undef) = parse_volume_id
($volid);
799 $storagehash->{$storeid} = 1;
804 activate_storage_list
($cfg, [keys %$storagehash], $cache);
806 foreach my $volid (@$vollist) {
807 my ($storeid, $volname) = parse_volume_id
($volid);
808 my $scfg = storage_config
($cfg, $storeid);
809 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
810 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
814 sub deactivate_volumes
{
815 my ($cfg, $vollist) = @_;
817 return if !($vollist && scalar(@$vollist));
822 foreach my $volid (@$vollist) {
823 my ($storeid, $volname) = parse_volume_id
($volid);
825 my $scfg = storage_config
($cfg, $storeid);
826 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
829 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
833 push @errlist, $volid;
837 die "volume deativation failed: " . join(' ', @errlist)
842 my ($cfg, $content) = @_;
844 my $ids = $cfg->{ids
};
849 foreach my $storeid (keys %$ids) {
851 next if $content && !$ids->{$storeid}->{content
}->{$content};
853 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
855 my $type = $ids->{$storeid}->{type
};
857 $info->{$storeid} = {
862 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
863 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
867 push @$slist, $storeid;
872 foreach my $storeid (keys %$ids) {
873 my $scfg = $ids->{$storeid};
874 next if !$info->{$storeid};
876 eval { activate_storage
($cfg, $storeid, $cache); };
882 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
883 my ($total, $avail, $used, $active);
884 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
887 $info->{$storeid}->{total
} = $total;
888 $info->{$storeid}->{avail
} = $avail;
889 $info->{$storeid}->{used
} = $used;
890 $info->{$storeid}->{active
} = $active;
899 my $packed_ip = gethostbyname($server);
900 if (defined $packed_ip) {
901 return inet_ntoa
($packed_ip);
907 my ($server_in) = @_;
910 if (!($server = resolv_server
($server_in))) {
911 die "unable to resolve address for server '${server_in}'\n";
914 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
917 run_command
($cmd, outfunc
=> sub {
920 # note: howto handle white spaces in export path??
921 if ($line =~ m!^(/\S+)\s+(.+)$!) {
931 my $cmd = ['zpool', 'list', '-H', '-o', 'name,size,free'];
934 run_command
($cmd, outfunc
=> sub {
937 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
938 my ($pool, $size_str, $free_str) = ($1, $2, $3);
939 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
940 my $free = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($free_str);
941 push @$res, { pool
=> $pool, size
=> $size, free
=> $free };
949 my ($portal, $noerr) = @_;
951 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
955 if (my $ip = resolv_server
($server)) {
957 return $port ?
"$server:$port" : $server;
960 return undef if $noerr;
962 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
965 # idea is from usbutils package (/usr/bin/usb-devices) script
966 sub __scan_usb_device
{
967 my ($res, $devpath, $parent, $level) = @_;
969 return if ! -d
$devpath;
970 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
971 my $port = $level ?
int($1 - 1) : 0;
973 my $busnum = int(file_read_firstline
("$devpath/busnum"));
974 my $devnum = int(file_read_firstline
("$devpath/devnum"));
981 speed
=> file_read_firstline
("$devpath/speed"),
982 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
983 vendid
=> file_read_firstline
("$devpath/idVendor"),
984 prodid
=> file_read_firstline
("$devpath/idProduct"),
988 my $usbpath = $devpath;
989 $usbpath =~ s
|^.*/\d
+\
-||;
990 $d->{usbpath
} = $usbpath;
993 my $product = file_read_firstline
("$devpath/product");
994 $d->{product
} = $product if $product;
996 my $manu = file_read_firstline
("$devpath/manufacturer");
997 $d->{manufacturer
} = $manu if $manu;
999 my $serial => file_read_firstline
("$devpath/serial");
1000 $d->{serial
} = $serial if $serial;
1004 foreach my $subdev (<$devpath/$busnum-*>) {
1005 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1006 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1015 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1016 __scan_usb_device
($devlist, $device, 0, 0);
1023 my ($portal_in) = @_;
1026 if (!($portal = resolv_portal
($portal_in))) {
1027 die "unable to parse/resolve portal address '${portal_in}'\n";
1030 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1033 sub storage_default_format
{
1034 my ($cfg, $storeid) = @_;
1036 my $scfg = storage_config
($cfg, $storeid);
1038 return PVE
::Storage
::Plugin
::default_format
($scfg);
1041 sub vgroup_is_used
{
1042 my ($cfg, $vgname) = @_;
1044 foreach my $storeid (keys %{$cfg->{ids
}}) {
1045 my $scfg = storage_config
($cfg, $storeid);
1046 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1054 sub target_is_used
{
1055 my ($cfg, $target) = @_;
1057 foreach my $storeid (keys %{$cfg->{ids
}}) {
1058 my $scfg = storage_config
($cfg, $storeid);
1059 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1067 sub volume_is_used
{
1068 my ($cfg, $volid) = @_;
1070 foreach my $storeid (keys %{$cfg->{ids
}}) {
1071 my $scfg = storage_config
($cfg, $storeid);
1072 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1080 sub storage_is_used
{
1081 my ($cfg, $storeid) = @_;
1083 foreach my $sid (keys %{$cfg->{ids
}}) {
1084 my $scfg = storage_config
($cfg, $sid);
1085 next if !$scfg->{base
};
1086 my ($st) = parse_volume_id
($scfg->{base
});
1087 return 1 if $st && $st eq $storeid;
1094 my ($list, $func) = @_;
1098 foreach my $sid (keys %$list) {
1099 foreach my $info (@{$list->{$sid}}) {
1100 my $volid = $info->{volid
};
1101 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1102 if ($sid1 && $sid1 eq $sid) {
1103 &$func ($volid, $sid, $info);
1105 warn "detected strange volid '$volid' in volume list for '$sid'\n";