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