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