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