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 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
26 description
=> "Allowed content types. Note: value 'rootdir' is used for Containers, and value 'images' for KVM-Qemu VM's.\n",
27 type
=> 'string', format
=> 'pve-storage-content-list',
31 description
=> "Flag to disable the storage.",
36 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
42 description
=> "Mark storage as shared.",
47 description
=> "Default Image format.",
48 type
=> 'string', format
=> 'pve-storage-format',
54 sub content_hash_to_string
{
58 foreach my $ct (keys %$hash) {
59 push @cta, $ct if $hash->{$ct};
62 return join(',', @cta);
65 sub valid_content_types
{
68 my $def = $defaultData->{plugindata
}->{$type};
72 return $def->{content
}->[0];
78 my $type = $scfg->{type
};
79 my $def = $defaultData->{plugindata
}->{$type};
81 my $def_format = 'raw';
82 my $valid_formats = [ $def_format ];
84 if (defined($def->{format
})) {
85 $def_format = $scfg->{format
} || $def->{format
}->[1];
86 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
89 return wantarray ?
($def_format, $valid_formats) : $def_format;
92 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
94 my ($path, $noerr) = @_;
96 # fixme: exclude more shell meta characters?
97 # we need absolute paths
98 if ($path !~ m
|^/[^;\
(\
)]+|) {
99 return undef if $noerr;
100 die "value does not look like a valid absolute path\n";
105 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
107 my ($server, $noerr) = @_;
109 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
110 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
112 return undef if $noerr;
113 die "value does not look like a valid server name or IP address\n";
118 # fixme: do we need this
119 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
121 # my ($portal, $noerr) = @_;
123 # # IP with optional port
124 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
125 # return undef if $noerr;
126 # die "value does not look like a valid portal address\n";
131 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
132 sub verify_portal_dns
{
133 my ($portal, $noerr) = @_;
135 # IP or DNS name with optional port
136 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
137 return undef if $noerr;
138 die "value does not look like a valid portal address\n";
143 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
145 my ($ct, $noerr) = @_;
147 my $valid_content = valid_content_types
('dir'); # dir includes all types
149 if (!$valid_content->{$ct}) {
150 return undef if $noerr;
151 die "invalid content type '$ct'\n";
157 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
159 my ($fmt, $noerr) = @_;
161 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
162 return undef if $noerr;
163 die "invalid format '$fmt'\n";
169 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
171 my ($value, $noerr) = @_;
173 # mount options (see man fstab)
174 if ($value !~ m/^\S+$/) {
175 return undef if $noerr;
176 die "invalid options '$value'\n";
182 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
183 sub parse_volume_id
{
184 my ($volid, $noerr) = @_;
186 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
187 return wantarray ?
($1, $2) : $1;
189 return undef if $noerr;
190 die "unable to parse volume ID '$volid'\n";
198 sub parse_section_header
{
199 my ($class, $line) = @_;
201 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
202 my ($type, $storeid) = (lc($1), $2);
203 my $errmsg = undef; # set if you want to skip whole section
204 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
206 my $config = {}; # to return additional attributes
207 return ($type, $storeid, $errmsg, $config);
213 my ($class, $type, $key, $value) = @_;
215 my $def = $defaultData->{plugindata
}->{$type};
217 if ($key eq 'content') {
218 my $valid_content = $def->{content
}->[0];
222 foreach my $c (PVE
::Tools
::split_list
($value)) {
223 if (!$valid_content->{$c}) {
224 die "storage does not support content type '$c'\n";
229 if ($res->{none
} && scalar (keys %$res) > 1) {
230 die "unable to combine 'none' with other content types\n";
234 } elsif ($key eq 'format') {
235 my $valid_formats = $def->{format
}->[0];
237 if (!$valid_formats->{$value}) {
238 die "storage does not support format '$value'\n";
242 } elsif ($key eq 'nodes') {
245 foreach my $node (PVE
::Tools
::split_list
($value)) {
246 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
252 # no node restrictions for local storage
253 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
254 # die "storage '$storeid' does not allow node restrictions\n";
264 my ($class, $type, $key, $value) = @_;
266 if ($key eq 'nodes') {
267 return join(',', keys(%$value));
268 } elsif ($key eq 'content') {
269 my $res = content_hash_to_string
($value) || 'none';
277 my ($class, $filename, $raw) = @_;
279 my $cfg = $class->SUPER::parse_config
($filename, $raw);
280 my $ids = $cfg->{ids
};
282 # make sure we have a reasonable 'local:' storage
283 # openvz expects things to be there
284 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
285 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
288 priority
=> 0, # force first entry
289 path
=> '/var/lib/vz',
291 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
295 # we always need this for OpenVZ
296 $ids->{local}->{content
}->{rootdir
} = 1;
297 $ids->{local}->{content
}->{vztmpl
} = 1;
298 delete ($ids->{local}->{disable
});
300 # make sure we have a path
301 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
303 # remove node restrictions for local storage
304 delete($ids->{local}->{nodes
});
306 foreach my $storeid (keys %$ids) {
307 my $d = $ids->{$storeid};
308 my $type = $d->{type
};
310 my $def = $defaultData->{plugindata
}->{$type};
312 if ($def->{content
}) {
313 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
316 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') {
324 # Storage implementation
326 sub cluster_lock_storage
{
327 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
331 my $lockid = "pve-storage-$storeid";
332 my $lockdir = "/var/lock/pve-manager";
334 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
337 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
346 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
347 return ($1, $3, $2); # (name, format, isBase)
350 die "unable to parse volume filename '$name'\n";
354 my ($class, $volname) = @_;
356 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
357 my ($basedvmid, $basename) = ($1, $2);
358 parse_name_dir
($basename);
359 my ($vmid, $name) = ($3, $4);
360 my (undef, $format, $isBase) = parse_name_dir
($name);
361 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
362 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
363 my ($vmid, $name) = ($1, $2);
364 my (undef, $format, $isBase) = parse_name_dir
($name);
365 return ('images', $name, $vmid, undef, undef, $isBase, $format);
366 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
368 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
369 return ('vztmpl', $1);
370 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
371 return ('rootdir', $1, $1);
372 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
374 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
375 return ('backup', $fn, $2);
377 return ('backup', $fn);
380 die "unable to parse directory volume name '$volname'\n";
383 my $vtype_subdirs = {
385 rootdir
=> 'private',
386 iso
=> 'template/iso',
387 vztmpl
=> 'template/cache',
392 my ($class, $scfg, $vtype) = @_;
394 my $path = $scfg->{path
};
396 die "storage definintion has no path\n" if !$path;
398 my $subdir = $vtype_subdirs->{$vtype};
400 die "unknown vtype '$vtype'\n" if !defined($subdir);
402 return "$path/$subdir";
405 sub filesystem_path
{
406 my ($class, $scfg, $volname, $snapname) = @_;
408 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
409 $class->parse_volname($volname);
411 # Note: qcow2/qed has internal snapshot, so path is always
412 # the same (with or without snapshot => same file).
413 die "can't snapshot this image format\n"
414 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
416 my $dir = $class->get_subdir($scfg, $vtype);
418 $dir .= "/$vmid" if $vtype eq 'images';
420 my $path = "$dir/$name";
422 return wantarray ?
($path, $vmid, $vtype) : $path;
426 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
428 return $class->filesystem_path($scfg, $volname, $snapname);
432 my ($class, $storeid, $scfg, $volname) = @_;
434 # this only works for file based storage types
435 die "storage definintion has no path\n" if !$scfg->{path
};
437 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
438 $class->parse_volname($volname);
440 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
442 die "create_base not possible with base image\n" if $isBase;
444 my $path = $class->filesystem_path($scfg, $volname);
446 my ($size, undef, $used, $parent) = file_size_info
($path);
447 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
449 die "volname '$volname' contains wrong information about parent\n"
450 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
453 $newname =~ s/^vm-/base-/;
455 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
458 my $newpath = $class->filesystem_path($scfg, $newvolname);
460 die "file '$newpath' already exists\n" if -f
$newpath;
462 rename($path, $newpath) ||
463 die "rename '$path' to '$newpath' failed - $!\n";
465 # We try to protect base volume
467 chmod(0444, $newpath); # nobody should write anything
469 # also try to set immutable flag
470 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
476 my $find_free_diskname = sub {
477 my ($imgdir, $vmid, $fmt) = @_;
480 PVE
::Tools
::dir_glob_foreach
($imgdir,
481 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
483 my ($fn, $type, $disk) = @_;
484 $disk_ids->{$disk} = 1;
487 for (my $i = 1; $i < 100; $i++) {
488 if (!$disk_ids->{$i}) {
489 return "vm-$vmid-disk-$i.$fmt";
493 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
497 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
499 # this only works for file based storage types
500 die "storage definintion has no path\n" if !$scfg->{path
};
502 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
503 $class->parse_volname($volname);
505 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
507 die "this storage type does not support clone_image on snapshot\n" if $snap;
509 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
511 die "clone_image only works on base images\n" if !$isBase;
513 my $imagedir = $class->get_subdir($scfg, 'images');
514 $imagedir .= "/$vmid";
518 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
520 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
522 my $newvol = "$basevmid/$basename/$vmid/$name";
524 my $path = $class->filesystem_path($scfg, $newvol);
526 # Note: we use relative paths, so we need to call chdir before qemu-img
528 local $CWD = $imagedir;
530 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
531 '-f', 'qcow2', $path];
543 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
545 my $imagedir = $class->get_subdir($scfg, 'images');
546 $imagedir .= "/$vmid";
550 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
552 my (undef, $tmpfmt) = parse_name_dir
($name);
554 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
557 my $path = "$imagedir/$name";
559 die "disk image '$path' already exists\n" if -e
$path;
561 if ($fmt eq 'subvol') {
562 # only allow this if size = 0, so that user knows what he is doing
563 die "storage does not support subvol quotas\n" if $size != 0;
565 (mkdir $path) || die "unable to create subvol '$path' - $!\n";
567 my $cmd = ['/usr/bin/qemu-img', 'create'];
569 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
571 push @$cmd, '-f', $fmt, $path, "${size}K";
573 run_command
($cmd, errmsg
=> "unable to create image");
576 return "$vmid/$name";
580 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
582 my $path = $class->filesystem_path($scfg, $volname);
584 if ($format eq 'subvol') {
585 File
::Path
::remove_tree
($path);
589 warn "disk image '$path' does not exists\n";
594 # try to remove immutable flag
595 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
599 unlink($path) || die "unlink '$path' failed - $!\n";
606 my ($filename, $timeout) = @_;
609 return wantarray ?
(0, 'subvol', 0, undef) : 1;
612 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
620 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
622 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
624 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
626 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
628 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
632 $used *= 1024 if $u eq 'K';
633 $used *= (1024*1024) if $u eq 'M';
634 $used *= (1024*1024*1024) if $u eq 'G';
635 $used *= (1024*1024*1024*1024) if $u eq 'T';
642 return wantarray ?
($size, $format, $used, $parent) : $size;
645 sub volume_size_info
{
646 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
647 my $path = $class->filesystem_path($scfg, $volname);
648 return file_size_info
($path, $timeout);
653 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
655 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
657 return 1 if $running;
659 my $path = $class->filesystem_path($scfg, $volname);
661 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
663 run_command
($cmd, timeout
=> 10);
668 sub volume_snapshot
{
669 my ($class, $scfg, $storeid, $volname, $snap) = @_;
671 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
673 my $path = $class->filesystem_path($scfg, $volname);
675 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
682 sub volume_rollback_is_possible
{
683 my ($class, $scfg, $storeid, $volname, $snap) = @_;
688 sub volume_snapshot_rollback
{
689 my ($class, $scfg, $storeid, $volname, $snap) = @_;
691 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
693 my $path = $class->filesystem_path($scfg, $volname);
695 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
702 sub volume_snapshot_delete
{
703 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
705 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
707 return 1 if $running;
709 my $path = $class->filesystem_path($scfg, $volname);
711 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
718 sub volume_has_feature
{
719 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
722 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
723 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
724 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
725 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
726 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
727 snap
=> {qcow2
=> 1} },
730 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
731 $class->parse_volname($volname);
737 $key = $isBase ?
'base' : 'current';
740 return 1 if defined($features->{$feature}->{$key}->{$format});
746 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
748 my $imagedir = $class->get_subdir($scfg, 'images');
750 my ($defFmt, $vaidFmts) = default_format
($scfg);
751 my $fmts = join ('|', @$vaidFmts);
755 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
757 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
763 next if !$vollist && defined($vmid) && ($owner ne $vmid);
765 my ($size, $format, $used, $parent) = file_size_info
($fn);
766 next if !($format && defined($size));
769 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
770 my ($basevmid, $basename) = ($1, $2);
771 $volid = "$storeid:$basevmid/$basename/$owner/$name";
773 $volid = "$storeid:$owner/$name";
777 my $found = grep { $_ eq $volid } @$vollist;
782 volid
=> $volid, format
=> $format,
783 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
791 my ($class, $storeid, $scfg, $cache) = @_;
793 my $path = $scfg->{path
};
795 die "storage definintion has no path\n" if !$path;
798 my $res = PVE
::Tools
::df
($path, $timeout);
800 return undef if !$res || !$res->{total
};
802 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
805 sub activate_storage
{
806 my ($class, $storeid, $scfg, $cache) = @_;
808 my $path = $scfg->{path
};
810 die "storage definintion has no path\n" if !$path;
812 die "unable to activate storage '$storeid' - " .
813 "directory '$path' does not exist\n" if ! -d
$path;
815 if (defined($scfg->{content
})) {
816 foreach my $vtype (keys %$vtype_subdirs) {
817 # OpenVZMigrate uses backup (dump) dir
818 if (defined($scfg->{content
}->{$vtype}) ||
819 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
820 my $subdir = $class->get_subdir($scfg, $vtype);
821 mkpath
$subdir if $subdir ne $path;
827 sub deactivate_storage
{
828 my ($class, $storeid, $scfg, $cache) = @_;
830 # do nothing by default
833 sub activate_volume
{
834 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
836 my $path = $class->filesystem_path($scfg, $volname);
838 # check is volume exists
840 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
842 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
846 sub deactivate_volume
{
847 my ($class, $storeid, $scfg, $volname, $cache) = @_;
849 # do nothing by default
852 sub check_connection
{
853 my ($class, $storeid, $scfg) = @_;
854 # do nothing by default