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