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