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(@_); });
19 # fixme: remove rootdir code (we now use subvols)
23 type
=> { description
=> "Storage type." },
24 storage
=> get_standard_option
('pve-storage-id'),
25 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
27 description
=> "Allowed content types.",
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 ($portal !~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[[:alnum:]\-\.]+)(:\d+)?$/) {
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, $storeid) = @_;
409 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
411 my $dir = $class->get_subdir($scfg, $vtype);
413 $dir .= "/$vmid" if $vtype eq 'images';
415 my $path = "$dir/$name";
417 return wantarray ?
($path, $vmid, $vtype) : $path;
421 my ($class, $scfg, $volname, $storeid) = @_;
423 return $class->filesystem_path($scfg, $volname, $storeid);
427 my ($class, $storeid, $scfg, $volname) = @_;
429 # this only works for file based storage types
430 die "storage definintion has no path\n" if !$scfg->{path
};
432 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
433 $class->parse_volname($volname);
435 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
437 die "create_base not possible with base image\n" if $isBase;
439 my $path = $class->filesystem_path($scfg, $volname);
441 my ($size, undef, $used, $parent) = file_size_info
($path);
442 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
444 die "volname '$volname' contains wrong information about parent\n"
445 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
448 $newname =~ s/^vm-/base-/;
450 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
453 my $newpath = $class->filesystem_path($scfg, $newvolname);
455 die "file '$newpath' already exists\n" if -f
$newpath;
457 rename($path, $newpath) ||
458 die "rename '$path' to '$newpath' failed - $!\n";
460 # We try to protect base volume
462 chmod(0444, $newpath); # nobody should write anything
464 # also try to set immutable flag
465 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
471 my $find_free_diskname = sub {
472 my ($imgdir, $vmid, $fmt) = @_;
475 PVE
::Tools
::dir_glob_foreach
($imgdir,
476 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
478 my ($fn, $type, $disk) = @_;
479 $disk_ids->{$disk} = 1;
482 for (my $i = 1; $i < 100; $i++) {
483 if (!$disk_ids->{$i}) {
484 return "vm-$vmid-disk-$i.$fmt";
488 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
492 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
494 # this only works for file based storage types
495 die "storage definintion has no path\n" if !$scfg->{path
};
497 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
498 $class->parse_volname($volname);
500 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
502 die "this storage type does not support clone_image on snapshot\n" if $snap;
504 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
506 die "clone_image only works on base images\n" if !$isBase;
508 my $imagedir = $class->get_subdir($scfg, 'images');
509 $imagedir .= "/$vmid";
513 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
515 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
517 my $newvol = "$basevmid/$basename/$vmid/$name";
519 my $path = $class->filesystem_path($scfg, $newvol);
521 # Note: we use relative paths, so we need to call chdir before qemu-img
523 local $CWD = $imagedir;
525 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
526 '-f', 'qcow2', $path];
538 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
540 my $imagedir = $class->get_subdir($scfg, 'images');
541 $imagedir .= "/$vmid";
545 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
547 my (undef, $tmpfmt) = parse_name_dir
($name);
549 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
552 my $path = "$imagedir/$name";
554 die "disk image '$path' already exists\n" if -e
$path;
556 if ($fmt eq 'subvol') {
557 # only allow this if size = 0, so that user knows what he is doing
558 die "storage does not support subvol quotas\n" if $size != 0;
560 (mkdir $path) || die "unable to create subvol '$path' - $!\n";
562 my $cmd = ['/usr/bin/qemu-img', 'create'];
564 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
566 push @$cmd, '-f', $fmt, $path, "${size}K";
568 run_command
($cmd, errmsg
=> "unable to create image");
571 return "$vmid/$name";
575 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
577 my $path = $class->filesystem_path($scfg, $volname);
579 if ($format eq 'subvol') {
580 File
::Path
::remove_tree
($path);
584 warn "disk image '$path' does not exists\n";
589 # try to remove immutable flag
590 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
594 unlink($path) || die "unlink '$path' failed - $!\n";
601 my ($filename, $timeout) = @_;
604 return wantarray ?
(0, 'subvol', 0, undef) : 1;
607 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
615 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
617 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
619 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
621 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
623 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
627 $used *= 1024 if $u eq 'K';
628 $used *= (1024*1024) if $u eq 'M';
629 $used *= (1024*1024*1024) if $u eq 'G';
630 $used *= (1024*1024*1024*1024) if $u eq 'T';
637 return wantarray ?
($size, $format, $used, $parent) : $size;
640 sub volume_size_info
{
641 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
642 my $path = $class->filesystem_path($scfg, $volname);
643 return file_size_info
($path, $timeout);
648 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
650 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
652 return 1 if $running;
654 my $path = $class->filesystem_path($scfg, $volname);
656 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
658 run_command
($cmd, timeout
=> 10);
663 sub volume_snapshot
{
664 my ($class, $scfg, $storeid, $volname, $snap) = @_;
666 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
668 my $path = $class->filesystem_path($scfg, $volname);
670 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
677 sub volume_rollback_is_possible
{
678 my ($class, $scfg, $storeid, $volname, $snap) = @_;
683 sub volume_snapshot_rollback
{
684 my ($class, $scfg, $storeid, $volname, $snap) = @_;
686 die "can't rollback 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','-a', $snap, $path];
697 sub volume_snapshot_delete
{
698 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
700 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
702 return 1 if $running;
704 my $path = $class->filesystem_path($scfg, $volname);
706 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
713 sub volume_has_feature
{
714 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
717 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
718 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
719 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
720 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
721 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
722 snap
=> {qcow2
=> 1} },
725 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
726 $class->parse_volname($volname);
732 $key = $isBase ?
'base' : 'current';
735 return 1 if defined($features->{$feature}->{$key}->{$format});
741 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
743 my $imagedir = $class->get_subdir($scfg, 'images');
745 my ($defFmt, $vaidFmts) = default_format
($scfg);
746 my $fmts = join ('|', @$vaidFmts);
750 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
752 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
758 next if !$vollist && defined($vmid) && ($owner ne $vmid);
760 my ($size, $format, $used, $parent) = file_size_info
($fn);
761 next if !($format && defined($size));
764 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
765 my ($basevmid, $basename) = ($1, $2);
766 $volid = "$storeid:$basevmid/$basename/$owner/$name";
768 $volid = "$storeid:$owner/$name";
772 my $found = grep { $_ eq $volid } @$vollist;
777 volid
=> $volid, format
=> $format,
778 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
786 my ($class, $storeid, $scfg, $cache) = @_;
788 my $path = $scfg->{path
};
790 die "storage definintion has no path\n" if !$path;
793 my $res = PVE
::Tools
::df
($path, $timeout);
795 return undef if !$res || !$res->{total
};
797 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
800 sub activate_storage
{
801 my ($class, $storeid, $scfg, $cache) = @_;
803 my $path = $scfg->{path
};
805 die "storage definintion has no path\n" if !$path;
807 die "unable to activate storage '$storeid' - " .
808 "directory '$path' does not exist\n" if ! -d
$path;
810 if (defined($scfg->{content
})) {
811 foreach my $vtype (keys %$vtype_subdirs) {
812 # OpenVZMigrate uses backup (dump) dir
813 if (defined($scfg->{content
}->{$vtype}) ||
814 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
815 my $subdir = $class->get_subdir($scfg, $vtype);
816 mkpath
$subdir if $subdir ne $path;
822 sub deactivate_storage
{
823 my ($class, $storeid, $scfg, $cache) = @_;
825 # do nothing by default
828 sub activate_volume
{
829 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
831 my $path = $class->filesystem_path($scfg, $volname);
833 # check is volume exists
835 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
837 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
841 sub deactivate_volume
{
842 my ($class, $storeid, $scfg, $volname, $cache) = @_;
844 # do nothing by default
847 sub check_connection
{
848 my ($class, $storeid, $scfg) = @_;
849 # do nothing by default