]>
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
;
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);
112 my ($cfg, $storeid, $vmid) = @_;
114 my $scfg = storage_config
($cfg, $storeid);
115 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
117 my $path = $plugin->get_subdir($scfg, 'images');
119 return $vmid ?
"$path/$vmid" : $path;
122 sub get_private_dir
{
123 my ($cfg, $storeid, $vmid) = @_;
125 my $scfg = storage_config
($cfg, $storeid);
126 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
128 my $path = $plugin->get_subdir($scfg, 'rootdir');
130 return $vmid ?
"$path/$vmid" : $path;
134 my ($cfg, $storeid) = @_;
136 my $scfg = storage_config
($cfg, $storeid);
137 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
139 return $plugin->get_subdir($scfg, 'iso');
143 my ($cfg, $storeid) = @_;
145 my $scfg = storage_config
($cfg, $storeid);
146 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
148 return $plugin->get_subdir($scfg, 'vztmpl');
152 my ($cfg, $storeid) = @_;
154 my $scfg = storage_config
($cfg, $storeid);
155 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
157 return $plugin->get_subdir($scfg, 'backup');
160 # library implementation
165 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
170 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
171 sub parse_volume_id
{
172 my ($volid, $noerr) = @_;
174 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
175 return wantarray ?
($1, $2) : $1;
177 return undef if $noerr;
178 die "unable to parse volume ID '$volid'\n";
181 # try to map a filesystem path to a volume identifier
182 sub path_to_volume_id
{
183 my ($cfg, $path) = @_;
185 my $ids = $cfg->{ids
};
187 my ($sid, $volname) = parse_volume_id
($path, 1);
189 if (my $scfg = $ids->{$sid}) {
190 if (my $path = $scfg->{path
}) {
191 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
192 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
193 return ($vtype, $path);
199 # Note: abs_path() return undef if $path doesn not exist
200 # for example when nfs storage is not mounted
201 $path = abs_path
($path) || $path;
203 foreach my $sid (keys %$ids) {
204 my $scfg = $ids->{$sid};
205 next if !$scfg->{path
};
206 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
207 my $imagedir = $plugin->get_subdir($scfg, 'images');
208 my $isodir = $plugin->get_subdir($scfg, 'iso');
209 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
210 my $backupdir = $plugin->get_subdir($scfg, 'backup');
211 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
213 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
216 return ('images', "$sid:$vmid/$name");
217 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
219 return ('iso', "$sid:iso/$name");
220 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
222 return ('vztmpl', "$sid:vztmpl/$name");
223 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
225 return ('rootdir', "$sid:rootdir/$vmid");
226 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
228 return ('iso', "$sid:backup/$name");
232 # can't map path to volume id
237 my ($cfg, $volid) = @_;
239 my ($storeid, $volname) = parse_volume_id
($volid);
241 my $scfg = storage_config
($cfg, $storeid);
243 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
244 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid);
245 return wantarray ?
($path, $owner, $vtype) : $path;
248 sub storage_migrate
{
249 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
251 my ($storeid, $volname) = parse_volume_id
($volid);
252 $target_volname = $volname if !$target_volname;
254 my $scfg = storage_config
($cfg, $storeid);
256 # no need to migrate shared content
257 return if $storeid eq $target_storeid && $scfg->{shared
};
259 my $tcfg = storage_config
($cfg, $target_storeid);
261 my $target_volid = "${target_storeid}:${target_volname}";
263 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
265 # blowfish is a fast block cipher, much faster then 3des
266 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
267 my $ssh = "/usr/bin/ssh $sshoptions";
269 local $ENV{RSYNC_RSH
} = $ssh;
271 # only implemented for file system based storage
275 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
276 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
277 my $src = $src_plugin->path($scfg, $volname, $storeid);
278 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
280 my $dirname = dirname
($dst);
282 if ($tcfg->{shared
}) { # we can do a local copy
284 run_command
(['/bin/mkdir', '-p', $dirname]);
286 run_command
(['/bin/cp', $src, $dst]);
290 run_command
(['/usr/bin/ssh', "root\@${target_host}",
291 '/bin/mkdir', '-p', $dirname]);
293 # we use rsync with --sparse, so we can't use --inplace,
294 # so we remove file on the target if it already exists to
296 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
297 if ($format && ($format eq 'raw') && $size) {
298 run_command
(['/usr/bin/ssh', "root\@${target_host}",
303 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
304 $src, "root\@${target_host}:$dst"];
308 run_command
($cmd, outfunc
=> sub {
311 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
314 print "rsync status: $1\n";
324 die "$errstr - target type '$tcfg->{type}' not implemented\n";
327 die "$errstr - source type '$scfg->{type}' not implemented\n";
332 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
334 die "no storage id specified\n" if !$storeid;
336 PVE
::JSONSchema
::parse_storage_id
($storeid);
338 my $scfg = storage_config
($cfg, $storeid);
340 die "no VMID specified\n" if !$vmid;
342 $vmid = parse_vmid
($vmid);
344 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
346 $fmt = $defformat if !$fmt;
348 activate_storage
($cfg, $storeid);
350 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
352 # lock shared storage
353 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
354 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
355 return "$storeid:$volname";
360 my ($cfg, $volid) = @_;
362 my ($storeid, $volname) = parse_volume_id
($volid);
364 my $scfg = storage_config
($cfg, $storeid);
366 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
368 activate_storage
($cfg, $storeid);
372 # lock shared storage
373 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
374 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
377 return if !$cleanup_worker;
379 my $rpcenv = PVE
::RPCEnvironment
::get
();
380 my $authuser = $rpcenv->get_user();
382 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
385 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
387 my ($cfg, $storeid, $tt) = @_;
389 die "unknown template type '$tt'\n"
390 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
392 my $ids = $cfg->{ids
};
394 storage_check_enabled
($cfg, $storeid) if ($storeid);
400 foreach my $sid (keys %$ids) {
401 next if $storeid && $storeid ne $sid;
403 my $scfg = $ids->{$sid};
404 my $type = $scfg->{type
};
406 next if !storage_check_enabled
($cfg, $sid, undef, 1);
408 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
409 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
410 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
412 activate_storage
($cfg, $sid);
415 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
417 my $path = $plugin->get_subdir($scfg, $tt);
419 foreach my $fn (<$path/*>) {
424 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
426 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
428 } elsif ($tt eq 'vztmpl') {
429 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
431 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
433 } elsif ($tt eq 'backup') {
434 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
436 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
439 $info->{size
} = -s
$fn;
441 push @{$res->{$sid}}, $info;
446 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
453 my ($cfg, $storeid, $vmid, $vollist) = @_;
455 my $ids = $cfg->{ids
};
457 storage_check_enabled
($cfg, $storeid) if ($storeid);
461 # prepare/activate/refresh all storages
463 my $storage_list = [];
465 foreach my $volid (@$vollist) {
466 my ($sid, undef) = parse_volume_id
($volid);
467 next if !defined($ids->{$sid});
468 next if !storage_check_enabled
($cfg, $sid, undef, 1);
469 push @$storage_list, $sid;
472 foreach my $sid (keys %$ids) {
473 next if $storeid && $storeid ne $sid;
474 next if !storage_check_enabled
($cfg, $sid, undef, 1);
475 push @$storage_list, $sid;
481 activate_storage_list
($cfg, $storage_list, $cache);
483 foreach my $sid (keys %$ids) {
484 next if $storeid && $storeid ne $sid;
485 next if !storage_check_enabled
($cfg, $sid, undef, 1);
487 my $scfg = $ids->{$sid};
488 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
489 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
490 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
498 my $filename = "/sys/kernel/uevent_seqnum";
501 if (my $fh = IO
::File-
>new($filename, "r")) {
503 if ($line =~ m/^(\d+)$/) {
511 sub __activate_storage_full
{
512 my ($cfg, $storeid, $cache) = @_;
514 my $scfg = storage_check_enabled
($cfg, $storeid);
516 return if $cache->{activated
}->{$storeid};
518 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
520 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
523 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
524 __activate_storage_full
($cfg, $baseid, $cache);
527 $plugin->activate_storage($storeid, $scfg, $cache);
529 my $newseq = uevent_seqnum
();
531 # only call udevsettle if there are events
532 if ($newseq > $cache->{uevent_seqnum
}) {
534 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
535 $cache->{uevent_seqnum
} = $newseq;
538 $cache->{activated
}->{$storeid} = 1;
541 sub activate_storage_list
{
542 my ($cfg, $storeid_list, $cache) = @_;
544 $cache = {} if !$cache;
546 foreach my $storeid (@$storeid_list) {
547 __activate_storage_full
($cfg, $storeid, $cache);
551 sub activate_storage
{
552 my ($cfg, $storeid) = @_;
556 __activate_storage_full
($cfg, $storeid, $cache);
560 sub deactivate_storage
{
561 my ($cfg, $storeid) = @_;
563 my $scfg = storage_config
($cfg, $storeid);
564 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
567 $plugin->deactivate_storage($storeid, $scfg, $cache);
570 sub activate_volumes
{
571 my ($cfg, $vollist, $exclusive) = @_;
573 return if !($vollist && scalar(@$vollist));
575 my $storagehash = {};
576 foreach my $volid (@$vollist) {
577 my ($storeid, undef) = parse_volume_id
($volid);
578 $storagehash->{$storeid} = 1;
583 activate_storage_list
($cfg, [keys %$storagehash], $cache);
585 foreach my $volid (@$vollist) {
586 my ($storeid, $volname) = parse_volume_id
($volid);
587 my $scfg = storage_config
($cfg, $storeid);
588 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
589 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
593 sub deactivate_volumes
{
594 my ($cfg, $vollist) = @_;
596 return if !($vollist && scalar(@$vollist));
601 foreach my $volid (@$vollist) {
602 my ($storeid, $volname) = parse_volume_id
($volid);
604 my $scfg = storage_config
($cfg, $storeid);
605 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
608 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
612 push @errlist, $volid;
616 die "volume deativation failed: " . join(' ', @errlist)
621 my ($cfg, $content) = @_;
623 my $ids = $cfg->{ids
};
628 foreach my $storeid (keys %$ids) {
630 next if $content && !$ids->{$storeid}->{content
}->{$content};
632 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
634 my $type = $ids->{$storeid}->{type
};
636 $info->{$storeid} = {
641 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
642 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
646 push @$slist, $storeid;
651 eval { activate_storage_list
($cfg, $slist, $cache); };
653 foreach my $storeid (keys %$ids) {
654 my $scfg = $ids->{$storeid};
655 next if !$info->{$storeid};
657 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
658 my ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache);
660 $info->{$storeid}->{total
} = $total;
661 $info->{$storeid}->{avail
} = $avail;
662 $info->{$storeid}->{used
} = $used;
663 $info->{$storeid}->{active
} = $active;
672 my $packed_ip = gethostbyname($server);
673 if (defined $packed_ip) {
674 return inet_ntoa
($packed_ip);
680 my ($server_in) = @_;
683 if (!($server = resolv_server
($server_in))) {
684 die "unable to resolve address for server '${server_in}'\n";
687 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
690 run_command
($cmd, outfunc
=> sub {
693 # note: howto handle white spaces in export path??
694 if ($line =~ m!^(/\S+)\s+(.+)$!) {
703 my ($portal, $noerr) = @_;
705 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
709 if (my $ip = resolv_server
($server)) {
711 return $port ?
"$server:$port" : $server;
714 return undef if $noerr;
716 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
719 # idea is from usbutils package (/usr/bin/usb-devices) script
720 sub __scan_usb_device
{
721 my ($res, $devpath, $parent, $level) = @_;
723 return if ! -d
$devpath;
724 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
725 my $port = $level ?
int($1 - 1) : 0;
727 my $busnum = int(file_read_firstline
("$devpath/busnum"));
728 my $devnum = int(file_read_firstline
("$devpath/devnum"));
735 speed
=> file_read_firstline
("$devpath/speed"),
736 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
737 vendid
=> file_read_firstline
("$devpath/idVendor"),
738 prodid
=> file_read_firstline
("$devpath/idProduct"),
742 my $usbpath = $devpath;
743 $usbpath =~ s
|^.*/\d
+\
-||;
744 $d->{usbpath
} = $usbpath;
747 my $product = file_read_firstline
("$devpath/product");
748 $d->{product
} = $product if $product;
750 my $manu = file_read_firstline
("$devpath/manufacturer");
751 $d->{manufacturer
} = $manu if $manu;
753 my $serial => file_read_firstline
("$devpath/serial");
754 $d->{serial
} = $serial if $serial;
758 foreach my $subdev (<$devpath/$busnum-*>) {
759 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
760 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
769 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
770 __scan_usb_device
($devlist, $device, 0, 0);
777 my ($portal_in) = @_;
780 if (!($portal = resolv_portal
($portal_in))) {
781 die "unable to parse/resolve portal address '${portal_in}'\n";
784 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
787 sub storage_default_format
{
788 my ($cfg, $storeid) = @_;
790 my $scfg = storage_config
($cfg, $storeid);
792 return PVE
::Storage
::Plugin
::default_format
($scfg);
796 my ($cfg, $vgname) = @_;
798 foreach my $storeid (keys %{$cfg->{ids
}}) {
799 my $scfg = storage_config
($cfg, $storeid);
800 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
809 my ($cfg, $target) = @_;
811 foreach my $storeid (keys %{$cfg->{ids
}}) {
812 my $scfg = storage_config
($cfg, $storeid);
813 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
822 my ($cfg, $volid) = @_;
824 foreach my $storeid (keys %{$cfg->{ids
}}) {
825 my $scfg = storage_config
($cfg, $storeid);
826 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
834 sub storage_is_used
{
835 my ($cfg, $storeid) = @_;
837 foreach my $sid (keys %{$cfg->{ids
}}) {
838 my $scfg = storage_config
($cfg, $sid);
839 next if !$scfg->{base
};
840 my ($st) = parse_volume_id
($scfg->{base
});
841 return 1 if $st && $st eq $storeid;
848 my ($list, $func) = @_;
852 foreach my $sid (keys %$list) {
853 foreach my $info (@{$list->{$sid}}) {
854 my $volid = $info->{volid
};
855 my ($sid1, $volname) = parse_volume_id
($volid, 1);
856 if ($sid1 && $sid1 eq $sid) {
857 &$func ($volid, $sid, $info);
859 warn "detected strange volid '$volid' in volume list for '$sid'\n";