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