]> git.proxmox.com Git - pve-storage.git/blame_incremental - PVE/Storage/Plugin.pm
pbs: use vmid parameter in list_snapshots
[pve-storage.git] / PVE / Storage / Plugin.pm
... / ...
CommitLineData
1package PVE::Storage::Plugin;
2
3use strict;
4use warnings;
5
6use Encode qw(decode);
7use Fcntl ':mode';
8use File::chdir;
9use File::Path;
10use File::Basename;
11use File::stat qw();
12
13use PVE::Tools qw(run_command);
14use PVE::JSONSchema qw(get_standard_option register_standard_option);
15use PVE::Cluster qw(cfs_register_file);
16
17use JSON;
18
19use base qw(PVE::SectionConfig);
20
21use constant COMPRESSOR_RE => 'gz|lzo|zst';
22
23use constant LOG_EXT => ".log";
24use constant NOTES_EXT => ".notes";
25
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
33our @SHARED_STORAGE = (
34 'iscsi',
35 'nfs',
36 'cifs',
37 'rbd',
38 'cephfs',
39 'iscsidirect',
40 'glusterfs',
41 'zfs',
42 'drbd',
43 'pbs',
44);
45
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
59our $MAX_VOLUMES_PER_GUEST = 1024;
60
61cfs_register_file ('storage.cfg',
62 sub { __PACKAGE__->parse_config(@_); },
63 sub { __PACKAGE__->write_config(@_); });
64
65my %prune_option = (
66 optional => 1,
67 type => 'integer', minimum => '0',
68 format_description => 'N',
69);
70
71our $prune_backups_format = {
72 'keep-all' => {
73 type => 'boolean',
74 description => 'Keep all backups. Conflicts with the other options when true.',
75 optional => 1,
76 },
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 },
106};
107PVE::JSONSchema::register_format('prune-backups', $prune_backups_format, \&validate_prune_backups);
108sub validate_prune_backups {
109 my ($prune_backups) = @_;
110
111 my $res = { $prune_backups->%* };
112
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) {
118 die "keep-all cannot be set together with other options.\n";
119 }
120
121 return $res;
122}
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});
133
134my $defaultData = {
135 propertyList => {
136 type => { description => "Storage type." },
137 storage => get_standard_option('pve-storage-id',
138 { completion => \&PVE::Storage::complete_storage }),
139 nodes => get_standard_option('pve-node-list', { optional => 1 }),
140 content => {
141 description => "Allowed content types.\n\nNOTE: the value " .
142 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
143 type => 'string', format => 'pve-storage-content-list',
144 optional => 1,
145 completion => \&PVE::Storage::complete_content_type,
146 },
147 disable => {
148 description => "Flag to disable the storage.",
149 type => 'boolean',
150 optional => 1,
151 },
152 maxfiles => {
153 description => "Deprecated: use 'prune-backups' instead. " .
154 "Maximal number of backup files per VM. Use '0' for unlimited.",
155 type => 'integer',
156 minimum => 0,
157 optional => 1,
158 },
159 'prune-backups' => get_standard_option('prune-backups'),
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 },
167 shared => {
168 description => "Mark storage as shared.",
169 type => 'boolean',
170 optional => 1,
171 },
172 'format' => {
173 description => "Default image format.",
174 type => 'string', format => 'pve-storage-format',
175 optional => 1,
176 },
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 },
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};
193 }
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};
213
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 }
221
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
242 if (!(PVE::JSONSchema::pve_verify_ip($server, 1) ||
243 PVE::JSONSchema::pve_verify_dns_name($server, 1)))
244 {
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
251PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name);
252sub parse_lvm_name {
253 my ($name, $noerr) = @_;
254
255 if ($name !~ m/^[a-z0-9][a-z0-9\-\_\.]*[a-z0-9]$/i) {
256 return undef if $noerr;
257 die "lvm name '$name' contains illegal characters\n";
258 }
259
260 return $name;
261}
262
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
281 if (!PVE::Tools::parse_host_and_port($portal)) {
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
293
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
306 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
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
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
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];
364
365 my $res = {};
366
367 foreach my $c (PVE::Tools::split_list($value)) {
368 if (!$valid_content->{$c}) {
369 warn "storage does not support content type '$c'\n";
370 next;
371 }
372 $res->{$c} = 1;
373 }
374
375 if ($res->{none} && scalar (keys %$res) > 1) {
376 die "unable to combine 'none' with other content types\n";
377 }
378
379 if (scalar(keys $res->%*) == 0 && !$valid_content->{none}) {
380 die "storage does not support content type 'none'\n";
381 }
382
383 return $res;
384 } elsif ($key eq 'format') {
385 my $valid_formats = $def->{format}->[0];
386
387 if (!$valid_formats->{$value}) {
388 warn "storage does not support format '$value'\n";
389 next;
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
434 # we want 'local' to be always the same 'type' (on all cluster nodes)
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',
441 'prune-backups' => 'keep-all=1',
442 content => {
443 backup => 1,
444 images => 1,
445 iso => 1,
446 rootdir => 1,
447 snippets => 1,
448 vztmpl => 1,
449 },
450 };
451 }
452
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 }
468 if (grep { $_ eq $type } @SHARED_STORAGE) {
469 $d->{shared} = 1;
470 }
471 }
472
473 return $cfg;
474}
475
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
501# Storage implementation
502
503# called during addition of storage (before the new storage config got written)
504# die to abort addition if there are (grave) problems
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
510 return undef;
511}
512
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
520 return undef;
521}
522
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
532 return undef;
533}
534
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;
543 $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
544 die $@ if $@;
545 } else {
546 $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
547 die $@ if $@;
548 }
549 return $res;
550}
551
552sub parse_name_dir {
553 my $name = shift;
554
555 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
556 return ($1, $3, $2); # (name, format, isBase)
557 }
558
559 die "unable to parse volume filename '$name'\n";
560}
561
562sub parse_volname {
563 my ($class, $volname) = @_;
564
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);
569 my (undef, $format, $isBase) = parse_name_dir($name);
570 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
571 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
572 my ($vmid, $name) = ($1, $2);
573 my (undef, $format, $isBase) = parse_name_dir($name);
574 return ('images', $name, $vmid, undef, undef, $isBase, $format);
575 } elsif ($volname =~ m!^iso/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!) {
576 return ('iso', $1);
577 } elsif ($volname =~ m!^vztmpl/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!) {
578 return ('vztmpl', $1);
579 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
580 return ('rootdir', $1, $1);
581 } elsif ($volname =~ m!^backup/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!) {
582 my $fn = $1;
583 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
584 return ('backup', $fn, $2);
585 }
586 return ('backup', $fn);
587 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
588 return ('snippets', $1);
589 }
590
591 die "unable to parse directory volume name '$volname'\n";
592}
593
594my $vtype_subdirs = {
595 images => 'images',
596 rootdir => 'private',
597 iso => 'template/iso',
598 vztmpl => 'template/cache',
599 backup => 'dump',
600 snippets => 'snippets',
601};
602
603sub get_vtype_subdirs {
604 return $vtype_subdirs;
605}
606
607sub get_subdir {
608 my ($class, $scfg, $vtype) = @_;
609
610 my $path = $scfg->{path};
611
612 die "storage definition has no path\n" if !$path;
613
614 my $subdir = $vtype_subdirs->{$vtype};
615
616 die "unknown vtype '$vtype'\n" if !defined($subdir);
617
618 return "$path/$subdir";
619}
620
621sub filesystem_path {
622 my ($class, $scfg, $volname, $snapname) = @_;
623
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)$/;
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
641sub path {
642 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
643
644 return $class->filesystem_path($scfg, $volname, $snapname);
645}
646
647sub create_base {
648 my ($class, $storeid, $scfg, $volname) = @_;
649
650 # this only works for file based storage types
651 die "storage definition has no path\n" if !$scfg->{path};
652
653 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
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
660 my $path = $class->filesystem_path($scfg, $volname);
661
662 my ($size, undef, $used, $parent) = file_size_info($path);
663 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
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
674 my $newpath = $class->filesystem_path($scfg, $newvolname);
675
676 die "file '$newpath' already exists\n" if -f $newpath;
677
678 rename($path, $newpath) ||
679 die "rename '$path' to '$newpath' failed - $!\n";
680
681 # We try to protect base volume
682
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 $@;
688
689 return $newvolname;
690}
691
692my $get_vm_disk_number = sub {
693 my ($disk_name, $scfg, $vmid, $suffix) = @_;
694
695 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
696
697 my $type = $scfg->{type};
698 my $def = { %{$defaultData->{plugindata}->{$type}} };
699
700 my $valid = $def->{format}[0];
701 if ($valid->{subvol}) {
702 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
703 }
704
705 if ($disk_name =~ m/$disk_regex/) {
706 return $2;
707 }
708
709 return undef;
710};
711
712sub get_next_vm_diskname {
713 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
714
715 $fmt //= '';
716 my $prefix = ($fmt eq 'subvol') ? 'subvol' : 'vm';
717 my $suffix = $add_fmt_suffix ? ".$fmt" : '';
718
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
725 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
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
734sub find_free_diskname {
735 my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
736
737 my $disks = $class->list_images($storeid, $scfg, $vmid);
738
739 my $disk_list = [ map { $_->{volid} } @$disks ];
740
741 return get_next_vm_diskname($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix);
742}
743
744sub clone_image {
745 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
746
747 # this only works for file based storage types
748 die "storage definition has no path\n" if !$scfg->{path};
749
750 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
751 $class->parse_volname($volname);
752
753 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
754
755 die "this storage type does not support clone_image on snapshot\n" if $snap;
756
757 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
758
759 die "clone_image only works on base images\n" if !$isBase;
760
761 my $imagedir = $class->get_subdir($scfg, 'images');
762 $imagedir .= "/$vmid";
763
764 mkpath $imagedir;
765
766 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, "qcow2", 1);
767
768 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
769
770 my $newvol = "$basevmid/$basename/$vmid/$name";
771
772 my $path = $class->filesystem_path($scfg, $newvol);
773
774 # Note: we use relative paths, so we need to call chdir before qemu-img
775 eval {
776 local $CWD = $imagedir;
777
778 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
779 '-F', $format, '-f', 'qcow2', $path];
780
781 run_command($cmd);
782 };
783 my $err = $@;
784
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
798 $name = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt, 1) if !$name;
799
800 my (undef, $tmpfmt) = parse_name_dir($name);
801
802 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
803 if $tmpfmt ne $fmt;
804
805 my $path = "$imagedir/$name";
806
807 die "disk image '$path' already exists\n" if -e $path;
808
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;
812
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;
818 } else {
819 my $cmd = ['/usr/bin/qemu-img', 'create'];
820
821 my $prealloc_opt = preallocation_cmd_option($scfg, $fmt);
822 push @$cmd, '-o', $prealloc_opt if defined($prealloc_opt);
823
824 push @$cmd, '-f', $fmt, $path, "${size}K";
825
826 eval { run_command($cmd, errmsg => "unable to create image"); };
827 if ($@) {
828 unlink $path;
829 rmdir $imagedir;
830 die "$@";
831 }
832 }
833
834 return "$vmid/$name";
835}
836
837sub free_image {
838 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
839
840 die "cannot remove protected volume '$volname' on '$storeid'\n"
841 if $class->get_volume_attribute($scfg, $storeid, $volname, 'protected');
842
843 my $path = $class->filesystem_path($scfg, $volname);
844
845 if ($isBase) {
846 # try to remove immutable flag
847 eval { run_command(['/usr/bin/chattr', '-i', $path]); };
848 warn $@ if $@;
849 }
850
851 if (defined($format) && ($format eq 'subvol')) {
852 File::Path::remove_tree($path);
853 } else {
854 if (!(-f $path || -l $path)) {
855 warn "disk image '$path' does not exist\n";
856 return undef;
857 }
858
859 unlink($path) || die "unlink '$path' failed - $!\n";
860 }
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);
866
867 return undef;
868}
869
870sub file_size_info {
871 my ($filename, $timeout) = @_;
872
873 my $st = File::stat::stat($filename);
874
875 if (!defined($st)) {
876 my $extramsg = -l $filename ? ' - dangling symlink?' : '';
877 warn "failed to stat '$filename'$extramsg\n";
878 return undef;
879 }
880
881 if (S_ISDIR($st->mode)) {
882 return wantarray ? (0, 'subvol', 0, undef, $st->ctime) : 1;
883 }
884
885 my $json = '';
886 eval {
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 );
892 };
893 warn $@ if $@;
894
895 my $info = eval { decode_json($json) };
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 }
900
901 my ($size, $format, $used, $parent) = $info->@{qw(virtual-size format actual-size backing-filename)};
902
903 ($size) = ($size =~ /^(\d+)$/) or die "size '$size' not an integer\n"; # untaint
904 # coerce back from string
905 $size = int($size);
906 ($used) = ($used =~ /^(\d+)$/) or die "used '$used' not an integer\n"; # untaint
907 # coerce back from string
908 $used = int($used);
909 ($format) = ($format =~ /^(\S+)$/) or die "format '$format' includes whitespace\n"; # untaint
910 if (defined($parent)) {
911 ($parent) = ($parent =~ /^(\S+)$/) or die "parent '$parent' includes whitespace\n"; # untaint
912 }
913 return wantarray ? ($size, $format, $used, $parent, $st->ctime) : $size;
914}
915
916# FIXME remove on the next APIAGE reset.
917# Deprecated, use get_volume_attribute instead.
918sub get_volume_notes {
919 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
920
921 die "volume notes are not supported for $class";
922}
923
924# FIXME remove on the next APIAGE reset.
925# Deprecated, use update_volume_attribute instead.
926sub update_volume_notes {
927 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
928
929 die "volume notes are not supported for $class";
930}
931
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.
936# protected - not to be removed by free_image, and for backups, ignored when pruning.
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
963sub volume_size_info {
964 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
965 my $path = $class->filesystem_path($scfg, $volname);
966 return file_size_info($path, $timeout);
967
968}
969
970sub volume_resize {
971 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
972
973 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
974
975 return 1 if $running;
976
977 my $path = $class->filesystem_path($scfg, $volname);
978
979 my $format = ($class->parse_volname($volname))[6];
980
981 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
982
983 run_command($cmd, timeout => 10);
984
985 return undef;
986}
987
988sub volume_snapshot {
989 my ($class, $scfg, $storeid, $volname, $snap) = @_;
990
991 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
992
993 my $path = $class->filesystem_path($scfg, $volname);
994
995 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
996
997 run_command($cmd);
998
999 return undef;
1000}
1001
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.
1005sub volume_rollback_is_possible {
1006 my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_;
1007
1008 return 1;
1009}
1010
1011sub volume_snapshot_rollback {
1012 my ($class, $scfg, $storeid, $volname, $snap) = @_;
1013
1014 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
1015
1016 my $path = $class->filesystem_path($scfg, $volname);
1017
1018 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
1019
1020 run_command($cmd);
1021
1022 return undef;
1023}
1024
1025sub volume_snapshot_delete {
1026 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
1027
1028 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
1029
1030 return 1 if $running;
1031
1032 my $path = $class->filesystem_path($scfg, $volname);
1033
1034 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
1035
1036 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
1037
1038 run_command($cmd);
1039
1040 return undef;
1041}
1042
1043sub volume_snapshot_needs_fsfreeze {
1044
1045 return 0;
1046}
1047sub storage_can_replicate {
1048 my ($class, $scfg, $storeid, $format) = @_;
1049
1050 return 0;
1051}
1052
1053sub volume_has_feature {
1054 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_;
1055
1056 my $features = {
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 },
1079 };
1080
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 }
1091
1092
1093 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname);
1094
1095 my $key = undef;
1096 if($snapname){
1097 $key = 'snap';
1098 }else{
1099 $key = $isBase ? 'base' : 'current';
1100 }
1101
1102 return 1 if defined($features->{$feature}->{$key}->{$format});
1103
1104 return undef;
1105}
1106
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);
1113 my $fmts = join ('|', @$vaidFmts);
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;
1124
1125 next if !$vollist && defined($vmid) && ($owner ne $vmid);
1126
1127 my ($size, $format, $used, $parent, $ctime) = file_size_info($fn);
1128 next if !($format && defined($size));
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 }
1137
1138 if ($vollist) {
1139 my $found = grep { $_ eq $volid } @$vollist;
1140 next if !$found;
1141 }
1142
1143 my $info = {
1144 volid => $volid, format => $format,
1145 size => $size, vmid => $owner, used => $used, parent => $parent
1146 };
1147
1148 $info->{ctime} = $ctime if $ctime;
1149
1150 push @$res, $info;
1151 }
1152
1153 return $res;
1154}
1155
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/*>) {
1163 my $st = File::stat::stat($fn);
1164
1165 next if (!$st || S_ISDIR($st->mode));
1166
1167 my $info;
1168
1169 if ($tt eq 'iso') {
1170 next if $fn !~ m!/([^/]+$PVE::Storage::ISO_EXT_RE_0)$!i;
1171
1172 $info = { volid => "$sid:iso/$1", format => 'iso' };
1173
1174 } elsif ($tt eq 'vztmpl') {
1175 next if $fn !~ m!/([^/]+$PVE::Storage::VZTMPL_EXT_RE_1)$!;
1176
1177 $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
1178
1179 } elsif ($tt eq 'backup') {
1180 next if $fn !~ m!/([^/]+$PVE::Storage::BACKUP_EXT_RE_2)$!;
1181 my $original = $fn;
1182 my $format = $2;
1183 $fn = $1;
1184
1185 # only match for VMID now, to avoid false positives (VMID in parent directory name)
1186 next if defined($vmid) && $fn !~ m/\S+-$vmid-\S+/;
1187
1188 $info = { volid => "$sid:backup/$fn", format => $format };
1189
1190 my $archive_info = eval { PVE::Storage::archive_info($fn) } // {};
1191
1192 $info->{ctime} = $archive_info->{ctime} if defined($archive_info->{ctime});
1193 $info->{subtype} = $archive_info->{type} // 'unknown';
1194
1195 if (defined($vmid) || $fn =~ m!\-([1-9][0-9]{2,8})\-[^/]+\.${format}$!) {
1196 $info->{vmid} = $vmid // $1;
1197 }
1198
1199 my $notes_fn = $original.NOTES_EXT;
1200 if (-f $notes_fn) {
1201 my $notes = PVE::Tools::file_read_firstline($notes_fn);
1202 $info->{notes} = eval { decode('UTF-8', $notes, 1) } // $notes if defined($notes);
1203 }
1204
1205 $info->{protected} = 1 if -e PVE::Storage::protection_file_path($original);
1206 } elsif ($tt eq 'snippets') {
1207
1208 $info = {
1209 volid => "$sid:snippets/". basename($fn),
1210 format => 'snippet',
1211 };
1212 }
1213
1214 $info->{size} = $st->size;
1215 $info->{ctime} //= $st->ctime;
1216
1217 push @$res, $info;
1218 }
1219
1220 return $res;
1221};
1222
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.
1225sub list_volumes {
1226 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
1227
1228 my $res = [];
1229 my $vmlist = PVE::Cluster::get_vmlist();
1230 foreach my $type (@$content_types) {
1231 my $data;
1232
1233 if ($type eq 'images' || $type eq 'rootdir') {
1234 $data = $class->list_images($storeid, $scfg, $vmid);
1235 } elsif ($scfg->{path}) {
1236 my $path = $class->get_subdir($scfg, $type);
1237
1238 if ($type eq 'iso' && !defined($vmid)) {
1239 $data = $get_subdir_files->($storeid, $path, 'iso');
1240 } elsif ($type eq 'vztmpl'&& !defined($vmid)) {
1241 $data = $get_subdir_files->($storeid, $path, 'vztmpl');
1242 } elsif ($type eq 'backup') {
1243 $data = $get_subdir_files->($storeid, $path, 'backup', $vmid);
1244 } elsif ($type eq 'snippets') {
1245 $data = $get_subdir_files->($storeid, $path, 'snippets');
1246 }
1247 }
1248
1249 next if !$data;
1250
1251 foreach my $item (@$data) {
1252 if ($type eq 'images' || $type eq 'rootdir') {
1253 my $vminfo = $vmlist->{ids}->{$item->{vmid}};
1254 my $vmtype;
1255 if (defined($vminfo)) {
1256 $vmtype = $vminfo->{type};
1257 }
1258 if (defined($vmtype) && $vmtype eq 'lxc') {
1259 $item->{content} = 'rootdir';
1260 } else {
1261 $item->{content} = 'images';
1262 }
1263 next if $type ne $item->{content};
1264 } else {
1265 $item->{content} = $type;
1266 }
1267
1268 push @$res, $item;
1269 }
1270 }
1271
1272 return $res;
1273}
1274
1275sub status {
1276 my ($class, $storeid, $scfg, $cache) = @_;
1277
1278 my $path = $scfg->{path};
1279
1280 die "storage definition has no path\n" if !$path;
1281
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
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
1300sub activate_storage {
1301 my ($class, $storeid, $scfg, $cache) = @_;
1302
1303 my $path = $scfg->{path};
1304
1305 die "storage definition has no path\n" if !$path;
1306
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
1314
1315 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
1316
1317 if (defined($scfg->{content})) {
1318 foreach my $vtype (keys %$vtype_subdirs) {
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 }
1325 }
1326 }
1327}
1328
1329sub deactivate_storage {
1330 my ($class, $storeid, $scfg, $cache) = @_;
1331
1332 # do nothing by default
1333}
1334
1335sub map_volume {
1336 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1337
1338 my ($path) = $class->path($scfg, $volname, $storeid, $snapname);
1339 return $path;
1340}
1341
1342sub unmap_volume {
1343 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
1344
1345 return 1;
1346}
1347
1348sub activate_volume {
1349 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1350
1351 my $path = $class->filesystem_path($scfg, $volname, $snapname);
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 {
1362 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1363
1364 # do nothing by default
1365}
1366
1367sub check_connection {
1368 my ($class, $storeid, $scfg) = @_;
1369 # do nothing by default
1370 return 1;
1371}
1372
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};
1385 my $archive_info = eval { PVE::Storage::archive_info($volid) } // {};
1386 my $backup_type = $archive_info->{type} // 'unknown';
1387 my $backup_vmid = $archive_info->{vmid} // $backup->{vmid};
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}) {
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;
1403
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
1409 $prune_entry->{mark} = 'renamed';
1410 }
1411
1412 $prune_entry->{mark} = 'protected' if $backup->{protected};
1413
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
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)
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 .`
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);
1482 die "import: no size found in export header, aborting.\n" if !defined($size);
1483 # Size is in bytes!
1484 return $size;
1485}
1486
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) = @_;
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);
1514 run_command(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1515 output => '>&'.fileno($fh));
1516 return;
1517 }
1518 }
1519 unsupported:
1520 die "volume export format $format not available for $class";
1521}
1522
1523sub volume_export_formats {
1524 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
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 ();
1538}
1539
1540# Import data from a stream, creating a new or replacing or adding to an existing volume.
1541sub volume_import {
1542 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
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);
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 }
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);
1575 my $oldname = $volname;
1576 $volname = $allocname;
1577 if (defined($name) && $allocname ne $oldname) {
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') {
1586 run_command(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
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 }
1597
1598 return "$storeid:$volname";
1599}
1600
1601sub volume_import_formats {
1602 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
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 ();
1613}
1614
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
16501;