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