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