1 package PVE
::Storage
::Plugin
;
10 use PVE
::Tools
qw(run_command);
11 use PVE
::JSONSchema
qw(get_standard_option);
12 use PVE
::Cluster
qw(cfs_register_file);
14 use base
qw(PVE::SectionConfig);
16 our @COMMON_TAR_FLAGS = qw(
18 -p --sparse --numeric-owner --acls
19 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
20 --warning=no-file-ignored --warning=no-xattr-write
23 our @SHARED_STORAGE = (
35 our $MAX_VOLUMES_PER_GUEST = 1024;
37 cfs_register_file
('storage.cfg',
38 sub { __PACKAGE__-
>parse_config(@_); },
39 sub { __PACKAGE__-
>write_config(@_); });
44 type
=> { description
=> "Storage type." },
45 storage
=> get_standard_option
('pve-storage-id',
46 { completion
=> \
&PVE
::Storage
::complete_storage
}),
47 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
49 description
=> "Allowed content types.\n\nNOTE: the value " .
50 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
51 type
=> 'string', format
=> 'pve-storage-content-list',
53 completion
=> \
&PVE
::Storage
::complete_content_type
,
56 description
=> "Flag to disable the storage.",
61 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
67 description
=> "Mark storage as shared.",
72 description
=> "Default image format.",
73 type
=> 'string', format
=> 'pve-storage-format',
79 sub content_hash_to_string
{
83 foreach my $ct (keys %$hash) {
84 push @cta, $ct if $hash->{$ct};
87 return join(',', @cta);
90 sub valid_content_types
{
93 my $def = $defaultData->{plugindata
}->{$type};
97 return $def->{content
}->[0];
103 my $type = $scfg->{type
};
104 my $def = $defaultData->{plugindata
}->{$type};
106 my $def_format = 'raw';
107 my $valid_formats = [ $def_format ];
109 if (defined($def->{format
})) {
110 $def_format = $scfg->{format
} || $def->{format
}->[1];
111 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
114 return wantarray ?
($def_format, $valid_formats) : $def_format;
117 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
119 my ($path, $noerr) = @_;
121 # fixme: exclude more shell meta characters?
122 # we need absolute paths
123 if ($path !~ m
|^/[^;\
(\
)]+|) {
124 return undef if $noerr;
125 die "value does not look like a valid absolute path\n";
130 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
132 my ($server, $noerr) = @_;
134 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
135 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
137 return undef if $noerr;
138 die "value does not look like a valid server name or IP address\n";
143 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
145 my ($name, $noerr) = @_;
147 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
148 return undef if $noerr;
149 die "lvm name '$name' contains illegal characters\n";
155 # fixme: do we need this
156 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
158 # my ($portal, $noerr) = @_;
160 # # IP with optional port
161 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
162 # return undef if $noerr;
163 # die "value does not look like a valid portal address\n";
168 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
169 sub verify_portal_dns
{
170 my ($portal, $noerr) = @_;
172 # IP or DNS name with optional port
173 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
174 return undef if $noerr;
175 die "value does not look like a valid portal address\n";
180 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
182 my ($ct, $noerr) = @_;
184 my $valid_content = valid_content_types
('dir'); # dir includes all types
186 if (!$valid_content->{$ct}) {
187 return undef if $noerr;
188 die "invalid content type '$ct'\n";
194 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
196 my ($fmt, $noerr) = @_;
198 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
199 return undef if $noerr;
200 die "invalid format '$fmt'\n";
206 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
208 my ($value, $noerr) = @_;
210 # mount options (see man fstab)
211 if ($value !~ m/^\S+$/) {
212 return undef if $noerr;
213 die "invalid options '$value'\n";
219 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
220 sub parse_volume_id
{
221 my ($volid, $noerr) = @_;
223 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
224 return wantarray ?
($1, $2) : $1;
226 return undef if $noerr;
227 die "unable to parse volume ID '$volid'\n";
235 sub parse_section_header
{
236 my ($class, $line) = @_;
238 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
239 my ($type, $storeid) = (lc($1), $2);
240 my $errmsg = undef; # set if you want to skip whole section
241 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
243 my $config = {}; # to return additional attributes
244 return ($type, $storeid, $errmsg, $config);
250 my ($class, $type, $key, $value) = @_;
252 my $def = $defaultData->{plugindata
}->{$type};
254 if ($key eq 'content') {
255 my $valid_content = $def->{content
}->[0];
259 foreach my $c (PVE
::Tools
::split_list
($value)) {
260 if (!$valid_content->{$c}) {
261 warn "storage does not support content type '$c'\n";
267 if ($res->{none
} && scalar (keys %$res) > 1) {
268 die "unable to combine 'none' with other content types\n";
272 } elsif ($key eq 'format') {
273 my $valid_formats = $def->{format
}->[0];
275 if (!$valid_formats->{$value}) {
276 warn "storage does not support format '$value'\n";
281 } elsif ($key eq 'nodes') {
284 foreach my $node (PVE
::Tools
::split_list
($value)) {
285 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
291 # no node restrictions for local storage
292 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
293 # die "storage '$storeid' does not allow node restrictions\n";
303 my ($class, $type, $key, $value) = @_;
305 if ($key eq 'nodes') {
306 return join(',', keys(%$value));
307 } elsif ($key eq 'content') {
308 my $res = content_hash_to_string
($value) || 'none';
316 my ($class, $filename, $raw) = @_;
318 my $cfg = $class->SUPER::parse_config
($filename, $raw);
319 my $ids = $cfg->{ids
};
321 # make sure we have a reasonable 'local:' storage
322 # we want 'local' to be always the same 'type' (on all cluster nodes)
323 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
324 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
327 priority
=> 0, # force first entry
328 path
=> '/var/lib/vz',
330 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1},
334 # make sure we have a path
335 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
337 # remove node restrictions for local storage
338 delete($ids->{local}->{nodes
});
340 foreach my $storeid (keys %$ids) {
341 my $d = $ids->{$storeid};
342 my $type = $d->{type
};
344 my $def = $defaultData->{plugindata
}->{$type};
346 if ($def->{content
}) {
347 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
349 if (grep { $_ eq $type } @SHARED_STORAGE) {
357 # Storage implementation
359 # called during addition of storage (before the new storage config got written)
360 # die to abort additon if there are (grave) problems
361 # NOTE: runs in a storage config *locked* context
363 my ($class, $storeid, $scfg, %param) = @_;
365 # do nothing by default
368 # called during deletion of storage (before the new storage config got written)
369 # and if the activate check on addition fails, to cleanup all storage traces
370 # which on_add_hook may have created.
371 # die to abort deletion if there are (very grave) problems
372 # NOTE: runs in a storage config *locked* context
374 my ($class, $storeid, $scfg) = @_;
376 # do nothing by default
379 sub cluster_lock_storage
{
380 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
384 my $lockid = "pve-storage-$storeid";
385 my $lockdir = "/var/lock/pve-manager";
387 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
390 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
399 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
400 return ($1, $3, $2); # (name, format, isBase)
403 die "unable to parse volume filename '$name'\n";
407 my ($class, $volname) = @_;
409 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
410 my ($basedvmid, $basename) = ($1, $2);
411 parse_name_dir
($basename);
412 my ($vmid, $name) = ($3, $4);
413 my (undef, $format, $isBase) = parse_name_dir
($name);
414 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
415 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
416 my ($vmid, $name) = ($1, $2);
417 my (undef, $format, $isBase) = parse_name_dir
($name);
418 return ('images', $name, $vmid, undef, undef, $isBase, $format);
419 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
421 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
422 return ('vztmpl', $1);
423 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
424 return ('rootdir', $1, $1);
425 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
427 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
428 return ('backup', $fn, $2);
430 return ('backup', $fn);
433 die "unable to parse directory volume name '$volname'\n";
436 my $vtype_subdirs = {
438 rootdir
=> 'private',
439 iso
=> 'template/iso',
440 vztmpl
=> 'template/cache',
445 my ($class, $scfg, $vtype) = @_;
447 my $path = $scfg->{path
};
449 die "storage definintion has no path\n" if !$path;
451 my $subdir = $vtype_subdirs->{$vtype};
453 die "unknown vtype '$vtype'\n" if !defined($subdir);
455 return "$path/$subdir";
458 sub filesystem_path
{
459 my ($class, $scfg, $volname, $snapname) = @_;
461 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
462 $class->parse_volname($volname);
464 # Note: qcow2/qed has internal snapshot, so path is always
465 # the same (with or without snapshot => same file).
466 die "can't snapshot this image format\n"
467 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
469 my $dir = $class->get_subdir($scfg, $vtype);
471 $dir .= "/$vmid" if $vtype eq 'images';
473 my $path = "$dir/$name";
475 return wantarray ?
($path, $vmid, $vtype) : $path;
479 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
481 return $class->filesystem_path($scfg, $volname, $snapname);
485 my ($class, $storeid, $scfg, $volname) = @_;
487 # this only works for file based storage types
488 die "storage definition has no path\n" if !$scfg->{path
};
490 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
491 $class->parse_volname($volname);
493 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
495 die "create_base not possible with base image\n" if $isBase;
497 my $path = $class->filesystem_path($scfg, $volname);
499 my ($size, undef, $used, $parent) = file_size_info
($path);
500 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
502 die "volname '$volname' contains wrong information about parent\n"
503 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
506 $newname =~ s/^vm-/base-/;
508 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
511 my $newpath = $class->filesystem_path($scfg, $newvolname);
513 die "file '$newpath' already exists\n" if -f
$newpath;
515 rename($path, $newpath) ||
516 die "rename '$path' to '$newpath' failed - $!\n";
518 # We try to protect base volume
520 chmod(0444, $newpath); # nobody should write anything
522 # also try to set immutable flag
523 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
529 my $get_vm_disk_number = sub {
530 my ($disk_name, $scfg, $vmid, $suffix) = @_;
532 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
534 my $type = $scfg->{type
};
535 my $def = { %{$defaultData->{plugindata
}->{$type}} };
537 my $valid = $def->{format
}[0];
538 if ($valid->{subvol
}) {
539 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
542 if ($disk_name =~ m/$disk_regex/) {
549 sub get_next_vm_diskname
{
550 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
553 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
554 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
557 foreach my $disk (@$disk_list) {
558 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
559 $disk_ids->{$disknum} = 1 if defined($disknum);
562 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
563 if (!$disk_ids->{$i}) {
564 return "$prefix-$vmid-disk-$i$suffix";
568 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
571 my $find_free_diskname = sub {
572 my ($imgdir, $vmid, $fmt, $scfg) = @_;
576 if (defined(my $dh = IO
::Dir-
>new($imgdir))) {
577 @$disk_list = $dh->read();
581 return get_next_vm_diskname
($disk_list, $imgdir, $vmid, $fmt, $scfg, 1);
585 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
587 # this only works for file based storage types
588 die "storage definintion has no path\n" if !$scfg->{path
};
590 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
591 $class->parse_volname($volname);
593 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
595 die "this storage type does not support clone_image on snapshot\n" if $snap;
597 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
599 die "clone_image only works on base images\n" if !$isBase;
601 my $imagedir = $class->get_subdir($scfg, 'images');
602 $imagedir .= "/$vmid";
606 my $name = $find_free_diskname->($imagedir, $vmid, "qcow2", $scfg);
608 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
610 my $newvol = "$basevmid/$basename/$vmid/$name";
612 my $path = $class->filesystem_path($scfg, $newvol);
614 # Note: we use relative paths, so we need to call chdir before qemu-img
616 local $CWD = $imagedir;
618 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
619 '-f', 'qcow2', $path];
631 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
633 my $imagedir = $class->get_subdir($scfg, 'images');
634 $imagedir .= "/$vmid";
638 $name = $find_free_diskname->($imagedir, $vmid, $fmt, $scfg) if !$name;
640 my (undef, $tmpfmt) = parse_name_dir
($name);
642 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
645 my $path = "$imagedir/$name";
647 die "disk image '$path' already exists\n" if -e
$path;
649 if ($fmt eq 'subvol') {
650 # only allow this if size = 0, so that user knows what he is doing
651 die "storage does not support subvol quotas\n" if $size != 0;
653 my $old_umask = umask(0022);
655 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
659 my $cmd = ['/usr/bin/qemu-img', 'create'];
661 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
663 push @$cmd, '-f', $fmt, $path, "${size}K";
665 run_command
($cmd, errmsg
=> "unable to create image");
668 return "$vmid/$name";
672 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
674 my $path = $class->filesystem_path($scfg, $volname);
677 # try to remove immutable flag
678 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
682 if (defined($format) && ($format eq 'subvol')) {
683 File
::Path
::remove_tree
($path);
687 warn "disk image '$path' does not exists\n";
691 unlink($path) || die "unlink '$path' failed - $!\n";
694 # try to cleanup directory to not clutter storage with empty $vmid dirs if
695 # all images from a guest got deleted
696 my $dir = dirname
($path);
703 my ($filename, $timeout) = @_;
706 return wantarray ?
(0, 'subvol', 0, undef) : 1;
709 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
717 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
719 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
721 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
723 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
725 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
729 $used *= 1024 if $u eq 'K';
730 $used *= (1024*1024) if $u eq 'M';
731 $used *= (1024*1024*1024) if $u eq 'G';
732 $used *= (1024*1024*1024*1024) if $u eq 'T';
739 return wantarray ?
($size, $format, $used, $parent) : $size;
742 sub volume_size_info
{
743 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
744 my $path = $class->filesystem_path($scfg, $volname);
745 return file_size_info
($path, $timeout);
750 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
752 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
754 return 1 if $running;
756 my $path = $class->filesystem_path($scfg, $volname);
758 my $format = ($class->parse_volname($volname))[6];
760 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
762 run_command
($cmd, timeout
=> 10);
767 sub volume_snapshot
{
768 my ($class, $scfg, $storeid, $volname, $snap) = @_;
770 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
772 my $path = $class->filesystem_path($scfg, $volname);
774 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
781 sub volume_rollback_is_possible
{
782 my ($class, $scfg, $storeid, $volname, $snap) = @_;
787 sub volume_snapshot_rollback
{
788 my ($class, $scfg, $storeid, $volname, $snap) = @_;
790 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
792 my $path = $class->filesystem_path($scfg, $volname);
794 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
801 sub volume_snapshot_delete
{
802 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
804 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
806 return 1 if $running;
808 my $path = $class->filesystem_path($scfg, $volname);
810 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
812 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
819 sub storage_can_replicate
{
820 my ($class, $scfg, $storeid, $format) = @_;
825 sub volume_has_feature
{
826 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
829 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
830 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
831 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
832 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
833 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
834 snap
=> {qcow2
=> 1} },
835 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
836 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
839 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
840 $class->parse_volname($volname);
846 $key = $isBase ?
'base' : 'current';
849 return 1 if defined($features->{$feature}->{$key}->{$format});
855 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
857 my $imagedir = $class->get_subdir($scfg, 'images');
859 my ($defFmt, $vaidFmts) = default_format
($scfg);
860 my $fmts = join ('|', @$vaidFmts);
864 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
866 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
872 next if !$vollist && defined($vmid) && ($owner ne $vmid);
874 my ($size, $format, $used, $parent) = file_size_info
($fn);
875 next if !($format && defined($size));
878 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
879 my ($basevmid, $basename) = ($1, $2);
880 $volid = "$storeid:$basevmid/$basename/$owner/$name";
882 $volid = "$storeid:$owner/$name";
886 my $found = grep { $_ eq $volid } @$vollist;
891 volid
=> $volid, format
=> $format,
892 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
900 my ($class, $storeid, $scfg, $cache) = @_;
902 my $path = $scfg->{path
};
904 die "storage definintion has no path\n" if !$path;
907 my $res = PVE
::Tools
::df
($path, $timeout);
909 return undef if !$res || !$res->{total
};
911 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
914 sub volume_snapshot_list
{
915 my ($class, $scfg, $storeid, $volname) = @_;
917 # implement in subclass
918 die "Volume_snapshot_list is not implemented for $class";
920 # return an empty array if dataset does not exist.
923 sub activate_storage
{
924 my ($class, $storeid, $scfg, $cache) = @_;
926 my $path = $scfg->{path
};
928 die "storage definintion has no path\n" if !$path;
930 # this path test may hang indefinitely on unresponsive mounts
932 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
933 die "unable to activate storage '$storeid' - " .
934 "directory '$path' does not exist or is unreachable\n";
938 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
940 if (defined($scfg->{content
})) {
941 foreach my $vtype (keys %$vtype_subdirs) {
942 # OpenVZMigrate uses backup (dump) dir
943 if (defined($scfg->{content
}->{$vtype}) ||
944 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
945 my $subdir = $class->get_subdir($scfg, $vtype);
946 mkpath
$subdir if $subdir ne $path;
952 sub deactivate_storage
{
953 my ($class, $storeid, $scfg, $cache) = @_;
955 # do nothing by default
959 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
965 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
970 sub activate_volume
{
971 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
973 my $path = $class->filesystem_path($scfg, $volname, $snapname);
975 # check is volume exists
977 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
979 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
983 sub deactivate_volume
{
984 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
986 # do nothing by default
989 sub check_connection
{
990 my ($class, $storeid, $scfg) = @_;
991 # do nothing by default
995 # Import/Export interface:
996 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
997 # the default implementations will return this if $scfg->{path} is set,
998 # mimicking the old PVE::Storage::storage_migrate() function.
1000 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1001 # functions in case the format doesn't match their specialized
1002 # implementations to reuse the raw/tar code.
1004 # Format specification:
1005 # The following formats are all prefixed with image information in the form
1006 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1007 # to preallocate the image on storages which require it.
1009 # raw+size: (image files only)
1010 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1011 # qcow2+size, vmdk: (image files only)
1012 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1013 # files which are already in qcow2 format, or via `qemu-img convert`.
1014 # Note that these formats are only valid with $with_snapshots being true.
1015 # tar+size: (subvolumes only)
1016 # A GNU tar stream containing just the inner contents of the subvolume.
1017 # This does not distinguish between the contents of a privileged or
1018 # unprivileged container. In other words, this is from the root user
1019 # namespace's point of view with no uid-mapping in effect.
1020 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1022 # Plugins may reuse these helpers. Changes to the header format should be
1023 # reflected by changes to the function prototypes.
1024 sub write_common_header
($$) {
1025 my ($fh, $image_size_in_bytes) = @_;
1026 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1029 sub read_common_header
($) {
1031 sysread($fh, my $size, 8);
1032 $size = unpack('Q<', $size);
1033 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
1038 # Export a volume into a file handle as a stream of desired format.
1040 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1041 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1042 my $file = $class->path($scfg, $volname, $storeid)
1043 or goto unsupported
;
1044 my ($size, $file_format) = file_size_info
($file);
1046 if ($format eq 'raw+size') {
1047 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1048 write_common_header
($fh, $size);
1049 if ($file_format eq 'raw') {
1050 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1052 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1053 output
=> '>&'.fileno($fh));
1056 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1057 my $data_format = $1;
1058 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1059 write_common_header
($fh, $size);
1060 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1062 } elsif ($format eq 'tar+size') {
1063 goto unsupported
if $file_format ne 'subvol';
1064 write_common_header
($fh, $size);
1065 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1066 output
=> '>&'.fileno($fh));
1071 die "volume export format $format not available for $class";
1074 sub volume_export_formats
{
1075 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1076 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1077 my $file = $class->path($scfg, $volname, $storeid)
1079 my ($size, $format) = file_size_info
($file);
1081 if ($with_snapshots) {
1082 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1085 return ('tar+size') if $format eq 'subvol';
1086 return ('raw+size');
1091 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1093 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1095 die "volume import format '$format' not available for $class\n"
1096 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1097 my $data_format = $1;
1099 die "format $format cannot be imported without snapshots\n"
1100 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1101 die "format $format cannot be imported with snapshots\n"
1102 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1104 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1105 $class->parse_volname($volname);
1107 # XXX: Should we bother with conversion routines at this level? This won't
1108 # happen without manual CLI usage, so for now we just error out...
1109 die "cannot import format $format into a file of format $file_format\n"
1110 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1112 # Check for an existing file first since interrupting alloc_image doesn't
1114 my $file = $class->path($scfg, $volname, $storeid);
1115 die "file '$file' already exists\n" if -e
$file;
1117 my ($size) = read_common_header
($fh);
1118 $size = int($size/1024);
1121 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1122 if ($allocname ne $volname) {
1123 my $oldname = $volname;
1124 $volname = $allocname; # Let the cleanup code know what to free
1125 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1127 my $file = $class->path($scfg, $volname, $storeid)
1128 or die "internal error: failed to get path to newly allocated volume $volname\n";
1129 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1130 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1131 input
=> '<&'.fileno($fh));
1132 } elsif ($data_format eq 'tar') {
1133 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1134 input
=> '<&'.fileno($fh));
1136 die "volume import format '$format' not available for $class";
1140 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1146 sub volume_import_formats
{
1147 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1148 if ($scfg->{path
} && !defined($base_snapshot)) {
1149 my $format = ($class->parse_volname($volname))[6];
1150 if ($with_snapshots) {
1151 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1154 return ('tar+size') if $format eq 'subvol';
1155 return ('raw+size');