]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/Plugin.pm
Added support for ZFS Storage Plugin
[pve-storage.git] / PVE / Storage / Plugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::Plugin;
2
3use strict;
4use warnings;
7fc619d5 5use File::chdir;
1dc01b9f
DM
6use File::Path;
7use PVE::Tools qw(run_command);
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::Cluster qw(cfs_register_file);
10
11use Data::Dumper;
12
13use base qw(PVE::SectionConfig);
14
045ae0a7 15cfs_register_file ('storage.cfg',
1dc01b9f
DM
16 sub { __PACKAGE__->parse_config(@_); },
17 sub { __PACKAGE__->write_config(@_); });
18
19my $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 },
045ae0a7 45 'format' => {
1dc01b9f
DM
46 description => "Default Image format.",
47 type => 'string', format => 'pve-storage-format',
48 optional => 1,
49 },
50 },
51};
52
53sub 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};
045ae0a7 59 }
1dc01b9f
DM
60
61 return join(',', @cta);
62}
63
64sub 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
74sub default_format {
75 my ($scfg) = @_;
76
77 my $type = $scfg->{type};
78 my $def = $defaultData->{plugindata}->{$type};
045ae0a7 79
1dc01b9f
DM
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 }
045ae0a7 87
1dc01b9f
DM
88 return wantarray ? ($def_format, $valid_formats) : $def_format;
89}
90
91PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
92sub 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
104PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
105sub verify_server {
106 my ($server, $noerr) = @_;
107
108 # fixme: use better regex ?
109 # IP or DNS name
110 if ($server !~ m/^[[:alnum:]\-\.]+$/) {
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
130PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns);
131sub 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
142PVE::JSONSchema::register_format('pve-storage-content', \&verify_content);
143sub verify_content {
144 my ($ct, $noerr) = @_;
145
146 my $valid_content = valid_content_types('dir'); # dir includes all types
045ae0a7 147
1dc01b9f
DM
148 if (!$valid_content->{$ct}) {
149 return undef if $noerr;
150 die "invalid content type '$ct'\n";
151 }
152
153 return $ct;
154}
155
156PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
157sub 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
168PVE::JSONSchema::register_format('pve-storage-options', \&verify_options);
169sub 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
a7f3d909
DM
181PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
182sub 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
1dc01b9f
DM
192
193sub private {
194 return $defaultData;
195}
196
197sub 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
211sub 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];
045ae0a7 218
1dc01b9f
DM
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;
045ae0a7 226 }
1dc01b9f
DM
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
262sub 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
275sub 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 }
045ae0a7 293
1dc01b9f
DM
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
4f914e6e 315 if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'nexenta' || $type eq 'glusterfs' || $type eq 'zfs') {
1dc01b9f
DM
316 $d->{shared} = 1;
317 }
318 }
319
320 return $cfg;
321}
322
323# Storage implementation
324
325sub 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;
045ae0a7 333 $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
1dc01b9f
DM
334 die $@ if $@;
335 } else {
336 $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
337 die $@ if $@;
045ae0a7 338 }
1dc01b9f
DM
339 return $res;
340}
341
342sub parse_name_dir {
343 my $name = shift;
344
2502b33b
DM
345 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) {
346 return ($1, $3, $2);
1dc01b9f
DM
347 }
348
349 die "unable to parse volume filename '$name'\n";
350}
351
352sub parse_volname {
353 my ($class, $volname) = @_;
354
2502b33b
DM
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+)$!) {
1dc01b9f 362 my ($vmid, $name) = ($1, $2);
2502b33b
DM
363 my (undef, undef, $isBase) = parse_name_dir($name);
364 return ('images', $name, $vmid, undef, undef, $isBase);
1dc01b9f
DM
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);
a22854e5 371 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
1dc01b9f
DM
372 my $fn = $1;
373 if ($fn =~ m/^vzdump-(openvz|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
045ae0a7 382my $vtype_subdirs = {
1dc01b9f
DM
383 images => 'images',
384 rootdir => 'private',
385 iso => 'template/iso',
386 vztmpl => 'template/cache',
387 backup => 'dump',
388};
389
390sub 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
045ae0a7 401 return "$path/$subdir";
1dc01b9f
DM
402}
403
08480ce7 404sub filesystem_path {
6bf56298 405 my ($class, $scfg, $volname, $storeid) = @_;
1dc01b9f
DM
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
08480ce7
DM
418sub path {
419 my ($class, $scfg, $volname, $storeid) = @_;
420
421 return $class->filesystem_path($scfg, $volname, $storeid);
422}
423
2502b33b
DM
424sub 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
1a3459ac 430 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
2502b33b
DM
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
08480ce7 437 my $path = $class->filesystem_path($scfg, $volname);
2502b33b
DM
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
08480ce7 451 my $newpath = $class->filesystem_path($scfg, $newvolname);
2502b33b
DM
452
453 die "file '$newpath' already exists\n" if -f $newpath;
454
1a3459ac 455 rename($path, $newpath) ||
2502b33b
DM
456 die "rename '$path' to '$newpath' failed - $!\n";
457
c803c396 458 # We try to protect base volume
2502b33b 459
c803c396
DM
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 $@;
1a3459ac 465
2502b33b
DM
466 return $newvolname;
467}
468
469my $find_free_diskname = sub {
470 my ($imgdir, $vmid, $fmt) = @_;
471
472 my $disk_ids = {};
1a3459ac 473 PVE::Tools::dir_glob_foreach($imgdir,
2502b33b
DM
474 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
475 sub {
1a3459ac 476 my ($fn, $type, $disk) = @_;
2502b33b
DM
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
489sub clone_image {
490 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
491
492 # this only works for file based storage types
493 die "storage definintion has no path\n" if !$scfg->{path};
494
1a3459ac 495 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
2502b33b
DM
496 $class->parse_volname($volname);
497
498 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
499
500 die "clone_image onyl works on base images\n" if !$isBase;
1dc01b9f
DM
501
502 my $imagedir = $class->get_subdir($scfg, 'images');
503 $imagedir .= "/$vmid";
504
505 mkpath $imagedir;
506
2502b33b
DM
507 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
508
509 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
510
511 my $newvol = "$basevmid/$basename/$vmid/$name";
512
08480ce7 513 my $path = $class->filesystem_path($scfg, $newvol);
2502b33b 514
1a3459ac 515 # Note: we use relative paths, so we need to call chdir before qemu-img
2502b33b 516 eval {
7fc619d5 517 local $CWD = $imagedir;
2502b33b 518
1a3459ac 519 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
2502b33b 520 '-f', 'qcow2', $path];
1a3459ac 521
2502b33b
DM
522 run_command($cmd);
523 };
524 my $err = $@;
1dc01b9f 525
2502b33b
DM
526 die $err if $err;
527
528 return $newvol;
529}
530
531sub alloc_image {
532 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
533
534 my $imagedir = $class->get_subdir($scfg, 'images');
535 $imagedir .= "/$vmid";
536
537 mkpath $imagedir;
538
539 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
1a3459ac 540
1dc01b9f
DM
541 my (undef, $tmpfmt) = parse_name_dir($name);
542
045ae0a7 543 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
1dc01b9f
DM
544 if $tmpfmt ne $fmt;
545
546 my $path = "$imagedir/$name";
547
548 die "disk image '$path' already exists\n" if -e $path;
549
045ae0a7
DM
550 my $cmd = ['/usr/bin/qemu-img', 'create'];
551
552 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
1e679ac5 553
045ae0a7
DM
554 push @$cmd, '-f', $fmt, $path, "${size}K";
555
556 run_command($cmd, errmsg => "unable to create image");
1dc01b9f
DM
557
558 return "$vmid/$name";
559}
560
561sub free_image {
32437ed2 562 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
1dc01b9f 563
08480ce7 564 my $path = $class->filesystem_path($scfg, $volname);
1dc01b9f
DM
565
566 if (! -f $path) {
567 warn "disk image '$path' does not exists\n";
a7f3d909
DM
568 return undef;
569 }
570
a7f3d909 571 if ($isBase) {
c803c396
DM
572 # try to remove immutable flag
573 eval { run_command(['/usr/bin/chattr', '-i', $path]); };
574 warn $@ if $@;
1dc01b9f
DM
575 }
576
c803c396 577 unlink($path) || die "unlink '$path' failed - $!\n";
a7f3d909 578
1dc01b9f
DM
579 return undef;
580}
581
582sub file_size_info {
583 my ($filename, $timeout) = @_;
584
585 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
586
587 my $format;
73b7847e 588 my $parent;
1dc01b9f
DM
589 my $size = 0;
590 my $used = 0;
591
592 eval {
593 run_command($cmd, timeout => $timeout, outfunc => sub {
594 my $line = shift;
1dc01b9f
DM
595 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
596 $format = $1;
73b7847e
AD
597 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
598 $parent = $1;
1dc01b9f
DM
599 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
600 $size = int($1);
601 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
602 $used = $1;
603 my $u = $3;
604
605 $used *= 1024 if $u eq 'K';
606 $used *= (1024*1024) if $u eq 'M';
607 $used *= (1024*1024*1024) if $u eq 'G';
608 $used *= (1024*1024*1024*1024) if $u eq 'T';
609
610 $used = int($used);
611 }
612 });
613 };
614
73b7847e 615 return wantarray ? ($size, $format, $used, $parent) : $size;
1dc01b9f
DM
616}
617
e47e548e
AD
618sub volume_size_info {
619 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
08480ce7 620 my $path = $class->filesystem_path($scfg, $volname);
e47e548e
AD
621 return file_size_info($path, $timeout);
622
623}
624
81f5058c
AD
625sub volume_resize {
626 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
627
628 die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/;
629
630 return 1 if $running;
631
08480ce7 632 my $path = $class->filesystem_path($scfg, $volname);
81f5058c
AD
633
634 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
635
1059cc99 636 run_command($cmd, timeout => 10);
81f5058c
AD
637
638 return undef;
639}
640
7dcb0697
AD
641sub volume_snapshot {
642 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
643
644 die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
645
646 return 1 if $running;
647
08480ce7 648 my $path = $class->filesystem_path($scfg, $volname);
7dcb0697
AD
649
650 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
651
3f13fd7d 652 run_command($cmd);
7dcb0697
AD
653
654 return undef;
655}
656
41dffa85
AD
657sub volume_snapshot_rollback {
658 my ($class, $scfg, $storeid, $volname, $snap) = @_;
659
660 die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/;
661
08480ce7 662 my $path = $class->filesystem_path($scfg, $volname);
41dffa85
AD
663
664 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
665
3f13fd7d 666 run_command($cmd);
41dffa85
AD
667
668 return undef;
669}
670
6000a061
AD
671sub volume_snapshot_delete {
672 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
673
674 die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/;
675
676 return 1 if $running;
677
08480ce7 678 my $path = $class->filesystem_path($scfg, $volname);
6000a061
AD
679
680 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
681
3f13fd7d 682 run_command($cmd);
6000a061
AD
683
684 return undef;
685}
686
f884fe11
AD
687sub volume_has_feature {
688 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
689
690 my $features = {
5649ccfe
AD
691 snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
692 clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
693 template => { current => {qcow2 => 1, raw => 1, vmdk => 1} },
694 copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
22b8cf97
AD
695 current => {qcow2 => 1, raw => 1, vmdk => 1},
696 snap => {qcow2 => 1} },
f884fe11
AD
697 };
698
dc4f2cb3
AD
699 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
700 $class->parse_volname($volname);
701
702 my (undef, $format) = parse_name_dir($name);
f884fe11 703
dc4f2cb3
AD
704 my $key = undef;
705 if($snapname){
2c5a7097 706 $key = 'snap';
dc4f2cb3
AD
707 }else{
708 $key = $isBase ? 'base' : 'current';
f884fe11 709 }
dc4f2cb3
AD
710
711 return 1 if defined($features->{$feature}->{$key}->{$format});
712
f884fe11
AD
713 return undef;
714}
715
1dc01b9f
DM
716sub list_images {
717 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
718
719 my $imagedir = $class->get_subdir($scfg, 'images');
720
721 my ($defFmt, $vaidFmts) = default_format($scfg);
045ae0a7 722 my $fmts = join ('|', @$vaidFmts);
1dc01b9f
DM
723
724 my $res = [];
725
726 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
727
728 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
729 $fn = $1; # untaint
730
731 my $owner = $2;
732 my $name = $3;
1dc01b9f 733
2502b33b 734 next if !$vollist && defined($vmid) && ($owner ne $vmid);
1dc01b9f 735
73b7847e 736 my ($size, $format, $used, $parent) = file_size_info($fn);
2502b33b
DM
737 next if !($format && $size);
738
739 my $volid;
740 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
741 my ($basevmid, $basename) = ($1, $2);
742 $volid = "$storeid:$basevmid/$basename/$owner/$name";
743 } else {
744 $volid = "$storeid:$owner/$name";
745 }
1dc01b9f 746
2502b33b
DM
747 if ($vollist) {
748 my $found = grep { $_ eq $volid } @$vollist;
749 next if !$found;
1dc01b9f
DM
750 }
751
2502b33b
DM
752 push @$res, {
753 volid => $volid, format => $format,
1a3459ac 754 size => $size, vmid => $owner, used => $used, parent => $parent
2502b33b 755 };
1dc01b9f
DM
756 }
757
758 return $res;
759}
760
761sub status {
762 my ($class, $storeid, $scfg, $cache) = @_;
763
764 my $path = $scfg->{path};
765
766 die "storage definintion has no path\n" if !$path;
045ae0a7 767
1dc01b9f
DM
768 my $timeout = 2;
769 my $res = PVE::Tools::df($path, $timeout);
770
771 return undef if !$res || !$res->{total};
772
773 return ($res->{total}, $res->{avail}, $res->{used}, 1);
774}
775
776sub activate_storage {
777 my ($class, $storeid, $scfg, $cache) = @_;
778
779 my $path = $scfg->{path};
780
781 die "storage definintion has no path\n" if !$path;
782
783 die "unable to activate storage '$storeid' - " .
784 "directory '$path' does not exist\n" if ! -d $path;
785
786 if (defined($scfg->{content})) {
787 foreach my $vtype (keys %$vtype_subdirs) {
6bcc16d7
DM
788 # OpenVZMigrate uses backup (dump) dir
789 if (defined($scfg->{content}->{$vtype}) ||
790 ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) {
791 my $subdir = $class->get_subdir($scfg, $vtype);
792 mkpath $subdir if $subdir ne $path;
793 }
1dc01b9f
DM
794 }
795 }
796}
797
798sub deactivate_storage {
799 my ($class, $storeid, $scfg, $cache) = @_;
800
801 # do nothing by default
802}
803
804sub activate_volume {
805 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
806
08480ce7 807 my $path = $class->filesystem_path($scfg, $volname);
1dc01b9f
DM
808
809 # check is volume exists
810 if ($scfg->{path}) {
811 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
812 } else {
813 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
814 }
815}
816
817sub deactivate_volume {
818 my ($class, $storeid, $scfg, $volname, $cache) = @_;
819
820 # do nothing by default
821}
822
c9eeac01
AD
823sub check_connection {
824 my ($class, $storeid, $scfg) = @_;
825 # do nothing by default
826 return 1;
827}
828
e47e548e 829
1dc01b9f 8301;