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