]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
Revert "Add function volume_snapshot_delete_remote."
[pve-storage.git] / PVE / Storage / Plugin.pm
1 package PVE::Storage::Plugin;
2
3 use strict;
4 use warnings;
5 use File::chdir;
6 use File::Path;
7 use PVE::Tools qw(run_command);
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::Cluster qw(cfs_register_file);
10
11 use Data::Dumper;
12
13 use base qw(PVE::SectionConfig);
14
15 cfs_register_file ('storage.cfg',
16 sub { __PACKAGE__->parse_config(@_); },
17 sub { __PACKAGE__->write_config(@_); });
18
19
20 my $defaultData = {
21 propertyList => {
22 type => { description => "Storage type." },
23 storage => get_standard_option('pve-storage-id',
24 { completion => \&PVE::Storage::complete_storage }),
25 nodes => get_standard_option('pve-node-list', { optional => 1 }),
26 content => {
27 description => "Allowed content types.\n\nNOTE: the value " .
28 "'rootdir' is used for Containers, and value 'images' for VMs.\n",
29 type => 'string', format => 'pve-storage-content-list',
30 optional => 1,
31 completion => \&PVE::Storage::complete_content_type,
32 },
33 disable => {
34 description => "Flag to disable the storage.",
35 type => 'boolean',
36 optional => 1,
37 },
38 maxfiles => {
39 description => "Maximal number of backup files per VM. Use '0' for unlimted.",
40 type => 'integer',
41 minimum => 0,
42 optional => 1,
43 },
44 shared => {
45 description => "Mark storage as shared.",
46 type => 'boolean',
47 optional => 1,
48 },
49 'format' => {
50 description => "Default image format.",
51 type => 'string', format => 'pve-storage-format',
52 optional => 1,
53 },
54 },
55 };
56
57 sub content_hash_to_string {
58 my $hash = shift;
59
60 my @cta;
61 foreach my $ct (keys %$hash) {
62 push @cta, $ct if $hash->{$ct};
63 }
64
65 return join(',', @cta);
66 }
67
68 sub valid_content_types {
69 my ($type) = @_;
70
71 my $def = $defaultData->{plugindata}->{$type};
72
73 return {} if !$def;
74
75 return $def->{content}->[0];
76 }
77
78 sub default_format {
79 my ($scfg) = @_;
80
81 my $type = $scfg->{type};
82 my $def = $defaultData->{plugindata}->{$type};
83
84 my $def_format = 'raw';
85 my $valid_formats = [ $def_format ];
86
87 if (defined($def->{format})) {
88 $def_format = $scfg->{format} || $def->{format}->[1];
89 $valid_formats = [ sort keys %{$def->{format}->[0]} ];
90 }
91
92 return wantarray ? ($def_format, $valid_formats) : $def_format;
93 }
94
95 PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
96 sub verify_path {
97 my ($path, $noerr) = @_;
98
99 # fixme: exclude more shell meta characters?
100 # we need absolute paths
101 if ($path !~ m|^/[^;\(\)]+|) {
102 return undef if $noerr;
103 die "value does not look like a valid absolute path\n";
104 }
105 return $path;
106 }
107
108 PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
109 sub verify_server {
110 my ($server, $noerr) = @_;
111
112 if (!(PVE::JSONSchema::pve_verify_ip($server, 1) ||
113 PVE::JSONSchema::pve_verify_dns_name($server, 1)))
114 {
115 return undef if $noerr;
116 die "value does not look like a valid server name or IP address\n";
117 }
118 return $server;
119 }
120
121 PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name);
122 sub parse_lvm_name {
123 my ($name, $noerr) = @_;
124
125 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
126 return undef if $noerr;
127 die "lvm name '$name' contains illegal characters\n";
128 }
129
130 return $name;
131 }
132
133 # fixme: do we need this
134 #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
135 #sub verify_portal {
136 # my ($portal, $noerr) = @_;
137 #
138 # # IP with optional port
139 # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
140 # return undef if $noerr;
141 # die "value does not look like a valid portal address\n";
142 # }
143 # return $portal;
144 #}
145
146 PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns);
147 sub verify_portal_dns {
148 my ($portal, $noerr) = @_;
149
150 # IP or DNS name with optional port
151 if (!PVE::Tools::parse_host_and_port($portal)) {
152 return undef if $noerr;
153 die "value does not look like a valid portal address\n";
154 }
155 return $portal;
156 }
157
158 PVE::JSONSchema::register_format('pve-storage-content', \&verify_content);
159 sub verify_content {
160 my ($ct, $noerr) = @_;
161
162 my $valid_content = valid_content_types('dir'); # dir includes all types
163
164 if (!$valid_content->{$ct}) {
165 return undef if $noerr;
166 die "invalid content type '$ct'\n";
167 }
168
169 return $ct;
170 }
171
172 PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
173 sub verify_format {
174 my ($fmt, $noerr) = @_;
175
176 if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) {
177 return undef if $noerr;
178 die "invalid format '$fmt'\n";
179 }
180
181 return $fmt;
182 }
183
184 PVE::JSONSchema::register_format('pve-storage-options', \&verify_options);
185 sub verify_options {
186 my ($value, $noerr) = @_;
187
188 # mount options (see man fstab)
189 if ($value !~ m/^\S+$/) {
190 return undef if $noerr;
191 die "invalid options '$value'\n";
192 }
193
194 return $value;
195 }
196
197 PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
198 sub parse_volume_id {
199 my ($volid, $noerr) = @_;
200
201 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
202 return wantarray ? ($1, $2) : $1;
203 }
204 return undef if $noerr;
205 die "unable to parse volume ID '$volid'\n";
206 }
207
208
209 sub private {
210 return $defaultData;
211 }
212
213 sub parse_section_header {
214 my ($class, $line) = @_;
215
216 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
217 my ($type, $storeid) = (lc($1), $2);
218 my $errmsg = undef; # set if you want to skip whole section
219 eval { PVE::JSONSchema::parse_storage_id($storeid); };
220 $errmsg = $@ if $@;
221 my $config = {}; # to return additional attributes
222 return ($type, $storeid, $errmsg, $config);
223 }
224 return undef;
225 }
226
227 sub decode_value {
228 my ($class, $type, $key, $value) = @_;
229
230 my $def = $defaultData->{plugindata}->{$type};
231
232 if ($key eq 'content') {
233 my $valid_content = $def->{content}->[0];
234
235 my $res = {};
236
237 foreach my $c (PVE::Tools::split_list($value)) {
238 if (!$valid_content->{$c}) {
239 warn "storage does not support content type '$c'\n";
240 next;
241 }
242 $res->{$c} = 1;
243 }
244
245 if ($res->{none} && scalar (keys %$res) > 1) {
246 die "unable to combine 'none' with other content types\n";
247 }
248
249 return $res;
250 } elsif ($key eq 'format') {
251 my $valid_formats = $def->{format}->[0];
252
253 if (!$valid_formats->{$value}) {
254 warn "storage does not support format '$value'\n";
255 next;
256 }
257
258 return $value;
259 } elsif ($key eq 'nodes') {
260 my $res = {};
261
262 foreach my $node (PVE::Tools::split_list($value)) {
263 if (PVE::JSONSchema::pve_verify_node_name($node)) {
264 $res->{$node} = 1;
265 }
266 }
267
268 # fixme:
269 # no node restrictions for local storage
270 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
271 # die "storage '$storeid' does not allow node restrictions\n";
272 #}
273
274 return $res;
275 }
276
277 return $value;
278 }
279
280 sub encode_value {
281 my ($class, $type, $key, $value) = @_;
282
283 if ($key eq 'nodes') {
284 return join(',', keys(%$value));
285 } elsif ($key eq 'content') {
286 my $res = content_hash_to_string($value) || 'none';
287 return $res;
288 }
289
290 return $value;
291 }
292
293 sub parse_config {
294 my ($class, $filename, $raw) = @_;
295
296 my $cfg = $class->SUPER::parse_config($filename, $raw);
297 my $ids = $cfg->{ids};
298
299 # make sure we have a reasonable 'local:' storage
300 # we want 'local' to be always the same 'type' (on all cluster nodes)
301 if (!$ids->{local} || $ids->{local}->{type} ne 'dir' ||
302 ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) {
303 $ids->{local} = {
304 type => 'dir',
305 priority => 0, # force first entry
306 path => '/var/lib/vz',
307 maxfiles => 0,
308 content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1},
309 };
310 }
311
312 # make sure we have a path
313 $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path};
314
315 # remove node restrictions for local storage
316 delete($ids->{local}->{nodes});
317
318 foreach my $storeid (keys %$ids) {
319 my $d = $ids->{$storeid};
320 my $type = $d->{type};
321
322 my $def = $defaultData->{plugindata}->{$type};
323
324 if ($def->{content}) {
325 $d->{content} = $def->{content}->[1] if !$d->{content};
326 }
327
328 if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'glusterfs' || $type eq 'zfs' || $type eq 'drbd') {
329 $d->{shared} = 1;
330 }
331 }
332
333 return $cfg;
334 }
335
336 # Storage implementation
337
338 sub cluster_lock_storage {
339 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
340
341 my $res;
342 if (!$shared) {
343 my $lockid = "pve-storage-$storeid";
344 my $lockdir = "/var/lock/pve-manager";
345 mkdir $lockdir;
346 $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
347 die $@ if $@;
348 } else {
349 $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
350 die $@ if $@;
351 }
352 return $res;
353 }
354
355 sub parse_name_dir {
356 my $name = shift;
357
358 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
359 return ($1, $3, $2); # (name, format, isBase)
360 }
361
362 die "unable to parse volume filename '$name'\n";
363 }
364
365 sub parse_volname {
366 my ($class, $volname) = @_;
367
368 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
369 my ($basedvmid, $basename) = ($1, $2);
370 parse_name_dir($basename);
371 my ($vmid, $name) = ($3, $4);
372 my (undef, $format, $isBase) = parse_name_dir($name);
373 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
374 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
375 my ($vmid, $name) = ($1, $2);
376 my (undef, $format, $isBase) = parse_name_dir($name);
377 return ('images', $name, $vmid, undef, undef, $isBase, $format);
378 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
379 return ('iso', $1);
380 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
381 return ('vztmpl', $1);
382 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
383 return ('rootdir', $1, $1);
384 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
385 my $fn = $1;
386 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
387 return ('backup', $fn, $2);
388 }
389 return ('backup', $fn);
390 }
391
392 die "unable to parse directory volume name '$volname'\n";
393 }
394
395 my $vtype_subdirs = {
396 images => 'images',
397 rootdir => 'private',
398 iso => 'template/iso',
399 vztmpl => 'template/cache',
400 backup => 'dump',
401 };
402
403 sub get_subdir {
404 my ($class, $scfg, $vtype) = @_;
405
406 my $path = $scfg->{path};
407
408 die "storage definintion has no path\n" if !$path;
409
410 my $subdir = $vtype_subdirs->{$vtype};
411
412 die "unknown vtype '$vtype'\n" if !defined($subdir);
413
414 return "$path/$subdir";
415 }
416
417 sub filesystem_path {
418 my ($class, $scfg, $volname, $snapname) = @_;
419
420 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
421 $class->parse_volname($volname);
422
423 # Note: qcow2/qed has internal snapshot, so path is always
424 # the same (with or without snapshot => same file).
425 die "can't snapshot this image format\n"
426 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
427
428 my $dir = $class->get_subdir($scfg, $vtype);
429
430 $dir .= "/$vmid" if $vtype eq 'images';
431
432 my $path = "$dir/$name";
433
434 return wantarray ? ($path, $vmid, $vtype) : $path;
435 }
436
437 sub path {
438 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
439
440 return $class->filesystem_path($scfg, $volname, $snapname);
441 }
442
443 sub create_base {
444 my ($class, $storeid, $scfg, $volname) = @_;
445
446 # this only works for file based storage types
447 die "storage definition has no path\n" if !$scfg->{path};
448
449 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
450 $class->parse_volname($volname);
451
452 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
453
454 die "create_base not possible with base image\n" if $isBase;
455
456 my $path = $class->filesystem_path($scfg, $volname);
457
458 my ($size, undef, $used, $parent) = file_size_info($path);
459 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
460
461 die "volname '$volname' contains wrong information about parent\n"
462 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
463
464 my $newname = $name;
465 $newname =~ s/^vm-/base-/;
466
467 my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" :
468 "$vmid/$newname";
469
470 my $newpath = $class->filesystem_path($scfg, $newvolname);
471
472 die "file '$newpath' already exists\n" if -f $newpath;
473
474 rename($path, $newpath) ||
475 die "rename '$path' to '$newpath' failed - $!\n";
476
477 # We try to protect base volume
478
479 chmod(0444, $newpath); # nobody should write anything
480
481 # also try to set immutable flag
482 eval { run_command(['/usr/bin/chattr', '+i', $newpath]); };
483 warn $@ if $@;
484
485 return $newvolname;
486 }
487
488 my $find_free_diskname = sub {
489 my ($imgdir, $vmid, $fmt) = @_;
490
491 my $disk_ids = {};
492 PVE::Tools::dir_glob_foreach($imgdir,
493 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
494 sub {
495 my ($fn, $type, $disk) = @_;
496 $disk_ids->{$disk} = 1;
497 });
498
499 for (my $i = 1; $i < 100; $i++) {
500 if (!$disk_ids->{$i}) {
501 return "vm-$vmid-disk-$i.$fmt";
502 }
503 }
504
505 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
506 };
507
508 sub clone_image {
509 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
510
511 # this only works for file based storage types
512 die "storage definintion has no path\n" if !$scfg->{path};
513
514 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
515 $class->parse_volname($volname);
516
517 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
518
519 die "this storage type does not support clone_image on snapshot\n" if $snap;
520
521 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
522
523 die "clone_image only works on base images\n" if !$isBase;
524
525 my $imagedir = $class->get_subdir($scfg, 'images');
526 $imagedir .= "/$vmid";
527
528 mkpath $imagedir;
529
530 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
531
532 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
533
534 my $newvol = "$basevmid/$basename/$vmid/$name";
535
536 my $path = $class->filesystem_path($scfg, $newvol);
537
538 # Note: we use relative paths, so we need to call chdir before qemu-img
539 eval {
540 local $CWD = $imagedir;
541
542 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
543 '-f', 'qcow2', $path];
544
545 run_command($cmd);
546 };
547 my $err = $@;
548
549 die $err if $err;
550
551 return $newvol;
552 }
553
554 sub alloc_image {
555 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
556
557 my $imagedir = $class->get_subdir($scfg, 'images');
558 $imagedir .= "/$vmid";
559
560 mkpath $imagedir;
561
562 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
563
564 my (undef, $tmpfmt) = parse_name_dir($name);
565
566 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
567 if $tmpfmt ne $fmt;
568
569 my $path = "$imagedir/$name";
570
571 die "disk image '$path' already exists\n" if -e $path;
572
573 if ($fmt eq 'subvol') {
574 # only allow this if size = 0, so that user knows what he is doing
575 die "storage does not support subvol quotas\n" if $size != 0;
576
577 my $old_umask = umask(0022);
578 my $err;
579 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
580 umask $old_umask;
581 die $err if $err;
582 } else {
583 my $cmd = ['/usr/bin/qemu-img', 'create'];
584
585 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
586
587 push @$cmd, '-f', $fmt, $path, "${size}K";
588
589 run_command($cmd, errmsg => "unable to create image");
590 }
591
592 return "$vmid/$name";
593 }
594
595 sub free_image {
596 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
597
598 my $path = $class->filesystem_path($scfg, $volname);
599
600 if ($isBase) {
601 # try to remove immutable flag
602 eval { run_command(['/usr/bin/chattr', '-i', $path]); };
603 warn $@ if $@;
604 }
605
606 if (defined($format) && ($format eq 'subvol')) {
607 File::Path::remove_tree($path);
608 } else {
609
610 if (! -f $path) {
611 warn "disk image '$path' does not exists\n";
612 return undef;
613 }
614
615 unlink($path) || die "unlink '$path' failed - $!\n";
616 }
617
618 return undef;
619 }
620
621 sub file_size_info {
622 my ($filename, $timeout) = @_;
623
624 if (-d $filename) {
625 return wantarray ? (0, 'subvol', 0, undef) : 1;
626 }
627
628 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
629
630 my $format;
631 my $parent;
632 my $size = 0;
633 my $used = 0;
634
635 eval {
636 run_command($cmd, timeout => $timeout, outfunc => sub {
637 my $line = shift;
638 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
639 $format = $1;
640 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
641 $parent = $1;
642 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
643 $size = int($1);
644 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
645 $used = $1;
646 my $u = $3;
647
648 $used *= 1024 if $u eq 'K';
649 $used *= (1024*1024) if $u eq 'M';
650 $used *= (1024*1024*1024) if $u eq 'G';
651 $used *= (1024*1024*1024*1024) if $u eq 'T';
652
653 $used = int($used);
654 }
655 });
656 };
657
658 return wantarray ? ($size, $format, $used, $parent) : $size;
659 }
660
661 sub volume_size_info {
662 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
663 my $path = $class->filesystem_path($scfg, $volname);
664 return file_size_info($path, $timeout);
665
666 }
667
668 sub volume_resize {
669 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
670
671 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
672
673 return 1 if $running;
674
675 my $path = $class->filesystem_path($scfg, $volname);
676
677 my $format = ($class->parse_volname($volname))[6];
678
679 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
680
681 run_command($cmd, timeout => 10);
682
683 return undef;
684 }
685
686 sub volume_snapshot {
687 my ($class, $scfg, $storeid, $volname, $snap) = @_;
688
689 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
690
691 my $path = $class->filesystem_path($scfg, $volname);
692
693 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
694
695 run_command($cmd);
696
697 return undef;
698 }
699
700 sub volume_send {
701 my ($class, $scfg, $storeid, $volname, $ip, $snap,
702 $incremental_snap, $verbose, $limit, $target_path) = @_;
703
704 # implement in subclass
705 die "Volume_send is not implemented for $class";
706 }
707
708 sub volume_rollback_is_possible {
709 my ($class, $scfg, $storeid, $volname, $snap) = @_;
710
711 return 1;
712 }
713
714 sub volume_snapshot_rollback {
715 my ($class, $scfg, $storeid, $volname, $snap) = @_;
716
717 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
718
719 my $path = $class->filesystem_path($scfg, $volname);
720
721 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
722
723 run_command($cmd);
724
725 return undef;
726 }
727
728 sub volume_snapshot_delete {
729 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
730
731 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
732
733 return 1 if $running;
734
735 my $path = $class->filesystem_path($scfg, $volname);
736
737 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
738
739 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
740
741 run_command($cmd);
742
743 return undef;
744 }
745
746 sub volume_has_feature {
747 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
748
749 my $features = {
750 snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
751 clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
752 template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} },
753 copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
754 current => {qcow2 => 1, raw => 1, vmdk => 1},
755 snap => {qcow2 => 1} },
756 sparseinit => { base => {qcow2 => 1, raw => 1, vmdk => 1},
757 current => {qcow2 => 1, raw => 1, vmdk => 1} },
758 };
759
760 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
761 $class->parse_volname($volname);
762
763 my $key = undef;
764 if($snapname){
765 $key = 'snap';
766 }else{
767 $key = $isBase ? 'base' : 'current';
768 }
769
770 return 1 if defined($features->{$feature}->{$key}->{$format});
771
772 return undef;
773 }
774
775 sub list_images {
776 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
777
778 my $imagedir = $class->get_subdir($scfg, 'images');
779
780 my ($defFmt, $vaidFmts) = default_format($scfg);
781 my $fmts = join ('|', @$vaidFmts);
782
783 my $res = [];
784
785 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
786
787 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
788 $fn = $1; # untaint
789
790 my $owner = $2;
791 my $name = $3;
792
793 next if !$vollist && defined($vmid) && ($owner ne $vmid);
794
795 my ($size, $format, $used, $parent) = file_size_info($fn);
796 next if !($format && defined($size));
797
798 my $volid;
799 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
800 my ($basevmid, $basename) = ($1, $2);
801 $volid = "$storeid:$basevmid/$basename/$owner/$name";
802 } else {
803 $volid = "$storeid:$owner/$name";
804 }
805
806 if ($vollist) {
807 my $found = grep { $_ eq $volid } @$vollist;
808 next if !$found;
809 }
810
811 push @$res, {
812 volid => $volid, format => $format,
813 size => $size, vmid => $owner, used => $used, parent => $parent
814 };
815 }
816
817 return $res;
818 }
819
820 sub status {
821 my ($class, $storeid, $scfg, $cache) = @_;
822
823 my $path = $scfg->{path};
824
825 die "storage definintion has no path\n" if !$path;
826
827 my $timeout = 2;
828 my $res = PVE::Tools::df($path, $timeout);
829
830 return undef if !$res || !$res->{total};
831
832 return ($res->{total}, $res->{avail}, $res->{used}, 1);
833 }
834
835 sub volume_snapshot_list {
836 my ($class, $scfg, $storeid, $volname, $prefix, $ip) = @_;
837
838 # implement in subclass
839 die "Volume_snapshot_list is not implemented for $class";
840
841 # retrun an empty array if dataset does not exist.
842 }
843
844 sub activate_storage {
845 my ($class, $storeid, $scfg, $cache) = @_;
846
847 my $path = $scfg->{path};
848
849 die "storage definintion has no path\n" if !$path;
850
851 die "unable to activate storage '$storeid' - " .
852 "directory '$path' does not exist\n" if ! -d $path;
853
854 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
855
856 if (defined($scfg->{content})) {
857 foreach my $vtype (keys %$vtype_subdirs) {
858 # OpenVZMigrate uses backup (dump) dir
859 if (defined($scfg->{content}->{$vtype}) ||
860 ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) {
861 my $subdir = $class->get_subdir($scfg, $vtype);
862 mkpath $subdir if $subdir ne $path;
863 }
864 }
865 }
866 }
867
868 sub deactivate_storage {
869 my ($class, $storeid, $scfg, $cache) = @_;
870
871 # do nothing by default
872 }
873
874 sub activate_volume {
875 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
876
877 my $path = $class->filesystem_path($scfg, $volname, $snapname);
878
879 # check is volume exists
880 if ($scfg->{path}) {
881 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
882 } else {
883 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
884 }
885 }
886
887 sub deactivate_volume {
888 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
889
890 # do nothing by default
891 }
892
893 sub check_connection {
894 my ($class, $storeid, $scfg) = @_;
895 # do nothing by default
896 return 1;
897 }
898
899
900 1;