]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::Plugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
074b2cb4 | 5 | |
43f8112f | 6 | use Encode qw(decode); |
ff9c5451 | 7 | use Fcntl ':mode'; |
7fc619d5 | 8 | use File::chdir; |
1dc01b9f | 9 | use File::Path; |
712e27f1 | 10 | use File::Basename; |
92ae59df | 11 | use File::stat qw(); |
074b2cb4 | 12 | |
1dc01b9f | 13 | use PVE::Tools qw(run_command); |
3353698f | 14 | use PVE::JSONSchema qw(get_standard_option register_standard_option); |
1dc01b9f DM |
15 | use PVE::Cluster qw(cfs_register_file); |
16 | ||
80699b1d TL |
17 | use JSON; |
18 | ||
1dc01b9f DM |
19 | use base qw(PVE::SectionConfig); |
20 | ||
014d36db | 21 | use constant COMPRESSOR_RE => 'gz|lzo|zst'; |
277cafc0 | 22 | |
90778f7c | 23 | use constant LOG_EXT => ".log"; |
6fef456c | 24 | use constant NOTES_EXT => ".notes"; |
d260b9f3 | 25 | |
766cfd9a WB |
26 | our @COMMON_TAR_FLAGS = qw( |
27 | --one-file-system | |
28 | -p --sparse --numeric-owner --acls | |
29 | --xattrs --xattrs-include=user.* --xattrs-include=security.capability | |
30 | --warning=no-file-ignored --warning=no-xattr-write | |
31 | ); | |
32 | ||
d7875239 WL |
33 | our @SHARED_STORAGE = ( |
34 | 'iscsi', | |
35 | 'nfs', | |
36 | 'cifs', | |
37 | 'rbd', | |
e34ce144 | 38 | 'cephfs', |
d7875239 WL |
39 | 'iscsidirect', |
40 | 'glusterfs', | |
41 | 'zfs', | |
68f1fc27 FE |
42 | 'drbd', |
43 | 'pbs', | |
44 | ); | |
d7875239 | 45 | |
95ff5dbd LS |
46 | our $QCOW2_PREALLOCATION = { |
47 | off => 1, | |
48 | metadata => 1, | |
49 | falloc => 1, | |
50 | full => 1, | |
51 | }; | |
52 | ||
53 | our $RAW_PREALLOCATION = { | |
54 | off => 1, | |
55 | falloc => 1, | |
56 | full => 1, | |
57 | }; | |
58 | ||
c0535aa7 SI |
59 | our $MAX_VOLUMES_PER_GUEST = 1024; |
60 | ||
045ae0a7 | 61 | cfs_register_file ('storage.cfg', |
1dc01b9f DM |
62 | sub { __PACKAGE__->parse_config(@_); }, |
63 | sub { __PACKAGE__->write_config(@_); }); | |
64 | ||
3353698f FE |
65 | my %prune_option = ( |
66 | optional => 1, | |
67 | type => 'integer', minimum => '0', | |
68 | format_description => 'N', | |
69 | ); | |
70 | ||
a0933d7e | 71 | our $prune_backups_format = { |
1b87f013 FE |
72 | 'keep-all' => { |
73 | type => 'boolean', | |
74 | description => 'Keep all backups. Conflicts with the other options when true.', | |
75 | optional => 1, | |
76 | }, | |
48d0cd02 TL |
77 | 'keep-last' => { |
78 | %prune_option, | |
79 | description => 'Keep the last <N> backups.', | |
80 | }, | |
81 | 'keep-hourly' => { | |
82 | %prune_option, | |
83 | description => 'Keep backups for the last <N> different hours. If there is more' . | |
84 | 'than one backup for a single hour, only the latest one is kept.' | |
85 | }, | |
86 | 'keep-daily' => { | |
87 | %prune_option, | |
88 | description => 'Keep backups for the last <N> different days. If there is more' . | |
89 | 'than one backup for a single day, only the latest one is kept.' | |
90 | }, | |
91 | 'keep-weekly' => { | |
92 | %prune_option, | |
93 | description => 'Keep backups for the last <N> different weeks. If there is more' . | |
94 | 'than one backup for a single week, only the latest one is kept.' | |
95 | }, | |
96 | 'keep-monthly' => { | |
97 | %prune_option, | |
98 | description => 'Keep backups for the last <N> different months. If there is more' . | |
99 | 'than one backup for a single month, only the latest one is kept.' | |
100 | }, | |
101 | 'keep-yearly' => { | |
102 | %prune_option, | |
103 | description => 'Keep backups for the last <N> different years. If there is more' . | |
104 | 'than one backup for a single year, only the latest one is kept.' | |
105 | }, | |
3353698f | 106 | }; |
1b87f013 FE |
107 | PVE::JSONSchema::register_format('prune-backups', $prune_backups_format, \&validate_prune_backups); |
108 | sub validate_prune_backups { | |
109 | my ($prune_backups) = @_; | |
110 | ||
d5fc3685 | 111 | my $res = { $prune_backups->%* }; |
1b87f013 | 112 | |
d5fc3685 FE |
113 | my $keep_all = delete $res->{'keep-all'}; |
114 | ||
115 | if (scalar(grep { $_ > 0 } values %{$res}) == 0) { | |
116 | $res = { 'keep-all' => 1 }; | |
117 | } elsif ($keep_all) { | |
1b87f013 FE |
118 | die "keep-all cannot be set together with other options.\n"; |
119 | } | |
120 | ||
d5fc3685 | 121 | return $res; |
1b87f013 | 122 | } |
3353698f FE |
123 | register_standard_option('prune-backups', { |
124 | description => "The retention options with shorter intervals are processed first " . | |
125 | "with --keep-last being the very first one. Each option covers a " . | |
126 | "specific period of time. We say that backups within this period " . | |
127 | "are covered by this option. The next option does not take care " . | |
128 | "of already covered backups and only considers older backups.", | |
129 | optional => 1, | |
130 | type => 'string', | |
131 | format => 'prune-backups', | |
132 | }); | |
35533c68 | 133 | |
1dc01b9f DM |
134 | my $defaultData = { |
135 | propertyList => { | |
136 | type => { description => "Storage type." }, | |
f7621c01 DM |
137 | storage => get_standard_option('pve-storage-id', |
138 | { completion => \&PVE::Storage::complete_storage }), | |
1dc01b9f DM |
139 | nodes => get_standard_option('pve-node-list', { optional => 1 }), |
140 | content => { | |
daccf21e FG |
141 | description => "Allowed content types.\n\nNOTE: the value " . |
142 | "'rootdir' is used for Containers, and value 'images' for VMs.\n", | |
1dc01b9f DM |
143 | type => 'string', format => 'pve-storage-content-list', |
144 | optional => 1, | |
98437f4c | 145 | completion => \&PVE::Storage::complete_content_type, |
1dc01b9f DM |
146 | }, |
147 | disable => { | |
148 | description => "Flag to disable the storage.", | |
149 | type => 'boolean', | |
150 | optional => 1, | |
151 | }, | |
152 | maxfiles => { | |
bbadd165 | 153 | description => "Deprecated: use 'prune-backups' instead. " . |
ffc31266 | 154 | "Maximal number of backup files per VM. Use '0' for unlimited.", |
1dc01b9f DM |
155 | type => 'integer', |
156 | minimum => 0, | |
157 | optional => 1, | |
158 | }, | |
3353698f | 159 | 'prune-backups' => get_standard_option('prune-backups'), |
8009417d FE |
160 | 'max-protected-backups' => { |
161 | description => "Maximal number of protected backups per guest. Use '-1' for unlimited.", | |
162 | type => 'integer', | |
163 | minimum => -1, | |
164 | optional => 1, | |
165 | default => "Unlimited for users with Datastore.Allocate privilege, 5 for other users", | |
166 | }, | |
1dc01b9f DM |
167 | shared => { |
168 | description => "Mark storage as shared.", | |
169 | type => 'boolean', | |
170 | optional => 1, | |
171 | }, | |
045ae0a7 | 172 | 'format' => { |
daccf21e | 173 | description => "Default image format.", |
1dc01b9f DM |
174 | type => 'string', format => 'pve-storage-format', |
175 | optional => 1, | |
176 | }, | |
95ff5dbd LS |
177 | preallocation => { |
178 | description => "Preallocation mode for raw and qcow2 images. " . | |
179 | "Using 'metadata' on raw images results in preallocation=off.", | |
180 | type => 'string', enum => ['off', 'metadata', 'falloc', 'full'], | |
181 | default => 'metadata', | |
182 | optional => 1, | |
183 | }, | |
b539bb46 LN |
184 | 'content-dirs' => { |
185 | description => "Overrides for default content type directories.", | |
3e7bd7da LN |
186 | type => "string", format => "pve-dir-override-list", |
187 | optional => 1, | |
188 | }, | |
1dc01b9f DM |
189 | }, |
190 | }; | |
191 | ||
192 | sub content_hash_to_string { | |
193 | my $hash = shift; | |
194 | ||
195 | my @cta; | |
196 | foreach my $ct (keys %$hash) { | |
197 | push @cta, $ct if $hash->{$ct}; | |
045ae0a7 | 198 | } |
1dc01b9f DM |
199 | |
200 | return join(',', @cta); | |
201 | } | |
202 | ||
203 | sub valid_content_types { | |
204 | my ($type) = @_; | |
205 | ||
206 | my $def = $defaultData->{plugindata}->{$type}; | |
207 | ||
208 | return {} if !$def; | |
209 | ||
210 | return $def->{content}->[0]; | |
211 | } | |
212 | ||
3e7bd7da LN |
213 | sub dirs_hash_to_string { |
214 | my $hash = shift; | |
215 | ||
b539bb46 | 216 | return join(',', map { "$_=$hash->{$_}" } sort keys %$hash); |
3e7bd7da LN |
217 | } |
218 | ||
1dc01b9f DM |
219 | sub default_format { |
220 | my ($scfg) = @_; | |
221 | ||
222 | my $type = $scfg->{type}; | |
223 | my $def = $defaultData->{plugindata}->{$type}; | |
045ae0a7 | 224 | |
1dc01b9f DM |
225 | my $def_format = 'raw'; |
226 | my $valid_formats = [ $def_format ]; | |
227 | ||
228 | if (defined($def->{format})) { | |
229 | $def_format = $scfg->{format} || $def->{format}->[1]; | |
230 | $valid_formats = [ sort keys %{$def->{format}->[0]} ]; | |
231 | } | |
045ae0a7 | 232 | |
1dc01b9f DM |
233 | return wantarray ? ($def_format, $valid_formats) : $def_format; |
234 | } | |
235 | ||
236 | PVE::JSONSchema::register_format('pve-storage-path', \&verify_path); | |
237 | sub verify_path { | |
238 | my ($path, $noerr) = @_; | |
239 | ||
240 | # fixme: exclude more shell meta characters? | |
241 | # we need absolute paths | |
242 | if ($path !~ m|^/[^;\(\)]+|) { | |
243 | return undef if $noerr; | |
244 | die "value does not look like a valid absolute path\n"; | |
245 | } | |
246 | return $path; | |
247 | } | |
248 | ||
249 | PVE::JSONSchema::register_format('pve-storage-server', \&verify_server); | |
250 | sub verify_server { | |
251 | my ($server, $noerr) = @_; | |
252 | ||
6bf617a9 WB |
253 | if (!(PVE::JSONSchema::pve_verify_ip($server, 1) || |
254 | PVE::JSONSchema::pve_verify_dns_name($server, 1))) | |
255 | { | |
1dc01b9f DM |
256 | return undef if $noerr; |
257 | die "value does not look like a valid server name or IP address\n"; | |
258 | } | |
259 | return $server; | |
260 | } | |
261 | ||
5dca5c7c DM |
262 | PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name); |
263 | sub parse_lvm_name { | |
264 | my ($name, $noerr) = @_; | |
265 | ||
97cf933f | 266 | if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) { |
5dca5c7c DM |
267 | return undef if $noerr; |
268 | die "lvm name '$name' contains illegal characters\n"; | |
269 | } | |
270 | ||
271 | return $name; | |
272 | } | |
273 | ||
1dc01b9f DM |
274 | # fixme: do we need this |
275 | #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal); | |
276 | #sub verify_portal { | |
277 | # my ($portal, $noerr) = @_; | |
278 | # | |
279 | # # IP with optional port | |
280 | # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) { | |
281 | # return undef if $noerr; | |
282 | # die "value does not look like a valid portal address\n"; | |
283 | # } | |
284 | # return $portal; | |
285 | #} | |
286 | ||
287 | PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns); | |
288 | sub verify_portal_dns { | |
289 | my ($portal, $noerr) = @_; | |
290 | ||
291 | # IP or DNS name with optional port | |
1689e627 | 292 | if (!PVE::Tools::parse_host_and_port($portal)) { |
1dc01b9f DM |
293 | return undef if $noerr; |
294 | die "value does not look like a valid portal address\n"; | |
295 | } | |
296 | return $portal; | |
297 | } | |
298 | ||
299 | PVE::JSONSchema::register_format('pve-storage-content', \&verify_content); | |
300 | sub verify_content { | |
301 | my ($ct, $noerr) = @_; | |
302 | ||
303 | my $valid_content = valid_content_types('dir'); # dir includes all types | |
045ae0a7 | 304 | |
1dc01b9f DM |
305 | if (!$valid_content->{$ct}) { |
306 | return undef if $noerr; | |
307 | die "invalid content type '$ct'\n"; | |
308 | } | |
309 | ||
310 | return $ct; | |
311 | } | |
312 | ||
313 | PVE::JSONSchema::register_format('pve-storage-format', \&verify_format); | |
314 | sub verify_format { | |
315 | my ($fmt, $noerr) = @_; | |
316 | ||
35533c68 | 317 | if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) { |
1dc01b9f DM |
318 | return undef if $noerr; |
319 | die "invalid format '$fmt'\n"; | |
320 | } | |
321 | ||
322 | return $fmt; | |
323 | } | |
324 | ||
325 | PVE::JSONSchema::register_format('pve-storage-options', \&verify_options); | |
326 | sub verify_options { | |
327 | my ($value, $noerr) = @_; | |
328 | ||
329 | # mount options (see man fstab) | |
330 | if ($value !~ m/^\S+$/) { | |
331 | return undef if $noerr; | |
332 | die "invalid options '$value'\n"; | |
333 | } | |
334 | ||
335 | return $value; | |
336 | } | |
337 | ||
a7f3d909 DM |
338 | PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id); |
339 | sub parse_volume_id { | |
340 | my ($volid, $noerr) = @_; | |
341 | ||
342 | if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) { | |
343 | return wantarray ? ($1, $2) : $1; | |
344 | } | |
345 | return undef if $noerr; | |
346 | die "unable to parse volume ID '$volid'\n"; | |
347 | } | |
348 | ||
3e7bd7da LN |
349 | PVE::JSONSchema::register_format('pve-dir-override', \&verify_dir_override); |
350 | sub verify_dir_override { | |
351 | my ($value, $noerr) = @_; | |
352 | ||
b539bb46 LN |
353 | if($value =~ m/^([a-z]+)=\/.+$/ && |
354 | verify_content($1, $noerr)) { | |
3e7bd7da LN |
355 | return $value; |
356 | } | |
357 | ||
358 | return undef if $noerr; | |
359 | die "invalid override '$value'\n"; | |
360 | } | |
1dc01b9f DM |
361 | |
362 | sub private { | |
363 | return $defaultData; | |
364 | } | |
365 | ||
366 | sub parse_section_header { | |
367 | my ($class, $line) = @_; | |
368 | ||
369 | if ($line =~ m/^(\S+):\s*(\S+)\s*$/) { | |
370 | my ($type, $storeid) = (lc($1), $2); | |
371 | my $errmsg = undef; # set if you want to skip whole section | |
372 | eval { PVE::JSONSchema::parse_storage_id($storeid); }; | |
373 | $errmsg = $@ if $@; | |
374 | my $config = {}; # to return additional attributes | |
375 | return ($type, $storeid, $errmsg, $config); | |
376 | } | |
377 | return undef; | |
378 | } | |
379 | ||
380 | sub decode_value { | |
381 | my ($class, $type, $key, $value) = @_; | |
382 | ||
383 | my $def = $defaultData->{plugindata}->{$type}; | |
384 | ||
385 | if ($key eq 'content') { | |
386 | my $valid_content = $def->{content}->[0]; | |
045ae0a7 | 387 | |
1dc01b9f DM |
388 | my $res = {}; |
389 | ||
390 | foreach my $c (PVE::Tools::split_list($value)) { | |
391 | if (!$valid_content->{$c}) { | |
703de49e WL |
392 | warn "storage does not support content type '$c'\n"; |
393 | next; | |
1dc01b9f DM |
394 | } |
395 | $res->{$c} = 1; | |
045ae0a7 | 396 | } |
1dc01b9f DM |
397 | |
398 | if ($res->{none} && scalar (keys %$res) > 1) { | |
399 | die "unable to combine 'none' with other content types\n"; | |
400 | } | |
401 | ||
03c487e5 FE |
402 | if (scalar(keys $res->%*) == 0 && !$valid_content->{none}) { |
403 | die "storage does not support content type 'none'\n"; | |
404 | } | |
405 | ||
1dc01b9f DM |
406 | return $res; |
407 | } elsif ($key eq 'format') { | |
408 | my $valid_formats = $def->{format}->[0]; | |
409 | ||
410 | if (!$valid_formats->{$value}) { | |
703de49e WL |
411 | warn "storage does not support format '$value'\n"; |
412 | next; | |
1dc01b9f DM |
413 | } |
414 | ||
415 | return $value; | |
416 | } elsif ($key eq 'nodes') { | |
417 | my $res = {}; | |
418 | ||
419 | foreach my $node (PVE::Tools::split_list($value)) { | |
420 | if (PVE::JSONSchema::pve_verify_node_name($node)) { | |
421 | $res->{$node} = 1; | |
422 | } | |
423 | } | |
424 | ||
425 | # fixme: | |
426 | # no node restrictions for local storage | |
427 | #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) { | |
428 | # die "storage '$storeid' does not allow node restrictions\n"; | |
429 | #} | |
430 | ||
3e7bd7da | 431 | return $res; |
b539bb46 | 432 | } elsif ($key eq 'content-dirs') { |
3e7bd7da LN |
433 | my $valid_content = $def->{content}->[0]; |
434 | my $res = {}; | |
435 | ||
436 | foreach my $dir (PVE::Tools::split_list($value)) { | |
b539bb46 | 437 | my ($content, $path) = split(/=/, $dir, 2); |
3e7bd7da LN |
438 | |
439 | if (!$valid_content->{$content}) { | |
440 | warn "storage does not support content type '$content'\n"; | |
441 | next; | |
442 | } | |
443 | ||
444 | $res->{$content} = $path; | |
445 | } | |
446 | ||
1dc01b9f DM |
447 | return $res; |
448 | } | |
449 | ||
450 | return $value; | |
451 | } | |
452 | ||
453 | sub encode_value { | |
454 | my ($class, $type, $key, $value) = @_; | |
455 | ||
456 | if ($key eq 'nodes') { | |
457 | return join(',', keys(%$value)); | |
458 | } elsif ($key eq 'content') { | |
459 | my $res = content_hash_to_string($value) || 'none'; | |
460 | return $res; | |
b539bb46 | 461 | } elsif ($key eq 'content-dirs') { |
3e7bd7da LN |
462 | my $res = dirs_hash_to_string($value); |
463 | return $res; | |
1dc01b9f DM |
464 | } |
465 | ||
466 | return $value; | |
467 | } | |
468 | ||
469 | sub parse_config { | |
470 | my ($class, $filename, $raw) = @_; | |
471 | ||
472 | my $cfg = $class->SUPER::parse_config($filename, $raw); | |
473 | my $ids = $cfg->{ids}; | |
474 | ||
475 | # make sure we have a reasonable 'local:' storage | |
dc6ff39f | 476 | # we want 'local' to be always the same 'type' (on all cluster nodes) |
1dc01b9f DM |
477 | if (!$ids->{local} || $ids->{local}->{type} ne 'dir' || |
478 | ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) { | |
479 | $ids->{local} = { | |
480 | type => 'dir', | |
481 | priority => 0, # force first entry | |
482 | path => '/var/lib/vz', | |
bbadd165 | 483 | 'prune-backups' => 'keep-all=1', |
3a3ff9d5 FE |
484 | content => { |
485 | backup => 1, | |
486 | images => 1, | |
487 | iso => 1, | |
488 | rootdir => 1, | |
489 | snippets => 1, | |
490 | vztmpl => 1, | |
491 | }, | |
1dc01b9f DM |
492 | }; |
493 | } | |
045ae0a7 | 494 | |
1dc01b9f DM |
495 | # make sure we have a path |
496 | $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path}; | |
497 | ||
498 | # remove node restrictions for local storage | |
499 | delete($ids->{local}->{nodes}); | |
500 | ||
501 | foreach my $storeid (keys %$ids) { | |
502 | my $d = $ids->{$storeid}; | |
503 | my $type = $d->{type}; | |
504 | ||
505 | my $def = $defaultData->{plugindata}->{$type}; | |
506 | ||
507 | if ($def->{content}) { | |
508 | $d->{content} = $def->{content}->[1] if !$d->{content}; | |
509 | } | |
d7875239 | 510 | if (grep { $_ eq $type } @SHARED_STORAGE) { |
1dc01b9f DM |
511 | $d->{shared} = 1; |
512 | } | |
513 | } | |
514 | ||
515 | return $cfg; | |
516 | } | |
517 | ||
95ff5dbd LS |
518 | sub preallocation_cmd_option { |
519 | my ($scfg, $fmt) = @_; | |
520 | ||
521 | my $prealloc = $scfg->{preallocation}; | |
522 | ||
523 | if ($fmt eq 'qcow2') { | |
524 | $prealloc = $prealloc // 'metadata'; | |
525 | ||
526 | die "preallocation mode '$prealloc' not supported by format '$fmt'\n" | |
527 | if !$QCOW2_PREALLOCATION->{$prealloc}; | |
528 | ||
529 | return "preallocation=$prealloc"; | |
530 | } elsif ($fmt eq 'raw') { | |
531 | $prealloc = $prealloc // 'off'; | |
532 | $prealloc = 'off' if $prealloc eq 'metadata'; | |
533 | ||
534 | die "preallocation mode '$prealloc' not supported by format '$fmt'\n" | |
535 | if !$RAW_PREALLOCATION->{$prealloc}; | |
536 | ||
537 | return "preallocation=$prealloc"; | |
538 | } | |
539 | ||
540 | return; | |
541 | } | |
542 | ||
1dc01b9f DM |
543 | # Storage implementation |
544 | ||
3932ca0d | 545 | # called during addition of storage (before the new storage config got written) |
ffc31266 | 546 | # die to abort addition if there are (grave) problems |
3932ca0d TL |
547 | # NOTE: runs in a storage config *locked* context |
548 | sub on_add_hook { | |
549 | my ($class, $storeid, $scfg, %param) = @_; | |
550 | ||
551 | # do nothing by default | |
afeda182 | 552 | return undef; |
3932ca0d TL |
553 | } |
554 | ||
0ff4cfea DM |
555 | # called during storage configuration update (before the updated storage config got written) |
556 | # die to abort the update if there are (grave) problems | |
557 | # NOTE: runs in a storage config *locked* context | |
558 | sub on_update_hook { | |
559 | my ($class, $storeid, $scfg, %param) = @_; | |
560 | ||
561 | # do nothing by default | |
afeda182 | 562 | return undef; |
0ff4cfea DM |
563 | } |
564 | ||
3932ca0d TL |
565 | # called during deletion of storage (before the new storage config got written) |
566 | # and if the activate check on addition fails, to cleanup all storage traces | |
567 | # which on_add_hook may have created. | |
568 | # die to abort deletion if there are (very grave) problems | |
569 | # NOTE: runs in a storage config *locked* context | |
570 | sub on_delete_hook { | |
571 | my ($class, $storeid, $scfg) = @_; | |
572 | ||
573 | # do nothing by default | |
afeda182 | 574 | return undef; |
3932ca0d TL |
575 | } |
576 | ||
1dc01b9f DM |
577 | sub cluster_lock_storage { |
578 | my ($class, $storeid, $shared, $timeout, $func, @param) = @_; | |
579 | ||
580 | my $res; | |
581 | if (!$shared) { | |
582 | my $lockid = "pve-storage-$storeid"; | |
583 | my $lockdir = "/var/lock/pve-manager"; | |
584 | mkdir $lockdir; | |
045ae0a7 | 585 | $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param); |
1dc01b9f DM |
586 | die $@ if $@; |
587 | } else { | |
588 | $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param); | |
589 | die $@ if $@; | |
045ae0a7 | 590 | } |
1dc01b9f DM |
591 | return $res; |
592 | } | |
593 | ||
594 | sub parse_name_dir { | |
595 | my $name = shift; | |
596 | ||
35533c68 DM |
597 | if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) { |
598 | return ($1, $3, $2); # (name, format, isBase) | |
1dc01b9f DM |
599 | } |
600 | ||
601 | die "unable to parse volume filename '$name'\n"; | |
602 | } | |
603 | ||
604 | sub parse_volname { | |
605 | my ($class, $volname) = @_; | |
606 | ||
2502b33b DM |
607 | if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) { |
608 | my ($basedvmid, $basename) = ($1, $2); | |
609 | parse_name_dir($basename); | |
610 | my ($vmid, $name) = ($3, $4); | |
35533c68 DM |
611 | my (undef, $format, $isBase) = parse_name_dir($name); |
612 | return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format); | |
2502b33b | 613 | } elsif ($volname =~ m!^(\d+)/(\S+)$!) { |
1dc01b9f | 614 | my ($vmid, $name) = ($1, $2); |
35533c68 DM |
615 | my (undef, $format, $isBase) = parse_name_dir($name); |
616 | return ('images', $name, $vmid, undef, undef, $isBase, $format); | |
cd461a50 | 617 | } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!) { |
1dc01b9f | 618 | return ('iso', $1); |
cd461a50 | 619 | } elsif ($volname =~ m!^vztmpl/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!) { |
1dc01b9f DM |
620 | return ('vztmpl', $1); |
621 | } elsif ($volname =~ m!^rootdir/(\d+)$!) { | |
622 | return ('rootdir', $1, $1); | |
18bf2e59 | 623 | } elsif ($volname =~ m!^backup/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!) { |
1dc01b9f | 624 | my $fn = $1; |
4cb6e060 | 625 | if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) { |
1dc01b9f DM |
626 | return ('backup', $fn, $2); |
627 | } | |
628 | return ('backup', $fn); | |
7c7ae12f DC |
629 | } elsif ($volname =~ m!^snippets/([^/]+)$!) { |
630 | return ('snippets', $1); | |
1dc01b9f DM |
631 | } |
632 | ||
633 | die "unable to parse directory volume name '$volname'\n"; | |
634 | } | |
635 | ||
045ae0a7 | 636 | my $vtype_subdirs = { |
1dc01b9f DM |
637 | images => 'images', |
638 | rootdir => 'private', | |
639 | iso => 'template/iso', | |
640 | vztmpl => 'template/cache', | |
641 | backup => 'dump', | |
7c7ae12f | 642 | snippets => 'snippets', |
1dc01b9f DM |
643 | }; |
644 | ||
c48801b5 AA |
645 | sub get_vtype_subdirs { |
646 | return $vtype_subdirs; | |
647 | } | |
648 | ||
1dc01b9f DM |
649 | sub get_subdir { |
650 | my ($class, $scfg, $vtype) = @_; | |
651 | ||
652 | my $path = $scfg->{path}; | |
653 | ||
ffc31266 | 654 | die "storage definition has no path\n" if !$path; |
3e7bd7da | 655 | die "unknown vtype '$vtype'\n" if !exists($vtype_subdirs->{$vtype}); |
1dc01b9f | 656 | |
b539bb46 | 657 | my $subdir = $scfg->{"content-dirs"}->{$vtype} // "/".$vtype_subdirs->{$vtype}; |
1dc01b9f | 658 | |
3e7bd7da | 659 | return $path.$subdir; |
1dc01b9f DM |
660 | } |
661 | ||
08480ce7 | 662 | sub filesystem_path { |
e67069eb | 663 | my ($class, $scfg, $volname, $snapname) = @_; |
1dc01b9f | 664 | |
e67069eb DM |
665 | my ($vtype, $name, $vmid, undef, undef, $isBase, $format) = |
666 | $class->parse_volname($volname); | |
667 | ||
668 | # Note: qcow2/qed has internal snapshot, so path is always | |
669 | # the same (with or without snapshot => same file). | |
670 | die "can't snapshot this image format\n" | |
671 | if defined($snapname) && $format !~ m/^(qcow2|qed)$/; | |
1dc01b9f DM |
672 | |
673 | my $dir = $class->get_subdir($scfg, $vtype); | |
674 | ||
675 | $dir .= "/$vmid" if $vtype eq 'images'; | |
676 | ||
677 | my $path = "$dir/$name"; | |
678 | ||
679 | return wantarray ? ($path, $vmid, $vtype) : $path; | |
680 | } | |
681 | ||
08480ce7 | 682 | sub path { |
e67069eb | 683 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
08480ce7 | 684 | |
e67069eb | 685 | return $class->filesystem_path($scfg, $volname, $snapname); |
08480ce7 DM |
686 | } |
687 | ||
2502b33b DM |
688 | sub create_base { |
689 | my ($class, $storeid, $scfg, $volname) = @_; | |
690 | ||
691 | # this only works for file based storage types | |
5510f5c9 | 692 | die "storage definition has no path\n" if !$scfg->{path}; |
2502b33b | 693 | |
35533c68 | 694 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = |
2502b33b DM |
695 | $class->parse_volname($volname); |
696 | ||
697 | die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
698 | ||
699 | die "create_base not possible with base image\n" if $isBase; | |
700 | ||
08480ce7 | 701 | my $path = $class->filesystem_path($scfg, $volname); |
2502b33b | 702 | |
35533c68 DM |
703 | my ($size, undef, $used, $parent) = file_size_info($path); |
704 | die "file_size_info on '$volname' failed\n" if !($format && defined($size)); | |
2502b33b DM |
705 | |
706 | die "volname '$volname' contains wrong information about parent\n" | |
707 | if $basename && (!$parent || $parent ne "../$basevmid/$basename"); | |
708 | ||
709 | my $newname = $name; | |
710 | $newname =~ s/^vm-/base-/; | |
711 | ||
712 | my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" : | |
713 | "$vmid/$newname"; | |
714 | ||
08480ce7 | 715 | my $newpath = $class->filesystem_path($scfg, $newvolname); |
2502b33b DM |
716 | |
717 | die "file '$newpath' already exists\n" if -f $newpath; | |
718 | ||
1a3459ac | 719 | rename($path, $newpath) || |
2502b33b DM |
720 | die "rename '$path' to '$newpath' failed - $!\n"; |
721 | ||
c803c396 | 722 | # We try to protect base volume |
2502b33b | 723 | |
c803c396 DM |
724 | chmod(0444, $newpath); # nobody should write anything |
725 | ||
726 | # also try to set immutable flag | |
727 | eval { run_command(['/usr/bin/chattr', '+i', $newpath]); }; | |
728 | warn $@ if $@; | |
1a3459ac | 729 | |
2502b33b DM |
730 | return $newvolname; |
731 | } | |
732 | ||
83a40b8d TL |
733 | my $get_vm_disk_number = sub { |
734 | my ($disk_name, $scfg, $vmid, $suffix) = @_; | |
345f8981 | 735 | |
dd1fa860 TL |
736 | my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/; |
737 | ||
345f8981 | 738 | my $type = $scfg->{type}; |
f4cc2c4a | 739 | my $def = { %{$defaultData->{plugindata}->{$type}} }; |
345f8981 | 740 | |
dd1fa860 TL |
741 | my $valid = $def->{format}[0]; |
742 | if ($valid->{subvol}) { | |
743 | $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/; | |
744 | } | |
345f8981 | 745 | |
83a40b8d TL |
746 | if ($disk_name =~ m/$disk_regex/) { |
747 | return $2; | |
345f8981 | 748 | } |
83a40b8d TL |
749 | |
750 | return undef; | |
751 | }; | |
345f8981 SI |
752 | |
753 | sub get_next_vm_diskname { | |
754 | my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_; | |
755 | ||
345f8981 SI |
756 | $fmt //= ''; |
757 | my $prefix = ($fmt eq 'subvol') ? 'subvol' : 'vm'; | |
758 | my $suffix = $add_fmt_suffix ? ".$fmt" : ''; | |
759 | ||
83a40b8d TL |
760 | my $disk_ids = {}; |
761 | foreach my $disk (@$disk_list) { | |
762 | my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix); | |
763 | $disk_ids->{$disknum} = 1 if defined($disknum); | |
764 | } | |
765 | ||
59fa9fd6 | 766 | for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) { |
345f8981 SI |
767 | if (!$disk_ids->{$i}) { |
768 | return "$prefix-$vmid-disk-$i$suffix"; | |
769 | } | |
770 | } | |
771 | ||
772 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" | |
773 | } | |
774 | ||
a44c0147 FE |
775 | sub find_free_diskname { |
776 | my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_; | |
777 | ||
778 | my $disks = $class->list_images($storeid, $scfg, $vmid); | |
2502b33b | 779 | |
01e872db | 780 | my $disk_list = [ map { $_->{volid} } @$disks ]; |
2502b33b | 781 | |
a44c0147 FE |
782 | return get_next_vm_diskname($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix); |
783 | } | |
2502b33b DM |
784 | |
785 | sub clone_image { | |
f236eaf8 | 786 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
2502b33b DM |
787 | |
788 | # this only works for file based storage types | |
ffc31266 | 789 | die "storage definition has no path\n" if !$scfg->{path}; |
2502b33b | 790 | |
35533c68 | 791 | my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = |
2502b33b DM |
792 | $class->parse_volname($volname); |
793 | ||
794 | die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
795 | ||
f236eaf8 SP |
796 | die "this storage type does not support clone_image on snapshot\n" if $snap; |
797 | ||
35533c68 DM |
798 | die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol'; |
799 | ||
f236eaf8 | 800 | die "clone_image only works on base images\n" if !$isBase; |
1dc01b9f DM |
801 | |
802 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
803 | $imagedir .= "/$vmid"; | |
804 | ||
805 | mkpath $imagedir; | |
806 | ||
d7f6f85e | 807 | my $name = $class->find_free_diskname($storeid, $scfg, $vmid, "qcow2", 1); |
2502b33b DM |
808 | |
809 | warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n"; | |
810 | ||
811 | my $newvol = "$basevmid/$basename/$vmid/$name"; | |
812 | ||
08480ce7 | 813 | my $path = $class->filesystem_path($scfg, $newvol); |
2502b33b | 814 | |
1a3459ac | 815 | # Note: we use relative paths, so we need to call chdir before qemu-img |
2502b33b | 816 | eval { |
7fc619d5 | 817 | local $CWD = $imagedir; |
2502b33b | 818 | |
1a3459ac | 819 | my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename", |
9177cc2e | 820 | '-F', $format, '-f', 'qcow2', $path]; |
1a3459ac | 821 | |
2502b33b DM |
822 | run_command($cmd); |
823 | }; | |
824 | my $err = $@; | |
1dc01b9f | 825 | |
2502b33b DM |
826 | die $err if $err; |
827 | ||
828 | return $newvol; | |
829 | } | |
830 | ||
831 | sub alloc_image { | |
832 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
833 | ||
834 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
835 | $imagedir .= "/$vmid"; | |
836 | ||
837 | mkpath $imagedir; | |
838 | ||
d7f6f85e | 839 | $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name; |
1a3459ac | 840 | |
1dc01b9f DM |
841 | my (undef, $tmpfmt) = parse_name_dir($name); |
842 | ||
045ae0a7 | 843 | die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n" |
1dc01b9f DM |
844 | if $tmpfmt ne $fmt; |
845 | ||
846 | my $path = "$imagedir/$name"; | |
847 | ||
848 | die "disk image '$path' already exists\n" if -e $path; | |
849 | ||
35533c68 DM |
850 | if ($fmt eq 'subvol') { |
851 | # only allow this if size = 0, so that user knows what he is doing | |
852 | die "storage does not support subvol quotas\n" if $size != 0; | |
3918b96a | 853 | |
1f5734bb WB |
854 | my $old_umask = umask(0022); |
855 | my $err; | |
856 | mkdir($path) or $err = "unable to create subvol '$path' - $!\n"; | |
857 | umask $old_umask; | |
858 | die $err if $err; | |
35533c68 DM |
859 | } else { |
860 | my $cmd = ['/usr/bin/qemu-img', 'create']; | |
045ae0a7 | 861 | |
95ff5dbd LS |
862 | my $prealloc_opt = preallocation_cmd_option($scfg, $fmt); |
863 | push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt); | |
3918b96a | 864 | |
35533c68 | 865 | push @$cmd, '-f', $fmt, $path, "${size}K"; |
1dc01b9f | 866 | |
a8ec2f02 CE |
867 | eval { run_command($cmd, errmsg => "unable to create image"); }; |
868 | if ($@) { | |
869 | unlink $path; | |
870 | rmdir $imagedir; | |
871 | die "$@"; | |
872 | } | |
35533c68 | 873 | } |
3918b96a | 874 | |
1dc01b9f DM |
875 | return "$vmid/$name"; |
876 | } | |
877 | ||
878 | sub free_image { | |
35533c68 | 879 | my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_; |
1dc01b9f | 880 | |
56897a92 FE |
881 | die "cannot remove protected volume '$volname' on '$storeid'\n" |
882 | if $class->get_volume_attribute($scfg, $storeid, $volname, 'protected'); | |
883 | ||
08480ce7 | 884 | my $path = $class->filesystem_path($scfg, $volname); |
1dc01b9f | 885 | |
f5451f28 DC |
886 | if ($isBase) { |
887 | # try to remove immutable flag | |
888 | eval { run_command(['/usr/bin/chattr', '-i', $path]); }; | |
889 | warn $@ if $@; | |
890 | } | |
891 | ||
4a7d2222 | 892 | if (defined($format) && ($format eq 'subvol')) { |
35533c68 DM |
893 | File::Path::remove_tree($path); |
894 | } else { | |
de0cd0c2 | 895 | if (!(-f $path || -l $path)) { |
481f6177 | 896 | warn "disk image '$path' does not exist\n"; |
35533c68 DM |
897 | return undef; |
898 | } | |
1dc01b9f | 899 | |
35533c68 DM |
900 | unlink($path) || die "unlink '$path' failed - $!\n"; |
901 | } | |
712e27f1 CE |
902 | |
903 | # try to cleanup directory to not clutter storage with empty $vmid dirs if | |
904 | # all images from a guest got deleted | |
905 | my $dir = dirname($path); | |
906 | rmdir($dir); | |
3918b96a | 907 | |
1dc01b9f DM |
908 | return undef; |
909 | } | |
910 | ||
911 | sub file_size_info { | |
912 | my ($filename, $timeout) = @_; | |
913 | ||
92ae59df | 914 | my $st = File::stat::stat($filename); |
51eee96d | 915 | |
d1f47000 FG |
916 | if (!defined($st)) { |
917 | my $extramsg = -l $filename ? ' - dangling symlink?' : ''; | |
918 | warn "failed to stat '$filename'$extramsg\n"; | |
919 | return undef; | |
920 | } | |
921 | ||
92ae59df AA |
922 | if (S_ISDIR($st->mode)) { |
923 | return wantarray ? (0, 'subvol', 0, undef, $st->ctime) : 1; | |
35533c68 | 924 | } |
3918b96a | 925 | |
af0335e8 | 926 | my $json = ''; |
1dbbd5ab | 927 | eval { |
80699b1d TL |
928 | run_command(['/usr/bin/qemu-img', 'info', '--output=json', $filename], |
929 | timeout => $timeout, | |
930 | outfunc => sub { $json .= shift }, | |
931 | errfunc => sub { warn "$_[0]\n" } | |
932 | ); | |
1dbbd5ab | 933 | }; |
aa4594b1 TM |
934 | warn $@ if $@; |
935 | ||
80699b1d | 936 | my $info = eval { decode_json($json) }; |
339a4eb3 TL |
937 | if (my $err = $@) { |
938 | warn "could not parse qemu-img info command output for '$filename' - $err\n"; | |
939 | return wantarray ? (undef, undef, undef, undef, $st->ctime) : undef; | |
940 | } | |
af0335e8 | 941 | |
80699b1d | 942 | my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)}; |
af0335e8 | 943 | |
d4e00f2b | 944 | ($size) = ($size =~ /^(\d+)$/) or die "size '$size' not an integer\n"; # untaint |
eba7935f ML |
945 | # coerce back from string |
946 | $size = int($size); | |
d4e00f2b | 947 | ($used) = ($used =~ /^(\d+)$/) or die "used '$used' not an integer\n"; # untaint |
eba7935f ML |
948 | # coerce back from string |
949 | $used = int($used); | |
d4e00f2b | 950 | ($format) = ($format =~ /^(\S+)$/) or die "format '$format' includes whitespace\n"; # untaint |
ac598d85 | 951 | if (defined($parent)) { |
d4e00f2b | 952 | ($parent) = ($parent =~ /^(\S+)$/) or die "parent '$parent' includes whitespace\n"; # untaint |
ac598d85 | 953 | } |
92ae59df | 954 | return wantarray ? ($size, $format, $used, $parent, $st->ctime) : $size; |
1dc01b9f DM |
955 | } |
956 | ||
f1de8281 FE |
957 | # FIXME remove on the next APIAGE reset. |
958 | # Deprecated, use get_volume_attribute instead. | |
e9991d26 DC |
959 | sub get_volume_notes { |
960 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
961 | ||
962 | die "volume notes are not supported for $class"; | |
963 | } | |
964 | ||
f1de8281 FE |
965 | # FIXME remove on the next APIAGE reset. |
966 | # Deprecated, use update_volume_attribute instead. | |
e9991d26 DC |
967 | sub update_volume_notes { |
968 | my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_; | |
969 | ||
970 | die "volume notes are not supported for $class"; | |
971 | } | |
972 | ||
f1de8281 FE |
973 | # Returns undef if the attribute is not supported for the volume. |
974 | # Should die if there is an error fetching the attribute. | |
975 | # Possible attributes: | |
976 | # notes - user-provided comments/notes. | |
56897a92 | 977 | # protected - not to be removed by free_image, and for backups, ignored when pruning. |
f1de8281 FE |
978 | sub get_volume_attribute { |
979 | my ($class, $scfg, $storeid, $volname, $attribute) = @_; | |
980 | ||
981 | if ($attribute eq 'notes') { | |
982 | my $notes = eval { $class->get_volume_notes($scfg, $storeid, $volname); }; | |
983 | if (my $err = $@) { | |
984 | return if $err =~ m/^volume notes are not supported/; | |
985 | die $err; | |
986 | } | |
987 | return $notes; | |
988 | } | |
989 | ||
990 | return; | |
991 | } | |
992 | ||
993 | # Dies if the attribute is not supported for the volume. | |
994 | sub update_volume_attribute { | |
995 | my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_; | |
996 | ||
997 | if ($attribute eq 'notes') { | |
998 | $class->update_volume_notes($scfg, $storeid, $volname, $value); | |
999 | } | |
1000 | ||
1001 | die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n"; | |
1002 | } | |
1003 | ||
e47e548e AD |
1004 | sub volume_size_info { |
1005 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
08480ce7 | 1006 | my $path = $class->filesystem_path($scfg, $volname); |
e47e548e AD |
1007 | return file_size_info($path, $timeout); |
1008 | ||
1009 | } | |
1010 | ||
81f5058c AD |
1011 | sub volume_resize { |
1012 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
1013 | ||
6d788031 | 1014 | die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/; |
81f5058c AD |
1015 | |
1016 | return 1 if $running; | |
1017 | ||
08480ce7 | 1018 | my $path = $class->filesystem_path($scfg, $volname); |
81f5058c | 1019 | |
0589e5f9 WL |
1020 | my $format = ($class->parse_volname($volname))[6]; |
1021 | ||
1022 | my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size]; | |
81f5058c | 1023 | |
1059cc99 | 1024 | run_command($cmd, timeout => 10); |
81f5058c AD |
1025 | |
1026 | return undef; | |
1027 | } | |
1028 | ||
7dcb0697 | 1029 | sub volume_snapshot { |
f5640e7d | 1030 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
7dcb0697 | 1031 | |
6d788031 | 1032 | die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/; |
7dcb0697 | 1033 | |
08480ce7 | 1034 | my $path = $class->filesystem_path($scfg, $volname); |
7dcb0697 AD |
1035 | |
1036 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path]; | |
1037 | ||
3f13fd7d | 1038 | run_command($cmd); |
7dcb0697 AD |
1039 | |
1040 | return undef; | |
1041 | } | |
1042 | ||
9a5d5095 FE |
1043 | # Asserts that a rollback to $snap on $volname is possible. |
1044 | # If certain snapshots are preventing the rollback and $blockers is an array | |
1045 | # reference, the snapshot names can be pushed onto $blockers prior to dying. | |
1597f1f9 | 1046 | sub volume_rollback_is_possible { |
9a5d5095 | 1047 | my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_; |
1597f1f9 | 1048 | |
3918b96a | 1049 | return 1; |
1597f1f9 WL |
1050 | } |
1051 | ||
41dffa85 AD |
1052 | sub volume_snapshot_rollback { |
1053 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
1054 | ||
6d788031 | 1055 | die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/; |
41dffa85 | 1056 | |
08480ce7 | 1057 | my $path = $class->filesystem_path($scfg, $volname); |
41dffa85 AD |
1058 | |
1059 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path]; | |
1060 | ||
3f13fd7d | 1061 | run_command($cmd); |
41dffa85 AD |
1062 | |
1063 | return undef; | |
1064 | } | |
1065 | ||
6000a061 AD |
1066 | sub volume_snapshot_delete { |
1067 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
1068 | ||
6d788031 | 1069 | die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/; |
6000a061 AD |
1070 | |
1071 | return 1 if $running; | |
1072 | ||
08480ce7 | 1073 | my $path = $class->filesystem_path($scfg, $volname); |
6000a061 | 1074 | |
399581a2 WB |
1075 | $class->deactivate_volume($storeid, $scfg, $volname, $snap, {}); |
1076 | ||
6000a061 AD |
1077 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path]; |
1078 | ||
3f13fd7d | 1079 | run_command($cmd); |
6000a061 AD |
1080 | |
1081 | return undef; | |
1082 | } | |
1083 | ||
2c036838 SI |
1084 | sub volume_snapshot_needs_fsfreeze { |
1085 | ||
1086 | return 0; | |
1087 | } | |
7118dd91 DM |
1088 | sub storage_can_replicate { |
1089 | my ($class, $scfg, $storeid, $format) = @_; | |
1090 | ||
1091 | return 0; | |
1092 | } | |
1093 | ||
f884fe11 | 1094 | sub volume_has_feature { |
e6f4eed4 | 1095 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_; |
f884fe11 AD |
1096 | |
1097 | my $features = { | |
34fd49a6 TL |
1098 | snapshot => { |
1099 | current => { qcow2 => 1 }, | |
1100 | snap => { qcow2 => 1 }, | |
1101 | }, | |
1102 | clone => { | |
1103 | base => { qcow2 => 1, raw => 1, vmdk => 1 }, | |
1104 | }, | |
1105 | template => { | |
1106 | current => { qcow2 => 1, raw => 1, vmdk => 1, subvol => 1 }, | |
1107 | }, | |
1108 | copy => { | |
1109 | base => { qcow2 => 1, raw => 1, vmdk => 1 }, | |
1110 | current => { qcow2 => 1, raw => 1, vmdk => 1 }, | |
1111 | snap => { qcow2 => 1 }, | |
1112 | }, | |
1113 | sparseinit => { | |
1114 | base => { qcow2 => 1, raw => 1, vmdk => 1 }, | |
1115 | current => { qcow2 => 1, raw => 1, vmdk => 1 }, | |
1116 | }, | |
1117 | rename => { | |
1118 | current => {qcow2 => 1, raw => 1, vmdk => 1}, | |
1119 | }, | |
f884fe11 AD |
1120 | }; |
1121 | ||
34fd49a6 TL |
1122 | if ($feature eq 'clone') { |
1123 | if ( | |
1124 | defined($opts->{valid_target_formats}) | |
1125 | && !(grep { $_ eq 'qcow2' } @{$opts->{valid_target_formats}}) | |
1126 | ) { | |
1127 | return 0; # clone_image creates a qcow2 volume | |
1128 | } | |
1129 | } elsif ($feature eq 'rename') { | |
1130 | return 0 if $class->can('api') && $class->api() < 10; | |
1131 | } | |
e6f4eed4 | 1132 | |
95dfa44c | 1133 | |
34fd49a6 | 1134 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname); |
dc4f2cb3 | 1135 | |
dc4f2cb3 AD |
1136 | my $key = undef; |
1137 | if($snapname){ | |
2c5a7097 | 1138 | $key = 'snap'; |
dc4f2cb3 AD |
1139 | }else{ |
1140 | $key = $isBase ? 'base' : 'current'; | |
f884fe11 | 1141 | } |
dc4f2cb3 AD |
1142 | |
1143 | return 1 if defined($features->{$feature}->{$key}->{$format}); | |
1144 | ||
f884fe11 AD |
1145 | return undef; |
1146 | } | |
1147 | ||
1dc01b9f DM |
1148 | sub list_images { |
1149 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
1150 | ||
1151 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
1152 | ||
1153 | my ($defFmt, $vaidFmts) = default_format($scfg); | |
045ae0a7 | 1154 | my $fmts = join ('|', @$vaidFmts); |
1dc01b9f DM |
1155 | |
1156 | my $res = []; | |
1157 | ||
1158 | foreach my $fn (<$imagedir/[0-9][0-9]*/*>) { | |
1159 | ||
1160 | next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!; | |
1161 | $fn = $1; # untaint | |
1162 | ||
1163 | my $owner = $2; | |
1164 | my $name = $3; | |
1dc01b9f | 1165 | |
2502b33b | 1166 | next if !$vollist && defined($vmid) && ($owner ne $vmid); |
1dc01b9f | 1167 | |
51eee96d | 1168 | my ($size, $format, $used, $parent, $ctime) = file_size_info($fn); |
35533c68 | 1169 | next if !($format && defined($size)); |
2502b33b DM |
1170 | |
1171 | my $volid; | |
1172 | if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) { | |
1173 | my ($basevmid, $basename) = ($1, $2); | |
1174 | $volid = "$storeid:$basevmid/$basename/$owner/$name"; | |
1175 | } else { | |
1176 | $volid = "$storeid:$owner/$name"; | |
1177 | } | |
1dc01b9f | 1178 | |
2502b33b DM |
1179 | if ($vollist) { |
1180 | my $found = grep { $_ eq $volid } @$vollist; | |
1181 | next if !$found; | |
1dc01b9f DM |
1182 | } |
1183 | ||
51eee96d | 1184 | my $info = { |
2502b33b | 1185 | volid => $volid, format => $format, |
1a3459ac | 1186 | size => $size, vmid => $owner, used => $used, parent => $parent |
2502b33b | 1187 | }; |
51eee96d DM |
1188 | |
1189 | $info->{ctime} = $ctime if $ctime; | |
1190 | ||
1191 | push @$res, $info; | |
1dc01b9f DM |
1192 | } |
1193 | ||
1194 | return $res; | |
1195 | } | |
1196 | ||
c2fc9fbe DM |
1197 | # list templates ($tt = <iso|vztmpl|backup|snippets>) |
1198 | my $get_subdir_files = sub { | |
1199 | my ($sid, $path, $tt, $vmid) = @_; | |
1200 | ||
1201 | my $res = []; | |
1202 | ||
1203 | foreach my $fn (<$path/*>) { | |
92ae59df AA |
1204 | my $st = File::stat::stat($fn); |
1205 | ||
3d10acf8 | 1206 | next if (!$st || S_ISDIR($st->mode)); |
c2fc9fbe DM |
1207 | |
1208 | my $info; | |
1209 | ||
1210 | if ($tt eq 'iso') { | |
cd461a50 | 1211 | next if $fn !~ m!/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!i; |
c2fc9fbe DM |
1212 | |
1213 | $info = { volid => "$sid:iso/$1", format => 'iso' }; | |
1214 | ||
1215 | } elsif ($tt eq 'vztmpl') { | |
cd461a50 | 1216 | next if $fn !~ m!/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!; |
c2fc9fbe DM |
1217 | |
1218 | $info = { volid => "$sid:vztmpl/$1", format => "t$2" }; | |
1219 | ||
1220 | } elsif ($tt eq 'backup') { | |
18bf2e59 | 1221 | next if $fn !~ m!/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!; |
d260b9f3 | 1222 | my $original = $fn; |
1b396425 | 1223 | my $format = $2; |
40c795e7 | 1224 | $fn = $1; |
edb92f70 | 1225 | |
4b702ac3 | 1226 | # only match for VMID now, to avoid false positives (VMID in parent directory name) |
edb92f70 FE |
1227 | next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/; |
1228 | ||
40c795e7 | 1229 | $info = { volid => "$sid:backup/$fn", format => $format }; |
1b396425 | 1230 | |
e9667b34 | 1231 | my $archive_info = eval { PVE::Storage::archive_info($fn) } // {}; |
fb821c18 | 1232 | |
e9667b34 | 1233 | $info->{ctime} = $archive_info->{ctime} if defined($archive_info->{ctime}); |
c66e0b8a | 1234 | $info->{subtype} = $archive_info->{type} // 'unknown'; |
9c629b3e | 1235 | |
93afc379 | 1236 | if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) { |
1b396425 DC |
1237 | $info->{vmid} = $vmid // $1; |
1238 | } | |
1239 | ||
6fef456c DC |
1240 | my $notes_fn = $original.NOTES_EXT; |
1241 | if (-f $notes_fn) { | |
1242 | my $notes = PVE::Tools::file_read_firstline($notes_fn); | |
43f8112f | 1243 | $info->{notes} = eval { decode('UTF-8', $notes, 1) } // $notes if defined($notes); |
d260b9f3 DC |
1244 | } |
1245 | ||
56897a92 | 1246 | $info->{protected} = 1 if -e PVE::Storage::protection_file_path($original); |
c2fc9fbe DM |
1247 | } elsif ($tt eq 'snippets') { |
1248 | ||
1249 | $info = { | |
1250 | volid => "$sid:snippets/". basename($fn), | |
1251 | format => 'snippet', | |
1252 | }; | |
1253 | } | |
1254 | ||
92ae59df AA |
1255 | $info->{size} = $st->size; |
1256 | $info->{ctime} //= $st->ctime; | |
c2fc9fbe DM |
1257 | |
1258 | push @$res, $info; | |
1259 | } | |
1260 | ||
1261 | return $res; | |
1262 | }; | |
1263 | ||
f1de8281 FE |
1264 | # If attributes are set on a volume, they should be included in the result. |
1265 | # See get_volume_attribute for a list of possible attributes. | |
c2fc9fbe DM |
1266 | sub list_volumes { |
1267 | my ($class, $storeid, $scfg, $vmid, $content_types) = @_; | |
1268 | ||
1269 | my $res = []; | |
5dae1a31 | 1270 | my $vmlist = PVE::Cluster::get_vmlist(); |
a4e41b0b | 1271 | foreach my $type (@$content_types) { |
c2fc9fbe DM |
1272 | my $data; |
1273 | ||
a4e41b0b | 1274 | if ($type eq 'images' || $type eq 'rootdir') { |
c2fc9fbe | 1275 | $data = $class->list_images($storeid, $scfg, $vmid); |
a14e0a5e | 1276 | } elsif ($scfg->{path}) { |
a4e41b0b | 1277 | my $path = $class->get_subdir($scfg, $type); |
a14e0a5e | 1278 | |
a4e41b0b | 1279 | if ($type eq 'iso' && !defined($vmid)) { |
a14e0a5e | 1280 | $data = $get_subdir_files->($storeid, $path, 'iso'); |
a4e41b0b | 1281 | } elsif ($type eq 'vztmpl'&& !defined($vmid)) { |
a14e0a5e | 1282 | $data = $get_subdir_files->($storeid, $path, 'vztmpl'); |
a4e41b0b | 1283 | } elsif ($type eq 'backup') { |
a14e0a5e | 1284 | $data = $get_subdir_files->($storeid, $path, 'backup', $vmid); |
a4e41b0b | 1285 | } elsif ($type eq 'snippets') { |
a14e0a5e FG |
1286 | $data = $get_subdir_files->($storeid, $path, 'snippets'); |
1287 | } | |
c2fc9fbe DM |
1288 | } |
1289 | ||
1290 | next if !$data; | |
1291 | ||
1292 | foreach my $item (@$data) { | |
a4e41b0b | 1293 | if ($type eq 'images' || $type eq 'rootdir') { |
a420842d FG |
1294 | my $vminfo = $vmlist->{ids}->{$item->{vmid}}; |
1295 | my $vmtype; | |
1296 | if (defined($vminfo)) { | |
1297 | $vmtype = $vminfo->{type}; | |
1298 | } | |
5dae1a31 TM |
1299 | if (defined($vmtype) && $vmtype eq 'lxc') { |
1300 | $item->{content} = 'rootdir'; | |
1301 | } else { | |
1302 | $item->{content} = 'images'; | |
1303 | } | |
0877df04 | 1304 | next if $type ne $item->{content}; |
5dae1a31 | 1305 | } else { |
a4e41b0b | 1306 | $item->{content} = $type; |
5dae1a31 TM |
1307 | } |
1308 | ||
c2fc9fbe DM |
1309 | push @$res, $item; |
1310 | } | |
1311 | } | |
1312 | ||
1313 | return $res; | |
1314 | } | |
1315 | ||
1dc01b9f DM |
1316 | sub status { |
1317 | my ($class, $storeid, $scfg, $cache) = @_; | |
1318 | ||
1319 | my $path = $scfg->{path}; | |
1320 | ||
ffc31266 | 1321 | die "storage definition has no path\n" if !$path; |
045ae0a7 | 1322 | |
1dc01b9f DM |
1323 | my $timeout = 2; |
1324 | my $res = PVE::Tools::df($path, $timeout); | |
1325 | ||
1326 | return undef if !$res || !$res->{total}; | |
1327 | ||
1328 | return ($res->{total}, $res->{avail}, $res->{used}, 1); | |
1329 | } | |
1330 | ||
8c20d8af FE |
1331 | # Returns a hash with the snapshot names as keys and the following data: |
1332 | # id - Unique id to distinguish different snapshots even if the have the same name. | |
1333 | # timestamp - Creation time of the snapshot (seconds since epoch). | |
1334 | # Returns an empty hash if the volume does not exist. | |
1335 | sub volume_snapshot_info { | |
1336 | my ($class, $scfg, $storeid, $volname) = @_; | |
1337 | ||
1338 | die "volume_snapshot_info is not implemented for $class"; | |
1339 | } | |
1340 | ||
1dc01b9f DM |
1341 | sub activate_storage { |
1342 | my ($class, $storeid, $scfg, $cache) = @_; | |
1343 | ||
1344 | my $path = $scfg->{path}; | |
1345 | ||
ffc31266 | 1346 | die "storage definition has no path\n" if !$path; |
1dc01b9f | 1347 | |
e53050ed EK |
1348 | # this path test may hang indefinitely on unresponsive mounts |
1349 | my $timeout = 2; | |
1350 | if (! PVE::Tools::run_fork_with_timeout($timeout, sub {-d $path})) { | |
1351 | die "unable to activate storage '$storeid' - " . | |
1352 | "directory '$path' does not exist or is unreachable\n"; | |
1353 | } | |
1354 | ||
1dc01b9f | 1355 | |
c7616abc WB |
1356 | return if defined($scfg->{mkdir}) && !$scfg->{mkdir}; |
1357 | ||
1dc01b9f DM |
1358 | if (defined($scfg->{content})) { |
1359 | foreach my $vtype (keys %$vtype_subdirs) { | |
6bcc16d7 DM |
1360 | # OpenVZMigrate uses backup (dump) dir |
1361 | if (defined($scfg->{content}->{$vtype}) || | |
1362 | ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) { | |
1363 | my $subdir = $class->get_subdir($scfg, $vtype); | |
1364 | mkpath $subdir if $subdir ne $path; | |
1365 | } | |
1dc01b9f DM |
1366 | } |
1367 | } | |
1368 | } | |
1369 | ||
1370 | sub deactivate_storage { | |
1371 | my ($class, $storeid, $scfg, $cache) = @_; | |
1372 | ||
1373 | # do nothing by default | |
1374 | } | |
1375 | ||
40d69893 DM |
1376 | sub map_volume { |
1377 | my ($class, $storeid, $scfg, $volname, $snapname) = @_; | |
1378 | ||
d560ec28 ML |
1379 | my ($path) = $class->path($scfg, $volname, $storeid, $snapname); |
1380 | return $path; | |
40d69893 DM |
1381 | } |
1382 | ||
1383 | sub unmap_volume { | |
1384 | my ($class, $storeid, $scfg, $volname, $snapname) = @_; | |
1385 | ||
1386 | return 1; | |
1387 | } | |
1388 | ||
1dc01b9f | 1389 | sub activate_volume { |
02e797b8 | 1390 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
1dc01b9f | 1391 | |
02e797b8 | 1392 | my $path = $class->filesystem_path($scfg, $volname, $snapname); |
1dc01b9f DM |
1393 | |
1394 | # check is volume exists | |
1395 | if ($scfg->{path}) { | |
1396 | die "volume '$storeid:$volname' does not exist\n" if ! -e $path; | |
1397 | } else { | |
1398 | die "volume '$storeid:$volname' does not exist\n" if ! -b $path; | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | sub deactivate_volume { | |
02e797b8 | 1403 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
1dc01b9f DM |
1404 | |
1405 | # do nothing by default | |
1406 | } | |
1407 | ||
c9eeac01 AD |
1408 | sub check_connection { |
1409 | my ($class, $storeid, $scfg) = @_; | |
1410 | # do nothing by default | |
1411 | return 1; | |
1412 | } | |
1413 | ||
8f26b391 FE |
1414 | sub prune_backups { |
1415 | my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_; | |
1416 | ||
1417 | $logfunc //= sub { print "$_[1]\n" }; | |
1418 | ||
1419 | my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']); | |
1420 | ||
1421 | my $backup_groups = {}; | |
1422 | my $prune_list = []; | |
1423 | ||
1424 | foreach my $backup (@{$backups}) { | |
1425 | my $volid = $backup->{volid}; | |
8f26b391 FE |
1426 | my $archive_info = eval { PVE::Storage::archive_info($volid) } // {}; |
1427 | my $backup_type = $archive_info->{type} // 'unknown'; | |
68ce0b31 | 1428 | my $backup_vmid = $archive_info->{vmid} // $backup->{vmid}; |
8f26b391 FE |
1429 | |
1430 | next if defined($type) && $type ne $backup_type; | |
1431 | ||
1432 | my $prune_entry = { | |
1433 | ctime => $backup->{ctime}, | |
1434 | type => $backup_type, | |
1435 | volid => $volid, | |
1436 | }; | |
1437 | ||
1438 | $prune_entry->{vmid} = $backup_vmid if defined($backup_vmid); | |
1439 | ||
1440 | if ($archive_info->{is_std_name}) { | |
cba2b7c1 TL |
1441 | die "internal error - got no VMID\n" if !defined($backup_vmid); |
1442 | die "internal error - got wrong VMID '$backup_vmid' != '$vmid'\n" | |
1443 | if defined($vmid) && $backup_vmid ne $vmid; | |
68ce0b31 | 1444 | |
8f26b391 FE |
1445 | $prune_entry->{ctime} = $archive_info->{ctime}; |
1446 | my $group = "$backup_type/$backup_vmid"; | |
1447 | push @{$backup_groups->{$group}}, $prune_entry; | |
1448 | } else { | |
1449 | # ignore backups that don't use the standard naming scheme | |
ecfe2505 | 1450 | $prune_entry->{mark} = 'renamed'; |
8f26b391 FE |
1451 | } |
1452 | ||
56897a92 FE |
1453 | $prune_entry->{mark} = 'protected' if $backup->{protected}; |
1454 | ||
8f26b391 FE |
1455 | push @{$prune_list}, $prune_entry; |
1456 | } | |
1457 | ||
1458 | foreach my $backup_group (values %{$backup_groups}) { | |
1459 | PVE::Storage::prune_mark_backup_group($backup_group, $keep); | |
1460 | } | |
1461 | ||
1462 | my $failed; | |
1463 | if (!$dryrun) { | |
1464 | foreach my $prune_entry (@{$prune_list}) { | |
1465 | next if $prune_entry->{mark} ne 'remove'; | |
1466 | ||
1467 | my $volid = $prune_entry->{volid}; | |
1468 | $logfunc->('info', "removing backup '$volid'"); | |
1469 | eval { | |
1470 | my (undef, $volname) = parse_volume_id($volid); | |
1471 | my $archive_path = $class->filesystem_path($scfg, $volname); | |
1472 | PVE::Storage::archive_remove($archive_path); | |
1473 | }; | |
1474 | if (my $err = $@) { | |
1475 | $logfunc->('err', "error when removing backup '$volid' - $err\n"); | |
1476 | $failed = 1; | |
1477 | } | |
1478 | } | |
1479 | } | |
1480 | die "error pruning backups - check log\n" if $failed; | |
1481 | ||
1482 | return $prune_list; | |
1483 | } | |
1484 | ||
e1f6cb39 DM |
1485 | # Import/Export interface: |
1486 | # Any path based storage is assumed to support 'raw' and 'tar' streams, so | |
1487 | # the default implementations will return this if $scfg->{path} is set, | |
1488 | # mimicking the old PVE::Storage::storage_migrate() function. | |
1489 | # | |
1490 | # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}... | |
1491 | # functions in case the format doesn't match their specialized | |
1492 | # implementations to reuse the raw/tar code. | |
1493 | # | |
1494 | # Format specification: | |
1495 | # The following formats are all prefixed with image information in the form | |
1496 | # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able | |
1497 | # to preallocate the image on storages which require it. | |
1498 | # | |
1499 | # raw+size: (image files only) | |
1500 | # A raw binary data stream such as produced via `dd if=TheImageFile`. | |
1501 | # qcow2+size, vmdk: (image files only) | |
1502 | # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for | |
1503 | # files which are already in qcow2 format, or via `qemu-img convert`. | |
1504 | # Note that these formats are only valid with $with_snapshots being true. | |
1505 | # tar+size: (subvolumes only) | |
6b3a4a25 WB |
1506 | # A GNU tar stream containing just the inner contents of the subvolume. |
1507 | # This does not distinguish between the contents of a privileged or | |
1508 | # unprivileged container. In other words, this is from the root user | |
1509 | # namespace's point of view with no uid-mapping in effect. | |
1510 | # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .` | |
e1f6cb39 DM |
1511 | |
1512 | # Plugins may reuse these helpers. Changes to the header format should be | |
1513 | # reflected by changes to the function prototypes. | |
1514 | sub write_common_header($$) { | |
1515 | my ($fh, $image_size_in_bytes) = @_; | |
1516 | syswrite($fh, pack("Q<", $image_size_in_bytes), 8); | |
1517 | } | |
1518 | ||
1519 | sub read_common_header($) { | |
1520 | my ($fh) = @_; | |
1521 | sysread($fh, my $size, 8); | |
1522 | $size = unpack('Q<', $size); | |
f105c176 | 1523 | die "import: no size found in export header, aborting.\n" if !defined($size); |
e1f6cb39 DM |
1524 | # Size is in bytes! |
1525 | return $size; | |
1526 | } | |
1527 | ||
47f37b53 WB |
1528 | # Export a volume into a file handle as a stream of desired format. |
1529 | sub volume_export { | |
1530 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
e1f6cb39 DM |
1531 | if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) { |
1532 | my $file = $class->path($scfg, $volname, $storeid) | |
1533 | or goto unsupported; | |
1534 | my ($size, $file_format) = file_size_info($file); | |
1535 | ||
1536 | if ($format eq 'raw+size') { | |
1537 | goto unsupported if $with_snapshots || $file_format eq 'subvol'; | |
1538 | write_common_header($fh, $size); | |
1539 | if ($file_format eq 'raw') { | |
1540 | run_command(['dd', "if=$file", "bs=4k"], output => '>&'.fileno($fh)); | |
1541 | } else { | |
1542 | run_command(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'], | |
1543 | output => '>&'.fileno($fh)); | |
1544 | } | |
1545 | return; | |
1546 | } elsif ($format =~ /^(qcow2|vmdk)\+size$/) { | |
1547 | my $data_format = $1; | |
1548 | goto unsupported if !$with_snapshots || $file_format ne $data_format; | |
1549 | write_common_header($fh, $size); | |
1550 | run_command(['dd', "if=$file", "bs=4k"], output => '>&'.fileno($fh)); | |
1551 | return; | |
1552 | } elsif ($format eq 'tar+size') { | |
1553 | goto unsupported if $file_format ne 'subvol'; | |
1554 | write_common_header($fh, $size); | |
6b3a4a25 | 1555 | run_command(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'], |
e1f6cb39 DM |
1556 | output => '>&'.fileno($fh)); |
1557 | return; | |
1558 | } | |
1559 | } | |
1560 | unsupported: | |
1561 | die "volume export format $format not available for $class"; | |
47f37b53 WB |
1562 | } |
1563 | ||
d390328b WB |
1564 | sub volume_export_formats { |
1565 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
e1f6cb39 DM |
1566 | if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) { |
1567 | my $file = $class->path($scfg, $volname, $storeid) | |
1568 | or return; | |
1569 | my ($size, $format) = file_size_info($file); | |
1570 | ||
1571 | if ($with_snapshots) { | |
1572 | return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk'); | |
1573 | return (); | |
1574 | } | |
1575 | return ('tar+size') if $format eq 'subvol'; | |
1576 | return ('raw+size'); | |
1577 | } | |
1578 | return (); | |
d390328b WB |
1579 | } |
1580 | ||
47f37b53 WB |
1581 | # Import data from a stream, creating a new or replacing or adding to an existing volume. |
1582 | sub volume_import { | |
3cc29a04 | 1583 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_; |
e1f6cb39 DM |
1584 | |
1585 | die "volume import format '$format' not available for $class\n" | |
1586 | if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/; | |
1587 | my $data_format = $1; | |
1588 | ||
1589 | die "format $format cannot be imported without snapshots\n" | |
1590 | if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk'); | |
1591 | die "format $format cannot be imported with snapshots\n" | |
1592 | if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar'); | |
1593 | ||
1594 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) = | |
1595 | $class->parse_volname($volname); | |
1596 | ||
1597 | # XXX: Should we bother with conversion routines at this level? This won't | |
1598 | # happen without manual CLI usage, so for now we just error out... | |
1599 | die "cannot import format $format into a file of format $file_format\n" | |
1600 | if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol'); | |
1601 | ||
1602 | # Check for an existing file first since interrupting alloc_image doesn't | |
1603 | # free it. | |
1604 | my $file = $class->path($scfg, $volname, $storeid); | |
a97d3ee4 FE |
1605 | if (-e $file) { |
1606 | die "file '$file' already exists\n" if !$allow_rename; | |
1607 | warn "file '$file' already exists - importing with a different name\n"; | |
1608 | $name = undef; | |
1609 | } | |
e1f6cb39 DM |
1610 | |
1611 | my ($size) = read_common_header($fh); | |
1612 | $size = int($size/1024); | |
1613 | ||
1614 | eval { | |
1615 | my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size); | |
a97d3ee4 FE |
1616 | my $oldname = $volname; |
1617 | $volname = $allocname; | |
1618 | if (defined($name) && $allocname ne $oldname) { | |
e1f6cb39 DM |
1619 | die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n"; |
1620 | } | |
1621 | my $file = $class->path($scfg, $volname, $storeid) | |
1622 | or die "internal error: failed to get path to newly allocated volume $volname\n"; | |
1623 | if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') { | |
1624 | run_command(['dd', "of=$file", 'conv=sparse', 'bs=64k'], | |
1625 | input => '<&'.fileno($fh)); | |
1626 | } elsif ($data_format eq 'tar') { | |
6b3a4a25 | 1627 | run_command(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'], |
e1f6cb39 DM |
1628 | input => '<&'.fileno($fh)); |
1629 | } else { | |
1630 | die "volume import format '$format' not available for $class"; | |
1631 | } | |
1632 | }; | |
1633 | if (my $err = $@) { | |
1634 | eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) }; | |
1635 | warn $@ if $@; | |
1636 | die $err; | |
1637 | } | |
a97d3ee4 FE |
1638 | |
1639 | return "$storeid:$volname"; | |
47f37b53 | 1640 | } |
e47e548e | 1641 | |
d390328b | 1642 | sub volume_import_formats { |
3cc29a04 | 1643 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; |
e1f6cb39 DM |
1644 | if ($scfg->{path} && !defined($base_snapshot)) { |
1645 | my $format = ($class->parse_volname($volname))[6]; | |
1646 | if ($with_snapshots) { | |
1647 | return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk'); | |
1648 | return (); | |
1649 | } | |
1650 | return ('tar+size') if $format eq 'subvol'; | |
1651 | return ('raw+size'); | |
1652 | } | |
1653 | return (); | |
d390328b WB |
1654 | } |
1655 | ||
95dfa44c AL |
1656 | sub rename_volume { |
1657 | my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_; | |
1658 | die "not implemented in storage plugin '$class'\n" if $class->can('api') && $class->api() < 10; | |
1659 | die "no path found\n" if !$scfg->{path}; | |
1660 | ||
1661 | my ( | |
1662 | undef, | |
1663 | $source_image, | |
1664 | $source_vmid, | |
1665 | $base_name, | |
1666 | $base_vmid, | |
1667 | undef, | |
1668 | $format | |
1669 | ) = $class->parse_volname($source_volname); | |
1670 | ||
1671 | $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 1) | |
1672 | if !$target_volname; | |
1673 | ||
1674 | my $basedir = $class->get_subdir($scfg, 'images'); | |
1675 | ||
1676 | mkpath "${basedir}/${target_vmid}"; | |
1677 | ||
1678 | my $old_path = "${basedir}/${source_vmid}/${source_image}"; | |
1679 | my $new_path = "${basedir}/${target_vmid}/${target_volname}"; | |
1680 | ||
1681 | die "target volume '${target_volname}' already exists\n" if -e $new_path; | |
1682 | ||
1683 | my $base = $base_name ? "${base_vmid}/${base_name}/" : ''; | |
1684 | ||
1685 | rename($old_path, $new_path) || | |
1686 | die "rename '$old_path' to '$new_path' failed - $!\n"; | |
1687 | ||
1688 | return "${storeid}:${base}${target_vmid}/${target_volname}"; | |
1689 | } | |
1690 | ||
1dc01b9f | 1691 | 1; |