]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
15c23d4fe17c880c74ae07b503550dc9b084704f
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 'glusterfs' || $type eq 'zfs') {
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";
404 sub filesystem_path
{
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, $scfg, $volname, $storeid) = @_;
421 return $class->filesystem_path($scfg, $volname, $storeid);
425 my ($class, $storeid, $scfg, $volname) = @_;
427 # this only works for file based storage types
428 die "storage definintion has no path\n" if !$scfg->{path
};
430 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
431 $class->parse_volname($volname);
433 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
435 die "create_base not possible with base image\n" if $isBase;
437 my $path = $class->filesystem_path($scfg, $volname);
439 my ($size, $format, $used, $parent) = file_size_info
($path);
440 die "file_size_info on '$volname' failed\n" if !($format && $size);
442 die "volname '$volname' contains wrong information about parent\n"
443 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
446 $newname =~ s/^vm-/base-/;
448 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
451 my $newpath = $class->filesystem_path($scfg, $newvolname);
453 die "file '$newpath' already exists\n" if -f
$newpath;
455 rename($path, $newpath) ||
456 die "rename '$path' to '$newpath' failed - $!\n";
458 # We try to protect base volume
460 chmod(0444, $newpath); # nobody should write anything
462 # also try to set immutable flag
463 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
469 my $find_free_diskname = sub {
470 my ($imgdir, $vmid, $fmt) = @_;
473 PVE
::Tools
::dir_glob_foreach
($imgdir,
474 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
476 my ($fn, $type, $disk) = @_;
477 $disk_ids->{$disk} = 1;
480 for (my $i = 1; $i < 100; $i++) {
481 if (!$disk_ids->{$i}) {
482 return "vm-$vmid-disk-$i.$fmt";
486 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
490 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
492 # this only works for file based storage types
493 die "storage definintion has no path\n" if !$scfg->{path
};
495 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
496 $class->parse_volname($volname);
498 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
500 die "this storage type does not support clone_image on snapshot\n" if $snap;
502 die "clone_image only works on base images\n" if !$isBase;
504 my $imagedir = $class->get_subdir($scfg, 'images');
505 $imagedir .= "/$vmid";
509 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
511 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
513 my $newvol = "$basevmid/$basename/$vmid/$name";
515 my $path = $class->filesystem_path($scfg, $newvol);
517 # Note: we use relative paths, so we need to call chdir before qemu-img
519 local $CWD = $imagedir;
521 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
522 '-f', 'qcow2', $path];
534 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
536 my $imagedir = $class->get_subdir($scfg, 'images');
537 $imagedir .= "/$vmid";
541 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
543 my (undef, $tmpfmt) = parse_name_dir
($name);
545 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
548 my $path = "$imagedir/$name";
550 die "disk image '$path' already exists\n" if -e
$path;
552 my $cmd = ['/usr/bin/qemu-img', 'create'];
554 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
556 push @$cmd, '-f', $fmt, $path, "${size}K";
558 run_command
($cmd, errmsg
=> "unable to create image");
560 return "$vmid/$name";
564 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
566 my $path = $class->filesystem_path($scfg, $volname);
569 warn "disk image '$path' does not exists\n";
574 # try to remove immutable flag
575 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
579 unlink($path) || die "unlink '$path' failed - $!\n";
585 my ($filename, $timeout) = @_;
587 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
595 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
597 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
599 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
601 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
603 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
607 $used *= 1024 if $u eq 'K';
608 $used *= (1024*1024) if $u eq 'M';
609 $used *= (1024*1024*1024) if $u eq 'G';
610 $used *= (1024*1024*1024*1024) if $u eq 'T';
617 return wantarray ?
($size, $format, $used, $parent) : $size;
620 sub volume_size_info
{
621 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
622 my $path = $class->filesystem_path($scfg, $volname);
623 return file_size_info
($path, $timeout);
628 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
630 die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/;
632 return 1 if $running;
634 my $path = $class->filesystem_path($scfg, $volname);
636 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
638 run_command
($cmd, timeout
=> 10);
643 sub volume_snapshot
{
644 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
646 die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
648 return 1 if $running;
650 my $path = $class->filesystem_path($scfg, $volname);
652 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
659 sub volume_snapshot_rollback
{
660 my ($class, $scfg, $storeid, $volname, $snap) = @_;
662 die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
664 my $path = $class->filesystem_path($scfg, $volname);
666 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
673 sub volume_snapshot_delete
{
674 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
676 die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/;
678 return 1 if $running;
680 my $path = $class->filesystem_path($scfg, $volname);
682 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
689 sub volume_has_feature
{
690 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
693 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
694 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
695 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
696 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
697 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
698 snap
=> {qcow2
=> 1} },
701 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
702 $class->parse_volname($volname);
704 my (undef, $format) = parse_name_dir
($name);
710 $key = $isBase ?
'base' : 'current';
713 return 1 if defined($features->{$feature}->{$key}->{$format});
719 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
721 my $imagedir = $class->get_subdir($scfg, 'images');
723 my ($defFmt, $vaidFmts) = default_format
($scfg);
724 my $fmts = join ('|', @$vaidFmts);
728 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
730 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
736 next if !$vollist && defined($vmid) && ($owner ne $vmid);
738 my ($size, $format, $used, $parent) = file_size_info
($fn);
739 next if !($format && $size);
742 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
743 my ($basevmid, $basename) = ($1, $2);
744 $volid = "$storeid:$basevmid/$basename/$owner/$name";
746 $volid = "$storeid:$owner/$name";
750 my $found = grep { $_ eq $volid } @$vollist;
755 volid
=> $volid, format
=> $format,
756 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
764 my ($class, $storeid, $scfg, $cache) = @_;
766 my $path = $scfg->{path
};
768 die "storage definintion has no path\n" if !$path;
771 my $res = PVE
::Tools
::df
($path, $timeout);
773 return undef if !$res || !$res->{total
};
775 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
778 sub activate_storage
{
779 my ($class, $storeid, $scfg, $cache) = @_;
781 my $path = $scfg->{path
};
783 die "storage definintion has no path\n" if !$path;
785 die "unable to activate storage '$storeid' - " .
786 "directory '$path' does not exist\n" if ! -d
$path;
788 if (defined($scfg->{content
})) {
789 foreach my $vtype (keys %$vtype_subdirs) {
790 # OpenVZMigrate uses backup (dump) dir
791 if (defined($scfg->{content
}->{$vtype}) ||
792 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
793 my $subdir = $class->get_subdir($scfg, $vtype);
794 mkpath
$subdir if $subdir ne $path;
800 sub deactivate_storage
{
801 my ($class, $storeid, $scfg, $cache) = @_;
803 # do nothing by default
806 sub activate_volume
{
807 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
809 my $path = $class->filesystem_path($scfg, $volname);
811 # check is volume exists
813 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
815 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
819 sub deactivate_volume
{
820 my ($class, $storeid, $scfg, $volname, $cache) = @_;
822 # do nothing by default
825 sub check_connection
{
826 my ($class, $storeid, $scfg) = @_;
827 # do nothing by default