]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
25e012c3979e8d40a1f9f577676c26319c66d3b8
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|vma|vma\.gz|vma\.lzo)))$!) {
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];
466 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
469 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
471 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
473 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
475 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
479 $used *= 1024 if $u eq 'K';
480 $used *= (1024*1024) if $u eq 'M';
481 $used *= (1024*1024*1024) if $u eq 'G';
482 $used *= (1024*1024*1024*1024) if $u eq 'T';
489 return wantarray ?
($size, $format, $used, $parent) : $size;
492 sub volume_size_info
{
493 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
494 my $path = $class->path($scfg, $volname);
495 return file_size_info
($path, $timeout);
500 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
502 die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/;
504 return 1 if $running;
506 my $path = $class->path($scfg, $volname);
508 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
510 run_command
($cmd, timeout
=> 10);
515 sub volume_snapshot
{
516 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
518 die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
520 return 1 if $running;
522 my $path = $class->path($scfg, $volname);
524 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
531 sub volume_snapshot_rollback
{
532 my ($class, $scfg, $storeid, $volname, $snap) = @_;
534 die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
536 my $path = $class->path($scfg, $volname);
538 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
545 sub volume_snapshot_delete
{
546 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
548 die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/;
550 return 1 if $running;
552 my $path = $class->path($scfg, $volname);
554 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
561 sub volume_has_feature
{
562 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
565 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
566 clone
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
569 if ($volname =~ m!^(\d+)/(\S+)$!) {
570 my ($vmid, $name) = ($1, $2);
571 my (undef, $format) = parse_name_dir
($name);
572 my $snap = $snapname ?
'snap' : 'current';
573 return 1 if defined($features->{$feature}->{$snap}->{$format});
580 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
582 my $imagedir = $class->get_subdir($scfg, 'images');
584 my ($defFmt, $vaidFmts) = default_format
($scfg);
585 my $fmts = join ('|', @$vaidFmts);
589 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
591 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
596 my $volid = "$storeid:$owner/$name";
599 my $found = grep { $_ eq $volid } @$vollist;
602 next if defined($vmid) && ($owner ne $vmid);
605 my ($size, $format, $used, $parent) = file_size_info
($fn);
607 if ($format && $size) {
609 volid
=> $volid, format
=> $format,
610 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent };
619 my ($class, $storeid, $scfg, $cache) = @_;
621 my $path = $scfg->{path
};
623 die "storage definintion has no path\n" if !$path;
626 my $res = PVE
::Tools
::df
($path, $timeout);
628 return undef if !$res || !$res->{total
};
630 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
633 sub activate_storage
{
634 my ($class, $storeid, $scfg, $cache) = @_;
636 my $path = $scfg->{path
};
638 die "storage definintion has no path\n" if !$path;
640 die "unable to activate storage '$storeid' - " .
641 "directory '$path' does not exist\n" if ! -d
$path;
643 if (defined($scfg->{content
})) {
644 foreach my $vtype (keys %$vtype_subdirs) {
645 # OpenVZMigrate uses backup (dump) dir
646 if (defined($scfg->{content
}->{$vtype}) ||
647 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
648 my $subdir = $class->get_subdir($scfg, $vtype);
649 mkpath
$subdir if $subdir ne $path;
655 sub deactivate_storage
{
656 my ($class, $storeid, $scfg, $cache) = @_;
658 # do nothing by default
661 sub activate_volume
{
662 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
664 my $path = $class->path($scfg, $volname);
666 # check is volume exists
668 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
670 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
674 sub deactivate_volume
{
675 my ($class, $storeid, $scfg, $volname, $cache) = @_;
677 # do nothing by default
680 sub check_connection
{
681 my ($class, $storeid, $scfg) = @_;
682 # do nothing by default