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 our @COMMON_TAR_FLAGS = qw(
23 -p --sparse --numeric-owner --acls
24 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
25 --warning=no-file-ignored --warning=no-xattr-write
28 our @SHARED_STORAGE = (
39 our $MAX_VOLUMES_PER_GUEST = 1024;
41 cfs_register_file
('storage.cfg',
42 sub { __PACKAGE__-
>parse_config(@_); },
43 sub { __PACKAGE__-
>write_config(@_); });
48 type
=> { description
=> "Storage type." },
49 storage
=> get_standard_option
('pve-storage-id',
50 { completion
=> \
&PVE
::Storage
::complete_storage
}),
51 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
53 description
=> "Allowed content types.\n\nNOTE: the value " .
54 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
55 type
=> 'string', format
=> 'pve-storage-content-list',
57 completion
=> \
&PVE
::Storage
::complete_content_type
,
60 description
=> "Flag to disable the storage.",
65 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
71 description
=> "Mark storage as shared.",
76 description
=> "Default image format.",
77 type
=> 'string', format
=> 'pve-storage-format',
83 sub content_hash_to_string
{
87 foreach my $ct (keys %$hash) {
88 push @cta, $ct if $hash->{$ct};
91 return join(',', @cta);
94 sub valid_content_types
{
97 my $def = $defaultData->{plugindata
}->{$type};
101 return $def->{content
}->[0];
107 my $type = $scfg->{type
};
108 my $def = $defaultData->{plugindata
}->{$type};
110 my $def_format = 'raw';
111 my $valid_formats = [ $def_format ];
113 if (defined($def->{format
})) {
114 $def_format = $scfg->{format
} || $def->{format
}->[1];
115 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
118 return wantarray ?
($def_format, $valid_formats) : $def_format;
121 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
123 my ($path, $noerr) = @_;
125 # fixme: exclude more shell meta characters?
126 # we need absolute paths
127 if ($path !~ m
|^/[^;\
(\
)]+|) {
128 return undef if $noerr;
129 die "value does not look like a valid absolute path\n";
134 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
136 my ($server, $noerr) = @_;
138 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
139 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
141 return undef if $noerr;
142 die "value does not look like a valid server name or IP address\n";
147 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
149 my ($name, $noerr) = @_;
151 if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) {
152 return undef if $noerr;
153 die "lvm name '$name' contains illegal characters\n";
159 # fixme: do we need this
160 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
162 # my ($portal, $noerr) = @_;
164 # # IP with optional port
165 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
166 # return undef if $noerr;
167 # die "value does not look like a valid portal address\n";
172 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
173 sub verify_portal_dns
{
174 my ($portal, $noerr) = @_;
176 # IP or DNS name with optional port
177 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
178 return undef if $noerr;
179 die "value does not look like a valid portal address\n";
184 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
186 my ($ct, $noerr) = @_;
188 my $valid_content = valid_content_types
('dir'); # dir includes all types
190 if (!$valid_content->{$ct}) {
191 return undef if $noerr;
192 die "invalid content type '$ct'\n";
198 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
200 my ($fmt, $noerr) = @_;
202 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
203 return undef if $noerr;
204 die "invalid format '$fmt'\n";
210 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
212 my ($value, $noerr) = @_;
214 # mount options (see man fstab)
215 if ($value !~ m/^\S+$/) {
216 return undef if $noerr;
217 die "invalid options '$value'\n";
223 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
224 sub parse_volume_id
{
225 my ($volid, $noerr) = @_;
227 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
228 return wantarray ?
($1, $2) : $1;
230 return undef if $noerr;
231 die "unable to parse volume ID '$volid'\n";
239 sub parse_section_header
{
240 my ($class, $line) = @_;
242 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
243 my ($type, $storeid) = (lc($1), $2);
244 my $errmsg = undef; # set if you want to skip whole section
245 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
247 my $config = {}; # to return additional attributes
248 return ($type, $storeid, $errmsg, $config);
254 my ($class, $type, $key, $value) = @_;
256 my $def = $defaultData->{plugindata
}->{$type};
258 if ($key eq 'content') {
259 my $valid_content = $def->{content
}->[0];
263 foreach my $c (PVE
::Tools
::split_list
($value)) {
264 if (!$valid_content->{$c}) {
265 warn "storage does not support content type '$c'\n";
271 if ($res->{none
} && scalar (keys %$res) > 1) {
272 die "unable to combine 'none' with other content types\n";
276 } elsif ($key eq 'format') {
277 my $valid_formats = $def->{format
}->[0];
279 if (!$valid_formats->{$value}) {
280 warn "storage does not support format '$value'\n";
285 } elsif ($key eq 'nodes') {
288 foreach my $node (PVE
::Tools
::split_list
($value)) {
289 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
295 # no node restrictions for local storage
296 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
297 # die "storage '$storeid' does not allow node restrictions\n";
307 my ($class, $type, $key, $value) = @_;
309 if ($key eq 'nodes') {
310 return join(',', keys(%$value));
311 } elsif ($key eq 'content') {
312 my $res = content_hash_to_string
($value) || 'none';
320 my ($class, $filename, $raw) = @_;
322 my $cfg = $class->SUPER::parse_config
($filename, $raw);
323 my $ids = $cfg->{ids
};
325 # make sure we have a reasonable 'local:' storage
326 # we want 'local' to be always the same 'type' (on all cluster nodes)
327 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
328 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
331 priority
=> 0, # force first entry
332 path
=> '/var/lib/vz',
334 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, snippets
=> 1},
338 # make sure we have a path
339 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
341 # remove node restrictions for local storage
342 delete($ids->{local}->{nodes
});
344 foreach my $storeid (keys %$ids) {
345 my $d = $ids->{$storeid};
346 my $type = $d->{type
};
348 my $def = $defaultData->{plugindata
}->{$type};
350 if ($def->{content
}) {
351 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
353 if (grep { $_ eq $type } @SHARED_STORAGE) {
361 # Storage implementation
363 # called during addition of storage (before the new storage config got written)
364 # die to abort additon if there are (grave) problems
365 # NOTE: runs in a storage config *locked* context
367 my ($class, $storeid, $scfg, %param) = @_;
369 # do nothing by default
372 # called during storage configuration update (before the updated storage config got written)
373 # die to abort the update if there are (grave) problems
374 # NOTE: runs in a storage config *locked* context
376 my ($class, $storeid, $scfg, %param) = @_;
378 # do nothing by default
381 # called during deletion of storage (before the new storage config got written)
382 # and if the activate check on addition fails, to cleanup all storage traces
383 # which on_add_hook may have created.
384 # die to abort deletion if there are (very grave) problems
385 # NOTE: runs in a storage config *locked* context
387 my ($class, $storeid, $scfg) = @_;
389 # do nothing by default
392 sub cluster_lock_storage
{
393 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
397 my $lockid = "pve-storage-$storeid";
398 my $lockdir = "/var/lock/pve-manager";
400 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
403 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
412 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
413 return ($1, $3, $2); # (name, format, isBase)
416 die "unable to parse volume filename '$name'\n";
420 my ($class, $volname) = @_;
422 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
423 my ($basedvmid, $basename) = ($1, $2);
424 parse_name_dir
($basename);
425 my ($vmid, $name) = ($3, $4);
426 my (undef, $format, $isBase) = parse_name_dir
($name);
427 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
428 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
429 my ($vmid, $name) = ($1, $2);
430 my (undef, $format, $isBase) = parse_name_dir
($name);
431 return ('images', $name, $vmid, undef, undef, $isBase, $format);
432 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::iso_extension_re)$!) {
434 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
435 return ('vztmpl', $1);
436 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
437 return ('rootdir', $1, $1);
438 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
440 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
441 return ('backup', $fn, $2);
443 return ('backup', $fn);
444 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
445 return ('snippets', $1);
448 die "unable to parse directory volume name '$volname'\n";
451 my $vtype_subdirs = {
453 rootdir
=> 'private',
454 iso
=> 'template/iso',
455 vztmpl
=> 'template/cache',
457 snippets
=> 'snippets',
460 sub get_vtype_subdirs
{
461 return $vtype_subdirs;
465 my ($class, $scfg, $vtype) = @_;
467 my $path = $scfg->{path
};
469 die "storage definintion has no path\n" if !$path;
471 my $subdir = $vtype_subdirs->{$vtype};
473 die "unknown vtype '$vtype'\n" if !defined($subdir);
475 return "$path/$subdir";
478 sub filesystem_path
{
479 my ($class, $scfg, $volname, $snapname) = @_;
481 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
482 $class->parse_volname($volname);
484 # Note: qcow2/qed has internal snapshot, so path is always
485 # the same (with or without snapshot => same file).
486 die "can't snapshot this image format\n"
487 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
489 my $dir = $class->get_subdir($scfg, $vtype);
491 $dir .= "/$vmid" if $vtype eq 'images';
493 my $path = "$dir/$name";
495 return wantarray ?
($path, $vmid, $vtype) : $path;
499 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
501 return $class->filesystem_path($scfg, $volname, $snapname);
505 my ($class, $storeid, $scfg, $volname) = @_;
507 # this only works for file based storage types
508 die "storage definition has no path\n" if !$scfg->{path
};
510 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
511 $class->parse_volname($volname);
513 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
515 die "create_base not possible with base image\n" if $isBase;
517 my $path = $class->filesystem_path($scfg, $volname);
519 my ($size, undef, $used, $parent) = file_size_info
($path);
520 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
522 die "volname '$volname' contains wrong information about parent\n"
523 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
526 $newname =~ s/^vm-/base-/;
528 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
531 my $newpath = $class->filesystem_path($scfg, $newvolname);
533 die "file '$newpath' already exists\n" if -f
$newpath;
535 rename($path, $newpath) ||
536 die "rename '$path' to '$newpath' failed - $!\n";
538 # We try to protect base volume
540 chmod(0444, $newpath); # nobody should write anything
542 # also try to set immutable flag
543 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
549 my $get_vm_disk_number = sub {
550 my ($disk_name, $scfg, $vmid, $suffix) = @_;
552 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
554 my $type = $scfg->{type
};
555 my $def = { %{$defaultData->{plugindata
}->{$type}} };
557 my $valid = $def->{format
}[0];
558 if ($valid->{subvol
}) {
559 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
562 if ($disk_name =~ m/$disk_regex/) {
569 sub get_next_vm_diskname
{
570 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
573 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
574 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
577 foreach my $disk (@$disk_list) {
578 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
579 $disk_ids->{$disknum} = 1 if defined($disknum);
582 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
583 if (!$disk_ids->{$i}) {
584 return "$prefix-$vmid-disk-$i$suffix";
588 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
591 sub find_free_diskname
{
592 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
594 my $disks = $class->list_images($storeid, $scfg, $vmid);
596 my $disk_list = [ map { $_->{volid
} } @$disks ];
598 return get_next_vm_diskname
($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
602 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
604 # this only works for file based storage types
605 die "storage definintion has no path\n" if !$scfg->{path
};
607 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
608 $class->parse_volname($volname);
610 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
612 die "this storage type does not support clone_image on snapshot\n" if $snap;
614 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
616 die "clone_image only works on base images\n" if !$isBase;
618 my $imagedir = $class->get_subdir($scfg, 'images');
619 $imagedir .= "/$vmid";
623 my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
625 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
627 my $newvol = "$basevmid/$basename/$vmid/$name";
629 my $path = $class->filesystem_path($scfg, $newvol);
631 # Note: we use relative paths, so we need to call chdir before qemu-img
633 local $CWD = $imagedir;
635 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
636 '-f', 'qcow2', $path];
648 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
650 my $imagedir = $class->get_subdir($scfg, 'images');
651 $imagedir .= "/$vmid";
655 $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
657 my (undef, $tmpfmt) = parse_name_dir
($name);
659 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
662 my $path = "$imagedir/$name";
664 die "disk image '$path' already exists\n" if -e
$path;
666 if ($fmt eq 'subvol') {
667 # only allow this if size = 0, so that user knows what he is doing
668 die "storage does not support subvol quotas\n" if $size != 0;
670 my $old_umask = umask(0022);
672 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
676 my $cmd = ['/usr/bin/qemu-img', 'create'];
678 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
680 push @$cmd, '-f', $fmt, $path, "${size}K";
682 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
690 return "$vmid/$name";
694 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
696 my $path = $class->filesystem_path($scfg, $volname);
699 # try to remove immutable flag
700 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
704 if (defined($format) && ($format eq 'subvol')) {
705 File
::Path
::remove_tree
($path);
707 if (!(-f
$path || -l
$path)) {
708 warn "disk image '$path' does not exist\n";
712 unlink($path) || die "unlink '$path' failed - $!\n";
715 # try to cleanup directory to not clutter storage with empty $vmid dirs if
716 # all images from a guest got deleted
717 my $dir = dirname
($path);
724 my ($filename, $timeout) = @_;
726 my $st = File
::stat::stat($filename);
728 if (S_ISDIR
($st->mode)) {
729 return wantarray ?
(0, 'subvol', 0, undef, $st->ctime) : 1;
734 run_command
(['/usr/bin/qemu-img', 'info', '--output=json', $filename],
736 outfunc
=> sub { $json .= shift },
737 errfunc
=> sub { warn "$_[0]\n" }
742 my $info = eval { decode_json
($json) };
743 warn "could not parse qemu-img info command output for '$filename'\n" if $@;
745 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
747 return wantarray ?
($size, $format, $used, $parent, $st->ctime) : $size;
750 sub volume_size_info
{
751 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
752 my $path = $class->filesystem_path($scfg, $volname);
753 return file_size_info
($path, $timeout);
758 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
760 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
762 return 1 if $running;
764 my $path = $class->filesystem_path($scfg, $volname);
766 my $format = ($class->parse_volname($volname))[6];
768 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
770 run_command
($cmd, timeout
=> 10);
775 sub volume_snapshot
{
776 my ($class, $scfg, $storeid, $volname, $snap) = @_;
778 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
780 my $path = $class->filesystem_path($scfg, $volname);
782 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
789 sub volume_rollback_is_possible
{
790 my ($class, $scfg, $storeid, $volname, $snap) = @_;
795 sub volume_snapshot_rollback
{
796 my ($class, $scfg, $storeid, $volname, $snap) = @_;
798 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
800 my $path = $class->filesystem_path($scfg, $volname);
802 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
809 sub volume_snapshot_delete
{
810 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
812 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
814 return 1 if $running;
816 my $path = $class->filesystem_path($scfg, $volname);
818 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
820 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
827 sub storage_can_replicate
{
828 my ($class, $scfg, $storeid, $format) = @_;
833 sub volume_has_feature
{
834 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_;
837 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
838 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
839 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
840 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
841 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
842 snap
=> {qcow2
=> 1} },
843 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
844 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
847 # clone_image creates a qcow2 volume
848 return 0 if $feature eq 'clone' &&
849 defined($opts->{valid_target_formats
}) &&
850 !(grep { $_ eq 'qcow2' } @{$opts->{valid_target_formats
}});
852 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
853 $class->parse_volname($volname);
859 $key = $isBase ?
'base' : 'current';
862 return 1 if defined($features->{$feature}->{$key}->{$format});
868 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
870 my $imagedir = $class->get_subdir($scfg, 'images');
872 my ($defFmt, $vaidFmts) = default_format
($scfg);
873 my $fmts = join ('|', @$vaidFmts);
877 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
879 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
885 next if !$vollist && defined($vmid) && ($owner ne $vmid);
887 my ($size, $format, $used, $parent, $ctime) = file_size_info
($fn);
888 next if !($format && defined($size));
891 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
892 my ($basevmid, $basename) = ($1, $2);
893 $volid = "$storeid:$basevmid/$basename/$owner/$name";
895 $volid = "$storeid:$owner/$name";
899 my $found = grep { $_ eq $volid } @$vollist;
904 volid
=> $volid, format
=> $format,
905 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
908 $info->{ctime
} = $ctime if $ctime;
916 # list templates ($tt = <iso|vztmpl|backup|snippets>)
917 my $get_subdir_files = sub {
918 my ($sid, $path, $tt, $vmid) = @_;
922 foreach my $fn (<$path/*>) {
924 my $st = File
::stat::stat($fn);
926 next if S_ISDIR
($st->mode);
931 next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
933 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
935 } elsif ($tt eq 'vztmpl') {
936 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
938 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
940 } elsif ($tt eq 'backup') {
941 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
942 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
945 $info = { volid
=> "$sid:backup/$1", format
=> $format };
947 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}$!) {
948 my $epoch = timelocal
($6, $5, $4, $3, $2-1, $1 - 1900);
949 $info->{ctime
} = $epoch;
952 if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
953 $info->{vmid
} = $vmid // $1;
957 } elsif ($tt eq 'snippets') {
960 volid
=> "$sid:snippets/". basename
($fn),
965 $info->{size
} = $st->size;
966 $info->{ctime
} //= $st->ctime;
975 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
978 my $vmlist = PVE
::Cluster
::get_vmlist
();
979 foreach my $type (@$content_types) {
982 if ($type eq 'images' || $type eq 'rootdir') {
983 $data = $class->list_images($storeid, $scfg, $vmid);
984 } elsif ($scfg->{path
}) {
985 my $path = $class->get_subdir($scfg, $type);
987 if ($type eq 'iso' && !defined($vmid)) {
988 $data = $get_subdir_files->($storeid, $path, 'iso');
989 } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
990 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
991 } elsif ($type eq 'backup') {
992 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
993 } elsif ($type eq 'snippets') {
994 $data = $get_subdir_files->($storeid, $path, 'snippets');
1000 foreach my $item (@$data) {
1001 if ($type eq 'images' || $type eq 'rootdir') {
1002 my $vminfo = $vmlist->{ids
}->{$item->{vmid
}};
1004 if (defined($vminfo)) {
1005 $vmtype = $vminfo->{type
};
1007 if (defined($vmtype) && $vmtype eq 'lxc') {
1008 $item->{content
} = 'rootdir';
1010 $item->{content
} = 'images';
1012 next if $type ne $item->{content
};
1014 $item->{content
} = $type;
1025 my ($class, $storeid, $scfg, $cache) = @_;
1027 my $path = $scfg->{path
};
1029 die "storage definintion has no path\n" if !$path;
1032 my $res = PVE
::Tools
::df
($path, $timeout);
1034 return undef if !$res || !$res->{total
};
1036 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
1039 sub volume_snapshot_list
{
1040 my ($class, $scfg, $storeid, $volname) = @_;
1042 # implement in subclass
1043 die "Volume_snapshot_list is not implemented for $class";
1045 # return an empty array if dataset does not exist.
1048 sub activate_storage
{
1049 my ($class, $storeid, $scfg, $cache) = @_;
1051 my $path = $scfg->{path
};
1053 die "storage definintion has no path\n" if !$path;
1055 # this path test may hang indefinitely on unresponsive mounts
1057 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
1058 die "unable to activate storage '$storeid' - " .
1059 "directory '$path' does not exist or is unreachable\n";
1063 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1065 if (defined($scfg->{content
})) {
1066 foreach my $vtype (keys %$vtype_subdirs) {
1067 # OpenVZMigrate uses backup (dump) dir
1068 if (defined($scfg->{content
}->{$vtype}) ||
1069 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
1070 my $subdir = $class->get_subdir($scfg, $vtype);
1071 mkpath
$subdir if $subdir ne $path;
1077 sub deactivate_storage
{
1078 my ($class, $storeid, $scfg, $cache) = @_;
1080 # do nothing by default
1084 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1086 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1091 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1096 sub activate_volume
{
1097 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1099 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1101 # check is volume exists
1102 if ($scfg->{path
}) {
1103 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
1105 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
1109 sub deactivate_volume
{
1110 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1112 # do nothing by default
1115 sub check_connection
{
1116 my ($class, $storeid, $scfg) = @_;
1117 # do nothing by default
1121 # Import/Export interface:
1122 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1123 # the default implementations will return this if $scfg->{path} is set,
1124 # mimicking the old PVE::Storage::storage_migrate() function.
1126 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1127 # functions in case the format doesn't match their specialized
1128 # implementations to reuse the raw/tar code.
1130 # Format specification:
1131 # The following formats are all prefixed with image information in the form
1132 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1133 # to preallocate the image on storages which require it.
1135 # raw+size: (image files only)
1136 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1137 # qcow2+size, vmdk: (image files only)
1138 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1139 # files which are already in qcow2 format, or via `qemu-img convert`.
1140 # Note that these formats are only valid with $with_snapshots being true.
1141 # tar+size: (subvolumes only)
1142 # A GNU tar stream containing just the inner contents of the subvolume.
1143 # This does not distinguish between the contents of a privileged or
1144 # unprivileged container. In other words, this is from the root user
1145 # namespace's point of view with no uid-mapping in effect.
1146 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1148 # Plugins may reuse these helpers. Changes to the header format should be
1149 # reflected by changes to the function prototypes.
1150 sub write_common_header
($$) {
1151 my ($fh, $image_size_in_bytes) = @_;
1152 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1155 sub read_common_header
($) {
1157 sysread($fh, my $size, 8);
1158 $size = unpack('Q<', $size);
1159 die "import: no size found in export header, aborting.\n" if !defined($size);
1160 die "import: got a bad size (not a multiple of 1K), aborting.\n" if ($size&1023);
1165 # Export a volume into a file handle as a stream of desired format.
1167 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1168 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1169 my $file = $class->path($scfg, $volname, $storeid)
1170 or goto unsupported
;
1171 my ($size, $file_format) = file_size_info
($file);
1173 if ($format eq 'raw+size') {
1174 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1175 write_common_header
($fh, $size);
1176 if ($file_format eq 'raw') {
1177 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1179 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1180 output
=> '>&'.fileno($fh));
1183 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1184 my $data_format = $1;
1185 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1186 write_common_header
($fh, $size);
1187 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1189 } elsif ($format eq 'tar+size') {
1190 goto unsupported
if $file_format ne 'subvol';
1191 write_common_header
($fh, $size);
1192 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1193 output
=> '>&'.fileno($fh));
1198 die "volume export format $format not available for $class";
1201 sub volume_export_formats
{
1202 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1203 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1204 my $file = $class->path($scfg, $volname, $storeid)
1206 my ($size, $format) = file_size_info
($file);
1208 if ($with_snapshots) {
1209 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1212 return ('tar+size') if $format eq 'subvol';
1213 return ('raw+size');
1218 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1220 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1222 die "volume import format '$format' not available for $class\n"
1223 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1224 my $data_format = $1;
1226 die "format $format cannot be imported without snapshots\n"
1227 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1228 die "format $format cannot be imported with snapshots\n"
1229 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1231 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1232 $class->parse_volname($volname);
1234 # XXX: Should we bother with conversion routines at this level? This won't
1235 # happen without manual CLI usage, so for now we just error out...
1236 die "cannot import format $format into a file of format $file_format\n"
1237 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1239 # Check for an existing file first since interrupting alloc_image doesn't
1241 my $file = $class->path($scfg, $volname, $storeid);
1243 die "file '$file' already exists\n" if !$allow_rename;
1244 warn "file '$file' already exists - importing with a different name\n";
1248 my ($size) = read_common_header
($fh);
1249 $size = int($size/1024);
1252 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1253 my $oldname = $volname;
1254 $volname = $allocname;
1255 if (defined($name) && $allocname ne $oldname) {
1256 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1258 my $file = $class->path($scfg, $volname, $storeid)
1259 or die "internal error: failed to get path to newly allocated volume $volname\n";
1260 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1261 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1262 input
=> '<&'.fileno($fh));
1263 } elsif ($data_format eq 'tar') {
1264 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1265 input
=> '<&'.fileno($fh));
1267 die "volume import format '$format' not available for $class";
1271 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1276 return "$storeid:$volname";
1279 sub volume_import_formats
{
1280 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1281 if ($scfg->{path
} && !defined($base_snapshot)) {
1282 my $format = ($class->parse_volname($volname))[6];
1283 if ($with_snapshots) {
1284 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1287 return ('tar+size') if $format eq 'subvol';
1288 return ('raw+size');