]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
storage: get subdirectory files: read .comment files for comments
[pve-storage.git] / PVE / Storage / Plugin.pm
1 package PVE::Storage::Plugin;
2
3 use strict;
4 use warnings;
5
6 use Fcntl ':mode';
7 use File::chdir;
8 use File::Path;
9 use File::Basename;
10 use File::stat qw();
11
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);
15
16 use JSON;
17
18 use base qw(PVE::SectionConfig);
19
20 use constant COMPRESSOR_RE => 'gz|lzo|zst';
21
22 use constant COMMENT_EXT => ".comment";
23
24 our @COMMON_TAR_FLAGS = qw(
25 --one-file-system
26 -p --sparse --numeric-owner --acls
27 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
28 --warning=no-file-ignored --warning=no-xattr-write
29 );
30
31 our @SHARED_STORAGE = (
32 'iscsi',
33 'nfs',
34 'cifs',
35 'rbd',
36 'cephfs',
37 'iscsidirect',
38 'glusterfs',
39 'zfs',
40 'drbd');
41
42 our $MAX_VOLUMES_PER_GUEST = 1024;
43
44 cfs_register_file ('storage.cfg',
45 sub { __PACKAGE__->parse_config(@_); },
46 sub { __PACKAGE__->write_config(@_); });
47
48 my %prune_option = (
49 optional => 1,
50 type => 'integer', minimum => '0',
51 format_description => 'N',
52 );
53
54 our $prune_backups_format = {
55 'keep-last' => {
56 %prune_option,
57 description => 'Keep the last <N> backups.',
58 },
59 'keep-hourly' => {
60 %prune_option,
61 description => 'Keep backups for the last <N> different hours. If there is more' .
62 'than one backup for a single hour, only the latest one is kept.'
63 },
64 'keep-daily' => {
65 %prune_option,
66 description => 'Keep backups for the last <N> different days. If there is more' .
67 'than one backup for a single day, only the latest one is kept.'
68 },
69 'keep-weekly' => {
70 %prune_option,
71 description => 'Keep backups for the last <N> different weeks. If there is more' .
72 'than one backup for a single week, only the latest one is kept.'
73 },
74 'keep-monthly' => {
75 %prune_option,
76 description => 'Keep backups for the last <N> different months. If there is more' .
77 'than one backup for a single month, only the latest one is kept.'
78 },
79 'keep-yearly' => {
80 %prune_option,
81 description => 'Keep backups for the last <N> different years. If there is more' .
82 'than one backup for a single year, only the latest one is kept.'
83 },
84 };
85 PVE::JSONSchema::register_format('prune-backups', $prune_backups_format, \&validate_prune_backups);
86 sub validate_prune_backups {
87 my ($keep) = @_;
88
89 die "at least one keep-option must be set and positive\n"
90 if !grep { $_ } values %{$keep};
91
92 return $keep;
93 }
94 register_standard_option('prune-backups', {
95 description => "The retention options with shorter intervals are processed first " .
96 "with --keep-last being the very first one. Each option covers a " .
97 "specific period of time. We say that backups within this period " .
98 "are covered by this option. The next option does not take care " .
99 "of already covered backups and only considers older backups.",
100 optional => 1,
101 type => 'string',
102 format => 'prune-backups',
103 });
104
105 my $defaultData = {
106 propertyList => {
107 type => { description => "Storage type." },
108 storage => get_standard_option('pve-storage-id',
109 { completion => \&PVE::Storage::complete_storage }),
110 nodes => get_standard_option('pve-node-list', { optional => 1 }),
111 content => {
112 description => "Allowed content types.\n\nNOTE: the value " .
113 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
114 type => 'string', format => 'pve-storage-content-list',
115 optional => 1,
116 completion => \&PVE::Storage::complete_content_type,
117 },
118 disable => {
119 description => "Flag to disable the storage.",
120 type => 'boolean',
121 optional => 1,
122 },
123 maxfiles => {
124 description => "Maximal number of backup files per VM. Use '0' for unlimted.",
125 type => 'integer',
126 minimum => 0,
127 optional => 1,
128 },
129 'prune-backups' => get_standard_option('prune-backups'),
130 shared => {
131 description => "Mark storage as shared.",
132 type => 'boolean',
133 optional => 1,
134 },
135 'format' => {
136 description => "Default image format.",
137 type => 'string', format => 'pve-storage-format',
138 optional => 1,
139 },
140 },
141 };
142
143 sub content_hash_to_string {
144 my $hash = shift;
145
146 my @cta;
147 foreach my $ct (keys %$hash) {
148 push @cta, $ct if $hash->{$ct};
149 }
150
151 return join(',', @cta);
152 }
153
154 sub valid_content_types {
155 my ($type) = @_;
156
157 my $def = $defaultData->{plugindata}->{$type};
158
159 return {} if !$def;
160
161 return $def->{content}->[0];
162 }
163
164 sub default_format {
165 my ($scfg) = @_;
166
167 my $type = $scfg->{type};
168 my $def = $defaultData->{plugindata}->{$type};
169
170 my $def_format = 'raw';
171 my $valid_formats = [ $def_format ];
172
173 if (defined($def->{format})) {
174 $def_format = $scfg->{format} || $def->{format}->[1];
175 $valid_formats = [ sort keys %{$def->{format}->[0]} ];
176 }
177
178 return wantarray ? ($def_format, $valid_formats) : $def_format;
179 }
180
181 PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
182 sub verify_path {
183 my ($path, $noerr) = @_;
184
185 # fixme: exclude more shell meta characters?
186 # we need absolute paths
187 if ($path !~ m|^/[^;\(\)]+|) {
188 return undef if $noerr;
189 die "value does not look like a valid absolute path\n";
190 }
191 return $path;
192 }
193
194 PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
195 sub verify_server {
196 my ($server, $noerr) = @_;
197
198 if (!(PVE::JSONSchema::pve_verify_ip($server, 1) ||
199 PVE::JSONSchema::pve_verify_dns_name($server, 1)))
200 {
201 return undef if $noerr;
202 die "value does not look like a valid server name or IP address\n";
203 }
204 return $server;
205 }
206
207 PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name);
208 sub parse_lvm_name {
209 my ($name, $noerr) = @_;
210
211 if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) {
212 return undef if $noerr;
213 die "lvm name '$name' contains illegal characters\n";
214 }
215
216 return $name;
217 }
218
219 # fixme: do we need this
220 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
221 #sub verify_portal {
222 # my ($portal, $noerr) = @_;
223 #
224 # # IP with optional port
225 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
226 # return undef if $noerr;
227 # die "value does not look like a valid portal address\n";
228 # }
229 # return $portal;
230 #}
231
232 PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns);
233 sub verify_portal_dns {
234 my ($portal, $noerr) = @_;
235
236 # IP or DNS name with optional port
237 if (!PVE::Tools::parse_host_and_port($portal)) {
238 return undef if $noerr;
239 die "value does not look like a valid portal address\n";
240 }
241 return $portal;
242 }
243
244 PVE::JSONSchema::register_format('pve-storage-content', \&verify_content);
245 sub verify_content {
246 my ($ct, $noerr) = @_;
247
248 my $valid_content = valid_content_types('dir'); # dir includes all types
249
250 if (!$valid_content->{$ct}) {
251 return undef if $noerr;
252 die "invalid content type '$ct'\n";
253 }
254
255 return $ct;
256 }
257
258 PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
259 sub verify_format {
260 my ($fmt, $noerr) = @_;
261
262 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
263 return undef if $noerr;
264 die "invalid format '$fmt'\n";
265 }
266
267 return $fmt;
268 }
269
270 PVE::JSONSchema::register_format('pve-storage-options', \&verify_options);
271 sub verify_options {
272 my ($value, $noerr) = @_;
273
274 # mount options (see man fstab)
275 if ($value !~ m/^\S+$/) {
276 return undef if $noerr;
277 die "invalid options '$value'\n";
278 }
279
280 return $value;
281 }
282
283 PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
284 sub parse_volume_id {
285 my ($volid, $noerr) = @_;
286
287 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
288 return wantarray ? ($1, $2) : $1;
289 }
290 return undef if $noerr;
291 die "unable to parse volume ID '$volid'\n";
292 }
293
294
295 sub private {
296 return $defaultData;
297 }
298
299 sub parse_section_header {
300 my ($class, $line) = @_;
301
302 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
303 my ($type, $storeid) = (lc($1), $2);
304 my $errmsg = undef; # set if you want to skip whole section
305 eval { PVE::JSONSchema::parse_storage_id($storeid); };
306 $errmsg = $@ if $@;
307 my $config = {}; # to return additional attributes
308 return ($type, $storeid, $errmsg, $config);
309 }
310 return undef;
311 }
312
313 sub decode_value {
314 my ($class, $type, $key, $value) = @_;
315
316 my $def = $defaultData->{plugindata}->{$type};
317
318 if ($key eq 'content') {
319 my $valid_content = $def->{content}->[0];
320
321 my $res = {};
322
323 foreach my $c (PVE::Tools::split_list($value)) {
324 if (!$valid_content->{$c}) {
325 warn "storage does not support content type '$c'\n";
326 next;
327 }
328 $res->{$c} = 1;
329 }
330
331 if ($res->{none} && scalar (keys %$res) > 1) {
332 die "unable to combine 'none' with other content types\n";
333 }
334
335 return $res;
336 } elsif ($key eq 'format') {
337 my $valid_formats = $def->{format}->[0];
338
339 if (!$valid_formats->{$value}) {
340 warn "storage does not support format '$value'\n";
341 next;
342 }
343
344 return $value;
345 } elsif ($key eq 'nodes') {
346 my $res = {};
347
348 foreach my $node (PVE::Tools::split_list($value)) {
349 if (PVE::JSONSchema::pve_verify_node_name($node)) {
350 $res->{$node} = 1;
351 }
352 }
353
354 # fixme:
355 # no node restrictions for local storage
356 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
357 # die "storage '$storeid' does not allow node restrictions\n";
358 #}
359
360 return $res;
361 }
362
363 return $value;
364 }
365
366 sub encode_value {
367 my ($class, $type, $key, $value) = @_;
368
369 if ($key eq 'nodes') {
370 return join(',', keys(%$value));
371 } elsif ($key eq 'content') {
372 my $res = content_hash_to_string($value) || 'none';
373 return $res;
374 }
375
376 return $value;
377 }
378
379 sub parse_config {
380 my ($class, $filename, $raw) = @_;
381
382 my $cfg = $class->SUPER::parse_config($filename, $raw);
383 my $ids = $cfg->{ids};
384
385 # make sure we have a reasonable 'local:' storage
386 # we want 'local' to be always the same 'type' (on all cluster nodes)
387 if (!$ids->{local} || $ids->{local}->{type} ne 'dir' ||
388 ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) {
389 $ids->{local} = {
390 type => 'dir',
391 priority => 0, # force first entry
392 path => '/var/lib/vz',
393 maxfiles => 0,
394 content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1, snippets => 1},
395 };
396 }
397
398 # make sure we have a path
399 $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path};
400
401 # remove node restrictions for local storage
402 delete($ids->{local}->{nodes});
403
404 foreach my $storeid (keys %$ids) {
405 my $d = $ids->{$storeid};
406 my $type = $d->{type};
407
408 my $def = $defaultData->{plugindata}->{$type};
409
410 if ($def->{content}) {
411 $d->{content} = $def->{content}->[1] if !$d->{content};
412 }
413 if (grep { $_ eq $type } @SHARED_STORAGE) {
414 $d->{shared} = 1;
415 }
416 }
417
418 return $cfg;
419 }
420
421 # Storage implementation
422
423 # called during addition of storage (before the new storage config got written)
424 # die to abort additon if there are (grave) problems
425 # NOTE: runs in a storage config *locked* context
426 sub on_add_hook {
427 my ($class, $storeid, $scfg, %param) = @_;
428
429 # do nothing by default
430 }
431
432 # called during storage configuration update (before the updated storage config got written)
433 # die to abort the update if there are (grave) problems
434 # NOTE: runs in a storage config *locked* context
435 sub on_update_hook {
436 my ($class, $storeid, $scfg, %param) = @_;
437
438 # do nothing by default
439 }
440
441 # called during deletion of storage (before the new storage config got written)
442 # and if the activate check on addition fails, to cleanup all storage traces
443 # which on_add_hook may have created.
444 # die to abort deletion if there are (very grave) problems
445 # NOTE: runs in a storage config *locked* context
446 sub on_delete_hook {
447 my ($class, $storeid, $scfg) = @_;
448
449 # do nothing by default
450 }
451
452 sub cluster_lock_storage {
453 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
454
455 my $res;
456 if (!$shared) {
457 my $lockid = "pve-storage-$storeid";
458 my $lockdir = "/var/lock/pve-manager";
459 mkdir $lockdir;
460 $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
461 die $@ if $@;
462 } else {
463 $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
464 die $@ if $@;
465 }
466 return $res;
467 }
468
469 sub parse_name_dir {
470 my $name = shift;
471
472 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
473 return ($1, $3, $2); # (name, format, isBase)
474 }
475
476 die "unable to parse volume filename '$name'\n";
477 }
478
479 sub parse_volname {
480 my ($class, $volname) = @_;
481
482 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
483 my ($basedvmid, $basename) = ($1, $2);
484 parse_name_dir($basename);
485 my ($vmid, $name) = ($3, $4);
486 my (undef, $format, $isBase) = parse_name_dir($name);
487 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
488 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
489 my ($vmid, $name) = ($1, $2);
490 my (undef, $format, $isBase) = parse_name_dir($name);
491 return ('images', $name, $vmid, undef, undef, $isBase, $format);
492 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::iso_extension_re)$!) {
493 return ('iso', $1);
494 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
495 return ('vztmpl', $1);
496 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
497 return ('rootdir', $1, $1);
498 } elsif ($volname =~ m!^backup/([^/]+(?:\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\COMPRESSOR_RE}))?))))$!) {
499 my $fn = $1;
500 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
501 return ('backup', $fn, $2);
502 }
503 return ('backup', $fn);
504 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
505 return ('snippets', $1);
506 }
507
508 die "unable to parse directory volume name '$volname'\n";
509 }
510
511 my $vtype_subdirs = {
512 images => 'images',
513 rootdir => 'private',
514 iso => 'template/iso',
515 vztmpl => 'template/cache',
516 backup => 'dump',
517 snippets => 'snippets',
518 };
519
520 sub get_vtype_subdirs {
521 return $vtype_subdirs;
522 }
523
524 sub get_subdir {
525 my ($class, $scfg, $vtype) = @_;
526
527 my $path = $scfg->{path};
528
529 die "storage definintion has no path\n" if !$path;
530
531 my $subdir = $vtype_subdirs->{$vtype};
532
533 die "unknown vtype '$vtype'\n" if !defined($subdir);
534
535 return "$path/$subdir";
536 }
537
538 sub filesystem_path {
539 my ($class, $scfg, $volname, $snapname) = @_;
540
541 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
542 $class->parse_volname($volname);
543
544 # Note: qcow2/qed has internal snapshot, so path is always
545 # the same (with or without snapshot => same file).
546 die "can't snapshot this image format\n"
547 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
548
549 my $dir = $class->get_subdir($scfg, $vtype);
550
551 $dir .= "/$vmid" if $vtype eq 'images';
552
553 my $path = "$dir/$name";
554
555 return wantarray ? ($path, $vmid, $vtype) : $path;
556 }
557
558 sub path {
559 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
560
561 return $class->filesystem_path($scfg, $volname, $snapname);
562 }
563
564 sub create_base {
565 my ($class, $storeid, $scfg, $volname) = @_;
566
567 # this only works for file based storage types
568 die "storage definition has no path\n" if !$scfg->{path};
569
570 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
571 $class->parse_volname($volname);
572
573 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
574
575 die "create_base not possible with base image\n" if $isBase;
576
577 my $path = $class->filesystem_path($scfg, $volname);
578
579 my ($size, undef, $used, $parent) = file_size_info($path);
580 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
581
582 die "volname '$volname' contains wrong information about parent\n"
583 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
584
585 my $newname = $name;
586 $newname =~ s/^vm-/base-/;
587
588 my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" :
589 "$vmid/$newname";
590
591 my $newpath = $class->filesystem_path($scfg, $newvolname);
592
593 die "file '$newpath' already exists\n" if -f $newpath;
594
595 rename($path, $newpath) ||
596 die "rename '$path' to '$newpath' failed - $!\n";
597
598 # We try to protect base volume
599
600 chmod(0444, $newpath); # nobody should write anything
601
602 # also try to set immutable flag
603 eval { run_command(['/usr/bin/chattr', '+i', $newpath]); };
604 warn $@ if $@;
605
606 return $newvolname;
607 }
608
609 my $get_vm_disk_number = sub {
610 my ($disk_name, $scfg, $vmid, $suffix) = @_;
611
612 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
613
614 my $type = $scfg->{type};
615 my $def = { %{$defaultData->{plugindata}->{$type}} };
616
617 my $valid = $def->{format}[0];
618 if ($valid->{subvol}) {
619 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
620 }
621
622 if ($disk_name =~ m/$disk_regex/) {
623 return $2;
624 }
625
626 return undef;
627 };
628
629 sub get_next_vm_diskname {
630 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
631
632 $fmt //= '';
633 my $prefix = ($fmt eq 'subvol') ? 'subvol' : 'vm';
634 my $suffix = $add_fmt_suffix ? ".$fmt" : '';
635
636 my $disk_ids = {};
637 foreach my $disk (@$disk_list) {
638 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
639 $disk_ids->{$disknum} = 1 if defined($disknum);
640 }
641
642 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
643 if (!$disk_ids->{$i}) {
644 return "$prefix-$vmid-disk-$i$suffix";
645 }
646 }
647
648 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
649 }
650
651 sub find_free_diskname {
652 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
653
654 my $disks = $class->list_images($storeid, $scfg, $vmid);
655
656 my $disk_list = [ map { $_->{volid} } @$disks ];
657
658 return get_next_vm_diskname($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
659 }
660
661 sub clone_image {
662 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
663
664 # this only works for file based storage types
665 die "storage definintion has no path\n" if !$scfg->{path};
666
667 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
668 $class->parse_volname($volname);
669
670 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
671
672 die "this storage type does not support clone_image on snapshot\n" if $snap;
673
674 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
675
676 die "clone_image only works on base images\n" if !$isBase;
677
678 my $imagedir = $class->get_subdir($scfg, 'images');
679 $imagedir .= "/$vmid";
680
681 mkpath $imagedir;
682
683 my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
684
685 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
686
687 my $newvol = "$basevmid/$basename/$vmid/$name";
688
689 my $path = $class->filesystem_path($scfg, $newvol);
690
691 # Note: we use relative paths, so we need to call chdir before qemu-img
692 eval {
693 local $CWD = $imagedir;
694
695 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
696 '-f', 'qcow2', $path];
697
698 run_command($cmd);
699 };
700 my $err = $@;
701
702 die $err if $err;
703
704 return $newvol;
705 }
706
707 sub alloc_image {
708 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
709
710 my $imagedir = $class->get_subdir($scfg, 'images');
711 $imagedir .= "/$vmid";
712
713 mkpath $imagedir;
714
715 $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
716
717 my (undef, $tmpfmt) = parse_name_dir($name);
718
719 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
720 if $tmpfmt ne $fmt;
721
722 my $path = "$imagedir/$name";
723
724 die "disk image '$path' already exists\n" if -e $path;
725
726 if ($fmt eq 'subvol') {
727 # only allow this if size = 0, so that user knows what he is doing
728 die "storage does not support subvol quotas\n" if $size != 0;
729
730 my $old_umask = umask(0022);
731 my $err;
732 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
733 umask $old_umask;
734 die $err if $err;
735 } else {
736 my $cmd = ['/usr/bin/qemu-img', 'create'];
737
738 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
739
740 push @$cmd, '-f', $fmt, $path, "${size}K";
741
742 eval { run_command($cmd, errmsg => "unable to create image"); };
743 if ($@) {
744 unlink $path;
745 rmdir $imagedir;
746 die "$@";
747 }
748 }
749
750 return "$vmid/$name";
751 }
752
753 sub free_image {
754 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
755
756 my $path = $class->filesystem_path($scfg, $volname);
757
758 if ($isBase) {
759 # try to remove immutable flag
760 eval { run_command(['/usr/bin/chattr', '-i', $path]); };
761 warn $@ if $@;
762 }
763
764 if (defined($format) && ($format eq 'subvol')) {
765 File::Path::remove_tree($path);
766 } else {
767 if (!(-f $path || -l $path)) {
768 warn "disk image '$path' does not exist\n";
769 return undef;
770 }
771
772 unlink($path) || die "unlink '$path' failed - $!\n";
773 }
774
775 # try to cleanup directory to not clutter storage with empty $vmid dirs if
776 # all images from a guest got deleted
777 my $dir = dirname($path);
778 rmdir($dir);
779
780 return undef;
781 }
782
783 sub file_size_info {
784 my ($filename, $timeout) = @_;
785
786 my $st = File::stat::stat($filename);
787
788 if (!defined($st)) {
789 my $extramsg = -l $filename ? ' - dangling symlink?' : '';
790 warn "failed to stat '$filename'$extramsg\n";
791 return undef;
792 }
793
794 if (S_ISDIR($st->mode)) {
795 return wantarray ? (0, 'subvol', 0, undef, $st->ctime) : 1;
796 }
797
798 my $json = '';
799 eval {
800 run_command(['/usr/bin/qemu-img', 'info', '--output=json', $filename],
801 timeout => $timeout,
802 outfunc => sub { $json .= shift },
803 errfunc => sub { warn "$_[0]\n" }
804 );
805 };
806 warn $@ if $@;
807
808 my $info = eval { decode_json($json) };
809 warn "could not parse qemu-img info command output for '$filename'\n" if $@;
810
811 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
812
813 return wantarray ? ($size, $format, $used, $parent, $st->ctime) : $size;
814 }
815
816 sub volume_size_info {
817 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
818 my $path = $class->filesystem_path($scfg, $volname);
819 return file_size_info($path, $timeout);
820
821 }
822
823 sub volume_resize {
824 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
825
826 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
827
828 return 1 if $running;
829
830 my $path = $class->filesystem_path($scfg, $volname);
831
832 my $format = ($class->parse_volname($volname))[6];
833
834 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
835
836 run_command($cmd, timeout => 10);
837
838 return undef;
839 }
840
841 sub volume_snapshot {
842 my ($class, $scfg, $storeid, $volname, $snap) = @_;
843
844 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
845
846 my $path = $class->filesystem_path($scfg, $volname);
847
848 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
849
850 run_command($cmd);
851
852 return undef;
853 }
854
855 sub volume_rollback_is_possible {
856 my ($class, $scfg, $storeid, $volname, $snap) = @_;
857
858 return 1;
859 }
860
861 sub volume_snapshot_rollback {
862 my ($class, $scfg, $storeid, $volname, $snap) = @_;
863
864 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
865
866 my $path = $class->filesystem_path($scfg, $volname);
867
868 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
869
870 run_command($cmd);
871
872 return undef;
873 }
874
875 sub volume_snapshot_delete {
876 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
877
878 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
879
880 return 1 if $running;
881
882 my $path = $class->filesystem_path($scfg, $volname);
883
884 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
885
886 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
887
888 run_command($cmd);
889
890 return undef;
891 }
892
893 sub volume_snapshot_needs_fsfreeze {
894
895 return 0;
896 }
897 sub storage_can_replicate {
898 my ($class, $scfg, $storeid, $format) = @_;
899
900 return 0;
901 }
902
903 sub volume_has_feature {
904 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_;
905
906 my $features = {
907 snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
908 clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
909 template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} },
910 copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
911 current => {qcow2 => 1, raw => 1, vmdk => 1},
912 snap => {qcow2 => 1} },
913 sparseinit => { base => {qcow2 => 1, raw => 1, vmdk => 1},
914 current => {qcow2 => 1, raw => 1, vmdk => 1} },
915 };
916
917 # clone_image creates a qcow2 volume
918 return 0 if $feature eq 'clone' &&
919 defined($opts->{valid_target_formats}) &&
920 !(grep { $_ eq 'qcow2' } @{$opts->{valid_target_formats}});
921
922 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
923 $class->parse_volname($volname);
924
925 my $key = undef;
926 if($snapname){
927 $key = 'snap';
928 }else{
929 $key = $isBase ? 'base' : 'current';
930 }
931
932 return 1 if defined($features->{$feature}->{$key}->{$format});
933
934 return undef;
935 }
936
937 sub list_images {
938 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
939
940 my $imagedir = $class->get_subdir($scfg, 'images');
941
942 my ($defFmt, $vaidFmts) = default_format($scfg);
943 my $fmts = join ('|', @$vaidFmts);
944
945 my $res = [];
946
947 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
948
949 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
950 $fn = $1; # untaint
951
952 my $owner = $2;
953 my $name = $3;
954
955 next if !$vollist && defined($vmid) && ($owner ne $vmid);
956
957 my ($size, $format, $used, $parent, $ctime) = file_size_info($fn);
958 next if !($format && defined($size));
959
960 my $volid;
961 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
962 my ($basevmid, $basename) = ($1, $2);
963 $volid = "$storeid:$basevmid/$basename/$owner/$name";
964 } else {
965 $volid = "$storeid:$owner/$name";
966 }
967
968 if ($vollist) {
969 my $found = grep { $_ eq $volid } @$vollist;
970 next if !$found;
971 }
972
973 my $info = {
974 volid => $volid, format => $format,
975 size => $size, vmid => $owner, used => $used, parent => $parent
976 };
977
978 $info->{ctime} = $ctime if $ctime;
979
980 push @$res, $info;
981 }
982
983 return $res;
984 }
985
986 # list templates ($tt = <iso|vztmpl|backup|snippets>)
987 my $get_subdir_files = sub {
988 my ($sid, $path, $tt, $vmid) = @_;
989
990 my $res = [];
991
992 my $has_comment = {};
993
994 foreach my $fn (<$path/*>) {
995
996 if (COMMENT_EXT eq substr($fn, -length(COMMENT_EXT))) {
997 my $real_fn = substr($fn, 0, length($fn) - length(COMMENT_EXT));
998 if (!defined($has_comment->{$real_fn})) {
999 $has_comment->{$real_fn} = (-f $fn);
1000 }
1001 next; # we do not need to do anything with comments themselves
1002 }
1003
1004 my $st = File::stat::stat($fn);
1005
1006 next if (!$st || S_ISDIR($st->mode));
1007
1008 my $info;
1009
1010 if ($tt eq 'iso') {
1011 next if $fn !~ m!/([^/]+$PVE::Storage::iso_extension_re)$!i;
1012
1013 $info = { volid => "$sid:iso/$1", format => 'iso' };
1014
1015 } elsif ($tt eq 'vztmpl') {
1016 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
1017
1018 $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
1019
1020 } elsif ($tt eq 'backup') {
1021 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
1022 next if $fn !~ m!/([^/]+\.(tgz|(?:(?:tar|vma)(?:\.(${\COMPRESSOR_RE}))?)))$!;
1023 my $original = $fn;
1024 my $format = $2;
1025 $fn = $1;
1026 $info = { volid => "$sid:backup/$fn", format => $format };
1027
1028 my $archive_info = eval { PVE::Storage::archive_info($fn) } // {};
1029
1030 $info->{ctime} = $archive_info->{ctime} if defined($archive_info->{ctime});
1031
1032 if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
1033 $info->{vmid} = $vmid // $1;
1034 }
1035
1036 my $comment_fn = $original.COMMENT_EXT;
1037 if (!defined($has_comment->{$original})) {
1038 $has_comment->{$original} = (-f $comment_fn);
1039 }
1040
1041 if ($has_comment->{$original}) {
1042 my $comment = PVE::Tools::file_read_firstline($comment_fn);
1043 $info->{comment} = $comment if defined($comment);
1044 }
1045
1046 } elsif ($tt eq 'snippets') {
1047
1048 $info = {
1049 volid => "$sid:snippets/". basename($fn),
1050 format => 'snippet',
1051 };
1052 }
1053
1054 $info->{size} = $st->size;
1055 $info->{ctime} //= $st->ctime;
1056
1057 push @$res, $info;
1058 }
1059
1060 return $res;
1061 };
1062
1063 sub list_volumes {
1064 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
1065
1066 my $res = [];
1067 my $vmlist = PVE::Cluster::get_vmlist();
1068 foreach my $type (@$content_types) {
1069 my $data;
1070
1071 if ($type eq 'images' || $type eq 'rootdir') {
1072 $data = $class->list_images($storeid, $scfg, $vmid);
1073 } elsif ($scfg->{path}) {
1074 my $path = $class->get_subdir($scfg, $type);
1075
1076 if ($type eq 'iso' && !defined($vmid)) {
1077 $data = $get_subdir_files->($storeid, $path, 'iso');
1078 } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
1079 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
1080 } elsif ($type eq 'backup') {
1081 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
1082 } elsif ($type eq 'snippets') {
1083 $data = $get_subdir_files->($storeid, $path, 'snippets');
1084 }
1085 }
1086
1087 next if !$data;
1088
1089 foreach my $item (@$data) {
1090 if ($type eq 'images' || $type eq 'rootdir') {
1091 my $vminfo = $vmlist->{ids}->{$item->{vmid}};
1092 my $vmtype;
1093 if (defined($vminfo)) {
1094 $vmtype = $vminfo->{type};
1095 }
1096 if (defined($vmtype) && $vmtype eq 'lxc') {
1097 $item->{content} = 'rootdir';
1098 } else {
1099 $item->{content} = 'images';
1100 }
1101 next if $type ne $item->{content};
1102 } else {
1103 $item->{content} = $type;
1104 }
1105
1106 push @$res, $item;
1107 }
1108 }
1109
1110 return $res;
1111 }
1112
1113 sub status {
1114 my ($class, $storeid, $scfg, $cache) = @_;
1115
1116 my $path = $scfg->{path};
1117
1118 die "storage definintion has no path\n" if !$path;
1119
1120 my $timeout = 2;
1121 my $res = PVE::Tools::df($path, $timeout);
1122
1123 return undef if !$res || !$res->{total};
1124
1125 return ($res->{total}, $res->{avail}, $res->{used}, 1);
1126 }
1127
1128 sub volume_snapshot_list {
1129 my ($class, $scfg, $storeid, $volname) = @_;
1130
1131 # implement in subclass
1132 die "Volume_snapshot_list is not implemented for $class";
1133
1134 # return an empty array if dataset does not exist.
1135 }
1136
1137 sub activate_storage {
1138 my ($class, $storeid, $scfg, $cache) = @_;
1139
1140 my $path = $scfg->{path};
1141
1142 die "storage definintion has no path\n" if !$path;
1143
1144 # this path test may hang indefinitely on unresponsive mounts
1145 my $timeout = 2;
1146 if (! PVE::Tools::run_fork_with_timeout($timeout, sub {-d $path})) {
1147 die "unable to activate storage '$storeid' - " .
1148 "directory '$path' does not exist or is unreachable\n";
1149 }
1150
1151
1152 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1153
1154 if (defined($scfg->{content})) {
1155 foreach my $vtype (keys %$vtype_subdirs) {
1156 # OpenVZMigrate uses backup (dump) dir
1157 if (defined($scfg->{content}->{$vtype}) ||
1158 ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) {
1159 my $subdir = $class->get_subdir($scfg, $vtype);
1160 mkpath $subdir if $subdir ne $path;
1161 }
1162 }
1163 }
1164 }
1165
1166 sub deactivate_storage {
1167 my ($class, $storeid, $scfg, $cache) = @_;
1168
1169 # do nothing by default
1170 }
1171
1172 sub map_volume {
1173 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1174
1175 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1176 return $path;
1177 }
1178
1179 sub unmap_volume {
1180 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1181
1182 return 1;
1183 }
1184
1185 sub activate_volume {
1186 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1187
1188 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1189
1190 # check is volume exists
1191 if ($scfg->{path}) {
1192 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
1193 } else {
1194 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
1195 }
1196 }
1197
1198 sub deactivate_volume {
1199 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1200
1201 # do nothing by default
1202 }
1203
1204 sub check_connection {
1205 my ($class, $storeid, $scfg) = @_;
1206 # do nothing by default
1207 return 1;
1208 }
1209
1210 sub prune_backups {
1211 my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
1212
1213 $logfunc //= sub { print "$_[1]\n" };
1214
1215 my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
1216
1217 my $backup_groups = {};
1218 my $prune_list = [];
1219
1220 foreach my $backup (@{$backups}) {
1221 my $volid = $backup->{volid};
1222 my $backup_vmid = $backup->{vmid};
1223 my $archive_info = eval { PVE::Storage::archive_info($volid) } // {};
1224 my $backup_type = $archive_info->{type} // 'unknown';
1225
1226 next if defined($type) && $type ne $backup_type;
1227
1228 my $prune_entry = {
1229 ctime => $backup->{ctime},
1230 type => $backup_type,
1231 volid => $volid,
1232 };
1233
1234 $prune_entry->{vmid} = $backup_vmid if defined($backup_vmid);
1235
1236 if ($archive_info->{is_std_name}) {
1237 $prune_entry->{ctime} = $archive_info->{ctime};
1238 my $group = "$backup_type/$backup_vmid";
1239 push @{$backup_groups->{$group}}, $prune_entry;
1240 } else {
1241 # ignore backups that don't use the standard naming scheme
1242 $prune_entry->{mark} = 'protected';
1243 }
1244
1245 push @{$prune_list}, $prune_entry;
1246 }
1247
1248 foreach my $backup_group (values %{$backup_groups}) {
1249 PVE::Storage::prune_mark_backup_group($backup_group, $keep);
1250 }
1251
1252 my $failed;
1253 if (!$dryrun) {
1254 foreach my $prune_entry (@{$prune_list}) {
1255 next if $prune_entry->{mark} ne 'remove';
1256
1257 my $volid = $prune_entry->{volid};
1258 $logfunc->('info', "removing backup '$volid'");
1259 eval {
1260 my (undef, $volname) = parse_volume_id($volid);
1261 my $archive_path = $class->filesystem_path($scfg, $volname);
1262 PVE::Storage::archive_remove($archive_path);
1263 };
1264 if (my $err = $@) {
1265 $logfunc->('err', "error when removing backup '$volid' - $err\n");
1266 $failed = 1;
1267 }
1268 }
1269 }
1270 die "error pruning backups - check log\n" if $failed;
1271
1272 return $prune_list;
1273 }
1274
1275 # Import/Export interface:
1276 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1277 # the default implementations will return this if $scfg->{path} is set,
1278 # mimicking the old PVE::Storage::storage_migrate() function.
1279 #
1280 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1281 # functions in case the format doesn't match their specialized
1282 # implementations to reuse the raw/tar code.
1283 #
1284 # Format specification:
1285 # The following formats are all prefixed with image information in the form
1286 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1287 # to preallocate the image on storages which require it.
1288 #
1289 # raw+size: (image files only)
1290 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1291 # qcow2+size, vmdk: (image files only)
1292 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1293 # files which are already in qcow2 format, or via `qemu-img convert`.
1294 # Note that these formats are only valid with $with_snapshots being true.
1295 # tar+size: (subvolumes only)
1296 # A GNU tar stream containing just the inner contents of the subvolume.
1297 # This does not distinguish between the contents of a privileged or
1298 # unprivileged container. In other words, this is from the root user
1299 # namespace's point of view with no uid-mapping in effect.
1300 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1301
1302 # Plugins may reuse these helpers. Changes to the header format should be
1303 # reflected by changes to the function prototypes.
1304 sub write_common_header($$) {
1305 my ($fh, $image_size_in_bytes) = @_;
1306 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1307 }
1308
1309 sub read_common_header($) {
1310 my ($fh) = @_;
1311 sysread($fh, my $size, 8);
1312 $size = unpack('Q<', $size);
1313 die "import: no size found in export header, aborting.\n" if !defined($size);
1314 die "import: got a bad size (not a multiple of 1K), aborting.\n" if ($size&1023);
1315 # Size is in bytes!
1316 return $size;
1317 }
1318
1319 # Export a volume into a file handle as a stream of desired format.
1320 sub volume_export {
1321 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1322 if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) {
1323 my $file = $class->path($scfg, $volname, $storeid)
1324 or goto unsupported;
1325 my ($size, $file_format) = file_size_info($file);
1326
1327 if ($format eq 'raw+size') {
1328 goto unsupported if $with_snapshots || $file_format eq 'subvol';
1329 write_common_header($fh, $size);
1330 if ($file_format eq 'raw') {
1331 run_command(['dd', "if=$file", "bs=4k"], output => '>&'.fileno($fh));
1332 } else {
1333 run_command(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1334 output => '>&'.fileno($fh));
1335 }
1336 return;
1337 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1338 my $data_format = $1;
1339 goto unsupported if !$with_snapshots || $file_format ne $data_format;
1340 write_common_header($fh, $size);
1341 run_command(['dd', "if=$file", "bs=4k"], output => '>&'.fileno($fh));
1342 return;
1343 } elsif ($format eq 'tar+size') {
1344 goto unsupported if $file_format ne 'subvol';
1345 write_common_header($fh, $size);
1346 run_command(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1347 output => '>&'.fileno($fh));
1348 return;
1349 }
1350 }
1351 unsupported:
1352 die "volume export format $format not available for $class";
1353 }
1354
1355 sub volume_export_formats {
1356 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1357 if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) {
1358 my $file = $class->path($scfg, $volname, $storeid)
1359 or return;
1360 my ($size, $format) = file_size_info($file);
1361
1362 if ($with_snapshots) {
1363 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1364 return ();
1365 }
1366 return ('tar+size') if $format eq 'subvol';
1367 return ('raw+size');
1368 }
1369 return ();
1370 }
1371
1372 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1373 sub volume_import {
1374 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
1375
1376 die "volume import format '$format' not available for $class\n"
1377 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1378 my $data_format = $1;
1379
1380 die "format $format cannot be imported without snapshots\n"
1381 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1382 die "format $format cannot be imported with snapshots\n"
1383 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1384
1385 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1386 $class->parse_volname($volname);
1387
1388 # XXX: Should we bother with conversion routines at this level? This won't
1389 # happen without manual CLI usage, so for now we just error out...
1390 die "cannot import format $format into a file of format $file_format\n"
1391 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1392
1393 # Check for an existing file first since interrupting alloc_image doesn't
1394 # free it.
1395 my $file = $class->path($scfg, $volname, $storeid);
1396 if (-e $file) {
1397 die "file '$file' already exists\n" if !$allow_rename;
1398 warn "file '$file' already exists - importing with a different name\n";
1399 $name = undef;
1400 }
1401
1402 my ($size) = read_common_header($fh);
1403 $size = int($size/1024);
1404
1405 eval {
1406 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1407 my $oldname = $volname;
1408 $volname = $allocname;
1409 if (defined($name) && $allocname ne $oldname) {
1410 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1411 }
1412 my $file = $class->path($scfg, $volname, $storeid)
1413 or die "internal error: failed to get path to newly allocated volume $volname\n";
1414 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1415 run_command(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1416 input => '<&'.fileno($fh));
1417 } elsif ($data_format eq 'tar') {
1418 run_command(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1419 input => '<&'.fileno($fh));
1420 } else {
1421 die "volume import format '$format' not available for $class";
1422 }
1423 };
1424 if (my $err = $@) {
1425 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1426 warn $@ if $@;
1427 die $err;
1428 }
1429
1430 return "$storeid:$volname";
1431 }
1432
1433 sub volume_import_formats {
1434 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1435 if ($scfg->{path} && !defined($base_snapshot)) {
1436 my $format = ($class->parse_volname($volname))[6];
1437 if ($with_snapshots) {
1438 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1439 return ();
1440 }
1441 return ('tar+size') if $format eq 'subvol';
1442 return ('raw+size');
1443 }
1444 return ();
1445 }
1446
1447 1;