1 package PVE
::Storage
::Plugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::Cluster
qw(cfs_register_file);
13 use base
qw(PVE::SectionConfig);
15 cfs_register_file
('storage.cfg',
16 sub { __PACKAGE__-
>parse_config(@_); },
17 sub { __PACKAGE__-
>write_config(@_); });
22 type
=> { description
=> "Storage type." },
23 storage
=> get_standard_option
('pve-storage-id',
24 { completion
=> \
&PVE
::Storage
::complete_storage
}),
25 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
27 description
=> "Allowed content types.\n\nNOTE: the value " .
28 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
29 type
=> 'string', format
=> 'pve-storage-content-list',
31 completion
=> \
&PVE
::Storage
::complete_content_type
,
34 description
=> "Flag to disable the storage.",
39 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
45 description
=> "Mark storage as shared.",
50 description
=> "Default image format.",
51 type
=> 'string', format
=> 'pve-storage-format',
57 sub content_hash_to_string
{
61 foreach my $ct (keys %$hash) {
62 push @cta, $ct if $hash->{$ct};
65 return join(',', @cta);
68 sub valid_content_types
{
71 my $def = $defaultData->{plugindata
}->{$type};
75 return $def->{content
}->[0];
81 my $type = $scfg->{type
};
82 my $def = $defaultData->{plugindata
}->{$type};
84 my $def_format = 'raw';
85 my $valid_formats = [ $def_format ];
87 if (defined($def->{format
})) {
88 $def_format = $scfg->{format
} || $def->{format
}->[1];
89 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
92 return wantarray ?
($def_format, $valid_formats) : $def_format;
95 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
97 my ($path, $noerr) = @_;
99 # fixme: exclude more shell meta characters?
100 # we need absolute paths
101 if ($path !~ m
|^/[^;\
(\
)]+|) {
102 return undef if $noerr;
103 die "value does not look like a valid absolute path\n";
108 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
110 my ($server, $noerr) = @_;
112 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
113 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
115 return undef if $noerr;
116 die "value does not look like a valid server name or IP address\n";
121 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
123 my ($name, $noerr) = @_;
125 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
126 return undef if $noerr;
127 die "lvm name '$name' contains illegal characters\n";
133 # fixme: do we need this
134 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
136 # my ($portal, $noerr) = @_;
138 # # IP with optional port
139 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
140 # return undef if $noerr;
141 # die "value does not look like a valid portal address\n";
146 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
147 sub verify_portal_dns
{
148 my ($portal, $noerr) = @_;
150 # IP or DNS name with optional port
151 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
152 return undef if $noerr;
153 die "value does not look like a valid portal address\n";
158 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
160 my ($ct, $noerr) = @_;
162 my $valid_content = valid_content_types
('dir'); # dir includes all types
164 if (!$valid_content->{$ct}) {
165 return undef if $noerr;
166 die "invalid content type '$ct'\n";
172 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
174 my ($fmt, $noerr) = @_;
176 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
177 return undef if $noerr;
178 die "invalid format '$fmt'\n";
184 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
186 my ($value, $noerr) = @_;
188 # mount options (see man fstab)
189 if ($value !~ m/^\S+$/) {
190 return undef if $noerr;
191 die "invalid options '$value'\n";
197 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
198 sub parse_volume_id
{
199 my ($volid, $noerr) = @_;
201 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
202 return wantarray ?
($1, $2) : $1;
204 return undef if $noerr;
205 die "unable to parse volume ID '$volid'\n";
213 sub parse_section_header
{
214 my ($class, $line) = @_;
216 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
217 my ($type, $storeid) = (lc($1), $2);
218 my $errmsg = undef; # set if you want to skip whole section
219 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
221 my $config = {}; # to return additional attributes
222 return ($type, $storeid, $errmsg, $config);
228 my ($class, $type, $key, $value) = @_;
230 my $def = $defaultData->{plugindata
}->{$type};
232 if ($key eq 'content') {
233 my $valid_content = $def->{content
}->[0];
237 foreach my $c (PVE
::Tools
::split_list
($value)) {
238 if (!$valid_content->{$c}) {
239 warn "storage does not support content type '$c'\n";
245 if ($res->{none
} && scalar (keys %$res) > 1) {
246 die "unable to combine 'none' with other content types\n";
250 } elsif ($key eq 'format') {
251 my $valid_formats = $def->{format
}->[0];
253 if (!$valid_formats->{$value}) {
254 warn "storage does not support format '$value'\n";
259 } elsif ($key eq 'nodes') {
262 foreach my $node (PVE
::Tools
::split_list
($value)) {
263 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
269 # no node restrictions for local storage
270 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
271 # die "storage '$storeid' does not allow node restrictions\n";
281 my ($class, $type, $key, $value) = @_;
283 if ($key eq 'nodes') {
284 return join(',', keys(%$value));
285 } elsif ($key eq 'content') {
286 my $res = content_hash_to_string
($value) || 'none';
294 my ($class, $filename, $raw) = @_;
296 my $cfg = $class->SUPER::parse_config
($filename, $raw);
297 my $ids = $cfg->{ids
};
299 # make sure we have a reasonable 'local:' storage
300 # we want 'local' to be always the same 'type' (on all cluster nodes)
301 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
302 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
305 priority
=> 0, # force first entry
306 path
=> '/var/lib/vz',
308 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
312 # make sure we have a path
313 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
315 # remove node restrictions for local storage
316 delete($ids->{local}->{nodes
});
318 foreach my $storeid (keys %$ids) {
319 my $d = $ids->{$storeid};
320 my $type = $d->{type
};
322 my $def = $defaultData->{plugindata
}->{$type};
324 if ($def->{content
}) {
325 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
328 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') {
336 # Storage implementation
338 sub cluster_lock_storage
{
339 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
343 my $lockid = "pve-storage-$storeid";
344 my $lockdir = "/var/lock/pve-manager";
346 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
349 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
358 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
359 return ($1, $3, $2); # (name, format, isBase)
362 die "unable to parse volume filename '$name'\n";
366 my ($class, $volname) = @_;
368 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
369 my ($basedvmid, $basename) = ($1, $2);
370 parse_name_dir
($basename);
371 my ($vmid, $name) = ($3, $4);
372 my (undef, $format, $isBase) = parse_name_dir
($name);
373 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
374 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
375 my ($vmid, $name) = ($1, $2);
376 my (undef, $format, $isBase) = parse_name_dir
($name);
377 return ('images', $name, $vmid, undef, undef, $isBase, $format);
378 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
380 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
381 return ('vztmpl', $1);
382 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
383 return ('rootdir', $1, $1);
384 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
386 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
387 return ('backup', $fn, $2);
389 return ('backup', $fn);
392 die "unable to parse directory volume name '$volname'\n";
395 my $vtype_subdirs = {
397 rootdir
=> 'private',
398 iso
=> 'template/iso',
399 vztmpl
=> 'template/cache',
404 my ($class, $scfg, $vtype) = @_;
406 my $path = $scfg->{path
};
408 die "storage definintion has no path\n" if !$path;
410 my $subdir = $vtype_subdirs->{$vtype};
412 die "unknown vtype '$vtype'\n" if !defined($subdir);
414 return "$path/$subdir";
417 sub filesystem_path
{
418 my ($class, $scfg, $volname, $snapname) = @_;
420 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
421 $class->parse_volname($volname);
423 # Note: qcow2/qed has internal snapshot, so path is always
424 # the same (with or without snapshot => same file).
425 die "can't snapshot this image format\n"
426 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
428 my $dir = $class->get_subdir($scfg, $vtype);
430 $dir .= "/$vmid" if $vtype eq 'images';
432 my $path = "$dir/$name";
434 return wantarray ?
($path, $vmid, $vtype) : $path;
438 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
440 return $class->filesystem_path($scfg, $volname, $snapname);
444 my ($class, $storeid, $scfg, $volname) = @_;
446 # this only works for file based storage types
447 die "storage definition has no path\n" if !$scfg->{path
};
449 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
450 $class->parse_volname($volname);
452 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
454 die "create_base not possible with base image\n" if $isBase;
456 my $path = $class->filesystem_path($scfg, $volname);
458 my ($size, undef, $used, $parent) = file_size_info
($path);
459 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
461 die "volname '$volname' contains wrong information about parent\n"
462 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
465 $newname =~ s/^vm-/base-/;
467 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
470 my $newpath = $class->filesystem_path($scfg, $newvolname);
472 die "file '$newpath' already exists\n" if -f
$newpath;
474 rename($path, $newpath) ||
475 die "rename '$path' to '$newpath' failed - $!\n";
477 # We try to protect base volume
479 chmod(0444, $newpath); # nobody should write anything
481 # also try to set immutable flag
482 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
488 my $find_free_diskname = sub {
489 my ($imgdir, $vmid, $fmt) = @_;
492 PVE
::Tools
::dir_glob_foreach
($imgdir,
493 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
495 my ($fn, $type, $disk) = @_;
496 $disk_ids->{$disk} = 1;
499 for (my $i = 1; $i < 100; $i++) {
500 if (!$disk_ids->{$i}) {
501 return "vm-$vmid-disk-$i.$fmt";
505 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
509 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
511 # this only works for file based storage types
512 die "storage definintion has no path\n" if !$scfg->{path
};
514 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
515 $class->parse_volname($volname);
517 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
519 die "this storage type does not support clone_image on snapshot\n" if $snap;
521 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
523 die "clone_image only works on base images\n" if !$isBase;
525 my $imagedir = $class->get_subdir($scfg, 'images');
526 $imagedir .= "/$vmid";
530 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
532 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
534 my $newvol = "$basevmid/$basename/$vmid/$name";
536 my $path = $class->filesystem_path($scfg, $newvol);
538 # Note: we use relative paths, so we need to call chdir before qemu-img
540 local $CWD = $imagedir;
542 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
543 '-f', 'qcow2', $path];
555 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
557 my $imagedir = $class->get_subdir($scfg, 'images');
558 $imagedir .= "/$vmid";
562 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
564 my (undef, $tmpfmt) = parse_name_dir
($name);
566 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
569 my $path = "$imagedir/$name";
571 die "disk image '$path' already exists\n" if -e
$path;
573 if ($fmt eq 'subvol') {
574 # only allow this if size = 0, so that user knows what he is doing
575 die "storage does not support subvol quotas\n" if $size != 0;
577 my $old_umask = umask(0022);
579 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
583 my $cmd = ['/usr/bin/qemu-img', 'create'];
585 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
587 push @$cmd, '-f', $fmt, $path, "${size}K";
589 run_command
($cmd, errmsg
=> "unable to create image");
592 return "$vmid/$name";
596 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
598 my $path = $class->filesystem_path($scfg, $volname);
601 # try to remove immutable flag
602 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
606 if (defined($format) && ($format eq 'subvol')) {
607 File
::Path
::remove_tree
($path);
611 warn "disk image '$path' does not exists\n";
615 unlink($path) || die "unlink '$path' failed - $!\n";
622 my ($filename, $timeout) = @_;
625 return wantarray ?
(0, 'subvol', 0, undef) : 1;
628 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
636 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
638 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
640 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
642 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
644 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
648 $used *= 1024 if $u eq 'K';
649 $used *= (1024*1024) if $u eq 'M';
650 $used *= (1024*1024*1024) if $u eq 'G';
651 $used *= (1024*1024*1024*1024) if $u eq 'T';
658 return wantarray ?
($size, $format, $used, $parent) : $size;
661 sub volume_size_info
{
662 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
663 my $path = $class->filesystem_path($scfg, $volname);
664 return file_size_info
($path, $timeout);
669 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
671 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
673 return 1 if $running;
675 my $path = $class->filesystem_path($scfg, $volname);
677 my $format = ($class->parse_volname($volname))[6];
679 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
681 run_command
($cmd, timeout
=> 10);
686 sub volume_snapshot
{
687 my ($class, $scfg, $storeid, $volname, $snap) = @_;
689 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
691 my $path = $class->filesystem_path($scfg, $volname);
693 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
700 sub volume_rollback_is_possible
{
701 my ($class, $scfg, $storeid, $volname, $snap) = @_;
706 sub volume_snapshot_rollback
{
707 my ($class, $scfg, $storeid, $volname, $snap) = @_;
709 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
711 my $path = $class->filesystem_path($scfg, $volname);
713 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
720 sub volume_snapshot_delete
{
721 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
723 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
725 return 1 if $running;
727 my $path = $class->filesystem_path($scfg, $volname);
729 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
731 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
738 sub volume_has_feature
{
739 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
742 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
743 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
744 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
745 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
746 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
747 snap
=> {qcow2
=> 1} },
748 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
749 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
752 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
753 $class->parse_volname($volname);
759 $key = $isBase ?
'base' : 'current';
762 return 1 if defined($features->{$feature}->{$key}->{$format});
768 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
770 my $imagedir = $class->get_subdir($scfg, 'images');
772 my ($defFmt, $vaidFmts) = default_format
($scfg);
773 my $fmts = join ('|', @$vaidFmts);
777 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
779 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
785 next if !$vollist && defined($vmid) && ($owner ne $vmid);
787 my ($size, $format, $used, $parent) = file_size_info
($fn);
788 next if !($format && defined($size));
791 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
792 my ($basevmid, $basename) = ($1, $2);
793 $volid = "$storeid:$basevmid/$basename/$owner/$name";
795 $volid = "$storeid:$owner/$name";
799 my $found = grep { $_ eq $volid } @$vollist;
804 volid
=> $volid, format
=> $format,
805 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
813 my ($class, $storeid, $scfg, $cache) = @_;
815 my $path = $scfg->{path
};
817 die "storage definintion has no path\n" if !$path;
820 my $res = PVE
::Tools
::df
($path, $timeout);
822 return undef if !$res || !$res->{total
};
824 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
827 sub volume_snapshot_list
{
828 my ($class, $scfg, $storeid, $volname) = @_;
830 # implement in subclass
831 die "Volume_snapshot_list is not implemented for $class";
833 # return an empty array if dataset does not exist.
836 sub activate_storage
{
837 my ($class, $storeid, $scfg, $cache) = @_;
839 my $path = $scfg->{path
};
841 die "storage definintion has no path\n" if !$path;
843 die "unable to activate storage '$storeid' - " .
844 "directory '$path' does not exist\n" if ! -d
$path;
846 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
848 if (defined($scfg->{content
})) {
849 foreach my $vtype (keys %$vtype_subdirs) {
850 # OpenVZMigrate uses backup (dump) dir
851 if (defined($scfg->{content
}->{$vtype}) ||
852 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
853 my $subdir = $class->get_subdir($scfg, $vtype);
854 mkpath
$subdir if $subdir ne $path;
860 sub deactivate_storage
{
861 my ($class, $storeid, $scfg, $cache) = @_;
863 # do nothing by default
866 sub activate_volume
{
867 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
869 my $path = $class->filesystem_path($scfg, $volname, $snapname);
871 # check is volume exists
873 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
875 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
879 sub deactivate_volume
{
880 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
882 # do nothing by default
885 sub check_connection
{
886 my ($class, $storeid, $scfg) = @_;
887 # do nothing by default
891 # Import/Export interface:
892 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
893 # the default implementations will return this if $scfg->{path} is set,
894 # mimicking the old PVE::Storage::storage_migrate() function.
896 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
897 # functions in case the format doesn't match their specialized
898 # implementations to reuse the raw/tar code.
900 # Format specification:
901 # The following formats are all prefixed with image information in the form
902 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
903 # to preallocate the image on storages which require it.
905 # raw+size: (image files only)
906 # A raw binary data stream such as produced via `dd if=TheImageFile`.
907 # qcow2+size, vmdk: (image files only)
908 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
909 # files which are already in qcow2 format, or via `qemu-img convert`.
910 # Note that these formats are only valid with $with_snapshots being true.
911 # tar+size: (subvolumes only)
912 # A GNU tar stream with the inner contents of the subvolume put into the
913 # 'subvol/' directory.
915 # Plugins may reuse these helpers. Changes to the header format should be
916 # reflected by changes to the function prototypes.
917 sub write_common_header
($$) {
918 my ($fh, $image_size_in_bytes) = @_;
919 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
922 sub read_common_header
($) {
924 sysread($fh, my $size, 8);
925 $size = unpack('Q<', $size);
926 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
931 # Export a volume into a file handle as a stream of desired format.
933 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
934 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
935 my $file = $class->path($scfg, $volname, $storeid)
937 my ($size, $file_format) = file_size_info
($file);
939 if ($format eq 'raw+size') {
940 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
941 write_common_header
($fh, $size);
942 if ($file_format eq 'raw') {
943 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
945 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
946 output
=> '>&'.fileno($fh));
949 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
950 my $data_format = $1;
951 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
952 write_common_header
($fh, $size);
953 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
955 } elsif ($format eq 'tar+size') {
956 goto unsupported
if $file_format ne 'subvol';
957 write_common_header
($fh, $size);
958 run_command
(['tar', '--xform=s,^\./,subvol/,S', '-cf', '-', '-C', $file, '.'],
959 output
=> '>&'.fileno($fh));
964 die "volume export format $format not available for $class";
967 sub volume_export_formats
{
968 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
969 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
970 my $file = $class->path($scfg, $volname, $storeid)
972 my ($size, $format) = file_size_info
($file);
974 if ($with_snapshots) {
975 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
978 return ('tar+size') if $format eq 'subvol';
984 # Import data from a stream, creating a new or replacing or adding to an existing volume.
986 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
988 die "volume import format '$format' not available for $class\n"
989 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
990 my $data_format = $1;
992 die "format $format cannot be imported without snapshots\n"
993 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
994 die "format $format cannot be imported with snapshots\n"
995 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
997 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
998 $class->parse_volname($volname);
1000 # XXX: Should we bother with conversion routines at this level? This won't
1001 # happen without manual CLI usage, so for now we just error out...
1002 die "cannot import format $format into a file of format $file_format\n"
1003 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1005 # Check for an existing file first since interrupting alloc_image doesn't
1007 my $file = $class->path($scfg, $volname, $storeid);
1008 die "file '$file' already exists\n" if -e
$file;
1010 my ($size) = read_common_header
($fh);
1011 $size = int($size/1024);
1014 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1015 if ($allocname ne $volname) {
1016 my $oldname = $volname;
1017 $volname = $allocname; # Let the cleanup code know what to free
1018 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1020 my $file = $class->path($scfg, $volname, $storeid)
1021 or die "internal error: failed to get path to newly allocated volume $volname\n";
1022 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1023 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1024 input
=> '<&'.fileno($fh));
1025 } elsif ($data_format eq 'tar') {
1026 run_command
(['tar', '-C', $file, '--xform=s,^subvol/,./,S', '-xf', '-'],
1027 input
=> '<&'.fileno($fh));
1029 die "volume import format '$format' not available for $class";
1033 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1039 sub volume_import_formats
{
1040 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1041 if ($scfg->{path
} && !defined($base_snapshot)) {
1042 my $format = ($class->parse_volname($volname))[6];
1043 if ($with_snapshots) {
1044 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1047 return ('tar+size') if $format eq 'subvol';
1048 return ('raw+size');