1 package PVE
::Storage
::Plugin
;
10 use PVE
::Tools
qw(run_command);
11 use PVE
::JSONSchema
qw(get_standard_option);
12 use PVE
::Cluster
qw(cfs_register_file);
14 use base
qw(PVE::SectionConfig);
16 our @COMMON_TAR_FLAGS = qw(
18 -p --sparse --numeric-owner --acls
19 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
20 --warning=no-file-ignored --warning=no-xattr-write
23 our @SHARED_STORAGE = (
34 our $MAX_VOLUMES_PER_GUEST = 1024;
36 cfs_register_file
('storage.cfg',
37 sub { __PACKAGE__-
>parse_config(@_); },
38 sub { __PACKAGE__-
>write_config(@_); });
43 type
=> { description
=> "Storage type." },
44 storage
=> get_standard_option
('pve-storage-id',
45 { completion
=> \
&PVE
::Storage
::complete_storage
}),
46 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
48 description
=> "Allowed content types.\n\nNOTE: the value " .
49 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
50 type
=> 'string', format
=> 'pve-storage-content-list',
52 completion
=> \
&PVE
::Storage
::complete_content_type
,
55 description
=> "Flag to disable the storage.",
60 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
66 description
=> "Mark storage as shared.",
71 description
=> "Default image format.",
72 type
=> 'string', format
=> 'pve-storage-format',
78 sub content_hash_to_string
{
82 foreach my $ct (keys %$hash) {
83 push @cta, $ct if $hash->{$ct};
86 return join(',', @cta);
89 sub valid_content_types
{
92 my $def = $defaultData->{plugindata
}->{$type};
96 return $def->{content
}->[0];
102 my $type = $scfg->{type
};
103 my $def = $defaultData->{plugindata
}->{$type};
105 my $def_format = 'raw';
106 my $valid_formats = [ $def_format ];
108 if (defined($def->{format
})) {
109 $def_format = $scfg->{format
} || $def->{format
}->[1];
110 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
113 return wantarray ?
($def_format, $valid_formats) : $def_format;
116 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
118 my ($path, $noerr) = @_;
120 # fixme: exclude more shell meta characters?
121 # we need absolute paths
122 if ($path !~ m
|^/[^;\
(\
)]+|) {
123 return undef if $noerr;
124 die "value does not look like a valid absolute path\n";
129 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
131 my ($server, $noerr) = @_;
133 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
134 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
136 return undef if $noerr;
137 die "value does not look like a valid server name or IP address\n";
142 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
144 my ($name, $noerr) = @_;
146 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
147 return undef if $noerr;
148 die "lvm name '$name' contains illegal characters\n";
154 # fixme: do we need this
155 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
157 # my ($portal, $noerr) = @_;
159 # # IP with optional port
160 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
161 # return undef if $noerr;
162 # die "value does not look like a valid portal address\n";
167 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
168 sub verify_portal_dns
{
169 my ($portal, $noerr) = @_;
171 # IP or DNS name with optional port
172 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
173 return undef if $noerr;
174 die "value does not look like a valid portal address\n";
179 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
181 my ($ct, $noerr) = @_;
183 my $valid_content = valid_content_types
('dir'); # dir includes all types
185 if (!$valid_content->{$ct}) {
186 return undef if $noerr;
187 die "invalid content type '$ct'\n";
193 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
195 my ($fmt, $noerr) = @_;
197 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
198 return undef if $noerr;
199 die "invalid format '$fmt'\n";
205 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
207 my ($value, $noerr) = @_;
209 # mount options (see man fstab)
210 if ($value !~ m/^\S+$/) {
211 return undef if $noerr;
212 die "invalid options '$value'\n";
218 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
219 sub parse_volume_id
{
220 my ($volid, $noerr) = @_;
222 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
223 return wantarray ?
($1, $2) : $1;
225 return undef if $noerr;
226 die "unable to parse volume ID '$volid'\n";
234 sub parse_section_header
{
235 my ($class, $line) = @_;
237 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
238 my ($type, $storeid) = (lc($1), $2);
239 my $errmsg = undef; # set if you want to skip whole section
240 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
242 my $config = {}; # to return additional attributes
243 return ($type, $storeid, $errmsg, $config);
249 my ($class, $type, $key, $value) = @_;
251 my $def = $defaultData->{plugindata
}->{$type};
253 if ($key eq 'content') {
254 my $valid_content = $def->{content
}->[0];
258 foreach my $c (PVE
::Tools
::split_list
($value)) {
259 if (!$valid_content->{$c}) {
260 warn "storage does not support content type '$c'\n";
266 if ($res->{none
} && scalar (keys %$res) > 1) {
267 die "unable to combine 'none' with other content types\n";
271 } elsif ($key eq 'format') {
272 my $valid_formats = $def->{format
}->[0];
274 if (!$valid_formats->{$value}) {
275 warn "storage does not support format '$value'\n";
280 } elsif ($key eq 'nodes') {
283 foreach my $node (PVE
::Tools
::split_list
($value)) {
284 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
290 # no node restrictions for local storage
291 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
292 # die "storage '$storeid' does not allow node restrictions\n";
302 my ($class, $type, $key, $value) = @_;
304 if ($key eq 'nodes') {
305 return join(',', keys(%$value));
306 } elsif ($key eq 'content') {
307 my $res = content_hash_to_string
($value) || 'none';
315 my ($class, $filename, $raw) = @_;
317 my $cfg = $class->SUPER::parse_config
($filename, $raw);
318 my $ids = $cfg->{ids
};
320 # make sure we have a reasonable 'local:' storage
321 # we want 'local' to be always the same 'type' (on all cluster nodes)
322 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
323 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
326 priority
=> 0, # force first entry
327 path
=> '/var/lib/vz',
329 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, snippets
=> 1},
333 # make sure we have a path
334 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
336 # remove node restrictions for local storage
337 delete($ids->{local}->{nodes
});
339 foreach my $storeid (keys %$ids) {
340 my $d = $ids->{$storeid};
341 my $type = $d->{type
};
343 my $def = $defaultData->{plugindata
}->{$type};
345 if ($def->{content
}) {
346 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
348 if (grep { $_ eq $type } @SHARED_STORAGE) {
356 # Storage implementation
358 # called during addition of storage (before the new storage config got written)
359 # die to abort additon if there are (grave) problems
360 # NOTE: runs in a storage config *locked* context
362 my ($class, $storeid, $scfg, %param) = @_;
364 # do nothing by default
367 # called during deletion of storage (before the new storage config got written)
368 # and if the activate check on addition fails, to cleanup all storage traces
369 # which on_add_hook may have created.
370 # die to abort deletion if there are (very grave) problems
371 # NOTE: runs in a storage config *locked* context
373 my ($class, $storeid, $scfg) = @_;
375 # do nothing by default
378 sub cluster_lock_storage
{
379 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
383 my $lockid = "pve-storage-$storeid";
384 my $lockdir = "/var/lock/pve-manager";
386 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
389 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
398 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
399 return ($1, $3, $2); # (name, format, isBase)
402 die "unable to parse volume filename '$name'\n";
406 my ($class, $volname) = @_;
408 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
409 my ($basedvmid, $basename) = ($1, $2);
410 parse_name_dir
($basename);
411 my ($vmid, $name) = ($3, $4);
412 my (undef, $format, $isBase) = parse_name_dir
($name);
413 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
414 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
415 my ($vmid, $name) = ($1, $2);
416 my (undef, $format, $isBase) = parse_name_dir
($name);
417 return ('images', $name, $vmid, undef, undef, $isBase, $format);
418 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
420 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
421 return ('vztmpl', $1);
422 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
423 return ('rootdir', $1, $1);
424 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
426 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
427 return ('backup', $fn, $2);
429 return ('backup', $fn);
430 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
431 return ('snippets', $1);
434 die "unable to parse directory volume name '$volname'\n";
437 my $vtype_subdirs = {
439 rootdir
=> 'private',
440 iso
=> 'template/iso',
441 vztmpl
=> 'template/cache',
443 snippets
=> 'snippets',
447 my ($class, $scfg, $vtype) = @_;
449 my $path = $scfg->{path
};
451 die "storage definintion has no path\n" if !$path;
453 my $subdir = $vtype_subdirs->{$vtype};
455 die "unknown vtype '$vtype'\n" if !defined($subdir);
457 return "$path/$subdir";
460 sub filesystem_path
{
461 my ($class, $scfg, $volname, $snapname) = @_;
463 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
464 $class->parse_volname($volname);
466 # Note: qcow2/qed has internal snapshot, so path is always
467 # the same (with or without snapshot => same file).
468 die "can't snapshot this image format\n"
469 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
471 my $dir = $class->get_subdir($scfg, $vtype);
473 $dir .= "/$vmid" if $vtype eq 'images';
475 my $path = "$dir/$name";
477 return wantarray ?
($path, $vmid, $vtype) : $path;
481 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
483 return $class->filesystem_path($scfg, $volname, $snapname);
487 my ($class, $storeid, $scfg, $volname) = @_;
489 # this only works for file based storage types
490 die "storage definition has no path\n" if !$scfg->{path
};
492 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
493 $class->parse_volname($volname);
495 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
497 die "create_base not possible with base image\n" if $isBase;
499 my $path = $class->filesystem_path($scfg, $volname);
501 my ($size, undef, $used, $parent) = file_size_info
($path);
502 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
504 die "volname '$volname' contains wrong information about parent\n"
505 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
508 $newname =~ s/^vm-/base-/;
510 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
513 my $newpath = $class->filesystem_path($scfg, $newvolname);
515 die "file '$newpath' already exists\n" if -f
$newpath;
517 rename($path, $newpath) ||
518 die "rename '$path' to '$newpath' failed - $!\n";
520 # We try to protect base volume
522 chmod(0444, $newpath); # nobody should write anything
524 # also try to set immutable flag
525 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
531 my $get_vm_disk_number = sub {
532 my ($disk_name, $scfg, $vmid, $suffix) = @_;
534 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
536 my $type = $scfg->{type
};
537 my $def = { %{$defaultData->{plugindata
}->{$type}} };
539 my $valid = $def->{format
}[0];
540 if ($valid->{subvol
}) {
541 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
544 if ($disk_name =~ m/$disk_regex/) {
551 sub get_next_vm_diskname
{
552 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
555 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
556 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
559 foreach my $disk (@$disk_list) {
560 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
561 $disk_ids->{$disknum} = 1 if defined($disknum);
564 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
565 if (!$disk_ids->{$i}) {
566 return "$prefix-$vmid-disk-$i$suffix";
570 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
573 my $find_free_diskname = sub {
574 my ($imgdir, $vmid, $fmt, $scfg) = @_;
578 if (defined(my $dh = IO
::Dir-
>new($imgdir))) {
579 @$disk_list = $dh->read();
583 return get_next_vm_diskname
($disk_list, $imgdir, $vmid, $fmt, $scfg, 1);
587 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
589 # this only works for file based storage types
590 die "storage definintion has no path\n" if !$scfg->{path
};
592 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
593 $class->parse_volname($volname);
595 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
597 die "this storage type does not support clone_image on snapshot\n" if $snap;
599 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
601 die "clone_image only works on base images\n" if !$isBase;
603 my $imagedir = $class->get_subdir($scfg, 'images');
604 $imagedir .= "/$vmid";
608 my $name = $find_free_diskname->($imagedir, $vmid, "qcow2", $scfg);
610 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
612 my $newvol = "$basevmid/$basename/$vmid/$name";
614 my $path = $class->filesystem_path($scfg, $newvol);
616 # Note: we use relative paths, so we need to call chdir before qemu-img
618 local $CWD = $imagedir;
620 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
621 '-f', 'qcow2', $path];
633 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
635 my $imagedir = $class->get_subdir($scfg, 'images');
636 $imagedir .= "/$vmid";
640 $name = $find_free_diskname->($imagedir, $vmid, $fmt, $scfg) if !$name;
642 my (undef, $tmpfmt) = parse_name_dir
($name);
644 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
647 my $path = "$imagedir/$name";
649 die "disk image '$path' already exists\n" if -e
$path;
651 if ($fmt eq 'subvol') {
652 # only allow this if size = 0, so that user knows what he is doing
653 die "storage does not support subvol quotas\n" if $size != 0;
655 my $old_umask = umask(0022);
657 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
661 my $cmd = ['/usr/bin/qemu-img', 'create'];
663 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
665 push @$cmd, '-f', $fmt, $path, "${size}K";
667 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
675 return "$vmid/$name";
679 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
681 my $path = $class->filesystem_path($scfg, $volname);
684 # try to remove immutable flag
685 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
689 if (defined($format) && ($format eq 'subvol')) {
690 File
::Path
::remove_tree
($path);
692 if (!(-f
$path || -l
$path)) {
693 warn "disk image '$path' does not exists\n";
697 unlink($path) || die "unlink '$path' failed - $!\n";
700 # try to cleanup directory to not clutter storage with empty $vmid dirs if
701 # all images from a guest got deleted
702 my $dir = dirname
($path);
709 my ($filename, $timeout) = @_;
712 return wantarray ?
(0, 'subvol', 0, undef) : 1;
715 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
723 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
725 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
727 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
729 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
731 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
735 $used *= 1024 if $u eq 'K';
736 $used *= (1024*1024) if $u eq 'M';
737 $used *= (1024*1024*1024) if $u eq 'G';
738 $used *= (1024*1024*1024*1024) if $u eq 'T';
745 return wantarray ?
($size, $format, $used, $parent) : $size;
748 sub volume_size_info
{
749 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
750 my $path = $class->filesystem_path($scfg, $volname);
751 return file_size_info
($path, $timeout);
756 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
758 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
760 return 1 if $running;
762 my $path = $class->filesystem_path($scfg, $volname);
764 my $format = ($class->parse_volname($volname))[6];
766 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
768 run_command
($cmd, timeout
=> 10);
773 sub volume_snapshot
{
774 my ($class, $scfg, $storeid, $volname, $snap) = @_;
776 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
778 my $path = $class->filesystem_path($scfg, $volname);
780 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
787 sub volume_rollback_is_possible
{
788 my ($class, $scfg, $storeid, $volname, $snap) = @_;
793 sub volume_snapshot_rollback
{
794 my ($class, $scfg, $storeid, $volname, $snap) = @_;
796 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
798 my $path = $class->filesystem_path($scfg, $volname);
800 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
807 sub volume_snapshot_delete
{
808 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
810 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
812 return 1 if $running;
814 my $path = $class->filesystem_path($scfg, $volname);
816 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
818 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
825 sub storage_can_replicate
{
826 my ($class, $scfg, $storeid, $format) = @_;
831 sub volume_has_feature
{
832 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
835 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
836 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
837 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
838 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
839 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
840 snap
=> {qcow2
=> 1} },
841 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
842 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
845 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
846 $class->parse_volname($volname);
852 $key = $isBase ?
'base' : 'current';
855 return 1 if defined($features->{$feature}->{$key}->{$format});
861 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
863 my $imagedir = $class->get_subdir($scfg, 'images');
865 my ($defFmt, $vaidFmts) = default_format
($scfg);
866 my $fmts = join ('|', @$vaidFmts);
870 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
872 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
878 next if !$vollist && defined($vmid) && ($owner ne $vmid);
880 my ($size, $format, $used, $parent) = file_size_info
($fn);
881 next if !($format && defined($size));
884 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
885 my ($basevmid, $basename) = ($1, $2);
886 $volid = "$storeid:$basevmid/$basename/$owner/$name";
888 $volid = "$storeid:$owner/$name";
892 my $found = grep { $_ eq $volid } @$vollist;
897 volid
=> $volid, format
=> $format,
898 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
905 # list templates ($tt = <iso|vztmpl|backup|snippets>)
906 my $get_subdir_files = sub {
907 my ($sid, $path, $tt, $vmid) = @_;
911 foreach my $fn (<$path/*>) {
918 next if $fn !~ m!/([^/]+\.iso)$!i;
920 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
922 } elsif ($tt eq 'vztmpl') {
923 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
925 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
927 } elsif ($tt eq 'backup') {
928 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
929 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
931 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
933 } elsif ($tt eq 'snippets') {
936 volid
=> "$sid:snippets/". basename
($fn),
941 $info->{size
} = -s
$fn // 0;
950 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
954 foreach my $ct (@$content_types) {
957 if ($ct eq 'images' || $ct eq 'rootdir') {
958 $data = $class->list_images($storeid, $scfg, $vmid);
959 } elsif ($scfg->{path
}) {
960 my $path = $class->get_subdir($scfg, $ct);
962 if ($ct eq 'iso' && !defined($vmid)) {
963 $data = $get_subdir_files->($storeid, $path, 'iso');
964 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
965 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
966 } elsif ($ct eq 'backup') {
967 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
968 } elsif ($ct eq 'snippets') {
969 $data = $get_subdir_files->($storeid, $path, 'snippets');
975 foreach my $item (@$data) {
976 $item->{content
} = $ct;
985 my ($class, $storeid, $scfg, $cache) = @_;
987 my $path = $scfg->{path
};
989 die "storage definintion has no path\n" if !$path;
992 my $res = PVE
::Tools
::df
($path, $timeout);
994 return undef if !$res || !$res->{total
};
996 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
999 sub volume_snapshot_list
{
1000 my ($class, $scfg, $storeid, $volname) = @_;
1002 # implement in subclass
1003 die "Volume_snapshot_list is not implemented for $class";
1005 # return an empty array if dataset does not exist.
1008 sub activate_storage
{
1009 my ($class, $storeid, $scfg, $cache) = @_;
1011 my $path = $scfg->{path
};
1013 die "storage definintion has no path\n" if !$path;
1015 # this path test may hang indefinitely on unresponsive mounts
1017 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
1018 die "unable to activate storage '$storeid' - " .
1019 "directory '$path' does not exist or is unreachable\n";
1023 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1025 if (defined($scfg->{content
})) {
1026 foreach my $vtype (keys %$vtype_subdirs) {
1027 # OpenVZMigrate uses backup (dump) dir
1028 if (defined($scfg->{content
}->{$vtype}) ||
1029 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
1030 my $subdir = $class->get_subdir($scfg, $vtype);
1031 mkpath
$subdir if $subdir ne $path;
1037 sub deactivate_storage
{
1038 my ($class, $storeid, $scfg, $cache) = @_;
1040 # do nothing by default
1044 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1046 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1051 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1056 sub activate_volume
{
1057 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1059 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1061 # check is volume exists
1062 if ($scfg->{path
}) {
1063 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
1065 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
1069 sub deactivate_volume
{
1070 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1072 # do nothing by default
1075 sub check_connection
{
1076 my ($class, $storeid, $scfg) = @_;
1077 # do nothing by default
1081 # Import/Export interface:
1082 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1083 # the default implementations will return this if $scfg->{path} is set,
1084 # mimicking the old PVE::Storage::storage_migrate() function.
1086 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1087 # functions in case the format doesn't match their specialized
1088 # implementations to reuse the raw/tar code.
1090 # Format specification:
1091 # The following formats are all prefixed with image information in the form
1092 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1093 # to preallocate the image on storages which require it.
1095 # raw+size: (image files only)
1096 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1097 # qcow2+size, vmdk: (image files only)
1098 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1099 # files which are already in qcow2 format, or via `qemu-img convert`.
1100 # Note that these formats are only valid with $with_snapshots being true.
1101 # tar+size: (subvolumes only)
1102 # A GNU tar stream containing just the inner contents of the subvolume.
1103 # This does not distinguish between the contents of a privileged or
1104 # unprivileged container. In other words, this is from the root user
1105 # namespace's point of view with no uid-mapping in effect.
1106 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1108 # Plugins may reuse these helpers. Changes to the header format should be
1109 # reflected by changes to the function prototypes.
1110 sub write_common_header
($$) {
1111 my ($fh, $image_size_in_bytes) = @_;
1112 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1115 sub read_common_header
($) {
1117 sysread($fh, my $size, 8);
1118 $size = unpack('Q<', $size);
1119 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
1124 # Export a volume into a file handle as a stream of desired format.
1126 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1127 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1128 my $file = $class->path($scfg, $volname, $storeid)
1129 or goto unsupported
;
1130 my ($size, $file_format) = file_size_info
($file);
1132 if ($format eq 'raw+size') {
1133 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1134 write_common_header
($fh, $size);
1135 if ($file_format eq 'raw') {
1136 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1138 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1139 output
=> '>&'.fileno($fh));
1142 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1143 my $data_format = $1;
1144 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1145 write_common_header
($fh, $size);
1146 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1148 } elsif ($format eq 'tar+size') {
1149 goto unsupported
if $file_format ne 'subvol';
1150 write_common_header
($fh, $size);
1151 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1152 output
=> '>&'.fileno($fh));
1157 die "volume export format $format not available for $class";
1160 sub volume_export_formats
{
1161 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1162 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1163 my $file = $class->path($scfg, $volname, $storeid)
1165 my ($size, $format) = file_size_info
($file);
1167 if ($with_snapshots) {
1168 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1171 return ('tar+size') if $format eq 'subvol';
1172 return ('raw+size');
1177 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1179 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1181 die "volume import format '$format' not available for $class\n"
1182 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1183 my $data_format = $1;
1185 die "format $format cannot be imported without snapshots\n"
1186 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1187 die "format $format cannot be imported with snapshots\n"
1188 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1190 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1191 $class->parse_volname($volname);
1193 # XXX: Should we bother with conversion routines at this level? This won't
1194 # happen without manual CLI usage, so for now we just error out...
1195 die "cannot import format $format into a file of format $file_format\n"
1196 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1198 # Check for an existing file first since interrupting alloc_image doesn't
1200 my $file = $class->path($scfg, $volname, $storeid);
1201 die "file '$file' already exists\n" if -e
$file;
1203 my ($size) = read_common_header
($fh);
1204 $size = int($size/1024);
1207 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1208 if ($allocname ne $volname) {
1209 my $oldname = $volname;
1210 $volname = $allocname; # Let the cleanup code know what to free
1211 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1213 my $file = $class->path($scfg, $volname, $storeid)
1214 or die "internal error: failed to get path to newly allocated volume $volname\n";
1215 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1216 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1217 input
=> '<&'.fileno($fh));
1218 } elsif ($data_format eq 'tar') {
1219 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1220 input
=> '<&'.fileno($fh));
1222 die "volume import format '$format' not available for $class";
1226 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1232 sub volume_import_formats
{
1233 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1234 if ($scfg->{path
} && !defined($base_snapshot)) {
1235 my $format = ($class->parse_volname($volname))[6];
1236 if ($with_snapshots) {
1237 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1240 return ('tar+size') if $format eq 'subvol';
1241 return ('raw+size');