]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::Plugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
2502b33b | 5 | use Cwd; |
1dc01b9f DM |
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 | ||
045ae0a7 | 15 | cfs_register_file ('storage.cfg', |
1dc01b9f DM |
16 | sub { __PACKAGE__->parse_config(@_); }, |
17 | sub { __PACKAGE__->write_config(@_); }); | |
18 | ||
19 | my $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 | ||
53 | sub 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 | ||
64 | sub 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 | ||
74 | sub 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 | ||
91 | PVE::JSONSchema::register_format('pve-storage-path', \&verify_path); | |
92 | sub 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 | ||
104 | PVE::JSONSchema::register_format('pve-storage-server', \&verify_server); | |
105 | sub 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 | ||
130 | PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns); | |
131 | sub 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 | ||
142 | PVE::JSONSchema::register_format('pve-storage-content', \&verify_content); | |
143 | sub 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 | ||
156 | PVE::JSONSchema::register_format('pve-storage-format', \&verify_format); | |
157 | sub 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 | ||
168 | PVE::JSONSchema::register_format('pve-storage-options', \&verify_options); | |
169 | sub 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 | ||
181 | ||
182 | sub private { | |
183 | return $defaultData; | |
184 | } | |
185 | ||
186 | sub parse_section_header { | |
187 | my ($class, $line) = @_; | |
188 | ||
189 | if ($line =~ m/^(\S+):\s*(\S+)\s*$/) { | |
190 | my ($type, $storeid) = (lc($1), $2); | |
191 | my $errmsg = undef; # set if you want to skip whole section | |
192 | eval { PVE::JSONSchema::parse_storage_id($storeid); }; | |
193 | $errmsg = $@ if $@; | |
194 | my $config = {}; # to return additional attributes | |
195 | return ($type, $storeid, $errmsg, $config); | |
196 | } | |
197 | return undef; | |
198 | } | |
199 | ||
200 | sub decode_value { | |
201 | my ($class, $type, $key, $value) = @_; | |
202 | ||
203 | my $def = $defaultData->{plugindata}->{$type}; | |
204 | ||
205 | if ($key eq 'content') { | |
206 | my $valid_content = $def->{content}->[0]; | |
045ae0a7 | 207 | |
1dc01b9f DM |
208 | my $res = {}; |
209 | ||
210 | foreach my $c (PVE::Tools::split_list($value)) { | |
211 | if (!$valid_content->{$c}) { | |
212 | die "storage does not support content type '$c'\n"; | |
213 | } | |
214 | $res->{$c} = 1; | |
045ae0a7 | 215 | } |
1dc01b9f DM |
216 | |
217 | if ($res->{none} && scalar (keys %$res) > 1) { | |
218 | die "unable to combine 'none' with other content types\n"; | |
219 | } | |
220 | ||
221 | return $res; | |
222 | } elsif ($key eq 'format') { | |
223 | my $valid_formats = $def->{format}->[0]; | |
224 | ||
225 | if (!$valid_formats->{$value}) { | |
226 | die "storage does not support format '$value'\n"; | |
227 | } | |
228 | ||
229 | return $value; | |
230 | } elsif ($key eq 'nodes') { | |
231 | my $res = {}; | |
232 | ||
233 | foreach my $node (PVE::Tools::split_list($value)) { | |
234 | if (PVE::JSONSchema::pve_verify_node_name($node)) { | |
235 | $res->{$node} = 1; | |
236 | } | |
237 | } | |
238 | ||
239 | # fixme: | |
240 | # no node restrictions for local storage | |
241 | #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) { | |
242 | # die "storage '$storeid' does not allow node restrictions\n"; | |
243 | #} | |
244 | ||
245 | return $res; | |
246 | } | |
247 | ||
248 | return $value; | |
249 | } | |
250 | ||
251 | sub encode_value { | |
252 | my ($class, $type, $key, $value) = @_; | |
253 | ||
254 | if ($key eq 'nodes') { | |
255 | return join(',', keys(%$value)); | |
256 | } elsif ($key eq 'content') { | |
257 | my $res = content_hash_to_string($value) || 'none'; | |
258 | return $res; | |
259 | } | |
260 | ||
261 | return $value; | |
262 | } | |
263 | ||
264 | sub parse_config { | |
265 | my ($class, $filename, $raw) = @_; | |
266 | ||
267 | my $cfg = $class->SUPER::parse_config($filename, $raw); | |
268 | my $ids = $cfg->{ids}; | |
269 | ||
270 | # make sure we have a reasonable 'local:' storage | |
271 | # openvz expects things to be there | |
272 | if (!$ids->{local} || $ids->{local}->{type} ne 'dir' || | |
273 | ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) { | |
274 | $ids->{local} = { | |
275 | type => 'dir', | |
276 | priority => 0, # force first entry | |
277 | path => '/var/lib/vz', | |
278 | maxfiles => 0, | |
279 | content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1}, | |
280 | }; | |
281 | } | |
045ae0a7 | 282 | |
1dc01b9f DM |
283 | # we always need this for OpenVZ |
284 | $ids->{local}->{content}->{rootdir} = 1; | |
285 | $ids->{local}->{content}->{vztmpl} = 1; | |
286 | delete ($ids->{local}->{disable}); | |
287 | ||
288 | # make sure we have a path | |
289 | $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path}; | |
290 | ||
291 | # remove node restrictions for local storage | |
292 | delete($ids->{local}->{nodes}); | |
293 | ||
294 | foreach my $storeid (keys %$ids) { | |
295 | my $d = $ids->{$storeid}; | |
296 | my $type = $d->{type}; | |
297 | ||
298 | my $def = $defaultData->{plugindata}->{$type}; | |
299 | ||
300 | if ($def->{content}) { | |
301 | $d->{content} = $def->{content}->[1] if !$d->{content}; | |
302 | } | |
303 | ||
40cd7d27 | 304 | if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'nexenta' ) { |
1dc01b9f DM |
305 | $d->{shared} = 1; |
306 | } | |
307 | } | |
308 | ||
309 | return $cfg; | |
310 | } | |
311 | ||
312 | # Storage implementation | |
313 | ||
314 | sub cluster_lock_storage { | |
315 | my ($class, $storeid, $shared, $timeout, $func, @param) = @_; | |
316 | ||
317 | my $res; | |
318 | if (!$shared) { | |
319 | my $lockid = "pve-storage-$storeid"; | |
320 | my $lockdir = "/var/lock/pve-manager"; | |
321 | mkdir $lockdir; | |
045ae0a7 | 322 | $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param); |
1dc01b9f DM |
323 | die $@ if $@; |
324 | } else { | |
325 | $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param); | |
326 | die $@ if $@; | |
045ae0a7 | 327 | } |
1dc01b9f DM |
328 | return $res; |
329 | } | |
330 | ||
331 | sub parse_name_dir { | |
332 | my $name = shift; | |
333 | ||
2502b33b DM |
334 | if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk))$!) { |
335 | return ($1, $3, $2); | |
1dc01b9f DM |
336 | } |
337 | ||
338 | die "unable to parse volume filename '$name'\n"; | |
339 | } | |
340 | ||
341 | sub parse_volname { | |
342 | my ($class, $volname) = @_; | |
343 | ||
2502b33b DM |
344 | if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) { |
345 | my ($basedvmid, $basename) = ($1, $2); | |
346 | parse_name_dir($basename); | |
347 | my ($vmid, $name) = ($3, $4); | |
348 | my (undef, undef, $isBase) = parse_name_dir($name); | |
349 | return ('images', $name, $vmid, $basename, $basedvmid, $isBase); | |
350 | } elsif ($volname =~ m!^(\d+)/(\S+)$!) { | |
1dc01b9f | 351 | my ($vmid, $name) = ($1, $2); |
2502b33b DM |
352 | my (undef, undef, $isBase) = parse_name_dir($name); |
353 | return ('images', $name, $vmid, undef, undef, $isBase); | |
1dc01b9f DM |
354 | } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) { |
355 | return ('iso', $1); | |
356 | } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) { | |
357 | return ('vztmpl', $1); | |
358 | } elsif ($volname =~ m!^rootdir/(\d+)$!) { | |
359 | return ('rootdir', $1, $1); | |
a22854e5 | 360 | } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) { |
1dc01b9f DM |
361 | my $fn = $1; |
362 | if ($fn =~ m/^vzdump-(openvz|qemu)-(\d+)-.+/) { | |
363 | return ('backup', $fn, $2); | |
364 | } | |
365 | return ('backup', $fn); | |
366 | } | |
367 | ||
368 | die "unable to parse directory volume name '$volname'\n"; | |
369 | } | |
370 | ||
045ae0a7 | 371 | my $vtype_subdirs = { |
1dc01b9f DM |
372 | images => 'images', |
373 | rootdir => 'private', | |
374 | iso => 'template/iso', | |
375 | vztmpl => 'template/cache', | |
376 | backup => 'dump', | |
377 | }; | |
378 | ||
379 | sub get_subdir { | |
380 | my ($class, $scfg, $vtype) = @_; | |
381 | ||
382 | my $path = $scfg->{path}; | |
383 | ||
384 | die "storage definintion has no path\n" if !$path; | |
385 | ||
386 | my $subdir = $vtype_subdirs->{$vtype}; | |
387 | ||
388 | die "unknown vtype '$vtype'\n" if !defined($subdir); | |
389 | ||
045ae0a7 | 390 | return "$path/$subdir"; |
1dc01b9f DM |
391 | } |
392 | ||
393 | sub path { | |
6bf56298 | 394 | my ($class, $scfg, $volname, $storeid) = @_; |
1dc01b9f DM |
395 | |
396 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
397 | ||
398 | my $dir = $class->get_subdir($scfg, $vtype); | |
399 | ||
400 | $dir .= "/$vmid" if $vtype eq 'images'; | |
401 | ||
402 | my $path = "$dir/$name"; | |
403 | ||
404 | return wantarray ? ($path, $vmid, $vtype) : $path; | |
405 | } | |
406 | ||
2502b33b DM |
407 | sub create_base { |
408 | my ($class, $storeid, $scfg, $volname) = @_; | |
409 | ||
410 | # this only works for file based storage types | |
411 | die "storage definintion has no path\n" if !$scfg->{path}; | |
412 | ||
413 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
414 | $class->parse_volname($volname); | |
415 | ||
416 | die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
417 | ||
418 | die "create_base not possible with base image\n" if $isBase; | |
419 | ||
420 | my $path = $class->path($scfg, $volname); | |
421 | ||
422 | my ($size, $format, $used, $parent) = file_size_info($path); | |
423 | die "file_size_info on '$volname' failed\n" if !($format && $size); | |
424 | ||
425 | die "volname '$volname' contains wrong information about parent\n" | |
426 | if $basename && (!$parent || $parent ne "../$basevmid/$basename"); | |
427 | ||
428 | my $newname = $name; | |
429 | $newname =~ s/^vm-/base-/; | |
430 | ||
431 | my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" : | |
432 | "$vmid/$newname"; | |
433 | ||
434 | my $newpath = $class->path($scfg, $newvolname); | |
435 | ||
436 | die "file '$newpath' already exists\n" if -f $newpath; | |
437 | ||
438 | rename($path, $newpath) || | |
439 | die "rename '$path' to '$newpath' failed - $!\n"; | |
440 | ||
441 | chmod(0444, $newpath); | |
442 | ||
443 | return $newvolname; | |
444 | } | |
445 | ||
446 | my $find_free_diskname = sub { | |
447 | my ($imgdir, $vmid, $fmt) = @_; | |
448 | ||
449 | my $disk_ids = {}; | |
450 | PVE::Tools::dir_glob_foreach($imgdir, | |
451 | qr!(vm|base)-$vmid-disk-(\d+)\..*!, | |
452 | sub { | |
453 | my ($fn, $type, $disk) = @_; | |
454 | $disk_ids->{$disk} = 1; | |
455 | }); | |
456 | ||
457 | for (my $i = 1; $i < 100; $i++) { | |
458 | if (!$disk_ids->{$i}) { | |
459 | return "vm-$vmid-disk-$i.$fmt"; | |
460 | } | |
461 | } | |
462 | ||
463 | die "unable to allocate a new image name for VM $vmid in '$imgdir'\n"; | |
464 | }; | |
465 | ||
466 | sub clone_image { | |
467 | my ($class, $scfg, $storeid, $volname, $vmid) = @_; | |
468 | ||
469 | # this only works for file based storage types | |
470 | die "storage definintion has no path\n" if !$scfg->{path}; | |
471 | ||
472 | my ($vtype, $basename, $basevmid, undef, undef, $isBase) = | |
473 | $class->parse_volname($volname); | |
474 | ||
475 | die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
476 | ||
477 | die "clone_image onyl works on base images\n" if !$isBase; | |
1dc01b9f DM |
478 | |
479 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
480 | $imagedir .= "/$vmid"; | |
481 | ||
482 | mkpath $imagedir; | |
483 | ||
2502b33b DM |
484 | my $name = &$find_free_diskname($imagedir, $vmid, "qcow2"); |
485 | ||
486 | warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n"; | |
487 | ||
488 | my $newvol = "$basevmid/$basename/$vmid/$name"; | |
489 | ||
490 | my $path = $class->path($scfg, $newvol); | |
491 | ||
492 | # Note: we use relative paths, so we need to call chdir before qemu-img | |
493 | my $oldcwd = cwd(); | |
494 | eval { | |
495 | chdir $imagedir; | |
496 | ||
497 | my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename", | |
498 | '-f', 'qcow2', $path]; | |
499 | ||
500 | run_command($cmd); | |
501 | }; | |
502 | my $err = $@; | |
1dc01b9f | 503 | |
2502b33b | 504 | chdir $oldcwd; |
1dc01b9f | 505 | |
2502b33b DM |
506 | die $err if $err; |
507 | ||
508 | return $newvol; | |
509 | } | |
510 | ||
511 | sub alloc_image { | |
512 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
513 | ||
514 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
515 | $imagedir .= "/$vmid"; | |
516 | ||
517 | mkpath $imagedir; | |
518 | ||
519 | $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name; | |
520 | ||
1dc01b9f DM |
521 | my (undef, $tmpfmt) = parse_name_dir($name); |
522 | ||
045ae0a7 | 523 | die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n" |
1dc01b9f DM |
524 | if $tmpfmt ne $fmt; |
525 | ||
526 | my $path = "$imagedir/$name"; | |
527 | ||
528 | die "disk image '$path' already exists\n" if -e $path; | |
529 | ||
045ae0a7 DM |
530 | my $cmd = ['/usr/bin/qemu-img', 'create']; |
531 | ||
532 | push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; | |
1e679ac5 | 533 | |
045ae0a7 DM |
534 | push @$cmd, '-f', $fmt, $path, "${size}K"; |
535 | ||
536 | run_command($cmd, errmsg => "unable to create image"); | |
1dc01b9f DM |
537 | |
538 | return "$vmid/$name"; | |
539 | } | |
540 | ||
541 | sub free_image { | |
542 | my ($class, $storeid, $scfg, $volname) = @_; | |
543 | ||
544 | my $path = $class->path($scfg, $volname); | |
545 | ||
546 | if (! -f $path) { | |
547 | warn "disk image '$path' does not exists\n"; | |
548 | } else { | |
549 | unlink $path; | |
550 | } | |
551 | ||
552 | return undef; | |
553 | } | |
554 | ||
555 | sub file_size_info { | |
556 | my ($filename, $timeout) = @_; | |
557 | ||
558 | my $cmd = ['/usr/bin/qemu-img', 'info', $filename]; | |
559 | ||
560 | my $format; | |
73b7847e | 561 | my $parent; |
1dc01b9f DM |
562 | my $size = 0; |
563 | my $used = 0; | |
564 | ||
565 | eval { | |
566 | run_command($cmd, timeout => $timeout, outfunc => sub { | |
567 | my $line = shift; | |
1dc01b9f DM |
568 | if ($line =~ m/^file format:\s+(\S+)\s*$/) { |
569 | $format = $1; | |
73b7847e AD |
570 | } elsif ($line =~ m/^backing file:\s(\S+)\s/) { |
571 | $parent = $1; | |
1dc01b9f DM |
572 | } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) { |
573 | $size = int($1); | |
574 | } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) { | |
575 | $used = $1; | |
576 | my $u = $3; | |
577 | ||
578 | $used *= 1024 if $u eq 'K'; | |
579 | $used *= (1024*1024) if $u eq 'M'; | |
580 | $used *= (1024*1024*1024) if $u eq 'G'; | |
581 | $used *= (1024*1024*1024*1024) if $u eq 'T'; | |
582 | ||
583 | $used = int($used); | |
584 | } | |
585 | }); | |
586 | }; | |
587 | ||
73b7847e | 588 | return wantarray ? ($size, $format, $used, $parent) : $size; |
1dc01b9f DM |
589 | } |
590 | ||
e47e548e AD |
591 | sub volume_size_info { |
592 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
593 | my $path = $class->path($scfg, $volname); | |
594 | return file_size_info($path, $timeout); | |
595 | ||
596 | } | |
597 | ||
81f5058c AD |
598 | sub volume_resize { |
599 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
600 | ||
601 | die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/; | |
602 | ||
603 | return 1 if $running; | |
604 | ||
605 | my $path = $class->path($scfg, $volname); | |
606 | ||
607 | my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size]; | |
608 | ||
1059cc99 | 609 | run_command($cmd, timeout => 10); |
81f5058c AD |
610 | |
611 | return undef; | |
612 | } | |
613 | ||
7dcb0697 AD |
614 | sub volume_snapshot { |
615 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
616 | ||
617 | die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
618 | ||
619 | return 1 if $running; | |
620 | ||
621 | my $path = $class->path($scfg, $volname); | |
622 | ||
623 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path]; | |
624 | ||
3f13fd7d | 625 | run_command($cmd); |
7dcb0697 AD |
626 | |
627 | return undef; | |
628 | } | |
629 | ||
41dffa85 AD |
630 | sub volume_snapshot_rollback { |
631 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
632 | ||
633 | die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
634 | ||
635 | my $path = $class->path($scfg, $volname); | |
636 | ||
637 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path]; | |
638 | ||
3f13fd7d | 639 | run_command($cmd); |
41dffa85 AD |
640 | |
641 | return undef; | |
642 | } | |
643 | ||
6000a061 AD |
644 | sub volume_snapshot_delete { |
645 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
646 | ||
647 | die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
648 | ||
649 | return 1 if $running; | |
650 | ||
651 | my $path = $class->path($scfg, $volname); | |
652 | ||
653 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path]; | |
654 | ||
3f13fd7d | 655 | run_command($cmd); |
6000a061 AD |
656 | |
657 | return undef; | |
658 | } | |
659 | ||
f884fe11 AD |
660 | sub volume_has_feature { |
661 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
662 | ||
663 | my $features = { | |
664 | snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} }, | |
665 | clone => { current => {qcow2 => 1, raw => 1, vmdk => 1} }, | |
666 | }; | |
667 | ||
668 | if ($volname =~ m!^(\d+)/(\S+)$!) { | |
669 | my ($vmid, $name) = ($1, $2); | |
670 | my (undef, $format) = parse_name_dir($name); | |
671 | my $snap = $snapname ? 'snap' : 'current'; | |
672 | return 1 if defined($features->{$feature}->{$snap}->{$format}); | |
673 | ||
674 | } | |
675 | return undef; | |
676 | } | |
677 | ||
1dc01b9f DM |
678 | sub list_images { |
679 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
680 | ||
681 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
682 | ||
683 | my ($defFmt, $vaidFmts) = default_format($scfg); | |
045ae0a7 | 684 | my $fmts = join ('|', @$vaidFmts); |
1dc01b9f DM |
685 | |
686 | my $res = []; | |
687 | ||
688 | foreach my $fn (<$imagedir/[0-9][0-9]*/*>) { | |
689 | ||
690 | next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!; | |
691 | $fn = $1; # untaint | |
692 | ||
693 | my $owner = $2; | |
694 | my $name = $3; | |
1dc01b9f | 695 | |
2502b33b | 696 | next if !$vollist && defined($vmid) && ($owner ne $vmid); |
1dc01b9f | 697 | |
73b7847e | 698 | my ($size, $format, $used, $parent) = file_size_info($fn); |
2502b33b DM |
699 | next if !($format && $size); |
700 | ||
701 | my $volid; | |
702 | if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) { | |
703 | my ($basevmid, $basename) = ($1, $2); | |
704 | $volid = "$storeid:$basevmid/$basename/$owner/$name"; | |
705 | } else { | |
706 | $volid = "$storeid:$owner/$name"; | |
707 | } | |
1dc01b9f | 708 | |
2502b33b DM |
709 | if ($vollist) { |
710 | my $found = grep { $_ eq $volid } @$vollist; | |
711 | next if !$found; | |
1dc01b9f DM |
712 | } |
713 | ||
2502b33b DM |
714 | push @$res, { |
715 | volid => $volid, format => $format, | |
716 | size => $size, vmid => $owner, used => $used, parent => $parent | |
717 | }; | |
1dc01b9f DM |
718 | } |
719 | ||
720 | return $res; | |
721 | } | |
722 | ||
723 | sub status { | |
724 | my ($class, $storeid, $scfg, $cache) = @_; | |
725 | ||
726 | my $path = $scfg->{path}; | |
727 | ||
728 | die "storage definintion has no path\n" if !$path; | |
045ae0a7 | 729 | |
1dc01b9f DM |
730 | my $timeout = 2; |
731 | my $res = PVE::Tools::df($path, $timeout); | |
732 | ||
733 | return undef if !$res || !$res->{total}; | |
734 | ||
735 | return ($res->{total}, $res->{avail}, $res->{used}, 1); | |
736 | } | |
737 | ||
738 | sub activate_storage { | |
739 | my ($class, $storeid, $scfg, $cache) = @_; | |
740 | ||
741 | my $path = $scfg->{path}; | |
742 | ||
743 | die "storage definintion has no path\n" if !$path; | |
744 | ||
745 | die "unable to activate storage '$storeid' - " . | |
746 | "directory '$path' does not exist\n" if ! -d $path; | |
747 | ||
748 | if (defined($scfg->{content})) { | |
749 | foreach my $vtype (keys %$vtype_subdirs) { | |
6bcc16d7 DM |
750 | # OpenVZMigrate uses backup (dump) dir |
751 | if (defined($scfg->{content}->{$vtype}) || | |
752 | ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) { | |
753 | my $subdir = $class->get_subdir($scfg, $vtype); | |
754 | mkpath $subdir if $subdir ne $path; | |
755 | } | |
1dc01b9f DM |
756 | } |
757 | } | |
758 | } | |
759 | ||
760 | sub deactivate_storage { | |
761 | my ($class, $storeid, $scfg, $cache) = @_; | |
762 | ||
763 | # do nothing by default | |
764 | } | |
765 | ||
766 | sub activate_volume { | |
767 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
768 | ||
769 | my $path = $class->path($scfg, $volname); | |
770 | ||
771 | # check is volume exists | |
772 | if ($scfg->{path}) { | |
773 | die "volume '$storeid:$volname' does not exist\n" if ! -e $path; | |
774 | } else { | |
775 | die "volume '$storeid:$volname' does not exist\n" if ! -b $path; | |
776 | } | |
777 | } | |
778 | ||
779 | sub deactivate_volume { | |
780 | my ($class, $storeid, $scfg, $volname, $cache) = @_; | |
781 | ||
782 | # do nothing by default | |
783 | } | |
784 | ||
c9eeac01 AD |
785 | sub check_connection { |
786 | my ($class, $storeid, $scfg) = @_; | |
787 | # do nothing by default | |
788 | return 1; | |
789 | } | |
790 | ||
e47e548e | 791 | |
1dc01b9f | 792 | 1; |