]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
796444124b43c5fbc0b41ff59c6a4043ce002b6c
[pve-storage.git] / PVE / Storage / Plugin.pm
1 package PVE::Storage::Plugin;
2
3 use strict;
4 use warnings;
5
6 use File::chdir;
7 use File::Path;
8 use File::Basename;
9
10 use PVE::Tools qw(run_command);
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::Cluster qw(cfs_register_file);
13
14 use base qw(PVE::SectionConfig);
15
16 our @COMMON_TAR_FLAGS = qw(
17 --one-file-system
18 -p --sparse --numeric-owner --acls
19 --xattrs --xattrs-include=user.* --xattrs-include=security.capability
20 --warning=no-file-ignored --warning=no-xattr-write
21 );
22
23 our @SHARED_STORAGE = (
24 'iscsi',
25 'nfs',
26 'cifs',
27 'rbd',
28 'cephfs',
29 'sheepdog',
30 'iscsidirect',
31 'glusterfs',
32 'zfs',
33 'drbd');
34
35 our $MAX_VOLUMES_PER_GUEST = 1024;
36
37 cfs_register_file ('storage.cfg',
38 sub { __PACKAGE__->parse_config(@_); },
39 sub { __PACKAGE__->write_config(@_); });
40
41
42 my $defaultData = {
43 propertyList => {
44 type => { description => "Storage type." },
45 storage => get_standard_option('pve-storage-id',
46 { completion => \&PVE::Storage::complete_storage }),
47 nodes => get_standard_option('pve-node-list', { optional => 1 }),
48 content => {
49 description => "Allowed content types.\n\nNOTE: the value " .
50 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
51 type => 'string', format => 'pve-storage-content-list',
52 optional => 1,
53 completion => \&PVE::Storage::complete_content_type,
54 },
55 disable => {
56 description => "Flag to disable the storage.",
57 type => 'boolean',
58 optional => 1,
59 },
60 maxfiles => {
61 description => "Maximal number of backup files per VM. Use '0' for unlimted.",
62 type => 'integer',
63 minimum => 0,
64 optional => 1,
65 },
66 shared => {
67 description => "Mark storage as shared.",
68 type => 'boolean',
69 optional => 1,
70 },
71 'format' => {
72 description => "Default image format.",
73 type => 'string', format => 'pve-storage-format',
74 optional => 1,
75 },
76 },
77 };
78
79 sub content_hash_to_string {
80 my $hash = shift;
81
82 my @cta;
83 foreach my $ct (keys %$hash) {
84 push @cta, $ct if $hash->{$ct};
85 }
86
87 return join(',', @cta);
88 }
89
90 sub valid_content_types {
91 my ($type) = @_;
92
93 my $def = $defaultData->{plugindata}->{$type};
94
95 return {} if !$def;
96
97 return $def->{content}->[0];
98 }
99
100 sub default_format {
101 my ($scfg) = @_;
102
103 my $type = $scfg->{type};
104 my $def = $defaultData->{plugindata}->{$type};
105
106 my $def_format = 'raw';
107 my $valid_formats = [ $def_format ];
108
109 if (defined($def->{format})) {
110 $def_format = $scfg->{format} || $def->{format}->[1];
111 $valid_formats = [ sort keys %{$def->{format}->[0]} ];
112 }
113
114 return wantarray ? ($def_format, $valid_formats) : $def_format;
115 }
116
117 PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
118 sub verify_path {
119 my ($path, $noerr) = @_;
120
121 # fixme: exclude more shell meta characters?
122 # we need absolute paths
123 if ($path !~ m|^/[^;\(\)]+|) {
124 return undef if $noerr;
125 die "value does not look like a valid absolute path\n";
126 }
127 return $path;
128 }
129
130 PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
131 sub verify_server {
132 my ($server, $noerr) = @_;
133
134 if (!(PVE::JSONSchema::pve_verify_ip($server, 1) ||
135 PVE::JSONSchema::pve_verify_dns_name($server, 1)))
136 {
137 return undef if $noerr;
138 die "value does not look like a valid server name or IP address\n";
139 }
140 return $server;
141 }
142
143 PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name);
144 sub parse_lvm_name {
145 my ($name, $noerr) = @_;
146
147 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
148 return undef if $noerr;
149 die "lvm name '$name' contains illegal characters\n";
150 }
151
152 return $name;
153 }
154
155 # fixme: do we need this
156 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
157 #sub verify_portal {
158 # my ($portal, $noerr) = @_;
159 #
160 # # IP with optional port
161 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
162 # return undef if $noerr;
163 # die "value does not look like a valid portal address\n";
164 # }
165 # return $portal;
166 #}
167
168 PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns);
169 sub verify_portal_dns {
170 my ($portal, $noerr) = @_;
171
172 # IP or DNS name with optional port
173 if (!PVE::Tools::parse_host_and_port($portal)) {
174 return undef if $noerr;
175 die "value does not look like a valid portal address\n";
176 }
177 return $portal;
178 }
179
180 PVE::JSONSchema::register_format('pve-storage-content', \&verify_content);
181 sub verify_content {
182 my ($ct, $noerr) = @_;
183
184 my $valid_content = valid_content_types('dir'); # dir includes all types
185
186 if (!$valid_content->{$ct}) {
187 return undef if $noerr;
188 die "invalid content type '$ct'\n";
189 }
190
191 return $ct;
192 }
193
194 PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
195 sub verify_format {
196 my ($fmt, $noerr) = @_;
197
198 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
199 return undef if $noerr;
200 die "invalid format '$fmt'\n";
201 }
202
203 return $fmt;
204 }
205
206 PVE::JSONSchema::register_format('pve-storage-options', \&verify_options);
207 sub verify_options {
208 my ($value, $noerr) = @_;
209
210 # mount options (see man fstab)
211 if ($value !~ m/^\S+$/) {
212 return undef if $noerr;
213 die "invalid options '$value'\n";
214 }
215
216 return $value;
217 }
218
219 PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
220 sub parse_volume_id {
221 my ($volid, $noerr) = @_;
222
223 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
224 return wantarray ? ($1, $2) : $1;
225 }
226 return undef if $noerr;
227 die "unable to parse volume ID '$volid'\n";
228 }
229
230
231 sub private {
232 return $defaultData;
233 }
234
235 sub parse_section_header {
236 my ($class, $line) = @_;
237
238 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
239 my ($type, $storeid) = (lc($1), $2);
240 my $errmsg = undef; # set if you want to skip whole section
241 eval { PVE::JSONSchema::parse_storage_id($storeid); };
242 $errmsg = $@ if $@;
243 my $config = {}; # to return additional attributes
244 return ($type, $storeid, $errmsg, $config);
245 }
246 return undef;
247 }
248
249 sub decode_value {
250 my ($class, $type, $key, $value) = @_;
251
252 my $def = $defaultData->{plugindata}->{$type};
253
254 if ($key eq 'content') {
255 my $valid_content = $def->{content}->[0];
256
257 my $res = {};
258
259 foreach my $c (PVE::Tools::split_list($value)) {
260 if (!$valid_content->{$c}) {
261 warn "storage does not support content type '$c'\n";
262 next;
263 }
264 $res->{$c} = 1;
265 }
266
267 if ($res->{none} && scalar (keys %$res) > 1) {
268 die "unable to combine 'none' with other content types\n";
269 }
270
271 return $res;
272 } elsif ($key eq 'format') {
273 my $valid_formats = $def->{format}->[0];
274
275 if (!$valid_formats->{$value}) {
276 warn "storage does not support format '$value'\n";
277 next;
278 }
279
280 return $value;
281 } elsif ($key eq 'nodes') {
282 my $res = {};
283
284 foreach my $node (PVE::Tools::split_list($value)) {
285 if (PVE::JSONSchema::pve_verify_node_name($node)) {
286 $res->{$node} = 1;
287 }
288 }
289
290 # fixme:
291 # no node restrictions for local storage
292 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
293 # die "storage '$storeid' does not allow node restrictions\n";
294 #}
295
296 return $res;
297 }
298
299 return $value;
300 }
301
302 sub encode_value {
303 my ($class, $type, $key, $value) = @_;
304
305 if ($key eq 'nodes') {
306 return join(',', keys(%$value));
307 } elsif ($key eq 'content') {
308 my $res = content_hash_to_string($value) || 'none';
309 return $res;
310 }
311
312 return $value;
313 }
314
315 sub parse_config {
316 my ($class, $filename, $raw) = @_;
317
318 my $cfg = $class->SUPER::parse_config($filename, $raw);
319 my $ids = $cfg->{ids};
320
321 # make sure we have a reasonable 'local:' storage
322 # we want 'local' to be always the same 'type' (on all cluster nodes)
323 if (!$ids->{local} || $ids->{local}->{type} ne 'dir' ||
324 ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) {
325 $ids->{local} = {
326 type => 'dir',
327 priority => 0, # force first entry
328 path => '/var/lib/vz',
329 maxfiles => 0,
330 content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1, snippets => 1},
331 };
332 }
333
334 # make sure we have a path
335 $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path};
336
337 # remove node restrictions for local storage
338 delete($ids->{local}->{nodes});
339
340 foreach my $storeid (keys %$ids) {
341 my $d = $ids->{$storeid};
342 my $type = $d->{type};
343
344 my $def = $defaultData->{plugindata}->{$type};
345
346 if ($def->{content}) {
347 $d->{content} = $def->{content}->[1] if !$d->{content};
348 }
349 if (grep { $_ eq $type } @SHARED_STORAGE) {
350 $d->{shared} = 1;
351 }
352 }
353
354 return $cfg;
355 }
356
357 # Storage implementation
358
359 # called during addition of storage (before the new storage config got written)
360 # die to abort additon if there are (grave) problems
361 # NOTE: runs in a storage config *locked* context
362 sub on_add_hook {
363 my ($class, $storeid, $scfg, %param) = @_;
364
365 # do nothing by default
366 }
367
368 # called during deletion of storage (before the new storage config got written)
369 # and if the activate check on addition fails, to cleanup all storage traces
370 # which on_add_hook may have created.
371 # die to abort deletion if there are (very grave) problems
372 # NOTE: runs in a storage config *locked* context
373 sub on_delete_hook {
374 my ($class, $storeid, $scfg) = @_;
375
376 # do nothing by default
377 }
378
379 sub cluster_lock_storage {
380 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
381
382 my $res;
383 if (!$shared) {
384 my $lockid = "pve-storage-$storeid";
385 my $lockdir = "/var/lock/pve-manager";
386 mkdir $lockdir;
387 $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
388 die $@ if $@;
389 } else {
390 $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
391 die $@ if $@;
392 }
393 return $res;
394 }
395
396 sub parse_name_dir {
397 my $name = shift;
398
399 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
400 return ($1, $3, $2); # (name, format, isBase)
401 }
402
403 die "unable to parse volume filename '$name'\n";
404 }
405
406 sub parse_volname {
407 my ($class, $volname) = @_;
408
409 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
410 my ($basedvmid, $basename) = ($1, $2);
411 parse_name_dir($basename);
412 my ($vmid, $name) = ($3, $4);
413 my (undef, $format, $isBase) = parse_name_dir($name);
414 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
415 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
416 my ($vmid, $name) = ($1, $2);
417 my (undef, $format, $isBase) = parse_name_dir($name);
418 return ('images', $name, $vmid, undef, undef, $isBase, $format);
419 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
420 return ('iso', $1);
421 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
422 return ('vztmpl', $1);
423 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
424 return ('rootdir', $1, $1);
425 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
426 my $fn = $1;
427 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
428 return ('backup', $fn, $2);
429 }
430 return ('backup', $fn);
431 } elsif ($volname =~ m!^snippets/([^/]+)$!) {
432 return ('snippets', $1);
433 }
434
435 die "unable to parse directory volume name '$volname'\n";
436 }
437
438 my $vtype_subdirs = {
439 images => 'images',
440 rootdir => 'private',
441 iso => 'template/iso',
442 vztmpl => 'template/cache',
443 backup => 'dump',
444 snippets => 'snippets',
445 };
446
447 sub get_subdir {
448 my ($class, $scfg, $vtype) = @_;
449
450 my $path = $scfg->{path};
451
452 die "storage definintion has no path\n" if !$path;
453
454 my $subdir = $vtype_subdirs->{$vtype};
455
456 die "unknown vtype '$vtype'\n" if !defined($subdir);
457
458 return "$path/$subdir";
459 }
460
461 sub filesystem_path {
462 my ($class, $scfg, $volname, $snapname) = @_;
463
464 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
465 $class->parse_volname($volname);
466
467 # Note: qcow2/qed has internal snapshot, so path is always
468 # the same (with or without snapshot => same file).
469 die "can't snapshot this image format\n"
470 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
471
472 my $dir = $class->get_subdir($scfg, $vtype);
473
474 $dir .= "/$vmid" if $vtype eq 'images';
475
476 my $path = "$dir/$name";
477
478 return wantarray ? ($path, $vmid, $vtype) : $path;
479 }
480
481 sub path {
482 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
483
484 return $class->filesystem_path($scfg, $volname, $snapname);
485 }
486
487 sub create_base {
488 my ($class, $storeid, $scfg, $volname) = @_;
489
490 # this only works for file based storage types
491 die "storage definition has no path\n" if !$scfg->{path};
492
493 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
494 $class->parse_volname($volname);
495
496 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
497
498 die "create_base not possible with base image\n" if $isBase;
499
500 my $path = $class->filesystem_path($scfg, $volname);
501
502 my ($size, undef, $used, $parent) = file_size_info($path);
503 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
504
505 die "volname '$volname' contains wrong information about parent\n"
506 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
507
508 my $newname = $name;
509 $newname =~ s/^vm-/base-/;
510
511 my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" :
512 "$vmid/$newname";
513
514 my $newpath = $class->filesystem_path($scfg, $newvolname);
515
516 die "file '$newpath' already exists\n" if -f $newpath;
517
518 rename($path, $newpath) ||
519 die "rename '$path' to '$newpath' failed - $!\n";
520
521 # We try to protect base volume
522
523 chmod(0444, $newpath); # nobody should write anything
524
525 # also try to set immutable flag
526 eval { run_command(['/usr/bin/chattr', '+i', $newpath]); };
527 warn $@ if $@;
528
529 return $newvolname;
530 }
531
532 my $get_vm_disk_number = sub {
533 my ($disk_name, $scfg, $vmid, $suffix) = @_;
534
535 my $disk_regex = qr/(vm|base)-$vmid-disk-(\d+)$suffix/;
536
537 my $type = $scfg->{type};
538 my $def = { %{$defaultData->{plugindata}->{$type}} };
539
540 my $valid = $def->{format}[0];
541 if ($valid->{subvol}) {
542 $disk_regex = qr/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/;
543 }
544
545 if ($disk_name =~ m/$disk_regex/) {
546 return $2;
547 }
548
549 return undef;
550 };
551
552 sub get_next_vm_diskname {
553 my ($disk_list, $storeid, $vmid, $fmt, $scfg, $add_fmt_suffix) = @_;
554
555 $fmt //= '';
556 my $prefix = ($fmt eq 'subvol') ? 'subvol' : 'vm';
557 my $suffix = $add_fmt_suffix ? ".$fmt" : '';
558
559 my $disk_ids = {};
560 foreach my $disk (@$disk_list) {
561 my $disknum = $get_vm_disk_number->($disk, $scfg, $vmid, $suffix);
562 $disk_ids->{$disknum} = 1 if defined($disknum);
563 }
564
565 for (my $i = 0; $i < $MAX_VOLUMES_PER_GUEST; $i++) {
566 if (!$disk_ids->{$i}) {
567 return "$prefix-$vmid-disk-$i$suffix";
568 }
569 }
570
571 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
572 }
573
574 my $find_free_diskname = sub {
575 my ($imgdir, $vmid, $fmt, $scfg) = @_;
576
577 my $disk_list = [];
578
579 if (defined(my $dh = IO::Dir->new($imgdir))) {
580 @$disk_list = $dh->read();
581 $dh->close();
582 }
583
584 return get_next_vm_diskname($disk_list, $imgdir, $vmid, $fmt, $scfg, 1);
585 };
586
587 sub clone_image {
588 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
589
590 # this only works for file based storage types
591 die "storage definintion has no path\n" if !$scfg->{path};
592
593 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
594 $class->parse_volname($volname);
595
596 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
597
598 die "this storage type does not support clone_image on snapshot\n" if $snap;
599
600 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
601
602 die "clone_image only works on base images\n" if !$isBase;
603
604 my $imagedir = $class->get_subdir($scfg, 'images');
605 $imagedir .= "/$vmid";
606
607 mkpath $imagedir;
608
609 my $name = $find_free_diskname->($imagedir, $vmid, "qcow2", $scfg);
610
611 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
612
613 my $newvol = "$basevmid/$basename/$vmid/$name";
614
615 my $path = $class->filesystem_path($scfg, $newvol);
616
617 # Note: we use relative paths, so we need to call chdir before qemu-img
618 eval {
619 local $CWD = $imagedir;
620
621 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
622 '-f', 'qcow2', $path];
623
624 run_command($cmd);
625 };
626 my $err = $@;
627
628 die $err if $err;
629
630 return $newvol;
631 }
632
633 sub alloc_image {
634 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
635
636 my $imagedir = $class->get_subdir($scfg, 'images');
637 $imagedir .= "/$vmid";
638
639 mkpath $imagedir;
640
641 $name = $find_free_diskname->($imagedir, $vmid, $fmt, $scfg) if !$name;
642
643 my (undef, $tmpfmt) = parse_name_dir($name);
644
645 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
646 if $tmpfmt ne $fmt;
647
648 my $path = "$imagedir/$name";
649
650 die "disk image '$path' already exists\n" if -e $path;
651
652 if ($fmt eq 'subvol') {
653 # only allow this if size = 0, so that user knows what he is doing
654 die "storage does not support subvol quotas\n" if $size != 0;
655
656 my $old_umask = umask(0022);
657 my $err;
658 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
659 umask $old_umask;
660 die $err if $err;
661 } else {
662 my $cmd = ['/usr/bin/qemu-img', 'create'];
663
664 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
665
666 push @$cmd, '-f', $fmt, $path, "${size}K";
667
668 eval { run_command($cmd, errmsg => "unable to create image"); };
669 if ($@) {
670 unlink $path;
671 rmdir $imagedir;
672 die "$@";
673 }
674 }
675
676 return "$vmid/$name";
677 }
678
679 sub free_image {
680 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
681
682 my $path = $class->filesystem_path($scfg, $volname);
683
684 if ($isBase) {
685 # try to remove immutable flag
686 eval { run_command(['/usr/bin/chattr', '-i', $path]); };
687 warn $@ if $@;
688 }
689
690 if (defined($format) && ($format eq 'subvol')) {
691 File::Path::remove_tree($path);
692 } else {
693
694 if (! -f $path) {
695 warn "disk image '$path' does not exists\n";
696 return undef;
697 }
698
699 unlink($path) || die "unlink '$path' failed - $!\n";
700 }
701
702 # try to cleanup directory to not clutter storage with empty $vmid dirs if
703 # all images from a guest got deleted
704 my $dir = dirname($path);
705 rmdir($dir);
706
707 return undef;
708 }
709
710 sub file_size_info {
711 my ($filename, $timeout) = @_;
712
713 if (-d $filename) {
714 return wantarray ? (0, 'subvol', 0, undef) : 1;
715 }
716
717 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
718
719 my $format;
720 my $parent;
721 my $size = 0;
722 my $used = 0;
723
724 eval {
725 run_command($cmd, timeout => $timeout, outfunc => sub {
726 my $line = shift;
727 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
728 $format = $1;
729 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
730 $parent = $1;
731 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
732 $size = int($1);
733 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
734 $used = $1;
735 my $u = $3;
736
737 $used *= 1024 if $u eq 'K';
738 $used *= (1024*1024) if $u eq 'M';
739 $used *= (1024*1024*1024) if $u eq 'G';
740 $used *= (1024*1024*1024*1024) if $u eq 'T';
741
742 $used = int($used);
743 }
744 });
745 };
746
747 return wantarray ? ($size, $format, $used, $parent) : $size;
748 }
749
750 sub volume_size_info {
751 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
752 my $path = $class->filesystem_path($scfg, $volname);
753 return file_size_info($path, $timeout);
754
755 }
756
757 sub volume_resize {
758 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
759
760 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
761
762 return 1 if $running;
763
764 my $path = $class->filesystem_path($scfg, $volname);
765
766 my $format = ($class->parse_volname($volname))[6];
767
768 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
769
770 run_command($cmd, timeout => 10);
771
772 return undef;
773 }
774
775 sub volume_snapshot {
776 my ($class, $scfg, $storeid, $volname, $snap) = @_;
777
778 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
779
780 my $path = $class->filesystem_path($scfg, $volname);
781
782 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
783
784 run_command($cmd);
785
786 return undef;
787 }
788
789 sub volume_rollback_is_possible {
790 my ($class, $scfg, $storeid, $volname, $snap) = @_;
791
792 return 1;
793 }
794
795 sub volume_snapshot_rollback {
796 my ($class, $scfg, $storeid, $volname, $snap) = @_;
797
798 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
799
800 my $path = $class->filesystem_path($scfg, $volname);
801
802 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
803
804 run_command($cmd);
805
806 return undef;
807 }
808
809 sub volume_snapshot_delete {
810 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
811
812 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
813
814 return 1 if $running;
815
816 my $path = $class->filesystem_path($scfg, $volname);
817
818 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
819
820 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
821
822 run_command($cmd);
823
824 return undef;
825 }
826
827 sub storage_can_replicate {
828 my ($class, $scfg, $storeid, $format) = @_;
829
830 return 0;
831 }
832
833 sub volume_has_feature {
834 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
835
836 my $features = {
837 snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
838 clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
839 template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} },
840 copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
841 current => {qcow2 => 1, raw => 1, vmdk => 1},
842 snap => {qcow2 => 1} },
843 sparseinit => { base => {qcow2 => 1, raw => 1, vmdk => 1},
844 current => {qcow2 => 1, raw => 1, vmdk => 1} },
845 };
846
847 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
848 $class->parse_volname($volname);
849
850 my $key = undef;
851 if($snapname){
852 $key = 'snap';
853 }else{
854 $key = $isBase ? 'base' : 'current';
855 }
856
857 return 1 if defined($features->{$feature}->{$key}->{$format});
858
859 return undef;
860 }
861
862 sub list_images {
863 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
864
865 my $imagedir = $class->get_subdir($scfg, 'images');
866
867 my ($defFmt, $vaidFmts) = default_format($scfg);
868 my $fmts = join ('|', @$vaidFmts);
869
870 my $res = [];
871
872 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
873
874 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
875 $fn = $1; # untaint
876
877 my $owner = $2;
878 my $name = $3;
879
880 next if !$vollist && defined($vmid) && ($owner ne $vmid);
881
882 my ($size, $format, $used, $parent) = file_size_info($fn);
883 next if !($format && defined($size));
884
885 my $volid;
886 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
887 my ($basevmid, $basename) = ($1, $2);
888 $volid = "$storeid:$basevmid/$basename/$owner/$name";
889 } else {
890 $volid = "$storeid:$owner/$name";
891 }
892
893 if ($vollist) {
894 my $found = grep { $_ eq $volid } @$vollist;
895 next if !$found;
896 }
897
898 push @$res, {
899 volid => $volid, format => $format,
900 size => $size, vmid => $owner, used => $used, parent => $parent
901 };
902 }
903
904 return $res;
905 }
906
907 sub status {
908 my ($class, $storeid, $scfg, $cache) = @_;
909
910 my $path = $scfg->{path};
911
912 die "storage definintion has no path\n" if !$path;
913
914 my $timeout = 2;
915 my $res = PVE::Tools::df($path, $timeout);
916
917 return undef if !$res || !$res->{total};
918
919 return ($res->{total}, $res->{avail}, $res->{used}, 1);
920 }
921
922 sub volume_snapshot_list {
923 my ($class, $scfg, $storeid, $volname) = @_;
924
925 # implement in subclass
926 die "Volume_snapshot_list is not implemented for $class";
927
928 # return an empty array if dataset does not exist.
929 }
930
931 sub activate_storage {
932 my ($class, $storeid, $scfg, $cache) = @_;
933
934 my $path = $scfg->{path};
935
936 die "storage definintion has no path\n" if !$path;
937
938 # this path test may hang indefinitely on unresponsive mounts
939 my $timeout = 2;
940 if (! PVE::Tools::run_fork_with_timeout($timeout, sub {-d $path})) {
941 die "unable to activate storage '$storeid' - " .
942 "directory '$path' does not exist or is unreachable\n";
943 }
944
945
946 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
947
948 if (defined($scfg->{content})) {
949 foreach my $vtype (keys %$vtype_subdirs) {
950 # OpenVZMigrate uses backup (dump) dir
951 if (defined($scfg->{content}->{$vtype}) ||
952 ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) {
953 my $subdir = $class->get_subdir($scfg, $vtype);
954 mkpath $subdir if $subdir ne $path;
955 }
956 }
957 }
958 }
959
960 sub deactivate_storage {
961 my ($class, $storeid, $scfg, $cache) = @_;
962
963 # do nothing by default
964 }
965
966 sub map_volume {
967 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
968
969 return undef;
970 }
971
972 sub unmap_volume {
973 my ($class, $storeid, $scfg, $volname, $snapname) = @_;
974
975 return 1;
976 }
977
978 sub activate_volume {
979 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
980
981 my $path = $class->filesystem_path($scfg, $volname, $snapname);
982
983 # check is volume exists
984 if ($scfg->{path}) {
985 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
986 } else {
987 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
988 }
989 }
990
991 sub deactivate_volume {
992 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
993
994 # do nothing by default
995 }
996
997 sub check_connection {
998 my ($class, $storeid, $scfg) = @_;
999 # do nothing by default
1000 return 1;
1001 }
1002
1003 # Import/Export interface:
1004 # Any path based storage is assumed to support 'raw' and 'tar' streams, so
1005 # the default implementations will return this if $scfg->{path} is set,
1006 # mimicking the old PVE::Storage::storage_migrate() function.
1007 #
1008 # Plugins may fall back to PVE::Storage::Plugin::volume_{export,import}...
1009 # functions in case the format doesn't match their specialized
1010 # implementations to reuse the raw/tar code.
1011 #
1012 # Format specification:
1013 # The following formats are all prefixed with image information in the form
1014 # of a 64 bit little endian unsigned integer (pack('Q<')) in order to be able
1015 # to preallocate the image on storages which require it.
1016 #
1017 # raw+size: (image files only)
1018 # A raw binary data stream such as produced via `dd if=TheImageFile`.
1019 # qcow2+size, vmdk: (image files only)
1020 # A raw qcow2/vmdk/... file such as produced via `dd if=some.qcow2` for
1021 # files which are already in qcow2 format, or via `qemu-img convert`.
1022 # Note that these formats are only valid with $with_snapshots being true.
1023 # tar+size: (subvolumes only)
1024 # A GNU tar stream containing just the inner contents of the subvolume.
1025 # This does not distinguish between the contents of a privileged or
1026 # unprivileged container. In other words, this is from the root user
1027 # namespace's point of view with no uid-mapping in effect.
1028 # As produced via `tar -C vm-100-disk-1.subvol -cpf TheOutputFile.dat .`
1029
1030 # Plugins may reuse these helpers. Changes to the header format should be
1031 # reflected by changes to the function prototypes.
1032 sub write_common_header($$) {
1033 my ($fh, $image_size_in_bytes) = @_;
1034 syswrite($fh, pack("Q<", $image_size_in_bytes), 8);
1035 }
1036
1037 sub read_common_header($) {
1038 my ($fh) = @_;
1039 sysread($fh, my $size, 8);
1040 $size = unpack('Q<', $size);
1041 die "got a bad size (not a multiple of 1K)\n" if ($size&1023);
1042 # Size is in bytes!
1043 return $size;
1044 }
1045
1046 # Export a volume into a file handle as a stream of desired format.
1047 sub volume_export {
1048 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1049 if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) {
1050 my $file = $class->path($scfg, $volname, $storeid)
1051 or goto unsupported;
1052 my ($size, $file_format) = file_size_info($file);
1053
1054 if ($format eq 'raw+size') {
1055 goto unsupported if $with_snapshots || $file_format eq 'subvol';
1056 write_common_header($fh, $size);
1057 if ($file_format eq 'raw') {
1058 run_command(['dd', "if=$file", "bs=4k"], output => '>&'.fileno($fh));
1059 } else {
1060 run_command(['qemu-img', 'convert', '-f', $file_format, '-O', 'raw', $file, '/dev/stdout'],
1061 output => '>&'.fileno($fh));
1062 }
1063 return;
1064 } elsif ($format =~ /^(qcow2|vmdk)\+size$/) {
1065 my $data_format = $1;
1066 goto unsupported if !$with_snapshots || $file_format ne $data_format;
1067 write_common_header($fh, $size);
1068 run_command(['dd', "if=$file", "bs=4k"], output => '>&'.fileno($fh));
1069 return;
1070 } elsif ($format eq 'tar+size') {
1071 goto unsupported if $file_format ne 'subvol';
1072 write_common_header($fh, $size);
1073 run_command(['tar', @COMMON_TAR_FLAGS, '-cf', '-', '-C', $file, '.'],
1074 output => '>&'.fileno($fh));
1075 return;
1076 }
1077 }
1078 unsupported:
1079 die "volume export format $format not available for $class";
1080 }
1081
1082 sub volume_export_formats {
1083 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
1084 if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) {
1085 my $file = $class->path($scfg, $volname, $storeid)
1086 or return;
1087 my ($size, $format) = file_size_info($file);
1088
1089 if ($with_snapshots) {
1090 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1091 return ();
1092 }
1093 return ('tar+size') if $format eq 'subvol';
1094 return ('raw+size');
1095 }
1096 return ();
1097 }
1098
1099 # Import data from a stream, creating a new or replacing or adding to an existing volume.
1100 sub volume_import {
1101 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
1102
1103 die "volume import format '$format' not available for $class\n"
1104 if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
1105 my $data_format = $1;
1106
1107 die "format $format cannot be imported without snapshots\n"
1108 if !$with_snapshots && ($data_format eq 'qcow2' || $data_format eq 'vmdk');
1109 die "format $format cannot be imported with snapshots\n"
1110 if $with_snapshots && ($data_format eq 'raw' || $data_format eq 'tar');
1111
1112 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
1113 $class->parse_volname($volname);
1114
1115 # XXX: Should we bother with conversion routines at this level? This won't
1116 # happen without manual CLI usage, so for now we just error out...
1117 die "cannot import format $format into a file of format $file_format\n"
1118 if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol');
1119
1120 # Check for an existing file first since interrupting alloc_image doesn't
1121 # free it.
1122 my $file = $class->path($scfg, $volname, $storeid);
1123 die "file '$file' already exists\n" if -e $file;
1124
1125 my ($size) = read_common_header($fh);
1126 $size = int($size/1024);
1127
1128 eval {
1129 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
1130 if ($allocname ne $volname) {
1131 my $oldname = $volname;
1132 $volname = $allocname; # Let the cleanup code know what to free
1133 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
1134 }
1135 my $file = $class->path($scfg, $volname, $storeid)
1136 or die "internal error: failed to get path to newly allocated volume $volname\n";
1137 if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
1138 run_command(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
1139 input => '<&'.fileno($fh));
1140 } elsif ($data_format eq 'tar') {
1141 run_command(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'],
1142 input => '<&'.fileno($fh));
1143 } else {
1144 die "volume import format '$format' not available for $class";
1145 }
1146 };
1147 if (my $err = $@) {
1148 eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
1149 warn $@ if $@;
1150 die $err;
1151 }
1152 }
1153
1154 sub volume_import_formats {
1155 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
1156 if ($scfg->{path} && !defined($base_snapshot)) {
1157 my $format = ($class->parse_volname($volname))[6];
1158 if ($with_snapshots) {
1159 return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk');
1160 return ();
1161 }
1162 return ('tar+size') if $format eq 'subvol';
1163 return ('raw+size');
1164 }
1165 return ();
1166 }
1167
1168 1;