]>
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 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
97 if (!$plugin->check_connection($storeid, $scfg)) {
98 die "storage '$storeid' is not available\n" if !$noerr;
102 return storage_check_node
($cfg, $storeid, $node, $noerr);
108 return keys %{$cfg->{ids
}};
112 my ($filename, $timeout) = @_;
114 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
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, 'images');
125 return $vmid ?
"$path/$vmid" : $path;
128 sub get_private_dir
{
129 my ($cfg, $storeid, $vmid) = @_;
131 my $scfg = storage_config
($cfg, $storeid);
132 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
134 my $path = $plugin->get_subdir($scfg, 'rootdir');
136 return $vmid ?
"$path/$vmid" : $path;
140 my ($cfg, $storeid) = @_;
142 my $scfg = storage_config
($cfg, $storeid);
143 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
145 return $plugin->get_subdir($scfg, 'iso');
149 my ($cfg, $storeid) = @_;
151 my $scfg = storage_config
($cfg, $storeid);
152 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
154 return $plugin->get_subdir($scfg, 'vztmpl');
158 my ($cfg, $storeid) = @_;
160 my $scfg = storage_config
($cfg, $storeid);
161 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
163 return $plugin->get_subdir($scfg, 'backup');
166 # library implementation
171 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
176 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
177 sub parse_volume_id
{
178 my ($volid, $noerr) = @_;
180 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
181 return wantarray ?
($1, $2) : $1;
183 return undef if $noerr;
184 die "unable to parse volume ID '$volid'\n";
187 # try to map a filesystem path to a volume identifier
188 sub path_to_volume_id
{
189 my ($cfg, $path) = @_;
191 my $ids = $cfg->{ids
};
193 my ($sid, $volname) = parse_volume_id
($path, 1);
195 if (my $scfg = $ids->{$sid}) {
196 if (my $path = $scfg->{path
}) {
197 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
198 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
199 return ($vtype, $path);
205 # Note: abs_path() return undef if $path doesn not exist
206 # for example when nfs storage is not mounted
207 $path = abs_path
($path) || $path;
209 foreach my $sid (keys %$ids) {
210 my $scfg = $ids->{$sid};
211 next if !$scfg->{path
};
212 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
213 my $imagedir = $plugin->get_subdir($scfg, 'images');
214 my $isodir = $plugin->get_subdir($scfg, 'iso');
215 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
216 my $backupdir = $plugin->get_subdir($scfg, 'backup');
217 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
219 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
222 return ('images', "$sid:$vmid/$name");
223 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
225 return ('iso', "$sid:iso/$name");
226 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
228 return ('vztmpl', "$sid:vztmpl/$name");
229 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
231 return ('rootdir', "$sid:rootdir/$vmid");
232 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
234 return ('iso', "$sid:backup/$name");
238 # can't map path to volume id
243 my ($cfg, $volid) = @_;
245 my ($storeid, $volname) = parse_volume_id
($volid);
247 my $scfg = storage_config
($cfg, $storeid);
249 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
250 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid);
251 return wantarray ?
($path, $owner, $vtype) : $path;
254 sub storage_migrate
{
255 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
257 my ($storeid, $volname) = parse_volume_id
($volid);
258 $target_volname = $volname if !$target_volname;
260 my $scfg = storage_config
($cfg, $storeid);
262 # no need to migrate shared content
263 return if $storeid eq $target_storeid && $scfg->{shared
};
265 my $tcfg = storage_config
($cfg, $target_storeid);
267 my $target_volid = "${target_storeid}:${target_volname}";
269 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
271 # blowfish is a fast block cipher, much faster then 3des
272 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
273 my $ssh = "/usr/bin/ssh $sshoptions";
275 local $ENV{RSYNC_RSH
} = $ssh;
277 # only implemented for file system based storage
281 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
282 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
283 my $src = $src_plugin->path($scfg, $volname, $storeid);
284 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
286 my $dirname = dirname
($dst);
288 if ($tcfg->{shared
}) { # we can do a local copy
290 run_command
(['/bin/mkdir', '-p', $dirname]);
292 run_command
(['/bin/cp', $src, $dst]);
296 run_command
(['/usr/bin/ssh', "root\@${target_host}",
297 '/bin/mkdir', '-p', $dirname]);
299 # we use rsync with --sparse, so we can't use --inplace,
300 # so we remove file on the target if it already exists to
302 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
303 if ($format && ($format eq 'raw') && $size) {
304 run_command
(['/usr/bin/ssh', "root\@${target_host}",
309 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
310 $src, "root\@${target_host}:$dst"];
314 run_command
($cmd, outfunc
=> sub {
317 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
320 print "rsync status: $1\n";
330 die "$errstr - target type '$tcfg->{type}' not implemented\n";
333 die "$errstr - source type '$scfg->{type}' not implemented\n";
338 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
340 die "no storage id specified\n" if !$storeid;
342 PVE
::JSONSchema
::parse_storage_id
($storeid);
344 my $scfg = storage_config
($cfg, $storeid);
346 die "no VMID specified\n" if !$vmid;
348 $vmid = parse_vmid
($vmid);
350 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
352 $fmt = $defformat if !$fmt;
354 activate_storage
($cfg, $storeid);
356 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
358 # lock shared storage
359 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
360 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
361 return "$storeid:$volname";
366 my ($cfg, $volid) = @_;
368 my ($storeid, $volname) = parse_volume_id
($volid);
370 my $scfg = storage_config
($cfg, $storeid);
372 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
374 activate_storage
($cfg, $storeid);
378 # lock shared storage
379 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
380 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
383 return if !$cleanup_worker;
385 my $rpcenv = PVE
::RPCEnvironment
::get
();
386 my $authuser = $rpcenv->get_user();
388 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
391 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
393 my ($cfg, $storeid, $tt) = @_;
395 die "unknown template type '$tt'\n"
396 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
398 my $ids = $cfg->{ids
};
400 storage_check_enabled
($cfg, $storeid) if ($storeid);
406 foreach my $sid (keys %$ids) {
407 next if $storeid && $storeid ne $sid;
409 my $scfg = $ids->{$sid};
410 my $type = $scfg->{type
};
412 next if !storage_check_enabled
($cfg, $sid, undef, 1);
414 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
415 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
416 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
418 activate_storage
($cfg, $sid);
421 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
423 my $path = $plugin->get_subdir($scfg, $tt);
425 foreach my $fn (<$path/*>) {
430 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
432 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
434 } elsif ($tt eq 'vztmpl') {
435 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
437 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
439 } elsif ($tt eq 'backup') {
440 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
442 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
445 $info->{size
} = -s
$fn;
447 push @{$res->{$sid}}, $info;
452 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
459 my ($cfg, $storeid, $vmid, $vollist) = @_;
461 my $ids = $cfg->{ids
};
463 storage_check_enabled
($cfg, $storeid) if ($storeid);
467 # prepare/activate/refresh all storages
469 my $storage_list = [];
471 foreach my $volid (@$vollist) {
472 my ($sid, undef) = parse_volume_id
($volid);
473 next if !defined($ids->{$sid});
474 next if !storage_check_enabled
($cfg, $sid, undef, 1);
475 push @$storage_list, $sid;
478 foreach my $sid (keys %$ids) {
479 next if $storeid && $storeid ne $sid;
480 next if !storage_check_enabled
($cfg, $sid, undef, 1);
481 push @$storage_list, $sid;
487 activate_storage_list
($cfg, $storage_list, $cache);
489 foreach my $sid (keys %$ids) {
490 next if $storeid && $storeid ne $sid;
491 next if !storage_check_enabled
($cfg, $sid, undef, 1);
493 my $scfg = $ids->{$sid};
494 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
495 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
496 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
504 my $filename = "/sys/kernel/uevent_seqnum";
507 if (my $fh = IO
::File-
>new($filename, "r")) {
509 if ($line =~ m/^(\d+)$/) {
517 sub __activate_storage_full
{
518 my ($cfg, $storeid, $cache) = @_;
520 my $scfg = storage_check_enabled
($cfg, $storeid);
522 return if $cache->{activated
}->{$storeid};
524 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
526 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
529 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
530 __activate_storage_full
($cfg, $baseid, $cache);
533 $plugin->activate_storage($storeid, $scfg, $cache);
535 my $newseq = uevent_seqnum
();
537 # only call udevsettle if there are events
538 if ($newseq > $cache->{uevent_seqnum
}) {
540 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
541 $cache->{uevent_seqnum
} = $newseq;
544 $cache->{activated
}->{$storeid} = 1;
547 sub activate_storage_list
{
548 my ($cfg, $storeid_list, $cache) = @_;
550 $cache = {} if !$cache;
552 foreach my $storeid (@$storeid_list) {
553 __activate_storage_full
($cfg, $storeid, $cache);
557 sub activate_storage
{
558 my ($cfg, $storeid) = @_;
562 __activate_storage_full
($cfg, $storeid, $cache);
566 sub deactivate_storage
{
567 my ($cfg, $storeid) = @_;
569 my $scfg = storage_config
($cfg, $storeid);
570 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
573 $plugin->deactivate_storage($storeid, $scfg, $cache);
576 sub activate_volumes
{
577 my ($cfg, $vollist, $exclusive) = @_;
579 return if !($vollist && scalar(@$vollist));
581 my $storagehash = {};
582 foreach my $volid (@$vollist) {
583 my ($storeid, undef) = parse_volume_id
($volid);
584 $storagehash->{$storeid} = 1;
589 activate_storage_list
($cfg, [keys %$storagehash], $cache);
591 foreach my $volid (@$vollist) {
592 my ($storeid, $volname) = parse_volume_id
($volid);
593 my $scfg = storage_config
($cfg, $storeid);
594 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
595 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
599 sub deactivate_volumes
{
600 my ($cfg, $vollist) = @_;
602 return if !($vollist && scalar(@$vollist));
607 foreach my $volid (@$vollist) {
608 my ($storeid, $volname) = parse_volume_id
($volid);
610 my $scfg = storage_config
($cfg, $storeid);
611 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
614 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
618 push @errlist, $volid;
622 die "volume deativation failed: " . join(' ', @errlist)
627 my ($cfg, $content) = @_;
629 my $ids = $cfg->{ids
};
634 foreach my $storeid (keys %$ids) {
636 next if $content && !$ids->{$storeid}->{content
}->{$content};
638 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
640 my $type = $ids->{$storeid}->{type
};
642 $info->{$storeid} = {
647 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
648 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
652 push @$slist, $storeid;
657 eval { activate_storage_list
($cfg, $slist, $cache); };
659 foreach my $storeid (keys %$ids) {
660 my $scfg = $ids->{$storeid};
661 next if !$info->{$storeid};
663 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
664 my ($total, $avail, $used, $active);
665 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
668 $info->{$storeid}->{total
} = $total;
669 $info->{$storeid}->{avail
} = $avail;
670 $info->{$storeid}->{used
} = $used;
671 $info->{$storeid}->{active
} = $active;
680 my $packed_ip = gethostbyname($server);
681 if (defined $packed_ip) {
682 return inet_ntoa
($packed_ip);
688 my ($server_in) = @_;
691 if (!($server = resolv_server
($server_in))) {
692 die "unable to resolve address for server '${server_in}'\n";
695 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
698 run_command
($cmd, outfunc
=> sub {
701 # note: howto handle white spaces in export path??
702 if ($line =~ m!^(/\S+)\s+(.+)$!) {
711 my ($portal, $noerr) = @_;
713 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
717 if (my $ip = resolv_server
($server)) {
719 return $port ?
"$server:$port" : $server;
722 return undef if $noerr;
724 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
727 # idea is from usbutils package (/usr/bin/usb-devices) script
728 sub __scan_usb_device
{
729 my ($res, $devpath, $parent, $level) = @_;
731 return if ! -d
$devpath;
732 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
733 my $port = $level ?
int($1 - 1) : 0;
735 my $busnum = int(file_read_firstline
("$devpath/busnum"));
736 my $devnum = int(file_read_firstline
("$devpath/devnum"));
743 speed
=> file_read_firstline
("$devpath/speed"),
744 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
745 vendid
=> file_read_firstline
("$devpath/idVendor"),
746 prodid
=> file_read_firstline
("$devpath/idProduct"),
750 my $usbpath = $devpath;
751 $usbpath =~ s
|^.*/\d
+\
-||;
752 $d->{usbpath
} = $usbpath;
755 my $product = file_read_firstline
("$devpath/product");
756 $d->{product
} = $product if $product;
758 my $manu = file_read_firstline
("$devpath/manufacturer");
759 $d->{manufacturer
} = $manu if $manu;
761 my $serial => file_read_firstline
("$devpath/serial");
762 $d->{serial
} = $serial if $serial;
766 foreach my $subdev (<$devpath/$busnum-*>) {
767 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
768 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
777 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
778 __scan_usb_device
($devlist, $device, 0, 0);
785 my ($portal_in) = @_;
788 if (!($portal = resolv_portal
($portal_in))) {
789 die "unable to parse/resolve portal address '${portal_in}'\n";
792 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
795 sub storage_default_format
{
796 my ($cfg, $storeid) = @_;
798 my $scfg = storage_config
($cfg, $storeid);
800 return PVE
::Storage
::Plugin
::default_format
($scfg);
804 my ($cfg, $vgname) = @_;
806 foreach my $storeid (keys %{$cfg->{ids
}}) {
807 my $scfg = storage_config
($cfg, $storeid);
808 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
817 my ($cfg, $target) = @_;
819 foreach my $storeid (keys %{$cfg->{ids
}}) {
820 my $scfg = storage_config
($cfg, $storeid);
821 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
830 my ($cfg, $volid) = @_;
832 foreach my $storeid (keys %{$cfg->{ids
}}) {
833 my $scfg = storage_config
($cfg, $storeid);
834 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
842 sub storage_is_used
{
843 my ($cfg, $storeid) = @_;
845 foreach my $sid (keys %{$cfg->{ids
}}) {
846 my $scfg = storage_config
($cfg, $sid);
847 next if !$scfg->{base
};
848 my ($st) = parse_volume_id
($scfg->{base
});
849 return 1 if $st && $st eq $storeid;
856 my ($list, $func) = @_;
860 foreach my $sid (keys %$list) {
861 foreach my $info (@{$list->{$sid}}) {
862 my $volid = $info->{volid
};
863 my ($sid1, $volname) = parse_volume_id
($volid, 1);
864 if ($sid1 && $sid1 eq $sid) {
865 &$func ($volid, $sid, $info);
867 warn "detected strange volid '$volid' in volume list for '$sid'\n";