]>
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
;
27 # load and initialize all plugins
28 PVE
::Storage
::DirPlugin-
>register();
29 PVE
::Storage
::LVMPlugin-
>register();
30 PVE
::Storage
::NFSPlugin-
>register();
31 PVE
::Storage
::ISCSIPlugin-
>register();
32 PVE
::Storage
::RBDPlugin-
>register();
33 PVE
::Storage
::SheepdogPlugin-
>register();
34 PVE
::Storage
::Plugin-
>init();
36 my $UDEVADM = '/sbin/udevadm';
38 # PVE::Storage utility functions
41 return cfs_read_file
("storage.cfg");
44 sub lock_storage_config
{
45 my ($code, $errmsg) = @_;
47 cfs_lock_file
("storage.cfg", undef, $code);
50 $errmsg ?
die "$errmsg: $err" : die $err;
55 my ($cfg, $storeid, $noerr) = @_;
57 die "no storage id specified\n" if !$storeid;
59 my $scfg = $cfg->{ids
}->{$storeid};
61 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
66 sub storage_check_node
{
67 my ($cfg, $storeid, $node, $noerr) = @_;
69 my $scfg = storage_config
($cfg, $storeid);
72 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
73 if (!$scfg->{nodes
}->{$node}) {
74 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
82 sub storage_check_enabled
{
83 my ($cfg, $storeid, $node, $noerr) = @_;
85 my $scfg = storage_config
($cfg, $storeid);
87 if ($scfg->{disable
}) {
88 die "storage '$storeid' is disabled\n" if !$noerr;
92 return storage_check_node
($cfg, $storeid, $node, $noerr);
98 return keys %{$cfg->{ids
}};
102 my ($filename, $timeout) = @_;
104 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
108 my ($cfg, $storeid, $vmid) = @_;
110 my $scfg = storage_config
($cfg, $storeid);
111 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
113 my $path = $plugin->get_subdir($scfg, 'images');
115 return $vmid ?
"$path/$vmid" : $path;
118 sub get_private_dir
{
119 my ($cfg, $storeid, $vmid) = @_;
121 my $scfg = storage_config
($cfg, $storeid);
122 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
124 my $path = $plugin->get_subdir($scfg, 'rootdir');
126 return $vmid ?
"$path/$vmid" : $path;
130 my ($cfg, $storeid) = @_;
132 my $scfg = storage_config
($cfg, $storeid);
133 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
135 return $plugin->get_subdir($scfg, 'iso');
139 my ($cfg, $storeid) = @_;
141 my $scfg = storage_config
($cfg, $storeid);
142 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
144 return $plugin->get_subdir($scfg, 'vztmpl');
148 my ($cfg, $storeid) = @_;
150 my $scfg = storage_config
($cfg, $storeid);
151 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
153 return $plugin->get_subdir($scfg, 'backup');
156 # library implementation
161 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
166 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
167 sub parse_volume_id
{
168 my ($volid, $noerr) = @_;
170 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
171 return wantarray ?
($1, $2) : $1;
173 return undef if $noerr;
174 die "unable to parse volume ID '$volid'\n";
177 # try to map a filesystem path to a volume identifier
178 sub path_to_volume_id
{
179 my ($cfg, $path) = @_;
181 my $ids = $cfg->{ids
};
183 my ($sid, $volname) = parse_volume_id
($path, 1);
185 if (my $scfg = $ids->{$sid}) {
186 if (my $path = $scfg->{path
}) {
187 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
188 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
189 return ($vtype, $path);
195 # Note: abs_path() return undef if $path doesn not exist
196 # for example when nfs storage is not mounted
197 $path = abs_path
($path) || $path;
199 foreach my $sid (keys %$ids) {
200 my $scfg = $ids->{$sid};
201 next if !$scfg->{path
};
202 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
203 my $imagedir = $plugin->get_subdir($scfg, 'images');
204 my $isodir = $plugin->get_subdir($scfg, 'iso');
205 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
206 my $backupdir = $plugin->get_subdir($scfg, 'backup');
207 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
209 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
212 return ('images', "$sid:$vmid/$name");
213 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
215 return ('iso', "$sid:iso/$name");
216 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
218 return ('vztmpl', "$sid:vztmpl/$name");
219 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
221 return ('rootdir', "$sid:rootdir/$vmid");
222 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
224 return ('iso', "$sid:backup/$name");
228 # can't map path to volume id
233 my ($cfg, $volid) = @_;
235 my ($storeid, $volname) = parse_volume_id
($volid);
237 my $scfg = storage_config
($cfg, $storeid);
239 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
240 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid);
241 return wantarray ?
($path, $owner, $vtype) : $path;
244 sub storage_migrate
{
245 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
247 my ($storeid, $volname) = parse_volume_id
($volid);
248 $target_volname = $volname if !$target_volname;
250 my $scfg = storage_config
($cfg, $storeid);
252 # no need to migrate shared content
253 return if $storeid eq $target_storeid && $scfg->{shared
};
255 my $tcfg = storage_config
($cfg, $target_storeid);
257 my $target_volid = "${target_storeid}:${target_volname}";
259 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
261 # blowfish is a fast block cipher, much faster then 3des
262 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
263 my $ssh = "/usr/bin/ssh $sshoptions";
265 local $ENV{RSYNC_RSH
} = $ssh;
267 # only implemented for file system based storage
271 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
272 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
273 my $src = $src_plugin->path($scfg, $volname, $storeid);
274 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
276 my $dirname = dirname
($dst);
278 if ($tcfg->{shared
}) { # we can do a local copy
280 run_command
(['/bin/mkdir', '-p', $dirname]);
282 run_command
(['/bin/cp', $src, $dst]);
286 run_command
(['/usr/bin/ssh', "root\@${target_host}",
287 '/bin/mkdir', '-p', $dirname]);
289 # we use rsync with --sparse, so we can't use --inplace,
290 # so we remove file on the target if it already exists to
292 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
293 if ($format && ($format eq 'raw') && $size) {
294 run_command
(['/usr/bin/ssh', "root\@${target_host}",
299 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
300 $src, "root\@${target_host}:$dst"];
304 run_command
($cmd, outfunc
=> sub {
307 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
310 print "rsync status: $1\n";
320 die "$errstr - target type '$tcfg->{type}' not implemented\n";
323 die "$errstr - source type '$scfg->{type}' not implemented\n";
328 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
330 die "no storage id specified\n" if !$storeid;
332 PVE
::JSONSchema
::parse_storage_id
($storeid);
334 my $scfg = storage_config
($cfg, $storeid);
336 die "no VMID specified\n" if !$vmid;
338 $vmid = parse_vmid
($vmid);
340 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
342 $fmt = $defformat if !$fmt;
344 activate_storage
($cfg, $storeid);
346 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
348 # lock shared storage
349 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
350 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
351 return "$storeid:$volname";
356 my ($cfg, $volid) = @_;
358 my ($storeid, $volname) = parse_volume_id
($volid);
360 my $scfg = storage_config
($cfg, $storeid);
362 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
364 activate_storage
($cfg, $storeid);
368 # lock shared storage
369 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
370 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
373 return if !$cleanup_worker;
375 my $rpcenv = PVE
::RPCEnvironment
::get
();
376 my $authuser = $rpcenv->get_user();
378 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
381 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
383 my ($cfg, $storeid, $tt) = @_;
385 die "unknown template type '$tt'\n"
386 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
388 my $ids = $cfg->{ids
};
390 storage_check_enabled
($cfg, $storeid) if ($storeid);
396 foreach my $sid (keys %$ids) {
397 next if $storeid && $storeid ne $sid;
399 my $scfg = $ids->{$sid};
400 my $type = $scfg->{type
};
402 next if !storage_check_enabled
($cfg, $sid, undef, 1);
404 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
405 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
406 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
408 activate_storage
($cfg, $sid);
411 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
413 my $path = $plugin->get_subdir($scfg, $tt);
415 foreach my $fn (<$path/*>) {
420 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
422 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
424 } elsif ($tt eq 'vztmpl') {
425 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
427 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
429 } elsif ($tt eq 'backup') {
430 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
432 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
435 $info->{size
} = -s
$fn;
437 push @{$res->{$sid}}, $info;
442 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
449 my ($cfg, $storeid, $vmid, $vollist) = @_;
451 my $ids = $cfg->{ids
};
453 storage_check_enabled
($cfg, $storeid) if ($storeid);
457 # prepare/activate/refresh all storages
459 my $storage_list = [];
461 foreach my $volid (@$vollist) {
462 my ($sid, undef) = parse_volume_id
($volid);
463 next if !defined($ids->{$sid});
464 next if !storage_check_enabled
($cfg, $sid, undef, 1);
465 push @$storage_list, $sid;
468 foreach my $sid (keys %$ids) {
469 next if $storeid && $storeid ne $sid;
470 next if !storage_check_enabled
($cfg, $sid, undef, 1);
471 push @$storage_list, $sid;
477 activate_storage_list
($cfg, $storage_list, $cache);
479 foreach my $sid (keys %$ids) {
480 next if $storeid && $storeid ne $sid;
481 next if !storage_check_enabled
($cfg, $sid, undef, 1);
483 my $scfg = $ids->{$sid};
484 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
485 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
486 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
494 my $filename = "/sys/kernel/uevent_seqnum";
497 if (my $fh = IO
::File-
>new($filename, "r")) {
499 if ($line =~ m/^(\d+)$/) {
507 sub __activate_storage_full
{
508 my ($cfg, $storeid, $cache) = @_;
510 my $scfg = storage_check_enabled
($cfg, $storeid);
512 return if $cache->{activated
}->{$storeid};
514 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
516 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
519 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
520 __activate_storage_full
($cfg, $baseid, $cache);
523 $plugin->activate_storage($storeid, $scfg, $cache);
525 my $newseq = uevent_seqnum
();
527 # only call udevsettle if there are events
528 if ($newseq > $cache->{uevent_seqnum
}) {
530 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
531 $cache->{uevent_seqnum
} = $newseq;
534 $cache->{activated
}->{$storeid} = 1;
537 sub activate_storage_list
{
538 my ($cfg, $storeid_list, $cache) = @_;
540 $cache = {} if !$cache;
542 foreach my $storeid (@$storeid_list) {
543 __activate_storage_full
($cfg, $storeid, $cache);
547 sub activate_storage
{
548 my ($cfg, $storeid) = @_;
552 __activate_storage_full
($cfg, $storeid, $cache);
556 sub deactivate_storage
{
557 my ($cfg, $storeid) = @_;
559 my $scfg = storage_config
($cfg, $storeid);
560 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
563 $plugin->deactivate_storage($storeid, $scfg, $cache);
566 sub activate_volumes
{
567 my ($cfg, $vollist, $exclusive) = @_;
569 return if !($vollist && scalar(@$vollist));
571 my $storagehash = {};
572 foreach my $volid (@$vollist) {
573 my ($storeid, undef) = parse_volume_id
($volid);
574 $storagehash->{$storeid} = 1;
579 activate_storage_list
($cfg, [keys %$storagehash], $cache);
581 foreach my $volid (@$vollist) {
582 my ($storeid, $volname) = parse_volume_id
($volid);
583 my $scfg = storage_config
($cfg, $storeid);
584 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
585 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
589 sub deactivate_volumes
{
590 my ($cfg, $vollist) = @_;
592 return if !($vollist && scalar(@$vollist));
597 foreach my $volid (@$vollist) {
598 my ($storeid, $volname) = parse_volume_id
($volid);
600 my $scfg = storage_config
($cfg, $storeid);
601 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
604 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
608 push @errlist, $volid;
612 die "volume deativation failed: " . join(' ', @errlist)
617 my ($cfg, $content) = @_;
619 my $ids = $cfg->{ids
};
624 foreach my $storeid (keys %$ids) {
626 next if $content && !$ids->{$storeid}->{content
}->{$content};
628 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
630 my $type = $ids->{$storeid}->{type
};
632 $info->{$storeid} = {
637 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
638 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
642 push @$slist, $storeid;
647 eval { activate_storage_list
($cfg, $slist, $cache); };
649 foreach my $storeid (keys %$ids) {
650 my $scfg = $ids->{$storeid};
651 next if !$info->{$storeid};
653 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
654 my ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache);
656 $info->{$storeid}->{total
} = $total;
657 $info->{$storeid}->{avail
} = $avail;
658 $info->{$storeid}->{used
} = $used;
659 $info->{$storeid}->{active
} = $active;
668 my $packed_ip = gethostbyname($server);
669 if (defined $packed_ip) {
670 return inet_ntoa
($packed_ip);
676 my ($server_in) = @_;
679 if (!($server = resolv_server
($server_in))) {
680 die "unable to resolve address for server '${server_in}'\n";
683 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
686 run_command
($cmd, outfunc
=> sub {
689 # note: howto handle white spaces in export path??
690 if ($line =~ m!^(/\S+)\s+(.+)$!) {
699 my ($portal, $noerr) = @_;
701 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
705 if (my $ip = resolv_server
($server)) {
707 return $port ?
"$server:$port" : $server;
710 return undef if $noerr;
712 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
715 # idea is from usbutils package (/usr/bin/usb-devices) script
716 sub __scan_usb_device
{
717 my ($res, $devpath, $parent, $level) = @_;
719 return if ! -d
$devpath;
720 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
721 my $port = $level ?
int($1 - 1) : 0;
723 my $busnum = int(file_read_firstline
("$devpath/busnum"));
724 my $devnum = int(file_read_firstline
("$devpath/devnum"));
731 speed
=> file_read_firstline
("$devpath/speed"),
732 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
733 vendid
=> file_read_firstline
("$devpath/idVendor"),
734 prodid
=> file_read_firstline
("$devpath/idProduct"),
738 my $usbpath = $devpath;
739 $usbpath =~ s
|^.*/\d
+\
-||;
740 $d->{usbpath
} = $usbpath;
743 my $product = file_read_firstline
("$devpath/product");
744 $d->{product
} = $product if $product;
746 my $manu = file_read_firstline
("$devpath/manufacturer");
747 $d->{manufacturer
} = $manu if $manu;
749 my $serial => file_read_firstline
("$devpath/serial");
750 $d->{serial
} = $serial if $serial;
754 foreach my $subdev (<$devpath/$busnum-*>) {
755 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
756 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
765 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
766 __scan_usb_device
($devlist, $device, 0, 0);
773 my ($portal_in) = @_;
776 if (!($portal = resolv_portal
($portal_in))) {
777 die "unable to parse/resolve portal address '${portal_in}'\n";
780 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
783 sub storage_default_format
{
784 my ($cfg, $storeid) = @_;
786 my $scfg = storage_config
($cfg, $storeid);
788 return PVE
::Storage
::Plugin
::default_format
($scfg);
792 my ($cfg, $vgname) = @_;
794 foreach my $storeid (keys %{$cfg->{ids
}}) {
795 my $scfg = storage_config
($cfg, $storeid);
796 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
805 my ($cfg, $target) = @_;
807 foreach my $storeid (keys %{$cfg->{ids
}}) {
808 my $scfg = storage_config
($cfg, $storeid);
809 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
818 my ($cfg, $volid) = @_;
820 foreach my $storeid (keys %{$cfg->{ids
}}) {
821 my $scfg = storage_config
($cfg, $storeid);
822 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
830 sub storage_is_used
{
831 my ($cfg, $storeid) = @_;
833 foreach my $sid (keys %{$cfg->{ids
}}) {
834 my $scfg = storage_config
($cfg, $sid);
835 next if !$scfg->{base
};
836 my ($st) = parse_volume_id
($scfg->{base
});
837 return 1 if $st && $st eq $storeid;
844 my ($list, $func) = @_;
848 foreach my $sid (keys %$list) {
849 foreach my $info (@{$list->{$sid}}) {
850 my $volid = $info->{volid
};
851 my ($sid1, $volname) = parse_volume_id
($volid, 1);
852 if ($sid1 && $sid1 eq $sid) {
853 &$func ($volid, $sid, $info);
855 warn "detected strange volid '$volid' in volume list for '$sid'\n";