]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/Plugin.pm
volume_snapshot_list: remove $ip parameter
[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_rollback_is_possible {
701 my ($class, $scfg, $storeid, $volname, $snap) = @_;
702
703 return 1;
704 }
705
706 sub volume_snapshot_rollback {
707 my ($class, $scfg, $storeid, $volname, $snap) = @_;
708
709 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
710
711 my $path = $class->filesystem_path($scfg, $volname);
712
713 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
714
715 run_command($cmd);
716
717 return undef;
718 }
719
720 sub volume_snapshot_delete {
721 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
722
723 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
724
725 return 1 if $running;
726
727 my $path = $class->filesystem_path($scfg, $volname);
728
729 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
730
731 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
732
733 run_command($cmd);
734
735 return undef;
736 }
737
738 sub volume_has_feature {
739 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
740
741 my $features = {
742 snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
743 clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
744 template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} },
745 copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
746 current => {qcow2 => 1, raw => 1, vmdk => 1},
747 snap => {qcow2 => 1} },
748 sparseinit => { base => {qcow2 => 1, raw => 1, vmdk => 1},
749 current => {qcow2 => 1, raw => 1, vmdk => 1} },
750 };
751
752 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
753 $class->parse_volname($volname);
754
755 my $key = undef;
756 if($snapname){
757 $key = 'snap';
758 }else{
759 $key = $isBase ? 'base' : 'current';
760 }
761
762 return 1 if defined($features->{$feature}->{$key}->{$format});
763
764 return undef;
765 }
766
767 sub list_images {
768 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
769
770 my $imagedir = $class->get_subdir($scfg, 'images');
771
772 my ($defFmt, $vaidFmts) = default_format($scfg);
773 my $fmts = join ('|', @$vaidFmts);
774
775 my $res = [];
776
777 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
778
779 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
780 $fn = $1; # untaint
781
782 my $owner = $2;
783 my $name = $3;
784
785 next if !$vollist && defined($vmid) && ($owner ne $vmid);
786
787 my ($size, $format, $used, $parent) = file_size_info($fn);
788 next if !($format && defined($size));
789
790 my $volid;
791 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
792 my ($basevmid, $basename) = ($1, $2);
793 $volid = "$storeid:$basevmid/$basename/$owner/$name";
794 } else {
795 $volid = "$storeid:$owner/$name";
796 }
797
798 if ($vollist) {
799 my $found = grep { $_ eq $volid } @$vollist;
800 next if !$found;
801 }
802
803 push @$res, {
804 volid => $volid, format => $format,
805 size => $size, vmid => $owner, used => $used, parent => $parent
806 };
807 }
808
809 return $res;
810 }
811
812 sub status {
813 my ($class, $storeid, $scfg, $cache) = @_;
814
815 my $path = $scfg->{path};
816
817 die "storage definintion has no path\n" if !$path;
818
819 my $timeout = 2;
820 my $res = PVE::Tools::df($path, $timeout);
821
822 return undef if !$res || !$res->{total};
823
824 return ($res->{total}, $res->{avail}, $res->{used}, 1);
825 }
826
827 sub volume_snapshot_list {
828 my ($class, $scfg, $storeid, $volname, $prefix) = @_;
829
830 # implement in subclass
831 die "Volume_snapshot_list is not implemented for $class";
832
833 # retrun an empty array if dataset does not exist.
834 }
835
836 sub activate_storage {
837 my ($class, $storeid, $scfg, $cache) = @_;
838
839 my $path = $scfg->{path};
840
841 die "storage definintion has no path\n" if !$path;
842
843 die "unable to activate storage '$storeid' - " .
844 "directory '$path' does not exist\n" if ! -d $path;
845
846 return if defined($scfg->{mkdir}) && !$scfg->{mkdir};
847
848 if (defined($scfg->{content})) {
849 foreach my $vtype (keys %$vtype_subdirs) {
850 # OpenVZMigrate uses backup (dump) dir
851 if (defined($scfg->{content}->{$vtype}) ||
852 ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) {
853 my $subdir = $class->get_subdir($scfg, $vtype);
854 mkpath $subdir if $subdir ne $path;
855 }
856 }
857 }
858 }
859
860 sub deactivate_storage {
861 my ($class, $storeid, $scfg, $cache) = @_;
862
863 # do nothing by default
864 }
865
866 sub activate_volume {
867 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
868
869 my $path = $class->filesystem_path($scfg, $volname, $snapname);
870
871 # check is volume exists
872 if ($scfg->{path}) {
873 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
874 } else {
875 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
876 }
877 }
878
879 sub deactivate_volume {
880 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
881
882 # do nothing by default
883 }
884
885 sub check_connection {
886 my ($class, $storeid, $scfg) = @_;
887 # do nothing by default
888 return 1;
889 }
890
891
892 1;