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(@_); });
22 type
=> { description
=> "Storage type." },
23 storage
=> get_standard_option
('pve-storage-id',
24 { completion
=> \
&PVE
::Storage
::complete_storage
}),
25 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
27 description
=> "Allowed content types. Note: value 'rootdir' is used for Containers, and value 'images' for KVM-Qemu VM's.\n",
28 type
=> 'string', format
=> 'pve-storage-content-list',
30 completion
=> \
&PVE
::Storage
::complete_content_type
,
33 description
=> "Flag to disable the storage.",
38 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
44 description
=> "Mark storage as shared.",
49 description
=> "Default Image format.",
50 type
=> 'string', format
=> 'pve-storage-format',
56 sub content_hash_to_string
{
60 foreach my $ct (keys %$hash) {
61 push @cta, $ct if $hash->{$ct};
64 return join(',', @cta);
67 sub valid_content_types
{
70 my $def = $defaultData->{plugindata
}->{$type};
74 return $def->{content
}->[0];
80 my $type = $scfg->{type
};
81 my $def = $defaultData->{plugindata
}->{$type};
83 my $def_format = 'raw';
84 my $valid_formats = [ $def_format ];
86 if (defined($def->{format
})) {
87 $def_format = $scfg->{format
} || $def->{format
}->[1];
88 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
91 return wantarray ?
($def_format, $valid_formats) : $def_format;
94 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
96 my ($path, $noerr) = @_;
98 # fixme: exclude more shell meta characters?
99 # we need absolute paths
100 if ($path !~ m
|^/[^;\
(\
)]+|) {
101 return undef if $noerr;
102 die "value does not look like a valid absolute path\n";
107 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
109 my ($server, $noerr) = @_;
111 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
112 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
114 return undef if $noerr;
115 die "value does not look like a valid server name or IP address\n";
120 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
122 my ($name, $noerr) = @_;
124 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
125 return undef if $noerr;
126 die "lvm name '$name' contains illegal characters\n";
132 # fixme: do we need this
133 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
135 # my ($portal, $noerr) = @_;
137 # # IP with optional port
138 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
139 # return undef if $noerr;
140 # die "value does not look like a valid portal address\n";
145 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
146 sub verify_portal_dns
{
147 my ($portal, $noerr) = @_;
149 # IP or DNS name with optional port
150 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
151 return undef if $noerr;
152 die "value does not look like a valid portal address\n";
157 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
159 my ($ct, $noerr) = @_;
161 my $valid_content = valid_content_types
('dir'); # dir includes all types
163 if (!$valid_content->{$ct}) {
164 return undef if $noerr;
165 die "invalid content type '$ct'\n";
171 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
173 my ($fmt, $noerr) = @_;
175 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
176 return undef if $noerr;
177 die "invalid format '$fmt'\n";
183 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
185 my ($value, $noerr) = @_;
187 # mount options (see man fstab)
188 if ($value !~ m/^\S+$/) {
189 return undef if $noerr;
190 die "invalid options '$value'\n";
196 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
197 sub parse_volume_id
{
198 my ($volid, $noerr) = @_;
200 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
201 return wantarray ?
($1, $2) : $1;
203 return undef if $noerr;
204 die "unable to parse volume ID '$volid'\n";
212 sub parse_section_header
{
213 my ($class, $line) = @_;
215 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
216 my ($type, $storeid) = (lc($1), $2);
217 my $errmsg = undef; # set if you want to skip whole section
218 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
220 my $config = {}; # to return additional attributes
221 return ($type, $storeid, $errmsg, $config);
227 my ($class, $type, $key, $value) = @_;
229 my $def = $defaultData->{plugindata
}->{$type};
231 if ($key eq 'content') {
232 my $valid_content = $def->{content
}->[0];
236 foreach my $c (PVE
::Tools
::split_list
($value)) {
237 if (!$valid_content->{$c}) {
238 die "storage does not support content type '$c'\n";
243 if ($res->{none
} && scalar (keys %$res) > 1) {
244 die "unable to combine 'none' with other content types\n";
248 } elsif ($key eq 'format') {
249 my $valid_formats = $def->{format
}->[0];
251 if (!$valid_formats->{$value}) {
252 die "storage does not support format '$value'\n";
256 } elsif ($key eq 'nodes') {
259 foreach my $node (PVE
::Tools
::split_list
($value)) {
260 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
266 # no node restrictions for local storage
267 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
268 # die "storage '$storeid' does not allow node restrictions\n";
278 my ($class, $type, $key, $value) = @_;
280 if ($key eq 'nodes') {
281 return join(',', keys(%$value));
282 } elsif ($key eq 'content') {
283 my $res = content_hash_to_string
($value) || 'none';
291 my ($class, $filename, $raw) = @_;
293 my $cfg = $class->SUPER::parse_config
($filename, $raw);
294 my $ids = $cfg->{ids
};
296 # make sure we have a reasonable 'local:' storage
297 # we want 'local' to be always the same 'type' (on all cluster nodes)
298 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
299 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
302 priority
=> 0, # force first entry
303 path
=> '/var/lib/vz',
305 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
309 # make sure we have a path
310 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
312 # remove node restrictions for local storage
313 delete($ids->{local}->{nodes
});
315 foreach my $storeid (keys %$ids) {
316 my $d = $ids->{$storeid};
317 my $type = $d->{type
};
319 my $def = $defaultData->{plugindata
}->{$type};
321 if ($def->{content
}) {
322 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
325 if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'glusterfs' || $type eq 'zfs' || $type eq 'drbd') {
333 # Storage implementation
335 sub cluster_lock_storage
{
336 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
340 my $lockid = "pve-storage-$storeid";
341 my $lockdir = "/var/lock/pve-manager";
343 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
346 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
355 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
356 return ($1, $3, $2); # (name, format, isBase)
359 die "unable to parse volume filename '$name'\n";
363 my ($class, $volname) = @_;
365 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
366 my ($basedvmid, $basename) = ($1, $2);
367 parse_name_dir
($basename);
368 my ($vmid, $name) = ($3, $4);
369 my (undef, $format, $isBase) = parse_name_dir
($name);
370 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
371 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
372 my ($vmid, $name) = ($1, $2);
373 my (undef, $format, $isBase) = parse_name_dir
($name);
374 return ('images', $name, $vmid, undef, undef, $isBase, $format);
375 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
377 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
378 return ('vztmpl', $1);
379 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
380 return ('rootdir', $1, $1);
381 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
383 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
384 return ('backup', $fn, $2);
386 return ('backup', $fn);
389 die "unable to parse directory volume name '$volname'\n";
392 my $vtype_subdirs = {
394 rootdir
=> 'private',
395 iso
=> 'template/iso',
396 vztmpl
=> 'template/cache',
401 my ($class, $scfg, $vtype) = @_;
403 my $path = $scfg->{path
};
405 die "storage definintion has no path\n" if !$path;
407 my $subdir = $vtype_subdirs->{$vtype};
409 die "unknown vtype '$vtype'\n" if !defined($subdir);
411 return "$path/$subdir";
414 sub filesystem_path
{
415 my ($class, $scfg, $volname, $snapname) = @_;
417 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
418 $class->parse_volname($volname);
420 # Note: qcow2/qed has internal snapshot, so path is always
421 # the same (with or without snapshot => same file).
422 die "can't snapshot this image format\n"
423 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
425 my $dir = $class->get_subdir($scfg, $vtype);
427 $dir .= "/$vmid" if $vtype eq 'images';
429 my $path = "$dir/$name";
431 return wantarray ?
($path, $vmid, $vtype) : $path;
435 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
437 return $class->filesystem_path($scfg, $volname, $snapname);
441 my ($class, $storeid, $scfg, $volname) = @_;
443 # this only works for file based storage types
444 die "storage definintion has no path\n" if !$scfg->{path
};
446 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
447 $class->parse_volname($volname);
449 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
451 die "create_base not possible with base image\n" if $isBase;
453 my $path = $class->filesystem_path($scfg, $volname);
455 my ($size, undef, $used, $parent) = file_size_info
($path);
456 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
458 die "volname '$volname' contains wrong information about parent\n"
459 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
462 $newname =~ s/^vm-/base-/;
464 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
467 my $newpath = $class->filesystem_path($scfg, $newvolname);
469 die "file '$newpath' already exists\n" if -f
$newpath;
471 rename($path, $newpath) ||
472 die "rename '$path' to '$newpath' failed - $!\n";
474 # We try to protect base volume
476 chmod(0444, $newpath); # nobody should write anything
478 # also try to set immutable flag
479 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
485 my $find_free_diskname = sub {
486 my ($imgdir, $vmid, $fmt) = @_;
489 PVE
::Tools
::dir_glob_foreach
($imgdir,
490 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
492 my ($fn, $type, $disk) = @_;
493 $disk_ids->{$disk} = 1;
496 for (my $i = 1; $i < 100; $i++) {
497 if (!$disk_ids->{$i}) {
498 return "vm-$vmid-disk-$i.$fmt";
502 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
506 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
508 # this only works for file based storage types
509 die "storage definintion has no path\n" if !$scfg->{path
};
511 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
512 $class->parse_volname($volname);
514 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
516 die "this storage type does not support clone_image on snapshot\n" if $snap;
518 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
520 die "clone_image only works on base images\n" if !$isBase;
522 my $imagedir = $class->get_subdir($scfg, 'images');
523 $imagedir .= "/$vmid";
527 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
529 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
531 my $newvol = "$basevmid/$basename/$vmid/$name";
533 my $path = $class->filesystem_path($scfg, $newvol);
535 # Note: we use relative paths, so we need to call chdir before qemu-img
537 local $CWD = $imagedir;
539 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
540 '-f', 'qcow2', $path];
552 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
554 my $imagedir = $class->get_subdir($scfg, 'images');
555 $imagedir .= "/$vmid";
559 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
561 my (undef, $tmpfmt) = parse_name_dir
($name);
563 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
566 my $path = "$imagedir/$name";
568 die "disk image '$path' already exists\n" if -e
$path;
570 if ($fmt eq 'subvol') {
571 # only allow this if size = 0, so that user knows what he is doing
572 die "storage does not support subvol quotas\n" if $size != 0;
574 my $old_umask = umask(0022);
576 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
580 my $cmd = ['/usr/bin/qemu-img', 'create'];
582 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
584 push @$cmd, '-f', $fmt, $path, "${size}K";
586 run_command
($cmd, errmsg
=> "unable to create image");
589 return "$vmid/$name";
593 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
595 my $path = $class->filesystem_path($scfg, $volname);
597 if ($format eq 'subvol') {
598 File
::Path
::remove_tree
($path);
602 warn "disk image '$path' does not exists\n";
607 # try to remove immutable flag
608 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
612 unlink($path) || die "unlink '$path' failed - $!\n";
619 my ($filename, $timeout) = @_;
622 return wantarray ?
(0, 'subvol', 0, undef) : 1;
625 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
633 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
635 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
637 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
639 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
641 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
645 $used *= 1024 if $u eq 'K';
646 $used *= (1024*1024) if $u eq 'M';
647 $used *= (1024*1024*1024) if $u eq 'G';
648 $used *= (1024*1024*1024*1024) if $u eq 'T';
655 return wantarray ?
($size, $format, $used, $parent) : $size;
658 sub volume_size_info
{
659 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
660 my $path = $class->filesystem_path($scfg, $volname);
661 return file_size_info
($path, $timeout);
666 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
668 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
670 return 1 if $running;
672 my $path = $class->filesystem_path($scfg, $volname);
674 my $format = ($class->parse_volname($volname))[6];
676 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
678 run_command
($cmd, timeout
=> 10);
683 sub volume_snapshot
{
684 my ($class, $scfg, $storeid, $volname, $snap) = @_;
686 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
688 my $path = $class->filesystem_path($scfg, $volname);
690 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
697 sub volume_rollback_is_possible
{
698 my ($class, $scfg, $storeid, $volname, $snap) = @_;
703 sub volume_snapshot_rollback
{
704 my ($class, $scfg, $storeid, $volname, $snap) = @_;
706 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
708 my $path = $class->filesystem_path($scfg, $volname);
710 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
717 sub volume_snapshot_delete
{
718 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
720 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
722 return 1 if $running;
724 my $path = $class->filesystem_path($scfg, $volname);
726 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
728 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
735 sub volume_has_feature
{
736 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
739 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
740 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
741 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
742 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
743 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
744 snap
=> {qcow2
=> 1} },
745 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
746 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
749 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
750 $class->parse_volname($volname);
756 $key = $isBase ?
'base' : 'current';
759 return 1 if defined($features->{$feature}->{$key}->{$format});
765 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
767 my $imagedir = $class->get_subdir($scfg, 'images');
769 my ($defFmt, $vaidFmts) = default_format
($scfg);
770 my $fmts = join ('|', @$vaidFmts);
774 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
776 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
782 next if !$vollist && defined($vmid) && ($owner ne $vmid);
784 my ($size, $format, $used, $parent) = file_size_info
($fn);
785 next if !($format && defined($size));
788 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
789 my ($basevmid, $basename) = ($1, $2);
790 $volid = "$storeid:$basevmid/$basename/$owner/$name";
792 $volid = "$storeid:$owner/$name";
796 my $found = grep { $_ eq $volid } @$vollist;
801 volid
=> $volid, format
=> $format,
802 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
810 my ($class, $storeid, $scfg, $cache) = @_;
812 my $path = $scfg->{path
};
814 die "storage definintion has no path\n" if !$path;
817 my $res = PVE
::Tools
::df
($path, $timeout);
819 return undef if !$res || !$res->{total
};
821 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
824 sub activate_storage
{
825 my ($class, $storeid, $scfg, $cache) = @_;
827 my $path = $scfg->{path
};
829 die "storage definintion has no path\n" if !$path;
831 die "unable to activate storage '$storeid' - " .
832 "directory '$path' does not exist\n" if ! -d
$path;
834 if (defined($scfg->{content
})) {
835 foreach my $vtype (keys %$vtype_subdirs) {
836 # OpenVZMigrate uses backup (dump) dir
837 if (defined($scfg->{content
}->{$vtype}) ||
838 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
839 my $subdir = $class->get_subdir($scfg, $vtype);
840 mkpath
$subdir if $subdir ne $path;
846 sub deactivate_storage
{
847 my ($class, $storeid, $scfg, $cache) = @_;
849 # do nothing by default
852 sub activate_volume
{
853 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
855 my $path = $class->filesystem_path($scfg, $volname, $snapname);
857 # check is volume exists
859 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
861 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
865 sub deactivate_volume
{
866 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
868 # do nothing by default
871 sub check_connection
{
872 my ($class, $storeid, $scfg) = @_;
873 # do nothing by default