]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
4afdf15e6106a43dd8164fe4290a27f51a9e79c5
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
;
25 use PVE
::Storage
::SheepdogPlugin
;
26 use PVE
::Storage
::ISCSIDirectPlugin
;
27 use PVE
::Storage
::NexentaPlugin
;
29 # load and initialize all plugins
30 PVE
::Storage
::DirPlugin-
>register();
31 PVE
::Storage
::LVMPlugin-
>register();
32 PVE
::Storage
::NFSPlugin-
>register();
33 PVE
::Storage
::ISCSIPlugin-
>register();
34 PVE
::Storage
::RBDPlugin-
>register();
35 PVE
::Storage
::SheepdogPlugin-
>register();
36 PVE
::Storage
::ISCSIDirectPlugin-
>register();
37 PVE
::Storage
::NexentaPlugin-
>register();
38 PVE
::Storage
::Plugin-
>init();
40 my $UDEVADM = '/sbin/udevadm';
42 # PVE::Storage utility functions
45 return cfs_read_file
("storage.cfg");
48 sub lock_storage_config
{
49 my ($code, $errmsg) = @_;
51 cfs_lock_file
("storage.cfg", undef, $code);
54 $errmsg ?
die "$errmsg: $err" : die $err;
59 my ($cfg, $storeid, $noerr) = @_;
61 die "no storage id specified\n" if !$storeid;
63 my $scfg = $cfg->{ids
}->{$storeid};
65 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
70 sub storage_check_node
{
71 my ($cfg, $storeid, $node, $noerr) = @_;
73 my $scfg = storage_config
($cfg, $storeid);
76 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
77 if (!$scfg->{nodes
}->{$node}) {
78 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
86 sub storage_check_enabled
{
87 my ($cfg, $storeid, $node, $noerr) = @_;
89 my $scfg = storage_config
($cfg, $storeid);
91 if ($scfg->{disable
}) {
92 die "storage '$storeid' is disabled\n" if !$noerr;
96 return storage_check_node
($cfg, $storeid, $node, $noerr);
102 return keys %{$cfg->{ids
}};
106 my ($filename, $timeout) = @_;
108 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
111 sub volume_size_info
{
112 my ($cfg, $volid, $timeout) = @_;
114 my ($storeid, $volname) = parse_volume_id
($volid, 1);
116 my $scfg = storage_config
($cfg, $storeid);
117 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
118 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
119 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
120 return file_size_info
($volid, $timeout);
127 my ($cfg, $volid, $size, $running) = @_;
129 my ($storeid, $volname) = parse_volume_id
($volid, 1);
131 my $scfg = storage_config
($cfg, $storeid);
132 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
133 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
134 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
135 die "resize device is not possible";
142 my ($cfg, $storeid, $vmid) = @_;
144 my $scfg = storage_config
($cfg, $storeid);
145 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
147 my $path = $plugin->get_subdir($scfg, 'images');
149 return $vmid ?
"$path/$vmid" : $path;
152 sub get_private_dir
{
153 my ($cfg, $storeid, $vmid) = @_;
155 my $scfg = storage_config
($cfg, $storeid);
156 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
158 my $path = $plugin->get_subdir($scfg, 'rootdir');
160 return $vmid ?
"$path/$vmid" : $path;
164 my ($cfg, $storeid) = @_;
166 my $scfg = storage_config
($cfg, $storeid);
167 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
169 return $plugin->get_subdir($scfg, 'iso');
173 my ($cfg, $storeid) = @_;
175 my $scfg = storage_config
($cfg, $storeid);
176 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
178 return $plugin->get_subdir($scfg, 'vztmpl');
182 my ($cfg, $storeid) = @_;
184 my $scfg = storage_config
($cfg, $storeid);
185 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
187 return $plugin->get_subdir($scfg, 'backup');
190 # library implementation
195 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
200 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
201 sub parse_volume_id
{
202 my ($volid, $noerr) = @_;
204 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
205 return wantarray ?
($1, $2) : $1;
207 return undef if $noerr;
208 die "unable to parse volume ID '$volid'\n";
211 # try to map a filesystem path to a volume identifier
212 sub path_to_volume_id
{
213 my ($cfg, $path) = @_;
215 my $ids = $cfg->{ids
};
217 my ($sid, $volname) = parse_volume_id
($path, 1);
219 if (my $scfg = $ids->{$sid}) {
220 if (my $path = $scfg->{path
}) {
221 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
222 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
223 return ($vtype, $path);
229 # Note: abs_path() return undef if $path doesn not exist
230 # for example when nfs storage is not mounted
231 $path = abs_path
($path) || $path;
233 foreach my $sid (keys %$ids) {
234 my $scfg = $ids->{$sid};
235 next if !$scfg->{path
};
236 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
237 my $imagedir = $plugin->get_subdir($scfg, 'images');
238 my $isodir = $plugin->get_subdir($scfg, 'iso');
239 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
240 my $backupdir = $plugin->get_subdir($scfg, 'backup');
241 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
243 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
246 return ('images', "$sid:$vmid/$name");
247 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
249 return ('iso', "$sid:iso/$name");
250 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
252 return ('vztmpl', "$sid:vztmpl/$name");
253 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
255 return ('rootdir', "$sid:rootdir/$vmid");
256 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
258 return ('iso', "$sid:backup/$name");
262 # can't map path to volume id
267 my ($cfg, $volid) = @_;
269 my ($storeid, $volname) = parse_volume_id
($volid);
271 my $scfg = storage_config
($cfg, $storeid);
273 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
274 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid);
275 return wantarray ?
($path, $owner, $vtype) : $path;
278 sub storage_migrate
{
279 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
281 my ($storeid, $volname) = parse_volume_id
($volid);
282 $target_volname = $volname if !$target_volname;
284 my $scfg = storage_config
($cfg, $storeid);
286 # no need to migrate shared content
287 return if $storeid eq $target_storeid && $scfg->{shared
};
289 my $tcfg = storage_config
($cfg, $target_storeid);
291 my $target_volid = "${target_storeid}:${target_volname}";
293 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
295 # blowfish is a fast block cipher, much faster then 3des
296 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
297 my $ssh = "/usr/bin/ssh $sshoptions";
299 local $ENV{RSYNC_RSH
} = $ssh;
301 # only implemented for file system based storage
305 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
306 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
307 my $src = $src_plugin->path($scfg, $volname, $storeid);
308 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
310 my $dirname = dirname
($dst);
312 if ($tcfg->{shared
}) { # we can do a local copy
314 run_command
(['/bin/mkdir', '-p', $dirname]);
316 run_command
(['/bin/cp', $src, $dst]);
320 run_command
(['/usr/bin/ssh', "root\@${target_host}",
321 '/bin/mkdir', '-p', $dirname]);
323 # we use rsync with --sparse, so we can't use --inplace,
324 # so we remove file on the target if it already exists to
326 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
327 if ($format && ($format eq 'raw') && $size) {
328 run_command
(['/usr/bin/ssh', "root\@${target_host}",
333 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
334 $src, "root\@${target_host}:$dst"];
338 run_command
($cmd, outfunc
=> sub {
341 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
344 print "rsync status: $1\n";
354 die "$errstr - target type '$tcfg->{type}' not implemented\n";
357 die "$errstr - source type '$scfg->{type}' not implemented\n";
362 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
364 die "no storage id specified\n" if !$storeid;
366 PVE
::JSONSchema
::parse_storage_id
($storeid);
368 my $scfg = storage_config
($cfg, $storeid);
370 die "no VMID specified\n" if !$vmid;
372 $vmid = parse_vmid
($vmid);
374 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
376 $fmt = $defformat if !$fmt;
378 activate_storage
($cfg, $storeid);
380 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
382 # lock shared storage
383 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
384 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
385 return "$storeid:$volname";
390 my ($cfg, $volid) = @_;
392 my ($storeid, $volname) = parse_volume_id
($volid);
394 my $scfg = storage_config
($cfg, $storeid);
396 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
398 activate_storage
($cfg, $storeid);
402 # lock shared storage
403 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
404 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
407 return if !$cleanup_worker;
409 my $rpcenv = PVE
::RPCEnvironment
::get
();
410 my $authuser = $rpcenv->get_user();
412 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
415 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
417 my ($cfg, $storeid, $tt) = @_;
419 die "unknown template type '$tt'\n"
420 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
422 my $ids = $cfg->{ids
};
424 storage_check_enabled
($cfg, $storeid) if ($storeid);
430 foreach my $sid (keys %$ids) {
431 next if $storeid && $storeid ne $sid;
433 my $scfg = $ids->{$sid};
434 my $type = $scfg->{type
};
436 next if !storage_check_enabled
($cfg, $sid, undef, 1);
438 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
439 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
440 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
442 activate_storage
($cfg, $sid);
445 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
447 my $path = $plugin->get_subdir($scfg, $tt);
449 foreach my $fn (<$path/*>) {
454 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
456 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
458 } elsif ($tt eq 'vztmpl') {
459 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
461 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
463 } elsif ($tt eq 'backup') {
464 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
466 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
469 $info->{size
} = -s
$fn;
471 push @{$res->{$sid}}, $info;
476 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
484 my ($cfg, $storeid, $vmid, $vollist) = @_;
486 my $ids = $cfg->{ids
};
488 storage_check_enabled
($cfg, $storeid) if ($storeid);
492 # prepare/activate/refresh all storages
494 my $storage_list = [];
496 foreach my $volid (@$vollist) {
497 my ($sid, undef) = parse_volume_id
($volid);
498 next if !defined($ids->{$sid});
499 next if !storage_check_enabled
($cfg, $sid, undef, 1);
500 push @$storage_list, $sid;
503 foreach my $sid (keys %$ids) {
504 next if $storeid && $storeid ne $sid;
505 next if !storage_check_enabled
($cfg, $sid, undef, 1);
506 push @$storage_list, $sid;
512 activate_storage_list
($cfg, $storage_list, $cache);
514 foreach my $sid (keys %$ids) {
515 next if $storeid && $storeid ne $sid;
516 next if !storage_check_enabled
($cfg, $sid, undef, 1);
518 my $scfg = $ids->{$sid};
519 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
520 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
521 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
529 my $filename = "/sys/kernel/uevent_seqnum";
532 if (my $fh = IO
::File-
>new($filename, "r")) {
534 if ($line =~ m/^(\d+)$/) {
542 sub activate_storage
{
543 my ($cfg, $storeid, $cache) = @_;
545 $cache = {} if !$cache;
547 my $scfg = storage_check_enabled
($cfg, $storeid);
549 return if $cache->{activated
}->{$storeid};
551 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
553 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
556 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
557 activate_storage
($cfg, $baseid, $cache);
560 if (!$plugin->check_connection($storeid, $scfg)) {
561 die "storage '$storeid' is not online\n";
564 $plugin->activate_storage($storeid, $scfg, $cache);
566 my $newseq = uevent_seqnum
();
568 # only call udevsettle if there are events
569 if ($newseq > $cache->{uevent_seqnum
}) {
571 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
572 $cache->{uevent_seqnum
} = $newseq;
575 $cache->{activated
}->{$storeid} = 1;
578 sub activate_storage_list
{
579 my ($cfg, $storeid_list, $cache) = @_;
581 $cache = {} if !$cache;
583 foreach my $storeid (@$storeid_list) {
584 activate_storage
($cfg, $storeid, $cache);
588 sub deactivate_storage
{
589 my ($cfg, $storeid) = @_;
591 my $scfg = storage_config
($cfg, $storeid);
592 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
595 $plugin->deactivate_storage($storeid, $scfg, $cache);
598 sub activate_volumes
{
599 my ($cfg, $vollist, $exclusive) = @_;
601 return if !($vollist && scalar(@$vollist));
603 my $storagehash = {};
604 foreach my $volid (@$vollist) {
605 my ($storeid, undef) = parse_volume_id
($volid);
606 $storagehash->{$storeid} = 1;
611 activate_storage_list
($cfg, [keys %$storagehash], $cache);
613 foreach my $volid (@$vollist) {
614 my ($storeid, $volname) = parse_volume_id
($volid);
615 my $scfg = storage_config
($cfg, $storeid);
616 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
617 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
621 sub deactivate_volumes
{
622 my ($cfg, $vollist) = @_;
624 return if !($vollist && scalar(@$vollist));
629 foreach my $volid (@$vollist) {
630 my ($storeid, $volname) = parse_volume_id
($volid);
632 my $scfg = storage_config
($cfg, $storeid);
633 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
636 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
640 push @errlist, $volid;
644 die "volume deativation failed: " . join(' ', @errlist)
649 my ($cfg, $content) = @_;
651 my $ids = $cfg->{ids
};
656 foreach my $storeid (keys %$ids) {
658 next if $content && !$ids->{$storeid}->{content
}->{$content};
660 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
662 my $type = $ids->{$storeid}->{type
};
664 $info->{$storeid} = {
669 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
670 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
674 push @$slist, $storeid;
679 foreach my $storeid (keys %$ids) {
680 my $scfg = $ids->{$storeid};
681 next if !$info->{$storeid};
683 eval { activate_storage
($cfg, $storeid, $cache); };
689 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
690 my ($total, $avail, $used, $active);
691 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
694 $info->{$storeid}->{total
} = $total;
695 $info->{$storeid}->{avail
} = $avail;
696 $info->{$storeid}->{used
} = $used;
697 $info->{$storeid}->{active
} = $active;
706 my $packed_ip = gethostbyname($server);
707 if (defined $packed_ip) {
708 return inet_ntoa
($packed_ip);
714 my ($server_in) = @_;
717 if (!($server = resolv_server
($server_in))) {
718 die "unable to resolve address for server '${server_in}'\n";
721 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
724 run_command
($cmd, outfunc
=> sub {
727 # note: howto handle white spaces in export path??
728 if ($line =~ m!^(/\S+)\s+(.+)$!) {
737 my ($portal, $noerr) = @_;
739 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
743 if (my $ip = resolv_server
($server)) {
745 return $port ?
"$server:$port" : $server;
748 return undef if $noerr;
750 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
753 # idea is from usbutils package (/usr/bin/usb-devices) script
754 sub __scan_usb_device
{
755 my ($res, $devpath, $parent, $level) = @_;
757 return if ! -d
$devpath;
758 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
759 my $port = $level ?
int($1 - 1) : 0;
761 my $busnum = int(file_read_firstline
("$devpath/busnum"));
762 my $devnum = int(file_read_firstline
("$devpath/devnum"));
769 speed
=> file_read_firstline
("$devpath/speed"),
770 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
771 vendid
=> file_read_firstline
("$devpath/idVendor"),
772 prodid
=> file_read_firstline
("$devpath/idProduct"),
776 my $usbpath = $devpath;
777 $usbpath =~ s
|^.*/\d
+\
-||;
778 $d->{usbpath
} = $usbpath;
781 my $product = file_read_firstline
("$devpath/product");
782 $d->{product
} = $product if $product;
784 my $manu = file_read_firstline
("$devpath/manufacturer");
785 $d->{manufacturer
} = $manu if $manu;
787 my $serial => file_read_firstline
("$devpath/serial");
788 $d->{serial
} = $serial if $serial;
792 foreach my $subdev (<$devpath/$busnum-*>) {
793 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
794 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
803 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
804 __scan_usb_device
($devlist, $device, 0, 0);
811 my ($portal_in) = @_;
814 if (!($portal = resolv_portal
($portal_in))) {
815 die "unable to parse/resolve portal address '${portal_in}'\n";
818 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
821 sub storage_default_format
{
822 my ($cfg, $storeid) = @_;
824 my $scfg = storage_config
($cfg, $storeid);
826 return PVE
::Storage
::Plugin
::default_format
($scfg);
830 my ($cfg, $vgname) = @_;
832 foreach my $storeid (keys %{$cfg->{ids
}}) {
833 my $scfg = storage_config
($cfg, $storeid);
834 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
843 my ($cfg, $target) = @_;
845 foreach my $storeid (keys %{$cfg->{ids
}}) {
846 my $scfg = storage_config
($cfg, $storeid);
847 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
856 my ($cfg, $volid) = @_;
858 foreach my $storeid (keys %{$cfg->{ids
}}) {
859 my $scfg = storage_config
($cfg, $storeid);
860 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
868 sub storage_is_used
{
869 my ($cfg, $storeid) = @_;
871 foreach my $sid (keys %{$cfg->{ids
}}) {
872 my $scfg = storage_config
($cfg, $sid);
873 next if !$scfg->{base
};
874 my ($st) = parse_volume_id
($scfg->{base
});
875 return 1 if $st && $st eq $storeid;
882 my ($list, $func) = @_;
886 foreach my $sid (keys %$list) {
887 foreach my $info (@{$list->{$sid}}) {
888 my $volid = $info->{volid
};
889 my ($sid1, $volname) = parse_volume_id
($volid, 1);
890 if ($sid1 && $sid1 eq $sid) {
891 &$func ($volid, $sid, $info);
893 warn "detected strange volid '$volid' in volume list for '$sid'\n";