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);
16 use base
qw(PVE::SectionConfig);
18 our @COMMON_TAR_FLAGS = qw(
20 -p --sparse --numeric-owner --acls
21 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
22 --warning=no-file-ignored --warning=no-xattr-write
25 our @SHARED_STORAGE = (
36 our $MAX_VOLUMES_PER_GUEST = 1024;
38 cfs_register_file
('storage.cfg',
39 sub { __PACKAGE__-
>parse_config(@_); },
40 sub { __PACKAGE__-
>write_config(@_); });
45 type
=> { description
=> "Storage type." },
46 storage
=> get_standard_option
('pve-storage-id',
47 { completion
=> \
&PVE
::Storage
::complete_storage
}),
48 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
50 description
=> "Allowed content types.\n\nNOTE: the value " .
51 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
52 type
=> 'string', format
=> 'pve-storage-content-list',
54 completion
=> \
&PVE
::Storage
::complete_content_type
,
57 description
=> "Flag to disable the storage.",
62 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
68 description
=> "Mark storage as shared.",
73 description
=> "Default image format.",
74 type
=> 'string', format
=> 'pve-storage-format',
80 sub content_hash_to_string
{
84 foreach my $ct (keys %$hash) {
85 push @cta, $ct if $hash->{$ct};
88 return join(',', @cta);
91 sub valid_content_types
{
94 my $def = $defaultData->{plugindata
}->{$type};
98 return $def->{content
}->[0];
104 my $type = $scfg->{type
};
105 my $def = $defaultData->{plugindata
}->{$type};
107 my $def_format = 'raw';
108 my $valid_formats = [ $def_format ];
110 if (defined($def->{format
})) {
111 $def_format = $scfg->{format
} || $def->{format
}->[1];
112 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
115 return wantarray ?
($def_format, $valid_formats) : $def_format;
118 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
120 my ($path, $noerr) = @_;
122 # fixme: exclude more shell meta characters?
123 # we need absolute paths
124 if ($path !~ m
|^/[^;\
(\
)]+|) {
125 return undef if $noerr;
126 die "value does not look like a valid absolute path\n";
131 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
133 my ($server, $noerr) = @_;
135 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
136 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
138 return undef if $noerr;
139 die "value does not look like a valid server name or IP address\n";
144 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
146 my ($name, $noerr) = @_;
148 if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) {
149 return undef if $noerr;
150 die "lvm name '$name' contains illegal characters\n";
156 # fixme: do we need this
157 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
159 # my ($portal, $noerr) = @_;
161 # # IP with optional port
162 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
163 # return undef if $noerr;
164 # die "value does not look like a valid portal address\n";
169 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
170 sub verify_portal_dns
{
171 my ($portal, $noerr) = @_;
173 # IP or DNS name with optional port
174 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
175 return undef if $noerr;
176 die "value does not look like a valid portal address\n";
181 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
183 my ($ct, $noerr) = @_;
185 my $valid_content = valid_content_types
('dir'); # dir includes all types
187 if (!$valid_content->{$ct}) {
188 return undef if $noerr;
189 die "invalid content type '$ct'\n";
195 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
197 my ($fmt, $noerr) = @_;
199 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
200 return undef if $noerr;
201 die "invalid format '$fmt'\n";
207 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
209 my ($value, $noerr) = @_;
211 # mount options (see man fstab)
212 if ($value !~ m/^\S+$/) {
213 return undef if $noerr;
214 die "invalid options '$value'\n";
220 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
221 sub parse_volume_id
{
222 my ($volid, $noerr) = @_;
224 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
225 return wantarray ?
($1, $2) : $1;
227 return undef if $noerr;
228 die "unable to parse volume ID '$volid'\n";
236 sub parse_section_header
{
237 my ($class, $line) = @_;
239 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
240 my ($type, $storeid) = (lc($1), $2);
241 my $errmsg = undef; # set if you want to skip whole section
242 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
244 my $config = {}; # to return additional attributes
245 return ($type, $storeid, $errmsg, $config);
251 my ($class, $type, $key, $value) = @_;
253 my $def = $defaultData->{plugindata
}->{$type};
255 if ($key eq 'content') {
256 my $valid_content = $def->{content
}->[0];
260 foreach my $c (PVE
::Tools
::split_list
($value)) {
261 if (!$valid_content->{$c}) {
262 warn "storage does not support content type '$c'\n";
268 if ($res->{none
} && scalar (keys %$res) > 1) {
269 die "unable to combine 'none' with other content types\n";
273 } elsif ($key eq 'format') {
274 my $valid_formats = $def->{format
}->[0];
276 if (!$valid_formats->{$value}) {
277 warn "storage does not support format '$value'\n";
282 } elsif ($key eq 'nodes') {
285 foreach my $node (PVE
::Tools
::split_list
($value)) {
286 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
292 # no node restrictions for local storage
293 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
294 # die "storage '$storeid' does not allow node restrictions\n";
304 my ($class, $type, $key, $value) = @_;
306 if ($key eq 'nodes') {
307 return join(',', keys(%$value));
308 } elsif ($key eq 'content') {
309 my $res = content_hash_to_string
($value) || 'none';
317 my ($class, $filename, $raw) = @_;
319 my $cfg = $class->SUPER::parse_config
($filename, $raw);
320 my $ids = $cfg->{ids
};
322 # make sure we have a reasonable 'local:' storage
323 # we want 'local' to be always the same 'type' (on all cluster nodes)
324 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
325 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
328 priority
=> 0, # force first entry
329 path
=> '/var/lib/vz',
331 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, snippets
=> 1},
335 # make sure we have a path
336 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
338 # remove node restrictions for local storage
339 delete($ids->{local}->{nodes
});
341 foreach my $storeid (keys %$ids) {
342 my $d = $ids->{$storeid};
343 my $type = $d->{type
};
345 my $def = $defaultData->{plugindata
}->{$type};
347 if ($def->{content
}) {
348 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
350 if (grep { $_ eq $type } @SHARED_STORAGE) {
358 # Storage implementation
360 # called during addition of storage (before the new storage config got written)
361 # die to abort additon if there are (grave) problems
362 # NOTE: runs in a storage config *locked* context
364 my ($class, $storeid, $scfg, %param) = @_;
366 # do nothing by default
369 # called during deletion of storage (before the new storage config got written)
370 # and if the activate check on addition fails, to cleanup all storage traces
371 # which on_add_hook may have created.
372 # die to abort deletion if there are (very grave) problems
373 # NOTE: runs in a storage config *locked* context
375 my ($class, $storeid, $scfg) = @_;
377 # do nothing by default
380 sub cluster_lock_storage
{
381 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
385 my $lockid = "pve-storage-$storeid";
386 my $lockdir = "/var/lock/pve-manager";
388 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
391 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
400 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
401 return ($1, $3, $2); # (name, format, isBase)
404 die "unable to parse volume filename '$name'\n";
408 my ($class, $volname) = @_;
410 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
411 my ($basedvmid, $basename) = ($1, $2);
412 parse_name_dir
($basename);
413 my ($vmid, $name) = ($3, $4);
414 my (undef, $format, $isBase) = parse_name_dir
($name);
415 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
416 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
417 my ($vmid, $name) = ($1, $2);
418 my (undef, $format, $isBase) = parse_name_dir
($name);
419 return ('images', $name, $vmid, undef, undef, $isBase, $format);
420 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::iso_extension_re)$!) {
422 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
423 return ('vztmpl', $1);
424 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
425 return ('rootdir', $1, $1);
426 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
428 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
429 return ('backup', $fn, $2);
431 return ('backup', $fn);
432 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
433 return ('snippets', $1);
436 die "unable to parse directory volume name '$volname'\n";
439 my $vtype_subdirs = {
441 rootdir
=> 'private',
442 iso
=> 'template/iso',
443 vztmpl
=> 'template/cache',
445 snippets
=> 'snippets',
449 my ($class, $scfg, $vtype) = @_;
451 my $path = $scfg->{path
};
453 die "storage definintion has no path\n" if !$path;
455 my $subdir = $vtype_subdirs->{$vtype};
457 die "unknown vtype '$vtype'\n" if !defined($subdir);
459 return "$path/$subdir";
462 sub filesystem_path
{
463 my ($class, $scfg, $volname, $snapname) = @_;
465 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
466 $class->parse_volname($volname);
468 # Note: qcow2/qed has internal snapshot, so path is always
469 # the same (with or without snapshot => same file).
470 die "can't snapshot this image format\n"
471 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
473 my $dir = $class->get_subdir($scfg, $vtype);
475 $dir .= "/$vmid" if $vtype eq 'images';
477 my $path = "$dir/$name";
479 return wantarray ?
($path, $vmid, $vtype) : $path;
483 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
485 return $class->filesystem_path($scfg, $volname, $snapname);
489 my ($class, $storeid, $scfg, $volname) = @_;
491 # this only works for file based storage types
492 die "storage definition has no path\n" if !$scfg->{path
};
494 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
495 $class->parse_volname($volname);
497 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
499 die "create_base not possible with base image\n" if $isBase;
501 my $path = $class->filesystem_path($scfg, $volname);
503 my ($size, undef, $used, $parent) = file_size_info
($path);
504 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
506 die "volname '$volname' contains wrong information about parent\n"
507 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
510 $newname =~ s/^vm-/base-/;
512 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
515 my $newpath = $class->filesystem_path($scfg, $newvolname);
517 die "file '$newpath' already exists\n" if -f
$newpath;
519 rename($path, $newpath) ||
520 die "rename '$path' to '$newpath' failed - $!\n";
522 # We try to protect base volume
524 chmod(0444, $newpath); # nobody should write anything
526 # also try to set immutable flag
527 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
533 my $get_vm_disk_number = sub {
534 my ($disk_name, $scfg, $vmid, $suffix) = @_;
536 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
538 my $type = $scfg->{type
};
539 my $def = { %{$defaultData->{plugindata
}->{$type}} };
541 my $valid = $def->{format
}[0];
542 if ($valid->{subvol
}) {
543 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
546 if ($disk_name =~ m/$disk_regex/) {
553 sub get_next_vm_diskname
{
554 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
557 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
558 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
561 foreach my $disk (@$disk_list) {
562 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
563 $disk_ids->{$disknum} = 1 if defined($disknum);
566 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
567 if (!$disk_ids->{$i}) {
568 return "$prefix-$vmid-disk-$i$suffix";
572 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
575 sub find_free_diskname
{
576 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
578 my $disks = $class->list_images($storeid, $scfg, $vmid);
582 foreach my $disk (@{$disks}) {
583 push @{$disk_list}, $disk->{volid
};
586 return get_next_vm_diskname
($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
590 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
592 # this only works for file based storage types
593 die "storage definintion has no path\n" if !$scfg->{path
};
595 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
596 $class->parse_volname($volname);
598 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
600 die "this storage type does not support clone_image on snapshot\n" if $snap;
602 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
604 die "clone_image only works on base images\n" if !$isBase;
606 my $imagedir = $class->get_subdir($scfg, 'images');
607 $imagedir .= "/$vmid";
611 my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
613 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
615 my $newvol = "$basevmid/$basename/$vmid/$name";
617 my $path = $class->filesystem_path($scfg, $newvol);
619 # Note: we use relative paths, so we need to call chdir before qemu-img
621 local $CWD = $imagedir;
623 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
624 '-f', 'qcow2', $path];
636 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
638 my $imagedir = $class->get_subdir($scfg, 'images');
639 $imagedir .= "/$vmid";
643 $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
645 my (undef, $tmpfmt) = parse_name_dir
($name);
647 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
650 my $path = "$imagedir/$name";
652 die "disk image '$path' already exists\n" if -e
$path;
654 if ($fmt eq 'subvol') {
655 # only allow this if size = 0, so that user knows what he is doing
656 die "storage does not support subvol quotas\n" if $size != 0;
658 my $old_umask = umask(0022);
660 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
664 my $cmd = ['/usr/bin/qemu-img', 'create'];
666 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
668 push @$cmd, '-f', $fmt, $path, "${size}K";
670 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
678 return "$vmid/$name";
682 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
684 my $path = $class->filesystem_path($scfg, $volname);
687 # try to remove immutable flag
688 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
692 if (defined($format) && ($format eq 'subvol')) {
693 File
::Path
::remove_tree
($path);
695 if (!(-f
$path || -l
$path)) {
696 warn "disk image '$path' does not exists\n";
700 unlink($path) || die "unlink '$path' failed - $!\n";
703 # try to cleanup directory to not clutter storage with empty $vmid dirs if
704 # all images from a guest got deleted
705 my $dir = dirname
($path);
712 my ($filename, $timeout) = @_;
715 return wantarray ?
(0, 'subvol', 0, undef) : 1;
720 run_command
(['/usr/bin/qemu-img', 'info', '--output=json', $filename],
722 outfunc
=> sub { $json .= shift },
723 errfunc
=> sub { warn "$_[0]\n" }
728 my $info = eval { decode_json
($json) };
729 warn "could not parse qemu-img info command output for '$filename'\n" if $@;
731 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
733 return wantarray ?
($size, $format, $used, $parent) : $size;
736 sub volume_size_info
{
737 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
738 my $path = $class->filesystem_path($scfg, $volname);
739 return file_size_info
($path, $timeout);
744 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
746 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
748 return 1 if $running;
750 my $path = $class->filesystem_path($scfg, $volname);
752 my $format = ($class->parse_volname($volname))[6];
754 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
756 run_command
($cmd, timeout
=> 10);
761 sub volume_snapshot
{
762 my ($class, $scfg, $storeid, $volname, $snap) = @_;
764 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
766 my $path = $class->filesystem_path($scfg, $volname);
768 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
775 sub volume_rollback_is_possible
{
776 my ($class, $scfg, $storeid, $volname, $snap) = @_;
781 sub volume_snapshot_rollback
{
782 my ($class, $scfg, $storeid, $volname, $snap) = @_;
784 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
786 my $path = $class->filesystem_path($scfg, $volname);
788 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
795 sub volume_snapshot_delete
{
796 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
798 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
800 return 1 if $running;
802 my $path = $class->filesystem_path($scfg, $volname);
804 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
806 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
813 sub storage_can_replicate
{
814 my ($class, $scfg, $storeid, $format) = @_;
819 sub volume_has_feature
{
820 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
823 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
824 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
825 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
826 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
827 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
828 snap
=> {qcow2
=> 1} },
829 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
830 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
833 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
834 $class->parse_volname($volname);
840 $key = $isBase ?
'base' : 'current';
843 return 1 if defined($features->{$feature}->{$key}->{$format});
849 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
851 my $imagedir = $class->get_subdir($scfg, 'images');
853 my ($defFmt, $vaidFmts) = default_format
($scfg);
854 my $fmts = join ('|', @$vaidFmts);
858 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
860 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
866 next if !$vollist && defined($vmid) && ($owner ne $vmid);
868 my ($size, $format, $used, $parent) = file_size_info
($fn);
869 next if !($format && defined($size));
872 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
873 my ($basevmid, $basename) = ($1, $2);
874 $volid = "$storeid:$basevmid/$basename/$owner/$name";
876 $volid = "$storeid:$owner/$name";
880 my $found = grep { $_ eq $volid } @$vollist;
885 volid
=> $volid, format
=> $format,
886 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
893 # list templates ($tt = <iso|vztmpl|backup|snippets>)
894 my $get_subdir_files = sub {
895 my ($sid, $path, $tt, $vmid) = @_;
899 foreach my $fn (<$path/*>) {
906 next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
908 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
910 } elsif ($tt eq 'vztmpl') {
911 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
913 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
915 } elsif ($tt eq 'backup') {
916 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
917 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
919 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
921 } elsif ($tt eq 'snippets') {
924 volid
=> "$sid:snippets/". basename
($fn),
929 $info->{size
} = -s
$fn // 0;
938 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
941 my $vmlist = PVE
::Cluster
::get_vmlist
();
942 foreach my $type (@$content_types) {
945 if ($type eq 'images' || $type eq 'rootdir') {
946 $data = $class->list_images($storeid, $scfg, $vmid);
947 } elsif ($scfg->{path
}) {
948 my $path = $class->get_subdir($scfg, $type);
950 if ($type eq 'iso' && !defined($vmid)) {
951 $data = $get_subdir_files->($storeid, $path, 'iso');
952 } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
953 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
954 } elsif ($type eq 'backup') {
955 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
956 } elsif ($type eq 'snippets') {
957 $data = $get_subdir_files->($storeid, $path, 'snippets');
963 foreach my $item (@$data) {
964 if ($type eq 'images' || $type eq 'rootdir') {
966 my $vmtype = $vmlist->{ids
}->{$item->{vmid
}}->{type
};
967 if (defined($vmtype) && $vmtype eq 'lxc') {
968 $item->{content
} = 'rootdir';
970 $item->{content
} = 'images';
972 next if $type ne $item->{content
};
974 $item->{content
} = $type;
985 my ($class, $storeid, $scfg, $cache) = @_;
987 my $path = $scfg->{path
};
989 die "storage definintion has no path\n" if !$path;
992 my $res = PVE
::Tools
::df
($path, $timeout);
994 return undef if !$res || !$res->{total
};
996 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
999 sub volume_snapshot_list
{
1000 my ($class, $scfg, $storeid, $volname) = @_;
1002 # implement in subclass
1003 die "Volume_snapshot_list is not implemented for $class";
1005 # return an empty array if dataset does not exist.
1008 sub activate_storage
{
1009 my ($class, $storeid, $scfg, $cache) = @_;
1011 my $path = $scfg->{path
};
1013 die "storage definintion has no path\n" if !$path;
1015 # this path test may hang indefinitely on unresponsive mounts
1017 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
1018 die "unable to activate storage '$storeid' - " .
1019 "directory '$path' does not exist or is unreachable\n";
1023 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1025 if (defined($scfg->{content
})) {
1026 foreach my $vtype (keys %$vtype_subdirs) {
1027 # OpenVZMigrate uses backup (dump) dir
1028 if (defined($scfg->{content
}->{$vtype}) ||
1029 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
1030 my $subdir = $class->get_subdir($scfg, $vtype);
1031 mkpath
$subdir if $subdir ne $path;
1037 sub deactivate_storage
{
1038 my ($class, $storeid, $scfg, $cache) = @_;
1040 # do nothing by default
1044 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1046 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1051 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1056 sub activate_volume
{
1057 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1059 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1061 # check is volume exists
1062 if ($scfg->{path
}) {
1063 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
1065 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
1069 sub deactivate_volume
{
1070 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1072 # do nothing by default
1075 sub check_connection
{
1076 my ($class, $storeid, $scfg) = @_;
1077 # do nothing by default
1081 # Import/Export interface:
1082 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1083 # the default implementations will return this if $scfg->{path} is set,
1084 # mimicking the old PVE::Storage::storage_migrate() function.
1086 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1087 # functions in case the format doesn't match their specialized
1088 # implementations to reuse the raw/tar code.
1090 # Format specification:
1091 # The following formats are all prefixed with image information in the form
1092 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1093 # to preallocate the image on storages which require it.
1095 # raw+size: (image files only)
1096 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1097 # qcow2+size, vmdk: (image files only)
1098 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1099 # files which are already in qcow2 format, or via `qemu-img convert`.
1100 # Note that these formats are only valid with $with_snapshots being true.
1101 # tar+size: (subvolumes only)
1102 # A GNU tar stream containing just the inner contents of the subvolume.
1103 # This does not distinguish between the contents of a privileged or
1104 # unprivileged container. In other words, this is from the root user
1105 # namespace's point of view with no uid-mapping in effect.
1106 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1108 # Plugins may reuse these helpers. Changes to the header format should be
1109 # reflected by changes to the function prototypes.
1110 sub write_common_header
($$) {
1111 my ($fh, $image_size_in_bytes) = @_;
1112 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1115 sub read_common_header
($) {
1117 sysread($fh, my $size, 8);
1118 $size = unpack('Q<', $size);
1119 die "import: no size found in export header, aborting.\n" if !defined($size);
1120 die "import: got a bad size (not a multiple of 1K), aborting.\n" if ($size&1023);
1125 # Export a volume into a file handle as a stream of desired format.
1127 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1128 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1129 my $file = $class->path($scfg, $volname, $storeid)
1130 or goto unsupported
;
1131 my ($size, $file_format) = file_size_info
($file);
1133 if ($format eq 'raw+size') {
1134 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1135 write_common_header
($fh, $size);
1136 if ($file_format eq 'raw') {
1137 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1139 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1140 output
=> '>&'.fileno($fh));
1143 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1144 my $data_format = $1;
1145 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1146 write_common_header
($fh, $size);
1147 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1149 } elsif ($format eq 'tar+size') {
1150 goto unsupported
if $file_format ne 'subvol';
1151 write_common_header
($fh, $size);
1152 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1153 output
=> '>&'.fileno($fh));
1158 die "volume export format $format not available for $class";
1161 sub volume_export_formats
{
1162 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1163 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1164 my $file = $class->path($scfg, $volname, $storeid)
1166 my ($size, $format) = file_size_info
($file);
1168 if ($with_snapshots) {
1169 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1172 return ('tar+size') if $format eq 'subvol';
1173 return ('raw+size');
1178 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1180 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1182 die "volume import format '$format' not available for $class\n"
1183 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1184 my $data_format = $1;
1186 die "format $format cannot be imported without snapshots\n"
1187 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1188 die "format $format cannot be imported with snapshots\n"
1189 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1191 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1192 $class->parse_volname($volname);
1194 # XXX: Should we bother with conversion routines at this level? This won't
1195 # happen without manual CLI usage, so for now we just error out...
1196 die "cannot import format $format into a file of format $file_format\n"
1197 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1199 # Check for an existing file first since interrupting alloc_image doesn't
1201 my $file = $class->path($scfg, $volname, $storeid);
1202 die "file '$file' already exists\n" if -e
$file;
1204 my ($size) = read_common_header
($fh);
1205 $size = int($size/1024);
1208 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1209 if ($allocname ne $volname) {
1210 my $oldname = $volname;
1211 $volname = $allocname; # Let the cleanup code know what to free
1212 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1214 my $file = $class->path($scfg, $volname, $storeid)
1215 or die "internal error: failed to get path to newly allocated volume $volname\n";
1216 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1217 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1218 input
=> '<&'.fileno($fh));
1219 } elsif ($data_format eq 'tar') {
1220 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1221 input
=> '<&'.fileno($fh));
1223 die "volume import format '$format' not available for $class";
1227 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1233 sub volume_import_formats
{
1234 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1235 if ($scfg->{path
} && !defined($base_snapshot)) {
1236 my $format = ($class->parse_volname($volname))[6];
1237 if ($with_snapshots) {
1238 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1241 return ('tar+size') if $format eq 'subvol';
1242 return ('raw+size');