]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::Plugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use File::Path; | |
6 | use PVE::Tools qw(run_command); | |
7 | use PVE::JSONSchema qw(get_standard_option); | |
8 | use PVE::Cluster qw(cfs_register_file); | |
9 | ||
10 | use Data::Dumper; | |
11 | ||
12 | use base qw(PVE::SectionConfig); | |
13 | ||
045ae0a7 | 14 | cfs_register_file ('storage.cfg', |
1dc01b9f DM |
15 | sub { __PACKAGE__->parse_config(@_); }, |
16 | sub { __PACKAGE__->write_config(@_); }); | |
17 | ||
18 | my $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 | }, | |
045ae0a7 | 44 | 'format' => { |
1dc01b9f DM |
45 | description => "Default Image format.", |
46 | type => 'string', format => 'pve-storage-format', | |
47 | optional => 1, | |
48 | }, | |
49 | }, | |
50 | }; | |
51 | ||
52 | sub 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}; | |
045ae0a7 | 58 | } |
1dc01b9f DM |
59 | |
60 | return join(',', @cta); | |
61 | } | |
62 | ||
63 | sub 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 | ||
73 | sub default_format { | |
74 | my ($scfg) = @_; | |
75 | ||
76 | my $type = $scfg->{type}; | |
77 | my $def = $defaultData->{plugindata}->{$type}; | |
045ae0a7 | 78 | |
1dc01b9f DM |
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 | } | |
045ae0a7 | 86 | |
1dc01b9f DM |
87 | return wantarray ? ($def_format, $valid_formats) : $def_format; |
88 | } | |
89 | ||
90 | PVE::JSONSchema::register_format('pve-storage-path', \&verify_path); | |
91 | sub 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 | ||
103 | PVE::JSONSchema::register_format('pve-storage-server', \&verify_server); | |
104 | sub 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 | ||
129 | PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns); | |
130 | sub 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 | ||
141 | PVE::JSONSchema::register_format('pve-storage-content', \&verify_content); | |
142 | sub verify_content { | |
143 | my ($ct, $noerr) = @_; | |
144 | ||
145 | my $valid_content = valid_content_types('dir'); # dir includes all types | |
045ae0a7 | 146 | |
1dc01b9f DM |
147 | if (!$valid_content->{$ct}) { |
148 | return undef if $noerr; | |
149 | die "invalid content type '$ct'\n"; | |
150 | } | |
151 | ||
152 | return $ct; | |
153 | } | |
154 | ||
155 | PVE::JSONSchema::register_format('pve-storage-format', \&verify_format); | |
156 | sub 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 | ||
167 | PVE::JSONSchema::register_format('pve-storage-options', \&verify_options); | |
168 | sub 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 | ||
181 | sub private { | |
182 | return $defaultData; | |
183 | } | |
184 | ||
185 | sub 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 | ||
199 | sub 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]; | |
045ae0a7 | 206 | |
1dc01b9f DM |
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; | |
045ae0a7 | 214 | } |
1dc01b9f DM |
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 | ||
250 | sub 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 | ||
263 | sub 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 | } | |
045ae0a7 | 281 | |
1dc01b9f DM |
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 | ||
40cd7d27 | 303 | if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'nexenta' ) { |
1dc01b9f DM |
304 | $d->{shared} = 1; |
305 | } | |
306 | } | |
307 | ||
308 | return $cfg; | |
309 | } | |
310 | ||
311 | # Storage implementation | |
312 | ||
313 | sub 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; | |
045ae0a7 | 321 | $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param); |
1dc01b9f DM |
322 | die $@ if $@; |
323 | } else { | |
324 | $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param); | |
325 | die $@ if $@; | |
045ae0a7 | 326 | } |
1dc01b9f DM |
327 | return $res; |
328 | } | |
329 | ||
330 | sub 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 | ||
340 | sub 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); | |
a22854e5 | 353 | } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) { |
1dc01b9f DM |
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 | ||
045ae0a7 | 364 | my $vtype_subdirs = { |
1dc01b9f DM |
365 | images => 'images', |
366 | rootdir => 'private', | |
367 | iso => 'template/iso', | |
368 | vztmpl => 'template/cache', | |
369 | backup => 'dump', | |
370 | }; | |
371 | ||
372 | sub 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 | ||
045ae0a7 | 383 | return "$path/$subdir"; |
1dc01b9f DM |
384 | } |
385 | ||
386 | sub path { | |
6bf56298 | 387 | my ($class, $scfg, $volname, $storeid) = @_; |
1dc01b9f DM |
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 | ||
400 | sub 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 | ||
045ae0a7 | 423 | die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n" |
1dc01b9f DM |
424 | if $tmpfmt ne $fmt; |
425 | ||
426 | my $path = "$imagedir/$name"; | |
427 | ||
428 | die "disk image '$path' already exists\n" if -e $path; | |
429 | ||
045ae0a7 DM |
430 | my $cmd = ['/usr/bin/qemu-img', 'create']; |
431 | ||
432 | push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; | |
1e679ac5 | 433 | |
045ae0a7 DM |
434 | push @$cmd, '-f', $fmt, $path, "${size}K"; |
435 | ||
436 | run_command($cmd, errmsg => "unable to create image"); | |
1dc01b9f DM |
437 | |
438 | return "$vmid/$name"; | |
439 | } | |
440 | ||
441 | sub free_image { | |
442 | my ($class, $storeid, $scfg, $volname) = @_; | |
443 | ||
444 | my $path = $class->path($scfg, $volname); | |
445 | ||
446 | if (! -f $path) { | |
447 | warn "disk image '$path' does not exists\n"; | |
448 | } else { | |
449 | unlink $path; | |
450 | } | |
451 | ||
452 | return undef; | |
453 | } | |
454 | ||
455 | sub file_size_info { | |
456 | my ($filename, $timeout) = @_; | |
457 | ||
458 | my $cmd = ['/usr/bin/qemu-img', 'info', $filename]; | |
459 | ||
460 | my $format; | |
73b7847e | 461 | my $parent; |
1dc01b9f DM |
462 | my $size = 0; |
463 | my $used = 0; | |
464 | ||
465 | eval { | |
466 | run_command($cmd, timeout => $timeout, outfunc => sub { | |
467 | my $line = shift; | |
468 | ||
469 | if ($line =~ m/^file format:\s+(\S+)\s*$/) { | |
470 | $format = $1; | |
73b7847e AD |
471 | } elsif ($line =~ m/^backing file:\s(\S+)\s/) { |
472 | $parent = $1; | |
1dc01b9f DM |
473 | } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) { |
474 | $size = int($1); | |
475 | } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) { | |
476 | $used = $1; | |
477 | my $u = $3; | |
478 | ||
479 | $used *= 1024 if $u eq 'K'; | |
480 | $used *= (1024*1024) if $u eq 'M'; | |
481 | $used *= (1024*1024*1024) if $u eq 'G'; | |
482 | $used *= (1024*1024*1024*1024) if $u eq 'T'; | |
483 | ||
484 | $used = int($used); | |
485 | } | |
486 | }); | |
487 | }; | |
488 | ||
73b7847e | 489 | return wantarray ? ($size, $format, $used, $parent) : $size; |
1dc01b9f DM |
490 | } |
491 | ||
e47e548e AD |
492 | sub volume_size_info { |
493 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
494 | my $path = $class->path($scfg, $volname); | |
495 | return file_size_info($path, $timeout); | |
496 | ||
497 | } | |
498 | ||
81f5058c AD |
499 | sub volume_resize { |
500 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
501 | ||
502 | die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/; | |
503 | ||
504 | return 1 if $running; | |
505 | ||
506 | my $path = $class->path($scfg, $volname); | |
507 | ||
508 | my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size]; | |
509 | ||
1059cc99 | 510 | run_command($cmd, timeout => 10); |
81f5058c AD |
511 | |
512 | return undef; | |
513 | } | |
514 | ||
7dcb0697 AD |
515 | sub volume_snapshot { |
516 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
517 | ||
518 | die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
519 | ||
520 | return 1 if $running; | |
521 | ||
522 | my $path = $class->path($scfg, $volname); | |
523 | ||
524 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path]; | |
525 | ||
3f13fd7d | 526 | run_command($cmd); |
7dcb0697 AD |
527 | |
528 | return undef; | |
529 | } | |
530 | ||
41dffa85 AD |
531 | sub volume_snapshot_rollback { |
532 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
533 | ||
534 | die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
535 | ||
536 | my $path = $class->path($scfg, $volname); | |
537 | ||
538 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path]; | |
539 | ||
3f13fd7d | 540 | run_command($cmd); |
41dffa85 AD |
541 | |
542 | return undef; | |
543 | } | |
544 | ||
6000a061 AD |
545 | sub volume_snapshot_delete { |
546 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
547 | ||
548 | die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
549 | ||
550 | return 1 if $running; | |
551 | ||
552 | my $path = $class->path($scfg, $volname); | |
553 | ||
554 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path]; | |
555 | ||
3f13fd7d | 556 | run_command($cmd); |
6000a061 AD |
557 | |
558 | return undef; | |
559 | } | |
560 | ||
1dc01b9f DM |
561 | sub list_images { |
562 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
563 | ||
564 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
565 | ||
566 | my ($defFmt, $vaidFmts) = default_format($scfg); | |
045ae0a7 | 567 | my $fmts = join ('|', @$vaidFmts); |
1dc01b9f DM |
568 | |
569 | my $res = []; | |
570 | ||
571 | foreach my $fn (<$imagedir/[0-9][0-9]*/*>) { | |
572 | ||
573 | next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!; | |
574 | $fn = $1; # untaint | |
575 | ||
576 | my $owner = $2; | |
577 | my $name = $3; | |
578 | my $volid = "$storeid:$owner/$name"; | |
579 | ||
580 | if ($vollist) { | |
581 | my $found = grep { $_ eq $volid } @$vollist; | |
582 | next if !$found; | |
583 | } else { | |
584 | next if defined($vmid) && ($owner ne $vmid); | |
585 | } | |
586 | ||
73b7847e | 587 | my ($size, $format, $used, $parent) = file_size_info($fn); |
1dc01b9f DM |
588 | |
589 | if ($format && $size) { | |
045ae0a7 | 590 | push @$res, { |
1dc01b9f | 591 | volid => $volid, format => $format, |
73b7847e | 592 | size => $size, vmid => $owner, used => $used, parent => $parent }; |
1dc01b9f DM |
593 | } |
594 | ||
595 | } | |
596 | ||
597 | return $res; | |
598 | } | |
599 | ||
600 | sub status { | |
601 | my ($class, $storeid, $scfg, $cache) = @_; | |
602 | ||
603 | my $path = $scfg->{path}; | |
604 | ||
605 | die "storage definintion has no path\n" if !$path; | |
045ae0a7 | 606 | |
1dc01b9f DM |
607 | my $timeout = 2; |
608 | my $res = PVE::Tools::df($path, $timeout); | |
609 | ||
610 | return undef if !$res || !$res->{total}; | |
611 | ||
612 | return ($res->{total}, $res->{avail}, $res->{used}, 1); | |
613 | } | |
614 | ||
615 | sub activate_storage { | |
616 | my ($class, $storeid, $scfg, $cache) = @_; | |
617 | ||
618 | my $path = $scfg->{path}; | |
619 | ||
620 | die "storage definintion has no path\n" if !$path; | |
621 | ||
622 | die "unable to activate storage '$storeid' - " . | |
623 | "directory '$path' does not exist\n" if ! -d $path; | |
624 | ||
625 | if (defined($scfg->{content})) { | |
626 | foreach my $vtype (keys %$vtype_subdirs) { | |
6bcc16d7 DM |
627 | # OpenVZMigrate uses backup (dump) dir |
628 | if (defined($scfg->{content}->{$vtype}) || | |
629 | ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) { | |
630 | my $subdir = $class->get_subdir($scfg, $vtype); | |
631 | mkpath $subdir if $subdir ne $path; | |
632 | } | |
1dc01b9f DM |
633 | } |
634 | } | |
635 | } | |
636 | ||
637 | sub deactivate_storage { | |
638 | my ($class, $storeid, $scfg, $cache) = @_; | |
639 | ||
640 | # do nothing by default | |
641 | } | |
642 | ||
643 | sub activate_volume { | |
644 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
645 | ||
646 | my $path = $class->path($scfg, $volname); | |
647 | ||
648 | # check is volume exists | |
649 | if ($scfg->{path}) { | |
650 | die "volume '$storeid:$volname' does not exist\n" if ! -e $path; | |
651 | } else { | |
652 | die "volume '$storeid:$volname' does not exist\n" if ! -b $path; | |
653 | } | |
654 | } | |
655 | ||
656 | sub deactivate_volume { | |
657 | my ($class, $storeid, $scfg, $volname, $cache) = @_; | |
658 | ||
659 | # do nothing by default | |
660 | } | |
661 | ||
c9eeac01 AD |
662 | sub check_connection { |
663 | my ($class, $storeid, $scfg) = @_; | |
664 | # do nothing by default | |
665 | return 1; | |
666 | } | |
667 | ||
e47e548e | 668 | |
1dc01b9f | 669 | 1; |