]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::Plugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
7fc619d5 | 5 | use File::chdir; |
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 | ||
a7f3d909 DM |
181 | PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id); |
182 | sub 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 | |
193 | sub private { | |
194 | return $defaultData; | |
195 | } | |
196 | ||
197 | sub 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 | ||
211 | sub 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 | ||
262 | sub 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 | ||
275 | sub 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 | ||
40cd7d27 | 315 | if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'nexenta' ) { |
1dc01b9f DM |
316 | $d->{shared} = 1; |
317 | } | |
318 | } | |
319 | ||
320 | return $cfg; | |
321 | } | |
322 | ||
323 | # Storage implementation | |
324 | ||
325 | sub 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 | ||
342 | sub 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 | ||
352 | sub 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 | 382 | my $vtype_subdirs = { |
1dc01b9f DM |
383 | images => 'images', |
384 | rootdir => 'private', | |
385 | iso => 'template/iso', | |
386 | vztmpl => 'template/cache', | |
387 | backup => 'dump', | |
388 | }; | |
389 | ||
390 | sub 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 | ||
404 | sub 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 | ||
2502b33b DM |
418 | sub create_base { |
419 | my ($class, $storeid, $scfg, $volname) = @_; | |
420 | ||
421 | # this only works for file based storage types | |
422 | die "storage definintion has no path\n" if !$scfg->{path}; | |
423 | ||
424 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
425 | $class->parse_volname($volname); | |
426 | ||
427 | die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
428 | ||
429 | die "create_base not possible with base image\n" if $isBase; | |
430 | ||
431 | my $path = $class->path($scfg, $volname); | |
432 | ||
433 | my ($size, $format, $used, $parent) = file_size_info($path); | |
434 | die "file_size_info on '$volname' failed\n" if !($format && $size); | |
435 | ||
436 | die "volname '$volname' contains wrong information about parent\n" | |
437 | if $basename && (!$parent || $parent ne "../$basevmid/$basename"); | |
438 | ||
439 | my $newname = $name; | |
440 | $newname =~ s/^vm-/base-/; | |
441 | ||
442 | my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" : | |
443 | "$vmid/$newname"; | |
444 | ||
445 | my $newpath = $class->path($scfg, $newvolname); | |
446 | ||
447 | die "file '$newpath' already exists\n" if -f $newpath; | |
448 | ||
449 | rename($path, $newpath) || | |
450 | die "rename '$path' to '$newpath' failed - $!\n"; | |
451 | ||
c803c396 | 452 | # We try to protect base volume |
2502b33b | 453 | |
c803c396 DM |
454 | chmod(0444, $newpath); # nobody should write anything |
455 | ||
456 | # also try to set immutable flag | |
457 | eval { run_command(['/usr/bin/chattr', '+i', $newpath]); }; | |
458 | warn $@ if $@; | |
459 | ||
2502b33b DM |
460 | return $newvolname; |
461 | } | |
462 | ||
463 | my $find_free_diskname = sub { | |
464 | my ($imgdir, $vmid, $fmt) = @_; | |
465 | ||
466 | my $disk_ids = {}; | |
467 | PVE::Tools::dir_glob_foreach($imgdir, | |
468 | qr!(vm|base)-$vmid-disk-(\d+)\..*!, | |
469 | sub { | |
470 | my ($fn, $type, $disk) = @_; | |
471 | $disk_ids->{$disk} = 1; | |
472 | }); | |
473 | ||
474 | for (my $i = 1; $i < 100; $i++) { | |
475 | if (!$disk_ids->{$i}) { | |
476 | return "vm-$vmid-disk-$i.$fmt"; | |
477 | } | |
478 | } | |
479 | ||
480 | die "unable to allocate a new image name for VM $vmid in '$imgdir'\n"; | |
481 | }; | |
482 | ||
483 | sub clone_image { | |
484 | my ($class, $scfg, $storeid, $volname, $vmid) = @_; | |
485 | ||
486 | # this only works for file based storage types | |
487 | die "storage definintion has no path\n" if !$scfg->{path}; | |
488 | ||
489 | my ($vtype, $basename, $basevmid, undef, undef, $isBase) = | |
490 | $class->parse_volname($volname); | |
491 | ||
492 | die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
493 | ||
494 | die "clone_image onyl works on base images\n" if !$isBase; | |
1dc01b9f DM |
495 | |
496 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
497 | $imagedir .= "/$vmid"; | |
498 | ||
499 | mkpath $imagedir; | |
500 | ||
2502b33b DM |
501 | my $name = &$find_free_diskname($imagedir, $vmid, "qcow2"); |
502 | ||
503 | warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n"; | |
504 | ||
505 | my $newvol = "$basevmid/$basename/$vmid/$name"; | |
506 | ||
507 | my $path = $class->path($scfg, $newvol); | |
508 | ||
7fc619d5 | 509 | # Note: we use relative paths, so we need to call chdir before qemu-img |
2502b33b | 510 | eval { |
7fc619d5 | 511 | local $CWD = $imagedir; |
2502b33b DM |
512 | |
513 | my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename", | |
514 | '-f', 'qcow2', $path]; | |
515 | ||
516 | run_command($cmd); | |
517 | }; | |
518 | my $err = $@; | |
1dc01b9f | 519 | |
2502b33b DM |
520 | die $err if $err; |
521 | ||
522 | return $newvol; | |
523 | } | |
524 | ||
525 | sub alloc_image { | |
526 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
527 | ||
528 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
529 | $imagedir .= "/$vmid"; | |
530 | ||
531 | mkpath $imagedir; | |
532 | ||
533 | $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name; | |
534 | ||
1dc01b9f DM |
535 | my (undef, $tmpfmt) = parse_name_dir($name); |
536 | ||
045ae0a7 | 537 | die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n" |
1dc01b9f DM |
538 | if $tmpfmt ne $fmt; |
539 | ||
540 | my $path = "$imagedir/$name"; | |
541 | ||
542 | die "disk image '$path' already exists\n" if -e $path; | |
543 | ||
045ae0a7 DM |
544 | my $cmd = ['/usr/bin/qemu-img', 'create']; |
545 | ||
546 | push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; | |
1e679ac5 | 547 | |
045ae0a7 DM |
548 | push @$cmd, '-f', $fmt, $path, "${size}K"; |
549 | ||
550 | run_command($cmd, errmsg => "unable to create image"); | |
1dc01b9f DM |
551 | |
552 | return "$vmid/$name"; | |
553 | } | |
554 | ||
555 | sub free_image { | |
32437ed2 | 556 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; |
1dc01b9f DM |
557 | |
558 | my $path = $class->path($scfg, $volname); | |
559 | ||
560 | if (! -f $path) { | |
561 | warn "disk image '$path' does not exists\n"; | |
a7f3d909 DM |
562 | return undef; |
563 | } | |
564 | ||
a7f3d909 | 565 | if ($isBase) { |
c803c396 DM |
566 | # try to remove immutable flag |
567 | eval { run_command(['/usr/bin/chattr', '-i', $path]); }; | |
568 | warn $@ if $@; | |
1dc01b9f DM |
569 | } |
570 | ||
c803c396 | 571 | unlink($path) || die "unlink '$path' failed - $!\n"; |
a7f3d909 | 572 | |
1dc01b9f DM |
573 | return undef; |
574 | } | |
575 | ||
576 | sub file_size_info { | |
577 | my ($filename, $timeout) = @_; | |
578 | ||
579 | my $cmd = ['/usr/bin/qemu-img', 'info', $filename]; | |
580 | ||
581 | my $format; | |
73b7847e | 582 | my $parent; |
1dc01b9f DM |
583 | my $size = 0; |
584 | my $used = 0; | |
585 | ||
586 | eval { | |
587 | run_command($cmd, timeout => $timeout, outfunc => sub { | |
588 | my $line = shift; | |
1dc01b9f DM |
589 | if ($line =~ m/^file format:\s+(\S+)\s*$/) { |
590 | $format = $1; | |
73b7847e AD |
591 | } elsif ($line =~ m/^backing file:\s(\S+)\s/) { |
592 | $parent = $1; | |
1dc01b9f DM |
593 | } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) { |
594 | $size = int($1); | |
595 | } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) { | |
596 | $used = $1; | |
597 | my $u = $3; | |
598 | ||
599 | $used *= 1024 if $u eq 'K'; | |
600 | $used *= (1024*1024) if $u eq 'M'; | |
601 | $used *= (1024*1024*1024) if $u eq 'G'; | |
602 | $used *= (1024*1024*1024*1024) if $u eq 'T'; | |
603 | ||
604 | $used = int($used); | |
605 | } | |
606 | }); | |
607 | }; | |
608 | ||
73b7847e | 609 | return wantarray ? ($size, $format, $used, $parent) : $size; |
1dc01b9f DM |
610 | } |
611 | ||
e47e548e AD |
612 | sub volume_size_info { |
613 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
614 | my $path = $class->path($scfg, $volname); | |
615 | return file_size_info($path, $timeout); | |
616 | ||
617 | } | |
618 | ||
81f5058c AD |
619 | sub volume_resize { |
620 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
621 | ||
622 | die "can't resize this image format" if $volname !~ m/\.(raw|qcow2)$/; | |
623 | ||
624 | return 1 if $running; | |
625 | ||
626 | my $path = $class->path($scfg, $volname); | |
627 | ||
628 | my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size]; | |
629 | ||
1059cc99 | 630 | run_command($cmd, timeout => 10); |
81f5058c AD |
631 | |
632 | return undef; | |
633 | } | |
634 | ||
7dcb0697 AD |
635 | sub volume_snapshot { |
636 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
637 | ||
638 | die "can't snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
639 | ||
640 | return 1 if $running; | |
641 | ||
642 | my $path = $class->path($scfg, $volname); | |
643 | ||
644 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path]; | |
645 | ||
3f13fd7d | 646 | run_command($cmd); |
7dcb0697 AD |
647 | |
648 | return undef; | |
649 | } | |
650 | ||
41dffa85 AD |
651 | sub volume_snapshot_rollback { |
652 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
653 | ||
654 | die "can't rollback snapshot this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
655 | ||
656 | my $path = $class->path($scfg, $volname); | |
657 | ||
658 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path]; | |
659 | ||
3f13fd7d | 660 | run_command($cmd); |
41dffa85 AD |
661 | |
662 | return undef; | |
663 | } | |
664 | ||
6000a061 AD |
665 | sub volume_snapshot_delete { |
666 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
667 | ||
668 | die "can't delete snapshot for this image format" if $volname !~ m/\.(qcow2|qed)$/; | |
669 | ||
670 | return 1 if $running; | |
671 | ||
672 | my $path = $class->path($scfg, $volname); | |
673 | ||
674 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path]; | |
675 | ||
3f13fd7d | 676 | run_command($cmd); |
6000a061 AD |
677 | |
678 | return undef; | |
679 | } | |
680 | ||
f884fe11 AD |
681 | sub volume_has_feature { |
682 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
683 | ||
684 | my $features = { | |
685 | snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} }, | |
686 | clone => { current => {qcow2 => 1, raw => 1, vmdk => 1} }, | |
687 | }; | |
688 | ||
689 | if ($volname =~ m!^(\d+)/(\S+)$!) { | |
690 | my ($vmid, $name) = ($1, $2); | |
691 | my (undef, $format) = parse_name_dir($name); | |
692 | my $snap = $snapname ? 'snap' : 'current'; | |
693 | return 1 if defined($features->{$feature}->{$snap}->{$format}); | |
694 | ||
695 | } | |
696 | return undef; | |
697 | } | |
698 | ||
1dc01b9f DM |
699 | sub list_images { |
700 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
701 | ||
702 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
703 | ||
704 | my ($defFmt, $vaidFmts) = default_format($scfg); | |
045ae0a7 | 705 | my $fmts = join ('|', @$vaidFmts); |
1dc01b9f DM |
706 | |
707 | my $res = []; | |
708 | ||
709 | foreach my $fn (<$imagedir/[0-9][0-9]*/*>) { | |
710 | ||
711 | next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!; | |
712 | $fn = $1; # untaint | |
713 | ||
714 | my $owner = $2; | |
715 | my $name = $3; | |
1dc01b9f | 716 | |
2502b33b | 717 | next if !$vollist && defined($vmid) && ($owner ne $vmid); |
1dc01b9f | 718 | |
73b7847e | 719 | my ($size, $format, $used, $parent) = file_size_info($fn); |
2502b33b DM |
720 | next if !($format && $size); |
721 | ||
722 | my $volid; | |
723 | if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) { | |
724 | my ($basevmid, $basename) = ($1, $2); | |
725 | $volid = "$storeid:$basevmid/$basename/$owner/$name"; | |
726 | } else { | |
727 | $volid = "$storeid:$owner/$name"; | |
728 | } | |
1dc01b9f | 729 | |
2502b33b DM |
730 | if ($vollist) { |
731 | my $found = grep { $_ eq $volid } @$vollist; | |
732 | next if !$found; | |
1dc01b9f DM |
733 | } |
734 | ||
2502b33b DM |
735 | push @$res, { |
736 | volid => $volid, format => $format, | |
737 | size => $size, vmid => $owner, used => $used, parent => $parent | |
738 | }; | |
1dc01b9f DM |
739 | } |
740 | ||
741 | return $res; | |
742 | } | |
743 | ||
744 | sub status { | |
745 | my ($class, $storeid, $scfg, $cache) = @_; | |
746 | ||
747 | my $path = $scfg->{path}; | |
748 | ||
749 | die "storage definintion has no path\n" if !$path; | |
045ae0a7 | 750 | |
1dc01b9f DM |
751 | my $timeout = 2; |
752 | my $res = PVE::Tools::df($path, $timeout); | |
753 | ||
754 | return undef if !$res || !$res->{total}; | |
755 | ||
756 | return ($res->{total}, $res->{avail}, $res->{used}, 1); | |
757 | } | |
758 | ||
759 | sub activate_storage { | |
760 | my ($class, $storeid, $scfg, $cache) = @_; | |
761 | ||
762 | my $path = $scfg->{path}; | |
763 | ||
764 | die "storage definintion has no path\n" if !$path; | |
765 | ||
766 | die "unable to activate storage '$storeid' - " . | |
767 | "directory '$path' does not exist\n" if ! -d $path; | |
768 | ||
769 | if (defined($scfg->{content})) { | |
770 | foreach my $vtype (keys %$vtype_subdirs) { | |
6bcc16d7 DM |
771 | # OpenVZMigrate uses backup (dump) dir |
772 | if (defined($scfg->{content}->{$vtype}) || | |
773 | ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) { | |
774 | my $subdir = $class->get_subdir($scfg, $vtype); | |
775 | mkpath $subdir if $subdir ne $path; | |
776 | } | |
1dc01b9f DM |
777 | } |
778 | } | |
779 | } | |
780 | ||
781 | sub deactivate_storage { | |
782 | my ($class, $storeid, $scfg, $cache) = @_; | |
783 | ||
784 | # do nothing by default | |
785 | } | |
786 | ||
787 | sub activate_volume { | |
788 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
789 | ||
790 | my $path = $class->path($scfg, $volname); | |
791 | ||
792 | # check is volume exists | |
793 | if ($scfg->{path}) { | |
794 | die "volume '$storeid:$volname' does not exist\n" if ! -e $path; | |
795 | } else { | |
796 | die "volume '$storeid:$volname' does not exist\n" if ! -b $path; | |
797 | } | |
798 | } | |
799 | ||
800 | sub deactivate_volume { | |
801 | my ($class, $storeid, $scfg, $volname, $cache) = @_; | |
802 | ||
803 | # do nothing by default | |
804 | } | |
805 | ||
c9eeac01 AD |
806 | sub check_connection { |
807 | my ($class, $storeid, $scfg) = @_; | |
808 | # do nothing by default | |
809 | return 1; | |
810 | } | |
811 | ||
e47e548e | 812 | |
1dc01b9f | 813 | 1; |