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 cfs_register_file
('storage.cfg',
23 sub { __PACKAGE__-
>parse_config(@_); },
24 sub { __PACKAGE__-
>write_config(@_); });
29 type
=> { description
=> "Storage type." },
30 storage
=> get_standard_option
('pve-storage-id',
31 { completion
=> \
&PVE
::Storage
::complete_storage
}),
32 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
34 description
=> "Allowed content types.\n\nNOTE: the value " .
35 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
36 type
=> 'string', format
=> 'pve-storage-content-list',
38 completion
=> \
&PVE
::Storage
::complete_content_type
,
41 description
=> "Flag to disable the storage.",
46 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
52 description
=> "Mark storage as shared.",
57 description
=> "Default image format.",
58 type
=> 'string', format
=> 'pve-storage-format',
64 sub content_hash_to_string
{
68 foreach my $ct (keys %$hash) {
69 push @cta, $ct if $hash->{$ct};
72 return join(',', @cta);
75 sub valid_content_types
{
78 my $def = $defaultData->{plugindata
}->{$type};
82 return $def->{content
}->[0];
88 my $type = $scfg->{type
};
89 my $def = $defaultData->{plugindata
}->{$type};
91 my $def_format = 'raw';
92 my $valid_formats = [ $def_format ];
94 if (defined($def->{format
})) {
95 $def_format = $scfg->{format
} || $def->{format
}->[1];
96 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
99 return wantarray ?
($def_format, $valid_formats) : $def_format;
102 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
104 my ($path, $noerr) = @_;
106 # fixme: exclude more shell meta characters?
107 # we need absolute paths
108 if ($path !~ m
|^/[^;\
(\
)]+|) {
109 return undef if $noerr;
110 die "value does not look like a valid absolute path\n";
115 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
117 my ($server, $noerr) = @_;
119 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
120 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
122 return undef if $noerr;
123 die "value does not look like a valid server name or IP address\n";
128 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
130 my ($name, $noerr) = @_;
132 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
133 return undef if $noerr;
134 die "lvm name '$name' contains illegal characters\n";
140 # fixme: do we need this
141 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
143 # my ($portal, $noerr) = @_;
145 # # IP with optional port
146 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
147 # return undef if $noerr;
148 # die "value does not look like a valid portal address\n";
153 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
154 sub verify_portal_dns
{
155 my ($portal, $noerr) = @_;
157 # IP or DNS name with optional port
158 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
159 return undef if $noerr;
160 die "value does not look like a valid portal address\n";
165 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
167 my ($ct, $noerr) = @_;
169 my $valid_content = valid_content_types
('dir'); # dir includes all types
171 if (!$valid_content->{$ct}) {
172 return undef if $noerr;
173 die "invalid content type '$ct'\n";
179 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
181 my ($fmt, $noerr) = @_;
183 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
184 return undef if $noerr;
185 die "invalid format '$fmt'\n";
191 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
193 my ($value, $noerr) = @_;
195 # mount options (see man fstab)
196 if ($value !~ m/^\S+$/) {
197 return undef if $noerr;
198 die "invalid options '$value'\n";
204 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
205 sub parse_volume_id
{
206 my ($volid, $noerr) = @_;
208 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
209 return wantarray ?
($1, $2) : $1;
211 return undef if $noerr;
212 die "unable to parse volume ID '$volid'\n";
220 sub parse_section_header
{
221 my ($class, $line) = @_;
223 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
224 my ($type, $storeid) = (lc($1), $2);
225 my $errmsg = undef; # set if you want to skip whole section
226 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
228 my $config = {}; # to return additional attributes
229 return ($type, $storeid, $errmsg, $config);
235 my ($class, $type, $key, $value) = @_;
237 my $def = $defaultData->{plugindata
}->{$type};
239 if ($key eq 'content') {
240 my $valid_content = $def->{content
}->[0];
244 foreach my $c (PVE
::Tools
::split_list
($value)) {
245 if (!$valid_content->{$c}) {
246 warn "storage does not support content type '$c'\n";
252 if ($res->{none
} && scalar (keys %$res) > 1) {
253 die "unable to combine 'none' with other content types\n";
257 } elsif ($key eq 'format') {
258 my $valid_formats = $def->{format
}->[0];
260 if (!$valid_formats->{$value}) {
261 warn "storage does not support format '$value'\n";
266 } elsif ($key eq 'nodes') {
269 foreach my $node (PVE
::Tools
::split_list
($value)) {
270 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
276 # no node restrictions for local storage
277 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
278 # die "storage '$storeid' does not allow node restrictions\n";
288 my ($class, $type, $key, $value) = @_;
290 if ($key eq 'nodes') {
291 return join(',', keys(%$value));
292 } elsif ($key eq 'content') {
293 my $res = content_hash_to_string
($value) || 'none';
301 my ($class, $filename, $raw) = @_;
303 my $cfg = $class->SUPER::parse_config
($filename, $raw);
304 my $ids = $cfg->{ids
};
306 # make sure we have a reasonable 'local:' storage
307 # we want 'local' to be always the same 'type' (on all cluster nodes)
308 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
309 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
312 priority
=> 0, # force first entry
313 path
=> '/var/lib/vz',
315 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
319 # make sure we have a path
320 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
322 # remove node restrictions for local storage
323 delete($ids->{local}->{nodes
});
325 foreach my $storeid (keys %$ids) {
326 my $d = $ids->{$storeid};
327 my $type = $d->{type
};
329 my $def = $defaultData->{plugindata
}->{$type};
331 if ($def->{content
}) {
332 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
335 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') {
343 # Storage implementation
345 sub cluster_lock_storage
{
346 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
350 my $lockid = "pve-storage-$storeid";
351 my $lockdir = "/var/lock/pve-manager";
353 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
356 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
365 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
366 return ($1, $3, $2); # (name, format, isBase)
369 die "unable to parse volume filename '$name'\n";
373 my ($class, $volname) = @_;
375 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
376 my ($basedvmid, $basename) = ($1, $2);
377 parse_name_dir
($basename);
378 my ($vmid, $name) = ($3, $4);
379 my (undef, $format, $isBase) = parse_name_dir
($name);
380 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
381 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
382 my ($vmid, $name) = ($1, $2);
383 my (undef, $format, $isBase) = parse_name_dir
($name);
384 return ('images', $name, $vmid, undef, undef, $isBase, $format);
385 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
387 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
388 return ('vztmpl', $1);
389 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
390 return ('rootdir', $1, $1);
391 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
393 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
394 return ('backup', $fn, $2);
396 return ('backup', $fn);
399 die "unable to parse directory volume name '$volname'\n";
402 my $vtype_subdirs = {
404 rootdir
=> 'private',
405 iso
=> 'template/iso',
406 vztmpl
=> 'template/cache',
411 my ($class, $scfg, $vtype) = @_;
413 my $path = $scfg->{path
};
415 die "storage definintion has no path\n" if !$path;
417 my $subdir = $vtype_subdirs->{$vtype};
419 die "unknown vtype '$vtype'\n" if !defined($subdir);
421 return "$path/$subdir";
424 sub filesystem_path
{
425 my ($class, $scfg, $volname, $snapname) = @_;
427 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
428 $class->parse_volname($volname);
430 # Note: qcow2/qed has internal snapshot, so path is always
431 # the same (with or without snapshot => same file).
432 die "can't snapshot this image format\n"
433 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
435 my $dir = $class->get_subdir($scfg, $vtype);
437 $dir .= "/$vmid" if $vtype eq 'images';
439 my $path = "$dir/$name";
441 return wantarray ?
($path, $vmid, $vtype) : $path;
445 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
447 return $class->filesystem_path($scfg, $volname, $snapname);
451 my ($class, $storeid, $scfg, $volname) = @_;
453 # this only works for file based storage types
454 die "storage definition has no path\n" if !$scfg->{path
};
456 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
457 $class->parse_volname($volname);
459 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
461 die "create_base not possible with base image\n" if $isBase;
463 my $path = $class->filesystem_path($scfg, $volname);
465 my ($size, undef, $used, $parent) = file_size_info
($path);
466 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
468 die "volname '$volname' contains wrong information about parent\n"
469 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
472 $newname =~ s/^vm-/base-/;
474 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
477 my $newpath = $class->filesystem_path($scfg, $newvolname);
479 die "file '$newpath' already exists\n" if -f
$newpath;
481 rename($path, $newpath) ||
482 die "rename '$path' to '$newpath' failed - $!\n";
484 # We try to protect base volume
486 chmod(0444, $newpath); # nobody should write anything
488 # also try to set immutable flag
489 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
495 my $find_free_diskname = sub {
496 my ($imgdir, $vmid, $fmt) = @_;
499 PVE
::Tools
::dir_glob_foreach
($imgdir,
500 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
502 my ($fn, $type, $disk) = @_;
503 $disk_ids->{$disk} = 1;
506 for (my $i = 1; $i < 100; $i++) {
507 if (!$disk_ids->{$i}) {
508 return "vm-$vmid-disk-$i.$fmt";
512 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
516 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
518 # this only works for file based storage types
519 die "storage definintion has no path\n" if !$scfg->{path
};
521 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
522 $class->parse_volname($volname);
524 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
526 die "this storage type does not support clone_image on snapshot\n" if $snap;
528 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
530 die "clone_image only works on base images\n" if !$isBase;
532 my $imagedir = $class->get_subdir($scfg, 'images');
533 $imagedir .= "/$vmid";
537 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
539 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
541 my $newvol = "$basevmid/$basename/$vmid/$name";
543 my $path = $class->filesystem_path($scfg, $newvol);
545 # Note: we use relative paths, so we need to call chdir before qemu-img
547 local $CWD = $imagedir;
549 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
550 '-f', 'qcow2', $path];
562 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
564 my $imagedir = $class->get_subdir($scfg, 'images');
565 $imagedir .= "/$vmid";
569 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
571 my (undef, $tmpfmt) = parse_name_dir
($name);
573 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
576 my $path = "$imagedir/$name";
578 die "disk image '$path' already exists\n" if -e
$path;
580 if ($fmt eq 'subvol') {
581 # only allow this if size = 0, so that user knows what he is doing
582 die "storage does not support subvol quotas\n" if $size != 0;
584 my $old_umask = umask(0022);
586 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
590 my $cmd = ['/usr/bin/qemu-img', 'create'];
592 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
594 push @$cmd, '-f', $fmt, $path, "${size}K";
596 run_command
($cmd, errmsg
=> "unable to create image");
599 return "$vmid/$name";
603 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
605 my $path = $class->filesystem_path($scfg, $volname);
608 # try to remove immutable flag
609 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
613 if (defined($format) && ($format eq 'subvol')) {
614 File
::Path
::remove_tree
($path);
618 warn "disk image '$path' does not exists\n";
622 unlink($path) || die "unlink '$path' failed - $!\n";
629 my ($filename, $timeout) = @_;
632 return wantarray ?
(0, 'subvol', 0, undef) : 1;
635 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
643 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
645 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
647 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
649 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
651 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
655 $used *= 1024 if $u eq 'K';
656 $used *= (1024*1024) if $u eq 'M';
657 $used *= (1024*1024*1024) if $u eq 'G';
658 $used *= (1024*1024*1024*1024) if $u eq 'T';
665 return wantarray ?
($size, $format, $used, $parent) : $size;
668 sub volume_size_info
{
669 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
670 my $path = $class->filesystem_path($scfg, $volname);
671 return file_size_info
($path, $timeout);
676 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
678 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
680 return 1 if $running;
682 my $path = $class->filesystem_path($scfg, $volname);
684 my $format = ($class->parse_volname($volname))[6];
686 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
688 run_command
($cmd, timeout
=> 10);
693 sub volume_snapshot
{
694 my ($class, $scfg, $storeid, $volname, $snap) = @_;
696 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
698 my $path = $class->filesystem_path($scfg, $volname);
700 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
707 sub volume_rollback_is_possible
{
708 my ($class, $scfg, $storeid, $volname, $snap) = @_;
713 sub volume_snapshot_rollback
{
714 my ($class, $scfg, $storeid, $volname, $snap) = @_;
716 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
718 my $path = $class->filesystem_path($scfg, $volname);
720 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
727 sub volume_snapshot_delete
{
728 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
730 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
732 return 1 if $running;
734 my $path = $class->filesystem_path($scfg, $volname);
736 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
738 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
745 sub storage_can_replicate
{
746 my ($class, $scfg, $storeid, $format) = @_;
751 sub volume_has_feature
{
752 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
755 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
756 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
757 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
758 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
759 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
760 snap
=> {qcow2
=> 1} },
761 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
762 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
765 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
766 $class->parse_volname($volname);
772 $key = $isBase ?
'base' : 'current';
775 return 1 if defined($features->{$feature}->{$key}->{$format});
781 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
783 my $imagedir = $class->get_subdir($scfg, 'images');
785 my ($defFmt, $vaidFmts) = default_format
($scfg);
786 my $fmts = join ('|', @$vaidFmts);
790 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
792 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
798 next if !$vollist && defined($vmid) && ($owner ne $vmid);
800 my ($size, $format, $used, $parent) = file_size_info
($fn);
801 next if !($format && defined($size));
804 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
805 my ($basevmid, $basename) = ($1, $2);
806 $volid = "$storeid:$basevmid/$basename/$owner/$name";
808 $volid = "$storeid:$owner/$name";
812 my $found = grep { $_ eq $volid } @$vollist;
817 volid
=> $volid, format
=> $format,
818 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
826 my ($class, $storeid, $scfg, $cache) = @_;
828 my $path = $scfg->{path
};
830 die "storage definintion has no path\n" if !$path;
833 my $res = PVE
::Tools
::df
($path, $timeout);
835 return undef if !$res || !$res->{total
};
837 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
840 sub volume_snapshot_list
{
841 my ($class, $scfg, $storeid, $volname) = @_;
843 # implement in subclass
844 die "Volume_snapshot_list is not implemented for $class";
846 # return an empty array if dataset does not exist.
849 sub activate_storage
{
850 my ($class, $storeid, $scfg, $cache) = @_;
852 my $path = $scfg->{path
};
854 die "storage definintion has no path\n" if !$path;
856 die "unable to activate storage '$storeid' - " .
857 "directory '$path' does not exist\n" if ! -d
$path;
859 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
861 if (defined($scfg->{content
})) {
862 foreach my $vtype (keys %$vtype_subdirs) {
863 # OpenVZMigrate uses backup (dump) dir
864 if (defined($scfg->{content
}->{$vtype}) ||
865 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
866 my $subdir = $class->get_subdir($scfg, $vtype);
867 mkpath
$subdir if $subdir ne $path;
873 sub deactivate_storage
{
874 my ($class, $storeid, $scfg, $cache) = @_;
876 # do nothing by default
879 sub activate_volume
{
880 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
882 my $path = $class->filesystem_path($scfg, $volname, $snapname);
884 # check is volume exists
886 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
888 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
892 sub deactivate_volume
{
893 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
895 # do nothing by default
898 sub check_connection
{
899 my ($class, $storeid, $scfg) = @_;
900 # do nothing by default
904 # Import/Export interface:
905 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
906 # the default implementations will return this if $scfg->{path} is set,
907 # mimicking the old PVE::Storage::storage_migrate() function.
909 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
910 # functions in case the format doesn't match their specialized
911 # implementations to reuse the raw/tar code.
913 # Format specification:
914 # The following formats are all prefixed with image information in the form
915 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
916 # to preallocate the image on storages which require it.
918 # raw+size: (image files only)
919 # A raw binary data stream such as produced via `dd if=TheImageFile`.
920 # qcow2+size, vmdk: (image files only)
921 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
922 # files which are already in qcow2 format, or via `qemu-img convert`.
923 # Note that these formats are only valid with $with_snapshots being true.
924 # tar+size: (subvolumes only)
925 # A GNU tar stream containing just the inner contents of the subvolume.
926 # This does not distinguish between the contents of a privileged or
927 # unprivileged container. In other words, this is from the root user
928 # namespace's point of view with no uid-mapping in effect.
929 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
931 # Plugins may reuse these helpers. Changes to the header format should be
932 # reflected by changes to the function prototypes.
933 sub write_common_header
($$) {
934 my ($fh, $image_size_in_bytes) = @_;
935 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
938 sub read_common_header
($) {
940 sysread($fh, my $size, 8);
941 $size = unpack('Q<', $size);
942 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
947 # Export a volume into a file handle as a stream of desired format.
949 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
950 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
951 my $file = $class->path($scfg, $volname, $storeid)
953 my ($size, $file_format) = file_size_info
($file);
955 if ($format eq 'raw+size') {
956 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
957 write_common_header
($fh, $size);
958 if ($file_format eq 'raw') {
959 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
961 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
962 output
=> '>&'.fileno($fh));
965 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
966 my $data_format = $1;
967 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
968 write_common_header
($fh, $size);
969 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
971 } elsif ($format eq 'tar+size') {
972 goto unsupported
if $file_format ne 'subvol';
973 write_common_header
($fh, $size);
974 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
975 output
=> '>&'.fileno($fh));
980 die "volume export format $format not available for $class";
983 sub volume_export_formats
{
984 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
985 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
986 my $file = $class->path($scfg, $volname, $storeid)
988 my ($size, $format) = file_size_info
($file);
990 if ($with_snapshots) {
991 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
994 return ('tar+size') if $format eq 'subvol';
1000 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1002 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1004 die "volume import format '$format' not available for $class\n"
1005 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1006 my $data_format = $1;
1008 die "format $format cannot be imported without snapshots\n"
1009 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1010 die "format $format cannot be imported with snapshots\n"
1011 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1013 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1014 $class->parse_volname($volname);
1016 # XXX: Should we bother with conversion routines at this level? This won't
1017 # happen without manual CLI usage, so for now we just error out...
1018 die "cannot import format $format into a file of format $file_format\n"
1019 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1021 # Check for an existing file first since interrupting alloc_image doesn't
1023 my $file = $class->path($scfg, $volname, $storeid);
1024 die "file '$file' already exists\n" if -e
$file;
1026 my ($size) = read_common_header
($fh);
1027 $size = int($size/1024);
1030 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1031 if ($allocname ne $volname) {
1032 my $oldname = $volname;
1033 $volname = $allocname; # Let the cleanup code know what to free
1034 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1036 my $file = $class->path($scfg, $volname, $storeid)
1037 or die "internal error: failed to get path to newly allocated volume $volname\n";
1038 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1039 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1040 input
=> '<&'.fileno($fh));
1041 } elsif ($data_format eq 'tar') {
1042 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1043 input
=> '<&'.fileno($fh));
1045 die "volume import format '$format' not available for $class";
1049 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1055 sub volume_import_formats
{
1056 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1057 if ($scfg->{path
} && !defined($base_snapshot)) {
1058 my $format = ($class->parse_volname($volname))[6];
1059 if ($with_snapshots) {
1060 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1063 return ('tar+size') if $format eq 'subvol';
1064 return ('raw+size');