]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
12 use PVE
::Tools
qw(run_command file_read_firstline);
13 use PVE
::Cluster
qw(cfs_read_file cfs_lock_file);
14 use PVE
::Exception
qw(raise_param_exc);
17 use PVE
::RPCEnvironment
;
19 use PVE
::Storage
::Plugin
;
20 use PVE
::Storage
::DirPlugin
;
21 use PVE
::Storage
::LVMPlugin
;
22 use PVE
::Storage
::NFSPlugin
;
23 use PVE
::Storage
::ISCSIPlugin
;
24 use PVE
::Storage
::RBDPlugin
;
26 # load and initialize all plugins
27 PVE
::Storage
::DirPlugin-
>register();
28 PVE
::Storage
::LVMPlugin-
>register();
29 PVE
::Storage
::NFSPlugin-
>register();
30 PVE
::Storage
::ISCSIPlugin-
>register();
31 PVE
::Storage
::RBDPlugin-
>register();
33 PVE
::Storage
::Plugin-
>init();
35 my $UDEVADM = '/sbin/udevadm';
37 # PVE::Storage utility functions
40 return cfs_read_file
("storage.cfg");
43 sub lock_storage_config
{
44 my ($code, $errmsg) = @_;
46 cfs_lock_file
("storage.cfg", undef, $code);
49 $errmsg ?
die "$errmsg: $err" : die $err;
54 my ($cfg, $storeid, $noerr) = @_;
56 die "no storage id specified\n" if !$storeid;
58 my $scfg = $cfg->{ids
}->{$storeid};
60 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
65 sub storage_check_node
{
66 my ($cfg, $storeid, $node, $noerr) = @_;
68 my $scfg = storage_config
($cfg, $storeid);
71 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
72 if (!$scfg->{nodes
}->{$node}) {
73 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
81 sub storage_check_enabled
{
82 my ($cfg, $storeid, $node, $noerr) = @_;
84 my $scfg = storage_config
($cfg, $storeid);
86 if ($scfg->{disable
}) {
87 die "storage '$storeid' is disabled\n" if !$noerr;
91 return storage_check_node
($cfg, $storeid, $node, $noerr);
97 return keys %{$cfg->{ids
}};
101 my ($filename, $timeout) = @_;
103 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
107 my ($cfg, $storeid, $vmid) = @_;
109 my $scfg = storage_config
($cfg, $storeid);
110 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
112 my $path = $plugin->get_subdir($scfg, 'images');
114 return $vmid ?
"$path/$vmid" : $path;
117 sub get_private_dir
{
118 my ($cfg, $storeid, $vmid) = @_;
120 my $scfg = storage_config
($cfg, $storeid);
121 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
123 my $path = $plugin->get_subdir($scfg, 'rootdir');
125 return $vmid ?
"$path/$vmid" : $path;
129 my ($cfg, $storeid) = @_;
131 my $scfg = storage_config
($cfg, $storeid);
132 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
134 return $plugin->get_subdir($scfg, 'iso');
138 my ($cfg, $storeid) = @_;
140 my $scfg = storage_config
($cfg, $storeid);
141 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
143 return $plugin->get_subdir($scfg, 'vztmpl');
147 my ($cfg, $storeid) = @_;
149 my $scfg = storage_config
($cfg, $storeid);
150 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
152 return $plugin->get_subdir($scfg, 'backup');
155 # library implementation
160 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
165 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
166 sub parse_volume_id
{
167 my ($volid, $noerr) = @_;
169 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
170 return wantarray ?
($1, $2) : $1;
172 return undef if $noerr;
173 die "unable to parse volume ID '$volid'\n";
176 # try to map a filesystem path to a volume identifier
177 sub path_to_volume_id
{
178 my ($cfg, $path) = @_;
180 my $ids = $cfg->{ids
};
182 my ($sid, $volname) = parse_volume_id
($path, 1);
184 if (my $scfg = $ids->{$sid}) {
185 if (my $path = $scfg->{path
}) {
186 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
187 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
188 return ($vtype, $path);
194 # Note: abs_path() return undef if $path doesn not exist
195 # for example when nfs storage is not mounted
196 $path = abs_path
($path) || $path;
198 foreach my $sid (keys %$ids) {
199 my $scfg = $ids->{$sid};
200 next if !$scfg->{path
};
201 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
202 my $imagedir = $plugin->get_subdir($scfg, 'images');
203 my $isodir = $plugin->get_subdir($scfg, 'iso');
204 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
205 my $backupdir = $plugin->get_subdir($scfg, 'backup');
206 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
208 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
211 return ('images', "$sid:$vmid/$name");
212 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
214 return ('iso', "$sid:iso/$name");
215 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
217 return ('vztmpl', "$sid:vztmpl/$name");
218 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
220 return ('rootdir', "$sid:rootdir/$vmid");
221 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
223 return ('iso', "$sid:backup/$name");
227 # can't map path to volume id
232 my ($cfg, $volid) = @_;
234 my ($storeid, $volname) = parse_volume_id
($volid);
236 my $scfg = storage_config
($cfg, $storeid);
238 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
239 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid);
240 return wantarray ?
($path, $owner, $vtype) : $path;
243 sub storage_migrate
{
244 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
246 my ($storeid, $volname) = parse_volume_id
($volid);
247 $target_volname = $volname if !$target_volname;
249 my $scfg = storage_config
($cfg, $storeid);
251 # no need to migrate shared content
252 return if $storeid eq $target_storeid && $scfg->{shared
};
254 my $tcfg = storage_config
($cfg, $target_storeid);
256 my $target_volid = "${target_storeid}:${target_volname}";
258 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
260 # blowfish is a fast block cipher, much faster then 3des
261 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
262 my $ssh = "/usr/bin/ssh $sshoptions";
264 local $ENV{RSYNC_RSH
} = $ssh;
266 # only implemented for file system based storage
270 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
271 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
272 my $src = $src_plugin->path($scfg, $volid);
273 my $dst = $dst_plugin->path($tcfg, $target_volid);
275 my $dirname = dirname
($dst);
277 if ($tcfg->{shared
}) { # we can do a local copy
279 run_command
(['/bin/mkdir', '-p', $dirname]);
281 run_command
(['/bin/cp', $src, $dst]);
285 run_command
(['/usr/bin/ssh', "root\@${target_host}",
286 '/bin/mkdir', '-p', $dirname]);
288 # we use rsync with --sparse, so we can't use --inplace,
289 # so we remove file on the target if it already exists to
291 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
292 if ($format && ($format eq 'raw') && $size) {
293 run_command
(['/usr/bin/ssh', "root\@${target_host}",
298 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
299 $src, "root\@${target_host}:$dst"];
303 run_command
($cmd, outfunc
=> sub {
306 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
309 print "rsync status: $1\n";
319 die "$errstr - target type '$tcfg->{type}' not implemented\n";
322 die "$errstr - source type '$scfg->{type}' not implemented\n";
327 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
329 die "no storage id specified\n" if !$storeid;
331 PVE
::JSONSchema
::parse_storage_id
($storeid);
333 my $scfg = storage_config
($cfg, $storeid);
335 die "no VMID specified\n" if !$vmid;
337 $vmid = parse_vmid
($vmid);
339 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
341 $fmt = $defformat if !$fmt;
343 activate_storage
($cfg, $storeid);
345 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
347 # lock shared storage
348 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
349 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
350 return "$storeid:$volname";
355 my ($cfg, $volid) = @_;
357 my ($storeid, $volname) = parse_volume_id
($volid);
359 my $scfg = storage_config
($cfg, $storeid);
361 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
363 activate_storage
($cfg, $storeid);
367 # lock shared storage
368 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
369 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
372 return if !$cleanup_worker;
374 my $rpcenv = PVE
::RPCEnvironment
::get
();
375 my $authuser = $rpcenv->get_user();
377 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
380 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
382 my ($cfg, $storeid, $tt) = @_;
384 die "unknown template type '$tt'\n"
385 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
387 my $ids = $cfg->{ids
};
389 storage_check_enabled
($cfg, $storeid) if ($storeid);
395 foreach my $sid (keys %$ids) {
396 next if $storeid && $storeid ne $sid;
398 my $scfg = $ids->{$sid};
399 my $type = $scfg->{type
};
401 next if !storage_check_enabled
($cfg, $sid, undef, 1);
403 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
404 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
405 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
407 activate_storage
($cfg, $sid);
410 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
412 my $path = $plugin->get_subdir($scfg, $tt);
414 foreach my $fn (<$path/*>) {
419 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
421 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
423 } elsif ($tt eq 'vztmpl') {
424 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
426 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
428 } elsif ($tt eq 'backup') {
429 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
431 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
434 $info->{size
} = -s
$fn;
436 push @{$res->{$sid}}, $info;
441 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
448 my ($cfg, $storeid, $vmid, $vollist) = @_;
450 my $ids = $cfg->{ids
};
452 storage_check_enabled
($cfg, $storeid) if ($storeid);
456 # prepare/activate/refresh all storages
458 my $storage_list = [];
460 foreach my $volid (@$vollist) {
461 my ($sid, undef) = parse_volume_id
($volid);
462 next if !defined($ids->{$sid});
463 next if !storage_check_enabled
($cfg, $sid, undef, 1);
464 push @$storage_list, $sid;
467 foreach my $sid (keys %$ids) {
468 next if $storeid && $storeid ne $sid;
469 next if !storage_check_enabled
($cfg, $sid, undef, 1);
470 push @$storage_list, $sid;
476 activate_storage_list
($cfg, $storage_list, $cache);
478 foreach my $sid (keys %$ids) {
479 next if $storeid && $storeid ne $sid;
480 next if !storage_check_enabled
($cfg, $sid, undef, 1);
482 my $scfg = $ids->{$sid};
483 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
484 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
485 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
493 my $filename = "/sys/kernel/uevent_seqnum";
496 if (my $fh = IO
::File-
>new($filename, "r")) {
498 if ($line =~ m/^(\d+)$/) {
506 sub __activate_storage_full
{
507 my ($cfg, $storeid, $cache) = @_;
509 my $scfg = storage_check_enabled
($cfg, $storeid);
511 return if $cache->{activated
}->{$storeid};
513 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
515 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
518 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
519 __activate_storage_full
($cfg, $baseid, $cache);
522 $plugin->activate_storage($storeid, $scfg, $cache);
524 my $newseq = uevent_seqnum
();
526 # only call udevsettle if there are events
527 if ($newseq > $cache->{uevent_seqnum
}) {
529 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
530 $cache->{uevent_seqnum
} = $newseq;
533 $cache->{activated
}->{$storeid} = 1;
536 sub activate_storage_list
{
537 my ($cfg, $storeid_list, $cache) = @_;
539 $cache = {} if !$cache;
541 foreach my $storeid (@$storeid_list) {
542 __activate_storage_full
($cfg, $storeid, $cache);
546 sub activate_storage
{
547 my ($cfg, $storeid) = @_;
551 __activate_storage_full
($cfg, $storeid, $cache);
555 sub deactivate_storage
{
556 my ($cfg, $storeid) = @_;
558 my $scfg = storage_config
($cfg, $storeid);
559 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
562 $plugin->deactivate_storage($storeid, $scfg, $cache);
565 sub activate_volumes
{
566 my ($cfg, $vollist, $exclusive) = @_;
568 return if !($vollist && scalar(@$vollist));
570 my $storagehash = {};
571 foreach my $volid (@$vollist) {
572 my ($storeid, undef) = parse_volume_id
($volid);
573 $storagehash->{$storeid} = 1;
578 activate_storage_list
($cfg, [keys %$storagehash], $cache);
580 foreach my $volid (@$vollist) {
581 my ($storeid, $volname) = parse_volume_id
($volid);
582 my $scfg = storage_config
($cfg, $storeid);
583 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
584 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
588 sub deactivate_volumes
{
589 my ($cfg, $vollist) = @_;
591 return if !($vollist && scalar(@$vollist));
596 foreach my $volid (@$vollist) {
597 my ($storeid, $volname) = parse_volume_id
($volid);
599 my $scfg = storage_config
($cfg, $storeid);
600 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
603 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
607 push @errlist, $volid;
611 die "volume deativation failed: " . join(' ', @errlist)
616 my ($cfg, $content) = @_;
618 my $ids = $cfg->{ids
};
623 foreach my $storeid (keys %$ids) {
625 next if $content && !$ids->{$storeid}->{content
}->{$content};
627 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
629 my $type = $ids->{$storeid}->{type
};
631 $info->{$storeid} = {
636 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
637 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
641 push @$slist, $storeid;
646 eval { activate_storage_list
($cfg, $slist, $cache); };
648 foreach my $storeid (keys %$ids) {
649 my $scfg = $ids->{$storeid};
650 next if !$info->{$storeid};
652 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
653 my ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache);
655 $info->{$storeid}->{total
} = $total;
656 $info->{$storeid}->{avail
} = $avail;
657 $info->{$storeid}->{used
} = $used;
658 $info->{$storeid}->{active
} = $active;
667 my $packed_ip = gethostbyname($server);
668 if (defined $packed_ip) {
669 return inet_ntoa
($packed_ip);
675 my ($server_in) = @_;
678 if (!($server = resolv_server
($server_in))) {
679 die "unable to resolve address for server '${server_in}'\n";
682 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
685 run_command
($cmd, outfunc
=> sub {
688 # note: howto handle white spaces in export path??
689 if ($line =~ m!^(/\S+)\s+(.+)$!) {
698 my ($portal, $noerr) = @_;
700 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
704 if (my $ip = resolv_server
($server)) {
706 return $port ?
"$server:$port" : $server;
709 return undef if $noerr;
711 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
714 # idea is from usbutils package (/usr/bin/usb-devices) script
715 sub __scan_usb_device
{
716 my ($res, $devpath, $parent, $level) = @_;
718 return if ! -d
$devpath;
719 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
720 my $port = $level ?
int($1 - 1) : 0;
722 my $busnum = int(file_read_firstline
("$devpath/busnum"));
723 my $devnum = int(file_read_firstline
("$devpath/devnum"));
730 speed
=> file_read_firstline
("$devpath/speed"),
731 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
732 vendid
=> file_read_firstline
("$devpath/idVendor"),
733 prodid
=> file_read_firstline
("$devpath/idProduct"),
737 my $usbpath = $devpath;
738 $usbpath =~ s
|^.*/\d
+\
-||;
739 $d->{usbpath
} = $usbpath;
742 my $product = file_read_firstline
("$devpath/product");
743 $d->{product
} = $product if $product;
745 my $manu = file_read_firstline
("$devpath/manufacturer");
746 $d->{manufacturer
} = $manu if $manu;
748 my $serial => file_read_firstline
("$devpath/serial");
749 $d->{serial
} = $serial if $serial;
753 foreach my $subdev (<$devpath/$busnum-*>) {
754 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
755 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
764 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
765 __scan_usb_device
($devlist, $device, 0, 0);
772 my ($portal_in) = @_;
775 if (!($portal = resolv_portal
($portal_in))) {
776 die "unable to parse/resolve portal address '${portal_in}'\n";
779 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
782 sub storage_default_format
{
783 my ($cfg, $storeid) = @_;
785 my $scfg = storage_config
($cfg, $storeid);
787 return PVE
::Storage
::Plugin
::default_format
($scfg);
791 my ($cfg, $vgname) = @_;
793 foreach my $storeid (keys %{$cfg->{ids
}}) {
794 my $scfg = storage_config
($cfg, $storeid);
795 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
804 my ($cfg, $target) = @_;
806 foreach my $storeid (keys %{$cfg->{ids
}}) {
807 my $scfg = storage_config
($cfg, $storeid);
808 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
817 my ($cfg, $volid) = @_;
819 foreach my $storeid (keys %{$cfg->{ids
}}) {
820 my $scfg = storage_config
($cfg, $storeid);
821 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
829 sub storage_is_used
{
830 my ($cfg, $storeid) = @_;
832 foreach my $sid (keys %{$cfg->{ids
}}) {
833 my $scfg = storage_config
($cfg, $sid);
834 next if !$scfg->{base
};
835 my ($st) = parse_volume_id
($scfg->{base
});
836 return 1 if $st && $st eq $storeid;
843 my ($list, $func) = @_;
847 foreach my $sid (keys %$list) {
848 foreach my $info (@{$list->{$sid}}) {
849 my $volid = $info->{volid
};
850 my ($sid1, $volname) = parse_volume_id
($volid, 1);
851 if ($sid1 && $sid1 eq $sid) {
852 &$func ($volid, $sid, $info);
854 warn "detected strange volid '$volid' in volume list for '$sid'\n";