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