]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage.pm
1d5abbdc568f70aa143a953e1bceb17f5329c8c5
11 use PVE
::Tools
qw(run_command file_read_firstline);
12 use PVE
::Cluster
qw(cfs_read_file cfs_lock_file);
13 use PVE
::Exception
qw(raise_param_exc);
16 use PVE
::RPCEnvironment
;
18 use PVE
::Storage
::Plugin
;
19 use PVE
::Storage
::DirPlugin
;
20 use PVE
::Storage
::LVMPlugin
;
21 use PVE
::Storage
::NFSPlugin
;
22 use PVE
::Storage
::ISCSIPlugin
;
24 # load and initialize all plugins
25 PVE
::Storage
::DirPlugin-
>register();
26 PVE
::Storage
::LVMPlugin-
>register();
27 PVE
::Storage
::NFSPlugin-
>register();
28 PVE
::Storage
::ISCSIPlugin-
>register();
29 PVE
::Storage
::Plugin-
>init();
31 my $UDEVADM = '/sbin/udevadm';
33 # PVE::Storage utility functions
36 return cfs_read_file
("storage.cfg");
39 sub lock_storage_config
{
40 my ($code, $errmsg) = @_;
42 cfs_lock_file
("storage.cfg", undef, $code);
45 $errmsg ?
die "$errmsg: $err" : die $err;
50 my ($cfg, $storeid, $noerr) = @_;
52 die "no storage id specified\n" if !$storeid;
54 my $scfg = $cfg->{ids
}->{$storeid};
56 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
61 sub storage_check_node
{
62 my ($cfg, $storeid, $node, $noerr) = @_;
64 my $scfg = storage_config
($cfg, $storeid);
67 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
68 if (!$scfg->{nodes
}->{$node}) {
69 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
77 sub storage_check_enabled
{
78 my ($cfg, $storeid, $node, $noerr) = @_;
80 my $scfg = storage_config
($cfg, $storeid);
82 if ($scfg->{disable
}) {
83 die "storage '$storeid' is disabled\n" if !$noerr;
87 return storage_check_node
($cfg, $storeid, $node, $noerr);
93 return keys %{$cfg->{ids
}};
97 my ($filename, $timeout) = @_;
99 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
103 my ($cfg, $storeid, $vmid) = @_;
105 my $scfg = storage_config
($cfg, $storeid);
106 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
108 my $path = $plugin->get_subdir($scfg, 'images');
110 return $vmid ?
"$path/$vmid" : $path;
113 sub get_private_dir
{
114 my ($cfg, $storeid, $vmid) = @_;
116 my $scfg = storage_config
($cfg, $storeid);
117 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
119 my $path = $plugin->get_subdir($scfg, 'rootdir');
121 return $vmid ?
"$path/$vmid" : $path;
125 my ($cfg, $storeid) = @_;
127 my $scfg = storage_config
($cfg, $storeid);
128 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
130 return $plugin->get_subdir($scfg, 'iso');
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, 'vztmpl');
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, 'backup');
151 # library implementation
156 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
161 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
162 sub parse_volume_id
{
163 my ($volid, $noerr) = @_;
165 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
166 return wantarray ?
($1, $2) : $1;
168 return undef if $noerr;
169 die "unable to parse volume ID '$volid'\n";
172 # try to map a filesystem path to a volume identifier
173 sub path_to_volume_id
{
174 my ($cfg, $path) = @_;
176 my $ids = $cfg->{ids
};
178 my ($sid, $volname) = parse_volume_id
($path, 1);
180 if (my $scfg = $ids->{$sid}) {
181 if (my $path = $scfg->{path
}) {
182 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
183 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
184 return ($vtype, $path);
190 # Note: abs_path() return undef if $path doesn not exist
191 # for example when nfs storage is not mounted
192 $path = abs_path
($path) || $path;
194 foreach my $sid (keys %$ids) {
195 my $scfg = $ids->{$sid};
196 next if !$scfg->{path
};
197 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
198 my $imagedir = $plugin->get_subdir($scfg, 'images');
199 my $isodir = $plugin->get_subdir($scfg, 'iso');
200 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
201 my $backupdir = $plugin->get_subdir($scfg, 'backup');
202 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
204 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
207 return ('images', "$sid:$vmid/$name");
208 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
210 return ('iso', "$sid:iso/$name");
211 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
213 return ('vztmpl', "$sid:vztmpl/$name");
214 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
216 return ('rootdir', "$sid:rootdir/$vmid");
217 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
219 return ('iso', "$sid:backup/$name");
223 # can't map path to volume id
228 my ($cfg, $volid) = @_;
230 my ($storeid, $volname) = parse_volume_id
($volid);
232 my $scfg = storage_config
($cfg, $storeid);
234 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
235 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname);
236 return wantarray ?
($path, $owner, $vtype) : $path;
239 sub storage_migrate
{
240 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
242 my ($storeid, $volname) = parse_volume_id
($volid);
243 $target_volname = $volname if !$target_volname;
245 my $scfg = storage_config
($cfg, $storeid);
247 # no need to migrate shared content
248 return if $storeid eq $target_storeid && $scfg->{shared
};
250 my $tcfg = storage_config
($cfg, $target_storeid);
252 my $target_volid = "${target_storeid}:${target_volname}";
254 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
256 # blowfish is a fast block cipher, much faster then 3des
257 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
258 my $ssh = "/usr/bin/ssh $sshoptions";
260 local $ENV{RSYNC_RSH
} = $ssh;
262 # only implemented for file system based storage
266 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
267 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
268 my $src = $src_plugin->path($scfg, $volid);
269 my $dst = $dst_plugin->path($tcfg, $target_volid);
271 my $dirname = dirname
($dst);
273 if ($tcfg->{shared
}) { # we can do a local copy
275 run_command
(['/bin/mkdir', '-p', $dirname]);
277 run_command
(['/bin/cp', $src, $dst]);
281 run_command
(['/usr/bin/ssh', "root\@${target_host}",
282 '/bin/mkdir', '-p', $dirname]);
284 # we use rsync with --sparse, so we can't use --inplace,
285 # so we remove file on the target if it already exists to
287 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
288 if ($format && ($format eq 'raw') && $size) {
289 run_command
(['/usr/bin/ssh', "root\@${target_host}",
294 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
295 $src, "root\@${target_host}:$dst"];
299 run_command
($cmd, outfunc
=> sub {
302 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
305 print "rsync status: $1\n";
315 die "$errstr - target type '$tcfg->{type}' not implemented\n";
318 die "$errstr - source type '$scfg->{type}' not implemented\n";
323 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
325 die "no storage id specified\n" if !$storeid;
327 PVE
::JSONSchema
::parse_storage_id
($storeid);
329 my $scfg = storage_config
($cfg, $storeid);
331 die "no VMID specified\n" if !$vmid;
333 $vmid = parse_vmid
($vmid);
335 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
337 $fmt = $defformat if !$fmt;
339 activate_storage
($cfg, $storeid);
341 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
343 # lock shared storage
344 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
345 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
346 return "$storeid:$volname";
351 my ($cfg, $volid) = @_;
353 my ($storeid, $volname) = parse_volume_id
($volid);
355 my $scfg = storage_config
($cfg, $storeid);
357 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
359 activate_storage
($cfg, $storeid);
363 # lock shared storage
364 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
365 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
368 return if !$cleanup_worker;
370 my $rpcenv = PVE
::RPCEnvironment
::get
();
371 my $authuser = $rpcenv->get_user();
373 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
376 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
378 my ($cfg, $storeid, $tt) = @_;
380 die "unknown template type '$tt'\n"
381 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
383 my $ids = $cfg->{ids
};
385 storage_check_enabled
($cfg, $storeid) if ($storeid);
391 foreach my $sid (keys %$ids) {
392 next if $storeid && $storeid ne $sid;
394 my $scfg = $ids->{$sid};
395 my $type = $scfg->{type
};
397 next if !storage_check_enabled
($cfg, $sid, undef, 1);
399 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
400 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
401 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
403 activate_storage
($cfg, $sid);
406 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
408 my $path = $plugin->get_subdir($scfg, $tt);
410 foreach my $fn (<$path/*>) {
415 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
417 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
419 } elsif ($tt eq 'vztmpl') {
420 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
422 $info = { volid
=> "$sid:vztmpl/$1", format
=> 'tgz' };
424 } elsif ($tt eq 'backup') {
425 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
427 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
430 $info->{size
} = -s
$fn;
432 push @{$res->{$sid}}, $info;
437 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
444 my ($cfg, $storeid, $vmid, $vollist) = @_;
446 my $ids = $cfg->{ids
};
448 storage_check_enabled
($cfg, $storeid) if ($storeid);
452 # prepare/activate/refresh all storages
454 my $storage_list = [];
456 foreach my $volid (@$vollist) {
457 my ($sid, undef) = parse_volume_id
($volid);
458 next if !defined($ids->{$sid});
459 next if !storage_check_enabled
($cfg, $sid, undef, 1);
460 push @$storage_list, $sid;
463 foreach my $sid (keys %$ids) {
464 next if $storeid && $storeid ne $sid;
465 next if !storage_check_enabled
($cfg, $sid, undef, 1);
466 push @$storage_list, $sid;
472 activate_storage_list
($cfg, $storage_list, $cache);
474 foreach my $sid (keys %$ids) {
475 next if $storeid && $storeid ne $sid;
476 next if !storage_check_enabled
($cfg, $sid, undef, 1);
478 my $scfg = $ids->{$sid};
479 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
480 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
481 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
489 my $filename = "/sys/kernel/uevent_seqnum";
492 if (my $fh = IO
::File-
>new($filename, "r")) {
494 if ($line =~ m/^(\d+)$/) {
502 sub __activate_storage_full
{
503 my ($cfg, $storeid, $cache) = @_;
505 my $scfg = storage_check_enabled
($cfg, $storeid);
507 return if $cache->{activated
}->{$storeid};
509 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
511 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
514 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
515 __activate_storage_full
($cfg, $baseid, $cache);
518 $plugin->activate_storage($storeid, $scfg, $cache);
520 my $newseq = uevent_seqnum
();
522 # only call udevsettle if there are events
523 if ($newseq > $cache->{uevent_seqnum
}) {
525 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
526 $cache->{uevent_seqnum
} = $newseq;
529 $cache->{activated
}->{$storeid} = 1;
532 sub activate_storage_list
{
533 my ($cfg, $storeid_list, $cache) = @_;
535 $cache = {} if !$cache;
537 foreach my $storeid (@$storeid_list) {
538 __activate_storage_full
($cfg, $storeid, $cache);
542 sub activate_storage
{
543 my ($cfg, $storeid) = @_;
547 __activate_storage_full
($cfg, $storeid, $cache);
551 sub deactivate_storage
{
552 my ($cfg, $storeid) = @_;
554 my $scfg = storage_config
($cfg, $storeid);
555 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
558 $plugin->deactivate_storage($storeid, $scfg, $cache);
561 sub activate_volumes
{
562 my ($cfg, $vollist, $exclusive) = @_;
564 return if !($vollist && scalar(@$vollist));
566 my $storagehash = {};
567 foreach my $volid (@$vollist) {
568 my ($storeid, undef) = parse_volume_id
($volid);
569 $storagehash->{$storeid} = 1;
574 activate_storage_list
($cfg, [keys %$storagehash], $cache);
576 foreach my $volid (@$vollist) {
577 my ($storeid, $volname) = parse_volume_id
($volid);
578 my $scfg = storage_config
($cfg, $storeid);
579 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
580 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
584 sub deactivate_volumes
{
585 my ($cfg, $vollist) = @_;
587 return if !($vollist && scalar(@$vollist));
592 foreach my $volid (@$vollist) {
593 my ($storeid, $volname) = parse_volume_id
($volid);
595 my $scfg = storage_config
($cfg, $storeid);
596 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
599 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
603 push @errlist, $volid;
607 die "volume deativation failed: " . join(' ', @errlist)
612 my ($cfg, $content) = @_;
614 my $ids = $cfg->{ids
};
619 foreach my $storeid (keys %$ids) {
621 next if $content && !$ids->{$storeid}->{content
}->{$content};
623 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
625 my $type = $ids->{$storeid}->{type
};
627 $info->{$storeid} = {
632 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
633 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
637 push @$slist, $storeid;
642 eval { activate_storage_list
($cfg, $slist, $cache); };
644 foreach my $storeid (keys %$ids) {
645 my $scfg = $ids->{$storeid};
646 next if !$info->{$storeid};
648 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
649 my ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache);
651 $info->{$storeid}->{total
} = $total;
652 $info->{$storeid}->{avail
} = $avail;
653 $info->{$storeid}->{used
} = $used;
654 $info->{$storeid}->{active
} = $active;
663 my $packed_ip = gethostbyname($server);
664 if (defined $packed_ip) {
665 return inet_ntoa
($packed_ip);
671 my ($server_in) = @_;
674 if (!($server = resolv_server
($server_in))) {
675 die "unable to resolve address for server '${server_in}'\n";
678 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
681 run_command
($cmd, outfunc
=> sub {
684 # note: howto handle white spaces in export path??
685 if ($line =~ m!^(/\S+)\s+(.+)$!) {
694 my ($portal, $noerr) = @_;
696 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
700 if (my $ip = resolv_server
($server)) {
702 return $port ?
"$server:$port" : $server;
705 return undef if $noerr;
707 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
710 # idea is from usbutils package (/usr/bin/usb-devices) script
711 sub __scan_usb_device
{
712 my ($res, $devpath, $parent, $level) = @_;
714 return if ! -d
$devpath;
715 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
716 my $port = $level ?
int($1 - 1) : 0;
718 my $busnum = int(file_read_firstline
("$devpath/busnum"));
719 my $devnum = int(file_read_firstline
("$devpath/devnum"));
726 speed
=> file_read_firstline
("$devpath/speed"),
727 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
728 vendid
=> file_read_firstline
("$devpath/idVendor"),
729 prodid
=> file_read_firstline
("$devpath/idProduct"),
733 my $usbpath = $devpath;
734 $usbpath =~ s
|^.*/\d
+\
-||;
735 $d->{usbpath
} = $usbpath;
738 my $product = file_read_firstline
("$devpath/product");
739 $d->{product
} = $product if $product;
741 my $manu = file_read_firstline
("$devpath/manufacturer");
742 $d->{manufacturer
} = $manu if $manu;
744 my $serial => file_read_firstline
("$devpath/serial");
745 $d->{serial
} = $serial if $serial;
749 foreach my $subdev (<$devpath/$busnum-*>) {
750 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
751 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
760 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
761 __scan_usb_device
($devlist, $device, 0, 0);
768 my ($portal_in) = @_;
771 if (!($portal = resolv_portal
($portal_in))) {
772 die "unable to parse/resolve portal address '${portal_in}'\n";
775 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
778 sub storage_default_format
{
779 my ($cfg, $storeid) = @_;
781 my $scfg = storage_config
($cfg, $storeid);
783 return PVE
::Storage
::Plugin
::default_format
($scfg);
787 my ($cfg, $vgname) = @_;
789 foreach my $storeid (keys %{$cfg->{ids
}}) {
790 my $scfg = storage_config
($cfg, $storeid);
791 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
800 my ($cfg, $target) = @_;
802 foreach my $storeid (keys %{$cfg->{ids
}}) {
803 my $scfg = storage_config
($cfg, $storeid);
804 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
813 my ($cfg, $volid) = @_;
815 foreach my $storeid (keys %{$cfg->{ids
}}) {
816 my $scfg = storage_config
($cfg, $storeid);
817 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
825 sub storage_is_used
{
826 my ($cfg, $storeid) = @_;
828 foreach my $sid (keys %{$cfg->{ids
}}) {
829 my $scfg = storage_config
($cfg, $sid);
830 next if !$scfg->{base
};
831 my ($st) = parse_volume_id
($scfg->{base
});
832 return 1 if $st && $st eq $storeid;
839 my ($list, $func) = @_;
843 foreach my $sid (keys %$list) {
844 foreach my $info (@{$list->{$sid}}) {
845 my $volid = $info->{volid
};
846 my ($sid1, $volname) = parse_volume_id
($volid, 1);
847 if ($sid1 && $sid1 eq $sid) {
848 &$func ($volid, $sid, $info);
850 warn "detected strange volid '$volid' in volume list for '$sid'\n";