]>
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 dir_glob_regex dir_glob_foreach);
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);
24 use PVE
::RPCEnvironment
;
26 my $ISCSIADM = '/usr/bin/iscsiadm';
27 my $UDEVADM = '/sbin/udevadm';
29 $ISCSIADM = undef if ! -X
$ISCSIADM;
31 # fixme: always_call_parser => 1 ??
32 cfs_register_file
('storage.cfg',
36 # generic utility function
39 return cfs_read_file
("storage.cfg");
42 sub check_iscsi_support
{
46 my $msg = "no iscsi support - please install open-iscsi";
48 warn "warning: $msg\n";
58 sub load_stable_scsi_paths
{
60 my $stable_paths = {};
62 my $stabledir = "/dev/disk/by-id";
64 if (my $dh = IO
::Dir-
>new($stabledir)) {
65 while (defined(my $tmp = $dh->read)) {
66 # exclude filenames with part in name (same disk but partitions)
67 # use only filenames with scsi(with multipath i have the same device
68 # with dm-uuid-mpath , dm-name and scsi in name)
69 if($tmp !~ m/-part\d+$/ && $tmp =~ m/^scsi-/) {
70 my $path = "$stabledir/$tmp";
71 my $bdevdest = readlink($path);
72 if ($bdevdest && $bdevdest =~ m
|^../../([^/]+)|) {
73 $stable_paths->{$1}=$tmp;
82 sub read_proc_mounts
{
84 local $/; # enable slurp mode
87 if (my $fd = IO
::File-
>new ("/proc/mounts", "r")) {
95 # PVE::Storage utility functions
97 sub lock_storage_config
{
98 my ($code, $errmsg) = @_;
100 cfs_lock_file
("storage.cfg", undef, $code);
103 $errmsg ?
die "$errmsg: $err" : die $err;
111 saferemove
=> 'bool',
113 content
=> 'content',
121 options
=> 'options',
122 maxfiles
=> 'natural',
125 my $required_config = {
127 nfs
=> ['path', 'server', 'export'],
129 iscsi
=> ['portal', 'target'],
134 nfs
=> ['path', 'server', 'export'],
135 lvm
=> ['vgname', 'base'],
136 iscsi
=> ['portal', 'target'],
139 my $default_config = {
146 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, backup
=> 1, none
=> 1 },
147 { images
=> 1, rootdir
=> 1 }],
148 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
159 content
=> [ { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, backup
=> 1},
161 format
=> [ { raw
=> 1, qcow2
=> 1, vmdk
=> 1 } , 'raw' ],
170 content
=> [ {images
=> 1}, { images
=> 1 }],
179 content
=> [ {images
=> 1, none
=> 1}, { images
=> 1 }],
183 sub valid_content_types
{
186 my $def = $default_config->{$stype};
190 return $def->{content
}->[0];
193 sub content_hash_to_string
{
197 foreach my $ct (keys %$hash) {
198 push @cta, $ct if $hash->{$ct};
201 return join(',', @cta);
204 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
206 my ($path, $noerr) = @_;
208 # fixme: exclude more shell meta characters?
209 # we need absolute paths
210 if ($path !~ m
|^/[^;\
(\
)]+|) {
211 return undef if $noerr;
212 die "value does not look like a valid absolute path\n";
217 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
219 my ($server, $noerr) = @_;
221 # fixme: use better regex ?
223 if ($server !~ m/^[[:alnum:]\-\.]+$/) {
224 return undef if $noerr;
225 die "value does not look like a valid server name or IP address\n";
230 PVE
::JSONSchema
::register_format
('pve-storage-portal', \
&verify_portal
);
232 my ($portal, $noerr) = @_;
234 # IP with optional port
235 if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
236 return undef if $noerr;
237 die "value does not look like a valid portal address\n";
242 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
243 sub verify_portal_dns
{
244 my ($portal, $noerr) = @_;
246 # IP or DNS name with optional port
247 if ($portal !~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[[:alnum:]\-\.]+)(:\d+)?$/) {
248 return undef if $noerr;
249 die "value does not look like a valid portal address\n";
254 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
256 my ($ct, $noerr) = @_;
258 my $valid_content = valid_content_types
('dir'); # dir includes all types
260 if (!$valid_content->{$ct}) {
261 return undef if $noerr;
262 die "invalid content type '$ct'\n";
268 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
270 my ($fmt, $noerr) = @_;
272 if ($fmt !~ m/(raw|qcow2|vmdk)/) {
273 return undef if $noerr;
274 die "invalid format '$fmt'\n";
280 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
282 my ($value, $noerr) = @_;
284 # mount options (see man fstab)
285 if ($value !~ m/^\S+$/) {
286 return undef if $noerr;
287 die "invalid options '$value'\n";
294 my ($stype, $ct, $key, $value, $storeid, $noerr) = @_;
296 my $def = $default_config->{$stype};
298 if (!$def) { # should not happen
299 return undef if $noerr;
300 die "unknown storage type '$stype'\n";
303 if (!defined($def->{$key})) {
304 return undef if $noerr;
305 die "unexpected property\n";
308 if (!defined ($value)) {
309 return undef if $noerr;
310 die "got undefined value\n";
313 if ($value =~ m/[\n\r]/) {
314 return undef if $noerr;
315 die "property contains a line feed\n";
319 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
320 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
321 return undef if $noerr;
322 die "type check ('boolean') failed - got '$value'\n";
323 } elsif ($ct eq 'options') {
324 return verify_options
($value, $noerr);
325 } elsif ($ct eq 'path') {
326 return verify_path
($value, $noerr);
327 } elsif ($ct eq 'server') {
328 return verify_server
($value, $noerr);
329 } elsif ($ct eq 'vgname') {
330 return parse_lvm_name
($value, $noerr);
331 } elsif ($ct eq 'portal') {
332 return verify_portal
($value, $noerr);
333 } elsif ($ct eq 'natural') {
334 return int($value) if $value =~ m/^\d+$/;
335 return undef if $noerr;
336 die "type check ('natural') failed - got '$value'\n";
337 } elsif ($ct eq 'nodes') {
340 foreach my $node (PVE
::Tools
::split_list
($value)) {
341 if (PVE
::JSONSchema
::pve_verify_node_name
($node, $noerr)) {
346 # no node restrictions for local storage
347 if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
348 return undef if $noerr;
349 die "storage '$storeid' does not allow node restrictions\n";
353 } elsif ($ct eq 'target') {
355 } elsif ($ct eq 'string') {
357 } elsif ($ct eq 'format') {
358 my $valid_formats = $def->{format
}->[0];
360 if (!$valid_formats->{$value}) {
361 return undef if $noerr;
362 die "storage does not support format '$value'\n";
367 } elsif ($ct eq 'content') {
368 my $valid_content = $def->{content
}->[0];
372 foreach my $c (PVE
::Tools
::split_list
($value)) {
373 if (!$valid_content->{$c}) {
374 return undef if $noerr;
375 die "storage does not support content type '$c'\n";
380 if ($res->{none
} && scalar (keys %$res) > 1) {
381 return undef if $noerr;
382 die "unable to combine 'none' with other content types\n";
386 } elsif ($ct eq 'volume') {
387 return $value if parse_volume_id
($value, $noerr);
390 return undef if $noerr;
391 die "type check not implemented - internal error\n";
395 my ($filename, $raw) = @_;
399 my $digest = Digest
::SHA
::sha1_hex
(defined($raw) ?
$raw : '');
403 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
406 next if $line =~ m/^\#/;
407 next if $line =~ m/^\s*$/;
409 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
414 if (!PVE
::JSONSchema
::parse_storage_id
($storeid, 1)) {
416 warn "ignoring storage '$storeid' - (illegal characters)\n";
417 } elsif (!$default_config->{$type}) {
419 warn "ignoring storage '$storeid' (unsupported type '$type')\n";
421 $ids->{$storeid}->{type
} = $type;
422 $ids->{$storeid}->{priority
} = $pri++;
425 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
428 next if $line =~ m/^\#/;
429 last if $line =~ m/^\s*$/;
431 next if $ignore; # skip
433 if ($line =~ m/^\s+(\S+)(\s+(.*\S))?\s*$/) {
434 my ($k, $v) = ($1, $3);
435 if (my $ct = $confvars->{$k}) {
436 $v = 1 if $ct eq 'bool' && !defined($v);
438 $ids->{$storeid}->{$k} = check_type
($type, $ct, $k, $v, $storeid);
440 warn "storage '$storeid' - unable to parse value of '$k': $@" if $@;
442 warn "storage '$storeid' - unable to parse value of '$k'\n";
446 warn "storage '$storeid' - ignore config line: $line\n";
450 warn "ignore config line: $line\n";
454 # make sure we have a reasonable 'local:' storage
455 # openvz expects things to be there
456 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
457 $ids->{local}->{path
} ne '/var/lib/vz') {
461 path
=> '/var/lib/vz',
462 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
466 # we always need this for OpenVZ
467 $ids->{local}->{content
}->{rootdir
} = 1;
468 $ids->{local}->{content
}->{vztmpl
} = 1;
469 delete ($ids->{local}->{disable
});
471 # remove node restrictions for local storage
472 delete($ids->{local}->{nodes
});
474 foreach my $storeid (keys %$ids) {
475 my $d = $ids->{$storeid};
477 my $req_keys = $required_config->{$d->{type
}};
478 foreach my $k (@$req_keys) {
479 if (!defined ($d->{$k})) {
480 warn "ignoring storage '$storeid' - missing value " .
481 "for required option '$k'\n";
482 delete $ids->{$storeid};
487 my $def = $default_config->{$d->{type
}};
489 if ($def->{content
}) {
490 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
493 if ($d->{type
} eq 'iscsi' || $d->{type
} eq 'nfs') {
498 my $cfg = { ids
=> $ids, digest
=> $digest};
504 my ($storeid, $stype, $param, $create) = @_;
506 my $settings = { type
=> $stype };
508 die "unknown storage type '$stype'\n"
509 if !$default_config->{$stype};
511 foreach my $opt (keys %$param) {
512 my $value = $param->{$opt};
514 my $ct = $confvars->{$opt};
515 if (defined($value)) {
517 $settings->{$opt} = check_type
($stype, $ct, $opt, $value, $storeid);
519 raise_param_exc
({ $opt => $@ }) if $@;
521 raise_param_exc
({ $opt => "got undefined value" });
526 my $req_keys = $required_config->{$stype};
527 foreach my $k (@$req_keys) {
529 if ($stype eq 'nfs' && !$settings->{path
}) {
530 $settings->{path
} = "/mnt/pve/$storeid";
533 # check if we have a value for all required options
534 if (!defined ($settings->{$k})) {
535 raise_param_exc
({ $k => "property is missing and it is not optional" });
539 my $fixed_keys = $fixed_config->{$stype};
540 foreach my $k (@$fixed_keys) {
542 # only allow to change non-fixed values
544 if (defined ($settings->{$k})) {
545 raise_param_exc
({$k => "can't change value (fixed parameter)"});
553 sub cluster_lock_storage
{
554 my ($storeid, $shared, $timeout, $func, @param) = @_;
558 my $lockid = "pve-storage-$storeid";
559 my $lockdir = "/var/lock/pve-manager";
561 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
564 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
571 my ($cfg, $storeid, $noerr) = @_;
573 die "no storage id specified\n" if !$storeid;
575 my $scfg = $cfg->{ids
}->{$storeid};
577 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
582 sub storage_check_node
{
583 my ($cfg, $storeid, $node, $noerr) = @_;
585 my $scfg = storage_config
($cfg, $storeid);
587 if ($scfg->{nodes
}) {
588 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
589 if (!$scfg->{nodes
}->{$node}) {
590 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
598 sub storage_check_enabled
{
599 my ($cfg, $storeid, $node, $noerr) = @_;
601 my $scfg = storage_config
($cfg, $storeid);
603 if ($scfg->{disable
}) {
604 die "storage '$storeid' is disabled\n" if !$noerr;
608 return storage_check_node
($cfg, $storeid, $node, $noerr);
614 my $ids = $cfg->{ids
};
616 my @sa = sort {$ids->{$a}->{priority
} <=> $ids->{$b}->{priority
}} keys %$ids;
621 sub assert_if_modified
{
622 my ($cfg, $digest) = @_;
624 if ($digest && ($cfg->{digest
} ne $digest)) {
625 die "detected modified storage configuration - try again\n";
629 sub sprint_config_line
{
632 my $ct = $confvars->{$k};
635 return $v ?
"\t$k\n" : '';
636 } elsif ($ct eq 'nodes') {
637 my $nlist = join(',', keys(%$v));
638 return $nlist ?
"\tnodes $nlist\n" : '';
639 } elsif ($ct eq 'content') {
640 my $clist = content_hash_to_string
($v);
642 return "\t$k $clist\n";
644 return "\t$k none\n";
652 my ($filename, $cfg) = @_;
656 my $ids = $cfg->{ids
};
659 foreach my $storeid (keys %$ids) {
660 my $pri = $ids->{$storeid}->{priority
};
661 $maxpri = $pri if $pri && $pri > $maxpri;
663 foreach my $storeid (keys %$ids) {
664 if (!defined ($ids->{$storeid}->{priority
})) {
665 $ids->{$storeid}->{priority
} = ++$maxpri;
669 foreach my $storeid (sort {$ids->{$a}->{priority
} <=> $ids->{$b}->{priority
}} keys %$ids) {
670 my $scfg = $ids->{$storeid};
671 my $type = $scfg->{type
};
672 my $def = $default_config->{$type};
674 die "unknown storage type '$type'\n" if !$def;
676 my $data = "$type: $storeid\n";
678 $data .= "\tdisable\n" if $scfg->{disable
};
680 my $done_hash = { disable
=> 1};
681 foreach my $k (@{$required_config->{$type}}) {
682 $done_hash->{$k} = 1;
683 my $v = $ids->{$storeid}->{$k};
684 die "storage '$storeid' - missing value for required option '$k'\n"
686 $data .= sprint_config_line
($k, $v);
689 foreach my $k (keys %$def) {
690 next if defined ($done_hash->{$k});
691 my $v = $ids->{$storeid}->{$k};
692 next if !defined($v);
693 $data .= sprint_config_line
($k, $v);
703 my ($cfg, $storeid, $vmid) = @_;
705 my $path = $cfg->{ids
}->{$storeid}->{path
};
706 return $vmid ?
"$path/images/$vmid" : "$path/images";
709 sub get_private_dir
{
710 my ($cfg, $storeid, $vmid) = @_;
712 my $path = $cfg->{ids
}->{$storeid}->{path
};
713 return $vmid ?
"$path/private/$vmid" : "$path/private";
717 my ($cfg, $storeid) = @_;
719 my $isodir = $cfg->{ids
}->{$storeid}->{path
};
720 $isodir .= '/template/iso';
726 my ($cfg, $storeid) = @_;
728 my $tmpldir = $cfg->{ids
}->{$storeid}->{path
};
729 $tmpldir .= '/template/cache';
735 my ($cfg, $storeid) = @_;
737 my $dir = $cfg->{ids
}->{$storeid}->{path
};
743 # iscsi utility functions
745 sub iscsi_session_list
{
747 check_iscsi_support
();
749 my $cmd = [$ISCSIADM, '--mode', 'session'];
753 run_command
($cmd, outfunc
=> sub {
756 if ($line =~ m/^tcp:\s+\[(\S+)\]\s+\S+\s+(\S+)\s*$/) {
757 my ($session, $target) = ($1, $2);
758 # there can be several sessions per target (multipath)
759 push @{$res->{$target}}, $session;
767 sub iscsi_test_portal
{
770 my ($server, $port) = split(':', $portal);
771 my $p = Net
::Ping-
>new("tcp", 2);
772 $p->port_number($port || 3260);
773 return $p->ping($server);
776 sub iscsi_discovery
{
779 check_iscsi_support
();
781 my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets',
782 '--portal', $portal];
786 return $res if !iscsi_test_portal
($portal); # fixme: raise exception here?
788 run_command
($cmd, outfunc
=> sub {
791 if ($line =~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\,\S+\s+(\S+)\s*$/) {
794 # one target can have more than one portal (multipath).
795 push @{$res->{$target}}, $portal;
803 my ($target, $portal_in) = @_;
805 check_iscsi_support
();
807 eval { iscsi_discovery
($portal_in); };
810 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--login'];
815 my ($target, $portal) = @_;
817 check_iscsi_support
();
819 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--logout'];
823 my $rescan_filename = "/var/run/pve-iscsi-rescan.lock";
825 sub iscsi_session_rescan
{
826 my $session_list = shift;
828 check_iscsi_support
();
830 my $rstat = stat ($rescan_filename);
833 if (my $fh = IO
::File-
>new ($rescan_filename, "a")) {
834 utime undef, undef, $fh;
838 my $atime = $rstat->atime;
839 my $tdiff = time() - $atime;
840 # avoid frequent rescans
841 return if !($tdiff < 0 || $tdiff > 10);
842 utime undef, undef, $rescan_filename;
845 foreach my $session (@$session_list) {
846 my $cmd = [$ISCSIADM, '--mode', 'session', '-r', $session, '-R'];
847 eval { run_command
($cmd, outfunc
=> sub {}); };
852 sub iscsi_device_list
{
856 my $dirname = '/sys/class/iscsi_session';
858 my $stable_paths = load_stable_scsi_paths
();
860 dir_glob_foreach
($dirname, 'session(\d+)', sub {
861 my ($ent, $session) = @_;
863 my $target = file_read_firstline
("$dirname/$ent/targetname");
866 my (undef, $host) = dir_glob_regex
("$dirname/$ent/device", 'target(\d+):.*');
867 return if !defined($host);
869 dir_glob_foreach
("/sys/bus/scsi/devices", "$host:" . '(\d+):(\d+):(\d+)', sub {
870 my ($tmp, $channel, $id, $lun) = @_;
872 my $type = file_read_firstline
("/sys/bus/scsi/devices/$tmp/type");
873 return if !defined($type) || $type ne '0'; # list disks only
876 if (-d
"/sys/bus/scsi/devices/$tmp/block") { # newer kernels
877 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp/block/", '([A-Za-z]\S*)');
879 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp", 'block:(\S+)');
884 if (-d
"/sys/block/$bdev/holders") {
885 my $multipathdev = dir_glob_regex
("/sys/block/$bdev/holders", '[A-Za-z]\S*');
886 $bdev = $multipathdev if $multipathdev;
889 my $blockdev = $stable_paths->{$bdev};
890 return if !$blockdev;
892 my $size = file_read_firstline
("/sys/block/$bdev/size");
895 my $volid = "$channel.$id.$lun.$blockdev";
897 $res->{$target}->{$volid} = {
899 'size' => int($size * 512),
900 'vmid' => 0, # not assigned to any vm
901 'channel' => int($channel),
906 #print "TEST: $target $session $host,$bus,$tg,$lun $blockdev\n";
914 # library implementation
916 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
918 my ($name, $noerr) = @_;
920 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
921 return undef if $noerr;
922 die "lvm name '$name' contains illegal characters\n";
931 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
936 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
937 sub parse_volume_id
{
938 my ($volid, $noerr) = @_;
940 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
941 return wantarray ?
($1, $2) : $1;
943 return undef if $noerr;
944 die "unable to parse volume ID '$volid'\n";
950 if ($name =~ m!^([^/\s]+\.(raw|qcow2|vmdk))$!) {
954 die "unable to parse volume filename '$name'\n";
957 sub parse_volname_dir
{
960 if ($volname =~ m!^(\d+)/(\S+)$!) {
961 my ($vmid, $name) = ($1, $2);
962 parse_name_dir
($name);
963 return ('image', $name, $vmid);
964 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
966 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
967 return ('vztmpl', $1);
968 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
969 return ('rootdir', $1, $1);
970 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz)))$!) {
972 if ($fn =~ m/^vzdump-(openvz|qemu)-(\d+)-.+/) {
973 return ('backup', $fn, $2);
975 return ('backup', $fn);
977 die "unable to parse directory volume name '$volname'\n";
980 sub parse_volname_lvm
{
983 parse_lvm_name
($volname);
985 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
989 die "unable to parse lvm volume name '$volname'\n";
992 sub parse_volname_iscsi
{
995 if ($volname =~ m!^\d+\.\d+\.\d+\.(\S+)$!) {
1000 die "unable to parse iscsi volume name '$volname'\n";
1003 # try to map a filesystem path to a volume identifier
1004 sub path_to_volume_id
{
1005 my ($cfg, $path) = @_;
1007 my $ids = $cfg->{ids
};
1009 my ($sid, $volname) = parse_volume_id
($path, 1);
1011 if ($ids->{$sid} && (my $type = $ids->{$sid}->{type
})) {
1012 if ($type eq 'dir' || $type eq 'nfs') {
1013 my ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1014 return ($vtype, $path);
1020 $path = abs_path
($path);
1022 foreach my $sid (keys %$ids) {
1023 my $type = $ids->{$sid}->{type
};
1024 next if !($type eq 'dir' || $type eq 'nfs');
1026 my $imagedir = get_image_dir
($cfg, $sid);
1027 my $isodir = get_iso_dir
($cfg, $sid);
1028 my $tmpldir = get_vztmpl_dir
($cfg, $sid);
1029 my $backupdir = get_backup_dir
($cfg, $sid);
1030 my $privatedir = get_private_dir
($cfg, $sid);
1032 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
1035 return ('image', "$sid:$vmid/$name");
1036 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
1038 return ('iso', "$sid:iso/$name");
1039 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
1041 return ('vztmpl', "$sid:vztmpl/$name");
1042 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
1044 return ('rootdir', "$sid:rootdir/$vmid");
1045 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
1047 return ('iso', "$sid:backup/$name");
1051 # can't map path to volume id
1056 my ($cfg, $volid) = @_;
1058 my ($storeid, $volname) = parse_volume_id
($volid);
1060 my $scfg = storage_config
($cfg, $storeid);
1064 my $vtype = 'image';
1066 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1068 ($vtype, $name, $vmid) = parse_volname_dir
($volname);
1071 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1072 my $isodir = get_iso_dir
($cfg, $storeid);
1073 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1074 my $backupdir = get_backup_dir
($cfg, $storeid);
1075 my $privatedir = get_private_dir
($cfg, $storeid);
1077 if ($vtype eq 'image') {
1078 $path = "$imagedir/$name";
1079 } elsif ($vtype eq 'iso') {
1080 $path = "$isodir/$name";
1081 } elsif ($vtype eq 'vztmpl') {
1082 $path = "$tmpldir/$name";
1083 } elsif ($vtype eq 'rootdir') {
1084 $path = "$privatedir/$name";
1085 } elsif ($vtype eq 'backup') {
1086 $path = "$backupdir/$name";
1088 die "should not be reached";
1091 } elsif ($scfg->{type
} eq 'lvm') {
1093 my $vg = $scfg->{vgname
};
1095 my ($name, $vmid) = parse_volname_lvm
($volname);
1098 $path = "/dev/$vg/$name";
1100 } elsif ($scfg->{type
} eq 'iscsi') {
1101 my $byid = parse_volname_iscsi
($volname);
1102 $path = "/dev/disk/by-id/$byid";
1104 die "unknown storage type '$scfg->{type}'";
1107 return wantarray ?
($path, $owner, $vtype) : $path;
1110 sub storage_migrate
{
1111 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
1113 my ($storeid, $volname) = parse_volume_id
($volid);
1114 $target_volname = $volname if !$target_volname;
1116 my $scfg = storage_config
($cfg, $storeid);
1118 # no need to migrate shared content
1119 return if $storeid eq $target_storeid && $scfg->{shared
};
1121 my $tcfg = storage_config
($cfg, $target_storeid);
1123 my $target_volid = "${target_storeid}:${target_volname}";
1125 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
1127 # blowfish is a fast block cipher, much faster then 3des
1128 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
1129 my $ssh = "/usr/bin/ssh $sshoptions";
1131 local $ENV{RSYNC_RSH
} = $ssh;
1133 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1134 if ($tcfg->{type
} eq 'dir' || $tcfg->{type
} eq 'nfs') {
1136 my $src = path
($cfg, $volid);
1137 my $dst = path
($cfg, $target_volid);
1139 my $dirname = dirname
($dst);
1141 if ($tcfg->{shared
}) { # we can do a local copy
1143 run_command
(['/bin/mkdir', '-p', $dirname]);
1145 run_command
(['/bin/cp', $src, $dst]);
1149 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1150 '/bin/mkdir', '-p', $dirname]);
1152 # we use rsync with --sparse, so we can't use --inplace,
1153 # so we remove file on the target if it already exists to
1155 my ($size, $format) = file_size_info
($src);
1156 if ($format && ($format eq 'raw') && $size) {
1157 run_command
(['/usr/bin/ssh', "root\@${target_host}",
1162 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
1163 $src, "root\@${target_host}:$dst"];
1167 run_command
($cmd, outfunc
=> sub {
1170 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
1171 if ($2 > $percent) {
1173 print "rsync status: $1\n";
1186 die "$errstr - target type '$tcfg->{type}' not implemented\n";
1190 die "$errstr - source type '$scfg->{type}' not implemented\n";
1195 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
1197 die "no storage id specified\n" if !$storeid;
1199 PVE
::JSONSchema
::parse_storage_id
($storeid);
1201 my $scfg = storage_config
($cfg, $storeid);
1203 die "no VMID specified\n" if !$vmid;
1205 $vmid = parse_vmid
($vmid);
1207 my $defformat = storage_default_format
($cfg, $storeid);
1209 $fmt = $defformat if !$fmt;
1211 activate_storage
($cfg, $storeid);
1213 # lock shared storage
1214 return cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1216 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1218 my $imagedir = get_image_dir
($cfg, $storeid, $vmid);
1224 for (my $i = 1; $i < 100; $i++) {
1225 my @gr = <$imagedir/vm-$vmid-disk
-$i.*>;
1227 $name = "vm-$vmid-disk-$i.$fmt";
1233 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1236 my (undef, $tmpfmt) = parse_name_dir
($name);
1238 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
1241 my $path = "$imagedir/$name";
1243 die "disk image '$path' already exists\n" if -e
$path;
1245 run_command
("/usr/bin/qemu-img create -f $fmt '$path' ${size}K",
1246 errmsg
=> "unable to create image");
1248 return "$storeid:$vmid/$name";
1250 } elsif ($scfg->{type
} eq 'lvm') {
1252 die "unsupported format '$fmt'" if $fmt ne 'raw';
1254 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
1255 if $name && $name !~ m/^vm-$vmid-/;
1257 my $vgs = lvm_vgs
();
1259 my $vg = $scfg->{vgname
};
1261 die "no such volume gruoup '$vg'\n" if !defined ($vgs->{$vg});
1263 my $free = int ($vgs->{$vg}->{free
});
1265 die "not enough free space ($free < $size)\n" if $free < $size;
1268 my $lvs = lvm_lvs
($vg);
1270 for (my $i = 1; $i < 100; $i++) {
1271 my $tn = "vm-$vmid-disk-$i";
1272 if (!defined ($lvs->{$vg}->{$tn})) {
1279 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
1282 my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
1284 run_command
($cmd, errmsg
=> "lvcreate '$vg/pve-vm-$vmid' error");
1286 return "$storeid:$name";
1288 } elsif ($scfg->{type
} eq 'iscsi') {
1289 die "can't allocate space in iscsi storage\n";
1291 die "unknown storage type '$scfg->{type}'";
1297 my ($cfg, $volid) = @_;
1299 my ($storeid, $volname) = parse_volume_id
($volid);
1301 my $scfg = storage_config
($cfg, $storeid);
1303 activate_storage
($cfg, $storeid);
1305 # we need to zero out LVM data for security reasons
1306 # and to allow thin provisioning
1310 # lock shared storage
1311 cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1313 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1314 my $path = path
($cfg, $volid);
1317 warn "disk image '$path' does not exists\n";
1321 } elsif ($scfg->{type
} eq 'lvm') {
1323 if ($scfg->{saferemove
}) {
1324 # avoid long running task, so we only rename here
1325 $vg = $scfg->{vgname
};
1326 my $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
1327 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
1329 my $tmpvg = $scfg->{vgname
};
1330 my $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
1331 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
1334 } elsif ($scfg->{type
} eq 'iscsi') {
1335 die "can't free space in iscsi storage\n";
1337 die "unknown storage type '$scfg->{type}'";
1343 my $zero_out_worker = sub {
1344 print "zero-out data on image $volname\n";
1345 my $cmd = ['dd', "if=/dev/zero", "of=/dev/$vg/del-$volname", "bs=1M"];
1346 eval { run_command
($cmd, errmsg
=> "zero out failed"); };
1349 cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
1350 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
1351 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
1353 print "successfully removed volume $volname\n";
1356 my $rpcenv = PVE
::RPCEnvironment
::get
();
1357 my $authuser = $rpcenv->get_user();
1359 $rpcenv->fork_worker('imgdel', undef, $authuser, $zero_out_worker);
1362 # lvm utility functions
1367 die "no device specified" if !$device;
1371 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
1372 run_command
($cmd, outfunc
=> sub {
1374 $has_label = 1 if $line =~ m/LVM2/;
1377 return undef if !$has_label;
1379 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
1380 '--unbuffered', '--nosuffix', '--options',
1381 'pv_name,pv_size,vg_name,pv_uuid', $device];
1384 run_command
($cmd, outfunc
=> sub {
1387 $line = trim
($line);
1389 my ($pvname, $size, $vgname, $uuid) = split (':', $line);
1391 die "found multiple pvs entries for device '$device'\n"
1405 sub clear_first_sector
{
1408 if (my $fh = IO
::File-
>new ($dev, "w")) {
1415 sub lvm_create_volume_group
{
1416 my ($device, $vgname, $shared) = @_;
1418 my $res = lvm_pv_info
($device);
1420 if ($res->{vgname
}) {
1421 return if $res->{vgname
} eq $vgname; # already created
1422 die "device '$device' is already used by volume group '$res->{vgname}'\n";
1425 clear_first_sector
($device); # else pvcreate fails
1427 # we use --metadatasize 250k, which reseults in "pe_start = 512"
1428 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
1429 my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
1431 run_command
($cmd, errmsg
=> "pvcreate '$device' error");
1433 $cmd = ['/sbin/vgcreate', $vgname, $device];
1434 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
1436 run_command
($cmd, errmsg
=> "vgcreate $vgname $device error");
1441 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
1442 '--unbuffered', '--nosuffix', '--options',
1443 'vg_name,vg_size,vg_free'];
1447 run_command
($cmd, outfunc
=> sub {
1450 $line = trim
($line);
1452 my ($name, $size, $free) = split (':', $line);
1454 $vgs->{$name} = { size
=> int ($size), free
=> int ($free) };
1459 # just warn (vgs return error code 5 if clvmd does not run)
1460 # but output is still OK (list without clustered VGs)
1469 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
1470 '--unbuffered', '--nosuffix', '--options',
1471 'vg_name,lv_name,lv_size,uuid,tags'];
1473 push @$cmd, $vgname if $vgname;
1476 run_command
($cmd, outfunc
=> sub {
1479 $line = trim
($line);
1481 my ($vg, $name, $size, $uuid, $tags) = split (':', $line);
1483 return if $name !~ m/^vm-(\d+)-/;
1487 foreach my $tag (split (/,/, $tags)) {
1488 if ($tag =~ m/^pve-vm-(\d+)$/) {
1495 if ($owner ne $nid) {
1496 warn "owner mismatch name = $name, owner = $owner\n";
1499 $lvs->{$vg}->{$name} = { format
=> 'raw', size
=> $size,
1500 uuid
=> $uuid, tags
=> $tags,
1508 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
1510 my ($cfg, $storeid, $tt) = @_;
1512 die "unknown template type '$tt'\n" if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
1514 my $ids = $cfg->{ids
};
1516 storage_check_enabled
($cfg, $storeid) if ($storeid);
1522 foreach my $sid (keys %$ids) {
1523 next if $storeid && $storeid ne $sid;
1525 my $scfg = $ids->{$sid};
1526 my $type = $scfg->{type
};
1528 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1530 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
1531 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
1532 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
1534 activate_storage
($cfg, $sid);
1536 if ($type eq 'dir' || $type eq 'nfs') {
1540 $path = get_iso_dir
($cfg, $sid);
1541 } elsif ($tt eq 'vztmpl') {
1542 $path = get_vztmpl_dir
($cfg, $sid);
1543 } elsif ($tt eq 'backup') {
1544 $path = get_backup_dir
($cfg, $sid);
1546 die "unknown template type '$tt'\n";
1549 foreach my $fn (<$path/*>) {
1554 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
1556 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
1558 } elsif ($tt eq 'vztmpl') {
1559 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
1561 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
1563 } elsif ($tt eq 'backup') {
1564 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
1566 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
1569 $info->{size
} = -s
$fn;
1571 push @{$res->{$sid}}, $info;
1576 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1582 sub file_size_info
{
1583 my ($filename, $timeout) = @_;
1585 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
1592 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
1595 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
1597 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
1599 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
1603 $used *= 1024 if $u eq 'K';
1604 $used *= (1024*1024) if $u eq 'M';
1605 $used *= (1024*1024*1024) if $u eq 'G';
1606 $used *= (1024*1024*1024*1024) if $u eq 'T';
1613 return wantarray ?
($size, $format, $used) : $size;
1617 my ($cfg, $storeid, $vmid, $vollist) = @_;
1619 my $ids = $cfg->{ids
};
1621 storage_check_enabled
($cfg, $storeid) if ($storeid);
1625 # prepare/activate/refresh all storages
1629 my $storage_list = [];
1631 foreach my $volid (@$vollist) {
1632 my ($sid, undef) = parse_volume_id
($volid);
1633 next if !defined ($ids->{$sid});
1634 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1635 push @$storage_list, $sid;
1636 $stypes->{$ids->{$sid}->{type
}} = 1;
1639 foreach my $sid (keys %$ids) {
1640 next if $storeid && $storeid ne $sid;
1641 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1642 push @$storage_list, $sid;
1643 $stypes->{$ids->{$sid}->{type
}} = 1;
1647 activate_storage_list
($cfg, $storage_list);
1649 my $lvs = $stypes->{lvm
} ? lvm_lvs
() : {};
1651 my $iscsi_devices = iscsi_device_list
() if $stypes->{iscsi
};
1655 foreach my $sid (keys %$ids) {
1657 next if $storeid ne $sid;
1658 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1660 my $scfg = $ids->{$sid};
1661 my $type = $scfg->{type
};
1663 if ($type eq 'dir' || $type eq 'nfs') {
1665 my $path = $scfg->{path
};
1667 my $fmts = join ('|', keys %{$default_config->{$type}->{format
}->[0]});
1669 foreach my $fn (<$path/images/[0-9][0-9]*/*>) {
1671 next if $fn !~ m!^(/.+/images/(\d+)/([^/]+\.($fmts)))$!;
1676 my $volid = "$sid:$owner/$name";
1679 my $found = grep { $_ eq $volid } @$vollist;
1682 next if defined ($vmid) && ($owner ne $vmid);
1685 my ($size, $format, $used) = file_size_info
($fn);
1687 if ($format && $size) {
1688 push @{$res->{$sid}}, {
1689 volid
=> $volid, format
=> $format,
1690 size
=> $size, vmid
=> $owner, used
=> $used };
1695 } elsif ($type eq 'lvm') {
1697 my $vgname = $scfg->{vgname
};
1699 if (my $dat = $lvs->{$vgname}) {
1701 foreach my $volname (keys %$dat) {
1703 my $owner = $dat->{$volname}->{vmid
};
1705 my $volid = "$sid:$volname";
1708 my $found = grep { $_ eq $volid } @$vollist;
1711 next if defined ($vmid) && ($owner ne $vmid);
1714 my $info = $dat->{$volname};
1715 $info->{volid
} = $volid;
1717 push @{$res->{$sid}}, $info;
1721 } elsif ($type eq 'iscsi') {
1723 # we have no owner for iscsi devices
1725 my $target = $scfg->{target
};
1727 if (my $dat = $iscsi_devices->{$target}) {
1729 foreach my $volname (keys %$dat) {
1731 my $volid = "$sid:$volname";
1734 my $found = grep { $_ eq $volid } @$vollist;
1737 next if !($storeid && ($storeid eq $sid));
1740 my $info = $dat->{$volname};
1741 $info->{volid
} = $volid;
1743 push @{$res->{$sid}}, $info;
1751 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
1757 sub nfs_is_mounted
{
1758 my ($server, $export, $mountpoint, $mountdata) = @_;
1760 my $source = "$server:$export";
1762 $mountdata = read_proc_mounts
() if !$mountdata;
1764 if ($mountdata =~ m
|^$source/?\s$mountpoint\snfs|m
) {
1772 my ($server, $export, $mountpoint, $options) = @_;
1774 my $source = "$server:$export";
1776 my $cmd = ['/bin/mount', '-t', 'nfs', $source, $mountpoint];
1778 push @$cmd, '-o', $options;
1781 run_command
($cmd, errmsg
=> "mount error");
1786 my $filename = "/sys/kernel/uevent_seqnum";
1789 if (my $fh = IO
::File-
>new ($filename, "r")) {
1791 if ($line =~ m/^(\d+)$/) {
1799 sub __activate_storage_full
{
1800 my ($cfg, $storeid, $session) = @_;
1802 my $scfg = storage_check_enabled
($cfg, $storeid);
1804 return if $session->{activated
}->{$storeid};
1806 if (!$session->{mountdata
}) {
1807 $session->{mountdata
} = read_proc_mounts
();
1810 if (!$session->{uevent_seqnum
}) {
1811 $session->{uevent_seqnum
} = uevent_seqnum
();
1814 my $mountdata = $session->{mountdata
};
1816 my $type = $scfg->{type
};
1818 if ($type eq 'dir' || $type eq 'nfs') {
1820 my $path = $scfg->{path
};
1822 if ($type eq 'nfs') {
1823 my $server = $scfg->{server
};
1824 my $export = $scfg->{export
};
1826 if (!nfs_is_mounted
($server, $export, $path, $mountdata)) {
1828 # NOTE: only call mkpath when not mounted (avoid hang
1829 # when NFS server is offline
1833 die "unable to activate storage '$storeid' - " .
1834 "directory '$path' does not exist\n" if ! -d
$path;
1836 nfs_mount
($server, $export, $path, $scfg->{options
});
1843 die "unable to activate storage '$storeid' - " .
1844 "directory '$path' does not exist\n" if ! -d
$path;
1847 my $imagedir = get_image_dir
($cfg, $storeid);
1848 my $isodir = get_iso_dir
($cfg, $storeid);
1849 my $tmpldir = get_vztmpl_dir
($cfg, $storeid);
1850 my $backupdir = get_backup_dir
($cfg, $storeid);
1851 my $privatedir = get_private_dir
($cfg, $storeid);
1853 if (defined($scfg->{content
})) {
1854 mkpath
$imagedir if $scfg->{content
}->{images
} &&
1856 mkpath
$isodir if $scfg->{content
}->{iso
} &&
1858 mkpath
$tmpldir if $scfg->{content
}->{vztmpl
} &&
1860 mkpath
$privatedir if $scfg->{content
}->{rootdir
} &&
1861 $privatedir ne $path;
1862 mkpath
$backupdir if $scfg->{content
}->{backup
} &&
1863 $backupdir ne $path;
1866 } elsif ($type eq 'lvm') {
1868 if ($scfg->{base
}) {
1869 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
1870 __activate_storage_full
($cfg, $baseid, $session);
1873 if (!$session->{vgs
}) {
1874 $session->{vgs
} = lvm_vgs
();
1877 # In LVM2, vgscans take place automatically;
1878 # this is just to be sure
1879 if ($session->{vgs
} && !$session->{vgscaned
} &&
1880 !$session->{vgs
}->{$scfg->{vgname
}}) {
1881 $session->{vgscaned
} = 1;
1882 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
1883 eval { run_command
($cmd, outfunc
=> sub {}); };
1887 # we do not acticate any volumes here ('vgchange -aly')
1888 # instead, volumes are activate individually later
1890 } elsif ($type eq 'iscsi') {
1892 return if !check_iscsi_support
(1);
1894 $session->{iscsi_sessions
} = iscsi_session_list
()
1895 if !$session->{iscsi_sessions
};
1897 my $iscsi_sess = $session->{iscsi_sessions
}->{$scfg->{target
}};
1898 if (!defined ($iscsi_sess)) {
1899 eval { iscsi_login
($scfg->{target
}, $scfg->{portal
}); };
1902 # make sure we get all devices
1903 iscsi_session_rescan
($iscsi_sess);
1910 my $newseq = uevent_seqnum
();
1912 # only call udevsettle if there are events
1913 if ($newseq > $session->{uevent_seqnum
}) {
1915 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1916 $session->{uevent_seqnum
} = $newseq;
1919 $session->{activated
}->{$storeid} = 1;
1922 sub activate_storage_list
{
1923 my ($cfg, $storeid_list, $session) = @_;
1925 $session = {} if !$session;
1927 foreach my $storeid (@$storeid_list) {
1928 __activate_storage_full
($cfg, $storeid, $session);
1932 sub activate_storage
{
1933 my ($cfg, $storeid) = @_;
1937 __activate_storage_full
($cfg, $storeid, $session);
1940 sub activate_volumes
{
1941 my ($cfg, $vollist, $exclusive) = @_;
1943 return if !($vollist && scalar(@$vollist));
1945 my $lvm_activate_mode = $exclusive ?
'ey' : 'ly';
1947 my $storagehash = {};
1948 foreach my $volid (@$vollist) {
1949 my ($storeid, undef) = parse_volume_id
($volid);
1950 $storagehash->{$storeid} = 1;
1953 activate_storage_list
($cfg, [keys %$storagehash]);
1955 foreach my $volid (@$vollist) {
1956 my ($storeid, $volname) = parse_volume_id
($volid);
1958 my $scfg = storage_config
($cfg, $storeid);
1960 my $path = path
($cfg, $volid);
1962 if ($scfg->{type
} eq 'lvm') {
1963 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
1964 run_command
($cmd, errmsg
=> "can't activate LV '$volid'");
1967 # check is volume exists
1968 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
1969 die "volume '$volid' does not exist\n" if ! -e
$path;
1971 die "volume '$volid' does not exist\n" if ! -b
$path;
1976 sub deactivate_volumes
{
1977 my ($cfg, $vollist) = @_;
1979 return if !($vollist && scalar(@$vollist));
1981 my $lvs = lvm_lvs
();
1984 foreach my $volid (@$vollist) {
1985 my ($storeid, $volname) = parse_volume_id
($volid);
1987 my $scfg = storage_config
($cfg, $storeid);
1989 if ($scfg->{type
} eq 'lvm') {
1990 my ($name) = parse_volname_lvm
($volname);
1992 if ($lvs->{$scfg->{vgname
}}->{$name}) {
1993 my $path = path
($cfg, $volid);
1994 my $cmd = ['/sbin/lvchange', '-aln', $path];
1995 eval { run_command
($cmd, errmsg
=> "can't deactivate LV '$volid'"); };
1998 push @errlist, $volid;
2004 die "volume deativation failed: " . join(' ', @errlist)
2005 if scalar(@errlist);
2008 sub deactivate_storage
{
2009 my ($cfg, $storeid) = @_;
2013 my $scfg = storage_config
($cfg, $storeid);
2015 my $type = $scfg->{type
};
2017 if ($type eq 'dir') {
2019 } elsif ($type eq 'nfs') {
2020 my $mountdata = read_proc_mounts
();
2021 my $server = $scfg->{server
};
2022 my $export = $scfg->{export
};
2023 my $path = $scfg->{path
};
2025 my $cmd = ['/bin/umount', $path];
2027 run_command
($cmd, errmsg
=> 'umount error')
2028 if nfs_is_mounted
($server, $export, $path, $mountdata);
2030 } elsif ($type eq 'lvm') {
2031 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
2032 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
2033 } elsif ($type eq 'iscsi') {
2034 my $portal = $scfg->{portal
};
2035 my $target = $scfg->{target
};
2037 my $iscsi_sessions = iscsi_session_list
();
2038 iscsi_logout
($target, $portal)
2039 if defined ($iscsi_sessions->{$target});
2047 my ($cfg, $content) = @_;
2049 my $ids = $cfg->{ids
};
2055 foreach my $storeid (keys %$ids) {
2057 next if $content && !$ids->{$storeid}->{content
}->{$content};
2059 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
2061 my $type = $ids->{$storeid}->{type
};
2063 $info->{$storeid} = {
2068 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
2069 content
=> content_hash_to_string
($ids->{$storeid}->{content
}),
2073 $stypes->{$type} = 1;
2075 push @$slist, $storeid;
2080 my $iscsi_sessions = {};
2083 if ($stypes->{lvm
}) {
2084 $session->{vgs
} = lvm_vgs
();
2085 $vgs = $session->{vgs
};
2087 if ($stypes->{nfs
}) {
2088 $mountdata = read_proc_mounts
();
2089 $session->{mountdata
} = $mountdata;
2091 if ($stypes->{iscsi
}) {
2092 $iscsi_sessions = iscsi_session_list
();
2093 $session->{iscsi_sessions
} = $iscsi_sessions;
2096 eval { activate_storage_list
($cfg, $slist, $session); };
2098 foreach my $storeid (keys %$ids) {
2099 my $scfg = $ids->{$storeid};
2101 next if !$info->{$storeid};
2103 my $type = $scfg->{type
};
2105 if ($type eq 'dir' || $type eq 'nfs') {
2107 my $path = $scfg->{path
};
2109 if ($type eq 'nfs') {
2110 my $server = $scfg->{server
};
2111 my $export = $scfg->{export
};
2113 next if !nfs_is_mounted
($server, $export, $path, $mountdata);
2117 my $res = PVE
::Tools
::df
($path, $timeout);
2119 next if !$res || !$res->{total
};
2121 $info->{$storeid}->{total
} = $res->{total
};
2122 $info->{$storeid}->{avail
} = $res->{avail
};
2123 $info->{$storeid}->{used
} = $res->{used
};
2124 $info->{$storeid}->{active
} = 1;
2126 } elsif ($type eq 'lvm') {
2128 my $vgname = $scfg->{vgname
};
2133 if (defined ($vgs->{$vgname})) {
2134 $total = $vgs->{$vgname}->{size
};
2135 $free = $vgs->{$vgname}->{free
};
2137 $info->{$storeid}->{total
} = $total;
2138 $info->{$storeid}->{avail
} = $free;
2139 $info->{$storeid}->{used
} = $total - $free;
2140 $info->{$storeid}->{active
} = 1;
2143 } elsif ($type eq 'iscsi') {
2145 $info->{$storeid}->{total
} = 0;
2146 $info->{$storeid}->{avail
} = 0;
2147 $info->{$storeid}->{used
} = 0;
2148 $info->{$storeid}->{active
} =
2149 defined ($iscsi_sessions->{$scfg->{target
}});
2162 my $packed_ip = gethostbyname($server);
2163 if (defined $packed_ip) {
2164 return inet_ntoa
($packed_ip);
2170 my ($server_in) = @_;
2173 if (!($server = resolv_server
($server_in))) {
2174 die "unable to resolve address for server '${server_in}'\n";
2177 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
2180 run_command
($cmd, outfunc
=> sub {
2183 # note: howto handle white spaces in export path??
2184 if ($line =~ m!^(/\S+)\s+(.+)$!) {
2193 my ($portal, $noerr) = @_;
2195 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
2199 if (my $ip = resolv_server
($server)) {
2201 return $port ?
"$server:$port" : $server;
2204 return undef if $noerr;
2206 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
2209 # idea is from usbutils package (/usr/bin/usb-devices) script
2210 sub __scan_usb_device
{
2211 my ($res, $devpath, $parent, $level) = @_;
2213 return if ! -d
$devpath;
2214 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
2215 my $port = $level ?
int($1 - 1) : 0;
2217 my $busnum = int(file_read_firstline
("$devpath/busnum"));
2218 my $devnum = int(file_read_firstline
("$devpath/devnum"));
2225 speed
=> file_read_firstline
("$devpath/speed"),
2226 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
2227 vendid
=> file_read_firstline
("$devpath/idVendor"),
2228 prodid
=> file_read_firstline
("$devpath/idProduct"),
2232 my $usbpath = $devpath;
2233 $usbpath =~ s
|^.*/\d
+\
-||;
2234 $d->{usbpath
} = $usbpath;
2237 my $product = file_read_firstline
("$devpath/product");
2238 $d->{product
} = $product if $product;
2240 my $manu = file_read_firstline
("$devpath/manufacturer");
2241 $d->{manufacturer
} = $manu if $manu;
2243 my $serial => file_read_firstline
("$devpath/serial");
2244 $d->{serial
} = $serial if $serial;
2248 foreach my $subdev (<$devpath/$busnum-*>) {
2249 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
2250 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
2259 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
2260 __scan_usb_device
($devlist, $device, 0, 0);
2267 my ($portal_in) = @_;
2270 if (!($portal = resolv_portal
($portal_in))) {
2271 die "unable to parse/resolve portal address '${portal_in}'\n";
2274 return iscsi_discovery
($portal);
2277 sub storage_default_format
{
2278 my ($cfg, $storeid) = @_;
2280 my $scfg = storage_config
($cfg, $storeid);
2282 my $def = $default_config->{$scfg->{type
}};
2284 my $def_format = 'raw';
2285 my $valid_formats = [ $def_format ];
2287 if (defined ($def->{format
})) {
2288 $def_format = $scfg->{format
} || $def->{format
}->[1];
2289 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
2292 return wantarray ?
($def_format, $valid_formats) : $def_format;
2295 sub vgroup_is_used
{
2296 my ($cfg, $vgname) = @_;
2298 foreach my $storeid (keys %{$cfg->{ids
}}) {
2299 my $scfg = storage_config
($cfg, $storeid);
2300 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
2308 sub target_is_used
{
2309 my ($cfg, $target) = @_;
2311 foreach my $storeid (keys %{$cfg->{ids
}}) {
2312 my $scfg = storage_config
($cfg, $storeid);
2313 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
2321 sub volume_is_used
{
2322 my ($cfg, $volid) = @_;
2324 foreach my $storeid (keys %{$cfg->{ids
}}) {
2325 my $scfg = storage_config
($cfg, $storeid);
2326 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
2334 sub storage_is_used
{
2335 my ($cfg, $storeid) = @_;
2337 foreach my $sid (keys %{$cfg->{ids
}}) {
2338 my $scfg = storage_config
($cfg, $sid);
2339 next if !$scfg->{base
};
2340 my ($st) = parse_volume_id
($scfg->{base
});
2341 return 1 if $st && $st eq $storeid;
2348 my ($list, $func) = @_;
2352 foreach my $sid (keys %$list) {
2353 foreach my $info (@{$list->{$sid}}) {
2354 my $volid = $info->{volid
};
2355 my ($sid1, $volname) = parse_volume_id
($volid, 1);
2356 if ($sid1 && $sid1 eq $sid) {
2357 &$func ($volid, $sid, $info);
2359 warn "detected strange volid '$volid' in volume list for '$sid'\n";