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',
32 description
=> "Flag to disable the storage.",
37 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
43 description
=> "Mark storage as shared.",
48 description
=> "Default Image format.",
49 type
=> 'string', format
=> 'pve-storage-format',
55 sub content_hash_to_string
{
59 foreach my $ct (keys %$hash) {
60 push @cta, $ct if $hash->{$ct};
63 return join(',', @cta);
66 sub valid_content_types
{
69 my $def = $defaultData->{plugindata
}->{$type};
73 return $def->{content
}->[0];
79 my $type = $scfg->{type
};
80 my $def = $defaultData->{plugindata
}->{$type};
82 my $def_format = 'raw';
83 my $valid_formats = [ $def_format ];
85 if (defined($def->{format
})) {
86 $def_format = $scfg->{format
} || $def->{format
}->[1];
87 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
90 return wantarray ?
($def_format, $valid_formats) : $def_format;
93 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
95 my ($path, $noerr) = @_;
97 # fixme: exclude more shell meta characters?
98 # we need absolute paths
99 if ($path !~ m
|^/[^;\
(\
)]+|) {
100 return undef if $noerr;
101 die "value does not look like a valid absolute path\n";
106 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
108 my ($server, $noerr) = @_;
110 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
111 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
113 return undef if $noerr;
114 die "value does not look like a valid server name or IP address\n";
119 # fixme: do we need this
120 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
122 # my ($portal, $noerr) = @_;
124 # # IP with optional port
125 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
126 # return undef if $noerr;
127 # die "value does not look like a valid portal address\n";
132 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
133 sub verify_portal_dns
{
134 my ($portal, $noerr) = @_;
136 # IP or DNS name with optional port
137 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
138 return undef if $noerr;
139 die "value does not look like a valid portal address\n";
144 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
146 my ($ct, $noerr) = @_;
148 my $valid_content = valid_content_types
('dir'); # dir includes all types
150 if (!$valid_content->{$ct}) {
151 return undef if $noerr;
152 die "invalid content type '$ct'\n";
158 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
160 my ($fmt, $noerr) = @_;
162 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
163 return undef if $noerr;
164 die "invalid format '$fmt'\n";
170 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
172 my ($value, $noerr) = @_;
174 # mount options (see man fstab)
175 if ($value !~ m/^\S+$/) {
176 return undef if $noerr;
177 die "invalid options '$value'\n";
183 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
184 sub parse_volume_id
{
185 my ($volid, $noerr) = @_;
187 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
188 return wantarray ?
($1, $2) : $1;
190 return undef if $noerr;
191 die "unable to parse volume ID '$volid'\n";
199 sub parse_section_header
{
200 my ($class, $line) = @_;
202 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
203 my ($type, $storeid) = (lc($1), $2);
204 my $errmsg = undef; # set if you want to skip whole section
205 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
207 my $config = {}; # to return additional attributes
208 return ($type, $storeid, $errmsg, $config);
214 my ($class, $type, $key, $value) = @_;
216 my $def = $defaultData->{plugindata
}->{$type};
218 if ($key eq 'content') {
219 my $valid_content = $def->{content
}->[0];
223 foreach my $c (PVE
::Tools
::split_list
($value)) {
224 if (!$valid_content->{$c}) {
225 die "storage does not support content type '$c'\n";
230 if ($res->{none
} && scalar (keys %$res) > 1) {
231 die "unable to combine 'none' with other content types\n";
235 } elsif ($key eq 'format') {
236 my $valid_formats = $def->{format
}->[0];
238 if (!$valid_formats->{$value}) {
239 die "storage does not support format '$value'\n";
243 } elsif ($key eq 'nodes') {
246 foreach my $node (PVE
::Tools
::split_list
($value)) {
247 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
253 # no node restrictions for local storage
254 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
255 # die "storage '$storeid' does not allow node restrictions\n";
265 my ($class, $type, $key, $value) = @_;
267 if ($key eq 'nodes') {
268 return join(',', keys(%$value));
269 } elsif ($key eq 'content') {
270 my $res = content_hash_to_string
($value) || 'none';
278 my ($class, $filename, $raw) = @_;
280 my $cfg = $class->SUPER::parse_config
($filename, $raw);
281 my $ids = $cfg->{ids
};
283 # make sure we have a reasonable 'local:' storage
284 # openvz expects things to be there
285 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
286 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
289 priority
=> 0, # force first entry
290 path
=> '/var/lib/vz',
292 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
296 # we always need this for OpenVZ
297 $ids->{local}->{content
}->{rootdir
} = 1;
298 $ids->{local}->{content
}->{vztmpl
} = 1;
299 delete ($ids->{local}->{disable
});
301 # make sure we have a path
302 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
304 # remove node restrictions for local storage
305 delete($ids->{local}->{nodes
});
307 foreach my $storeid (keys %$ids) {
308 my $d = $ids->{$storeid};
309 my $type = $d->{type
};
311 my $def = $defaultData->{plugindata
}->{$type};
313 if ($def->{content
}) {
314 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
317 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') {
325 # Storage implementation
327 sub cluster_lock_storage
{
328 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
332 my $lockid = "pve-storage-$storeid";
333 my $lockdir = "/var/lock/pve-manager";
335 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
338 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
347 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
348 return ($1, $3, $2); # (name, format, isBase)
351 die "unable to parse volume filename '$name'\n";
355 my ($class, $volname) = @_;
357 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
358 my ($basedvmid, $basename) = ($1, $2);
359 parse_name_dir
($basename);
360 my ($vmid, $name) = ($3, $4);
361 my (undef, $format, $isBase) = parse_name_dir
($name);
362 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
363 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
364 my ($vmid, $name) = ($1, $2);
365 my (undef, $format, $isBase) = parse_name_dir
($name);
366 return ('images', $name, $vmid, undef, undef, $isBase, $format);
367 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
369 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
370 return ('vztmpl', $1);
371 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
372 return ('rootdir', $1, $1);
373 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
375 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
376 return ('backup', $fn, $2);
378 return ('backup', $fn);
381 die "unable to parse directory volume name '$volname'\n";
384 my $vtype_subdirs = {
386 rootdir
=> 'private',
387 iso
=> 'template/iso',
388 vztmpl
=> 'template/cache',
393 my ($class, $scfg, $vtype) = @_;
395 my $path = $scfg->{path
};
397 die "storage definintion has no path\n" if !$path;
399 my $subdir = $vtype_subdirs->{$vtype};
401 die "unknown vtype '$vtype'\n" if !defined($subdir);
403 return "$path/$subdir";
406 sub filesystem_path
{
407 my ($class, $scfg, $volname, $snapname) = @_;
409 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
410 $class->parse_volname($volname);
412 # Note: qcow2/qed has internal snapshot, so path is always
413 # the same (with or without snapshot => same file).
414 die "can't snapshot this image format\n"
415 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
417 my $dir = $class->get_subdir($scfg, $vtype);
419 $dir .= "/$vmid" if $vtype eq 'images';
421 my $path = "$dir/$name";
423 return wantarray ?
($path, $vmid, $vtype) : $path;
427 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
429 return $class->filesystem_path($scfg, $volname, $snapname);
433 my ($class, $storeid, $scfg, $volname) = @_;
435 # this only works for file based storage types
436 die "storage definintion has no path\n" if !$scfg->{path
};
438 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
439 $class->parse_volname($volname);
441 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
443 die "create_base not possible with base image\n" if $isBase;
445 my $path = $class->filesystem_path($scfg, $volname);
447 my ($size, undef, $used, $parent) = file_size_info
($path);
448 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
450 die "volname '$volname' contains wrong information about parent\n"
451 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
454 $newname =~ s/^vm-/base-/;
456 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
459 my $newpath = $class->filesystem_path($scfg, $newvolname);
461 die "file '$newpath' already exists\n" if -f
$newpath;
463 rename($path, $newpath) ||
464 die "rename '$path' to '$newpath' failed - $!\n";
466 # We try to protect base volume
468 chmod(0444, $newpath); # nobody should write anything
470 # also try to set immutable flag
471 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
477 my $find_free_diskname = sub {
478 my ($imgdir, $vmid, $fmt) = @_;
481 PVE
::Tools
::dir_glob_foreach
($imgdir,
482 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
484 my ($fn, $type, $disk) = @_;
485 $disk_ids->{$disk} = 1;
488 for (my $i = 1; $i < 100; $i++) {
489 if (!$disk_ids->{$i}) {
490 return "vm-$vmid-disk-$i.$fmt";
494 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
498 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
500 # this only works for file based storage types
501 die "storage definintion has no path\n" if !$scfg->{path
};
503 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
504 $class->parse_volname($volname);
506 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
508 die "this storage type does not support clone_image on snapshot\n" if $snap;
510 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
512 die "clone_image only works on base images\n" if !$isBase;
514 my $imagedir = $class->get_subdir($scfg, 'images');
515 $imagedir .= "/$vmid";
519 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
521 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
523 my $newvol = "$basevmid/$basename/$vmid/$name";
525 my $path = $class->filesystem_path($scfg, $newvol);
527 # Note: we use relative paths, so we need to call chdir before qemu-img
529 local $CWD = $imagedir;
531 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
532 '-f', 'qcow2', $path];
544 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
546 my $imagedir = $class->get_subdir($scfg, 'images');
547 $imagedir .= "/$vmid";
551 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
553 my (undef, $tmpfmt) = parse_name_dir
($name);
555 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
558 my $path = "$imagedir/$name";
560 die "disk image '$path' already exists\n" if -e
$path;
562 if ($fmt eq 'subvol') {
563 # only allow this if size = 0, so that user knows what he is doing
564 die "storage does not support subvol quotas\n" if $size != 0;
566 (mkdir $path) || die "unable to create subvol '$path' - $!\n";
568 my $cmd = ['/usr/bin/qemu-img', 'create'];
570 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
572 push @$cmd, '-f', $fmt, $path, "${size}K";
574 run_command
($cmd, errmsg
=> "unable to create image");
577 return "$vmid/$name";
581 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
583 my $path = $class->filesystem_path($scfg, $volname);
585 if ($format eq 'subvol') {
586 File
::Path
::remove_tree
($path);
590 warn "disk image '$path' does not exists\n";
595 # try to remove immutable flag
596 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
600 unlink($path) || die "unlink '$path' failed - $!\n";
607 my ($filename, $timeout) = @_;
610 return wantarray ?
(0, 'subvol', 0, undef) : 1;
613 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
621 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
623 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
625 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
627 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
629 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
633 $used *= 1024 if $u eq 'K';
634 $used *= (1024*1024) if $u eq 'M';
635 $used *= (1024*1024*1024) if $u eq 'G';
636 $used *= (1024*1024*1024*1024) if $u eq 'T';
643 return wantarray ?
($size, $format, $used, $parent) : $size;
646 sub volume_size_info
{
647 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
648 my $path = $class->filesystem_path($scfg, $volname);
649 return file_size_info
($path, $timeout);
654 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
656 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
658 return 1 if $running;
660 my $path = $class->filesystem_path($scfg, $volname);
662 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
664 run_command
($cmd, timeout
=> 10);
669 sub volume_snapshot
{
670 my ($class, $scfg, $storeid, $volname, $snap) = @_;
672 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
674 my $path = $class->filesystem_path($scfg, $volname);
676 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
683 sub volume_rollback_is_possible
{
684 my ($class, $scfg, $storeid, $volname, $snap) = @_;
689 sub volume_snapshot_rollback
{
690 my ($class, $scfg, $storeid, $volname, $snap) = @_;
692 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
694 my $path = $class->filesystem_path($scfg, $volname);
696 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
703 sub volume_snapshot_delete
{
704 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
706 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
708 return 1 if $running;
710 my $path = $class->filesystem_path($scfg, $volname);
712 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
719 sub volume_has_feature
{
720 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
723 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
724 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
725 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
726 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
727 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
728 snap
=> {qcow2
=> 1} },
731 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
732 $class->parse_volname($volname);
738 $key = $isBase ?
'base' : 'current';
741 return 1 if defined($features->{$feature}->{$key}->{$format});
747 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
749 my $imagedir = $class->get_subdir($scfg, 'images');
751 my ($defFmt, $vaidFmts) = default_format
($scfg);
752 my $fmts = join ('|', @$vaidFmts);
756 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
758 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
764 next if !$vollist && defined($vmid) && ($owner ne $vmid);
766 my ($size, $format, $used, $parent) = file_size_info
($fn);
767 next if !($format && defined($size));
770 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
771 my ($basevmid, $basename) = ($1, $2);
772 $volid = "$storeid:$basevmid/$basename/$owner/$name";
774 $volid = "$storeid:$owner/$name";
778 my $found = grep { $_ eq $volid } @$vollist;
783 volid
=> $volid, format
=> $format,
784 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
792 my ($class, $storeid, $scfg, $cache) = @_;
794 my $path = $scfg->{path
};
796 die "storage definintion has no path\n" if !$path;
799 my $res = PVE
::Tools
::df
($path, $timeout);
801 return undef if !$res || !$res->{total
};
803 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
806 sub activate_storage
{
807 my ($class, $storeid, $scfg, $cache) = @_;
809 my $path = $scfg->{path
};
811 die "storage definintion has no path\n" if !$path;
813 die "unable to activate storage '$storeid' - " .
814 "directory '$path' does not exist\n" if ! -d
$path;
816 if (defined($scfg->{content
})) {
817 foreach my $vtype (keys %$vtype_subdirs) {
818 # OpenVZMigrate uses backup (dump) dir
819 if (defined($scfg->{content
}->{$vtype}) ||
820 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
821 my $subdir = $class->get_subdir($scfg, $vtype);
822 mkpath
$subdir if $subdir ne $path;
828 sub deactivate_storage
{
829 my ($class, $storeid, $scfg, $cache) = @_;
831 # do nothing by default
834 sub activate_volume
{
835 my ($class, $storeid, $scfg, $volname, $cache) = @_;
837 my $path = $class->filesystem_path($scfg, $volname);
839 # check is volume exists
841 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
843 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
847 sub deactivate_volume
{
848 my ($class, $storeid, $scfg, $volname, $cache) = @_;
850 # do nothing by default
853 sub check_connection
{
854 my ($class, $storeid, $scfg) = @_;
855 # do nothing by default