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);
16 use base
qw(PVE::SectionConfig);
18 our @COMMON_TAR_FLAGS = qw(
20 -p --sparse --numeric-owner --acls
21 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
22 --warning=no-file-ignored --warning=no-xattr-write
25 our @SHARED_STORAGE = (
36 our $MAX_VOLUMES_PER_GUEST = 1024;
38 cfs_register_file
('storage.cfg',
39 sub { __PACKAGE__-
>parse_config(@_); },
40 sub { __PACKAGE__-
>write_config(@_); });
45 type
=> { description
=> "Storage type." },
46 storage
=> get_standard_option
('pve-storage-id',
47 { completion
=> \
&PVE
::Storage
::complete_storage
}),
48 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
50 description
=> "Allowed content types.\n\nNOTE: the value " .
51 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
52 type
=> 'string', format
=> 'pve-storage-content-list',
54 completion
=> \
&PVE
::Storage
::complete_content_type
,
57 description
=> "Flag to disable the storage.",
62 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
68 description
=> "Mark storage as shared.",
73 description
=> "Default image format.",
74 type
=> 'string', format
=> 'pve-storage-format',
80 sub content_hash_to_string
{
84 foreach my $ct (keys %$hash) {
85 push @cta, $ct if $hash->{$ct};
88 return join(',', @cta);
91 sub valid_content_types
{
94 my $def = $defaultData->{plugindata
}->{$type};
98 return $def->{content
}->[0];
104 my $type = $scfg->{type
};
105 my $def = $defaultData->{plugindata
}->{$type};
107 my $def_format = 'raw';
108 my $valid_formats = [ $def_format ];
110 if (defined($def->{format
})) {
111 $def_format = $scfg->{format
} || $def->{format
}->[1];
112 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
115 return wantarray ?
($def_format, $valid_formats) : $def_format;
118 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
120 my ($path, $noerr) = @_;
122 # fixme: exclude more shell meta characters?
123 # we need absolute paths
124 if ($path !~ m
|^/[^;\
(\
)]+|) {
125 return undef if $noerr;
126 die "value does not look like a valid absolute path\n";
131 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
133 my ($server, $noerr) = @_;
135 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
136 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
138 return undef if $noerr;
139 die "value does not look like a valid server name or IP address\n";
144 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
146 my ($name, $noerr) = @_;
148 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
149 return undef if $noerr;
150 die "lvm name '$name' contains illegal characters\n";
156 # fixme: do we need this
157 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
159 # my ($portal, $noerr) = @_;
161 # # IP with optional port
162 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
163 # return undef if $noerr;
164 # die "value does not look like a valid portal address\n";
169 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
170 sub verify_portal_dns
{
171 my ($portal, $noerr) = @_;
173 # IP or DNS name with optional port
174 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
175 return undef if $noerr;
176 die "value does not look like a valid portal address\n";
181 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
183 my ($ct, $noerr) = @_;
185 my $valid_content = valid_content_types
('dir'); # dir includes all types
187 if (!$valid_content->{$ct}) {
188 return undef if $noerr;
189 die "invalid content type '$ct'\n";
195 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
197 my ($fmt, $noerr) = @_;
199 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
200 return undef if $noerr;
201 die "invalid format '$fmt'\n";
207 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
209 my ($value, $noerr) = @_;
211 # mount options (see man fstab)
212 if ($value !~ m/^\S+$/) {
213 return undef if $noerr;
214 die "invalid options '$value'\n";
220 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
221 sub parse_volume_id
{
222 my ($volid, $noerr) = @_;
224 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
225 return wantarray ?
($1, $2) : $1;
227 return undef if $noerr;
228 die "unable to parse volume ID '$volid'\n";
236 sub parse_section_header
{
237 my ($class, $line) = @_;
239 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
240 my ($type, $storeid) = (lc($1), $2);
241 my $errmsg = undef; # set if you want to skip whole section
242 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
244 my $config = {}; # to return additional attributes
245 return ($type, $storeid, $errmsg, $config);
251 my ($class, $type, $key, $value) = @_;
253 my $def = $defaultData->{plugindata
}->{$type};
255 if ($key eq 'content') {
256 my $valid_content = $def->{content
}->[0];
260 foreach my $c (PVE
::Tools
::split_list
($value)) {
261 if (!$valid_content->{$c}) {
262 warn "storage does not support content type '$c'\n";
268 if ($res->{none
} && scalar (keys %$res) > 1) {
269 die "unable to combine 'none' with other content types\n";
273 } elsif ($key eq 'format') {
274 my $valid_formats = $def->{format
}->[0];
276 if (!$valid_formats->{$value}) {
277 warn "storage does not support format '$value'\n";
282 } elsif ($key eq 'nodes') {
285 foreach my $node (PVE
::Tools
::split_list
($value)) {
286 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
292 # no node restrictions for local storage
293 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
294 # die "storage '$storeid' does not allow node restrictions\n";
304 my ($class, $type, $key, $value) = @_;
306 if ($key eq 'nodes') {
307 return join(',', keys(%$value));
308 } elsif ($key eq 'content') {
309 my $res = content_hash_to_string
($value) || 'none';
317 my ($class, $filename, $raw) = @_;
319 my $cfg = $class->SUPER::parse_config
($filename, $raw);
320 my $ids = $cfg->{ids
};
322 # make sure we have a reasonable 'local:' storage
323 # we want 'local' to be always the same 'type' (on all cluster nodes)
324 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
325 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
328 priority
=> 0, # force first entry
329 path
=> '/var/lib/vz',
331 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, snippets
=> 1},
335 # make sure we have a path
336 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
338 # remove node restrictions for local storage
339 delete($ids->{local}->{nodes
});
341 foreach my $storeid (keys %$ids) {
342 my $d = $ids->{$storeid};
343 my $type = $d->{type
};
345 my $def = $defaultData->{plugindata
}->{$type};
347 if ($def->{content
}) {
348 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
350 if (grep { $_ eq $type } @SHARED_STORAGE) {
358 # Storage implementation
360 # called during addition of storage (before the new storage config got written)
361 # die to abort additon if there are (grave) problems
362 # NOTE: runs in a storage config *locked* context
364 my ($class, $storeid, $scfg, %param) = @_;
366 # do nothing by default
369 # called during deletion of storage (before the new storage config got written)
370 # and if the activate check on addition fails, to cleanup all storage traces
371 # which on_add_hook may have created.
372 # die to abort deletion if there are (very grave) problems
373 # NOTE: runs in a storage config *locked* context
375 my ($class, $storeid, $scfg) = @_;
377 # do nothing by default
380 sub cluster_lock_storage
{
381 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
385 my $lockid = "pve-storage-$storeid";
386 my $lockdir = "/var/lock/pve-manager";
388 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
391 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
400 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
401 return ($1, $3, $2); # (name, format, isBase)
404 die "unable to parse volume filename '$name'\n";
408 my ($class, $volname) = @_;
410 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
411 my ($basedvmid, $basename) = ($1, $2);
412 parse_name_dir
($basename);
413 my ($vmid, $name) = ($3, $4);
414 my (undef, $format, $isBase) = parse_name_dir
($name);
415 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
416 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
417 my ($vmid, $name) = ($1, $2);
418 my (undef, $format, $isBase) = parse_name_dir
($name);
419 return ('images', $name, $vmid, undef, undef, $isBase, $format);
420 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::iso_extension_re)$!) {
422 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
423 return ('vztmpl', $1);
424 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
425 return ('rootdir', $1, $1);
426 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
428 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
429 return ('backup', $fn, $2);
431 return ('backup', $fn);
432 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
433 return ('snippets', $1);
436 die "unable to parse directory volume name '$volname'\n";
439 my $vtype_subdirs = {
441 rootdir
=> 'private',
442 iso
=> 'template/iso',
443 vztmpl
=> 'template/cache',
445 snippets
=> 'snippets',
449 my ($class, $scfg, $vtype) = @_;
451 my $path = $scfg->{path
};
453 die "storage definintion has no path\n" if !$path;
455 my $subdir = $vtype_subdirs->{$vtype};
457 die "unknown vtype '$vtype'\n" if !defined($subdir);
459 return "$path/$subdir";
462 sub filesystem_path
{
463 my ($class, $scfg, $volname, $snapname) = @_;
465 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
466 $class->parse_volname($volname);
468 # Note: qcow2/qed has internal snapshot, so path is always
469 # the same (with or without snapshot => same file).
470 die "can't snapshot this image format\n"
471 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
473 my $dir = $class->get_subdir($scfg, $vtype);
475 $dir .= "/$vmid" if $vtype eq 'images';
477 my $path = "$dir/$name";
479 return wantarray ?
($path, $vmid, $vtype) : $path;
483 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
485 return $class->filesystem_path($scfg, $volname, $snapname);
489 my ($class, $storeid, $scfg, $volname) = @_;
491 # this only works for file based storage types
492 die "storage definition has no path\n" if !$scfg->{path
};
494 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
495 $class->parse_volname($volname);
497 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
499 die "create_base not possible with base image\n" if $isBase;
501 my $path = $class->filesystem_path($scfg, $volname);
503 my ($size, undef, $used, $parent) = file_size_info
($path);
504 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
506 die "volname '$volname' contains wrong information about parent\n"
507 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
510 $newname =~ s/^vm-/base-/;
512 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
515 my $newpath = $class->filesystem_path($scfg, $newvolname);
517 die "file '$newpath' already exists\n" if -f
$newpath;
519 rename($path, $newpath) ||
520 die "rename '$path' to '$newpath' failed - $!\n";
522 # We try to protect base volume
524 chmod(0444, $newpath); # nobody should write anything
526 # also try to set immutable flag
527 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
533 my $get_vm_disk_number = sub {
534 my ($disk_name, $scfg, $vmid, $suffix) = @_;
536 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
538 my $type = $scfg->{type
};
539 my $def = { %{$defaultData->{plugindata
}->{$type}} };
541 my $valid = $def->{format
}[0];
542 if ($valid->{subvol
}) {
543 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
546 if ($disk_name =~ m/$disk_regex/) {
553 sub get_next_vm_diskname
{
554 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
557 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
558 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
561 foreach my $disk (@$disk_list) {
562 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
563 $disk_ids->{$disknum} = 1 if defined($disknum);
566 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
567 if (!$disk_ids->{$i}) {
568 return "$prefix-$vmid-disk-$i$suffix";
572 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
575 my $find_free_diskname = sub {
576 my ($imgdir, $vmid, $fmt, $scfg) = @_;
580 if (defined(my $dh = IO
::Dir-
>new($imgdir))) {
581 @$disk_list = $dh->read();
585 return get_next_vm_diskname
($disk_list, $imgdir, $vmid, $fmt, $scfg, 1);
589 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
591 # this only works for file based storage types
592 die "storage definintion has no path\n" if !$scfg->{path
};
594 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
595 $class->parse_volname($volname);
597 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
599 die "this storage type does not support clone_image on snapshot\n" if $snap;
601 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
603 die "clone_image only works on base images\n" if !$isBase;
605 my $imagedir = $class->get_subdir($scfg, 'images');
606 $imagedir .= "/$vmid";
610 my $name = $find_free_diskname->($imagedir, $vmid, "qcow2", $scfg);
612 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
614 my $newvol = "$basevmid/$basename/$vmid/$name";
616 my $path = $class->filesystem_path($scfg, $newvol);
618 # Note: we use relative paths, so we need to call chdir before qemu-img
620 local $CWD = $imagedir;
622 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
623 '-f', 'qcow2', $path];
635 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
637 my $imagedir = $class->get_subdir($scfg, 'images');
638 $imagedir .= "/$vmid";
642 $name = $find_free_diskname->($imagedir, $vmid, $fmt, $scfg) if !$name;
644 my (undef, $tmpfmt) = parse_name_dir
($name);
646 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
649 my $path = "$imagedir/$name";
651 die "disk image '$path' already exists\n" if -e
$path;
653 if ($fmt eq 'subvol') {
654 # only allow this if size = 0, so that user knows what he is doing
655 die "storage does not support subvol quotas\n" if $size != 0;
657 my $old_umask = umask(0022);
659 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
663 my $cmd = ['/usr/bin/qemu-img', 'create'];
665 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
667 push @$cmd, '-f', $fmt, $path, "${size}K";
669 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
677 return "$vmid/$name";
681 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
683 my $path = $class->filesystem_path($scfg, $volname);
686 # try to remove immutable flag
687 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
691 if (defined($format) && ($format eq 'subvol')) {
692 File
::Path
::remove_tree
($path);
694 if (!(-f
$path || -l
$path)) {
695 warn "disk image '$path' does not exists\n";
699 unlink($path) || die "unlink '$path' failed - $!\n";
702 # try to cleanup directory to not clutter storage with empty $vmid dirs if
703 # all images from a guest got deleted
704 my $dir = dirname
($path);
711 my ($filename, $timeout) = @_;
714 return wantarray ?
(0, 'subvol', 0, undef) : 1;
719 run_command
(['/usr/bin/qemu-img', 'info', '--output=json', $filename],
721 outfunc
=> sub { $json .= shift },
722 errfunc
=> sub { warn "$_[0]\n" }
727 my $info = eval { decode_json
($json) };
728 warn "could not parse qemu-img info command output for '$filename'\n" if $@;
730 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
732 return wantarray ?
($size, $format, $used, $parent) : $size;
735 sub volume_size_info
{
736 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
737 my $path = $class->filesystem_path($scfg, $volname);
738 return file_size_info
($path, $timeout);
743 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
745 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
747 return 1 if $running;
749 my $path = $class->filesystem_path($scfg, $volname);
751 my $format = ($class->parse_volname($volname))[6];
753 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
755 run_command
($cmd, timeout
=> 10);
760 sub volume_snapshot
{
761 my ($class, $scfg, $storeid, $volname, $snap) = @_;
763 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
765 my $path = $class->filesystem_path($scfg, $volname);
767 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
774 sub volume_rollback_is_possible
{
775 my ($class, $scfg, $storeid, $volname, $snap) = @_;
780 sub volume_snapshot_rollback
{
781 my ($class, $scfg, $storeid, $volname, $snap) = @_;
783 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
785 my $path = $class->filesystem_path($scfg, $volname);
787 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
794 sub volume_snapshot_delete
{
795 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
797 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
799 return 1 if $running;
801 my $path = $class->filesystem_path($scfg, $volname);
803 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
805 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
812 sub storage_can_replicate
{
813 my ($class, $scfg, $storeid, $format) = @_;
818 sub volume_has_feature
{
819 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
822 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
823 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
824 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
825 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
826 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
827 snap
=> {qcow2
=> 1} },
828 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
829 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
832 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
833 $class->parse_volname($volname);
839 $key = $isBase ?
'base' : 'current';
842 return 1 if defined($features->{$feature}->{$key}->{$format});
848 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
850 my $imagedir = $class->get_subdir($scfg, 'images');
852 my ($defFmt, $vaidFmts) = default_format
($scfg);
853 my $fmts = join ('|', @$vaidFmts);
857 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
859 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
865 next if !$vollist && defined($vmid) && ($owner ne $vmid);
867 my ($size, $format, $used, $parent) = file_size_info
($fn);
868 next if !($format && defined($size));
871 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
872 my ($basevmid, $basename) = ($1, $2);
873 $volid = "$storeid:$basevmid/$basename/$owner/$name";
875 $volid = "$storeid:$owner/$name";
879 my $found = grep { $_ eq $volid } @$vollist;
884 volid
=> $volid, format
=> $format,
885 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
892 # list templates ($tt = <iso|vztmpl|backup|snippets>)
893 my $get_subdir_files = sub {
894 my ($sid, $path, $tt, $vmid) = @_;
898 foreach my $fn (<$path/*>) {
905 next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
907 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
909 } elsif ($tt eq 'vztmpl') {
910 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
912 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
914 } elsif ($tt eq 'backup') {
915 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
916 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
918 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
920 } elsif ($tt eq 'snippets') {
923 volid
=> "$sid:snippets/". basename
($fn),
928 $info->{size
} = -s
$fn // 0;
937 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
941 foreach my $ct (@$content_types) {
944 if ($ct eq 'images' || $ct eq 'rootdir') {
945 $data = $class->list_images($storeid, $scfg, $vmid);
946 } elsif ($scfg->{path
}) {
947 my $path = $class->get_subdir($scfg, $ct);
949 if ($ct eq 'iso' && !defined($vmid)) {
950 $data = $get_subdir_files->($storeid, $path, 'iso');
951 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
952 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
953 } elsif ($ct eq 'backup') {
954 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
955 } elsif ($ct eq 'snippets') {
956 $data = $get_subdir_files->($storeid, $path, 'snippets');
962 foreach my $item (@$data) {
963 $item->{content
} = $ct;
972 my ($class, $storeid, $scfg, $cache) = @_;
974 my $path = $scfg->{path
};
976 die "storage definintion has no path\n" if !$path;
979 my $res = PVE
::Tools
::df
($path, $timeout);
981 return undef if !$res || !$res->{total
};
983 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
986 sub volume_snapshot_list
{
987 my ($class, $scfg, $storeid, $volname) = @_;
989 # implement in subclass
990 die "Volume_snapshot_list is not implemented for $class";
992 # return an empty array if dataset does not exist.
995 sub activate_storage
{
996 my ($class, $storeid, $scfg, $cache) = @_;
998 my $path = $scfg->{path
};
1000 die "storage definintion has no path\n" if !$path;
1002 # this path test may hang indefinitely on unresponsive mounts
1004 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
1005 die "unable to activate storage '$storeid' - " .
1006 "directory '$path' does not exist or is unreachable\n";
1010 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1012 if (defined($scfg->{content
})) {
1013 foreach my $vtype (keys %$vtype_subdirs) {
1014 # OpenVZMigrate uses backup (dump) dir
1015 if (defined($scfg->{content
}->{$vtype}) ||
1016 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
1017 my $subdir = $class->get_subdir($scfg, $vtype);
1018 mkpath
$subdir if $subdir ne $path;
1024 sub deactivate_storage
{
1025 my ($class, $storeid, $scfg, $cache) = @_;
1027 # do nothing by default
1031 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1033 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1038 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1043 sub activate_volume
{
1044 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1046 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1048 # check is volume exists
1049 if ($scfg->{path
}) {
1050 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
1052 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
1056 sub deactivate_volume
{
1057 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1059 # do nothing by default
1062 sub check_connection
{
1063 my ($class, $storeid, $scfg) = @_;
1064 # do nothing by default
1068 # Import/Export interface:
1069 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1070 # the default implementations will return this if $scfg->{path} is set,
1071 # mimicking the old PVE::Storage::storage_migrate() function.
1073 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1074 # functions in case the format doesn't match their specialized
1075 # implementations to reuse the raw/tar code.
1077 # Format specification:
1078 # The following formats are all prefixed with image information in the form
1079 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1080 # to preallocate the image on storages which require it.
1082 # raw+size: (image files only)
1083 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1084 # qcow2+size, vmdk: (image files only)
1085 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1086 # files which are already in qcow2 format, or via `qemu-img convert`.
1087 # Note that these formats are only valid with $with_snapshots being true.
1088 # tar+size: (subvolumes only)
1089 # A GNU tar stream containing just the inner contents of the subvolume.
1090 # This does not distinguish between the contents of a privileged or
1091 # unprivileged container. In other words, this is from the root user
1092 # namespace's point of view with no uid-mapping in effect.
1093 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1095 # Plugins may reuse these helpers. Changes to the header format should be
1096 # reflected by changes to the function prototypes.
1097 sub write_common_header
($$) {
1098 my ($fh, $image_size_in_bytes) = @_;
1099 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1102 sub read_common_header
($) {
1104 sysread($fh, my $size, 8);
1105 $size = unpack('Q<', $size);
1106 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
1111 # Export a volume into a file handle as a stream of desired format.
1113 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1114 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1115 my $file = $class->path($scfg, $volname, $storeid)
1116 or goto unsupported
;
1117 my ($size, $file_format) = file_size_info
($file);
1119 if ($format eq 'raw+size') {
1120 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1121 write_common_header
($fh, $size);
1122 if ($file_format eq 'raw') {
1123 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1125 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1126 output
=> '>&'.fileno($fh));
1129 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1130 my $data_format = $1;
1131 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1132 write_common_header
($fh, $size);
1133 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1135 } elsif ($format eq 'tar+size') {
1136 goto unsupported
if $file_format ne 'subvol';
1137 write_common_header
($fh, $size);
1138 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1139 output
=> '>&'.fileno($fh));
1144 die "volume export format $format not available for $class";
1147 sub volume_export_formats
{
1148 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1149 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1150 my $file = $class->path($scfg, $volname, $storeid)
1152 my ($size, $format) = file_size_info
($file);
1154 if ($with_snapshots) {
1155 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1158 return ('tar+size') if $format eq 'subvol';
1159 return ('raw+size');
1164 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1166 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1168 die "volume import format '$format' not available for $class\n"
1169 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1170 my $data_format = $1;
1172 die "format $format cannot be imported without snapshots\n"
1173 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1174 die "format $format cannot be imported with snapshots\n"
1175 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1177 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1178 $class->parse_volname($volname);
1180 # XXX: Should we bother with conversion routines at this level? This won't
1181 # happen without manual CLI usage, so for now we just error out...
1182 die "cannot import format $format into a file of format $file_format\n"
1183 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1185 # Check for an existing file first since interrupting alloc_image doesn't
1187 my $file = $class->path($scfg, $volname, $storeid);
1188 die "file '$file' already exists\n" if -e
$file;
1190 my ($size) = read_common_header
($fh);
1191 $size = int($size/1024);
1194 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1195 if ($allocname ne $volname) {
1196 my $oldname = $volname;
1197 $volname = $allocname; # Let the cleanup code know what to free
1198 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1200 my $file = $class->path($scfg, $volname, $storeid)
1201 or die "internal error: failed to get path to newly allocated volume $volname\n";
1202 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1203 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1204 input
=> '<&'.fileno($fh));
1205 } elsif ($data_format eq 'tar') {
1206 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1207 input
=> '<&'.fileno($fh));
1209 die "volume import format '$format' not available for $class";
1213 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1219 sub volume_import_formats
{
1220 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1221 if ($scfg->{path
} && !defined($base_snapshot)) {
1222 my $format = ($class->parse_volname($volname))[6];
1223 if ($with_snapshots) {
1224 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1227 return ('tar+size') if $format eq 'subvol';
1228 return ('raw+size');