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