]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
1 package PVE
::Storage
::Plugin
;
6 use PVE
::Tools
qw(run_command);
7 use PVE
::JSONSchema
qw(get_standard_option);
8 use PVE
::Cluster
qw(cfs_register_file);
12 use base
qw(PVE::SectionConfig);
14 cfs_register_file
('storage.cfg',
15 sub { __PACKAGE__-
>parse_config(@_); },
16 sub { __PACKAGE__-
>write_config(@_); });
20 type
=> { description
=> "Storage type." },
21 storage
=> get_standard_option
('pve-storage-id'),
22 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
24 description
=> "Allowed content types.",
25 type
=> 'string', format
=> 'pve-storage-content-list',
29 description
=> "Flag to disable the storage.",
34 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
40 description
=> "Mark storage as shared.",
45 description
=> "Default Image format.",
46 type
=> 'string', format
=> 'pve-storage-format',
52 sub content_hash_to_string
{
56 foreach my $ct (keys %$hash) {
57 push @cta, $ct if $hash->{$ct};
60 return join(',', @cta);
63 sub valid_content_types
{
66 my $def = $defaultData->{plugindata
}->{$type};
70 return $def->{content
}->[0];
76 my $type = $scfg->{type
};
77 my $def = $defaultData->{plugindata
}->{$type};
79 my $def_format = 'raw';
80 my $valid_formats = [ $def_format ];
82 if (defined($def->{format
})) {
83 $def_format = $scfg->{format
} || $def->{format
}->[1];
84 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
87 return wantarray ?
($def_format, $valid_formats) : $def_format;
90 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
92 my ($path, $noerr) = @_;
94 # fixme: exclude more shell meta characters?
95 # we need absolute paths
96 if ($path !~ m
|^/[^;\
(\
)]+|) {
97 return undef if $noerr;
98 die "value does not look like a valid absolute path\n";
103 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
105 my ($server, $noerr) = @_;
107 # fixme: use better regex ?
109 if ($server !~ m/^[[:alnum:]\-\.]+$/) {
110 return undef if $noerr;
111 die "value does not look like a valid server name or IP address\n";
116 # fixme: do we need this
117 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
119 # my ($portal, $noerr) = @_;
121 # # IP with optional port
122 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
123 # return undef if $noerr;
124 # die "value does not look like a valid portal address\n";
129 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
130 sub verify_portal_dns
{
131 my ($portal, $noerr) = @_;
133 # IP or DNS name with optional port
134 if ($portal !~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[[:alnum:]\-\.]+)(:\d+)?$/) {
135 return undef if $noerr;
136 die "value does not look like a valid portal address\n";
141 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
143 my ($ct, $noerr) = @_;
145 my $valid_content = valid_content_types
('dir'); # dir includes all types
147 if (!$valid_content->{$ct}) {
148 return undef if $noerr;
149 die "invalid content type '$ct'\n";
155 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
157 my ($fmt, $noerr) = @_;
159 if ($fmt !~ m/(raw|qcow2|vmdk)/) {
160 return undef if $noerr;
161 die "invalid format '$fmt'\n";
167 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
169 my ($value, $noerr) = @_;
171 # mount options (see man fstab)
172 if ($value !~ m/^\S+$/) {
173 return undef if $noerr;
174 die "invalid options '$value'\n";
185 sub parse_section_header
{
186 my ($class, $line) = @_;
188 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
189 my ($type, $storeid) = (lc($1), $2);
190 my $errmsg = undef; # set if you want to skip whole section
191 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
193 my $config = {}; # to return additional attributes
194 return ($type, $storeid, $errmsg, $config);
200 my ($class, $type, $key, $value) = @_;
202 my $def = $defaultData->{plugindata
}->{$type};
204 if ($key eq 'content') {
205 my $valid_content = $def->{content
}->[0];
209 foreach my $c (PVE
::Tools
::split_list
($value)) {
210 if (!$valid_content->{$c}) {
211 die "storage does not support content type '$c'\n";
216 if ($res->{none
} && scalar (keys %$res) > 1) {
217 die "unable to combine 'none' with other content types\n";
221 } elsif ($key eq 'format') {
222 my $valid_formats = $def->{format
}->[0];
224 if (!$valid_formats->{$value}) {
225 die "storage does not support format '$value'\n";
229 } elsif ($key eq 'nodes') {
232 foreach my $node (PVE
::Tools
::split_list
($value)) {
233 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
239 # no node restrictions for local storage
240 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
241 # die "storage '$storeid' does not allow node restrictions\n";
251 my ($class, $type, $key, $value) = @_;
253 if ($key eq 'nodes') {
254 return join(',', keys(%$value));
255 } elsif ($key eq 'content') {
256 my $res = content_hash_to_string
($value) || 'none';
264 my ($class, $filename, $raw) = @_;
266 my $cfg = $class->SUPER::parse_config
($filename, $raw);
267 my $ids = $cfg->{ids
};
269 # make sure we have a reasonable 'local:' storage
270 # openvz expects things to be there
271 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
272 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
275 priority
=> 0, # force first entry
276 path
=> '/var/lib/vz',
278 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
282 # we always need this for OpenVZ
283 $ids->{local}->{content
}->{rootdir
} = 1;
284 $ids->{local}->{content
}->{vztmpl
} = 1;
285 delete ($ids->{local}->{disable
});
287 # make sure we have a path
288 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
290 # remove node restrictions for local storage
291 delete($ids->{local}->{nodes
});
293 foreach my $storeid (keys %$ids) {
294 my $d = $ids->{$storeid};
295 my $type = $d->{type
};
297 my $def = $defaultData->{plugindata
}->{$type};
299 if ($def->{content
}) {
300 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
303 if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'nexenta' ) {
311 # Storage implementation
313 sub cluster_lock_storage
{
314 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
318 my $lockid = "pve-storage-$storeid";
319 my $lockdir = "/var/lock/pve-manager";
321 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
324 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
333 if ($name =~ m!^([^/\s]+\.(raw|qcow2|vmdk))$!) {
337 die "unable to parse volume filename '$name'\n";
341 my ($class, $volname) = @_;
343 if ($volname =~ m!^(\d+)/(\S+)$!) {
344 my ($vmid, $name) = ($1, $2);
345 parse_name_dir
($name);
346 return ('images', $name, $vmid);
347 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
349 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
350 return ('vztmpl', $1);
351 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
352 return ('rootdir', $1, $1);
353 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz)))$!) {
355 if ($fn =~ m/^vzdump-(openvz|qemu)-(\d+)-.+/) {
356 return ('backup', $fn, $2);
358 return ('backup', $fn);
361 die "unable to parse directory volume name '$volname'\n";
364 my $vtype_subdirs = {
366 rootdir
=> 'private',
367 iso
=> 'template/iso',
368 vztmpl
=> 'template/cache',
373 my ($class, $scfg, $vtype) = @_;
375 my $path = $scfg->{path
};
377 die "storage definintion has no path\n" if !$path;
379 my $subdir = $vtype_subdirs->{$vtype};
381 die "unknown vtype '$vtype'\n" if !defined($subdir);
383 return "$path/$subdir";
387 my ($class, $scfg, $volname, $storeid) = @_;
389 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
391 my $dir = $class->get_subdir($scfg, $vtype);
393 $dir .= "/$vmid" if $vtype eq 'images';
395 my $path = "$dir/$name";
397 return wantarray ?
($path, $vmid, $vtype) : $path;
401 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
403 my $imagedir = $class->get_subdir($scfg, 'images');
404 $imagedir .= "/$vmid";
409 for (my $i = 1; $i < 100; $i++) {
410 my @gr = <$imagedir/vm-$vmid-disk
-$i.*>;
412 $name = "vm-$vmid-disk-$i.$fmt";
418 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
421 my (undef, $tmpfmt) = parse_name_dir
($name);
423 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
426 my $path = "$imagedir/$name";
428 die "disk image '$path' already exists\n" if -e
$path;
430 my $cmd = ['/usr/bin/qemu-img', 'create'];
432 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
434 push @$cmd, '-f', $fmt, $path, "${size}K";
436 run_command
($cmd, errmsg
=> "unable to create image");
438 return "$vmid/$name";
442 my ($class, $storeid, $scfg, $volname) = @_;
444 my $path = $class->path($scfg, $volname);
447 warn "disk image '$path' does not exists\n";
456 my ($filename, $timeout) = @_;
458 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
465 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
468 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
470 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
472 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
476 $used *= 1024 if $u eq 'K';
477 $used *= (1024*1024) if $u eq 'M';
478 $used *= (1024*1024*1024) if $u eq 'G';
479 $used *= (1024*1024*1024*1024) if $u eq 'T';
486 return wantarray ?
($size, $format, $used) : $size;
489 sub volume_size_info
{
490 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
491 my $path = $class->path($scfg, $volname);
492 return file_size_info
($path, $timeout);
497 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
499 die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/;
501 return 1 if $running;
503 my $path = $class->path($scfg, $volname);
505 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
507 run_command
($cmd, timeout
=> 10);
512 sub volume_snapshot
{
513 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
515 die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
517 return 1 if $running;
519 my $path = $class->path($scfg, $volname);
521 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
528 sub volume_snapshot_rollback
{
529 my ($class, $scfg, $storeid, $volname, $snap) = @_;
531 die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
533 my $path = $class->path($scfg, $volname);
535 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
542 sub volume_snapshot_delete
{
543 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
545 die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/;
547 return 1 if $running;
549 my $path = $class->path($scfg, $volname);
551 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
559 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
561 my $imagedir = $class->get_subdir($scfg, 'images');
563 my ($defFmt, $vaidFmts) = default_format
($scfg);
564 my $fmts = join ('|', @$vaidFmts);
568 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
570 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
575 my $volid = "$storeid:$owner/$name";
578 my $found = grep { $_ eq $volid } @$vollist;
581 next if defined($vmid) && ($owner ne $vmid);
584 my ($size, $format, $used) = file_size_info
($fn);
586 if ($format && $size) {
588 volid
=> $volid, format
=> $format,
589 size
=> $size, vmid
=> $owner, used
=> $used };
598 my ($class, $storeid, $scfg, $cache) = @_;
600 my $path = $scfg->{path
};
602 die "storage definintion has no path\n" if !$path;
605 my $res = PVE
::Tools
::df
($path, $timeout);
607 return undef if !$res || !$res->{total
};
609 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
612 sub activate_storage
{
613 my ($class, $storeid, $scfg, $cache) = @_;
615 my $path = $scfg->{path
};
617 die "storage definintion has no path\n" if !$path;
619 die "unable to activate storage '$storeid' - " .
620 "directory '$path' does not exist\n" if ! -d
$path;
622 if (defined($scfg->{content
})) {
623 foreach my $vtype (keys %$vtype_subdirs) {
624 # OpenVZMigrate uses backup (dump) dir
625 if (defined($scfg->{content
}->{$vtype}) ||
626 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
627 my $subdir = $class->get_subdir($scfg, $vtype);
628 mkpath
$subdir if $subdir ne $path;
634 sub deactivate_storage
{
635 my ($class, $storeid, $scfg, $cache) = @_;
637 # do nothing by default
640 sub activate_volume
{
641 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
643 my $path = $class->path($scfg, $volname);
645 # check is volume exists
647 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
649 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
653 sub deactivate_volume
{
654 my ($class, $storeid, $scfg, $volname, $cache) = @_;
656 # do nothing by default
659 sub check_connection
{
660 my ($class, $storeid, $scfg) = @_;
661 # do nothing by default