]>
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);
999 die "unable to parse directory volume name '$volname'\n";
1002 sub parse_volname_lvm
{
1003 my $volname = shift;
1005 parse_lvm_name
($volname);
1007 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
1011 die "unable to parse lvm volume name '$volname'\n";
1014 sub parse_volname_iscsi
{
1015 my $volname = shift;
1017 if ($volname =~ m!^\d+\.\d+\.\d+\.(\S+)$!) {
1022 die "unable to parse iscsi volume name '$volname'\n";
1025 # try to map a filesystem path to a volume identifier
1026 sub path_to_volume_id
{
1027 my ($cfg, $path) = @_;
1029 my $ids = $cfg->{ids
};
1031 my ($sid, $volname) = parse_volume_id
($path, 1);
1033 if ($ids->{$sid} && (my $type = $ids->{$sid}->{type
})) {
1034 if ($type eq 'dir' || $type eq 'nfs') {
1035 my ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1036 return ($vtype, $path);
1042 $path = abs_path
($path);
1044 foreach my $sid (keys %$ids) {
1045 my $type = $ids->{$sid}->{type
};
1046 next if !($type eq 'dir' || $type eq 'nfs');
1048 my $imagedir = get_image_dir
($cfg, $sid);
1049 my $isodir = get_iso_dir
($cfg, $sid);
1050 my $tmpldir = get_vztmpl_dir
($cfg, $sid);
1051 my $backupdir = get_backup_dir
($cfg, $sid);
1053 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
1056 return ('image', "$sid:$vmid/$name");
1057 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
1059 return ('iso', "$sid:iso/$name");
1060 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
1062 return ('vztmpl', "$sid:vztmpl/$name");
1063 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tgz))$!) {
1065 return ('iso', "$sid:backup/$name");
1069 # can't map path to volume id
1074 my ($cfg, $volid) = @_;
1076 my ($storeid, $volname) = parse_volume_id
($volid);
1078 my $scfg = storage_config
($cfg, $storeid);
1083 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1084 my ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1087 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1088 my $isodir = get_iso_dir
($cfg, $storeid);
1089 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1090 my $backupdir = get_backup_dir
($cfg, $storeid);
1092 if ($vtype eq 'image') {
1093 $path = "$imagedir/$name";
1094 } elsif ($vtype eq 'iso') {
1095 $path = "$isodir/$name";
1096 } elsif ($vtype eq 'vztmpl') {
1097 $path = "$tmpldir/$name";
1098 } elsif ($vtype eq 'backup') {
1099 $path = "$backupdir/$name";
1101 die "should not be reached";
1104 } elsif ($scfg->{type
} eq 'lvm') {
1106 my $vg = $scfg->{vgname
};
1108 my ($name, $vmid) = parse_volname_lvm
($volname);
1111 $path = "/dev/$vg/$name";
1113 } elsif ($scfg->{type
} eq 'iscsi') {
1114 my $byid = parse_volname_iscsi
($volname);
1115 $path = "/dev/disk/by-id/$byid";
1117 die "unknown storage type '$scfg->{type}'";
1120 return wantarray ?
($path, $owner) : $path;
1123 sub storage_migrate
{
1124 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
1126 my ($storeid, $volname) = parse_volume_id
($volid);
1127 $target_volname = $volname if !$target_volname;
1129 my $scfg = storage_config
($cfg, $storeid);
1131 # no need to migrate shared content
1132 return if $storeid eq $target_storeid && $scfg->{shared
};
1134 my $tcfg = storage_config
($cfg, $target_storeid);
1136 my $target_volid = "${target_storeid}:${target_volname}";
1138 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
1140 # blowfish is a fast block cipher, much faster then 3des
1141 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
1142 my $ssh = "/usr/bin/ssh $sshoptions";
1144 local $ENV{RSYNC_RSH
} = $ssh;
1146 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1147 if ($tcfg->{type
} eq 'dir' || $tcfg->{type
} eq 'nfs') {
1149 my $src = path
($cfg, $volid);
1150 my $dst = path
($cfg, $target_volid);
1152 my $dirname = dirname
($dst);
1154 if ($tcfg->{shared
}) { # we can do a local copy
1156 run_command
(['/bin/mkdir', '-p', $dirname]);
1158 run_command
(['/bin/cp', $src, $dst]);
1162 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1163 '/bin/mkdir', '-p', $dirname]);
1165 # we use rsync with --sparse, so we can't use --inplace,
1166 # so we remove file on the target if it already exists to
1168 my ($size, $format) = file_size_info
($src);
1169 if ($format && ($format eq 'raw') && $size) {
1170 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1175 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
1176 $src, "root\@${target_host}:$dst"];
1180 run_command
($cmd, outfunc
=> sub {
1183 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
1184 if ($2 > $percent) {
1186 print "rsync status: $1\n";
1199 die "$errstr - target type '$tcfg->{type}' not implemented\n";
1203 die "$errstr - source type '$scfg->{type}' not implemented\n";
1208 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
1210 die "no storage id specified\n" if !$storeid;
1212 parse_storage_id
($storeid);
1214 my $scfg = storage_config
($cfg, $storeid);
1216 die "no VMID specified\n" if !$vmid;
1218 $vmid = parse_vmid
($vmid);
1220 my $defformat = storage_default_format
($cfg, $storeid);
1222 $fmt = $defformat if !$fmt;
1224 activate_storage
($cfg, $storeid);
1226 # lock shared storage
1227 return cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1229 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1231 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1237 for (my $i = 1; $i < 100; $i++) {
1238 my @gr = <$imagedir/vm-$vmid-disk
-$i.*>;
1240 $name = "vm-$vmid-disk-$i.$fmt";
1246 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1249 my (undef, $tmpfmt) = parse_name_dir
($name);
1251 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
1254 my $path = "$imagedir/$name";
1256 die "disk image '$path' already exists\n" if -f
$path;
1258 run_command
("/usr/bin/qemu-img create -f $fmt '$path' ${size}K",
1259 errmsg
=> "unable to create image");
1261 return "$storeid:$vmid/$name";
1263 } elsif ($scfg->{type
} eq 'lvm') {
1265 die "unsupported format '$fmt'" if $fmt ne 'raw';
1267 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
1268 if $name && $name !~ m/^vm-$vmid-/;
1270 my $vgs = lvm_vgs
();
1272 my $vg = $scfg->{vgname
};
1274 die "no such volume gruoup '$vg'\n" if !defined ($vgs->{$vg});
1276 my $free = int ($vgs->{$vg}->{free
});
1278 die "not enough free space ($free < $size)\n" if $free < $size;
1281 my $lvs = lvm_lvs
($vg);
1283 for (my $i = 1; $i < 100; $i++) {
1284 my $tn = "vm-$vmid-disk-$i";
1285 if (!defined ($lvs->{$vg}->{$tn})) {
1292 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1295 my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
1299 return "$storeid:$name";
1301 } elsif ($scfg->{type
} eq 'iscsi') {
1302 die "can't allocate space in iscsi storage\n";
1304 die "unknown storage type '$scfg->{type}'";
1310 my ($cfg, $volid) = @_;
1312 my ($storeid, $volname) = parse_volume_id
($volid);
1314 my $scfg = storage_config
($cfg, $storeid);
1316 activate_storage
($cfg, $storeid);
1318 # lock shared storage
1319 cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1321 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1322 my $path = path
($cfg, $volid);
1325 warn "disk image '$path' does not exists\n";
1329 } elsif ($scfg->{type
} eq 'lvm') {
1331 my $vg = $scfg->{vgname
};
1333 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
1336 } elsif ($scfg->{type
} eq 'iscsi') {
1337 die "can't free space in iscsi storage\n";
1339 die "unknown storage type '$scfg->{type}'";
1344 # lvm utility functions
1349 die "no device specified" if !$device;
1353 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
1354 run_command
($cmd, outfunc
=> sub {
1356 $has_label = 1 if $line =~ m/LVM2/;
1359 return undef if !$has_label;
1361 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
1362 '--unbuffered', '--nosuffix', '--options',
1363 'pv_name,pv_size,vg_name,pv_uuid', $device];
1366 run_command
($cmd, outfunc
=> sub {
1369 $line = trim
($line);
1371 my ($pvname, $size, $vgname, $uuid) = split (':', $line);
1373 die "found multiple pvs entries for device '$device'\n"
1387 sub clear_first_sector
{
1390 if (my $fh = IO
::File-
>new ($dev, "w")) {
1397 sub lvm_create_volume_group
{
1398 my ($device, $vgname, $shared) = @_;
1400 my $res = lvm_pv_info
($device);
1402 if ($res->{vgname
}) {
1403 return if $res->{vgname
} eq $vgname; # already created
1404 die "device '$device' is already used by volume group '$res->{vgname}'\n";
1407 clear_first_sector
($device); # else pvcreate fails
1409 # we use --metadatasize 250k, which reseults in "pe_start = 512"
1410 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
1411 my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
1415 $cmd = ['/sbin/vgcreate', $vgname, $device];
1416 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
1423 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
1424 '--unbuffered', '--nosuffix', '--options',
1425 'vg_name,vg_size,vg_free'];
1428 run_command
($cmd, outfunc
=> sub {
1431 $line = trim
($line);
1433 my ($name, $size, $free) = split (':', $line);
1435 $vgs->{$name} = { size
=> int ($size), free
=> int ($free) };
1444 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
1445 '--unbuffered', '--nosuffix', '--options',
1446 'vg_name,lv_name,lv_size,uuid,tags'];
1448 push @$cmd, $vgname if $vgname;
1451 run_command
($cmd, outfunc
=> sub {
1454 $line = trim
($line);
1456 my ($vg, $name, $size, $uuid, $tags) = split (':', $line);
1458 return if $name !~ m/^vm-(\d+)-/;
1462 foreach my $tag (split (/,/, $tags)) {
1463 if ($tag =~ m/^pve-vm-(\d+)$/) {
1470 if ($owner ne $nid) {
1471 warn "owner mismatch name = $name, owner = $owner\n";
1474 $lvs->{$vg}->{$name} = { format
=> 'raw', size
=> $size,
1475 uuid
=> $uuid, tags
=> $tags,
1483 #install iso or openvz template ($tt = <iso|vztmpl>)
1484 # we simply overwrite when file already exists
1485 sub install_template
{
1486 my ($cfg, $storeid, $tt, $srcfile, $destfile) = @_;
1488 my $scfg = storage_config
($cfg, $storeid);
1490 my $type = $scfg->{type
};
1492 die "invalid storage type '$type'" if !($type eq 'dir' || $type eq 'nfs');
1497 die "file '$destfile' has no '.iso' extension\n"
1498 if $destfile !~ m![^/]+\.[Ii][Ss][Oo]$!;
1499 die "storage '$storeid' does not support 'iso' content\n"
1500 if !$scfg->{content
}->{iso
};
1501 $path = get_iso_dir
($cfg, $storeid);
1502 } elsif ($tt eq 'vztmpl') {
1503 die "file '$destfile' has no '.tar.gz' extension\n"
1504 if $destfile !~ m![^/]+\.tar\.gz$!;
1505 die "storage '$storeid' does not support 'vztmpl' content\n"
1506 if !$scfg->{content
}->{vztmpl
};
1507 $path = get_vztmpl_dir
($cfg, $storeid);
1509 die "unknown template type '$tt'";
1512 activate_storage
($cfg, $storeid);
1514 my $dest = "$path/$destfile";
1516 my $cmd = ['cp', $srcfile, $dest];
1518 eval { run_command
($cmd); };
1527 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
1529 my ($cfg, $storeid, $tt) = @_;
1531 die "unknown template type '$tt'\n" if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
1533 my $ids = $cfg->{ids
};
1535 storage_check_enabled
($cfg, $storeid) if ($storeid);
1541 foreach my $sid (keys %$ids) {
1542 next if $storeid && $storeid ne $sid;
1544 my $scfg = $ids->{$sid};
1545 my $type = $scfg->{type
};
1547 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1549 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
1550 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
1551 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
1553 activate_storage
($cfg, $sid);
1555 if ($type eq 'dir' || $type eq 'nfs') {
1559 $path = get_iso_dir
($cfg, $sid);
1560 } elsif ($tt eq 'vztmpl') {
1561 $path = get_vztmpl_dir
($cfg, $sid);
1562 } elsif ($tt eq 'backup') {
1563 $path = get_backup_dir
($cfg, $sid);
1565 die "unknown template type '$tt'\n";
1568 foreach my $fn (<$path/*>) {
1573 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
1575 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
1577 } elsif ($tt eq 'vztmpl') {
1578 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
1580 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
1582 } elsif ($tt eq 'backup') {
1583 next if $fn !~ m!/([^/]+\.(tar|tgz))$!;
1585 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
1588 $info->{size
} = -s
$fn;
1590 push @{$res->{$sid}}, $info;
1595 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1601 sub file_size_info
{
1602 my ($filename, $timeout) = @_;
1604 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
1611 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
1614 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
1616 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
1618 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
1622 $used *= 1024 if $u eq 'K';
1623 $used *= (1024*1024) if $u eq 'M';
1624 $used *= (1024*1024*1024) if $u eq 'G';
1625 $used *= (1024*1024*1024*1024) if $u eq 'T';
1632 return wantarray ?
($size, $format, $used) : $size;
1636 my ($cfg, $storeid, $vmid, $vollist) = @_;
1638 my $ids = $cfg->{ids
};
1640 storage_check_enabled
($cfg, $storeid) if ($storeid);
1644 # prepare/activate/refresh all storages
1648 my $storage_list = [];
1650 foreach my $volid (@$vollist) {
1651 my ($sid, undef) = parse_volume_id
($volid);
1652 next if !defined ($ids->{$sid});
1653 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1654 push @$storage_list, $sid;
1655 $stypes->{$ids->{$sid}->{type
}} = 1;
1658 foreach my $sid (keys %$ids) {
1659 next if $storeid && $storeid ne $sid;
1660 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1661 push @$storage_list, $sid;
1662 $stypes->{$ids->{$sid}->{type
}} = 1;
1666 activate_storage_list
($cfg, $storage_list);
1668 my $lvs = $stypes->{lvm
} ? lvm_lvs
() : {};
1670 my $iscsi_devices = iscsi_device_list
() if $stypes->{iscsi
};
1674 foreach my $sid (keys %$ids) {
1676 next if $storeid ne $sid;
1677 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1679 my $scfg = $ids->{$sid};
1680 my $type = $scfg->{type
};
1682 if ($type eq 'dir' || $type eq 'nfs') {
1684 my $path = $scfg->{path
};
1686 my $fmts = join ('|', keys %{$default_config->{$type}->{format
}->[0]});
1688 foreach my $fn (<$path/images/[0-9][0-9]*/*>) {
1690 next if $fn !~ m!^(/.+/images/(\d+)/([^/]+\.($fmts)))$!;
1695 my $volid = "$sid:$owner/$name";
1698 my $found = grep { $_ eq $volid } @$vollist;
1701 next if defined ($vmid) && ($owner ne $vmid);
1704 my ($size, $format, $used) = file_size_info
($fn);
1706 if ($format && $size) {
1707 push @{$res->{$sid}}, {
1708 volid
=> $volid, format
=> $format,
1709 size
=> $size, vmid
=> $owner, used
=> $used };
1714 } elsif ($type eq 'lvm') {
1716 my $vgname = $scfg->{vgname
};
1718 if (my $dat = $lvs->{$vgname}) {
1720 foreach my $volname (keys %$dat) {
1722 my $owner = $dat->{$volname}->{vmid
};
1724 my $volid = "$sid:$volname";
1727 my $found = grep { $_ eq $volid } @$vollist;
1730 next if defined ($vmid) && ($owner ne $vmid);
1733 my $info = $dat->{$volname};
1734 $info->{volid
} = $volid;
1736 push @{$res->{$sid}}, $info;
1740 } elsif ($type eq 'iscsi') {
1742 # we have no owner for iscsi devices
1744 my $target = $scfg->{target
};
1746 if (my $dat = $iscsi_devices->{$target}) {
1748 foreach my $volname (keys %$dat) {
1750 my $volid = "$sid:$volname";
1753 my $found = grep { $_ eq $volid } @$vollist;
1756 next if !($storeid && ($storeid eq $sid));
1759 my $info = $dat->{$volname};
1760 $info->{volid
} = $volid;
1762 push @{$res->{$sid}}, $info;
1770 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1776 sub nfs_is_mounted
{
1777 my ($server, $export, $mountpoint, $mountdata) = @_;
1779 my $source = "$server:$export";
1781 $mountdata = read_proc_mounts
() if !$mountdata;
1783 if ($mountdata =~ m/^$source\s$mountpoint\snfs/m) {
1791 my ($server, $export, $mountpoint, $options) = @_;
1793 my $source = "$server:$export";
1795 my $cmd = ['/bin/mount', '-t', 'nfs', $source, $mountpoint];
1797 push @$cmd, '-o', $options;
1805 my $filename = "/sys/kernel/uevent_seqnum";
1808 if (my $fh = IO
::File-
>new ($filename, "r")) {
1810 if ($line =~ m/^(\d+)$/) {
1818 sub __activate_storage_full
{
1819 my ($cfg, $storeid, $session) = @_;
1821 my $scfg = storage_check_enabled
($cfg, $storeid);
1823 return if $session->{activated
}->{$storeid};
1825 if (!$session->{mountdata
}) {
1826 $session->{mountdata
} = read_proc_mounts
();
1829 if (!$session->{uevent_seqnum
}) {
1830 $session->{uevent_seqnum
} = uevent_seqnum
();
1833 my $mountdata = $session->{mountdata
};
1835 my $type = $scfg->{type
};
1837 if ($type eq 'dir' || $type eq 'nfs') {
1839 my $path = $scfg->{path
};
1841 if ($type eq 'nfs') {
1842 my $server = $scfg->{server
};
1843 my $export = $scfg->{export
};
1845 if (!nfs_is_mounted
($server, $export, $path, $mountdata)) {
1847 # NOTE: only call mkpath when not mounted (avoid hang
1848 # when NFS server is offline
1852 die "unable to activate storage '$storeid' - " .
1853 "directory '$path' does not exist\n" if ! -d
$path;
1855 nfs_mount
($server, $export, $path, $scfg->{options
});
1862 die "unable to activate storage '$storeid' - " .
1863 "directory '$path' does not exist\n" if ! -d
$path;
1866 my $imagedir = get_image_dir
($cfg, $storeid);
1867 my $isodir = get_iso_dir
($cfg, $storeid);
1868 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1869 my $backupdir = get_backup_dir
($cfg, $storeid);
1871 if (defined($scfg->{content
})) {
1872 mkpath
$imagedir if $scfg->{content
}->{images
} &&
1874 mkpath
$isodir if $scfg->{content
}->{iso
} &&
1876 mkpath
$tmpldir if $scfg->{content
}->{vztmpl
} &&
1878 mkpath
$backupdir if $scfg->{content
}->{backup
} &&
1879 $backupdir ne $path;
1882 } elsif ($type eq 'lvm') {
1884 if ($scfg->{base
}) {
1885 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
1886 __activate_storage_full
($cfg, $baseid, $session);
1889 if (!$session->{vgs
}) {
1890 $session->{vgs
} = lvm_vgs
();
1893 # In LVM2, vgscans take place automatically;
1894 # this is just to be sure
1895 if ($session->{vgs
} && !$session->{vgscaned
} &&
1896 !$session->{vgs
}->{$scfg->{vgname
}}) {
1897 $session->{vgscaned
} = 1;
1898 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
1899 eval { run_command
($cmd, outfunc
=> sub {}); };
1903 my $cmd = ['/sbin/vgchange', '-aly', $scfg->{vgname
}];
1904 run_command
($cmd, outfunc
=> sub {});
1906 } elsif ($type eq 'iscsi') {
1908 return if !check_iscsi_support
(1);
1910 $session->{iscsi_sessions
} = iscsi_session_list
()
1911 if !$session->{iscsi_sessions
};
1913 my $iscsi_sess = $session->{iscsi_sessions
}->{$scfg->{target
}};
1914 if (!defined ($iscsi_sess)) {
1915 eval { iscsi_login
($scfg->{target
}, $scfg->{portal
}); };
1918 # make sure we get all devices
1919 iscsi_session_rescan
($iscsi_sess);
1926 my $newseq = uevent_seqnum
();
1928 # only call udevsettle if there are events
1929 if ($newseq > $session->{uevent_seqnum
}) {
1931 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1932 $session->{uevent_seqnum
} = $newseq;
1935 $session->{activated
}->{$storeid} = 1;
1938 sub activate_storage_list
{
1939 my ($cfg, $storeid_list, $session) = @_;
1941 $session = {} if !$session;
1943 foreach my $storeid (@$storeid_list) {
1944 __activate_storage_full
($cfg, $storeid, $session);
1948 sub activate_storage
{
1949 my ($cfg, $storeid) = @_;
1953 __activate_storage_full
($cfg, $storeid, $session);
1956 sub activate_volumes
{
1957 my ($cfg, $vollist) = @_;
1959 my $storagehash = {};
1960 foreach my $volid (@$vollist) {
1961 my ($storeid, undef) = parse_volume_id
($volid);
1962 $storagehash->{$storeid} = 1;
1965 activate_storage_list
($cfg, [keys %$storagehash]);
1967 foreach my $volid (@$vollist) {
1968 my ($storeid, $volname) = parse_volume_id
($volid);
1970 my $scfg = storage_config
($cfg, $storeid);
1972 my $path = path
($cfg, $volid);
1974 if ($scfg->{type
} eq 'lvm') {
1975 my $cmd = ['/sbin/lvchange', '-aly', $path];
1976 eval { run_command
($cmd); };
1980 # check is volume exists
1981 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1982 die "volume '$volid' does not exist\n" if ! -f
$path;
1984 die "volume '$volid' does not exist\n" if ! -b
$path;
1989 sub deactivate_volumes
{
1990 my ($cfg, $vollist) = @_;
1992 my $lvs = lvm_lvs
();
1994 foreach my $volid (@$vollist) {
1995 my ($storeid, $volname) = parse_volume_id
($volid);
1997 my $scfg = storage_config
($cfg, $storeid);
1999 if ($scfg->{type
} eq 'lvm') {
2000 my ($name) = parse_volname_lvm
($volname);
2002 if ($lvs->{$scfg->{vgname
}}->{$name}) {
2003 my $path = path
($cfg, $volid);
2004 my $cmd = ['/sbin/lvchange', '-aln', $path];
2005 eval { run_command
($cmd); };
2012 sub deactivate_storage
{
2013 my ($cfg, $storeid) = @_;
2017 my $scfg = storage_config
($cfg, $storeid);
2019 my $type = $scfg->{type
};
2021 if ($type eq 'dir') {
2023 } elsif ($type eq 'nfs') {
2024 my $mountdata = read_proc_mounts
();
2025 my $server = $scfg->{server
};
2026 my $export = $scfg->{export
};
2027 my $path = $scfg->{path
};
2029 my $cmd = ['/bin/umount', $path];
2031 run_command
($cmd) if nfs_is_mounted
($server, $export, $path, $mountdata);
2032 } elsif ($type eq 'lvm') {
2033 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
2035 } elsif ($type eq 'iscsi') {
2036 my $portal = $scfg->{portal
};
2037 my $target = $scfg->{target
};
2039 my $iscsi_sessions = iscsi_session_list
();
2040 iscsi_logout
($target, $portal)
2041 if defined ($iscsi_sessions->{$target});
2049 my ($cfg, $content) = @_;
2051 my $ids = $cfg->{ids
};
2057 foreach my $storeid (keys %$ids) {
2059 next if $content && !$ids->{$storeid}->{content
}->{$content};
2061 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
2063 my $type = $ids->{$storeid}->{type
};
2065 $info->{$storeid} = {
2070 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
2071 content
=> content_hash_to_string
($ids->{$storeid}->{content
}),
2075 $stypes->{$type} = 1;
2077 push @$slist, $storeid;
2082 my $iscsi_sessions = {};
2085 if ($stypes->{lvm
}) {
2086 $session->{vgs
} = lvm_vgs
();
2087 $vgs = $session->{vgs
};
2089 if ($stypes->{nfs
}) {
2090 $mountdata = read_proc_mounts
();
2091 $session->{mountdata
} = $mountdata;
2093 if ($stypes->{iscsi
}) {
2094 $iscsi_sessions = iscsi_session_list
();
2095 $session->{iscsi_sessions
} = $iscsi_sessions;
2098 eval { activate_storage_list
($cfg, $slist, $session); };
2100 foreach my $storeid (keys %$ids) {
2101 my $scfg = $ids->{$storeid};
2103 next if !$info->{$storeid};
2105 my $type = $scfg->{type
};
2107 if ($type eq 'dir' || $type eq 'nfs') {
2109 my $path = $scfg->{path
};
2111 if ($type eq 'nfs') {
2112 my $server = $scfg->{server
};
2113 my $export = $scfg->{export
};
2115 next if !nfs_is_mounted
($server, $export, $path, $mountdata);
2119 my $res = PVE
::Tools
::df
($path, $timeout);
2121 next if !$res || !$res->{total
};
2123 $info->{$storeid}->{total
} = $res->{total
};
2124 $info->{$storeid}->{avail
} = $res->{avail
};
2125 $info->{$storeid}->{used
} = $res->{used
};
2126 $info->{$storeid}->{active
} = 1;
2128 } elsif ($type eq 'lvm') {
2130 my $vgname = $scfg->{vgname
};
2135 if (defined ($vgs->{$vgname})) {
2136 $total = $vgs->{$vgname}->{size
};
2137 $free = $vgs->{$vgname}->{free
};
2139 $info->{$storeid}->{total
} = $total;
2140 $info->{$storeid}->{avail
} = $free;
2141 $info->{$storeid}->{used
} = $total - $free;
2142 $info->{$storeid}->{active
} = 1;
2145 } elsif ($type eq 'iscsi') {
2147 $info->{$storeid}->{total
} = 0;
2148 $info->{$storeid}->{avail
} = 0;
2149 $info->{$storeid}->{used
} = 0;
2150 $info->{$storeid}->{active
} =
2151 defined ($iscsi_sessions->{$scfg->{target
}});
2164 my $packed_ip = gethostbyname($server);
2165 if (defined $packed_ip) {
2166 return inet_ntoa
($packed_ip);
2172 my ($server_in) = @_;
2175 if (!($server = resolv_server
($server_in))) {
2176 die "unable to resolve address for server '${server_in}'\n";
2179 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
2182 run_command
($cmd, outfunc
=> sub {
2185 # note: howto handle white spaces in export path??
2186 if ($line =~ m!^(/\S+)\s+(.+)$!) {
2195 my ($portal, $noerr) = @_;
2197 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
2201 if (my $ip = resolv_server
($server)) {
2203 return $port ?
"$server:$port" : $server;
2206 return undef if $noerr;
2208 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
2211 # idea is from usbutils package (/usr/bin/usb-devices) script
2212 sub __scan_usb_device
{
2213 my ($res, $devpath, $parent, $level) = @_;
2215 return if ! -d
$devpath;
2216 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
2217 my $port = $level ?
int($1 - 1) : 0;
2219 my $busnum = int(file_read_firstline
("$devpath/busnum"));
2220 my $devnum = int(file_read_firstline
("$devpath/devnum"));
2227 speed
=> file_read_firstline
("$devpath/speed"),
2228 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
2229 vendid
=> file_read_firstline
("$devpath/idVendor"),
2230 prodid
=> file_read_firstline
("$devpath/idProduct"),
2234 my $usbpath = $devpath;
2235 $usbpath =~ s
|^.*/\d
+\
-||;
2236 $d->{usbpath
} = $usbpath;
2239 my $product = file_read_firstline
("$devpath/product");
2240 $d->{product
} = $product if $product;
2242 my $manu = file_read_firstline
("$devpath/manufacturer");
2243 $d->{manufacturer
} = $manu if $manu;
2245 my $serial => file_read_firstline
("$devpath/serial");
2246 $d->{serial
} = $serial if $serial;
2250 foreach my $subdev (<$devpath/$busnum-*>) {
2251 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
2252 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
2261 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
2262 __scan_usb_device
($devlist, $device, 0, 0);
2269 my ($portal_in) = @_;
2272 if (!($portal = resolv_portal
($portal_in))) {
2273 die "unable to parse/resolve portal address '${portal_in}'\n";
2276 return iscsi_discovery
($portal);
2279 sub storage_default_format
{
2280 my ($cfg, $storeid) = @_;
2282 my $scfg = storage_config
($cfg, $storeid);
2284 my $def = $default_config->{$scfg->{type
}};
2286 my $def_format = 'raw';
2287 my $valid_formats = [ $def_format ];
2289 if (defined ($def->{format
})) {
2290 $def_format = $scfg->{format
} || $def->{format
}->[1];
2291 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
2294 return wantarray ?
($def_format, $valid_formats) : $def_format;
2297 sub vgroup_is_used
{
2298 my ($cfg, $vgname) = @_;
2300 foreach my $storeid (keys %{$cfg->{ids
}}) {
2301 my $scfg = storage_config
($cfg, $storeid);
2302 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
2310 sub target_is_used
{
2311 my ($cfg, $target) = @_;
2313 foreach my $storeid (keys %{$cfg->{ids
}}) {
2314 my $scfg = storage_config
($cfg, $storeid);
2315 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
2323 sub volume_is_used
{
2324 my ($cfg, $volid) = @_;
2326 foreach my $storeid (keys %{$cfg->{ids
}}) {
2327 my $scfg = storage_config
($cfg, $storeid);
2328 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
2336 sub storage_is_used
{
2337 my ($cfg, $storeid) = @_;
2339 foreach my $sid (keys %{$cfg->{ids
}}) {
2340 my $scfg = storage_config
($cfg, $sid);
2341 next if !$scfg->{base
};
2342 my ($st) = parse_volume_id
($scfg->{base
});
2343 return 1 if $st && $st eq $storeid;
2350 my ($list, $func) = @_;
2354 foreach my $sid (keys %$list) {
2355 foreach my $info (@{$list->{$sid}}) {
2356 my $volid = $info->{volid
};
2357 my ($sid1, $volname) = parse_volume_id
($volid, 1);
2358 if ($sid1 && $sid1 eq $sid) {
2359 &$func ($volid, $sid, $info);
2361 warn "detected strange volid '$volid' in volume list for '$sid'\n";