]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/Plugin.pm
new plugin architecture
[pve-storage.git] / PVE / Storage / Plugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::Plugin;
2
3use strict;
4use warnings;
5use File::Path;
6use PVE::Tools qw(run_command);
7use PVE::JSONSchema qw(get_standard_option);
8use PVE::Cluster qw(cfs_register_file);
9
10use Data::Dumper;
11
12use base qw(PVE::SectionConfig);
13
14cfs_register_file ('storage.cfg',
15 sub { __PACKAGE__->parse_config(@_); },
16 sub { __PACKAGE__->write_config(@_); });
17
18my $defaultData = {
19 propertyList => {
20 type => { description => "Storage type." },
21 storage => get_standard_option('pve-storage-id'),
22 nodes => get_standard_option('pve-node-list', { optional => 1 }),
23 content => {
24 description => "Allowed content types.",
25 type => 'string', format => 'pve-storage-content-list',
26 optional => 1,
27 },
28 disable => {
29 description => "Flag to disable the storage.",
30 type => 'boolean',
31 optional => 1,
32 },
33 maxfiles => {
34 description => "Maximal number of backup files per VM. Use '0' for unlimted.",
35 type => 'integer',
36 minimum => 0,
37 optional => 1,
38 },
39 shared => {
40 description => "Mark storage as shared.",
41 type => 'boolean',
42 optional => 1,
43 },
44 'format' => {
45 description => "Default Image format.",
46 type => 'string', format => 'pve-storage-format',
47 optional => 1,
48 },
49 },
50};
51
52sub content_hash_to_string {
53 my $hash = shift;
54
55 my @cta;
56 foreach my $ct (keys %$hash) {
57 push @cta, $ct if $hash->{$ct};
58 }
59
60 return join(',', @cta);
61}
62
63sub valid_content_types {
64 my ($type) = @_;
65
66 my $def = $defaultData->{plugindata}->{$type};
67
68 return {} if !$def;
69
70 return $def->{content}->[0];
71}
72
73sub default_format {
74 my ($scfg) = @_;
75
76 my $type = $scfg->{type};
77 my $def = $defaultData->{plugindata}->{$type};
78
79 my $def_format = 'raw';
80 my $valid_formats = [ $def_format ];
81
82 if (defined($def->{format})) {
83 $def_format = $scfg->{format} || $def->{format}->[1];
84 $valid_formats = [ sort keys %{$def->{format}->[0]} ];
85 }
86
87 return wantarray ? ($def_format, $valid_formats) : $def_format;
88}
89
90PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
91sub verify_path {
92 my ($path, $noerr) = @_;
93
94 # fixme: exclude more shell meta characters?
95 # we need absolute paths
96 if ($path !~ m|^/[^;\(\)]+|) {
97 return undef if $noerr;
98 die "value does not look like a valid absolute path\n";
99 }
100 return $path;
101}
102
103PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
104sub verify_server {
105 my ($server, $noerr) = @_;
106
107 # fixme: use better regex ?
108 # IP or DNS name
109 if ($server !~ m/^[[:alnum:]\-\.]+$/) {
110 return undef if $noerr;
111 die "value does not look like a valid server name or IP address\n";
112 }
113 return $server;
114}
115
116# fixme: do we need this
117#PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
118#sub verify_portal {
119# my ($portal, $noerr) = @_;
120#
121# # IP with optional port
122# if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
123# return undef if $noerr;
124# die "value does not look like a valid portal address\n";
125# }
126# return $portal;
127#}
128
129PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns);
130sub verify_portal_dns {
131 my ($portal, $noerr) = @_;
132
133 # IP or DNS name with optional port
134 if ($portal !~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[[:alnum:]\-\.]+)(:\d+)?$/) {
135 return undef if $noerr;
136 die "value does not look like a valid portal address\n";
137 }
138 return $portal;
139}
140
141PVE::JSONSchema::register_format('pve-storage-content', \&verify_content);
142sub verify_content {
143 my ($ct, $noerr) = @_;
144
145 my $valid_content = valid_content_types('dir'); # dir includes all types
146
147 if (!$valid_content->{$ct}) {
148 return undef if $noerr;
149 die "invalid content type '$ct'\n";
150 }
151
152 return $ct;
153}
154
155PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
156sub verify_format {
157 my ($fmt, $noerr) = @_;
158
159 if ($fmt !~ m/(raw|qcow2|vmdk)/) {
160 return undef if $noerr;
161 die "invalid format '$fmt'\n";
162 }
163
164 return $fmt;
165}
166
167PVE::JSONSchema::register_format('pve-storage-options', \&verify_options);
168sub verify_options {
169 my ($value, $noerr) = @_;
170
171 # mount options (see man fstab)
172 if ($value !~ m/^\S+$/) {
173 return undef if $noerr;
174 die "invalid options '$value'\n";
175 }
176
177 return $value;
178}
179
180
181sub private {
182 return $defaultData;
183}
184
185sub parse_section_header {
186 my ($class, $line) = @_;
187
188 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
189 my ($type, $storeid) = (lc($1), $2);
190 my $errmsg = undef; # set if you want to skip whole section
191 eval { PVE::JSONSchema::parse_storage_id($storeid); };
192 $errmsg = $@ if $@;
193 my $config = {}; # to return additional attributes
194 return ($type, $storeid, $errmsg, $config);
195 }
196 return undef;
197}
198
199sub decode_value {
200 my ($class, $type, $key, $value) = @_;
201
202 my $def = $defaultData->{plugindata}->{$type};
203
204 if ($key eq 'content') {
205 my $valid_content = $def->{content}->[0];
206
207 my $res = {};
208
209 foreach my $c (PVE::Tools::split_list($value)) {
210 if (!$valid_content->{$c}) {
211 die "storage does not support content type '$c'\n";
212 }
213 $res->{$c} = 1;
214 }
215
216 if ($res->{none} && scalar (keys %$res) > 1) {
217 die "unable to combine 'none' with other content types\n";
218 }
219
220 return $res;
221 } elsif ($key eq 'format') {
222 my $valid_formats = $def->{format}->[0];
223
224 if (!$valid_formats->{$value}) {
225 die "storage does not support format '$value'\n";
226 }
227
228 return $value;
229 } elsif ($key eq 'nodes') {
230 my $res = {};
231
232 foreach my $node (PVE::Tools::split_list($value)) {
233 if (PVE::JSONSchema::pve_verify_node_name($node)) {
234 $res->{$node} = 1;
235 }
236 }
237
238 # fixme:
239 # no node restrictions for local storage
240 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
241 # die "storage '$storeid' does not allow node restrictions\n";
242 #}
243
244 return $res;
245 }
246
247 return $value;
248}
249
250sub encode_value {
251 my ($class, $type, $key, $value) = @_;
252
253 if ($key eq 'nodes') {
254 return join(',', keys(%$value));
255 } elsif ($key eq 'content') {
256 my $res = content_hash_to_string($value) || 'none';
257 return $res;
258 }
259
260 return $value;
261}
262
263sub parse_config {
264 my ($class, $filename, $raw) = @_;
265
266 my $cfg = $class->SUPER::parse_config($filename, $raw);
267 my $ids = $cfg->{ids};
268
269 # make sure we have a reasonable 'local:' storage
270 # openvz expects things to be there
271 if (!$ids->{local} || $ids->{local}->{type} ne 'dir' ||
272 ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) {
273 $ids->{local} = {
274 type => 'dir',
275 priority => 0, # force first entry
276 path => '/var/lib/vz',
277 maxfiles => 0,
278 content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1},
279 };
280 }
281
282 # we always need this for OpenVZ
283 $ids->{local}->{content}->{rootdir} = 1;
284 $ids->{local}->{content}->{vztmpl} = 1;
285 delete ($ids->{local}->{disable});
286
287 # make sure we have a path
288 $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path};
289
290 # remove node restrictions for local storage
291 delete($ids->{local}->{nodes});
292
293 foreach my $storeid (keys %$ids) {
294 my $d = $ids->{$storeid};
295 my $type = $d->{type};
296
297 my $def = $defaultData->{plugindata}->{$type};
298
299 if ($def->{content}) {
300 $d->{content} = $def->{content}->[1] if !$d->{content};
301 }
302
303 if ($type eq 'iscsi' || $type eq 'nfs') {
304 $d->{shared} = 1;
305 }
306 }
307
308 return $cfg;
309}
310
311# Storage implementation
312
313sub cluster_lock_storage {
314 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
315
316 my $res;
317 if (!$shared) {
318 my $lockid = "pve-storage-$storeid";
319 my $lockdir = "/var/lock/pve-manager";
320 mkdir $lockdir;
321 $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
322 die $@ if $@;
323 } else {
324 $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
325 die $@ if $@;
326 }
327 return $res;
328}
329
330sub parse_name_dir {
331 my $name = shift;
332
333 if ($name =~ m!^([^/\s]+\.(raw|qcow2|vmdk))$!) {
334 return ($1, $2);
335 }
336
337 die "unable to parse volume filename '$name'\n";
338}
339
340sub parse_volname {
341 my ($class, $volname) = @_;
342
343 if ($volname =~ m!^(\d+)/(\S+)$!) {
344 my ($vmid, $name) = ($1, $2);
345 parse_name_dir($name);
346 return ('images', $name, $vmid);
347 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
348 return ('iso', $1);
349 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
350 return ('vztmpl', $1);
351 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
352 return ('rootdir', $1, $1);
353 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz)))$!) {
354 my $fn = $1;
355 if ($fn =~ m/^vzdump-(openvz|qemu)-(\d+)-.+/) {
356 return ('backup', $fn, $2);
357 }
358 return ('backup', $fn);
359 }
360
361 die "unable to parse directory volume name '$volname'\n";
362}
363
364my $vtype_subdirs = {
365 images => 'images',
366 rootdir => 'private',
367 iso => 'template/iso',
368 vztmpl => 'template/cache',
369 backup => 'dump',
370};
371
372sub get_subdir {
373 my ($class, $scfg, $vtype) = @_;
374
375 my $path = $scfg->{path};
376
377 die "storage definintion has no path\n" if !$path;
378
379 my $subdir = $vtype_subdirs->{$vtype};
380
381 die "unknown vtype '$vtype'\n" if !defined($subdir);
382
383 return "$path/$subdir";
384}
385
386sub path {
387 my ($class, $scfg, $volname) = @_;
388
389 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
390
391 my $dir = $class->get_subdir($scfg, $vtype);
392
393 $dir .= "/$vmid" if $vtype eq 'images';
394
395 my $path = "$dir/$name";
396
397 return wantarray ? ($path, $vmid, $vtype) : $path;
398}
399
400sub alloc_image {
401 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
402
403 my $imagedir = $class->get_subdir($scfg, 'images');
404 $imagedir .= "/$vmid";
405
406 mkpath $imagedir;
407
408 if (!$name) {
409 for (my $i = 1; $i < 100; $i++) {
410 my @gr = <$imagedir/vm-$vmid-disk-$i.*>;
411 if (!scalar(@gr)) {
412 $name = "vm-$vmid-disk-$i.$fmt";
413 last;
414 }
415 }
416 }
417
418 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
419 if !$name;
420
421 my (undef, $tmpfmt) = parse_name_dir($name);
422
423 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
424 if $tmpfmt ne $fmt;
425
426 my $path = "$imagedir/$name";
427
428 die "disk image '$path' already exists\n" if -e $path;
429
430 run_command("/usr/bin/qemu-img create -f $fmt '$path' ${size}K",
431 errmsg => "unable to create image");
432
433 return "$vmid/$name";
434}
435
436sub free_image {
437 my ($class, $storeid, $scfg, $volname) = @_;
438
439 my $path = $class->path($scfg, $volname);
440
441 if (! -f $path) {
442 warn "disk image '$path' does not exists\n";
443 } else {
444 unlink $path;
445 }
446
447 return undef;
448}
449
450sub file_size_info {
451 my ($filename, $timeout) = @_;
452
453 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
454
455 my $format;
456 my $size = 0;
457 my $used = 0;
458
459 eval {
460 run_command($cmd, timeout => $timeout, outfunc => sub {
461 my $line = shift;
462
463 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
464 $format = $1;
465 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
466 $size = int($1);
467 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
468 $used = $1;
469 my $u = $3;
470
471 $used *= 1024 if $u eq 'K';
472 $used *= (1024*1024) if $u eq 'M';
473 $used *= (1024*1024*1024) if $u eq 'G';
474 $used *= (1024*1024*1024*1024) if $u eq 'T';
475
476 $used = int($used);
477 }
478 });
479 };
480
481 return wantarray ? ($size, $format, $used) : $size;
482}
483
484sub list_images {
485 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
486
487 my $imagedir = $class->get_subdir($scfg, 'images');
488
489 my ($defFmt, $vaidFmts) = default_format($scfg);
490 my $fmts = join ('|', @$vaidFmts);
491
492 my $res = [];
493
494 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
495
496 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
497 $fn = $1; # untaint
498
499 my $owner = $2;
500 my $name = $3;
501 my $volid = "$storeid:$owner/$name";
502
503 if ($vollist) {
504 my $found = grep { $_ eq $volid } @$vollist;
505 next if !$found;
506 } else {
507 next if defined($vmid) && ($owner ne $vmid);
508 }
509
510 my ($size, $format, $used) = file_size_info($fn);
511
512 if ($format && $size) {
513 push @$res, {
514 volid => $volid, format => $format,
515 size => $size, vmid => $owner, used => $used };
516 }
517
518 }
519
520 return $res;
521}
522
523sub status {
524 my ($class, $storeid, $scfg, $cache) = @_;
525
526 my $path = $scfg->{path};
527
528 die "storage definintion has no path\n" if !$path;
529
530 my $timeout = 2;
531 my $res = PVE::Tools::df($path, $timeout);
532
533 return undef if !$res || !$res->{total};
534
535 return ($res->{total}, $res->{avail}, $res->{used}, 1);
536}
537
538sub activate_storage {
539 my ($class, $storeid, $scfg, $cache) = @_;
540
541 my $path = $scfg->{path};
542
543 die "storage definintion has no path\n" if !$path;
544
545 die "unable to activate storage '$storeid' - " .
546 "directory '$path' does not exist\n" if ! -d $path;
547
548 if (defined($scfg->{content})) {
549 foreach my $vtype (keys %$vtype_subdirs) {
550 next if !defined($scfg->{content}->{$vtype});
551 my $subdir = $class->get_subdir($scfg, $vtype);
552 mkpath $subdir if $subdir ne $path;
553 }
554 }
555}
556
557sub deactivate_storage {
558 my ($class, $storeid, $scfg, $cache) = @_;
559
560 # do nothing by default
561}
562
563sub activate_volume {
564 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
565
566 my $path = $class->path($scfg, $volname);
567
568 # check is volume exists
569 if ($scfg->{path}) {
570 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
571 } else {
572 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
573 }
574}
575
576sub deactivate_volume {
577 my ($class, $storeid, $scfg, $volname, $cache) = @_;
578
579 # do nothing by default
580}
581
5821;