]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
1 package PVE
::Storage
::Plugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::Cluster
qw(cfs_register_file);
13 use base
qw(PVE::SectionConfig);
15 cfs_register_file
('storage.cfg',
16 sub { __PACKAGE__-
>parse_config(@_); },
17 sub { __PACKAGE__-
>write_config(@_); });
21 type
=> { description
=> "Storage type." },
22 storage
=> get_standard_option
('pve-storage-id'),
23 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
25 description
=> "Allowed content types.",
26 type
=> 'string', format
=> 'pve-storage-content-list',
30 description
=> "Flag to disable the storage.",
35 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
41 description
=> "Mark storage as shared.",
46 description
=> "Default Image format.",
47 type
=> 'string', format
=> 'pve-storage-format',
53 sub content_hash_to_string
{
57 foreach my $ct (keys %$hash) {
58 push @cta, $ct if $hash->{$ct};
61 return join(',', @cta);
64 sub valid_content_types
{
67 my $def = $defaultData->{plugindata
}->{$type};
71 return $def->{content
}->[0];
77 my $type = $scfg->{type
};
78 my $def = $defaultData->{plugindata
}->{$type};
80 my $def_format = 'raw';
81 my $valid_formats = [ $def_format ];
83 if (defined($def->{format
})) {
84 $def_format = $scfg->{format
} || $def->{format
}->[1];
85 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
88 return wantarray ?
($def_format, $valid_formats) : $def_format;
91 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
93 my ($path, $noerr) = @_;
95 # fixme: exclude more shell meta characters?
96 # we need absolute paths
97 if ($path !~ m
|^/[^;\
(\
)]+|) {
98 return undef if $noerr;
99 die "value does not look like a valid absolute path\n";
104 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
106 my ($server, $noerr) = @_;
108 # fixme: use better regex ?
110 if ($server !~ m/^[[:alnum:]\-\.]+$/) {
111 return undef if $noerr;
112 die "value does not look like a valid server name or IP address\n";
117 # fixme: do we need this
118 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
120 # my ($portal, $noerr) = @_;
122 # # IP with optional port
123 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
124 # return undef if $noerr;
125 # die "value does not look like a valid portal address\n";
130 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
131 sub verify_portal_dns
{
132 my ($portal, $noerr) = @_;
134 # IP or DNS name with optional port
135 if ($portal !~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[[:alnum:]\-\.]+)(:\d+)?$/) {
136 return undef if $noerr;
137 die "value does not look like a valid portal address\n";
142 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
144 my ($ct, $noerr) = @_;
146 my $valid_content = valid_content_types
('dir'); # dir includes all types
148 if (!$valid_content->{$ct}) {
149 return undef if $noerr;
150 die "invalid content type '$ct'\n";
156 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
158 my ($fmt, $noerr) = @_;
160 if ($fmt !~ m/(raw|qcow2|vmdk)/) {
161 return undef if $noerr;
162 die "invalid format '$fmt'\n";
168 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
170 my ($value, $noerr) = @_;
172 # mount options (see man fstab)
173 if ($value !~ m/^\S+$/) {
174 return undef if $noerr;
175 die "invalid options '$value'\n";
181 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
182 sub parse_volume_id
{
183 my ($volid, $noerr) = @_;
185 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
186 return wantarray ?
($1, $2) : $1;
188 return undef if $noerr;
189 die "unable to parse volume ID '$volid'\n";
197 sub parse_section_header
{
198 my ($class, $line) = @_;
200 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
201 my ($type, $storeid) = (lc($1), $2);
202 my $errmsg = undef; # set if you want to skip whole section
203 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
205 my $config = {}; # to return additional attributes
206 return ($type, $storeid, $errmsg, $config);
212 my ($class, $type, $key, $value) = @_;
214 my $def = $defaultData->{plugindata
}->{$type};
216 if ($key eq 'content') {
217 my $valid_content = $def->{content
}->[0];
221 foreach my $c (PVE
::Tools
::split_list
($value)) {
222 if (!$valid_content->{$c}) {
223 die "storage does not support content type '$c'\n";
228 if ($res->{none
} && scalar (keys %$res) > 1) {
229 die "unable to combine 'none' with other content types\n";
233 } elsif ($key eq 'format') {
234 my $valid_formats = $def->{format
}->[0];
236 if (!$valid_formats->{$value}) {
237 die "storage does not support format '$value'\n";
241 } elsif ($key eq 'nodes') {
244 foreach my $node (PVE
::Tools
::split_list
($value)) {
245 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
251 # no node restrictions for local storage
252 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
253 # die "storage '$storeid' does not allow node restrictions\n";
263 my ($class, $type, $key, $value) = @_;
265 if ($key eq 'nodes') {
266 return join(',', keys(%$value));
267 } elsif ($key eq 'content') {
268 my $res = content_hash_to_string
($value) || 'none';
276 my ($class, $filename, $raw) = @_;
278 my $cfg = $class->SUPER::parse_config
($filename, $raw);
279 my $ids = $cfg->{ids
};
281 # make sure we have a reasonable 'local:' storage
282 # openvz expects things to be there
283 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
284 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
287 priority
=> 0, # force first entry
288 path
=> '/var/lib/vz',
290 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
294 # we always need this for OpenVZ
295 $ids->{local}->{content
}->{rootdir
} = 1;
296 $ids->{local}->{content
}->{vztmpl
} = 1;
297 delete ($ids->{local}->{disable
});
299 # make sure we have a path
300 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
302 # remove node restrictions for local storage
303 delete($ids->{local}->{nodes
});
305 foreach my $storeid (keys %$ids) {
306 my $d = $ids->{$storeid};
307 my $type = $d->{type
};
309 my $def = $defaultData->{plugindata
}->{$type};
311 if ($def->{content
}) {
312 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
315 if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'nexenta' ) {
323 # Storage implementation
325 sub cluster_lock_storage
{
326 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
330 my $lockid = "pve-storage-$storeid";
331 my $lockdir = "/var/lock/pve-manager";
333 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
336 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
345 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
349 die "unable to parse volume filename '$name'\n";
353 my ($class, $volname) = @_;
355 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
356 my ($basedvmid, $basename) = ($1, $2);
357 parse_name_dir
($basename);
358 my ($vmid, $name) = ($3, $4);
359 my (undef, undef, $isBase) = parse_name_dir
($name);
360 return ('images', $name, $vmid, $basename, $basedvmid, $isBase);
361 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
362 my ($vmid, $name) = ($1, $2);
363 my (undef, undef, $isBase) = parse_name_dir
($name);
364 return ('images', $name, $vmid, undef, undef, $isBase);
365 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
367 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
368 return ('vztmpl', $1);
369 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
370 return ('rootdir', $1, $1);
371 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
373 if ($fn =~ m/^vzdump-(openvz|qemu)-(\d+)-.+/) {
374 return ('backup', $fn, $2);
376 return ('backup', $fn);
379 die "unable to parse directory volume name '$volname'\n";
382 my $vtype_subdirs = {
384 rootdir
=> 'private',
385 iso
=> 'template/iso',
386 vztmpl
=> 'template/cache',
391 my ($class, $scfg, $vtype) = @_;
393 my $path = $scfg->{path
};
395 die "storage definintion has no path\n" if !$path;
397 my $subdir = $vtype_subdirs->{$vtype};
399 die "unknown vtype '$vtype'\n" if !defined($subdir);
401 return "$path/$subdir";
405 my ($class, $scfg, $volname, $storeid) = @_;
407 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
409 my $dir = $class->get_subdir($scfg, $vtype);
411 $dir .= "/$vmid" if $vtype eq 'images';
413 my $path = "$dir/$name";
415 return wantarray ?
($path, $vmid, $vtype) : $path;
419 my ($class, $storeid, $scfg, $volname) = @_;
421 # this only works for file based storage types
422 die "storage definintion has no path\n" if !$scfg->{path
};
424 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
425 $class->parse_volname($volname);
427 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
429 die "create_base not possible with base image\n" if $isBase;
431 my $path = $class->path($scfg, $volname);
433 my ($size, $format, $used, $parent) = file_size_info
($path);
434 die "file_size_info on '$volname' failed\n" if !($format && $size);
436 die "volname '$volname' contains wrong information about parent\n"
437 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
440 $newname =~ s/^vm-/base-/;
442 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
445 my $newpath = $class->path($scfg, $newvolname);
447 die "file '$newpath' already exists\n" if -f
$newpath;
449 rename($path, $newpath) ||
450 die "rename '$path' to '$newpath' failed - $!\n";
452 # We try to protect base volume
454 chmod(0444, $newpath); # nobody should write anything
456 # also try to set immutable flag
457 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
463 my $find_free_diskname = sub {
464 my ($imgdir, $vmid, $fmt) = @_;
467 PVE
::Tools
::dir_glob_foreach
($imgdir,
468 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
470 my ($fn, $type, $disk) = @_;
471 $disk_ids->{$disk} = 1;
474 for (my $i = 1; $i < 100; $i++) {
475 if (!$disk_ids->{$i}) {
476 return "vm-$vmid-disk-$i.$fmt";
480 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
484 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
486 # this only works for file based storage types
487 die "storage definintion has no path\n" if !$scfg->{path
};
489 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
490 $class->parse_volname($volname);
492 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
494 die "clone_image onyl works on base images\n" if !$isBase;
496 my $imagedir = $class->get_subdir($scfg, 'images');
497 $imagedir .= "/$vmid";
501 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
503 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
505 my $newvol = "$basevmid/$basename/$vmid/$name";
507 my $path = $class->path($scfg, $newvol);
509 # Note: we use relative paths, so we need to call chdir before qemu-img
511 local $CWD = $imagedir;
513 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
514 '-f', 'qcow2', $path];
526 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
528 my $imagedir = $class->get_subdir($scfg, 'images');
529 $imagedir .= "/$vmid";
533 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
535 my (undef, $tmpfmt) = parse_name_dir
($name);
537 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
540 my $path = "$imagedir/$name";
542 die "disk image '$path' already exists\n" if -e
$path;
544 my $cmd = ['/usr/bin/qemu-img', 'create'];
546 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
548 push @$cmd, '-f', $fmt, $path, "${size}K";
550 run_command
($cmd, errmsg
=> "unable to create image");
552 return "$vmid/$name";
556 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
558 my $path = $class->path($scfg, $volname);
561 warn "disk image '$path' does not exists\n";
566 # try to remove immutable flag
567 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
571 unlink($path) || die "unlink '$path' failed - $!\n";
577 my ($filename, $timeout) = @_;
579 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
587 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
589 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
591 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
593 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
595 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
599 $used *= 1024 if $u eq 'K';
600 $used *= (1024*1024) if $u eq 'M';
601 $used *= (1024*1024*1024) if $u eq 'G';
602 $used *= (1024*1024*1024*1024) if $u eq 'T';
609 return wantarray ?
($size, $format, $used, $parent) : $size;
612 sub volume_size_info
{
613 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
614 my $path = $class->path($scfg, $volname);
615 return file_size_info
($path, $timeout);
620 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
622 die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/;
624 return 1 if $running;
626 my $path = $class->path($scfg, $volname);
628 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
630 run_command
($cmd, timeout
=> 10);
635 sub volume_snapshot
{
636 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
638 die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
640 return 1 if $running;
642 my $path = $class->path($scfg, $volname);
644 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
651 sub volume_snapshot_rollback
{
652 my ($class, $scfg, $storeid, $volname, $snap) = @_;
654 die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
656 my $path = $class->path($scfg, $volname);
658 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
665 sub volume_snapshot_delete
{
666 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
668 die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/;
670 return 1 if $running;
672 my $path = $class->path($scfg, $volname);
674 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
681 sub volume_has_feature
{
682 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
685 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
686 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
687 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
688 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
689 snap
=> {qcow2
=> 1} },
692 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
693 $class->parse_volname($volname);
695 my (undef, $format) = parse_name_dir
($name);
701 $key = $isBase ?
'base' : 'current';
704 return 1 if defined($features->{$feature}->{$key}->{$format});
710 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
712 my $imagedir = $class->get_subdir($scfg, 'images');
714 my ($defFmt, $vaidFmts) = default_format
($scfg);
715 my $fmts = join ('|', @$vaidFmts);
719 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
721 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
727 next if !$vollist && defined($vmid) && ($owner ne $vmid);
729 my ($size, $format, $used, $parent) = file_size_info
($fn);
730 next if !($format && $size);
733 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
734 my ($basevmid, $basename) = ($1, $2);
735 $volid = "$storeid:$basevmid/$basename/$owner/$name";
737 $volid = "$storeid:$owner/$name";
741 my $found = grep { $_ eq $volid } @$vollist;
746 volid
=> $volid, format
=> $format,
747 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
755 my ($class, $storeid, $scfg, $cache) = @_;
757 my $path = $scfg->{path
};
759 die "storage definintion has no path\n" if !$path;
762 my $res = PVE
::Tools
::df
($path, $timeout);
764 return undef if !$res || !$res->{total
};
766 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
769 sub activate_storage
{
770 my ($class, $storeid, $scfg, $cache) = @_;
772 my $path = $scfg->{path
};
774 die "storage definintion has no path\n" if !$path;
776 die "unable to activate storage '$storeid' - " .
777 "directory '$path' does not exist\n" if ! -d
$path;
779 if (defined($scfg->{content
})) {
780 foreach my $vtype (keys %$vtype_subdirs) {
781 # OpenVZMigrate uses backup (dump) dir
782 if (defined($scfg->{content
}->{$vtype}) ||
783 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
784 my $subdir = $class->get_subdir($scfg, $vtype);
785 mkpath
$subdir if $subdir ne $path;
791 sub deactivate_storage
{
792 my ($class, $storeid, $scfg, $cache) = @_;
794 # do nothing by default
797 sub activate_volume
{
798 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
800 my $path = $class->path($scfg, $volname);
802 # check is volume exists
804 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
806 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
810 sub deactivate_volume
{
811 my ($class, $storeid, $scfg, $volname, $cache) = @_;
813 # do nothing by default
816 sub check_connection
{
817 my ($class, $storeid, $scfg) = @_;
818 # do nothing by default