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