]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
14 use Getopt
::Long
qw(GetOptionsFromArray);
19 use PVE
::Tools
qw(run_command file_read_firstline trim);
20 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
21 use PVE
::Exception
qw(raise_param_exc);
25 my $ISCSIADM = '/usr/bin/iscsiadm';
26 my $UDEVADM = '/sbin/udevadm';
28 $ISCSIADM = undef if ! -X
$ISCSIADM;
30 # fixme: always_call_parser => 1 ??
31 cfs_register_file
('storage.cfg',
35 # generic utility function
38 return cfs_read_file
("storage.cfg");
41 sub check_iscsi_support
{
45 my $msg = "no iscsi support - please install open-iscsi";
47 warn "warning: $msg\n";
57 sub load_stable_scsi_paths
{
59 my $stable_paths = {};
61 my $stabledir = "/dev/disk/by-id";
63 if (my $dh = IO
::Dir-
>new($stabledir)) {
64 while (defined(my $tmp = $dh->read)) {
65 # exclude filenames with part in name (same disk but partitions)
66 # use only filenames with scsi(with multipath i have the same device
67 # with dm-uuid-mpath , dm-name and scsi in name)
68 if($tmp !~ m/-part\d+$/ && $tmp =~ m/^scsi-/) {
69 my $path = "$stabledir/$tmp";
70 my $bdevdest = readlink($path);
71 if ($bdevdest && $bdevdest =~ m
|^../../([^/]+)|) {
72 $stable_paths->{$1}=$tmp;
82 my ($dir, $regex) = @_;
84 my $dh = IO
::Dir-
>new ($dir);
85 return wantarray ?
() : undef if !$dh;
87 while (defined(my $tmp = $dh->read)) {
88 if (my @res = $tmp =~ m/^($regex)$/) {
90 return wantarray ?
@res : $tmp;
95 return wantarray ?
() : undef;
98 sub dir_glob_foreach
{
99 my ($dir, $regex, $func) = @_;
101 my $dh = IO
::Dir-
>new ($dir);
103 while (defined(my $tmp = $dh->read)) {
104 if (my @res = $tmp =~ m/^($regex)$/) {
111 sub read_proc_mounts
{
113 local $/; # enable slurp mode
116 if (my $fd = IO
::File-
>new ("/proc/mounts", "r")) {
124 # PVE::Storage utility functions
126 sub lock_storage_config
{
127 my ($code, $errmsg) = @_;
129 cfs_lock_file
("storage.cfg", undef, $code);
132 $errmsg ?
die "$errmsg: $err" : die $err;
141 content
=> 'content',
149 options
=> 'options',
152 my $required_config = {
154 nfs
=> ['path', 'server', 'export'],
156 iscsi
=> ['portal', 'target'],
161 nfs
=> ['path', 'server', 'export'],
162 lvm
=> ['vgname', 'base'],
163 iscsi
=> ['portal', 'target'],
166 my $default_config = {
172 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, backup
=> 1, none
=> 1 },
173 { images
=> 1, rootdir
=> 1 }],
174 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
184 content
=> [ { images
=> 1, iso
=> 1, backup
=> 1},
186 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
194 content
=> [ {images
=> 1}, { images
=> 1 }],
203 content
=> [ {images
=> 1, none
=> 1}, { images
=> 1 }],
207 sub valid_content_types
{
210 my $def = $default_config->{$stype};
214 return $def->{content
}->[0];
217 sub content_hash_to_string
{
221 foreach my $ct (keys %$hash) {
222 push @cta, $ct if $hash->{$ct};
225 return join(',', @cta);
228 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
230 my ($path, $noerr) = @_;
232 # fixme: exclude more shell meta characters?
233 # we need absolute paths
234 if ($path !~ m
|^/[^;\
(\
)]+|) {
235 return undef if $noerr;
236 die "value does not look like a valid absolute path\n";
241 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
243 my ($server, $noerr) = @_;
245 # fixme: use better regex ?
247 if ($server !~ m/^[[:alnum:]\-\.]+$/) {
248 return undef if $noerr;
249 die "value does not look like a valid server name or IP address\n";
254 PVE
::JSONSchema
::register_format
('pve-storage-portal', \
&verify_portal
);
256 my ($portal, $noerr) = @_;
258 # IP with optional port
259 if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
260 return undef if $noerr;
261 die "value does not look like a valid portal address\n";
266 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
267 sub verify_portal_dns
{
268 my ($portal, $noerr) = @_;
270 # IP or DNS name with optional port
271 if ($portal !~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[[:alnum:]\-\.]+)(:\d+)?$/) {
272 return undef if $noerr;
273 die "value does not look like a valid portal address\n";
278 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
280 my ($ct, $noerr) = @_;
282 my $valid_content = valid_content_types
('dir'); # dir includes all types
284 if (!$valid_content->{$ct}) {
285 return undef if $noerr;
286 die "invalid content type '$ct'\n";
292 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
294 my ($fmt, $noerr) = @_;
296 if ($fmt !~ m/(raw|qcow2|vmdk)/) {
297 return undef if $noerr;
298 die "invalid format '$fmt'\n";
304 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
306 my ($value, $noerr) = @_;
308 # mount options (see man fstab)
309 if ($value !~ m/^\S+$/) {
310 return undef if $noerr;
311 die "invalid options '$value'\n";
318 my ($stype, $ct, $key, $value, $storeid, $noerr) = @_;
320 my $def = $default_config->{$stype};
322 if (!$def) { # should not happen
323 return undef if $noerr;
324 die "unknown storage type '$stype'\n";
327 if (!defined($def->{$key})) {
328 return undef if $noerr;
329 die "unexpected property\n";
332 if (!defined ($value)) {
333 return undef if $noerr;
334 die "got undefined value\n";
337 if ($value =~ m/[\n\r]/) {
338 return undef if $noerr;
339 die "property contains a line feed\n";
343 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
344 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
345 return undef if $noerr;
346 die "type check ('boolean') failed - got '$value'\n";
347 } elsif ($ct eq 'options') {
348 return verify_options
($value, $noerr);
349 } elsif ($ct eq 'path') {
350 return verify_path
($value, $noerr);
351 } elsif ($ct eq 'server') {
352 return verify_server
($value, $noerr);
353 } elsif ($ct eq 'vgname') {
354 return parse_lvm_name
($value, $noerr);
355 } elsif ($ct eq 'portal') {
356 return verify_portal
($value, $noerr);
357 } elsif ($ct eq 'nodes') {
360 foreach my $node (PVE
::Tools
::split_list
($value)) {
361 if (PVE
::JSONSchema
::pve_verify_node_name
($node, $noerr)) {
366 # no node restrictions for local storage
367 if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
368 return undef if $noerr;
369 die "storage '$storeid' does not allow node restrictions\n";
373 } elsif ($ct eq 'target') {
375 } elsif ($ct eq 'string') {
377 } elsif ($ct eq 'format') {
378 my $valid_formats = $def->{format
}->[0];
380 if (!$valid_formats->{$value}) {
381 return undef if $noerr;
382 die "storage does not support format '$value'\n";
387 } elsif ($ct eq 'content') {
388 my $valid_content = $def->{content
}->[0];
392 foreach my $c (PVE
::Tools
::split_list
($value)) {
393 if (!$valid_content->{$c}) {
394 return undef if $noerr;
395 die "storage does not support content type '$c'\n";
400 # only local storage may have several content types
401 if ($res->{none
} || !($storeid && $storeid eq 'local')) {
402 if (scalar (keys %$res) > 1) {
403 return undef if $noerr;
404 die "storage does not support multiple content types\n";
409 } elsif ($ct eq 'volume') {
410 return $value if parse_volume_id
($value, $noerr);
413 return undef if $noerr;
414 die "type check not implemented - internal error\n";
418 my ($filename, $raw) = @_;
422 my $digest = Digest
::SHA1
::sha1_hex
(defined($raw) ?
$raw : '');
426 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
429 next if $line =~ m/^\#/;
430 next if $line =~ m/^\s*$/;
432 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
437 if (!parse_storage_id
($storeid, 1)) {
439 warn "ignoring storage '$storeid' - (illegal characters)\n";
440 } elsif (!$default_config->{$type}) {
442 warn "ignoring storage '$storeid' (unsupported type '$type')\n";
444 $ids->{$storeid}->{type
} = $type;
445 $ids->{$storeid}->{priority
} = $pri++;
448 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
451 next if $line =~ m/^\#/;
452 last if $line =~ m/^\s*$/;
454 next if $ignore; # skip
456 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
457 my ($k, $v) = ($1, $3);
458 if (my $ct = $confvars->{$k}) {
459 $v = 1 if $ct eq 'bool' && !defined($v);
461 $ids->{$storeid}->{$k} = check_type
($type, $ct, $k, $v, $storeid);
463 warn "storage '$storeid' - unable to parse value of '$k': $@" if $@;
465 warn "storage '$storeid' - unable to parse value of '$k'\n";
469 warn "storage '$storeid' - ignore config line: $line\n";
473 warn "ignore config line: $line\n";
477 # make sure we have a reasonable 'local:' storage
478 # openvz expects things to be there
479 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
480 $ids->{local}->{path
} ne '/var/lib/vz') {
484 path
=> '/var/lib/vz',
485 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
489 # we always need this for OpenVZ
490 $ids->{local}->{content
}->{rootdir
} = 1;
491 $ids->{local}->{content
}->{vztmpl
} = 1;
492 delete ($ids->{local}->{disable
});
494 # remove node restrictions for local storage
495 delete($ids->{local}->{nodes
});
497 foreach my $storeid (keys %$ids) {
498 my $d = $ids->{$storeid};
500 my $req_keys = $required_config->{$d->{type
}};
501 foreach my $k (@$req_keys) {
502 if (!defined ($d->{$k})) {
503 warn "ignoring storage '$storeid' - missing value " .
504 "for required option '$k'\n";
505 delete $ids->{$storeid};
510 my $def = $default_config->{$d->{type
}};
512 if ($def->{content
}) {
513 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
516 if ($d->{type
} eq 'iscsi' || $d->{type
} eq 'nfs') {
521 my $cfg = { ids
=> $ids, digest
=> $digest};
527 my ($storeid, $stype, $param, $create) = @_;
529 my $settings = { type
=> $stype };
531 die "unknown storage type '$stype'\n"
532 if !$default_config->{$stype};
534 foreach my $opt (keys %$param) {
535 my $value = $param->{$opt};
537 my $ct = $confvars->{$opt};
538 if (defined($value)) {
540 $settings->{$opt} = check_type
($stype, $ct, $opt, $value, $storeid);
542 raise_param_exc
({ $opt => $@ }) if $@;
544 raise_param_exc
({ $opt => "got undefined value" });
549 my $req_keys = $required_config->{$stype};
550 foreach my $k (@$req_keys) {
552 if ($stype eq 'nfs' && !$settings->{path
}) {
553 $settings->{path
} = "/mnt/pve/$storeid";
556 # check if we have a value for all required options
557 if (!defined ($settings->{$k})) {
558 raise_param_exc
({ $k => "property is missing and it is not optional" });
562 my $fixed_keys = $fixed_config->{$stype};
563 foreach my $k (@$fixed_keys) {
565 # only allow to change non-fixed values
567 if (defined ($settings->{$k})) {
568 raise_param_exc
({$k => "can't change value (fixed parameter)"});
576 sub cluster_lock_storage
{
577 my ($storeid, $shared, $timeout, $func, @param) = @_;
581 my $lockid = "pve-storage-$storeid";
582 my $lockdir = "/var/lock/pve-manager";
584 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
587 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
594 my ($cfg, $storeid, $noerr) = @_;
596 die "no storage id specified\n" if !$storeid;
598 my $scfg = $cfg->{ids
}->{$storeid};
600 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
605 sub storage_check_node
{
606 my ($cfg, $storeid, $node, $noerr) = @_;
608 my $scfg = storage_config
($cfg, $storeid);
610 if ($scfg->{nodes
}) {
611 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
612 if (!$scfg->{nodes
}->{$node}) {
613 die "storage '$storeid' is not available on node '$node'" if !$noerr;
621 sub storage_check_enabled
{
622 my ($cfg, $storeid, $node, $noerr) = @_;
624 my $scfg = storage_config
($cfg, $storeid);
626 if ($scfg->{disable
}) {
627 die "storage '$storeid' is disabled\n" if !$noerr;
631 return storage_check_node
($cfg, $storeid, $node, $noerr);
637 my $ids = $cfg->{ids
};
639 my @sa = sort {$ids->{$a}->{priority
} <=> $ids->{$b}->{priority
}} keys %$ids;
644 sub assert_if_modified
{
645 my ($cfg, $digest) = @_;
647 if ($digest && ($cfg->{digest
} ne $digest)) {
648 die "detected modified storage configuration - try again\n";
652 sub sprint_config_line
{
655 my $ct = $confvars->{$k};
658 return $v ?
"\t$k\n" : '';
659 } elsif ($ct eq 'nodes') {
660 my $nlist = join(',', keys(%$v));
661 return $nlist ?
"\tnodes $nlist\n" : '';
662 } elsif ($ct eq 'content') {
663 my $clist = content_hash_to_string
($v);
665 return "\t$k $clist\n";
667 return "\t$k none\n";
675 my ($filename, $cfg) = @_;
679 my $ids = $cfg->{ids
};
682 foreach my $storeid (keys %$ids) {
683 my $pri = $ids->{$storeid}->{priority
};
684 $maxpri = $pri if $pri && $pri > $maxpri;
686 foreach my $storeid (keys %$ids) {
687 if (!defined ($ids->{$storeid}->{priority
})) {
688 $ids->{$storeid}->{priority
} = ++$maxpri;
692 foreach my $storeid (sort {$ids->{$a}->{priority
} <=> $ids->{$b}->{priority
}} keys %$ids) {
693 my $scfg = $ids->{$storeid};
694 my $type = $scfg->{type
};
695 my $def = $default_config->{$type};
697 die "unknown storage type '$type'\n" if !$def;
699 my $data = "$type: $storeid\n";
701 $data .= "\tdisable\n" if $scfg->{disable
};
703 my $done_hash = { disable
=> 1};
704 foreach my $k (@{$required_config->{$type}}) {
705 $done_hash->{$k} = 1;
706 my $v = $ids->{$storeid}->{$k};
707 die "storage '$storeid' - missing value for required option '$k'\n"
709 $data .= sprint_config_line
($k, $v);
712 foreach my $k (keys %$def) {
713 next if defined ($done_hash->{$k});
714 if (defined (my $v = $ids->{$storeid}->{$k})) {
715 $data .= sprint_config_line
($k, $v);
726 my ($cfg, $storeid, $vmid) = @_;
728 my $path = $cfg->{ids
}->{$storeid}->{path
};
729 return $vmid ?
"$path/images/$vmid" : "$path/images";
733 my ($cfg, $storeid) = @_;
735 my $isodir = $cfg->{ids
}->{$storeid}->{path
};
736 $isodir .= '/template/iso' if $storeid eq 'local';
742 my ($cfg, $storeid) = @_;
744 my $tmpldir = $cfg->{ids
}->{$storeid}->{path
};
745 $tmpldir .= '/template/cache' if $storeid eq 'local';
751 my ($cfg, $storeid) = @_;
753 my $dir = $cfg->{ids
}->{$storeid}->{path
};
754 $dir .= '/dump' if $storeid eq 'local';
759 # iscsi utility functions
761 sub iscsi_session_list
{
763 check_iscsi_support
();
765 my $cmd = [$ISCSIADM, '--mode', 'session'];
769 run_command
($cmd, outfunc
=> sub {
772 if ($line =~ m/^tcp:\s+\[(\S+)\]\s+\S+\s+(\S+)\s*$/) {
773 my ($session, $target) = ($1, $2);
774 # there can be several sessions per target (multipath)
775 push @{$res->{$target}}, $session;
783 sub iscsi_test_portal
{
786 my ($server, $port) = split(':', $portal);
787 my $p = Net
::Ping-
>new("tcp", 2);
788 $p->port_number($port || 3260);
789 return $p->ping($server);
792 sub iscsi_discovery
{
795 check_iscsi_support
();
797 my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets',
798 '--portal', $portal];
802 return $res if !iscsi_test_portal
($portal); # fixme: raise exception here?
804 run_command
($cmd, outfunc
=> sub {
807 if ($line =~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\,\S+\s+(\S+)\s*$/) {
810 # one target can have more than one portal (multipath).
811 push @{$res->{$target}}, $portal;
819 my ($target, $portal_in) = @_;
821 check_iscsi_support
();
823 eval { iscsi_discovery
($portal_in); };
826 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--login'];
831 my ($target, $portal) = @_;
833 check_iscsi_support
();
835 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--logout'];
839 my $rescan_filename = "/var/run/pve-iscsi-rescan.lock";
841 sub iscsi_session_rescan
{
842 my $session_list = shift;
844 check_iscsi_support
();
846 my $rstat = stat ($rescan_filename);
849 if (my $fh = IO
::File-
>new ($rescan_filename, "a")) {
850 utime undef, undef, $fh;
854 my $atime = $rstat->atime;
855 my $tdiff = time() - $atime;
856 # avoid frequent rescans
857 return if !($tdiff < 0 || $tdiff > 10);
858 utime undef, undef, $rescan_filename;
861 foreach my $session (@$session_list) {
862 my $cmd = [$ISCSIADM, '--mode', 'session', '-r', $session, '-R'];
863 eval { run_command
($cmd, outfunc
=> sub {}); };
868 sub iscsi_device_list
{
872 my $dirname = '/sys/class/iscsi_session';
874 my $stable_paths = load_stable_scsi_paths
();
876 dir_glob_foreach
($dirname, 'session(\d+)', sub {
877 my ($ent, $session) = @_;
879 my $target = file_read_firstline
("$dirname/$ent/targetname");
882 my (undef, $host) = dir_glob_regex
("$dirname/$ent/device", 'target(\d+):.*');
883 return if !defined($host);
885 dir_glob_foreach
("/sys/bus/scsi/devices", "$host:" . '(\d+):(\d+):(\d+)', sub {
886 my ($tmp, $channel, $id, $lun) = @_;
888 my $type = file_read_firstline
("/sys/bus/scsi/devices/$tmp/type");
889 return if !defined($type) || $type ne '0'; # list disks only
892 if (-d
"/sys/bus/scsi/devices/$tmp/block") { # newer kernels
893 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp/block/", '([A-Za-z]\S*)');
895 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp", 'block:(\S+)');
900 if (-d
"/sys/block/$bdev/holders") {
901 my $multipathdev = dir_glob_regex
("/sys/block/$bdev/holders", '[A-Za-z]\S*');
902 $bdev = $multipathdev if $multipathdev;
905 my $blockdev = $stable_paths->{$bdev};
906 return if !$blockdev;
908 my $size = file_read_firstline
("/sys/block/$bdev/size");
911 my $volid = "$channel.$id.$lun.$blockdev";
913 $res->{$target}->{$volid} = {
915 'size' => int($size / 2),
916 'vmid' => 0, # not assigned to any vm
917 'channel' => int($channel),
922 #print "TEST: $target $session $host,$bus,$tg,$lun $blockdev\n";
930 # library implementation
933 PVE
::JSONSchema
::register_format
('pve-storage-id', \
&parse_storage_id
);
934 sub parse_storage_id
{
935 my ($storeid, $noerr) = @_;
937 if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
938 return undef if $noerr;
939 die "storage ID '$storeid' contains illegal characters\n";
944 PVE
::JSONSchema
::register_standard_option
('pve-storage-id', {
945 description
=> "The storage identifier.",
946 type
=> 'string', format
=> 'pve-storage-id',
949 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
951 my ($name, $noerr) = @_;
953 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
954 return undef if $noerr;
955 die "lvm name '$name' contains illegal characters\n";
964 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
969 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
970 sub parse_volume_id
{
971 my ($volid, $noerr) = @_;
973 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
974 return wantarray ?
($1, $2) : $1;
976 return undef if $noerr;
977 die "unable to parse volume ID '$volid'\n";
983 if ($name =~ m!^([^/\s]+\.(raw|qcow2|vmdk))$!) {
987 die "unable to parse volume filename '$name'\n";
990 sub parse_volname_dir
{
993 if ($volname =~ m!^(\d+)/(\S+)$!) {
994 my ($vmid, $name) = ($1, $2);
995 parse_name_dir
($name);
996 return ('image', $name, $vmid);
997 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
999 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
1000 return ('vztmpl', $1);
1002 die "unable to parse directory volume name '$volname'\n";
1005 sub parse_volname_lvm
{
1006 my $volname = shift;
1008 parse_lvm_name
($volname);
1010 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
1014 die "unable to parse lvm volume name '$volname'\n";
1017 sub parse_volname_iscsi
{
1018 my $volname = shift;
1020 if ($volname =~ m!^\d+\.\d+\.\d+\.(\S+)$!) {
1025 die "unable to parse iscsi volume name '$volname'\n";
1028 # try to map a filesystem path to a volume identifier
1029 sub path_to_volume_id
{
1030 my ($cfg, $path) = @_;
1032 my $ids = $cfg->{ids
};
1034 my ($sid, $volname) = parse_volume_id
($path, 1);
1036 if ($ids->{$sid} && (my $type = $ids->{$sid}->{type
})) {
1037 if ($type eq 'dir' || $type eq 'nfs') {
1038 my ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1039 return ($vtype, $path);
1045 $path = abs_path
($path);
1047 foreach my $sid (keys %$ids) {
1048 my $type = $ids->{$sid}->{type
};
1049 next if !($type eq 'dir' || $type eq 'nfs');
1051 my $imagedir = get_image_dir
($cfg, $sid);
1052 my $isodir = get_iso_dir
($cfg, $sid);
1053 my $tmpldir = get_vztmpl_dir
($cfg, $sid);
1054 my $backupdir = get_backup_dir
($cfg, $sid);
1056 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
1059 return ('image', "$sid:$vmid/$name");
1060 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
1062 return ('iso', "$sid:iso/$name");
1063 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
1065 return ('vztmpl', "$sid:vztmpl/$name");
1066 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tgz))$!) {
1068 return ('iso', "$sid:backup/$name");
1072 # can't map path to volume id
1077 my ($cfg, $volid) = @_;
1079 my ($storeid, $volname) = parse_volume_id
($volid);
1081 my $scfg = storage_config
($cfg, $storeid);
1086 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1087 my ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1090 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1091 my $isodir = get_iso_dir
($cfg, $storeid);
1092 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1093 my $backupdir = get_backup_dir
($cfg, $storeid);
1095 if ($vtype eq 'image') {
1096 $path = "$imagedir/$name";
1097 } elsif ($vtype eq 'iso') {
1098 $path = "$isodir/$name";
1099 } elsif ($vtype eq 'vztmpl') {
1100 $path = "$tmpldir/$name";
1101 } elsif ($vtype eq 'backup') {
1102 $path = "$backupdir/$name";
1104 die "should not be reached";
1107 } elsif ($scfg->{type
} eq 'lvm') {
1109 my $vg = $scfg->{vgname
};
1111 my ($name, $vmid) = parse_volname_lvm
($volname);
1114 $path = "/dev/$vg/$name";
1116 } elsif ($scfg->{type
} eq 'iscsi') {
1117 my $byid = parse_volname_iscsi
($volname);
1118 $path = "/dev/disk/by-id/$byid";
1120 die "unknown storage type '$scfg->{type}'";
1123 return wantarray ?
($path, $owner) : $path;
1126 sub storage_migrate
{
1127 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
1129 my ($storeid, $volname) = parse_volume_id
($volid);
1130 $target_volname = $volname if !$target_volname;
1132 my $scfg = storage_config
($cfg, $storeid);
1134 # no need to migrate shared content
1135 return if $storeid eq $target_storeid && $scfg->{shared
};
1137 my $tcfg = storage_config
($cfg, $target_storeid);
1139 my $target_volid = "${target_storeid}:${target_volname}";
1141 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
1143 # blowfish is a fast block cipher, much faster then 3des
1144 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
1145 my $ssh = "/usr/bin/ssh $sshoptions";
1147 local $ENV{RSYNC_RSH
} = $ssh;
1149 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1150 if ($tcfg->{type
} eq 'dir' || $tcfg->{type
} eq 'nfs') {
1152 my $src = path
($cfg, $volid);
1153 my $dst = path
($cfg, $target_volid);
1155 my $dirname = dirname
($dst);
1157 if ($tcfg->{shared
}) { # we can do a local copy
1159 run_command
(['/bin/mkdir', '-p', $dirname]);
1161 run_command
(['/bin/cp', $src, $dst]);
1165 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1166 '/bin/mkdir', '-p', $dirname]);
1168 # we use rsync with --sparse, so we can't use --inplace,
1169 # so we remove file on the target if it already exists to
1171 my ($size, $format) = file_size_info
($src);
1172 if ($format && ($format eq 'raw') && $size) {
1173 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1178 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
1179 $src, "root\@${target_host}:$dst"];
1183 run_command
($cmd, outfunc
=> sub {
1186 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
1187 if ($2 > $percent) {
1189 print "rsync status: $1\n";
1202 die "$errstr - target type '$tcfg->{type}' not implemented\n";
1206 die "$errstr - source type '$scfg->{type}' not implemented\n";
1211 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
1213 die "no storage id specified\n" if !$storeid;
1215 parse_storage_id
($storeid);
1217 my $scfg = storage_config
($cfg, $storeid);
1219 die "no VMID specified\n" if !$vmid;
1221 $vmid = parse_vmid
($vmid);
1223 my $defformat = storage_default_format
($cfg, $storeid);
1225 $fmt = $defformat if !$fmt;
1227 activate_storage
($cfg, $storeid);
1229 # lock shared storage
1230 return cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1232 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1234 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1240 for (my $i = 1; $i < 100; $i++) {
1241 my @gr = <$imagedir/vm-$vmid-disk
-$i.*>;
1243 $name = "vm-$vmid-disk-$i.$fmt";
1249 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1252 my (undef, $tmpfmt) = parse_name_dir
($name);
1254 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
1257 my $path = "$imagedir/$name";
1259 die "disk image '$path' already exists\n" if -f
$path;
1261 run_command
("/usr/bin/qemu-img create -f $fmt '$path' ${size}K",
1262 errmsg
=> "unable to create image");
1264 return "$storeid:$vmid/$name";
1266 } elsif ($scfg->{type
} eq 'lvm') {
1268 die "unsupported format '$fmt'" if $fmt ne 'raw';
1270 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
1271 if $name && $name !~ m/^vm-$vmid-/;
1273 my $vgs = lvm_vgs
();
1275 my $vg = $scfg->{vgname
};
1277 die "no such volume gruoup '$vg'\n" if !defined ($vgs->{$vg});
1279 my $free = int ($vgs->{$vg}->{free
});
1281 die "not enough free space ($free < $size)\n" if $free < $size;
1284 my $lvs = lvm_lvs
($vg);
1286 for (my $i = 1; $i < 100; $i++) {
1287 my $tn = "vm-$vmid-disk-$i";
1288 if (!defined ($lvs->{$vg}->{$tn})) {
1295 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1298 my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
1302 return "$storeid:$name";
1304 } elsif ($scfg->{type
} eq 'iscsi') {
1305 die "can't allocate space in iscsi storage\n";
1307 die "unknown storage type '$scfg->{type}'";
1313 my ($cfg, $volid) = @_;
1315 my ($storeid, $volname) = parse_volume_id
($volid);
1317 my $scfg = storage_config
($cfg, $storeid);
1319 activate_storage
($cfg, $storeid);
1321 # lock shared storage
1322 cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1324 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1325 my $path = path
($cfg, $volid);
1328 warn "disk image '$path' does not exists\n";
1332 } elsif ($scfg->{type
} eq 'lvm') {
1334 my $vg = $scfg->{vgname
};
1336 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
1339 } elsif ($scfg->{type
} eq 'iscsi') {
1340 die "can't free space in iscsi storage\n";
1342 die "unknown storage type '$scfg->{type}'";
1347 # lvm utility functions
1352 die "no device specified" if !$device;
1356 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
1357 run_command
($cmd, outfunc
=> sub {
1359 $has_label = 1 if $line =~ m/LVM2/;
1362 return undef if !$has_label;
1364 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
1365 '--unbuffered', '--nosuffix', '--options',
1366 'pv_name,pv_size,vg_name,pv_uuid', $device];
1369 run_command
($cmd, outfunc
=> sub {
1372 $line = trim
($line);
1374 my ($pvname, $size, $vgname, $uuid) = split (':', $line);
1376 die "found multiple pvs entries for device '$device'\n"
1390 sub clear_first_sector
{
1393 if (my $fh = IO
::File-
>new ($dev, "w")) {
1400 sub lvm_create_volume_group
{
1401 my ($device, $vgname, $shared) = @_;
1403 my $res = lvm_pv_info
($device);
1405 if ($res->{vgname
}) {
1406 return if $res->{vgname
} eq $vgname; # already created
1407 die "device '$device' is already used by volume group '$res->{vgname}'\n";
1410 clear_first_sector
($device); # else pvcreate fails
1412 # we use --metadatasize 250k, which reseults in "pe_start = 512"
1413 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
1414 my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
1418 $cmd = ['/sbin/vgcreate', $vgname, $device];
1419 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
1426 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
1427 '--unbuffered', '--nosuffix', '--options',
1428 'vg_name,vg_size,vg_free'];
1431 run_command
($cmd, outfunc
=> sub {
1434 $line = trim
($line);
1436 my ($name, $size, $free) = split (':', $line);
1438 $vgs->{$name} = { size
=> int ($size), free
=> int ($free) };
1447 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
1448 '--unbuffered', '--nosuffix', '--options',
1449 'vg_name,lv_name,lv_size,uuid,tags'];
1451 push @$cmd, $vgname if $vgname;
1454 run_command
($cmd, outfunc
=> sub {
1457 $line = trim
($line);
1459 my ($vg, $name, $size, $uuid, $tags) = split (':', $line);
1461 return if $name !~ m/^vm-(\d+)-/;
1465 foreach my $tag (split (/,/, $tags)) {
1466 if ($tag =~ m/^pve-vm-(\d+)$/) {
1473 if ($owner ne $nid) {
1474 warn "owner mismatch name = $name, owner = $owner\n";
1477 $lvs->{$vg}->{$name} = { format
=> 'raw', size
=> $size,
1478 uuid
=> $uuid, tags
=> $tags,
1486 #install iso or openvz template ($tt = <iso|vztmpl>)
1487 # we simply overwrite when file already exists
1488 sub install_template
{
1489 my ($cfg, $storeid, $tt, $srcfile, $destfile) = @_;
1491 my $scfg = storage_config
($cfg, $storeid);
1493 my $type = $scfg->{type
};
1495 die "invalid storage type '$type'" if !($type eq 'dir' || $type eq 'nfs');
1500 die "file '$destfile' has no '.iso' extension\n"
1501 if $destfile !~ m![^/]+\.[Ii][Ss][Oo]$!;
1502 die "storage '$storeid' does not support 'iso' content\n"
1503 if !$scfg->{content
}->{iso
};
1504 $path = get_iso_dir
($cfg, $storeid);
1505 } elsif ($tt eq 'vztmpl') {
1506 die "file '$destfile' has no '.tar.gz' extension\n"
1507 if $destfile !~ m![^/]+\.tar\.gz$!;
1508 die "storage '$storeid' does not support 'vztmpl' content\n"
1509 if !$scfg->{content
}->{vztmpl
};
1510 $path = get_vztmpl_dir
($cfg, $storeid);
1512 die "unknown template type '$tt'";
1515 activate_storage
($cfg, $storeid);
1517 my $dest = "$path/$destfile";
1519 my $cmd = ['cp', $srcfile, $dest];
1521 eval { run_command
($cmd); };
1530 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
1532 my ($cfg, $storeid, $tt) = @_;
1534 die "unknown template type '$tt'\n" if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
1536 my $ids = $cfg->{ids
};
1538 storage_check_enabled
($cfg, $storeid) if ($storeid);
1544 foreach my $sid (keys %$ids) {
1545 next if $storeid && $storeid ne $sid;
1547 my $scfg = $ids->{$sid};
1548 my $type = $scfg->{type
};
1550 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1552 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
1553 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
1554 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
1556 activate_storage
($cfg, $sid);
1558 if ($type eq 'dir' || $type eq 'nfs') {
1562 $path = get_iso_dir
($cfg, $sid);
1563 } elsif ($tt eq 'vztmpl') {
1564 $path = get_vztmpl_dir
($cfg, $sid);
1565 } elsif ($tt eq 'backup') {
1566 $path = get_backup_dir
($cfg, $sid);
1568 die "unknown template type '$tt'\n";
1571 foreach my $fn (<$path/*>) {
1576 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
1578 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
1580 } elsif ($tt eq 'vztmpl') {
1581 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
1583 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
1585 } elsif ($tt eq 'backup') {
1586 next if $fn !~ m!/([^/]+\.(tar|tgz))$!;
1588 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
1591 $info->{size
} = -s
$fn;
1593 push @{$res->{$sid}}, $info;
1598 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1604 sub file_size_info
{
1605 my ($filename, $timeout) = @_;
1607 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
1614 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
1617 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
1619 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
1621 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
1625 $used *= 1024 if $u eq 'K';
1626 $used *= (1024*1024) if $u eq 'M';
1627 $used *= (1024*1024*1024) if $u eq 'G';
1628 $used *= (1024*1024*1024*1024) if $u eq 'T';
1635 return wantarray ?
($size, $format, $used) : $size;
1639 my ($cfg, $storeid, $vmid, $vollist) = @_;
1641 my $ids = $cfg->{ids
};
1643 storage_check_enabled
($cfg, $storeid) if ($storeid);
1647 # prepare/activate/refresh all storages
1651 my $storage_list = [];
1653 foreach my $volid (@$vollist) {
1654 my ($sid, undef) = parse_volume_id
($volid);
1655 next if !defined ($ids->{$sid});
1656 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1657 push @$storage_list, $sid;
1658 $stypes->{$ids->{$sid}->{type
}} = 1;
1661 foreach my $sid (keys %$ids) {
1662 next if $storeid && $storeid ne $sid;
1663 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1664 push @$storage_list, $sid;
1665 $stypes->{$ids->{$sid}->{type
}} = 1;
1669 activate_storage_list
($cfg, $storage_list);
1671 my $lvs = $stypes->{lvm
} ? lvm_lvs
() : {};
1673 my $iscsi_devices = iscsi_device_list
() if $stypes->{iscsi
};
1677 foreach my $sid (keys %$ids) {
1679 next if $storeid ne $sid;
1680 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1682 my $scfg = $ids->{$sid};
1683 my $type = $scfg->{type
};
1685 if ($type eq 'dir' || $type eq 'nfs') {
1687 my $path = $scfg->{path
};
1689 my $fmts = join ('|', keys %{$default_config->{$type}->{format
}->[0]});
1691 foreach my $fn (<$path/images/[0-9][0-9]*/*>) {
1693 next if $fn !~ m!^(/.+/images/(\d+)/([^/]+\.($fmts)))$!;
1698 my $volid = "$sid:$owner/$name";
1701 my $found = grep { $_ eq $volid } @$vollist;
1704 next if defined ($vmid) && ($owner ne $vmid);
1707 my ($size, $format, $used) = file_size_info
($fn);
1709 if ($format && $size) {
1710 push @{$res->{$sid}}, {
1711 volid
=> $volid, format
=> $format,
1712 size
=> $size, vmid
=> $owner, used
=> $used };
1717 } elsif ($type eq 'lvm') {
1719 my $vgname = $scfg->{vgname
};
1721 if (my $dat = $lvs->{$vgname}) {
1723 foreach my $volname (keys %$dat) {
1725 my $owner = $dat->{$volname}->{vmid
};
1727 my $volid = "$sid:$volname";
1730 my $found = grep { $_ eq $volid } @$vollist;
1733 next if defined ($vmid) && ($owner ne $vmid);
1736 my $info = $dat->{$volname};
1737 $info->{volid
} = $volid;
1739 push @{$res->{$sid}}, $info;
1743 } elsif ($type eq 'iscsi') {
1745 # we have no owner for iscsi devices
1747 my $target = $scfg->{target
};
1749 if (my $dat = $iscsi_devices->{$target}) {
1751 foreach my $volname (keys %$dat) {
1753 my $volid = "$sid:$volname";
1756 my $found = grep { $_ eq $volid } @$vollist;
1759 next if !($storeid && ($storeid eq $sid));
1762 my $info = $dat->{$volname};
1763 $info->{volid
} = $volid;
1765 push @{$res->{$sid}}, $info;
1773 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1779 sub nfs_is_mounted
{
1780 my ($server, $export, $mountpoint, $mountdata) = @_;
1782 my $source = "$server:$export";
1784 $mountdata = read_proc_mounts
() if !$mountdata;
1786 if ($mountdata =~ m/^$source\s$mountpoint\snfs/m) {
1794 my ($server, $export, $mountpoint, $options) = @_;
1796 my $source = "$server:$export";
1798 my $cmd = ['/bin/mount', '-t', 'nfs', $source, $mountpoint];
1800 push @$cmd, '-o', $options;
1808 my $filename = "/sys/kernel/uevent_seqnum";
1811 if (my $fh = IO
::File-
>new ($filename, "r")) {
1813 if ($line =~ m/^(\d+)$/) {
1821 sub __activate_storage_full
{
1822 my ($cfg, $storeid, $session) = @_;
1824 my $scfg = storage_check_enabled
($cfg, $storeid);
1826 return if $session->{activated
}->{$storeid};
1828 if (!$session->{mountdata
}) {
1829 $session->{mountdata
} = read_proc_mounts
();
1832 if (!$session->{uevent_seqnum
}) {
1833 $session->{uevent_seqnum
} = uevent_seqnum
();
1836 my $mountdata = $session->{mountdata
};
1838 my $type = $scfg->{type
};
1840 if ($type eq 'dir' || $type eq 'nfs') {
1842 my $path = $scfg->{path
};
1844 if ($type eq 'nfs') {
1845 my $server = $scfg->{server
};
1846 my $export = $scfg->{export
};
1848 if (!nfs_is_mounted
($server, $export, $path, $mountdata)) {
1850 # NOTE: only call mkpath when not mounted (avoid hang
1851 # when NFS server is offline
1855 die "unable to activate storage '$storeid' - " .
1856 "directory '$path' does not exist\n" if ! -d
$path;
1858 nfs_mount
($server, $export, $path, $scfg->{options
});
1865 die "unable to activate storage '$storeid' - " .
1866 "directory '$path' does not exist\n" if ! -d
$path;
1869 my $imagedir = get_image_dir
($cfg, $storeid);
1870 my $isodir = get_iso_dir
($cfg, $storeid);
1871 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1872 my $backupdir = get_backup_dir
($cfg, $storeid);
1874 if (defined($scfg->{content
})) {
1875 mkpath
$imagedir if $scfg->{content
}->{images
} &&
1877 mkpath
$isodir if $scfg->{content
}->{iso
} &&
1879 mkpath
$tmpldir if $scfg->{content
}->{vztmpl
} &&
1881 mkpath
$backupdir if $scfg->{content
}->{backup
} &&
1882 $backupdir ne $path;
1885 } elsif ($type eq 'lvm') {
1887 if ($scfg->{base
}) {
1888 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
1889 __activate_storage_full
($cfg, $baseid, $session);
1892 if (!$session->{vgs
}) {
1893 $session->{vgs
} = lvm_vgs
();
1896 # In LVM2, vgscans take place automatically;
1897 # this is just to be sure
1898 if ($session->{vgs
} && !$session->{vgscaned
} &&
1899 !$session->{vgs
}->{$scfg->{vgname
}}) {
1900 $session->{vgscaned
} = 1;
1901 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
1902 eval { run_command
($cmd, outfunc
=> sub {}); };
1906 my $cmd = ['/sbin/vgchange', '-aly', $scfg->{vgname
}];
1907 run_command
($cmd, outfunc
=> sub {});
1909 } elsif ($type eq 'iscsi') {
1911 return if !check_iscsi_support
(1);
1913 $session->{iscsi_sessions
} = iscsi_session_list
()
1914 if !$session->{iscsi_sessions
};
1916 my $iscsi_sess = $session->{iscsi_sessions
}->{$scfg->{target
}};
1917 if (!defined ($iscsi_sess)) {
1918 eval { iscsi_login
($scfg->{target
}, $scfg->{portal
}); };
1921 # make sure we get all devices
1922 iscsi_session_rescan
($iscsi_sess);
1929 my $newseq = uevent_seqnum
();
1931 # only call udevsettle if there are events
1932 if ($newseq > $session->{uevent_seqnum
}) {
1934 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1935 $session->{uevent_seqnum
} = $newseq;
1938 $session->{activated
}->{$storeid} = 1;
1941 sub activate_storage_list
{
1942 my ($cfg, $storeid_list, $session) = @_;
1944 $session = {} if !$session;
1946 foreach my $storeid (@$storeid_list) {
1947 __activate_storage_full
($cfg, $storeid, $session);
1951 sub activate_storage
{
1952 my ($cfg, $storeid) = @_;
1956 __activate_storage_full
($cfg, $storeid, $session);
1959 sub activate_volumes
{
1960 my ($cfg, $vollist) = @_;
1962 my $storagehash = {};
1963 foreach my $volid (@$vollist) {
1964 my ($storeid, undef) = parse_volume_id
($volid);
1965 $storagehash->{$storeid} = 1;
1968 activate_storage_list
($cfg, [keys %$storagehash]);
1970 foreach my $volid (@$vollist) {
1971 my ($storeid, $volname) = parse_volume_id
($volid);
1973 my $scfg = storage_config
($cfg, $storeid);
1975 my $path = path
($cfg, $volid);
1977 if ($scfg->{type
} eq 'lvm') {
1978 my $cmd = ['/sbin/lvchange', '-aly', $path];
1979 eval { run_command
($cmd); };
1983 # check is volume exists
1984 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1985 die "volume '$volid' does not exist\n" if ! -f
$path;
1987 die "volume '$volid' does not exist\n" if ! -b
$path;
1992 sub deactivate_volumes
{
1993 my ($cfg, $vollist) = @_;
1995 my $lvs = lvm_lvs
();
1997 foreach my $volid (@$vollist) {
1998 my ($storeid, $volname) = parse_volume_id
($volid);
2000 my $scfg = storage_config
($cfg, $storeid);
2002 if ($scfg->{type
} eq 'lvm') {
2003 my ($name) = parse_volname_lvm
($volname);
2005 if ($lvs->{$scfg->{vgname
}}->{$name}) {
2006 my $path = path
($cfg, $volid);
2007 my $cmd = ['/sbin/lvchange', '-aln', $path];
2008 eval { run_command
($cmd); };
2015 sub deactivate_storage
{
2016 my ($cfg, $storeid) = @_;
2020 my $scfg = storage_config
($cfg, $storeid);
2022 my $type = $scfg->{type
};
2024 if ($type eq 'dir') {
2026 } elsif ($type eq 'nfs') {
2027 my $mountdata = read_proc_mounts
();
2028 my $server = $scfg->{server
};
2029 my $export = $scfg->{export
};
2030 my $path = $scfg->{path
};
2032 my $cmd = ['/bin/umount', $path];
2034 run_command
($cmd) if nfs_is_mounted
($server, $export, $path, $mountdata);
2035 } elsif ($type eq 'lvm') {
2036 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
2038 } elsif ($type eq 'iscsi') {
2039 my $portal = $scfg->{portal
};
2040 my $target = $scfg->{target
};
2042 my $iscsi_sessions = iscsi_session_list
();
2043 iscsi_logout
($target, $portal)
2044 if defined ($iscsi_sessions->{$target});
2052 my ($cfg, $content) = @_;
2054 my $ids = $cfg->{ids
};
2060 foreach my $storeid (keys %$ids) {
2062 next if $content && !$ids->{$storeid}->{content
}->{$content};
2064 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
2066 my $type = $ids->{$storeid}->{type
};
2068 $info->{$storeid} = {
2073 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
2074 content
=> content_hash_to_string
($ids->{$storeid}->{content
}),
2078 $stypes->{$type} = 1;
2080 push @$slist, $storeid;
2085 my $iscsi_sessions = {};
2088 if ($stypes->{lvm
}) {
2089 $session->{vgs
} = lvm_vgs
();
2090 $vgs = $session->{vgs
};
2092 if ($stypes->{nfs
}) {
2093 $mountdata = read_proc_mounts
();
2094 $session->{mountdata
} = $mountdata;
2096 if ($stypes->{iscsi
}) {
2097 $iscsi_sessions = iscsi_session_list
();
2098 $session->{iscsi_sessions
} = $iscsi_sessions;
2101 eval { activate_storage_list
($cfg, $slist, $session); };
2103 foreach my $storeid (keys %$ids) {
2104 my $scfg = $ids->{$storeid};
2106 next if !$info->{$storeid};
2108 my $type = $scfg->{type
};
2110 if ($type eq 'dir' || $type eq 'nfs') {
2112 my $path = $scfg->{path
};
2114 if ($type eq 'nfs') {
2115 my $server = $scfg->{server
};
2116 my $export = $scfg->{export
};
2118 next if !nfs_is_mounted
($server, $export, $path, $mountdata);
2122 my $res = PVE
::Tools
::df
($path, $timeout);
2124 next if !$res || !$res->{total
};
2126 $info->{$storeid}->{total
} = $res->{total
};
2127 $info->{$storeid}->{avail
} = $res->{avail
};
2128 $info->{$storeid}->{used
} = $res->{used
};
2129 $info->{$storeid}->{active
} = 1;
2131 } elsif ($type eq 'lvm') {
2133 my $vgname = $scfg->{vgname
};
2138 if (defined ($vgs->{$vgname})) {
2139 $total = $vgs->{$vgname}->{size
};
2140 $free = $vgs->{$vgname}->{free
};
2142 $info->{$storeid}->{total
} = $total;
2143 $info->{$storeid}->{avail
} = $free;
2144 $info->{$storeid}->{used
} = $total - $free;
2145 $info->{$storeid}->{active
} = 1;
2148 } elsif ($type eq 'iscsi') {
2150 $info->{$storeid}->{total
} = 0;
2151 $info->{$storeid}->{avail
} = 0;
2152 $info->{$storeid}->{used
} = 0;
2153 $info->{$storeid}->{active
} =
2154 defined ($iscsi_sessions->{$scfg->{target
}});
2167 my $packed_ip = gethostbyname($server);
2168 if (defined $packed_ip) {
2169 return inet_ntoa
($packed_ip);
2175 my ($server_in) = @_;
2178 if (!($server = resolv_server
($server_in))) {
2179 die "unable to resolve address for server '${server_in}'\n";
2182 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
2185 run_command
($cmd, outfunc
=> sub {
2188 # note: howto handle white spaces in export path??
2189 if ($line =~ m!^(/\S+)\s+(.+)$!) {
2198 my ($portal, $noerr) = @_;
2200 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
2204 if (my $ip = resolv_server
($server)) {
2206 return $port ?
"$server:$port" : $server;
2209 return undef if $noerr;
2211 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
2214 # idea is from usbutils package (/usr/bin/usb-devices) script
2215 sub __scan_usb_device
{
2216 my ($res, $devpath, $parent, $level) = @_;
2218 return if ! -d
$devpath;
2219 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
2220 my $port = $level ?
int($1 - 1) : 0;
2222 my $busnum = int(file_read_firstline
("$devpath/busnum"));
2223 my $devnum = int(file_read_firstline
("$devpath/devnum"));
2230 speed
=> file_read_firstline
("$devpath/speed"),
2231 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
2232 vendid
=> file_read_firstline
("$devpath/idVendor"),
2233 prodid
=> file_read_firstline
("$devpath/idProduct"),
2237 my $usbpath = $devpath;
2238 $usbpath =~ s
|^.*/\d
+\
-||;
2239 $d->{usbpath
} = $usbpath;
2242 my $product = file_read_firstline
("$devpath/product");
2243 $d->{product
} = $product if $product;
2245 my $manu = file_read_firstline
("$devpath/manufacturer");
2246 $d->{manufacturer
} = $manu if $manu;
2248 my $serial => file_read_firstline
("$devpath/serial");
2249 $d->{serial
} = $serial if $serial;
2253 foreach my $subdev (<$devpath/$busnum-*>) {
2254 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
2255 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
2264 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
2265 __scan_usb_device
($devlist, $device, 0, 0);
2272 my ($portal_in) = @_;
2275 if (!($portal = resolv_portal
($portal_in))) {
2276 die "unable to parse/resolve portal address '${portal_in}'\n";
2279 return iscsi_discovery
($portal);
2282 sub storage_default_format
{
2283 my ($cfg, $storeid) = @_;
2285 my $scfg = storage_config
($cfg, $storeid);
2287 my $def = $default_config->{$scfg->{type
}};
2289 my $def_format = 'raw';
2290 my $valid_formats = [ $def_format ];
2292 if (defined ($def->{format
})) {
2293 $def_format = $scfg->{format
} || $def->{format
}->[1];
2294 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
2297 return wantarray ?
($def_format, $valid_formats) : $def_format;
2300 sub vgroup_is_used
{
2301 my ($cfg, $vgname) = @_;
2303 foreach my $storeid (keys %{$cfg->{ids
}}) {
2304 my $scfg = storage_config
($cfg, $storeid);
2305 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
2313 sub target_is_used
{
2314 my ($cfg, $target) = @_;
2316 foreach my $storeid (keys %{$cfg->{ids
}}) {
2317 my $scfg = storage_config
($cfg, $storeid);
2318 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
2326 sub volume_is_used
{
2327 my ($cfg, $volid) = @_;
2329 foreach my $storeid (keys %{$cfg->{ids
}}) {
2330 my $scfg = storage_config
($cfg, $storeid);
2331 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
2339 sub storage_is_used
{
2340 my ($cfg, $storeid) = @_;
2342 foreach my $sid (keys %{$cfg->{ids
}}) {
2343 my $scfg = storage_config
($cfg, $sid);
2344 next if !$scfg->{base
};
2345 my ($st) = parse_volume_id
($scfg->{base
});
2346 return 1 if $st && $st eq $storeid;
2353 my ($list, $func) = @_;
2357 foreach my $sid (keys %$list) {
2358 foreach my $info (@{$list->{$sid}}) {
2359 my $volid = $info->{volid
};
2360 my ($sid1, $volname) = parse_volume_id
($volid, 1);
2361 if ($sid1 && $sid1 eq $sid) {
2362 &$func ($volid, $sid, $info);
2364 warn "detected strange volid '$volid' in volume list for '$sid'\n";