]>
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 if ($res->{none
} && scalar (keys %$res) > 1) {
401 return undef if $noerr;
402 die "unable to combine 'none' with other content types\n";
406 } elsif ($ct eq 'volume') {
407 return $value if parse_volume_id
($value, $noerr);
410 return undef if $noerr;
411 die "type check not implemented - internal error\n";
415 my ($filename, $raw) = @_;
419 my $digest = Digest
::SHA1
::sha1_hex
(defined($raw) ?
$raw : '');
423 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
426 next if $line =~ m/^\#/;
427 next if $line =~ m/^\s*$/;
429 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
434 if (!parse_storage_id
($storeid, 1)) {
436 warn "ignoring storage '$storeid' - (illegal characters)\n";
437 } elsif (!$default_config->{$type}) {
439 warn "ignoring storage '$storeid' (unsupported type '$type')\n";
441 $ids->{$storeid}->{type
} = $type;
442 $ids->{$storeid}->{priority
} = $pri++;
445 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
448 next if $line =~ m/^\#/;
449 last if $line =~ m/^\s*$/;
451 next if $ignore; # skip
453 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
454 my ($k, $v) = ($1, $3);
455 if (my $ct = $confvars->{$k}) {
456 $v = 1 if $ct eq 'bool' && !defined($v);
458 $ids->{$storeid}->{$k} = check_type
($type, $ct, $k, $v, $storeid);
460 warn "storage '$storeid' - unable to parse value of '$k': $@" if $@;
462 warn "storage '$storeid' - unable to parse value of '$k'\n";
466 warn "storage '$storeid' - ignore config line: $line\n";
470 warn "ignore config line: $line\n";
474 # make sure we have a reasonable 'local:' storage
475 # openvz expects things to be there
476 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
477 $ids->{local}->{path
} ne '/var/lib/vz') {
481 path
=> '/var/lib/vz',
482 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
486 # we always need this for OpenVZ
487 $ids->{local}->{content
}->{rootdir
} = 1;
488 $ids->{local}->{content
}->{vztmpl
} = 1;
489 delete ($ids->{local}->{disable
});
491 # remove node restrictions for local storage
492 delete($ids->{local}->{nodes
});
494 foreach my $storeid (keys %$ids) {
495 my $d = $ids->{$storeid};
497 my $req_keys = $required_config->{$d->{type
}};
498 foreach my $k (@$req_keys) {
499 if (!defined ($d->{$k})) {
500 warn "ignoring storage '$storeid' - missing value " .
501 "for required option '$k'\n";
502 delete $ids->{$storeid};
507 my $def = $default_config->{$d->{type
}};
509 if ($def->{content
}) {
510 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
513 if ($d->{type
} eq 'iscsi' || $d->{type
} eq 'nfs') {
518 my $cfg = { ids
=> $ids, digest
=> $digest};
524 my ($storeid, $stype, $param, $create) = @_;
526 my $settings = { type
=> $stype };
528 die "unknown storage type '$stype'\n"
529 if !$default_config->{$stype};
531 foreach my $opt (keys %$param) {
532 my $value = $param->{$opt};
534 my $ct = $confvars->{$opt};
535 if (defined($value)) {
537 $settings->{$opt} = check_type
($stype, $ct, $opt, $value, $storeid);
539 raise_param_exc
({ $opt => $@ }) if $@;
541 raise_param_exc
({ $opt => "got undefined value" });
546 my $req_keys = $required_config->{$stype};
547 foreach my $k (@$req_keys) {
549 if ($stype eq 'nfs' && !$settings->{path
}) {
550 $settings->{path
} = "/mnt/pve/$storeid";
553 # check if we have a value for all required options
554 if (!defined ($settings->{$k})) {
555 raise_param_exc
({ $k => "property is missing and it is not optional" });
559 my $fixed_keys = $fixed_config->{$stype};
560 foreach my $k (@$fixed_keys) {
562 # only allow to change non-fixed values
564 if (defined ($settings->{$k})) {
565 raise_param_exc
({$k => "can't change value (fixed parameter)"});
573 sub cluster_lock_storage
{
574 my ($storeid, $shared, $timeout, $func, @param) = @_;
578 my $lockid = "pve-storage-$storeid";
579 my $lockdir = "/var/lock/pve-manager";
581 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
584 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
591 my ($cfg, $storeid, $noerr) = @_;
593 die "no storage id specified\n" if !$storeid;
595 my $scfg = $cfg->{ids
}->{$storeid};
597 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
602 sub storage_check_node
{
603 my ($cfg, $storeid, $node, $noerr) = @_;
605 my $scfg = storage_config
($cfg, $storeid);
607 if ($scfg->{nodes
}) {
608 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
609 if (!$scfg->{nodes
}->{$node}) {
610 die "storage '$storeid' is not available on node '$node'" if !$noerr;
618 sub storage_check_enabled
{
619 my ($cfg, $storeid, $node, $noerr) = @_;
621 my $scfg = storage_config
($cfg, $storeid);
623 if ($scfg->{disable
}) {
624 die "storage '$storeid' is disabled\n" if !$noerr;
628 return storage_check_node
($cfg, $storeid, $node, $noerr);
634 my $ids = $cfg->{ids
};
636 my @sa = sort {$ids->{$a}->{priority
} <=> $ids->{$b}->{priority
}} keys %$ids;
641 sub assert_if_modified
{
642 my ($cfg, $digest) = @_;
644 if ($digest && ($cfg->{digest
} ne $digest)) {
645 die "detected modified storage configuration - try again\n";
649 sub sprint_config_line
{
652 my $ct = $confvars->{$k};
655 return $v ?
"\t$k\n" : '';
656 } elsif ($ct eq 'nodes') {
657 my $nlist = join(',', keys(%$v));
658 return $nlist ?
"\tnodes $nlist\n" : '';
659 } elsif ($ct eq 'content') {
660 my $clist = content_hash_to_string
($v);
662 return "\t$k $clist\n";
664 return "\t$k none\n";
672 my ($filename, $cfg) = @_;
676 my $ids = $cfg->{ids
};
679 foreach my $storeid (keys %$ids) {
680 my $pri = $ids->{$storeid}->{priority
};
681 $maxpri = $pri if $pri && $pri > $maxpri;
683 foreach my $storeid (keys %$ids) {
684 if (!defined ($ids->{$storeid}->{priority
})) {
685 $ids->{$storeid}->{priority
} = ++$maxpri;
689 foreach my $storeid (sort {$ids->{$a}->{priority
} <=> $ids->{$b}->{priority
}} keys %$ids) {
690 my $scfg = $ids->{$storeid};
691 my $type = $scfg->{type
};
692 my $def = $default_config->{$type};
694 die "unknown storage type '$type'\n" if !$def;
696 my $data = "$type: $storeid\n";
698 $data .= "\tdisable\n" if $scfg->{disable
};
700 my $done_hash = { disable
=> 1};
701 foreach my $k (@{$required_config->{$type}}) {
702 $done_hash->{$k} = 1;
703 my $v = $ids->{$storeid}->{$k};
704 die "storage '$storeid' - missing value for required option '$k'\n"
706 $data .= sprint_config_line
($k, $v);
709 foreach my $k (keys %$def) {
710 next if defined ($done_hash->{$k});
711 if (defined (my $v = $ids->{$storeid}->{$k})) {
712 $data .= sprint_config_line
($k, $v);
723 my ($cfg, $storeid, $vmid) = @_;
725 my $path = $cfg->{ids
}->{$storeid}->{path
};
726 return $vmid ?
"$path/images/$vmid" : "$path/images";
730 my ($cfg, $storeid) = @_;
732 my $isodir = $cfg->{ids
}->{$storeid}->{path
};
733 $isodir .= '/template/iso';
739 my ($cfg, $storeid) = @_;
741 my $tmpldir = $cfg->{ids
}->{$storeid}->{path
};
742 $tmpldir .= '/template/cache';
748 my ($cfg, $storeid) = @_;
750 my $dir = $cfg->{ids
}->{$storeid}->{path
};
756 # iscsi utility functions
758 sub iscsi_session_list
{
760 check_iscsi_support
();
762 my $cmd = [$ISCSIADM, '--mode', 'session'];
766 run_command
($cmd, outfunc
=> sub {
769 if ($line =~ m/^tcp:\s+\[(\S+)\]\s+\S+\s+(\S+)\s*$/) {
770 my ($session, $target) = ($1, $2);
771 # there can be several sessions per target (multipath)
772 push @{$res->{$target}}, $session;
780 sub iscsi_test_portal
{
783 my ($server, $port) = split(':', $portal);
784 my $p = Net
::Ping-
>new("tcp", 2);
785 $p->port_number($port || 3260);
786 return $p->ping($server);
789 sub iscsi_discovery
{
792 check_iscsi_support
();
794 my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets',
795 '--portal', $portal];
799 return $res if !iscsi_test_portal
($portal); # fixme: raise exception here?
801 run_command
($cmd, outfunc
=> sub {
804 if ($line =~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\,\S+\s+(\S+)\s*$/) {
807 # one target can have more than one portal (multipath).
808 push @{$res->{$target}}, $portal;
816 my ($target, $portal_in) = @_;
818 check_iscsi_support
();
820 eval { iscsi_discovery
($portal_in); };
823 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--login'];
828 my ($target, $portal) = @_;
830 check_iscsi_support
();
832 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--logout'];
836 my $rescan_filename = "/var/run/pve-iscsi-rescan.lock";
838 sub iscsi_session_rescan
{
839 my $session_list = shift;
841 check_iscsi_support
();
843 my $rstat = stat ($rescan_filename);
846 if (my $fh = IO
::File-
>new ($rescan_filename, "a")) {
847 utime undef, undef, $fh;
851 my $atime = $rstat->atime;
852 my $tdiff = time() - $atime;
853 # avoid frequent rescans
854 return if !($tdiff < 0 || $tdiff > 10);
855 utime undef, undef, $rescan_filename;
858 foreach my $session (@$session_list) {
859 my $cmd = [$ISCSIADM, '--mode', 'session', '-r', $session, '-R'];
860 eval { run_command
($cmd, outfunc
=> sub {}); };
865 sub iscsi_device_list
{
869 my $dirname = '/sys/class/iscsi_session';
871 my $stable_paths = load_stable_scsi_paths
();
873 dir_glob_foreach
($dirname, 'session(\d+)', sub {
874 my ($ent, $session) = @_;
876 my $target = file_read_firstline
("$dirname/$ent/targetname");
879 my (undef, $host) = dir_glob_regex
("$dirname/$ent/device", 'target(\d+):.*');
880 return if !defined($host);
882 dir_glob_foreach
("/sys/bus/scsi/devices", "$host:" . '(\d+):(\d+):(\d+)', sub {
883 my ($tmp, $channel, $id, $lun) = @_;
885 my $type = file_read_firstline
("/sys/bus/scsi/devices/$tmp/type");
886 return if !defined($type) || $type ne '0'; # list disks only
889 if (-d
"/sys/bus/scsi/devices/$tmp/block") { # newer kernels
890 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp/block/", '([A-Za-z]\S*)');
892 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp", 'block:(\S+)');
897 if (-d
"/sys/block/$bdev/holders") {
898 my $multipathdev = dir_glob_regex
("/sys/block/$bdev/holders", '[A-Za-z]\S*');
899 $bdev = $multipathdev if $multipathdev;
902 my $blockdev = $stable_paths->{$bdev};
903 return if !$blockdev;
905 my $size = file_read_firstline
("/sys/block/$bdev/size");
908 my $volid = "$channel.$id.$lun.$blockdev";
910 $res->{$target}->{$volid} = {
912 'size' => int($size / 2),
913 'vmid' => 0, # not assigned to any vm
914 'channel' => int($channel),
919 #print "TEST: $target $session $host,$bus,$tg,$lun $blockdev\n";
927 # library implementation
930 PVE
::JSONSchema
::register_format
('pve-storage-id', \
&parse_storage_id
);
931 sub parse_storage_id
{
932 my ($storeid, $noerr) = @_;
934 if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
935 return undef if $noerr;
936 die "storage ID '$storeid' contains illegal characters\n";
941 PVE
::JSONSchema
::register_standard_option
('pve-storage-id', {
942 description
=> "The storage identifier.",
943 type
=> 'string', format
=> 'pve-storage-id',
946 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
948 my ($name, $noerr) = @_;
950 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
951 return undef if $noerr;
952 die "lvm name '$name' contains illegal characters\n";
961 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
966 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
967 sub parse_volume_id
{
968 my ($volid, $noerr) = @_;
970 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
971 return wantarray ?
($1, $2) : $1;
973 return undef if $noerr;
974 die "unable to parse volume ID '$volid'\n";
980 if ($name =~ m!^([^/\s]+\.(raw|qcow2|vmdk))$!) {
984 die "unable to parse volume filename '$name'\n";
987 sub parse_volname_dir
{
990 if ($volname =~ m!^(\d+)/(\S+)$!) {
991 my ($vmid, $name) = ($1, $2);
992 parse_name_dir
($name);
993 return ('image', $name, $vmid);
994 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
996 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
997 return ('vztmpl', $1);
998 } elsif ($volname =~ m!^backup/([^/]+(\.tar|\.tgz))$!) {
999 return ('backup', $1);
1001 die "unable to parse directory volume name '$volname'\n";
1004 sub parse_volname_lvm
{
1005 my $volname = shift;
1007 parse_lvm_name
($volname);
1009 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
1013 die "unable to parse lvm volume name '$volname'\n";
1016 sub parse_volname_iscsi
{
1017 my $volname = shift;
1019 if ($volname =~ m!^\d+\.\d+\.\d+\.(\S+)$!) {
1024 die "unable to parse iscsi volume name '$volname'\n";
1027 # try to map a filesystem path to a volume identifier
1028 sub path_to_volume_id
{
1029 my ($cfg, $path) = @_;
1031 my $ids = $cfg->{ids
};
1033 my ($sid, $volname) = parse_volume_id
($path, 1);
1035 if ($ids->{$sid} && (my $type = $ids->{$sid}->{type
})) {
1036 if ($type eq 'dir' || $type eq 'nfs') {
1037 my ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1038 return ($vtype, $path);
1044 $path = abs_path
($path);
1046 foreach my $sid (keys %$ids) {
1047 my $type = $ids->{$sid}->{type
};
1048 next if !($type eq 'dir' || $type eq 'nfs');
1050 my $imagedir = get_image_dir
($cfg, $sid);
1051 my $isodir = get_iso_dir
($cfg, $sid);
1052 my $tmpldir = get_vztmpl_dir
($cfg, $sid);
1053 my $backupdir = get_backup_dir
($cfg, $sid);
1055 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
1058 return ('image', "$sid:$vmid/$name");
1059 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
1061 return ('iso', "$sid:iso/$name");
1062 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
1064 return ('vztmpl', "$sid:vztmpl/$name");
1065 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tgz))$!) {
1067 return ('iso', "$sid:backup/$name");
1071 # can't map path to volume id
1076 my ($cfg, $volid) = @_;
1078 my ($storeid, $volname) = parse_volume_id
($volid);
1080 my $scfg = storage_config
($cfg, $storeid);
1085 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1086 my ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1089 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1090 my $isodir = get_iso_dir
($cfg, $storeid);
1091 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1092 my $backupdir = get_backup_dir
($cfg, $storeid);
1094 if ($vtype eq 'image') {
1095 $path = "$imagedir/$name";
1096 } elsif ($vtype eq 'iso') {
1097 $path = "$isodir/$name";
1098 } elsif ($vtype eq 'vztmpl') {
1099 $path = "$tmpldir/$name";
1100 } elsif ($vtype eq 'backup') {
1101 $path = "$backupdir/$name";
1103 die "should not be reached";
1106 } elsif ($scfg->{type
} eq 'lvm') {
1108 my $vg = $scfg->{vgname
};
1110 my ($name, $vmid) = parse_volname_lvm
($volname);
1113 $path = "/dev/$vg/$name";
1115 } elsif ($scfg->{type
} eq 'iscsi') {
1116 my $byid = parse_volname_iscsi
($volname);
1117 $path = "/dev/disk/by-id/$byid";
1119 die "unknown storage type '$scfg->{type}'";
1122 return wantarray ?
($path, $owner) : $path;
1125 sub storage_migrate
{
1126 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
1128 my ($storeid, $volname) = parse_volume_id
($volid);
1129 $target_volname = $volname if !$target_volname;
1131 my $scfg = storage_config
($cfg, $storeid);
1133 # no need to migrate shared content
1134 return if $storeid eq $target_storeid && $scfg->{shared
};
1136 my $tcfg = storage_config
($cfg, $target_storeid);
1138 my $target_volid = "${target_storeid}:${target_volname}";
1140 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
1142 # blowfish is a fast block cipher, much faster then 3des
1143 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
1144 my $ssh = "/usr/bin/ssh $sshoptions";
1146 local $ENV{RSYNC_RSH
} = $ssh;
1148 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1149 if ($tcfg->{type
} eq 'dir' || $tcfg->{type
} eq 'nfs') {
1151 my $src = path
($cfg, $volid);
1152 my $dst = path
($cfg, $target_volid);
1154 my $dirname = dirname
($dst);
1156 if ($tcfg->{shared
}) { # we can do a local copy
1158 run_command
(['/bin/mkdir', '-p', $dirname]);
1160 run_command
(['/bin/cp', $src, $dst]);
1164 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1165 '/bin/mkdir', '-p', $dirname]);
1167 # we use rsync with --sparse, so we can't use --inplace,
1168 # so we remove file on the target if it already exists to
1170 my ($size, $format) = file_size_info
($src);
1171 if ($format && ($format eq 'raw') && $size) {
1172 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1177 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
1178 $src, "root\@${target_host}:$dst"];
1182 run_command
($cmd, outfunc
=> sub {
1185 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
1186 if ($2 > $percent) {
1188 print "rsync status: $1\n";
1201 die "$errstr - target type '$tcfg->{type}' not implemented\n";
1205 die "$errstr - source type '$scfg->{type}' not implemented\n";
1210 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
1212 die "no storage id specified\n" if !$storeid;
1214 parse_storage_id
($storeid);
1216 my $scfg = storage_config
($cfg, $storeid);
1218 die "no VMID specified\n" if !$vmid;
1220 $vmid = parse_vmid
($vmid);
1222 my $defformat = storage_default_format
($cfg, $storeid);
1224 $fmt = $defformat if !$fmt;
1226 activate_storage
($cfg, $storeid);
1228 # lock shared storage
1229 return cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1231 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1233 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1239 for (my $i = 1; $i < 100; $i++) {
1240 my @gr = <$imagedir/vm-$vmid-disk
-$i.*>;
1242 $name = "vm-$vmid-disk-$i.$fmt";
1248 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1251 my (undef, $tmpfmt) = parse_name_dir
($name);
1253 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
1256 my $path = "$imagedir/$name";
1258 die "disk image '$path' already exists\n" if -f
$path;
1260 run_command
("/usr/bin/qemu-img create -f $fmt '$path' ${size}K",
1261 errmsg
=> "unable to create image");
1263 return "$storeid:$vmid/$name";
1265 } elsif ($scfg->{type
} eq 'lvm') {
1267 die "unsupported format '$fmt'" if $fmt ne 'raw';
1269 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
1270 if $name && $name !~ m/^vm-$vmid-/;
1272 my $vgs = lvm_vgs
();
1274 my $vg = $scfg->{vgname
};
1276 die "no such volume gruoup '$vg'\n" if !defined ($vgs->{$vg});
1278 my $free = int ($vgs->{$vg}->{free
});
1280 die "not enough free space ($free < $size)\n" if $free < $size;
1283 my $lvs = lvm_lvs
($vg);
1285 for (my $i = 1; $i < 100; $i++) {
1286 my $tn = "vm-$vmid-disk-$i";
1287 if (!defined ($lvs->{$vg}->{$tn})) {
1294 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1297 my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
1301 return "$storeid:$name";
1303 } elsif ($scfg->{type
} eq 'iscsi') {
1304 die "can't allocate space in iscsi storage\n";
1306 die "unknown storage type '$scfg->{type}'";
1312 my ($cfg, $volid) = @_;
1314 my ($storeid, $volname) = parse_volume_id
($volid);
1316 my $scfg = storage_config
($cfg, $storeid);
1318 activate_storage
($cfg, $storeid);
1320 # lock shared storage
1321 cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1323 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1324 my $path = path
($cfg, $volid);
1327 warn "disk image '$path' does not exists\n";
1331 } elsif ($scfg->{type
} eq 'lvm') {
1333 my $vg = $scfg->{vgname
};
1335 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
1338 } elsif ($scfg->{type
} eq 'iscsi') {
1339 die "can't free space in iscsi storage\n";
1341 die "unknown storage type '$scfg->{type}'";
1346 # lvm utility functions
1351 die "no device specified" if !$device;
1355 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
1356 run_command
($cmd, outfunc
=> sub {
1358 $has_label = 1 if $line =~ m/LVM2/;
1361 return undef if !$has_label;
1363 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
1364 '--unbuffered', '--nosuffix', '--options',
1365 'pv_name,pv_size,vg_name,pv_uuid', $device];
1368 run_command
($cmd, outfunc
=> sub {
1371 $line = trim
($line);
1373 my ($pvname, $size, $vgname, $uuid) = split (':', $line);
1375 die "found multiple pvs entries for device '$device'\n"
1389 sub clear_first_sector
{
1392 if (my $fh = IO
::File-
>new ($dev, "w")) {
1399 sub lvm_create_volume_group
{
1400 my ($device, $vgname, $shared) = @_;
1402 my $res = lvm_pv_info
($device);
1404 if ($res->{vgname
}) {
1405 return if $res->{vgname
} eq $vgname; # already created
1406 die "device '$device' is already used by volume group '$res->{vgname}'\n";
1409 clear_first_sector
($device); # else pvcreate fails
1411 # we use --metadatasize 250k, which reseults in "pe_start = 512"
1412 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
1413 my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
1417 $cmd = ['/sbin/vgcreate', $vgname, $device];
1418 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
1425 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
1426 '--unbuffered', '--nosuffix', '--options',
1427 'vg_name,vg_size,vg_free'];
1430 run_command
($cmd, outfunc
=> sub {
1433 $line = trim
($line);
1435 my ($name, $size, $free) = split (':', $line);
1437 $vgs->{$name} = { size
=> int ($size), free
=> int ($free) };
1446 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
1447 '--unbuffered', '--nosuffix', '--options',
1448 'vg_name,lv_name,lv_size,uuid,tags'];
1450 push @$cmd, $vgname if $vgname;
1453 run_command
($cmd, outfunc
=> sub {
1456 $line = trim
($line);
1458 my ($vg, $name, $size, $uuid, $tags) = split (':', $line);
1460 return if $name !~ m/^vm-(\d+)-/;
1464 foreach my $tag (split (/,/, $tags)) {
1465 if ($tag =~ m/^pve-vm-(\d+)$/) {
1472 if ($owner ne $nid) {
1473 warn "owner mismatch name = $name, owner = $owner\n";
1476 $lvs->{$vg}->{$name} = { format
=> 'raw', size
=> $size,
1477 uuid
=> $uuid, tags
=> $tags,
1485 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
1487 my ($cfg, $storeid, $tt) = @_;
1489 die "unknown template type '$tt'\n" if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
1491 my $ids = $cfg->{ids
};
1493 storage_check_enabled
($cfg, $storeid) if ($storeid);
1499 foreach my $sid (keys %$ids) {
1500 next if $storeid && $storeid ne $sid;
1502 my $scfg = $ids->{$sid};
1503 my $type = $scfg->{type
};
1505 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1507 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
1508 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
1509 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
1511 activate_storage
($cfg, $sid);
1513 if ($type eq 'dir' || $type eq 'nfs') {
1517 $path = get_iso_dir
($cfg, $sid);
1518 } elsif ($tt eq 'vztmpl') {
1519 $path = get_vztmpl_dir
($cfg, $sid);
1520 } elsif ($tt eq 'backup') {
1521 $path = get_backup_dir
($cfg, $sid);
1523 die "unknown template type '$tt'\n";
1526 foreach my $fn (<$path/*>) {
1531 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
1533 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
1535 } elsif ($tt eq 'vztmpl') {
1536 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
1538 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
1540 } elsif ($tt eq 'backup') {
1541 next if $fn !~ m!/([^/]+\.(tar|tgz))$!;
1543 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
1546 $info->{size
} = -s
$fn;
1548 push @{$res->{$sid}}, $info;
1553 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1559 sub file_size_info
{
1560 my ($filename, $timeout) = @_;
1562 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
1569 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
1572 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
1574 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
1576 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
1580 $used *= 1024 if $u eq 'K';
1581 $used *= (1024*1024) if $u eq 'M';
1582 $used *= (1024*1024*1024) if $u eq 'G';
1583 $used *= (1024*1024*1024*1024) if $u eq 'T';
1590 return wantarray ?
($size, $format, $used) : $size;
1594 my ($cfg, $storeid, $vmid, $vollist) = @_;
1596 my $ids = $cfg->{ids
};
1598 storage_check_enabled
($cfg, $storeid) if ($storeid);
1602 # prepare/activate/refresh all storages
1606 my $storage_list = [];
1608 foreach my $volid (@$vollist) {
1609 my ($sid, undef) = parse_volume_id
($volid);
1610 next if !defined ($ids->{$sid});
1611 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1612 push @$storage_list, $sid;
1613 $stypes->{$ids->{$sid}->{type
}} = 1;
1616 foreach my $sid (keys %$ids) {
1617 next if $storeid && $storeid ne $sid;
1618 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1619 push @$storage_list, $sid;
1620 $stypes->{$ids->{$sid}->{type
}} = 1;
1624 activate_storage_list
($cfg, $storage_list);
1626 my $lvs = $stypes->{lvm
} ? lvm_lvs
() : {};
1628 my $iscsi_devices = iscsi_device_list
() if $stypes->{iscsi
};
1632 foreach my $sid (keys %$ids) {
1634 next if $storeid ne $sid;
1635 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1637 my $scfg = $ids->{$sid};
1638 my $type = $scfg->{type
};
1640 if ($type eq 'dir' || $type eq 'nfs') {
1642 my $path = $scfg->{path
};
1644 my $fmts = join ('|', keys %{$default_config->{$type}->{format
}->[0]});
1646 foreach my $fn (<$path/images/[0-9][0-9]*/*>) {
1648 next if $fn !~ m!^(/.+/images/(\d+)/([^/]+\.($fmts)))$!;
1653 my $volid = "$sid:$owner/$name";
1656 my $found = grep { $_ eq $volid } @$vollist;
1659 next if defined ($vmid) && ($owner ne $vmid);
1662 my ($size, $format, $used) = file_size_info
($fn);
1664 if ($format && $size) {
1665 push @{$res->{$sid}}, {
1666 volid
=> $volid, format
=> $format,
1667 size
=> $size, vmid
=> $owner, used
=> $used };
1672 } elsif ($type eq 'lvm') {
1674 my $vgname = $scfg->{vgname
};
1676 if (my $dat = $lvs->{$vgname}) {
1678 foreach my $volname (keys %$dat) {
1680 my $owner = $dat->{$volname}->{vmid
};
1682 my $volid = "$sid:$volname";
1685 my $found = grep { $_ eq $volid } @$vollist;
1688 next if defined ($vmid) && ($owner ne $vmid);
1691 my $info = $dat->{$volname};
1692 $info->{volid
} = $volid;
1694 push @{$res->{$sid}}, $info;
1698 } elsif ($type eq 'iscsi') {
1700 # we have no owner for iscsi devices
1702 my $target = $scfg->{target
};
1704 if (my $dat = $iscsi_devices->{$target}) {
1706 foreach my $volname (keys %$dat) {
1708 my $volid = "$sid:$volname";
1711 my $found = grep { $_ eq $volid } @$vollist;
1714 next if !($storeid && ($storeid eq $sid));
1717 my $info = $dat->{$volname};
1718 $info->{volid
} = $volid;
1720 push @{$res->{$sid}}, $info;
1728 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1734 sub nfs_is_mounted
{
1735 my ($server, $export, $mountpoint, $mountdata) = @_;
1737 my $source = "$server:$export";
1739 $mountdata = read_proc_mounts
() if !$mountdata;
1741 if ($mountdata =~ m/^$source\s$mountpoint\snfs/m) {
1749 my ($server, $export, $mountpoint, $options) = @_;
1751 my $source = "$server:$export";
1753 my $cmd = ['/bin/mount', '-t', 'nfs', $source, $mountpoint];
1755 push @$cmd, '-o', $options;
1763 my $filename = "/sys/kernel/uevent_seqnum";
1766 if (my $fh = IO
::File-
>new ($filename, "r")) {
1768 if ($line =~ m/^(\d+)$/) {
1776 sub __activate_storage_full
{
1777 my ($cfg, $storeid, $session) = @_;
1779 my $scfg = storage_check_enabled
($cfg, $storeid);
1781 return if $session->{activated
}->{$storeid};
1783 if (!$session->{mountdata
}) {
1784 $session->{mountdata
} = read_proc_mounts
();
1787 if (!$session->{uevent_seqnum
}) {
1788 $session->{uevent_seqnum
} = uevent_seqnum
();
1791 my $mountdata = $session->{mountdata
};
1793 my $type = $scfg->{type
};
1795 if ($type eq 'dir' || $type eq 'nfs') {
1797 my $path = $scfg->{path
};
1799 if ($type eq 'nfs') {
1800 my $server = $scfg->{server
};
1801 my $export = $scfg->{export
};
1803 if (!nfs_is_mounted
($server, $export, $path, $mountdata)) {
1805 # NOTE: only call mkpath when not mounted (avoid hang
1806 # when NFS server is offline
1810 die "unable to activate storage '$storeid' - " .
1811 "directory '$path' does not exist\n" if ! -d
$path;
1813 nfs_mount
($server, $export, $path, $scfg->{options
});
1820 die "unable to activate storage '$storeid' - " .
1821 "directory '$path' does not exist\n" if ! -d
$path;
1824 my $imagedir = get_image_dir
($cfg, $storeid);
1825 my $isodir = get_iso_dir
($cfg, $storeid);
1826 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1827 my $backupdir = get_backup_dir
($cfg, $storeid);
1829 if (defined($scfg->{content
})) {
1830 mkpath
$imagedir if $scfg->{content
}->{images
} &&
1832 mkpath
$isodir if $scfg->{content
}->{iso
} &&
1834 mkpath
$tmpldir if $scfg->{content
}->{vztmpl
} &&
1836 mkpath
$backupdir if $scfg->{content
}->{backup
} &&
1837 $backupdir ne $path;
1840 } elsif ($type eq 'lvm') {
1842 if ($scfg->{base
}) {
1843 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
1844 __activate_storage_full
($cfg, $baseid, $session);
1847 if (!$session->{vgs
}) {
1848 $session->{vgs
} = lvm_vgs
();
1851 # In LVM2, vgscans take place automatically;
1852 # this is just to be sure
1853 if ($session->{vgs
} && !$session->{vgscaned
} &&
1854 !$session->{vgs
}->{$scfg->{vgname
}}) {
1855 $session->{vgscaned
} = 1;
1856 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
1857 eval { run_command
($cmd, outfunc
=> sub {}); };
1861 my $cmd = ['/sbin/vgchange', '-aly', $scfg->{vgname
}];
1862 run_command
($cmd, outfunc
=> sub {});
1864 } elsif ($type eq 'iscsi') {
1866 return if !check_iscsi_support
(1);
1868 $session->{iscsi_sessions
} = iscsi_session_list
()
1869 if !$session->{iscsi_sessions
};
1871 my $iscsi_sess = $session->{iscsi_sessions
}->{$scfg->{target
}};
1872 if (!defined ($iscsi_sess)) {
1873 eval { iscsi_login
($scfg->{target
}, $scfg->{portal
}); };
1876 # make sure we get all devices
1877 iscsi_session_rescan
($iscsi_sess);
1884 my $newseq = uevent_seqnum
();
1886 # only call udevsettle if there are events
1887 if ($newseq > $session->{uevent_seqnum
}) {
1889 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1890 $session->{uevent_seqnum
} = $newseq;
1893 $session->{activated
}->{$storeid} = 1;
1896 sub activate_storage_list
{
1897 my ($cfg, $storeid_list, $session) = @_;
1899 $session = {} if !$session;
1901 foreach my $storeid (@$storeid_list) {
1902 __activate_storage_full
($cfg, $storeid, $session);
1906 sub activate_storage
{
1907 my ($cfg, $storeid) = @_;
1911 __activate_storage_full
($cfg, $storeid, $session);
1914 sub activate_volumes
{
1915 my ($cfg, $vollist) = @_;
1917 my $storagehash = {};
1918 foreach my $volid (@$vollist) {
1919 my ($storeid, undef) = parse_volume_id
($volid);
1920 $storagehash->{$storeid} = 1;
1923 activate_storage_list
($cfg, [keys %$storagehash]);
1925 foreach my $volid (@$vollist) {
1926 my ($storeid, $volname) = parse_volume_id
($volid);
1928 my $scfg = storage_config
($cfg, $storeid);
1930 my $path = path
($cfg, $volid);
1932 if ($scfg->{type
} eq 'lvm') {
1933 my $cmd = ['/sbin/lvchange', '-aly', $path];
1934 eval { run_command
($cmd); };
1938 # check is volume exists
1939 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1940 die "volume '$volid' does not exist\n" if ! -f
$path;
1942 die "volume '$volid' does not exist\n" if ! -b
$path;
1947 sub deactivate_volumes
{
1948 my ($cfg, $vollist) = @_;
1950 my $lvs = lvm_lvs
();
1952 foreach my $volid (@$vollist) {
1953 my ($storeid, $volname) = parse_volume_id
($volid);
1955 my $scfg = storage_config
($cfg, $storeid);
1957 if ($scfg->{type
} eq 'lvm') {
1958 my ($name) = parse_volname_lvm
($volname);
1960 if ($lvs->{$scfg->{vgname
}}->{$name}) {
1961 my $path = path
($cfg, $volid);
1962 my $cmd = ['/sbin/lvchange', '-aln', $path];
1963 eval { run_command
($cmd); };
1970 sub deactivate_storage
{
1971 my ($cfg, $storeid) = @_;
1975 my $scfg = storage_config
($cfg, $storeid);
1977 my $type = $scfg->{type
};
1979 if ($type eq 'dir') {
1981 } elsif ($type eq 'nfs') {
1982 my $mountdata = read_proc_mounts
();
1983 my $server = $scfg->{server
};
1984 my $export = $scfg->{export
};
1985 my $path = $scfg->{path
};
1987 my $cmd = ['/bin/umount', $path];
1989 run_command
($cmd) if nfs_is_mounted
($server, $export, $path, $mountdata);
1990 } elsif ($type eq 'lvm') {
1991 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
1993 } elsif ($type eq 'iscsi') {
1994 my $portal = $scfg->{portal
};
1995 my $target = $scfg->{target
};
1997 my $iscsi_sessions = iscsi_session_list
();
1998 iscsi_logout
($target, $portal)
1999 if defined ($iscsi_sessions->{$target});
2007 my ($cfg, $content) = @_;
2009 my $ids = $cfg->{ids
};
2015 foreach my $storeid (keys %$ids) {
2017 next if $content && !$ids->{$storeid}->{content
}->{$content};
2019 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
2021 my $type = $ids->{$storeid}->{type
};
2023 $info->{$storeid} = {
2028 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
2029 content
=> content_hash_to_string
($ids->{$storeid}->{content
}),
2033 $stypes->{$type} = 1;
2035 push @$slist, $storeid;
2040 my $iscsi_sessions = {};
2043 if ($stypes->{lvm
}) {
2044 $session->{vgs
} = lvm_vgs
();
2045 $vgs = $session->{vgs
};
2047 if ($stypes->{nfs
}) {
2048 $mountdata = read_proc_mounts
();
2049 $session->{mountdata
} = $mountdata;
2051 if ($stypes->{iscsi
}) {
2052 $iscsi_sessions = iscsi_session_list
();
2053 $session->{iscsi_sessions
} = $iscsi_sessions;
2056 eval { activate_storage_list
($cfg, $slist, $session); };
2058 foreach my $storeid (keys %$ids) {
2059 my $scfg = $ids->{$storeid};
2061 next if !$info->{$storeid};
2063 my $type = $scfg->{type
};
2065 if ($type eq 'dir' || $type eq 'nfs') {
2067 my $path = $scfg->{path
};
2069 if ($type eq 'nfs') {
2070 my $server = $scfg->{server
};
2071 my $export = $scfg->{export
};
2073 next if !nfs_is_mounted
($server, $export, $path, $mountdata);
2077 my $res = PVE
::Tools
::df
($path, $timeout);
2079 next if !$res || !$res->{total
};
2081 $info->{$storeid}->{total
} = $res->{total
};
2082 $info->{$storeid}->{avail
} = $res->{avail
};
2083 $info->{$storeid}->{used
} = $res->{used
};
2084 $info->{$storeid}->{active
} = 1;
2086 } elsif ($type eq 'lvm') {
2088 my $vgname = $scfg->{vgname
};
2093 if (defined ($vgs->{$vgname})) {
2094 $total = $vgs->{$vgname}->{size
};
2095 $free = $vgs->{$vgname}->{free
};
2097 $info->{$storeid}->{total
} = $total;
2098 $info->{$storeid}->{avail
} = $free;
2099 $info->{$storeid}->{used
} = $total - $free;
2100 $info->{$storeid}->{active
} = 1;
2103 } elsif ($type eq 'iscsi') {
2105 $info->{$storeid}->{total
} = 0;
2106 $info->{$storeid}->{avail
} = 0;
2107 $info->{$storeid}->{used
} = 0;
2108 $info->{$storeid}->{active
} =
2109 defined ($iscsi_sessions->{$scfg->{target
}});
2122 my $packed_ip = gethostbyname($server);
2123 if (defined $packed_ip) {
2124 return inet_ntoa
($packed_ip);
2130 my ($server_in) = @_;
2133 if (!($server = resolv_server
($server_in))) {
2134 die "unable to resolve address for server '${server_in}'\n";
2137 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
2140 run_command
($cmd, outfunc
=> sub {
2143 # note: howto handle white spaces in export path??
2144 if ($line =~ m!^(/\S+)\s+(.+)$!) {
2153 my ($portal, $noerr) = @_;
2155 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
2159 if (my $ip = resolv_server
($server)) {
2161 return $port ?
"$server:$port" : $server;
2164 return undef if $noerr;
2166 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
2169 # idea is from usbutils package (/usr/bin/usb-devices) script
2170 sub __scan_usb_device
{
2171 my ($res, $devpath, $parent, $level) = @_;
2173 return if ! -d
$devpath;
2174 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
2175 my $port = $level ?
int($1 - 1) : 0;
2177 my $busnum = int(file_read_firstline
("$devpath/busnum"));
2178 my $devnum = int(file_read_firstline
("$devpath/devnum"));
2185 speed
=> file_read_firstline
("$devpath/speed"),
2186 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
2187 vendid
=> file_read_firstline
("$devpath/idVendor"),
2188 prodid
=> file_read_firstline
("$devpath/idProduct"),
2192 my $usbpath = $devpath;
2193 $usbpath =~ s
|^.*/\d
+\
-||;
2194 $d->{usbpath
} = $usbpath;
2197 my $product = file_read_firstline
("$devpath/product");
2198 $d->{product
} = $product if $product;
2200 my $manu = file_read_firstline
("$devpath/manufacturer");
2201 $d->{manufacturer
} = $manu if $manu;
2203 my $serial => file_read_firstline
("$devpath/serial");
2204 $d->{serial
} = $serial if $serial;
2208 foreach my $subdev (<$devpath/$busnum-*>) {
2209 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
2210 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
2219 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
2220 __scan_usb_device
($devlist, $device, 0, 0);
2227 my ($portal_in) = @_;
2230 if (!($portal = resolv_portal
($portal_in))) {
2231 die "unable to parse/resolve portal address '${portal_in}'\n";
2234 return iscsi_discovery
($portal);
2237 sub storage_default_format
{
2238 my ($cfg, $storeid) = @_;
2240 my $scfg = storage_config
($cfg, $storeid);
2242 my $def = $default_config->{$scfg->{type
}};
2244 my $def_format = 'raw';
2245 my $valid_formats = [ $def_format ];
2247 if (defined ($def->{format
})) {
2248 $def_format = $scfg->{format
} || $def->{format
}->[1];
2249 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
2252 return wantarray ?
($def_format, $valid_formats) : $def_format;
2255 sub vgroup_is_used
{
2256 my ($cfg, $vgname) = @_;
2258 foreach my $storeid (keys %{$cfg->{ids
}}) {
2259 my $scfg = storage_config
($cfg, $storeid);
2260 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
2268 sub target_is_used
{
2269 my ($cfg, $target) = @_;
2271 foreach my $storeid (keys %{$cfg->{ids
}}) {
2272 my $scfg = storage_config
($cfg, $storeid);
2273 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
2281 sub volume_is_used
{
2282 my ($cfg, $volid) = @_;
2284 foreach my $storeid (keys %{$cfg->{ids
}}) {
2285 my $scfg = storage_config
($cfg, $storeid);
2286 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
2294 sub storage_is_used
{
2295 my ($cfg, $storeid) = @_;
2297 foreach my $sid (keys %{$cfg->{ids
}}) {
2298 my $scfg = storage_config
($cfg, $sid);
2299 next if !$scfg->{base
};
2300 my ($st) = parse_volume_id
($scfg->{base
});
2301 return 1 if $st && $st eq $storeid;
2308 my ($list, $func) = @_;
2312 foreach my $sid (keys %$list) {
2313 foreach my $info (@{$list->{$sid}}) {
2314 my $volid = $info->{volid
};
2315 my ($sid1, $volname) = parse_volume_id
($volid, 1);
2316 if ($sid1 && $sid1 eq $sid) {
2317 &$func ($volid, $sid, $info);
2319 warn "detected strange volid '$volid' in volume list for '$sid'\n";