1 package PVE
::Storage
::Plugin
;
12 use PVE
::Tools
qw(run_command);
13 use PVE
::JSONSchema
qw(get_standard_option register_standard_option);
14 use PVE
::Cluster
qw(cfs_register_file);
18 use base
qw(PVE::SectionConfig);
20 use constant COMPRESSOR_RE
=> 'gz|lzo|zst';
22 use constant NOTES_EXT
=> ".notes";
24 our @COMMON_TAR_FLAGS = qw(
26 -p --sparse --numeric-owner --acls
27 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
28 --warning=no-file-ignored --warning=no-xattr-write
31 our @SHARED_STORAGE = (
44 our $MAX_VOLUMES_PER_GUEST = 1024;
46 cfs_register_file
('storage.cfg',
47 sub { __PACKAGE__-
>parse_config(@_); },
48 sub { __PACKAGE__-
>write_config(@_); });
52 type
=> 'integer', minimum
=> '0',
53 format_description
=> 'N',
56 our $prune_backups_format = {
59 description
=> 'Keep all backups. Conflicts with the other options when true.',
64 description
=> 'Keep the last <N> backups.',
68 description
=> 'Keep backups for the last <N> different hours. If there is more' .
69 'than one backup for a single hour, only the latest one is kept.'
73 description
=> 'Keep backups for the last <N> different days. If there is more' .
74 'than one backup for a single day, only the latest one is kept.'
78 description
=> 'Keep backups for the last <N> different weeks. If there is more' .
79 'than one backup for a single week, only the latest one is kept.'
83 description
=> 'Keep backups for the last <N> different months. If there is more' .
84 'than one backup for a single month, only the latest one is kept.'
88 description
=> 'Keep backups for the last <N> different years. If there is more' .
89 'than one backup for a single year, only the latest one is kept.'
92 PVE
::JSONSchema
::register_format
('prune-backups', $prune_backups_format, \
&validate_prune_backups
);
93 sub validate_prune_backups
{
94 my ($prune_backups) = @_;
96 my $keep_all = delete $prune_backups->{'keep-all'};
98 if (!scalar(grep {$_ > 0} values %{$prune_backups})) {
99 $prune_backups = { 'keep-all' => 1 };
100 } elsif ($keep_all) {
101 die "keep-all cannot be set together with other options.\n";
104 return $prune_backups;
106 register_standard_option
('prune-backups', {
107 description
=> "The retention options with shorter intervals are processed first " .
108 "with --keep-last being the very first one. Each option covers a " .
109 "specific period of time. We say that backups within this period " .
110 "are covered by this option. The next option does not take care " .
111 "of already covered backups and only considers older backups.",
114 format
=> 'prune-backups',
119 type
=> { description
=> "Storage type." },
120 storage
=> get_standard_option
('pve-storage-id',
121 { completion
=> \
&PVE
::Storage
::complete_storage
}),
122 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
124 description
=> "Allowed content types.\n\nNOTE: the value " .
125 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
126 type
=> 'string', format
=> 'pve-storage-content-list',
128 completion
=> \
&PVE
::Storage
::complete_content_type
,
131 description
=> "Flag to disable the storage.",
136 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
141 'prune-backups' => get_standard_option
('prune-backups'),
143 description
=> "Mark storage as shared.",
148 description
=> "Default image format.",
149 type
=> 'string', format
=> 'pve-storage-format',
155 sub content_hash_to_string
{
159 foreach my $ct (keys %$hash) {
160 push @cta, $ct if $hash->{$ct};
163 return join(',', @cta);
166 sub valid_content_types
{
169 my $def = $defaultData->{plugindata
}->{$type};
173 return $def->{content
}->[0];
179 my $type = $scfg->{type
};
180 my $def = $defaultData->{plugindata
}->{$type};
182 my $def_format = 'raw';
183 my $valid_formats = [ $def_format ];
185 if (defined($def->{format
})) {
186 $def_format = $scfg->{format
} || $def->{format
}->[1];
187 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
190 return wantarray ?
($def_format, $valid_formats) : $def_format;
193 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
195 my ($path, $noerr) = @_;
197 # fixme: exclude more shell meta characters?
198 # we need absolute paths
199 if ($path !~ m
|^/[^;\
(\
)]+|) {
200 return undef if $noerr;
201 die "value does not look like a valid absolute path\n";
206 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
208 my ($server, $noerr) = @_;
210 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
211 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
213 return undef if $noerr;
214 die "value does not look like a valid server name or IP address\n";
219 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
221 my ($name, $noerr) = @_;
223 if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) {
224 return undef if $noerr;
225 die "lvm name '$name' contains illegal characters\n";
231 # fixme: do we need this
232 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
234 # my ($portal, $noerr) = @_;
236 # # IP with optional port
237 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
238 # return undef if $noerr;
239 # die "value does not look like a valid portal address\n";
244 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
245 sub verify_portal_dns
{
246 my ($portal, $noerr) = @_;
248 # IP or DNS name with optional port
249 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
250 return undef if $noerr;
251 die "value does not look like a valid portal address\n";
256 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
258 my ($ct, $noerr) = @_;
260 my $valid_content = valid_content_types
('dir'); # dir includes all types
262 if (!$valid_content->{$ct}) {
263 return undef if $noerr;
264 die "invalid content type '$ct'\n";
270 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
272 my ($fmt, $noerr) = @_;
274 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
275 return undef if $noerr;
276 die "invalid format '$fmt'\n";
282 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
284 my ($value, $noerr) = @_;
286 # mount options (see man fstab)
287 if ($value !~ m/^\S+$/) {
288 return undef if $noerr;
289 die "invalid options '$value'\n";
295 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
296 sub parse_volume_id
{
297 my ($volid, $noerr) = @_;
299 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
300 return wantarray ?
($1, $2) : $1;
302 return undef if $noerr;
303 die "unable to parse volume ID '$volid'\n";
311 sub parse_section_header
{
312 my ($class, $line) = @_;
314 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
315 my ($type, $storeid) = (lc($1), $2);
316 my $errmsg = undef; # set if you want to skip whole section
317 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
319 my $config = {}; # to return additional attributes
320 return ($type, $storeid, $errmsg, $config);
326 my ($class, $type, $key, $value) = @_;
328 my $def = $defaultData->{plugindata
}->{$type};
330 if ($key eq 'content') {
331 my $valid_content = $def->{content
}->[0];
335 foreach my $c (PVE
::Tools
::split_list
($value)) {
336 if (!$valid_content->{$c}) {
337 warn "storage does not support content type '$c'\n";
343 if ($res->{none
} && scalar (keys %$res) > 1) {
344 die "unable to combine 'none' with other content types\n";
348 } elsif ($key eq 'format') {
349 my $valid_formats = $def->{format
}->[0];
351 if (!$valid_formats->{$value}) {
352 warn "storage does not support format '$value'\n";
357 } elsif ($key eq 'nodes') {
360 foreach my $node (PVE
::Tools
::split_list
($value)) {
361 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
367 # no node restrictions for local storage
368 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
369 # die "storage '$storeid' does not allow node restrictions\n";
379 my ($class, $type, $key, $value) = @_;
381 if ($key eq 'nodes') {
382 return join(',', keys(%$value));
383 } elsif ($key eq 'content') {
384 my $res = content_hash_to_string
($value) || 'none';
392 my ($class, $filename, $raw) = @_;
394 my $cfg = $class->SUPER::parse_config
($filename, $raw);
395 my $ids = $cfg->{ids
};
397 # make sure we have a reasonable 'local:' storage
398 # we want 'local' to be always the same 'type' (on all cluster nodes)
399 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
400 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
403 priority
=> 0, # force first entry
404 path
=> '/var/lib/vz',
406 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, snippets
=> 1},
410 # make sure we have a path
411 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
413 # remove node restrictions for local storage
414 delete($ids->{local}->{nodes
});
416 foreach my $storeid (keys %$ids) {
417 my $d = $ids->{$storeid};
418 my $type = $d->{type
};
420 my $def = $defaultData->{plugindata
}->{$type};
422 if ($def->{content
}) {
423 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
425 if (grep { $_ eq $type } @SHARED_STORAGE) {
433 # Storage implementation
435 # called during addition of storage (before the new storage config got written)
436 # die to abort additon if there are (grave) problems
437 # NOTE: runs in a storage config *locked* context
439 my ($class, $storeid, $scfg, %param) = @_;
441 # do nothing by default
445 # called during storage configuration update (before the updated storage config got written)
446 # die to abort the update if there are (grave) problems
447 # NOTE: runs in a storage config *locked* context
449 my ($class, $storeid, $scfg, %param) = @_;
451 # do nothing by default
455 # called during deletion of storage (before the new storage config got written)
456 # and if the activate check on addition fails, to cleanup all storage traces
457 # which on_add_hook may have created.
458 # die to abort deletion if there are (very grave) problems
459 # NOTE: runs in a storage config *locked* context
461 my ($class, $storeid, $scfg) = @_;
463 # do nothing by default
467 sub cluster_lock_storage
{
468 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
472 my $lockid = "pve-storage-$storeid";
473 my $lockdir = "/var/lock/pve-manager";
475 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
478 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
487 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
488 return ($1, $3, $2); # (name, format, isBase)
491 die "unable to parse volume filename '$name'\n";
495 my ($class, $volname) = @_;
497 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
498 my ($basedvmid, $basename) = ($1, $2);
499 parse_name_dir
($basename);
500 my ($vmid, $name) = ($3, $4);
501 my (undef, $format, $isBase) = parse_name_dir
($name);
502 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
503 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
504 my ($vmid, $name) = ($1, $2);
505 my (undef, $format, $isBase) = parse_name_dir
($name);
506 return ('images', $name, $vmid, undef, undef, $isBase, $format);
507 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::iso_extension_re)$!) {
509 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
510 return ('vztmpl', $1);
511 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
512 return ('rootdir', $1, $1);
513 } elsif ($volname =~ m!^backup/([^/]+(?:\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\COMPRESSOR_RE}))?))))$!) {
515 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
516 return ('backup', $fn, $2);
518 return ('backup', $fn);
519 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
520 return ('snippets', $1);
523 die "unable to parse directory volume name '$volname'\n";
526 my $vtype_subdirs = {
528 rootdir
=> 'private',
529 iso
=> 'template/iso',
530 vztmpl
=> 'template/cache',
532 snippets
=> 'snippets',
535 sub get_vtype_subdirs
{
536 return $vtype_subdirs;
540 my ($class, $scfg, $vtype) = @_;
542 my $path = $scfg->{path
};
544 die "storage definintion has no path\n" if !$path;
546 my $subdir = $vtype_subdirs->{$vtype};
548 die "unknown vtype '$vtype'\n" if !defined($subdir);
550 return "$path/$subdir";
553 sub filesystem_path
{
554 my ($class, $scfg, $volname, $snapname) = @_;
556 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
557 $class->parse_volname($volname);
559 # Note: qcow2/qed has internal snapshot, so path is always
560 # the same (with or without snapshot => same file).
561 die "can't snapshot this image format\n"
562 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
564 my $dir = $class->get_subdir($scfg, $vtype);
566 $dir .= "/$vmid" if $vtype eq 'images';
568 my $path = "$dir/$name";
570 return wantarray ?
($path, $vmid, $vtype) : $path;
574 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
576 return $class->filesystem_path($scfg, $volname, $snapname);
580 my ($class, $storeid, $scfg, $volname) = @_;
582 # this only works for file based storage types
583 die "storage definition has no path\n" if !$scfg->{path
};
585 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
586 $class->parse_volname($volname);
588 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
590 die "create_base not possible with base image\n" if $isBase;
592 my $path = $class->filesystem_path($scfg, $volname);
594 my ($size, undef, $used, $parent) = file_size_info
($path);
595 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
597 die "volname '$volname' contains wrong information about parent\n"
598 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
601 $newname =~ s/^vm-/base-/;
603 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
606 my $newpath = $class->filesystem_path($scfg, $newvolname);
608 die "file '$newpath' already exists\n" if -f
$newpath;
610 rename($path, $newpath) ||
611 die "rename '$path' to '$newpath' failed - $!\n";
613 # We try to protect base volume
615 chmod(0444, $newpath); # nobody should write anything
617 # also try to set immutable flag
618 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
624 my $get_vm_disk_number = sub {
625 my ($disk_name, $scfg, $vmid, $suffix) = @_;
627 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
629 my $type = $scfg->{type
};
630 my $def = { %{$defaultData->{plugindata
}->{$type}} };
632 my $valid = $def->{format
}[0];
633 if ($valid->{subvol
}) {
634 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
637 if ($disk_name =~ m/$disk_regex/) {
644 sub get_next_vm_diskname
{
645 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
648 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
649 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
652 foreach my $disk (@$disk_list) {
653 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
654 $disk_ids->{$disknum} = 1 if defined($disknum);
657 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
658 if (!$disk_ids->{$i}) {
659 return "$prefix-$vmid-disk-$i$suffix";
663 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
666 sub find_free_diskname
{
667 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
669 my $disks = $class->list_images($storeid, $scfg, $vmid);
671 my $disk_list = [ map { $_->{volid
} } @$disks ];
673 return get_next_vm_diskname
($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
677 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
679 # this only works for file based storage types
680 die "storage definintion has no path\n" if !$scfg->{path
};
682 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
683 $class->parse_volname($volname);
685 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
687 die "this storage type does not support clone_image on snapshot\n" if $snap;
689 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
691 die "clone_image only works on base images\n" if !$isBase;
693 my $imagedir = $class->get_subdir($scfg, 'images');
694 $imagedir .= "/$vmid";
698 my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
700 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
702 my $newvol = "$basevmid/$basename/$vmid/$name";
704 my $path = $class->filesystem_path($scfg, $newvol);
706 # Note: we use relative paths, so we need to call chdir before qemu-img
708 local $CWD = $imagedir;
710 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
711 '-f', 'qcow2', $path];
723 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
725 my $imagedir = $class->get_subdir($scfg, 'images');
726 $imagedir .= "/$vmid";
730 $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
732 my (undef, $tmpfmt) = parse_name_dir
($name);
734 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
737 my $path = "$imagedir/$name";
739 die "disk image '$path' already exists\n" if -e
$path;
741 if ($fmt eq 'subvol') {
742 # only allow this if size = 0, so that user knows what he is doing
743 die "storage does not support subvol quotas\n" if $size != 0;
745 my $old_umask = umask(0022);
747 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
751 my $cmd = ['/usr/bin/qemu-img', 'create'];
753 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
755 push @$cmd, '-f', $fmt, $path, "${size}K";
757 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
765 return "$vmid/$name";
769 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
771 my $path = $class->filesystem_path($scfg, $volname);
774 # try to remove immutable flag
775 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
779 if (defined($format) && ($format eq 'subvol')) {
780 File
::Path
::remove_tree
($path);
782 if (!(-f
$path || -l
$path)) {
783 warn "disk image '$path' does not exist\n";
787 unlink($path) || die "unlink '$path' failed - $!\n";
790 # try to cleanup directory to not clutter storage with empty $vmid dirs if
791 # all images from a guest got deleted
792 my $dir = dirname
($path);
799 my ($filename, $timeout) = @_;
801 my $st = File
::stat::stat($filename);
804 my $extramsg = -l
$filename ?
' - dangling symlink?' : '';
805 warn "failed to stat '$filename'$extramsg\n";
809 if (S_ISDIR
($st->mode)) {
810 return wantarray ?
(0, 'subvol', 0, undef, $st->ctime) : 1;
815 run_command
(['/usr/bin/qemu-img', 'info', '--output=json', $filename],
817 outfunc
=> sub { $json .= shift },
818 errfunc
=> sub { warn "$_[0]\n" }
823 my $info = eval { decode_json
($json) };
824 warn "could not parse qemu-img info command output for '$filename'\n" if $@;
826 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
828 return wantarray ?
($size, $format, $used, $parent, $st->ctime) : $size;
831 sub get_volume_notes
{
832 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
834 die "volume notes are not supported for $class";
837 sub update_volume_notes
{
838 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
840 die "volume notes are not supported for $class";
843 sub volume_size_info
{
844 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
845 my $path = $class->filesystem_path($scfg, $volname);
846 return file_size_info
($path, $timeout);
851 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
853 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
855 return 1 if $running;
857 my $path = $class->filesystem_path($scfg, $volname);
859 my $format = ($class->parse_volname($volname))[6];
861 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
863 run_command
($cmd, timeout
=> 10);
868 sub volume_snapshot
{
869 my ($class, $scfg, $storeid, $volname, $snap) = @_;
871 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
873 my $path = $class->filesystem_path($scfg, $volname);
875 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
882 sub volume_rollback_is_possible
{
883 my ($class, $scfg, $storeid, $volname, $snap) = @_;
888 sub volume_snapshot_rollback
{
889 my ($class, $scfg, $storeid, $volname, $snap) = @_;
891 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
893 my $path = $class->filesystem_path($scfg, $volname);
895 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
902 sub volume_snapshot_delete
{
903 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
905 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
907 return 1 if $running;
909 my $path = $class->filesystem_path($scfg, $volname);
911 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
913 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
920 sub volume_snapshot_needs_fsfreeze
{
924 sub storage_can_replicate
{
925 my ($class, $scfg, $storeid, $format) = @_;
930 sub volume_has_feature
{
931 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_;
934 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
935 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
936 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
937 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
938 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
939 snap
=> {qcow2
=> 1} },
940 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
941 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
944 # clone_image creates a qcow2 volume
945 return 0 if $feature eq 'clone' &&
946 defined($opts->{valid_target_formats
}) &&
947 !(grep { $_ eq 'qcow2' } @{$opts->{valid_target_formats
}});
949 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
950 $class->parse_volname($volname);
956 $key = $isBase ?
'base' : 'current';
959 return 1 if defined($features->{$feature}->{$key}->{$format});
965 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
967 my $imagedir = $class->get_subdir($scfg, 'images');
969 my ($defFmt, $vaidFmts) = default_format
($scfg);
970 my $fmts = join ('|', @$vaidFmts);
974 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
976 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
982 next if !$vollist && defined($vmid) && ($owner ne $vmid);
984 my ($size, $format, $used, $parent, $ctime) = file_size_info
($fn);
985 next if !($format && defined($size));
988 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
989 my ($basevmid, $basename) = ($1, $2);
990 $volid = "$storeid:$basevmid/$basename/$owner/$name";
992 $volid = "$storeid:$owner/$name";
996 my $found = grep { $_ eq $volid } @$vollist;
1001 volid
=> $volid, format
=> $format,
1002 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
1005 $info->{ctime
} = $ctime if $ctime;
1013 # list templates ($tt = <iso|vztmpl|backup|snippets>)
1014 my $get_subdir_files = sub {
1015 my ($sid, $path, $tt, $vmid) = @_;
1019 foreach my $fn (<$path/*>) {
1020 my $st = File
::stat::stat($fn);
1022 next if (!$st || S_ISDIR
($st->mode));
1027 next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
1029 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
1031 } elsif ($tt eq 'vztmpl') {
1032 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
1034 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
1036 } elsif ($tt eq 'backup') {
1037 next if $fn !~ m!/([^/]+\.(tgz|(?:(?:tar|vma)(?:\.(${\COMPRESSOR_RE}))?)))$!;
1042 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
1044 $info = { volid
=> "$sid:backup/$fn", format
=> $format };
1046 my $archive_info = eval { PVE
::Storage
::archive_info
($fn) } // {};
1048 $info->{ctime
} = $archive_info->{ctime
} if defined($archive_info->{ctime
});
1050 if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
1051 $info->{vmid
} = $vmid // $1;
1054 my $notes_fn = $original.NOTES_EXT
;
1056 my $notes = PVE
::Tools
::file_read_firstline
($notes_fn);
1057 $info->{notes
} = $notes if defined($notes);
1060 } elsif ($tt eq 'snippets') {
1063 volid
=> "$sid:snippets/". basename
($fn),
1064 format
=> 'snippet',
1068 $info->{size
} = $st->size;
1069 $info->{ctime
} //= $st->ctime;
1078 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
1081 my $vmlist = PVE
::Cluster
::get_vmlist
();
1082 foreach my $type (@$content_types) {
1085 if ($type eq 'images' || $type eq 'rootdir') {
1086 $data = $class->list_images($storeid, $scfg, $vmid);
1087 } elsif ($scfg->{path
}) {
1088 my $path = $class->get_subdir($scfg, $type);
1090 if ($type eq 'iso' && !defined($vmid)) {
1091 $data = $get_subdir_files->($storeid, $path, 'iso');
1092 } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
1093 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
1094 } elsif ($type eq 'backup') {
1095 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
1096 } elsif ($type eq 'snippets') {
1097 $data = $get_subdir_files->($storeid, $path, 'snippets');
1103 foreach my $item (@$data) {
1104 if ($type eq 'images' || $type eq 'rootdir') {
1105 my $vminfo = $vmlist->{ids
}->{$item->{vmid
}};
1107 if (defined($vminfo)) {
1108 $vmtype = $vminfo->{type
};
1110 if (defined($vmtype) && $vmtype eq 'lxc') {
1111 $item->{content
} = 'rootdir';
1113 $item->{content
} = 'images';
1115 next if $type ne $item->{content
};
1117 $item->{content
} = $type;
1128 my ($class, $storeid, $scfg, $cache) = @_;
1130 my $path = $scfg->{path
};
1132 die "storage definintion has no path\n" if !$path;
1135 my $res = PVE
::Tools
::df
($path, $timeout);
1137 return undef if !$res || !$res->{total
};
1139 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
1142 sub volume_snapshot_list
{
1143 my ($class, $scfg, $storeid, $volname) = @_;
1145 # implement in subclass
1146 die "Volume_snapshot_list is not implemented for $class";
1148 # return an empty array if dataset does not exist.
1151 sub activate_storage
{
1152 my ($class, $storeid, $scfg, $cache) = @_;
1154 my $path = $scfg->{path
};
1156 die "storage definintion has no path\n" if !$path;
1158 # this path test may hang indefinitely on unresponsive mounts
1160 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
1161 die "unable to activate storage '$storeid' - " .
1162 "directory '$path' does not exist or is unreachable\n";
1166 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1168 if (defined($scfg->{content
})) {
1169 foreach my $vtype (keys %$vtype_subdirs) {
1170 # OpenVZMigrate uses backup (dump) dir
1171 if (defined($scfg->{content
}->{$vtype}) ||
1172 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
1173 my $subdir = $class->get_subdir($scfg, $vtype);
1174 mkpath
$subdir if $subdir ne $path;
1180 sub deactivate_storage
{
1181 my ($class, $storeid, $scfg, $cache) = @_;
1183 # do nothing by default
1187 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1189 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1194 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1199 sub activate_volume
{
1200 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1202 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1204 # check is volume exists
1205 if ($scfg->{path
}) {
1206 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
1208 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
1212 sub deactivate_volume
{
1213 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1215 # do nothing by default
1218 sub check_connection
{
1219 my ($class, $storeid, $scfg) = @_;
1220 # do nothing by default
1225 my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
1227 $logfunc //= sub { print "$_[1]\n" };
1229 my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
1231 my $backup_groups = {};
1232 my $prune_list = [];
1234 foreach my $backup (@{$backups}) {
1235 my $volid = $backup->{volid
};
1236 my $backup_vmid = $backup->{vmid
};
1237 my $archive_info = eval { PVE
::Storage
::archive_info
($volid) } // {};
1238 my $backup_type = $archive_info->{type
} // 'unknown';
1240 next if defined($type) && $type ne $backup_type;
1243 ctime
=> $backup->{ctime
},
1244 type
=> $backup_type,
1248 $prune_entry->{vmid
} = $backup_vmid if defined($backup_vmid);
1250 if ($archive_info->{is_std_name
}) {
1251 $prune_entry->{ctime
} = $archive_info->{ctime
};
1252 my $group = "$backup_type/$backup_vmid";
1253 push @{$backup_groups->{$group}}, $prune_entry;
1255 # ignore backups that don't use the standard naming scheme
1256 $prune_entry->{mark
} = 'protected';
1259 push @{$prune_list}, $prune_entry;
1262 foreach my $backup_group (values %{$backup_groups}) {
1263 PVE
::Storage
::prune_mark_backup_group
($backup_group, $keep);
1268 foreach my $prune_entry (@{$prune_list}) {
1269 next if $prune_entry->{mark
} ne 'remove';
1271 my $volid = $prune_entry->{volid
};
1272 $logfunc->('info', "removing backup '$volid'");
1274 my (undef, $volname) = parse_volume_id
($volid);
1275 my $archive_path = $class->filesystem_path($scfg, $volname);
1276 PVE
::Storage
::archive_remove
($archive_path);
1279 $logfunc->('err', "error when removing backup '$volid' - $err\n");
1284 die "error pruning backups - check log\n" if $failed;
1289 # Import/Export interface:
1290 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1291 # the default implementations will return this if $scfg->{path} is set,
1292 # mimicking the old PVE::Storage::storage_migrate() function.
1294 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1295 # functions in case the format doesn't match their specialized
1296 # implementations to reuse the raw/tar code.
1298 # Format specification:
1299 # The following formats are all prefixed with image information in the form
1300 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1301 # to preallocate the image on storages which require it.
1303 # raw+size: (image files only)
1304 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1305 # qcow2+size, vmdk: (image files only)
1306 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1307 # files which are already in qcow2 format, or via `qemu-img convert`.
1308 # Note that these formats are only valid with $with_snapshots being true.
1309 # tar+size: (subvolumes only)
1310 # A GNU tar stream containing just the inner contents of the subvolume.
1311 # This does not distinguish between the contents of a privileged or
1312 # unprivileged container. In other words, this is from the root user
1313 # namespace's point of view with no uid-mapping in effect.
1314 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1316 # Plugins may reuse these helpers. Changes to the header format should be
1317 # reflected by changes to the function prototypes.
1318 sub write_common_header
($$) {
1319 my ($fh, $image_size_in_bytes) = @_;
1320 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1323 sub read_common_header
($) {
1325 sysread($fh, my $size, 8);
1326 $size = unpack('Q<', $size);
1327 die "import: no size found in export header, aborting.\n" if !defined($size);
1328 die "import: got a bad size (not a multiple of 1K), aborting.\n" if ($size&1023);
1333 # Export a volume into a file handle as a stream of desired format.
1335 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1336 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1337 my $file = $class->path($scfg, $volname, $storeid)
1338 or goto unsupported
;
1339 my ($size, $file_format) = file_size_info
($file);
1341 if ($format eq 'raw+size') {
1342 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1343 write_common_header
($fh, $size);
1344 if ($file_format eq 'raw') {
1345 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1347 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1348 output
=> '>&'.fileno($fh));
1351 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1352 my $data_format = $1;
1353 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1354 write_common_header
($fh, $size);
1355 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1357 } elsif ($format eq 'tar+size') {
1358 goto unsupported
if $file_format ne 'subvol';
1359 write_common_header
($fh, $size);
1360 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1361 output
=> '>&'.fileno($fh));
1366 die "volume export format $format not available for $class";
1369 sub volume_export_formats
{
1370 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1371 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1372 my $file = $class->path($scfg, $volname, $storeid)
1374 my ($size, $format) = file_size_info
($file);
1376 if ($with_snapshots) {
1377 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1380 return ('tar+size') if $format eq 'subvol';
1381 return ('raw+size');
1386 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1388 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1390 die "volume import format '$format' not available for $class\n"
1391 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1392 my $data_format = $1;
1394 die "format $format cannot be imported without snapshots\n"
1395 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1396 die "format $format cannot be imported with snapshots\n"
1397 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1399 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1400 $class->parse_volname($volname);
1402 # XXX: Should we bother with conversion routines at this level? This won't
1403 # happen without manual CLI usage, so for now we just error out...
1404 die "cannot import format $format into a file of format $file_format\n"
1405 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1407 # Check for an existing file first since interrupting alloc_image doesn't
1409 my $file = $class->path($scfg, $volname, $storeid);
1411 die "file '$file' already exists\n" if !$allow_rename;
1412 warn "file '$file' already exists - importing with a different name\n";
1416 my ($size) = read_common_header
($fh);
1417 $size = int($size/1024);
1420 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1421 my $oldname = $volname;
1422 $volname = $allocname;
1423 if (defined($name) && $allocname ne $oldname) {
1424 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1426 my $file = $class->path($scfg, $volname, $storeid)
1427 or die "internal error: failed to get path to newly allocated volume $volname\n";
1428 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1429 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1430 input
=> '<&'.fileno($fh));
1431 } elsif ($data_format eq 'tar') {
1432 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1433 input
=> '<&'.fileno($fh));
1435 die "volume import format '$format' not available for $class";
1439 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1444 return "$storeid:$volname";
1447 sub volume_import_formats
{
1448 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1449 if ($scfg->{path
} && !defined($base_snapshot)) {
1450 my $format = ($class->parse_volname($volname))[6];
1451 if ($with_snapshots) {
1452 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1455 return ('tar+size') if $format eq 'subvol';
1456 return ('raw+size');