1 package PVE
::Storage
::Plugin
;
10 use Time
::Local
qw(timelocal);
12 use PVE
::Tools
qw(run_command);
13 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::Cluster
qw(cfs_register_file);
18 use base
qw(PVE::SectionConfig);
20 our @COMMON_TAR_FLAGS = qw(
22 -p --sparse --numeric-owner --acls
23 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
24 --warning=no-file-ignored --warning=no-xattr-write
27 our @SHARED_STORAGE = (
38 our $MAX_VOLUMES_PER_GUEST = 1024;
40 cfs_register_file
('storage.cfg',
41 sub { __PACKAGE__-
>parse_config(@_); },
42 sub { __PACKAGE__-
>write_config(@_); });
47 type
=> { description
=> "Storage type." },
48 storage
=> get_standard_option
('pve-storage-id',
49 { completion
=> \
&PVE
::Storage
::complete_storage
}),
50 nodes
=> get_standard_option
('pve-node-list', { optional
=> 1 }),
52 description
=> "Allowed content types.\n\nNOTE: the value " .
53 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
54 type
=> 'string', format
=> 'pve-storage-content-list',
56 completion
=> \
&PVE
::Storage
::complete_content_type
,
59 description
=> "Flag to disable the storage.",
64 description
=> "Maximal number of backup files per VM. Use '0' for unlimted.",
70 description
=> "Mark storage as shared.",
75 description
=> "Default image format.",
76 type
=> 'string', format
=> 'pve-storage-format',
82 sub content_hash_to_string
{
86 foreach my $ct (keys %$hash) {
87 push @cta, $ct if $hash->{$ct};
90 return join(',', @cta);
93 sub valid_content_types
{
96 my $def = $defaultData->{plugindata
}->{$type};
100 return $def->{content
}->[0];
106 my $type = $scfg->{type
};
107 my $def = $defaultData->{plugindata
}->{$type};
109 my $def_format = 'raw';
110 my $valid_formats = [ $def_format ];
112 if (defined($def->{format
})) {
113 $def_format = $scfg->{format
} || $def->{format
}->[1];
114 $valid_formats = [ sort keys %{$def->{format
}->[0]} ];
117 return wantarray ?
($def_format, $valid_formats) : $def_format;
120 PVE
::JSONSchema
::register_format
('pve-storage-path', \
&verify_path
);
122 my ($path, $noerr) = @_;
124 # fixme: exclude more shell meta characters?
125 # we need absolute paths
126 if ($path !~ m
|^/[^;\
(\
)]+|) {
127 return undef if $noerr;
128 die "value does not look like a valid absolute path\n";
133 PVE
::JSONSchema
::register_format
('pve-storage-server', \
&verify_server
);
135 my ($server, $noerr) = @_;
137 if (!(PVE
::JSONSchema
::pve_verify_ip
($server, 1) ||
138 PVE
::JSONSchema
::pve_verify_dns_name
($server, 1)))
140 return undef if $noerr;
141 die "value does not look like a valid server name or IP address\n";
146 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
148 my ($name, $noerr) = @_;
150 if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) {
151 return undef if $noerr;
152 die "lvm name '$name' contains illegal characters\n";
158 # fixme: do we need this
159 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
161 # my ($portal, $noerr) = @_;
163 # # IP with optional port
164 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
165 # return undef if $noerr;
166 # die "value does not look like a valid portal address\n";
171 PVE
::JSONSchema
::register_format
('pve-storage-portal-dns', \
&verify_portal_dns
);
172 sub verify_portal_dns
{
173 my ($portal, $noerr) = @_;
175 # IP or DNS name with optional port
176 if (!PVE
::Tools
::parse_host_and_port
($portal)) {
177 return undef if $noerr;
178 die "value does not look like a valid portal address\n";
183 PVE
::JSONSchema
::register_format
('pve-storage-content', \
&verify_content
);
185 my ($ct, $noerr) = @_;
187 my $valid_content = valid_content_types
('dir'); # dir includes all types
189 if (!$valid_content->{$ct}) {
190 return undef if $noerr;
191 die "invalid content type '$ct'\n";
197 PVE
::JSONSchema
::register_format
('pve-storage-format', \
&verify_format
);
199 my ($fmt, $noerr) = @_;
201 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
202 return undef if $noerr;
203 die "invalid format '$fmt'\n";
209 PVE
::JSONSchema
::register_format
('pve-storage-options', \
&verify_options
);
211 my ($value, $noerr) = @_;
213 # mount options (see man fstab)
214 if ($value !~ m/^\S+$/) {
215 return undef if $noerr;
216 die "invalid options '$value'\n";
222 PVE
::JSONSchema
::register_format
('pve-volume-id', \
&parse_volume_id
);
223 sub parse_volume_id
{
224 my ($volid, $noerr) = @_;
226 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
227 return wantarray ?
($1, $2) : $1;
229 return undef if $noerr;
230 die "unable to parse volume ID '$volid'\n";
238 sub parse_section_header
{
239 my ($class, $line) = @_;
241 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
242 my ($type, $storeid) = (lc($1), $2);
243 my $errmsg = undef; # set if you want to skip whole section
244 eval { PVE
::JSONSchema
::parse_storage_id
($storeid); };
246 my $config = {}; # to return additional attributes
247 return ($type, $storeid, $errmsg, $config);
253 my ($class, $type, $key, $value) = @_;
255 my $def = $defaultData->{plugindata
}->{$type};
257 if ($key eq 'content') {
258 my $valid_content = $def->{content
}->[0];
262 foreach my $c (PVE
::Tools
::split_list
($value)) {
263 if (!$valid_content->{$c}) {
264 warn "storage does not support content type '$c'\n";
270 if ($res->{none
} && scalar (keys %$res) > 1) {
271 die "unable to combine 'none' with other content types\n";
275 } elsif ($key eq 'format') {
276 my $valid_formats = $def->{format
}->[0];
278 if (!$valid_formats->{$value}) {
279 warn "storage does not support format '$value'\n";
284 } elsif ($key eq 'nodes') {
287 foreach my $node (PVE
::Tools
::split_list
($value)) {
288 if (PVE
::JSONSchema
::pve_verify_node_name
($node)) {
294 # no node restrictions for local storage
295 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
296 # die "storage '$storeid' does not allow node restrictions\n";
306 my ($class, $type, $key, $value) = @_;
308 if ($key eq 'nodes') {
309 return join(',', keys(%$value));
310 } elsif ($key eq 'content') {
311 my $res = content_hash_to_string
($value) || 'none';
319 my ($class, $filename, $raw) = @_;
321 my $cfg = $class->SUPER::parse_config
($filename, $raw);
322 my $ids = $cfg->{ids
};
324 # make sure we have a reasonable 'local:' storage
325 # we want 'local' to be always the same 'type' (on all cluster nodes)
326 if (!$ids->{local} || $ids->{local}->{type
} ne 'dir' ||
327 ($ids->{local}->{path
} && $ids->{local}->{path
} ne '/var/lib/vz')) {
330 priority
=> 0, # force first entry
331 path
=> '/var/lib/vz',
333 content
=> { images
=> 1, rootdir
=> 1, vztmpl
=> 1, iso
=> 1, snippets
=> 1},
337 # make sure we have a path
338 $ids->{local}->{path
} = '/var/lib/vz' if !$ids->{local}->{path
};
340 # remove node restrictions for local storage
341 delete($ids->{local}->{nodes
});
343 foreach my $storeid (keys %$ids) {
344 my $d = $ids->{$storeid};
345 my $type = $d->{type
};
347 my $def = $defaultData->{plugindata
}->{$type};
349 if ($def->{content
}) {
350 $d->{content
} = $def->{content
}->[1] if !$d->{content
};
352 if (grep { $_ eq $type } @SHARED_STORAGE) {
360 # Storage implementation
362 # called during addition of storage (before the new storage config got written)
363 # die to abort additon if there are (grave) problems
364 # NOTE: runs in a storage config *locked* context
366 my ($class, $storeid, $scfg, %param) = @_;
368 # do nothing by default
371 # called during storage configuration update (before the updated storage config got written)
372 # die to abort the update if there are (grave) problems
373 # NOTE: runs in a storage config *locked* context
375 my ($class, $storeid, $scfg, %param) = @_;
377 # do nothing by default
380 # called during deletion of storage (before the new storage config got written)
381 # and if the activate check on addition fails, to cleanup all storage traces
382 # which on_add_hook may have created.
383 # die to abort deletion if there are (very grave) problems
384 # NOTE: runs in a storage config *locked* context
386 my ($class, $storeid, $scfg) = @_;
388 # do nothing by default
391 sub cluster_lock_storage
{
392 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
396 my $lockid = "pve-storage-$storeid";
397 my $lockdir = "/var/lock/pve-manager";
399 $res = PVE
::Tools
::lock_file
("$lockdir/$lockid", $timeout, $func, @param);
402 $res = PVE
::Cluster
::cfs_lock_storage
($storeid, $timeout, $func, @param);
411 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
412 return ($1, $3, $2); # (name, format, isBase)
415 die "unable to parse volume filename '$name'\n";
419 my ($class, $volname) = @_;
421 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
422 my ($basedvmid, $basename) = ($1, $2);
423 parse_name_dir
($basename);
424 my ($vmid, $name) = ($3, $4);
425 my (undef, $format, $isBase) = parse_name_dir
($name);
426 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
427 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
428 my ($vmid, $name) = ($1, $2);
429 my (undef, $format, $isBase) = parse_name_dir
($name);
430 return ('images', $name, $vmid, undef, undef, $isBase, $format);
431 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::iso_extension_re)$!) {
433 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
434 return ('vztmpl', $1);
435 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
436 return ('rootdir', $1, $1);
437 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
439 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
440 return ('backup', $fn, $2);
442 return ('backup', $fn);
443 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
444 return ('snippets', $1);
447 die "unable to parse directory volume name '$volname'\n";
450 my $vtype_subdirs = {
452 rootdir
=> 'private',
453 iso
=> 'template/iso',
454 vztmpl
=> 'template/cache',
456 snippets
=> 'snippets',
460 my ($class, $scfg, $vtype) = @_;
462 my $path = $scfg->{path
};
464 die "storage definintion has no path\n" if !$path;
466 my $subdir = $vtype_subdirs->{$vtype};
468 die "unknown vtype '$vtype'\n" if !defined($subdir);
470 return "$path/$subdir";
473 sub filesystem_path
{
474 my ($class, $scfg, $volname, $snapname) = @_;
476 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
477 $class->parse_volname($volname);
479 # Note: qcow2/qed has internal snapshot, so path is always
480 # the same (with or without snapshot => same file).
481 die "can't snapshot this image format\n"
482 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
484 my $dir = $class->get_subdir($scfg, $vtype);
486 $dir .= "/$vmid" if $vtype eq 'images';
488 my $path = "$dir/$name";
490 return wantarray ?
($path, $vmid, $vtype) : $path;
494 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
496 return $class->filesystem_path($scfg, $volname, $snapname);
500 my ($class, $storeid, $scfg, $volname) = @_;
502 # this only works for file based storage types
503 die "storage definition has no path\n" if !$scfg->{path
};
505 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
506 $class->parse_volname($volname);
508 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
510 die "create_base not possible with base image\n" if $isBase;
512 my $path = $class->filesystem_path($scfg, $volname);
514 my ($size, undef, $used, $parent) = file_size_info
($path);
515 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
517 die "volname '$volname' contains wrong information about parent\n"
518 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
521 $newname =~ s/^vm-/base-/;
523 my $newvolname = $basename ?
"$basevmid/$basename/$vmid/$newname" :
526 my $newpath = $class->filesystem_path($scfg, $newvolname);
528 die "file '$newpath' already exists\n" if -f
$newpath;
530 rename($path, $newpath) ||
531 die "rename '$path' to '$newpath' failed - $!\n";
533 # We try to protect base volume
535 chmod(0444, $newpath); # nobody should write anything
537 # also try to set immutable flag
538 eval { run_command
(['/usr/bin/chattr', '+i', $newpath]); };
544 my $get_vm_disk_number = sub {
545 my ($disk_name, $scfg, $vmid, $suffix) = @_;
547 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
549 my $type = $scfg->{type
};
550 my $def = { %{$defaultData->{plugindata
}->{$type}} };
552 my $valid = $def->{format
}[0];
553 if ($valid->{subvol
}) {
554 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
557 if ($disk_name =~ m/$disk_regex/) {
564 sub get_next_vm_diskname
{
565 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
568 my $prefix = ($fmt eq 'subvol') ?
'subvol' : 'vm';
569 my $suffix = $add_fmt_suffix ?
".$fmt" : '';
572 foreach my $disk (@$disk_list) {
573 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
574 $disk_ids->{$disknum} = 1 if defined($disknum);
577 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
578 if (!$disk_ids->{$i}) {
579 return "$prefix-$vmid-disk-$i$suffix";
583 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
586 sub find_free_diskname
{
587 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
589 my $disks = $class->list_images($storeid, $scfg, $vmid);
591 my $disk_list = [ map { $_->{volid
} } @$disks ];
593 return get_next_vm_diskname
($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
597 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
599 # this only works for file based storage types
600 die "storage definintion has no path\n" if !$scfg->{path
};
602 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
603 $class->parse_volname($volname);
605 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
607 die "this storage type does not support clone_image on snapshot\n" if $snap;
609 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
611 die "clone_image only works on base images\n" if !$isBase;
613 my $imagedir = $class->get_subdir($scfg, 'images');
614 $imagedir .= "/$vmid";
618 my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
620 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
622 my $newvol = "$basevmid/$basename/$vmid/$name";
624 my $path = $class->filesystem_path($scfg, $newvol);
626 # Note: we use relative paths, so we need to call chdir before qemu-img
628 local $CWD = $imagedir;
630 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
631 '-f', 'qcow2', $path];
643 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
645 my $imagedir = $class->get_subdir($scfg, 'images');
646 $imagedir .= "/$vmid";
650 $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
652 my (undef, $tmpfmt) = parse_name_dir
($name);
654 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
657 my $path = "$imagedir/$name";
659 die "disk image '$path' already exists\n" if -e
$path;
661 if ($fmt eq 'subvol') {
662 # only allow this if size = 0, so that user knows what he is doing
663 die "storage does not support subvol quotas\n" if $size != 0;
665 my $old_umask = umask(0022);
667 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
671 my $cmd = ['/usr/bin/qemu-img', 'create'];
673 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
675 push @$cmd, '-f', $fmt, $path, "${size}K";
677 eval { run_command
($cmd, errmsg
=> "unable to create image"); };
685 return "$vmid/$name";
689 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
691 my $path = $class->filesystem_path($scfg, $volname);
694 # try to remove immutable flag
695 eval { run_command
(['/usr/bin/chattr', '-i', $path]); };
699 if (defined($format) && ($format eq 'subvol')) {
700 File
::Path
::remove_tree
($path);
702 if (!(-f
$path || -l
$path)) {
703 warn "disk image '$path' does not exist\n";
707 unlink($path) || die "unlink '$path' failed - $!\n";
710 # try to cleanup directory to not clutter storage with empty $vmid dirs if
711 # all images from a guest got deleted
712 my $dir = dirname
($path);
719 my ($filename, $timeout) = @_;
721 my @fs = stat($filename);
725 if (S_ISDIR
($mode)) {
726 return wantarray ?
(0, 'subvol', 0, undef, $ctime) : 1;
731 run_command
(['/usr/bin/qemu-img', 'info', '--output=json', $filename],
733 outfunc
=> sub { $json .= shift },
734 errfunc
=> sub { warn "$_[0]\n" }
739 my $info = eval { decode_json
($json) };
740 warn "could not parse qemu-img info command output for '$filename'\n" if $@;
742 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
744 return wantarray ?
($size, $format, $used, $parent, $ctime) : $size;
747 sub volume_size_info
{
748 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
749 my $path = $class->filesystem_path($scfg, $volname);
750 return file_size_info
($path, $timeout);
755 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
757 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
759 return 1 if $running;
761 my $path = $class->filesystem_path($scfg, $volname);
763 my $format = ($class->parse_volname($volname))[6];
765 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
767 run_command
($cmd, timeout
=> 10);
772 sub volume_snapshot
{
773 my ($class, $scfg, $storeid, $volname, $snap) = @_;
775 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
777 my $path = $class->filesystem_path($scfg, $volname);
779 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
786 sub volume_rollback_is_possible
{
787 my ($class, $scfg, $storeid, $volname, $snap) = @_;
792 sub volume_snapshot_rollback
{
793 my ($class, $scfg, $storeid, $volname, $snap) = @_;
795 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
797 my $path = $class->filesystem_path($scfg, $volname);
799 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
806 sub volume_snapshot_delete
{
807 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
809 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
811 return 1 if $running;
813 my $path = $class->filesystem_path($scfg, $volname);
815 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
817 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
824 sub storage_can_replicate
{
825 my ($class, $scfg, $storeid, $format) = @_;
830 sub volume_has_feature
{
831 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
834 snapshot
=> { current
=> { qcow2
=> 1}, snap
=> { qcow2
=> 1} },
835 clone
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
836 template
=> { current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1, subvol
=> 1} },
837 copy
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
838 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
839 snap
=> {qcow2
=> 1} },
840 sparseinit
=> { base
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1},
841 current
=> {qcow2
=> 1, raw
=> 1, vmdk
=> 1} },
844 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
845 $class->parse_volname($volname);
851 $key = $isBase ?
'base' : 'current';
854 return 1 if defined($features->{$feature}->{$key}->{$format});
860 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
862 my $imagedir = $class->get_subdir($scfg, 'images');
864 my ($defFmt, $vaidFmts) = default_format
($scfg);
865 my $fmts = join ('|', @$vaidFmts);
869 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
871 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
877 next if !$vollist && defined($vmid) && ($owner ne $vmid);
879 my ($size, $format, $used, $parent, $ctime) = file_size_info
($fn);
880 next if !($format && defined($size));
883 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
884 my ($basevmid, $basename) = ($1, $2);
885 $volid = "$storeid:$basevmid/$basename/$owner/$name";
887 $volid = "$storeid:$owner/$name";
891 my $found = grep { $_ eq $volid } @$vollist;
896 volid
=> $volid, format
=> $format,
897 size
=> $size, vmid
=> $owner, used
=> $used, parent
=> $parent
900 $info->{ctime
} = $ctime if $ctime;
908 # list templates ($tt = <iso|vztmpl|backup|snippets>)
909 my $get_subdir_files = sub {
910 my ($sid, $path, $tt, $vmid) = @_;
914 foreach my $fn (<$path/*>) {
916 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
917 $atime,$mtime,$ctime,$blksize,$blocks)
920 next if S_ISDIR
($mode);
925 next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
927 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
929 } elsif ($tt eq 'vztmpl') {
930 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
932 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
934 } elsif ($tt eq 'backup') {
935 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
936 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
939 $info = { volid
=> "$sid:backup/$1", format
=> $format };
941 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}$!) {
942 my $epoch = timelocal
($6, $5, $4, $3, $2-1, $1 - 1900);
943 $info->{ctime
} = $epoch;
946 if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
947 $info->{vmid
} = $vmid // $1;
951 } elsif ($tt eq 'snippets') {
954 volid
=> "$sid:snippets/". basename
($fn),
959 $info->{size
} = $size;
960 $info->{ctime
} //= $ctime;
969 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
972 my $vmlist = PVE
::Cluster
::get_vmlist
();
973 foreach my $type (@$content_types) {
976 if ($type eq 'images' || $type eq 'rootdir') {
977 $data = $class->list_images($storeid, $scfg, $vmid);
978 } elsif ($scfg->{path
}) {
979 my $path = $class->get_subdir($scfg, $type);
981 if ($type eq 'iso' && !defined($vmid)) {
982 $data = $get_subdir_files->($storeid, $path, 'iso');
983 } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
984 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
985 } elsif ($type eq 'backup') {
986 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
987 } elsif ($type eq 'snippets') {
988 $data = $get_subdir_files->($storeid, $path, 'snippets');
994 foreach my $item (@$data) {
995 if ($type eq 'images' || $type eq 'rootdir') {
996 my $vminfo = $vmlist->{ids
}->{$item->{vmid
}};
998 if (defined($vminfo)) {
999 $vmtype = $vminfo->{type
};
1001 if (defined($vmtype) && $vmtype eq 'lxc') {
1002 $item->{content
} = 'rootdir';
1004 $item->{content
} = 'images';
1006 next if $type ne $item->{content
};
1008 $item->{content
} = $type;
1019 my ($class, $storeid, $scfg, $cache) = @_;
1021 my $path = $scfg->{path
};
1023 die "storage definintion has no path\n" if !$path;
1026 my $res = PVE
::Tools
::df
($path, $timeout);
1028 return undef if !$res || !$res->{total
};
1030 return ($res->{total
}, $res->{avail
}, $res->{used
}, 1);
1033 sub volume_snapshot_list
{
1034 my ($class, $scfg, $storeid, $volname) = @_;
1036 # implement in subclass
1037 die "Volume_snapshot_list is not implemented for $class";
1039 # return an empty array if dataset does not exist.
1042 sub activate_storage
{
1043 my ($class, $storeid, $scfg, $cache) = @_;
1045 my $path = $scfg->{path
};
1047 die "storage definintion has no path\n" if !$path;
1049 # this path test may hang indefinitely on unresponsive mounts
1051 if (! PVE
::Tools
::run_fork_with_timeout
($timeout, sub {-d
$path})) {
1052 die "unable to activate storage '$storeid' - " .
1053 "directory '$path' does not exist or is unreachable\n";
1057 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1059 if (defined($scfg->{content
})) {
1060 foreach my $vtype (keys %$vtype_subdirs) {
1061 # OpenVZMigrate uses backup (dump) dir
1062 if (defined($scfg->{content
}->{$vtype}) ||
1063 ($vtype eq 'backup' && defined($scfg->{content
}->{'rootdir'}))) {
1064 my $subdir = $class->get_subdir($scfg, $vtype);
1065 mkpath
$subdir if $subdir ne $path;
1071 sub deactivate_storage
{
1072 my ($class, $storeid, $scfg, $cache) = @_;
1074 # do nothing by default
1078 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1080 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1085 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1090 sub activate_volume
{
1091 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1093 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1095 # check is volume exists
1096 if ($scfg->{path
}) {
1097 die "volume '$storeid:$volname' does not exist\n" if ! -e
$path;
1099 die "volume '$storeid:$volname' does not exist\n" if ! -b
$path;
1103 sub deactivate_volume
{
1104 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1106 # do nothing by default
1109 sub check_connection
{
1110 my ($class, $storeid, $scfg) = @_;
1111 # do nothing by default
1115 # Import/Export interface:
1116 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1117 # the default implementations will return this if $scfg->{path} is set,
1118 # mimicking the old PVE::Storage::storage_migrate() function.
1120 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1121 # functions in case the format doesn't match their specialized
1122 # implementations to reuse the raw/tar code.
1124 # Format specification:
1125 # The following formats are all prefixed with image information in the form
1126 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1127 # to preallocate the image on storages which require it.
1129 # raw+size: (image files only)
1130 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1131 # qcow2+size, vmdk: (image files only)
1132 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1133 # files which are already in qcow2 format, or via `qemu-img convert`.
1134 # Note that these formats are only valid with $with_snapshots being true.
1135 # tar+size: (subvolumes only)
1136 # A GNU tar stream containing just the inner contents of the subvolume.
1137 # This does not distinguish between the contents of a privileged or
1138 # unprivileged container. In other words, this is from the root user
1139 # namespace's point of view with no uid-mapping in effect.
1140 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1142 # Plugins may reuse these helpers. Changes to the header format should be
1143 # reflected by changes to the function prototypes.
1144 sub write_common_header
($$) {
1145 my ($fh, $image_size_in_bytes) = @_;
1146 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1149 sub read_common_header
($) {
1151 sysread($fh, my $size, 8);
1152 $size = unpack('Q<', $size);
1153 die "import: no size found in export header, aborting.\n" if !defined($size);
1154 die "import: got a bad size (not a multiple of 1K), aborting.\n" if ($size&1023);
1159 # Export a volume into a file handle as a stream of desired format.
1161 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1162 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1163 my $file = $class->path($scfg, $volname, $storeid)
1164 or goto unsupported
;
1165 my ($size, $file_format) = file_size_info
($file);
1167 if ($format eq 'raw+size') {
1168 goto unsupported
if $with_snapshots || $file_format eq 'subvol';
1169 write_common_header
($fh, $size);
1170 if ($file_format eq 'raw') {
1171 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1173 run_command
(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1174 output
=> '>&'.fileno($fh));
1177 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1178 my $data_format = $1;
1179 goto unsupported
if !$with_snapshots || $file_format ne $data_format;
1180 write_common_header
($fh, $size);
1181 run_command
(['dd', "if=$file", "bs=4k"], output
=> '>&'.fileno($fh));
1183 } elsif ($format eq 'tar+size') {
1184 goto unsupported
if $file_format ne 'subvol';
1185 write_common_header
($fh, $size);
1186 run_command
(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1187 output
=> '>&'.fileno($fh));
1192 die "volume export format $format not available for $class";
1195 sub volume_export_formats
{
1196 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1197 if ($scfg->{path
} && !defined($snapshot) && !defined($base_snapshot)) {
1198 my $file = $class->path($scfg, $volname, $storeid)
1200 my ($size, $format) = file_size_info
($file);
1202 if ($with_snapshots) {
1203 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1206 return ('tar+size') if $format eq 'subvol';
1207 return ('raw+size');
1212 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1214 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1216 die "volume import format '$format' not available for $class\n"
1217 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1218 my $data_format = $1;
1220 die "format $format cannot be imported without snapshots\n"
1221 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1222 die "format $format cannot be imported with snapshots\n"
1223 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1225 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1226 $class->parse_volname($volname);
1228 # XXX: Should we bother with conversion routines at this level? This won't
1229 # happen without manual CLI usage, so for now we just error out...
1230 die "cannot import format $format into a file of format $file_format\n"
1231 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1233 # Check for an existing file first since interrupting alloc_image doesn't
1235 my $file = $class->path($scfg, $volname, $storeid);
1236 die "file '$file' already exists\n" if -e
$file;
1238 my ($size) = read_common_header
($fh);
1239 $size = int($size/1024);
1242 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1243 if ($allocname ne $volname) {
1244 my $oldname = $volname;
1245 $volname = $allocname; # Let the cleanup code know what to free
1246 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1248 my $file = $class->path($scfg, $volname, $storeid)
1249 or die "internal error: failed to get path to newly allocated volume $volname\n";
1250 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1251 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1252 input
=> '<&'.fileno($fh));
1253 } elsif ($data_format eq 'tar') {
1254 run_command
(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1255 input
=> '<&'.fileno($fh));
1257 die "volume import format '$format' not available for $class";
1261 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1267 sub volume_import_formats
{
1268 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1269 if ($scfg->{path
} && !defined($base_snapshot)) {
1270 my $format = ($class->parse_volname($volname))[6];
1271 if ($with_snapshots) {
1272 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1275 return ('tar+size') if $format eq 'subvol';
1276 return ('raw+size');