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, snippets
=> 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);
431 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
432 return ('snippets', $1);
435 die "unable to parse directory volume name '$volname'\n";
438 my $vtype_subdirs = {
440 rootdir
=> 'private',
441 iso
=> 'template/iso',
442 vztmpl
=> 'template/cache',
444 snippets
=> 'snippets',
448 my ($class, $scfg, $vtype) = @_;
450 my $path = $scfg->{path
};
452 die "storage definintion has no path\n" if !$path;
454 my $subdir = $vtype_subdirs->{$vtype};
456 die "unknown vtype '$vtype'\n" if !defined($subdir);
458 return "$path/$subdir";
461 sub filesystem_path
{
462 my ($class, $scfg, $volname, $snapname) = @_;
464 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
465 $class->parse_volname($volname);
467 # Note: qcow2/qed has internal snapshot, so path is always
468 # the same (with or without snapshot => same file).
469 die "can't snapshot this image format\n"
470 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
472 my $dir = $class->get_subdir($scfg, $vtype);
474 $dir .= "/$vmid" if $vtype eq 'images';
476 my $path = "$dir/$name";
478 return wantarray ?
($path, $vmid, $vtype) : $path;
482 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
484 return $class->filesystem_path($scfg, $volname, $snapname);
488 my ($class, $storeid, $scfg, $volname) = @_;
490 # this only works for file based storage types
491 die "storage definition has no path\n" if !$scfg->{path
};
493 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
494 $class->parse_volname($volname);
496 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
498 die "create_base not possible with base image\n" if $isBase;
500 my $path = $class->filesystem_path($scfg, $volname);
502 my ($size, undef, $used, $parent) = file_size_info
($path);
503 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
505 die "volname '$volname' contains wrong information about parent\n"
506 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
509 $newname =~ s/^vm-/base-/;
511 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
514 my $newpath = $class->filesystem_path($scfg, $newvolname);
516 die "file '$newpath' already exists\n" if -f
$newpath;
518 rename($path, $newpath) ||
519 die "rename '$path' to '$newpath' failed - $!\n";
521 # We try to protect base volume
523 chmod(0444, $newpath); # nobody should write anything
525 # also try to set immutable flag
526 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
532 my $get_vm_disk_number = sub {
533 my ($disk_name, $scfg, $vmid, $suffix) = @_;
535 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
537 my $type = $scfg->{type
};
538 my $def = { %{$defaultData->{plugindata
}->{$type}} };
540 my $valid = $def->{format
}[0];
541 if ($valid->{subvol
}) {
542 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
545 if ($disk_name =~ m/$disk_regex/) {
552 sub get_next_vm_diskname
{
553 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
556 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
557 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
560 foreach my $disk (@$disk_list) {
561 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
562 $disk_ids->{$disknum} = 1 if defined($disknum);
565 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
566 if (!$disk_ids->{$i}) {
567 return "$prefix-$vmid-disk-$i$suffix";
571 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
574 my $find_free_diskname = sub {
575 my ($imgdir, $vmid, $fmt, $scfg) = @_;
579 if (defined(my $dh = IO
::Dir-
>new($imgdir))) {
580 @$disk_list = $dh->read();
584 return get_next_vm_diskname
($disk_list, $imgdir, $vmid, $fmt, $scfg, 1);
588 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
590 # this only works for file based storage types
591 die "storage definintion has no path\n" if !$scfg->{path
};
593 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
594 $class->parse_volname($volname);
596 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
598 die "this storage type does not support clone_image on snapshot\n" if $snap;
600 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
602 die "clone_image only works on base images\n" if !$isBase;
604 my $imagedir = $class->get_subdir($scfg, 'images');
605 $imagedir .= "/$vmid";
609 my $name = $find_free_diskname->($imagedir, $vmid, "qcow2", $scfg);
611 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
613 my $newvol = "$basevmid/$basename/$vmid/$name";
615 my $path = $class->filesystem_path($scfg, $newvol);
617 # Note: we use relative paths, so we need to call chdir before qemu-img
619 local $CWD = $imagedir;
621 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
622 '-f', 'qcow2', $path];
634 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
636 my $imagedir = $class->get_subdir($scfg, 'images');
637 $imagedir .= "/$vmid";
641 $name = $find_free_diskname->($imagedir, $vmid, $fmt, $scfg) if !$name;
643 my (undef, $tmpfmt) = parse_name_dir
($name);
645 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
648 my $path = "$imagedir/$name";
650 die "disk image '$path' already exists\n" if -e
$path;
652 if ($fmt eq 'subvol') {
653 # only allow this if size = 0, so that user knows what he is doing
654 die "storage does not support subvol quotas\n" if $size != 0;
656 my $old_umask = umask(0022);
658 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
662 my $cmd = ['/usr/bin/qemu-img', 'create'];
664 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
666 push @$cmd, '-f', $fmt, $path, "${size}K";
668 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
676 return "$vmid/$name";
680 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
682 my $path = $class->filesystem_path($scfg, $volname);
685 # try to remove immutable flag
686 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
690 if (defined($format) && ($format eq 'subvol')) {
691 File
::Path
::remove_tree
($path);
695 warn "disk image '$path' does not exists\n";
699 unlink($path) || die "unlink '$path' failed - $!\n";
702 # try to cleanup directory to not clutter storage with empty $vmid dirs if
703 # all images from a guest got deleted
704 my $dir = dirname
($path);
711 my ($filename, $timeout) = @_;
714 return wantarray ?
(0, 'subvol', 0, undef) : 1;
717 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
725 run_command
($cmd, timeout
=> $timeout, outfunc
=> sub {
727 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
729 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
731 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
733 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
737 $used *= 1024 if $u eq 'K';
738 $used *= (1024*1024) if $u eq 'M';
739 $used *= (1024*1024*1024) if $u eq 'G';
740 $used *= (1024*1024*1024*1024) if $u eq 'T';
747 return wantarray ?
($size, $format, $used, $parent) : $size;
750 sub volume_size_info
{
751 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
752 my $path = $class->filesystem_path($scfg, $volname);
753 return file_size_info
($path, $timeout);
758 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
760 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
762 return 1 if $running;
764 my $path = $class->filesystem_path($scfg, $volname);
766 my $format = ($class->parse_volname($volname))[6];
768 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
770 run_command
($cmd, timeout
=> 10);
775 sub volume_snapshot
{
776 my ($class, $scfg, $storeid, $volname, $snap) = @_;
778 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
780 my $path = $class->filesystem_path($scfg, $volname);
782 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
789 sub volume_rollback_is_possible
{
790 my ($class, $scfg, $storeid, $volname, $snap) = @_;
795 sub volume_snapshot_rollback
{
796 my ($class, $scfg, $storeid, $volname, $snap) = @_;
798 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
800 my $path = $class->filesystem_path($scfg, $volname);
802 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
809 sub volume_snapshot_delete
{
810 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
812 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
814 return 1 if $running;
816 my $path = $class->filesystem_path($scfg, $volname);
818 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
820 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
827 sub storage_can_replicate
{
828 my ($class, $scfg, $storeid, $format) = @_;
833 sub volume_has_feature
{
834 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
837 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
838 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
839 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
840 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
841 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
842 snap
=> {qcow2
=> 1} },
843 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
844 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
847 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
848 $class->parse_volname($volname);
854 $key = $isBase ?
'base' : 'current';
857 return 1 if defined($features->{$feature}->{$key}->{$format});
863 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
865 my $imagedir = $class->get_subdir($scfg, 'images');
867 my ($defFmt, $vaidFmts) = default_format
($scfg);
868 my $fmts = join ('|', @$vaidFmts);
872 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
874 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
880 next if !$vollist && defined($vmid) && ($owner ne $vmid);
882 my ($size, $format, $used, $parent) = file_size_info
($fn);
883 next if !($format && defined($size));
886 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
887 my ($basevmid, $basename) = ($1, $2);
888 $volid = "$storeid:$basevmid/$basename/$owner/$name";
890 $volid = "$storeid:$owner/$name";
894 my $found = grep { $_ eq $volid } @$vollist;
899 volid
=> $volid, format
=> $format,
900 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
908 my ($class, $storeid, $scfg, $cache) = @_;
910 my $path = $scfg->{path
};
912 die "storage definintion has no path\n" if !$path;
915 my $res = PVE
::Tools
::df
($path, $timeout);
917 return undef if !$res || !$res->{total
};
919 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
922 sub volume_snapshot_list
{
923 my ($class, $scfg, $storeid, $volname) = @_;
925 # implement in subclass
926 die "Volume_snapshot_list is not implemented for $class";
928 # return an empty array if dataset does not exist.
931 sub activate_storage
{
932 my ($class, $storeid, $scfg, $cache) = @_;
934 my $path = $scfg->{path
};
936 die "storage definintion has no path\n" if !$path;
938 # this path test may hang indefinitely on unresponsive mounts
940 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
941 die "unable to activate storage '$storeid' - " .
942 "directory '$path' does not exist or is unreachable\n";
946 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
948 if (defined($scfg->{content
})) {
949 foreach my $vtype (keys %$vtype_subdirs) {
950 # OpenVZMigrate uses backup (dump) dir
951 if (defined($scfg->{content
}->{$vtype}) ||
952 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
953 my $subdir = $class->get_subdir($scfg, $vtype);
954 mkpath
$subdir if $subdir ne $path;
960 sub deactivate_storage
{
961 my ($class, $storeid, $scfg, $cache) = @_;
963 # do nothing by default
967 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
969 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
974 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
979 sub activate_volume
{
980 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
982 my $path = $class->filesystem_path($scfg, $volname, $snapname);
984 # check is volume exists
986 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
988 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
992 sub deactivate_volume
{
993 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
995 # do nothing by default
998 sub check_connection
{
999 my ($class, $storeid, $scfg) = @_;
1000 # do nothing by default
1004 # Import/Export interface:
1005 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1006 # the default implementations will return this if $scfg->{path} is set,
1007 # mimicking the old PVE::Storage::storage_migrate() function.
1009 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1010 # functions in case the format doesn't match their specialized
1011 # implementations to reuse the raw/tar code.
1013 # Format specification:
1014 # The following formats are all prefixed with image information in the form
1015 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1016 # to preallocate the image on storages which require it.
1018 # raw+size: (image files only)
1019 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1020 # qcow2+size, vmdk: (image files only)
1021 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1022 # files which are already in qcow2 format, or via `qemu-img convert`.
1023 # Note that these formats are only valid with $with_snapshots being true.
1024 # tar+size: (subvolumes only)
1025 # A GNU tar stream containing just the inner contents of the subvolume.
1026 # This does not distinguish between the contents of a privileged or
1027 # unprivileged container. In other words, this is from the root user
1028 # namespace's point of view with no uid-mapping in effect.
1029 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1031 # Plugins may reuse these helpers. Changes to the header format should be
1032 # reflected by changes to the function prototypes.
1033 sub write_common_header
($$) {
1034 my ($fh, $image_size_in_bytes) = @_;
1035 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1038 sub read_common_header
($) {
1040 sysread($fh, my $size, 8);
1041 $size = unpack('Q<', $size);
1042 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
1047 # Export a volume into a file handle as a stream of desired format.
1049 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1050 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1051 my $file = $class->path($scfg, $volname, $storeid)
1052 or goto unsupported
;
1053 my ($size, $file_format) = file_size_info
($file);
1055 if ($format eq 'raw+size') {
1056 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1057 write_common_header
($fh, $size);
1058 if ($file_format eq 'raw') {
1059 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1061 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1062 output
=> '>&'.fileno($fh));
1065 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1066 my $data_format = $1;
1067 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1068 write_common_header
($fh, $size);
1069 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1071 } elsif ($format eq 'tar+size') {
1072 goto unsupported
if $file_format ne 'subvol';
1073 write_common_header
($fh, $size);
1074 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1075 output
=> '>&'.fileno($fh));
1080 die "volume export format $format not available for $class";
1083 sub volume_export_formats
{
1084 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1085 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1086 my $file = $class->path($scfg, $volname, $storeid)
1088 my ($size, $format) = file_size_info
($file);
1090 if ($with_snapshots) {
1091 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1094 return ('tar+size') if $format eq 'subvol';
1095 return ('raw+size');
1100 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1102 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1104 die "volume import format '$format' not available for $class\n"
1105 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1106 my $data_format = $1;
1108 die "format $format cannot be imported without snapshots\n"
1109 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1110 die "format $format cannot be imported with snapshots\n"
1111 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1113 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1114 $class->parse_volname($volname);
1116 # XXX: Should we bother with conversion routines at this level? This won't
1117 # happen without manual CLI usage, so for now we just error out...
1118 die "cannot import format $format into a file of format $file_format\n"
1119 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1121 # Check for an existing file first since interrupting alloc_image doesn't
1123 my $file = $class->path($scfg, $volname, $storeid);
1124 die "file '$file' already exists\n" if -e
$file;
1126 my ($size) = read_common_header
($fh);
1127 $size = int($size/1024);
1130 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1131 if ($allocname ne $volname) {
1132 my $oldname = $volname;
1133 $volname = $allocname; # Let the cleanup code know what to free
1134 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1136 my $file = $class->path($scfg, $volname, $storeid)
1137 or die "internal error: failed to get path to newly allocated volume $volname\n";
1138 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1139 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1140 input
=> '<&'.fileno($fh));
1141 } elsif ($data_format eq 'tar') {
1142 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1143 input
=> '<&'.fileno($fh));
1145 die "volume import format '$format' not available for $class";
1149 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1155 sub volume_import_formats
{
1156 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1157 if ($scfg->{path
} && !defined($base_snapshot)) {
1158 my $format = ($class->parse_volname($volname))[6];
1159 if ($with_snapshots) {
1160 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1163 return ('tar+size') if $format eq 'subvol';
1164 return ('raw+size');