1 package PVE
::Storage
::Plugin
;
11 use Time
::Local
qw(timelocal);
13 use PVE
::Tools
qw(run_command);
14 use PVE
::JSONSchema
qw(get_standard_option);
15 use PVE
::Cluster
qw(cfs_register_file);
19 use base
qw(PVE::SectionConfig);
21 use constant COMPRESSOR_RE
=> 'gz|lzo|zst';
23 our @COMMON_TAR_FLAGS = qw(
25 -p --sparse --numeric-owner --acls
26 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
27 --warning=no-file-ignored --warning=no-xattr-write
30 our @SHARED_STORAGE = (
41 our $MAX_VOLUMES_PER_GUEST = 1024;
43 cfs_register_file
('storage.cfg',
44 sub { __PACKAGE__-
>parse_config(@_); },
45 sub { __PACKAGE__-
>write_config(@_); });
50 type
=> { description
=> "Storage type." },
51 storage
=> get_standard_option
('pve-storage-id',
52 { completion
=> \
&PVE
::Storage
::complete_storage
}),
53 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
55 description
=> "Allowed content types.\n\nNOTE: the value " .
56 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
57 type
=> 'string', format
=> 'pve-storage-content-list',
59 completion
=> \
&PVE
::Storage
::complete_content_type
,
62 description
=> "Flag to disable the storage.",
67 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
73 description
=> "Mark storage as shared.",
78 description
=> "Default image format.",
79 type
=> 'string', format
=> 'pve-storage-format',
85 sub content_hash_to_string
{
89 foreach my $ct (keys %$hash) {
90 push @cta, $ct if $hash->{$ct};
93 return join(',', @cta);
96 sub valid_content_types
{
99 my $def = $defaultData->{plugindata
}->{$type};
103 return $def->{content
}->[0];
109 my $type = $scfg->{type
};
110 my $def = $defaultData->{plugindata
}->{$type};
112 my $def_format = 'raw';
113 my $valid_formats = [ $def_format ];
115 if (defined($def->{format
})) {
116 $def_format = $scfg->{format
} || $def->{format
}->[1];
117 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
120 return wantarray ?
($def_format, $valid_formats) : $def_format;
123 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
125 my ($path, $noerr) = @_;
127 # fixme: exclude more shell meta characters?
128 # we need absolute paths
129 if ($path !~ m
|^/[^;\
(\
)]+|) {
130 return undef if $noerr;
131 die "value does not look like a valid absolute path\n";
136 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
138 my ($server, $noerr) = @_;
140 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
141 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
143 return undef if $noerr;
144 die "value does not look like a valid server name or IP address\n";
149 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
151 my ($name, $noerr) = @_;
153 if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) {
154 return undef if $noerr;
155 die "lvm name '$name' contains illegal characters\n";
161 # fixme: do we need this
162 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
164 # my ($portal, $noerr) = @_;
166 # # IP with optional port
167 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
168 # return undef if $noerr;
169 # die "value does not look like a valid portal address\n";
174 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
175 sub verify_portal_dns
{
176 my ($portal, $noerr) = @_;
178 # IP or DNS name with optional port
179 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
180 return undef if $noerr;
181 die "value does not look like a valid portal address\n";
186 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
188 my ($ct, $noerr) = @_;
190 my $valid_content = valid_content_types
('dir'); # dir includes all types
192 if (!$valid_content->{$ct}) {
193 return undef if $noerr;
194 die "invalid content type '$ct'\n";
200 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
202 my ($fmt, $noerr) = @_;
204 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
205 return undef if $noerr;
206 die "invalid format '$fmt'\n";
212 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
214 my ($value, $noerr) = @_;
216 # mount options (see man fstab)
217 if ($value !~ m/^\S+$/) {
218 return undef if $noerr;
219 die "invalid options '$value'\n";
225 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
226 sub parse_volume_id
{
227 my ($volid, $noerr) = @_;
229 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
230 return wantarray ?
($1, $2) : $1;
232 return undef if $noerr;
233 die "unable to parse volume ID '$volid'\n";
241 sub parse_section_header
{
242 my ($class, $line) = @_;
244 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
245 my ($type, $storeid) = (lc($1), $2);
246 my $errmsg = undef; # set if you want to skip whole section
247 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
249 my $config = {}; # to return additional attributes
250 return ($type, $storeid, $errmsg, $config);
256 my ($class, $type, $key, $value) = @_;
258 my $def = $defaultData->{plugindata
}->{$type};
260 if ($key eq 'content') {
261 my $valid_content = $def->{content
}->[0];
265 foreach my $c (PVE
::Tools
::split_list
($value)) {
266 if (!$valid_content->{$c}) {
267 warn "storage does not support content type '$c'\n";
273 if ($res->{none
} && scalar (keys %$res) > 1) {
274 die "unable to combine 'none' with other content types\n";
278 } elsif ($key eq 'format') {
279 my $valid_formats = $def->{format
}->[0];
281 if (!$valid_formats->{$value}) {
282 warn "storage does not support format '$value'\n";
287 } elsif ($key eq 'nodes') {
290 foreach my $node (PVE
::Tools
::split_list
($value)) {
291 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
297 # no node restrictions for local storage
298 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
299 # die "storage '$storeid' does not allow node restrictions\n";
309 my ($class, $type, $key, $value) = @_;
311 if ($key eq 'nodes') {
312 return join(',', keys(%$value));
313 } elsif ($key eq 'content') {
314 my $res = content_hash_to_string
($value) || 'none';
322 my ($class, $filename, $raw) = @_;
324 my $cfg = $class->SUPER::parse_config
($filename, $raw);
325 my $ids = $cfg->{ids
};
327 # make sure we have a reasonable 'local:' storage
328 # we want 'local' to be always the same 'type' (on all cluster nodes)
329 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
330 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
333 priority
=> 0, # force first entry
334 path
=> '/var/lib/vz',
336 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, snippets
=> 1},
340 # make sure we have a path
341 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
343 # remove node restrictions for local storage
344 delete($ids->{local}->{nodes
});
346 foreach my $storeid (keys %$ids) {
347 my $d = $ids->{$storeid};
348 my $type = $d->{type
};
350 my $def = $defaultData->{plugindata
}->{$type};
352 if ($def->{content
}) {
353 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
355 if (grep { $_ eq $type } @SHARED_STORAGE) {
363 # Storage implementation
365 # called during addition of storage (before the new storage config got written)
366 # die to abort additon if there are (grave) problems
367 # NOTE: runs in a storage config *locked* context
369 my ($class, $storeid, $scfg, %param) = @_;
371 # do nothing by default
374 # called during storage configuration update (before the updated storage config got written)
375 # die to abort the update if there are (grave) problems
376 # NOTE: runs in a storage config *locked* context
378 my ($class, $storeid, $scfg, %param) = @_;
380 # do nothing by default
383 # called during deletion of storage (before the new storage config got written)
384 # and if the activate check on addition fails, to cleanup all storage traces
385 # which on_add_hook may have created.
386 # die to abort deletion if there are (very grave) problems
387 # NOTE: runs in a storage config *locked* context
389 my ($class, $storeid, $scfg) = @_;
391 # do nothing by default
394 sub cluster_lock_storage
{
395 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
399 my $lockid = "pve-storage-$storeid";
400 my $lockdir = "/var/lock/pve-manager";
402 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
405 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
414 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
415 return ($1, $3, $2); # (name, format, isBase)
418 die "unable to parse volume filename '$name'\n";
422 my ($class, $volname) = @_;
424 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
425 my ($basedvmid, $basename) = ($1, $2);
426 parse_name_dir
($basename);
427 my ($vmid, $name) = ($3, $4);
428 my (undef, $format, $isBase) = parse_name_dir
($name);
429 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
430 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
431 my ($vmid, $name) = ($1, $2);
432 my (undef, $format, $isBase) = parse_name_dir
($name);
433 return ('images', $name, $vmid, undef, undef, $isBase, $format);
434 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::iso_extension_re)$!) {
436 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
437 return ('vztmpl', $1);
438 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
439 return ('rootdir', $1, $1);
440 } elsif ($volname =~ m!^backup/([^/]+(?:\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\COMPRESSOR_RE}))?))))$!) {
442 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
443 return ('backup', $fn, $2);
445 return ('backup', $fn);
446 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
447 return ('snippets', $1);
450 die "unable to parse directory volume name '$volname'\n";
453 my $vtype_subdirs = {
455 rootdir
=> 'private',
456 iso
=> 'template/iso',
457 vztmpl
=> 'template/cache',
459 snippets
=> 'snippets',
462 sub get_vtype_subdirs
{
463 return $vtype_subdirs;
467 my ($class, $scfg, $vtype) = @_;
469 my $path = $scfg->{path
};
471 die "storage definintion has no path\n" if !$path;
473 my $subdir = $vtype_subdirs->{$vtype};
475 die "unknown vtype '$vtype'\n" if !defined($subdir);
477 return "$path/$subdir";
480 sub filesystem_path
{
481 my ($class, $scfg, $volname, $snapname) = @_;
483 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
484 $class->parse_volname($volname);
486 # Note: qcow2/qed has internal snapshot, so path is always
487 # the same (with or without snapshot => same file).
488 die "can't snapshot this image format\n"
489 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
491 my $dir = $class->get_subdir($scfg, $vtype);
493 $dir .= "/$vmid" if $vtype eq 'images';
495 my $path = "$dir/$name";
497 return wantarray ?
($path, $vmid, $vtype) : $path;
501 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
503 return $class->filesystem_path($scfg, $volname, $snapname);
507 my ($class, $storeid, $scfg, $volname) = @_;
509 # this only works for file based storage types
510 die "storage definition has no path\n" if !$scfg->{path
};
512 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
513 $class->parse_volname($volname);
515 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
517 die "create_base not possible with base image\n" if $isBase;
519 my $path = $class->filesystem_path($scfg, $volname);
521 my ($size, undef, $used, $parent) = file_size_info
($path);
522 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
524 die "volname '$volname' contains wrong information about parent\n"
525 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
528 $newname =~ s/^vm-/base-/;
530 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
533 my $newpath = $class->filesystem_path($scfg, $newvolname);
535 die "file '$newpath' already exists\n" if -f
$newpath;
537 rename($path, $newpath) ||
538 die "rename '$path' to '$newpath' failed - $!\n";
540 # We try to protect base volume
542 chmod(0444, $newpath); # nobody should write anything
544 # also try to set immutable flag
545 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
551 my $get_vm_disk_number = sub {
552 my ($disk_name, $scfg, $vmid, $suffix) = @_;
554 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
556 my $type = $scfg->{type
};
557 my $def = { %{$defaultData->{plugindata
}->{$type}} };
559 my $valid = $def->{format
}[0];
560 if ($valid->{subvol
}) {
561 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
564 if ($disk_name =~ m/$disk_regex/) {
571 sub get_next_vm_diskname
{
572 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
575 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
576 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
579 foreach my $disk (@$disk_list) {
580 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
581 $disk_ids->{$disknum} = 1 if defined($disknum);
584 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
585 if (!$disk_ids->{$i}) {
586 return "$prefix-$vmid-disk-$i$suffix";
590 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
593 sub find_free_diskname
{
594 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
596 my $disks = $class->list_images($storeid, $scfg, $vmid);
598 my $disk_list = [ map { $_->{volid
} } @$disks ];
600 return get_next_vm_diskname
($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
604 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
606 # this only works for file based storage types
607 die "storage definintion has no path\n" if !$scfg->{path
};
609 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
610 $class->parse_volname($volname);
612 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
614 die "this storage type does not support clone_image on snapshot\n" if $snap;
616 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
618 die "clone_image only works on base images\n" if !$isBase;
620 my $imagedir = $class->get_subdir($scfg, 'images');
621 $imagedir .= "/$vmid";
625 my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
627 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
629 my $newvol = "$basevmid/$basename/$vmid/$name";
631 my $path = $class->filesystem_path($scfg, $newvol);
633 # Note: we use relative paths, so we need to call chdir before qemu-img
635 local $CWD = $imagedir;
637 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
638 '-f', 'qcow2', $path];
650 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
652 my $imagedir = $class->get_subdir($scfg, 'images');
653 $imagedir .= "/$vmid";
657 $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
659 my (undef, $tmpfmt) = parse_name_dir
($name);
661 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
664 my $path = "$imagedir/$name";
666 die "disk image '$path' already exists\n" if -e
$path;
668 if ($fmt eq 'subvol') {
669 # only allow this if size = 0, so that user knows what he is doing
670 die "storage does not support subvol quotas\n" if $size != 0;
672 my $old_umask = umask(0022);
674 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
678 my $cmd = ['/usr/bin/qemu-img', 'create'];
680 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
682 push @$cmd, '-f', $fmt, $path, "${size}K";
684 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
692 return "$vmid/$name";
696 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
698 my $path = $class->filesystem_path($scfg, $volname);
701 # try to remove immutable flag
702 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
706 if (defined($format) && ($format eq 'subvol')) {
707 File
::Path
::remove_tree
($path);
709 if (!(-f
$path || -l
$path)) {
710 warn "disk image '$path' does not exist\n";
714 unlink($path) || die "unlink '$path' failed - $!\n";
717 # try to cleanup directory to not clutter storage with empty $vmid dirs if
718 # all images from a guest got deleted
719 my $dir = dirname
($path);
726 my ($filename, $timeout) = @_;
728 my $st = File
::stat::stat($filename);
730 if (S_ISDIR
($st->mode)) {
731 return wantarray ?
(0, 'subvol', 0, undef, $st->ctime) : 1;
736 run_command
(['/usr/bin/qemu-img', 'info', '--output=json', $filename],
738 outfunc
=> sub { $json .= shift },
739 errfunc
=> sub { warn "$_[0]\n" }
744 my $info = eval { decode_json
($json) };
745 warn "could not parse qemu-img info command output for '$filename'\n" if $@;
747 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
749 return wantarray ?
($size, $format, $used, $parent, $st->ctime) : $size;
752 sub volume_size_info
{
753 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
754 my $path = $class->filesystem_path($scfg, $volname);
755 return file_size_info
($path, $timeout);
760 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
762 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
764 return 1 if $running;
766 my $path = $class->filesystem_path($scfg, $volname);
768 my $format = ($class->parse_volname($volname))[6];
770 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
772 run_command
($cmd, timeout
=> 10);
777 sub volume_snapshot
{
778 my ($class, $scfg, $storeid, $volname, $snap) = @_;
780 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
782 my $path = $class->filesystem_path($scfg, $volname);
784 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
791 sub volume_rollback_is_possible
{
792 my ($class, $scfg, $storeid, $volname, $snap) = @_;
797 sub volume_snapshot_rollback
{
798 my ($class, $scfg, $storeid, $volname, $snap) = @_;
800 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
802 my $path = $class->filesystem_path($scfg, $volname);
804 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
811 sub volume_snapshot_delete
{
812 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
814 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
816 return 1 if $running;
818 my $path = $class->filesystem_path($scfg, $volname);
820 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
822 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
829 sub storage_can_replicate
{
830 my ($class, $scfg, $storeid, $format) = @_;
835 sub volume_has_feature
{
836 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_;
839 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
840 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
841 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
842 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
843 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
844 snap
=> {qcow2
=> 1} },
845 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
846 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
849 # clone_image creates a qcow2 volume
850 return 0 if $feature eq 'clone' &&
851 defined($opts->{valid_target_formats
}) &&
852 !(grep { $_ eq 'qcow2' } @{$opts->{valid_target_formats
}});
854 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
855 $class->parse_volname($volname);
861 $key = $isBase ?
'base' : 'current';
864 return 1 if defined($features->{$feature}->{$key}->{$format});
870 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
872 my $imagedir = $class->get_subdir($scfg, 'images');
874 my ($defFmt, $vaidFmts) = default_format
($scfg);
875 my $fmts = join ('|', @$vaidFmts);
879 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
881 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
887 next if !$vollist && defined($vmid) && ($owner ne $vmid);
889 my ($size, $format, $used, $parent, $ctime) = file_size_info
($fn);
890 next if !($format && defined($size));
893 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
894 my ($basevmid, $basename) = ($1, $2);
895 $volid = "$storeid:$basevmid/$basename/$owner/$name";
897 $volid = "$storeid:$owner/$name";
901 my $found = grep { $_ eq $volid } @$vollist;
906 volid
=> $volid, format
=> $format,
907 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
910 $info->{ctime
} = $ctime if $ctime;
918 # list templates ($tt = <iso|vztmpl|backup|snippets>)
919 my $get_subdir_files = sub {
920 my ($sid, $path, $tt, $vmid) = @_;
924 foreach my $fn (<$path/*>) {
926 my $st = File
::stat::stat($fn);
928 next if S_ISDIR
($st->mode);
933 next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
935 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
937 } elsif ($tt eq 'vztmpl') {
938 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
940 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
942 } elsif ($tt eq 'backup') {
943 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
944 next if $fn !~ m!/([^/]+\.(tgz|(?:(?:tar|vma)(?:\.(${\COMPRESSOR_RE}))?)))$!;
948 $info = { volid
=> "$sid:backup/$fn", format
=> $format };
950 if ($fn =~ m!^vzdump\-(?:lxc|qemu)\-(?:[1-9][0-9]{2,8})\-(\d{4})_(\d{2})_(\d{2})\-(\d{2})_(\d{2})_(\d{2})\.${format}$!) {
951 my $epoch = timelocal
($6, $5, $4, $3, $2-1, $1 - 1900);
952 $info->{ctime
} = $epoch;
955 if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
956 $info->{vmid
} = $vmid // $1;
960 } elsif ($tt eq 'snippets') {
963 volid
=> "$sid:snippets/". basename
($fn),
968 $info->{size
} = $st->size;
969 $info->{ctime
} //= $st->ctime;
978 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
981 my $vmlist = PVE
::Cluster
::get_vmlist
();
982 foreach my $type (@$content_types) {
985 if ($type eq 'images' || $type eq 'rootdir') {
986 $data = $class->list_images($storeid, $scfg, $vmid);
987 } elsif ($scfg->{path
}) {
988 my $path = $class->get_subdir($scfg, $type);
990 if ($type eq 'iso' && !defined($vmid)) {
991 $data = $get_subdir_files->($storeid, $path, 'iso');
992 } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
993 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
994 } elsif ($type eq 'backup') {
995 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
996 } elsif ($type eq 'snippets') {
997 $data = $get_subdir_files->($storeid, $path, 'snippets');
1003 foreach my $item (@$data) {
1004 if ($type eq 'images' || $type eq 'rootdir') {
1005 my $vminfo = $vmlist->{ids
}->{$item->{vmid
}};
1007 if (defined($vminfo)) {
1008 $vmtype = $vminfo->{type
};
1010 if (defined($vmtype) && $vmtype eq 'lxc') {
1011 $item->{content
} = 'rootdir';
1013 $item->{content
} = 'images';
1015 next if $type ne $item->{content
};
1017 $item->{content
} = $type;
1028 my ($class, $storeid, $scfg, $cache) = @_;
1030 my $path = $scfg->{path
};
1032 die "storage definintion has no path\n" if !$path;
1035 my $res = PVE
::Tools
::df
($path, $timeout);
1037 return undef if !$res || !$res->{total
};
1039 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
1042 sub volume_snapshot_list
{
1043 my ($class, $scfg, $storeid, $volname) = @_;
1045 # implement in subclass
1046 die "Volume_snapshot_list is not implemented for $class";
1048 # return an empty array if dataset does not exist.
1051 sub activate_storage
{
1052 my ($class, $storeid, $scfg, $cache) = @_;
1054 my $path = $scfg->{path
};
1056 die "storage definintion has no path\n" if !$path;
1058 # this path test may hang indefinitely on unresponsive mounts
1060 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
1061 die "unable to activate storage '$storeid' - " .
1062 "directory '$path' does not exist or is unreachable\n";
1066 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1068 if (defined($scfg->{content
})) {
1069 foreach my $vtype (keys %$vtype_subdirs) {
1070 # OpenVZMigrate uses backup (dump) dir
1071 if (defined($scfg->{content
}->{$vtype}) ||
1072 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
1073 my $subdir = $class->get_subdir($scfg, $vtype);
1074 mkpath
$subdir if $subdir ne $path;
1080 sub deactivate_storage
{
1081 my ($class, $storeid, $scfg, $cache) = @_;
1083 # do nothing by default
1087 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1089 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1094 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1099 sub activate_volume
{
1100 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1102 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1104 # check is volume exists
1105 if ($scfg->{path
}) {
1106 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
1108 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
1112 sub deactivate_volume
{
1113 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1115 # do nothing by default
1118 sub check_connection
{
1119 my ($class, $storeid, $scfg) = @_;
1120 # do nothing by default
1124 # Import/Export interface:
1125 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1126 # the default implementations will return this if $scfg->{path} is set,
1127 # mimicking the old PVE::Storage::storage_migrate() function.
1129 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1130 # functions in case the format doesn't match their specialized
1131 # implementations to reuse the raw/tar code.
1133 # Format specification:
1134 # The following formats are all prefixed with image information in the form
1135 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1136 # to preallocate the image on storages which require it.
1138 # raw+size: (image files only)
1139 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1140 # qcow2+size, vmdk: (image files only)
1141 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1142 # files which are already in qcow2 format, or via `qemu-img convert`.
1143 # Note that these formats are only valid with $with_snapshots being true.
1144 # tar+size: (subvolumes only)
1145 # A GNU tar stream containing just the inner contents of the subvolume.
1146 # This does not distinguish between the contents of a privileged or
1147 # unprivileged container. In other words, this is from the root user
1148 # namespace's point of view with no uid-mapping in effect.
1149 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1151 # Plugins may reuse these helpers. Changes to the header format should be
1152 # reflected by changes to the function prototypes.
1153 sub write_common_header
($$) {
1154 my ($fh, $image_size_in_bytes) = @_;
1155 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1158 sub read_common_header
($) {
1160 sysread($fh, my $size, 8);
1161 $size = unpack('Q<', $size);
1162 die "import: no size found in export header, aborting.\n" if !defined($size);
1163 die "import: got a bad size (not a multiple of 1K), aborting.\n" if ($size&1023);
1168 # Export a volume into a file handle as a stream of desired format.
1170 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1171 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1172 my $file = $class->path($scfg, $volname, $storeid)
1173 or goto unsupported
;
1174 my ($size, $file_format) = file_size_info
($file);
1176 if ($format eq 'raw+size') {
1177 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1178 write_common_header
($fh, $size);
1179 if ($file_format eq 'raw') {
1180 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1182 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1183 output
=> '>&'.fileno($fh));
1186 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1187 my $data_format = $1;
1188 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1189 write_common_header
($fh, $size);
1190 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1192 } elsif ($format eq 'tar+size') {
1193 goto unsupported
if $file_format ne 'subvol';
1194 write_common_header
($fh, $size);
1195 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1196 output
=> '>&'.fileno($fh));
1201 die "volume export format $format not available for $class";
1204 sub volume_export_formats
{
1205 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1206 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1207 my $file = $class->path($scfg, $volname, $storeid)
1209 my ($size, $format) = file_size_info
($file);
1211 if ($with_snapshots) {
1212 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1215 return ('tar+size') if $format eq 'subvol';
1216 return ('raw+size');
1221 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1223 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1225 die "volume import format '$format' not available for $class\n"
1226 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1227 my $data_format = $1;
1229 die "format $format cannot be imported without snapshots\n"
1230 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1231 die "format $format cannot be imported with snapshots\n"
1232 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1234 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1235 $class->parse_volname($volname);
1237 # XXX: Should we bother with conversion routines at this level? This won't
1238 # happen without manual CLI usage, so for now we just error out...
1239 die "cannot import format $format into a file of format $file_format\n"
1240 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1242 # Check for an existing file first since interrupting alloc_image doesn't
1244 my $file = $class->path($scfg, $volname, $storeid);
1246 die "file '$file' already exists\n" if !$allow_rename;
1247 warn "file '$file' already exists - importing with a different name\n";
1251 my ($size) = read_common_header
($fh);
1252 $size = int($size/1024);
1255 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1256 my $oldname = $volname;
1257 $volname = $allocname;
1258 if (defined($name) && $allocname ne $oldname) {
1259 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1261 my $file = $class->path($scfg, $volname, $storeid)
1262 or die "internal error: failed to get path to newly allocated volume $volname\n";
1263 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1264 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1265 input
=> '<&'.fileno($fh));
1266 } elsif ($data_format eq 'tar') {
1267 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1268 input
=> '<&'.fileno($fh));
1270 die "volume import format '$format' not available for $class";
1274 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1279 return "$storeid:$volname";
1282 sub volume_import_formats
{
1283 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1284 if ($scfg->{path
} && !defined($base_snapshot)) {
1285 my $format = ($class->parse_volname($volname))[6];
1286 if ($with_snapshots) {
1287 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1290 return ('tar+size') if $format eq 'subvol';
1291 return ('raw+size');