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 our @COMMON_TAR_FLAGS = qw(
17 -p --sparse --numeric-owner --acls
18 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
19 --warning=no-file-ignored --warning=no-xattr-write
22 our @SHARED_STORAGE = (
34 cfs_register_file
('storage.cfg',
35 sub { __PACKAGE__-
>parse_config(@_); },
36 sub { __PACKAGE__-
>write_config(@_); });
41 type
=> { description
=> "Storage type." },
42 storage
=> get_standard_option
('pve-storage-id',
43 { completion
=> \
&PVE
::Storage
::complete_storage
}),
44 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
46 description
=> "Allowed content types.\n\nNOTE: the value " .
47 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
48 type
=> 'string', format
=> 'pve-storage-content-list',
50 completion
=> \
&PVE
::Storage
::complete_content_type
,
53 description
=> "Flag to disable the storage.",
58 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
64 description
=> "Mark storage as shared.",
69 description
=> "Default image format.",
70 type
=> 'string', format
=> 'pve-storage-format',
76 sub content_hash_to_string
{
80 foreach my $ct (keys %$hash) {
81 push @cta, $ct if $hash->{$ct};
84 return join(',', @cta);
87 sub valid_content_types
{
90 my $def = $defaultData->{plugindata
}->{$type};
94 return $def->{content
}->[0];
100 my $type = $scfg->{type
};
101 my $def = $defaultData->{plugindata
}->{$type};
103 my $def_format = 'raw';
104 my $valid_formats = [ $def_format ];
106 if (defined($def->{format
})) {
107 $def_format = $scfg->{format
} || $def->{format
}->[1];
108 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
111 return wantarray ?
($def_format, $valid_formats) : $def_format;
114 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
116 my ($path, $noerr) = @_;
118 # fixme: exclude more shell meta characters?
119 # we need absolute paths
120 if ($path !~ m
|^/[^;\
(\
)]+|) {
121 return undef if $noerr;
122 die "value does not look like a valid absolute path\n";
127 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
129 my ($server, $noerr) = @_;
131 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
132 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
134 return undef if $noerr;
135 die "value does not look like a valid server name or IP address\n";
140 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
142 my ($name, $noerr) = @_;
144 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
145 return undef if $noerr;
146 die "lvm name '$name' contains illegal characters\n";
152 # fixme: do we need this
153 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
155 # my ($portal, $noerr) = @_;
157 # # IP with optional port
158 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
159 # return undef if $noerr;
160 # die "value does not look like a valid portal address\n";
165 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
166 sub verify_portal_dns
{
167 my ($portal, $noerr) = @_;
169 # IP or DNS name with optional port
170 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
171 return undef if $noerr;
172 die "value does not look like a valid portal address\n";
177 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
179 my ($ct, $noerr) = @_;
181 my $valid_content = valid_content_types
('dir'); # dir includes all types
183 if (!$valid_content->{$ct}) {
184 return undef if $noerr;
185 die "invalid content type '$ct'\n";
191 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
193 my ($fmt, $noerr) = @_;
195 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
196 return undef if $noerr;
197 die "invalid format '$fmt'\n";
203 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
205 my ($value, $noerr) = @_;
207 # mount options (see man fstab)
208 if ($value !~ m/^\S+$/) {
209 return undef if $noerr;
210 die "invalid options '$value'\n";
216 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
217 sub parse_volume_id
{
218 my ($volid, $noerr) = @_;
220 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
221 return wantarray ?
($1, $2) : $1;
223 return undef if $noerr;
224 die "unable to parse volume ID '$volid'\n";
232 sub parse_section_header
{
233 my ($class, $line) = @_;
235 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
236 my ($type, $storeid) = (lc($1), $2);
237 my $errmsg = undef; # set if you want to skip whole section
238 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
240 my $config = {}; # to return additional attributes
241 return ($type, $storeid, $errmsg, $config);
247 my ($class, $type, $key, $value) = @_;
249 my $def = $defaultData->{plugindata
}->{$type};
251 if ($key eq 'content') {
252 my $valid_content = $def->{content
}->[0];
256 foreach my $c (PVE
::Tools
::split_list
($value)) {
257 if (!$valid_content->{$c}) {
258 warn "storage does not support content type '$c'\n";
264 if ($res->{none
} && scalar (keys %$res) > 1) {
265 die "unable to combine 'none' with other content types\n";
269 } elsif ($key eq 'format') {
270 my $valid_formats = $def->{format
}->[0];
272 if (!$valid_formats->{$value}) {
273 warn "storage does not support format '$value'\n";
278 } elsif ($key eq 'nodes') {
281 foreach my $node (PVE
::Tools
::split_list
($value)) {
282 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
288 # no node restrictions for local storage
289 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
290 # die "storage '$storeid' does not allow node restrictions\n";
300 my ($class, $type, $key, $value) = @_;
302 if ($key eq 'nodes') {
303 return join(',', keys(%$value));
304 } elsif ($key eq 'content') {
305 my $res = content_hash_to_string
($value) || 'none';
313 my ($class, $filename, $raw) = @_;
315 my $cfg = $class->SUPER::parse_config
($filename, $raw);
316 my $ids = $cfg->{ids
};
318 # make sure we have a reasonable 'local:' storage
319 # we want 'local' to be always the same 'type' (on all cluster nodes)
320 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
321 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
324 priority
=> 0, # force first entry
325 path
=> '/var/lib/vz',
327 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
331 # make sure we have a path
332 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
334 # remove node restrictions for local storage
335 delete($ids->{local}->{nodes
});
337 foreach my $storeid (keys %$ids) {
338 my $d = $ids->{$storeid};
339 my $type = $d->{type
};
341 my $def = $defaultData->{plugindata
}->{$type};
343 if ($def->{content
}) {
344 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
346 if (grep { $_ eq $type } @SHARED_STORAGE) {
354 # Storage implementation
356 # called during addition of storage (before the new storage config got written)
357 # die to abort additon if there are (grave) problems
358 # NOTE: runs in a storage config *locked* context
360 my ($class, $storeid, $scfg, %param) = @_;
362 # do nothing by default
365 # called during deletion of storage (before the new storage config got written)
366 # and if the activate check on addition fails, to cleanup all storage traces
367 # which on_add_hook may have created.
368 # die to abort deletion if there are (very grave) problems
369 # NOTE: runs in a storage config *locked* context
371 my ($class, $storeid, $scfg) = @_;
373 # do nothing by default
376 sub cluster_lock_storage
{
377 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
381 my $lockid = "pve-storage-$storeid";
382 my $lockdir = "/var/lock/pve-manager";
384 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
387 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
396 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
397 return ($1, $3, $2); # (name, format, isBase)
400 die "unable to parse volume filename '$name'\n";
404 my ($class, $volname) = @_;
406 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
407 my ($basedvmid, $basename) = ($1, $2);
408 parse_name_dir
($basename);
409 my ($vmid, $name) = ($3, $4);
410 my (undef, $format, $isBase) = parse_name_dir
($name);
411 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
412 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
413 my ($vmid, $name) = ($1, $2);
414 my (undef, $format, $isBase) = parse_name_dir
($name);
415 return ('images', $name, $vmid, undef, undef, $isBase, $format);
416 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
418 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
419 return ('vztmpl', $1);
420 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
421 return ('rootdir', $1, $1);
422 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
424 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
425 return ('backup', $fn, $2);
427 return ('backup', $fn);
430 die "unable to parse directory volume name '$volname'\n";
433 my $vtype_subdirs = {
435 rootdir
=> 'private',
436 iso
=> 'template/iso',
437 vztmpl
=> 'template/cache',
442 my ($class, $scfg, $vtype) = @_;
444 my $path = $scfg->{path
};
446 die "storage definintion has no path\n" if !$path;
448 my $subdir = $vtype_subdirs->{$vtype};
450 die "unknown vtype '$vtype'\n" if !defined($subdir);
452 return "$path/$subdir";
455 sub filesystem_path
{
456 my ($class, $scfg, $volname, $snapname) = @_;
458 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
459 $class->parse_volname($volname);
461 # Note: qcow2/qed has internal snapshot, so path is always
462 # the same (with or without snapshot => same file).
463 die "can't snapshot this image format\n"
464 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
466 my $dir = $class->get_subdir($scfg, $vtype);
468 $dir .= "/$vmid" if $vtype eq 'images';
470 my $path = "$dir/$name";
472 return wantarray ?
($path, $vmid, $vtype) : $path;
476 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
478 return $class->filesystem_path($scfg, $volname, $snapname);
482 my ($class, $storeid, $scfg, $volname) = @_;
484 # this only works for file based storage types
485 die "storage definition has no path\n" if !$scfg->{path
};
487 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
488 $class->parse_volname($volname);
490 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
492 die "create_base not possible with base image\n" if $isBase;
494 my $path = $class->filesystem_path($scfg, $volname);
496 my ($size, undef, $used, $parent) = file_size_info
($path);
497 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
499 die "volname '$volname' contains wrong information about parent\n"
500 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
503 $newname =~ s/^vm-/base-/;
505 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
508 my $newpath = $class->filesystem_path($scfg, $newvolname);
510 die "file '$newpath' already exists\n" if -f
$newpath;
512 rename($path, $newpath) ||
513 die "rename '$path' to '$newpath' failed - $!\n";
515 # We try to protect base volume
517 chmod(0444, $newpath); # nobody should write anything
519 # also try to set immutable flag
520 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
526 my $find_free_diskname = sub {
527 my ($imgdir, $vmid, $fmt) = @_;
530 PVE
::Tools
::dir_glob_foreach
($imgdir,
531 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
533 my ($fn, $type, $disk) = @_;
534 $disk_ids->{$disk} = 1;
537 for (my $i = 1; $i < 100; $i++) {
538 if (!$disk_ids->{$i}) {
539 return "vm-$vmid-disk-$i.$fmt";
543 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
547 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
549 # this only works for file based storage types
550 die "storage definintion has no path\n" if !$scfg->{path
};
552 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
553 $class->parse_volname($volname);
555 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
557 die "this storage type does not support clone_image on snapshot\n" if $snap;
559 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
561 die "clone_image only works on base images\n" if !$isBase;
563 my $imagedir = $class->get_subdir($scfg, 'images');
564 $imagedir .= "/$vmid";
568 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
570 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
572 my $newvol = "$basevmid/$basename/$vmid/$name";
574 my $path = $class->filesystem_path($scfg, $newvol);
576 # Note: we use relative paths, so we need to call chdir before qemu-img
578 local $CWD = $imagedir;
580 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
581 '-f', 'qcow2', $path];
593 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
595 my $imagedir = $class->get_subdir($scfg, 'images');
596 $imagedir .= "/$vmid";
600 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
602 my (undef, $tmpfmt) = parse_name_dir
($name);
604 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
607 my $path = "$imagedir/$name";
609 die "disk image '$path' already exists\n" if -e
$path;
611 if ($fmt eq 'subvol') {
612 # only allow this if size = 0, so that user knows what he is doing
613 die "storage does not support subvol quotas\n" if $size != 0;
615 my $old_umask = umask(0022);
617 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
621 my $cmd = ['/usr/bin/qemu-img', 'create'];
623 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
625 push @$cmd, '-f', $fmt, $path, "${size}K";
627 run_command
($cmd, errmsg
=> "unable to create image");
630 return "$vmid/$name";
634 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
636 my $path = $class->filesystem_path($scfg, $volname);
639 # try to remove immutable flag
640 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
644 if (defined($format) && ($format eq 'subvol')) {
645 File
::Path
::remove_tree
($path);
649 warn "disk image '$path' does not exists\n";
653 unlink($path) || die "unlink '$path' failed - $!\n";
660 my ($filename, $timeout) = @_;
663 return wantarray ?
(0, 'subvol', 0, undef) : 1;
666 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
674 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
676 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
678 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
680 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
682 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
686 $used *= 1024 if $u eq 'K';
687 $used *= (1024*1024) if $u eq 'M';
688 $used *= (1024*1024*1024) if $u eq 'G';
689 $used *= (1024*1024*1024*1024) if $u eq 'T';
696 return wantarray ?
($size, $format, $used, $parent) : $size;
699 sub volume_size_info
{
700 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
701 my $path = $class->filesystem_path($scfg, $volname);
702 return file_size_info
($path, $timeout);
707 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
709 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
711 return 1 if $running;
713 my $path = $class->filesystem_path($scfg, $volname);
715 my $format = ($class->parse_volname($volname))[6];
717 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
719 run_command
($cmd, timeout
=> 10);
724 sub volume_snapshot
{
725 my ($class, $scfg, $storeid, $volname, $snap) = @_;
727 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
729 my $path = $class->filesystem_path($scfg, $volname);
731 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
738 sub volume_rollback_is_possible
{
739 my ($class, $scfg, $storeid, $volname, $snap) = @_;
744 sub volume_snapshot_rollback
{
745 my ($class, $scfg, $storeid, $volname, $snap) = @_;
747 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
749 my $path = $class->filesystem_path($scfg, $volname);
751 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
758 sub volume_snapshot_delete
{
759 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
761 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
763 return 1 if $running;
765 my $path = $class->filesystem_path($scfg, $volname);
767 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
769 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
776 sub storage_can_replicate
{
777 my ($class, $scfg, $storeid, $format) = @_;
782 sub volume_has_feature
{
783 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
786 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
787 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
788 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
789 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
790 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
791 snap
=> {qcow2
=> 1} },
792 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
793 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
796 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
797 $class->parse_volname($volname);
803 $key = $isBase ?
'base' : 'current';
806 return 1 if defined($features->{$feature}->{$key}->{$format});
812 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
814 my $imagedir = $class->get_subdir($scfg, 'images');
816 my ($defFmt, $vaidFmts) = default_format
($scfg);
817 my $fmts = join ('|', @$vaidFmts);
821 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
823 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
829 next if !$vollist && defined($vmid) && ($owner ne $vmid);
831 my ($size, $format, $used, $parent) = file_size_info
($fn);
832 next if !($format && defined($size));
835 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
836 my ($basevmid, $basename) = ($1, $2);
837 $volid = "$storeid:$basevmid/$basename/$owner/$name";
839 $volid = "$storeid:$owner/$name";
843 my $found = grep { $_ eq $volid } @$vollist;
848 volid
=> $volid, format
=> $format,
849 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
857 my ($class, $storeid, $scfg, $cache) = @_;
859 my $path = $scfg->{path
};
861 die "storage definintion has no path\n" if !$path;
864 my $res = PVE
::Tools
::df
($path, $timeout);
866 return undef if !$res || !$res->{total
};
868 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
871 sub volume_snapshot_list
{
872 my ($class, $scfg, $storeid, $volname) = @_;
874 # implement in subclass
875 die "Volume_snapshot_list is not implemented for $class";
877 # return an empty array if dataset does not exist.
880 sub activate_storage
{
881 my ($class, $storeid, $scfg, $cache) = @_;
883 my $path = $scfg->{path
};
885 die "storage definintion has no path\n" if !$path;
887 # this path test may hang indefinitely on unresponsive mounts
889 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
890 die "unable to activate storage '$storeid' - " .
891 "directory '$path' does not exist or is unreachable\n";
895 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
897 if (defined($scfg->{content
})) {
898 foreach my $vtype (keys %$vtype_subdirs) {
899 # OpenVZMigrate uses backup (dump) dir
900 if (defined($scfg->{content
}->{$vtype}) ||
901 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
902 my $subdir = $class->get_subdir($scfg, $vtype);
903 mkpath
$subdir if $subdir ne $path;
909 sub deactivate_storage
{
910 my ($class, $storeid, $scfg, $cache) = @_;
912 # do nothing by default
915 sub activate_volume
{
916 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
918 my $path = $class->filesystem_path($scfg, $volname, $snapname);
920 # check is volume exists
922 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
924 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
928 sub deactivate_volume
{
929 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
931 # do nothing by default
934 sub check_connection
{
935 my ($class, $storeid, $scfg) = @_;
936 # do nothing by default
940 # Import/Export interface:
941 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
942 # the default implementations will return this if $scfg->{path} is set,
943 # mimicking the old PVE::Storage::storage_migrate() function.
945 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
946 # functions in case the format doesn't match their specialized
947 # implementations to reuse the raw/tar code.
949 # Format specification:
950 # The following formats are all prefixed with image information in the form
951 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
952 # to preallocate the image on storages which require it.
954 # raw+size: (image files only)
955 # A raw binary data stream such as produced via `dd if=TheImageFile`.
956 # qcow2+size, vmdk: (image files only)
957 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
958 # files which are already in qcow2 format, or via `qemu-img convert`.
959 # Note that these formats are only valid with $with_snapshots being true.
960 # tar+size: (subvolumes only)
961 # A GNU tar stream containing just the inner contents of the subvolume.
962 # This does not distinguish between the contents of a privileged or
963 # unprivileged container. In other words, this is from the root user
964 # namespace's point of view with no uid-mapping in effect.
965 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
967 # Plugins may reuse these helpers. Changes to the header format should be
968 # reflected by changes to the function prototypes.
969 sub write_common_header
($$) {
970 my ($fh, $image_size_in_bytes) = @_;
971 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
974 sub read_common_header
($) {
976 sysread($fh, my $size, 8);
977 $size = unpack('Q<', $size);
978 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
983 # Export a volume into a file handle as a stream of desired format.
985 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
986 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
987 my $file = $class->path($scfg, $volname, $storeid)
989 my ($size, $file_format) = file_size_info
($file);
991 if ($format eq 'raw+size') {
992 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
993 write_common_header
($fh, $size);
994 if ($file_format eq 'raw') {
995 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
997 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
998 output
=> '>&'.fileno($fh));
1001 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1002 my $data_format = $1;
1003 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1004 write_common_header
($fh, $size);
1005 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1007 } elsif ($format eq 'tar+size') {
1008 goto unsupported
if $file_format ne 'subvol';
1009 write_common_header
($fh, $size);
1010 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1011 output
=> '>&'.fileno($fh));
1016 die "volume export format $format not available for $class";
1019 sub volume_export_formats
{
1020 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1021 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1022 my $file = $class->path($scfg, $volname, $storeid)
1024 my ($size, $format) = file_size_info
($file);
1026 if ($with_snapshots) {
1027 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1030 return ('tar+size') if $format eq 'subvol';
1031 return ('raw+size');
1036 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1038 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1040 die "volume import format '$format' not available for $class\n"
1041 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1042 my $data_format = $1;
1044 die "format $format cannot be imported without snapshots\n"
1045 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1046 die "format $format cannot be imported with snapshots\n"
1047 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1049 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1050 $class->parse_volname($volname);
1052 # XXX: Should we bother with conversion routines at this level? This won't
1053 # happen without manual CLI usage, so for now we just error out...
1054 die "cannot import format $format into a file of format $file_format\n"
1055 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1057 # Check for an existing file first since interrupting alloc_image doesn't
1059 my $file = $class->path($scfg, $volname, $storeid);
1060 die "file '$file' already exists\n" if -e
$file;
1062 my ($size) = read_common_header
($fh);
1063 $size = int($size/1024);
1066 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1067 if ($allocname ne $volname) {
1068 my $oldname = $volname;
1069 $volname = $allocname; # Let the cleanup code know what to free
1070 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1072 my $file = $class->path($scfg, $volname, $storeid)
1073 or die "internal error: failed to get path to newly allocated volume $volname\n";
1074 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1075 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1076 input
=> '<&'.fileno($fh));
1077 } elsif ($data_format eq 'tar') {
1078 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1079 input
=> '<&'.fileno($fh));
1081 die "volume import format '$format' not available for $class";
1085 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1091 sub volume_import_formats
{
1092 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1093 if ($scfg->{path
} && !defined($base_snapshot)) {
1094 my $format = ($class->parse_volname($volname))[6];
1095 if ($with_snapshots) {
1096 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1099 return ('tar+size') if $format eq 'subvol';
1100 return ('raw+size');