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