]>
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 | ||
35533c68 | 19 | |
1dc01b9f DM |
20 | my $defaultData = { |
21 | propertyList => { | |
22 | type => { description => "Storage type." }, | |
f7621c01 DM |
23 | storage => get_standard_option('pve-storage-id', |
24 | { completion => \&PVE::Storage::complete_storage }), | |
1dc01b9f DM |
25 | nodes => get_standard_option('pve-node-list', { optional => 1 }), |
26 | content => { | |
daccf21e FG |
27 | description => "Allowed content types.\n\nNOTE: the value " . |
28 | "'rootdir' is used for Containers, and value 'images' for VMs.\n", | |
1dc01b9f DM |
29 | type => 'string', format => 'pve-storage-content-list', |
30 | optional => 1, | |
98437f4c | 31 | completion => \&PVE::Storage::complete_content_type, |
1dc01b9f DM |
32 | }, |
33 | disable => { | |
34 | description => "Flag to disable the storage.", | |
35 | type => 'boolean', | |
36 | optional => 1, | |
37 | }, | |
38 | maxfiles => { | |
39 | description => "Maximal number of backup files per VM. Use '0' for unlimted.", | |
40 | type => 'integer', | |
41 | minimum => 0, | |
42 | optional => 1, | |
43 | }, | |
44 | shared => { | |
45 | description => "Mark storage as shared.", | |
46 | type => 'boolean', | |
47 | optional => 1, | |
48 | }, | |
045ae0a7 | 49 | 'format' => { |
daccf21e | 50 | description => "Default image format.", |
1dc01b9f DM |
51 | type => 'string', format => 'pve-storage-format', |
52 | optional => 1, | |
53 | }, | |
54 | }, | |
55 | }; | |
56 | ||
57 | sub content_hash_to_string { | |
58 | my $hash = shift; | |
59 | ||
60 | my @cta; | |
61 | foreach my $ct (keys %$hash) { | |
62 | push @cta, $ct if $hash->{$ct}; | |
045ae0a7 | 63 | } |
1dc01b9f DM |
64 | |
65 | return join(',', @cta); | |
66 | } | |
67 | ||
68 | sub valid_content_types { | |
69 | my ($type) = @_; | |
70 | ||
71 | my $def = $defaultData->{plugindata}->{$type}; | |
72 | ||
73 | return {} if !$def; | |
74 | ||
75 | return $def->{content}->[0]; | |
76 | } | |
77 | ||
78 | sub default_format { | |
79 | my ($scfg) = @_; | |
80 | ||
81 | my $type = $scfg->{type}; | |
82 | my $def = $defaultData->{plugindata}->{$type}; | |
045ae0a7 | 83 | |
1dc01b9f DM |
84 | my $def_format = 'raw'; |
85 | my $valid_formats = [ $def_format ]; | |
86 | ||
87 | if (defined($def->{format})) { | |
88 | $def_format = $scfg->{format} || $def->{format}->[1]; | |
89 | $valid_formats = [ sort keys %{$def->{format}->[0]} ]; | |
90 | } | |
045ae0a7 | 91 | |
1dc01b9f DM |
92 | return wantarray ? ($def_format, $valid_formats) : $def_format; |
93 | } | |
94 | ||
95 | PVE::JSONSchema::register_format('pve-storage-path', \&verify_path); | |
96 | sub verify_path { | |
97 | my ($path, $noerr) = @_; | |
98 | ||
99 | # fixme: exclude more shell meta characters? | |
100 | # we need absolute paths | |
101 | if ($path !~ m|^/[^;\(\)]+|) { | |
102 | return undef if $noerr; | |
103 | die "value does not look like a valid absolute path\n"; | |
104 | } | |
105 | return $path; | |
106 | } | |
107 | ||
108 | PVE::JSONSchema::register_format('pve-storage-server', \&verify_server); | |
109 | sub verify_server { | |
110 | my ($server, $noerr) = @_; | |
111 | ||
6bf617a9 WB |
112 | if (!(PVE::JSONSchema::pve_verify_ip($server, 1) || |
113 | PVE::JSONSchema::pve_verify_dns_name($server, 1))) | |
114 | { | |
1dc01b9f DM |
115 | return undef if $noerr; |
116 | die "value does not look like a valid server name or IP address\n"; | |
117 | } | |
118 | return $server; | |
119 | } | |
120 | ||
5dca5c7c DM |
121 | PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name); |
122 | sub parse_lvm_name { | |
123 | my ($name, $noerr) = @_; | |
124 | ||
125 | if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) { | |
126 | return undef if $noerr; | |
127 | die "lvm name '$name' contains illegal characters\n"; | |
128 | } | |
129 | ||
130 | return $name; | |
131 | } | |
132 | ||
1dc01b9f DM |
133 | # fixme: do we need this |
134 | #PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal); | |
135 | #sub verify_portal { | |
136 | # my ($portal, $noerr) = @_; | |
137 | # | |
138 | # # IP with optional port | |
139 | # if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) { | |
140 | # return undef if $noerr; | |
141 | # die "value does not look like a valid portal address\n"; | |
142 | # } | |
143 | # return $portal; | |
144 | #} | |
145 | ||
146 | PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns); | |
147 | sub verify_portal_dns { | |
148 | my ($portal, $noerr) = @_; | |
149 | ||
150 | # IP or DNS name with optional port | |
1689e627 | 151 | if (!PVE::Tools::parse_host_and_port($portal)) { |
1dc01b9f DM |
152 | return undef if $noerr; |
153 | die "value does not look like a valid portal address\n"; | |
154 | } | |
155 | return $portal; | |
156 | } | |
157 | ||
158 | PVE::JSONSchema::register_format('pve-storage-content', \&verify_content); | |
159 | sub verify_content { | |
160 | my ($ct, $noerr) = @_; | |
161 | ||
162 | my $valid_content = valid_content_types('dir'); # dir includes all types | |
045ae0a7 | 163 | |
1dc01b9f DM |
164 | if (!$valid_content->{$ct}) { |
165 | return undef if $noerr; | |
166 | die "invalid content type '$ct'\n"; | |
167 | } | |
168 | ||
169 | return $ct; | |
170 | } | |
171 | ||
172 | PVE::JSONSchema::register_format('pve-storage-format', \&verify_format); | |
173 | sub verify_format { | |
174 | my ($fmt, $noerr) = @_; | |
175 | ||
35533c68 | 176 | if ($fmt !~ m/(raw|qcow2|vmdk|subvol)/) { |
1dc01b9f DM |
177 | return undef if $noerr; |
178 | die "invalid format '$fmt'\n"; | |
179 | } | |
180 | ||
181 | return $fmt; | |
182 | } | |
183 | ||
184 | PVE::JSONSchema::register_format('pve-storage-options', \&verify_options); | |
185 | sub verify_options { | |
186 | my ($value, $noerr) = @_; | |
187 | ||
188 | # mount options (see man fstab) | |
189 | if ($value !~ m/^\S+$/) { | |
190 | return undef if $noerr; | |
191 | die "invalid options '$value'\n"; | |
192 | } | |
193 | ||
194 | return $value; | |
195 | } | |
196 | ||
a7f3d909 DM |
197 | PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id); |
198 | sub parse_volume_id { | |
199 | my ($volid, $noerr) = @_; | |
200 | ||
201 | if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) { | |
202 | return wantarray ? ($1, $2) : $1; | |
203 | } | |
204 | return undef if $noerr; | |
205 | die "unable to parse volume ID '$volid'\n"; | |
206 | } | |
207 | ||
1dc01b9f DM |
208 | |
209 | sub private { | |
210 | return $defaultData; | |
211 | } | |
212 | ||
213 | sub parse_section_header { | |
214 | my ($class, $line) = @_; | |
215 | ||
216 | if ($line =~ m/^(\S+):\s*(\S+)\s*$/) { | |
217 | my ($type, $storeid) = (lc($1), $2); | |
218 | my $errmsg = undef; # set if you want to skip whole section | |
219 | eval { PVE::JSONSchema::parse_storage_id($storeid); }; | |
220 | $errmsg = $@ if $@; | |
221 | my $config = {}; # to return additional attributes | |
222 | return ($type, $storeid, $errmsg, $config); | |
223 | } | |
224 | return undef; | |
225 | } | |
226 | ||
227 | sub decode_value { | |
228 | my ($class, $type, $key, $value) = @_; | |
229 | ||
230 | my $def = $defaultData->{plugindata}->{$type}; | |
231 | ||
232 | if ($key eq 'content') { | |
233 | my $valid_content = $def->{content}->[0]; | |
045ae0a7 | 234 | |
1dc01b9f DM |
235 | my $res = {}; |
236 | ||
237 | foreach my $c (PVE::Tools::split_list($value)) { | |
238 | if (!$valid_content->{$c}) { | |
703de49e WL |
239 | warn "storage does not support content type '$c'\n"; |
240 | next; | |
1dc01b9f DM |
241 | } |
242 | $res->{$c} = 1; | |
045ae0a7 | 243 | } |
1dc01b9f DM |
244 | |
245 | if ($res->{none} && scalar (keys %$res) > 1) { | |
246 | die "unable to combine 'none' with other content types\n"; | |
247 | } | |
248 | ||
249 | return $res; | |
250 | } elsif ($key eq 'format') { | |
251 | my $valid_formats = $def->{format}->[0]; | |
252 | ||
253 | if (!$valid_formats->{$value}) { | |
703de49e WL |
254 | warn "storage does not support format '$value'\n"; |
255 | next; | |
1dc01b9f DM |
256 | } |
257 | ||
258 | return $value; | |
259 | } elsif ($key eq 'nodes') { | |
260 | my $res = {}; | |
261 | ||
262 | foreach my $node (PVE::Tools::split_list($value)) { | |
263 | if (PVE::JSONSchema::pve_verify_node_name($node)) { | |
264 | $res->{$node} = 1; | |
265 | } | |
266 | } | |
267 | ||
268 | # fixme: | |
269 | # no node restrictions for local storage | |
270 | #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) { | |
271 | # die "storage '$storeid' does not allow node restrictions\n"; | |
272 | #} | |
273 | ||
274 | return $res; | |
275 | } | |
276 | ||
277 | return $value; | |
278 | } | |
279 | ||
280 | sub encode_value { | |
281 | my ($class, $type, $key, $value) = @_; | |
282 | ||
283 | if ($key eq 'nodes') { | |
284 | return join(',', keys(%$value)); | |
285 | } elsif ($key eq 'content') { | |
286 | my $res = content_hash_to_string($value) || 'none'; | |
287 | return $res; | |
288 | } | |
289 | ||
290 | return $value; | |
291 | } | |
292 | ||
293 | sub parse_config { | |
294 | my ($class, $filename, $raw) = @_; | |
295 | ||
296 | my $cfg = $class->SUPER::parse_config($filename, $raw); | |
297 | my $ids = $cfg->{ids}; | |
298 | ||
299 | # make sure we have a reasonable 'local:' storage | |
dc6ff39f | 300 | # we want 'local' to be always the same 'type' (on all cluster nodes) |
1dc01b9f DM |
301 | if (!$ids->{local} || $ids->{local}->{type} ne 'dir' || |
302 | ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) { | |
303 | $ids->{local} = { | |
304 | type => 'dir', | |
305 | priority => 0, # force first entry | |
306 | path => '/var/lib/vz', | |
307 | maxfiles => 0, | |
308 | content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1}, | |
309 | }; | |
310 | } | |
045ae0a7 | 311 | |
1dc01b9f DM |
312 | # make sure we have a path |
313 | $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path}; | |
314 | ||
315 | # remove node restrictions for local storage | |
316 | delete($ids->{local}->{nodes}); | |
317 | ||
318 | foreach my $storeid (keys %$ids) { | |
319 | my $d = $ids->{$storeid}; | |
320 | my $type = $d->{type}; | |
321 | ||
322 | my $def = $defaultData->{plugindata}->{$type}; | |
323 | ||
324 | if ($def->{content}) { | |
325 | $d->{content} = $def->{content}->[1] if !$d->{content}; | |
326 | } | |
327 | ||
d26e1891 | 328 | if ($type eq 'iscsi' || $type eq 'nfs' || $type eq 'rbd' || $type eq 'sheepdog' || $type eq 'iscsidirect' || $type eq 'glusterfs' || $type eq 'zfs' || $type eq 'drbd') { |
1dc01b9f DM |
329 | $d->{shared} = 1; |
330 | } | |
331 | } | |
332 | ||
333 | return $cfg; | |
334 | } | |
335 | ||
336 | # Storage implementation | |
337 | ||
338 | sub cluster_lock_storage { | |
339 | my ($class, $storeid, $shared, $timeout, $func, @param) = @_; | |
340 | ||
341 | my $res; | |
342 | if (!$shared) { | |
343 | my $lockid = "pve-storage-$storeid"; | |
344 | my $lockdir = "/var/lock/pve-manager"; | |
345 | mkdir $lockdir; | |
045ae0a7 | 346 | $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param); |
1dc01b9f DM |
347 | die $@ if $@; |
348 | } else { | |
349 | $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param); | |
350 | die $@ if $@; | |
045ae0a7 | 351 | } |
1dc01b9f DM |
352 | return $res; |
353 | } | |
354 | ||
355 | sub parse_name_dir { | |
356 | my $name = shift; | |
357 | ||
35533c68 DM |
358 | if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) { |
359 | return ($1, $3, $2); # (name, format, isBase) | |
1dc01b9f DM |
360 | } |
361 | ||
362 | die "unable to parse volume filename '$name'\n"; | |
363 | } | |
364 | ||
365 | sub parse_volname { | |
366 | my ($class, $volname) = @_; | |
367 | ||
2502b33b DM |
368 | if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) { |
369 | my ($basedvmid, $basename) = ($1, $2); | |
370 | parse_name_dir($basename); | |
371 | my ($vmid, $name) = ($3, $4); | |
35533c68 DM |
372 | my (undef, $format, $isBase) = parse_name_dir($name); |
373 | return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format); | |
2502b33b | 374 | } elsif ($volname =~ m!^(\d+)/(\S+)$!) { |
1dc01b9f | 375 | my ($vmid, $name) = ($1, $2); |
35533c68 DM |
376 | my (undef, $format, $isBase) = parse_name_dir($name); |
377 | return ('images', $name, $vmid, undef, undef, $isBase, $format); | |
1dc01b9f DM |
378 | } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) { |
379 | return ('iso', $1); | |
13d2cb79 | 380 | } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) { |
1dc01b9f DM |
381 | return ('vztmpl', $1); |
382 | } elsif ($volname =~ m!^rootdir/(\d+)$!) { | |
383 | return ('rootdir', $1, $1); | |
a22854e5 | 384 | } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) { |
1dc01b9f | 385 | my $fn = $1; |
4cb6e060 | 386 | if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) { |
1dc01b9f DM |
387 | return ('backup', $fn, $2); |
388 | } | |
389 | return ('backup', $fn); | |
390 | } | |
391 | ||
392 | die "unable to parse directory volume name '$volname'\n"; | |
393 | } | |
394 | ||
045ae0a7 | 395 | my $vtype_subdirs = { |
1dc01b9f DM |
396 | images => 'images', |
397 | rootdir => 'private', | |
398 | iso => 'template/iso', | |
399 | vztmpl => 'template/cache', | |
400 | backup => 'dump', | |
401 | }; | |
402 | ||
403 | sub get_subdir { | |
404 | my ($class, $scfg, $vtype) = @_; | |
405 | ||
406 | my $path = $scfg->{path}; | |
407 | ||
408 | die "storage definintion has no path\n" if !$path; | |
409 | ||
410 | my $subdir = $vtype_subdirs->{$vtype}; | |
411 | ||
412 | die "unknown vtype '$vtype'\n" if !defined($subdir); | |
413 | ||
045ae0a7 | 414 | return "$path/$subdir"; |
1dc01b9f DM |
415 | } |
416 | ||
08480ce7 | 417 | sub filesystem_path { |
e67069eb | 418 | my ($class, $scfg, $volname, $snapname) = @_; |
1dc01b9f | 419 | |
e67069eb DM |
420 | my ($vtype, $name, $vmid, undef, undef, $isBase, $format) = |
421 | $class->parse_volname($volname); | |
422 | ||
423 | # Note: qcow2/qed has internal snapshot, so path is always | |
424 | # the same (with or without snapshot => same file). | |
425 | die "can't snapshot this image format\n" | |
426 | if defined($snapname) && $format !~ m/^(qcow2|qed)$/; | |
1dc01b9f DM |
427 | |
428 | my $dir = $class->get_subdir($scfg, $vtype); | |
429 | ||
430 | $dir .= "/$vmid" if $vtype eq 'images'; | |
431 | ||
432 | my $path = "$dir/$name"; | |
433 | ||
434 | return wantarray ? ($path, $vmid, $vtype) : $path; | |
435 | } | |
436 | ||
08480ce7 | 437 | sub path { |
e67069eb | 438 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
08480ce7 | 439 | |
e67069eb | 440 | return $class->filesystem_path($scfg, $volname, $snapname); |
08480ce7 DM |
441 | } |
442 | ||
2502b33b DM |
443 | sub create_base { |
444 | my ($class, $storeid, $scfg, $volname) = @_; | |
445 | ||
446 | # this only works for file based storage types | |
5510f5c9 | 447 | die "storage definition has no path\n" if !$scfg->{path}; |
2502b33b | 448 | |
35533c68 | 449 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = |
2502b33b DM |
450 | $class->parse_volname($volname); |
451 | ||
452 | die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
453 | ||
454 | die "create_base not possible with base image\n" if $isBase; | |
455 | ||
08480ce7 | 456 | my $path = $class->filesystem_path($scfg, $volname); |
2502b33b | 457 | |
35533c68 DM |
458 | my ($size, undef, $used, $parent) = file_size_info($path); |
459 | die "file_size_info on '$volname' failed\n" if !($format && defined($size)); | |
2502b33b DM |
460 | |
461 | die "volname '$volname' contains wrong information about parent\n" | |
462 | if $basename && (!$parent || $parent ne "../$basevmid/$basename"); | |
463 | ||
464 | my $newname = $name; | |
465 | $newname =~ s/^vm-/base-/; | |
466 | ||
467 | my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" : | |
468 | "$vmid/$newname"; | |
469 | ||
08480ce7 | 470 | my $newpath = $class->filesystem_path($scfg, $newvolname); |
2502b33b DM |
471 | |
472 | die "file '$newpath' already exists\n" if -f $newpath; | |
473 | ||
1a3459ac | 474 | rename($path, $newpath) || |
2502b33b DM |
475 | die "rename '$path' to '$newpath' failed - $!\n"; |
476 | ||
c803c396 | 477 | # We try to protect base volume |
2502b33b | 478 | |
c803c396 DM |
479 | chmod(0444, $newpath); # nobody should write anything |
480 | ||
481 | # also try to set immutable flag | |
482 | eval { run_command(['/usr/bin/chattr', '+i', $newpath]); }; | |
483 | warn $@ if $@; | |
1a3459ac | 484 | |
2502b33b DM |
485 | return $newvolname; |
486 | } | |
487 | ||
488 | my $find_free_diskname = sub { | |
489 | my ($imgdir, $vmid, $fmt) = @_; | |
490 | ||
491 | my $disk_ids = {}; | |
1a3459ac | 492 | PVE::Tools::dir_glob_foreach($imgdir, |
2502b33b DM |
493 | qr!(vm|base)-$vmid-disk-(\d+)\..*!, |
494 | sub { | |
1a3459ac | 495 | my ($fn, $type, $disk) = @_; |
2502b33b DM |
496 | $disk_ids->{$disk} = 1; |
497 | }); | |
498 | ||
499 | for (my $i = 1; $i < 100; $i++) { | |
500 | if (!$disk_ids->{$i}) { | |
501 | return "vm-$vmid-disk-$i.$fmt"; | |
502 | } | |
503 | } | |
504 | ||
505 | die "unable to allocate a new image name for VM $vmid in '$imgdir'\n"; | |
506 | }; | |
507 | ||
508 | sub clone_image { | |
f236eaf8 | 509 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
2502b33b DM |
510 | |
511 | # this only works for file based storage types | |
512 | die "storage definintion has no path\n" if !$scfg->{path}; | |
513 | ||
35533c68 | 514 | my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) = |
2502b33b DM |
515 | $class->parse_volname($volname); |
516 | ||
517 | die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images'; | |
518 | ||
f236eaf8 SP |
519 | die "this storage type does not support clone_image on snapshot\n" if $snap; |
520 | ||
35533c68 DM |
521 | die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol'; |
522 | ||
f236eaf8 | 523 | die "clone_image only works on base images\n" if !$isBase; |
1dc01b9f DM |
524 | |
525 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
526 | $imagedir .= "/$vmid"; | |
527 | ||
528 | mkpath $imagedir; | |
529 | ||
2502b33b DM |
530 | my $name = &$find_free_diskname($imagedir, $vmid, "qcow2"); |
531 | ||
532 | warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n"; | |
533 | ||
534 | my $newvol = "$basevmid/$basename/$vmid/$name"; | |
535 | ||
08480ce7 | 536 | my $path = $class->filesystem_path($scfg, $newvol); |
2502b33b | 537 | |
1a3459ac | 538 | # Note: we use relative paths, so we need to call chdir before qemu-img |
2502b33b | 539 | eval { |
7fc619d5 | 540 | local $CWD = $imagedir; |
2502b33b | 541 | |
1a3459ac | 542 | my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename", |
2502b33b | 543 | '-f', 'qcow2', $path]; |
1a3459ac | 544 | |
2502b33b DM |
545 | run_command($cmd); |
546 | }; | |
547 | my $err = $@; | |
1dc01b9f | 548 | |
2502b33b DM |
549 | die $err if $err; |
550 | ||
551 | return $newvol; | |
552 | } | |
553 | ||
554 | sub alloc_image { | |
555 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
556 | ||
557 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
558 | $imagedir .= "/$vmid"; | |
559 | ||
560 | mkpath $imagedir; | |
561 | ||
562 | $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name; | |
1a3459ac | 563 | |
1dc01b9f DM |
564 | my (undef, $tmpfmt) = parse_name_dir($name); |
565 | ||
045ae0a7 | 566 | die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n" |
1dc01b9f DM |
567 | if $tmpfmt ne $fmt; |
568 | ||
569 | my $path = "$imagedir/$name"; | |
570 | ||
571 | die "disk image '$path' already exists\n" if -e $path; | |
572 | ||
35533c68 DM |
573 | if ($fmt eq 'subvol') { |
574 | # only allow this if size = 0, so that user knows what he is doing | |
575 | die "storage does not support subvol quotas\n" if $size != 0; | |
576 | ||
1f5734bb WB |
577 | my $old_umask = umask(0022); |
578 | my $err; | |
579 | mkdir($path) or $err = "unable to create subvol '$path' - $!\n"; | |
580 | umask $old_umask; | |
581 | die $err if $err; | |
35533c68 DM |
582 | } else { |
583 | my $cmd = ['/usr/bin/qemu-img', 'create']; | |
045ae0a7 | 584 | |
35533c68 DM |
585 | push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2'; |
586 | ||
587 | push @$cmd, '-f', $fmt, $path, "${size}K"; | |
1dc01b9f | 588 | |
35533c68 DM |
589 | run_command($cmd, errmsg => "unable to create image"); |
590 | } | |
591 | ||
1dc01b9f DM |
592 | return "$vmid/$name"; |
593 | } | |
594 | ||
595 | sub free_image { | |
35533c68 | 596 | my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_; |
1dc01b9f | 597 | |
08480ce7 | 598 | my $path = $class->filesystem_path($scfg, $volname); |
1dc01b9f | 599 | |
f5451f28 DC |
600 | if ($isBase) { |
601 | # try to remove immutable flag | |
602 | eval { run_command(['/usr/bin/chattr', '-i', $path]); }; | |
603 | warn $@ if $@; | |
604 | } | |
605 | ||
4a7d2222 | 606 | if (defined($format) && ($format eq 'subvol')) { |
35533c68 DM |
607 | File::Path::remove_tree($path); |
608 | } else { | |
609 | ||
610 | if (! -f $path) { | |
611 | warn "disk image '$path' does not exists\n"; | |
612 | return undef; | |
613 | } | |
1dc01b9f | 614 | |
35533c68 DM |
615 | unlink($path) || die "unlink '$path' failed - $!\n"; |
616 | } | |
617 | ||
1dc01b9f DM |
618 | return undef; |
619 | } | |
620 | ||
621 | sub file_size_info { | |
622 | my ($filename, $timeout) = @_; | |
623 | ||
35533c68 DM |
624 | if (-d $filename) { |
625 | return wantarray ? (0, 'subvol', 0, undef) : 1; | |
626 | } | |
627 | ||
1dc01b9f DM |
628 | my $cmd = ['/usr/bin/qemu-img', 'info', $filename]; |
629 | ||
630 | my $format; | |
73b7847e | 631 | my $parent; |
1dc01b9f DM |
632 | my $size = 0; |
633 | my $used = 0; | |
634 | ||
635 | eval { | |
636 | run_command($cmd, timeout => $timeout, outfunc => sub { | |
637 | my $line = shift; | |
1dc01b9f DM |
638 | if ($line =~ m/^file format:\s+(\S+)\s*$/) { |
639 | $format = $1; | |
73b7847e AD |
640 | } elsif ($line =~ m/^backing file:\s(\S+)\s/) { |
641 | $parent = $1; | |
1dc01b9f DM |
642 | } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) { |
643 | $size = int($1); | |
644 | } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) { | |
645 | $used = $1; | |
646 | my $u = $3; | |
647 | ||
648 | $used *= 1024 if $u eq 'K'; | |
649 | $used *= (1024*1024) if $u eq 'M'; | |
650 | $used *= (1024*1024*1024) if $u eq 'G'; | |
651 | $used *= (1024*1024*1024*1024) if $u eq 'T'; | |
652 | ||
653 | $used = int($used); | |
654 | } | |
655 | }); | |
656 | }; | |
657 | ||
73b7847e | 658 | return wantarray ? ($size, $format, $used, $parent) : $size; |
1dc01b9f DM |
659 | } |
660 | ||
e47e548e AD |
661 | sub volume_size_info { |
662 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
08480ce7 | 663 | my $path = $class->filesystem_path($scfg, $volname); |
e47e548e AD |
664 | return file_size_info($path, $timeout); |
665 | ||
666 | } | |
667 | ||
81f5058c AD |
668 | sub volume_resize { |
669 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
670 | ||
6d788031 | 671 | die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/; |
81f5058c AD |
672 | |
673 | return 1 if $running; | |
674 | ||
08480ce7 | 675 | my $path = $class->filesystem_path($scfg, $volname); |
81f5058c | 676 | |
0589e5f9 WL |
677 | my $format = ($class->parse_volname($volname))[6]; |
678 | ||
679 | my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size]; | |
81f5058c | 680 | |
1059cc99 | 681 | run_command($cmd, timeout => 10); |
81f5058c AD |
682 | |
683 | return undef; | |
684 | } | |
685 | ||
7dcb0697 | 686 | sub volume_snapshot { |
f5640e7d | 687 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
7dcb0697 | 688 | |
6d788031 | 689 | die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/; |
7dcb0697 | 690 | |
08480ce7 | 691 | my $path = $class->filesystem_path($scfg, $volname); |
7dcb0697 AD |
692 | |
693 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path]; | |
694 | ||
3f13fd7d | 695 | run_command($cmd); |
7dcb0697 AD |
696 | |
697 | return undef; | |
698 | } | |
699 | ||
1597f1f9 WL |
700 | sub volume_rollback_is_possible { |
701 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
702 | ||
703 | return 1; | |
704 | } | |
705 | ||
41dffa85 AD |
706 | sub volume_snapshot_rollback { |
707 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
708 | ||
6d788031 | 709 | die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/; |
41dffa85 | 710 | |
08480ce7 | 711 | my $path = $class->filesystem_path($scfg, $volname); |
41dffa85 AD |
712 | |
713 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path]; | |
714 | ||
3f13fd7d | 715 | run_command($cmd); |
41dffa85 AD |
716 | |
717 | return undef; | |
718 | } | |
719 | ||
6000a061 AD |
720 | sub volume_snapshot_delete { |
721 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
722 | ||
6d788031 | 723 | die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/; |
6000a061 AD |
724 | |
725 | return 1 if $running; | |
726 | ||
08480ce7 | 727 | my $path = $class->filesystem_path($scfg, $volname); |
6000a061 | 728 | |
399581a2 WB |
729 | $class->deactivate_volume($storeid, $scfg, $volname, $snap, {}); |
730 | ||
6000a061 AD |
731 | my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path]; |
732 | ||
3f13fd7d | 733 | run_command($cmd); |
6000a061 AD |
734 | |
735 | return undef; | |
736 | } | |
737 | ||
f884fe11 AD |
738 | sub volume_has_feature { |
739 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
740 | ||
741 | my $features = { | |
5649ccfe AD |
742 | snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} }, |
743 | clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} }, | |
35533c68 | 744 | template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} }, |
5649ccfe | 745 | copy => { base => {qcow2 => 1, raw => 1, vmdk => 1}, |
22b8cf97 AD |
746 | current => {qcow2 => 1, raw => 1, vmdk => 1}, |
747 | snap => {qcow2 => 1} }, | |
baafddbd DC |
748 | sparseinit => { base => {qcow2 => 1, raw => 1, vmdk => 1}, |
749 | current => {qcow2 => 1, raw => 1, vmdk => 1} }, | |
f884fe11 AD |
750 | }; |
751 | ||
35533c68 | 752 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = |
dc4f2cb3 AD |
753 | $class->parse_volname($volname); |
754 | ||
dc4f2cb3 AD |
755 | my $key = undef; |
756 | if($snapname){ | |
2c5a7097 | 757 | $key = 'snap'; |
dc4f2cb3 AD |
758 | }else{ |
759 | $key = $isBase ? 'base' : 'current'; | |
f884fe11 | 760 | } |
dc4f2cb3 AD |
761 | |
762 | return 1 if defined($features->{$feature}->{$key}->{$format}); | |
763 | ||
f884fe11 AD |
764 | return undef; |
765 | } | |
766 | ||
1dc01b9f DM |
767 | sub list_images { |
768 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
769 | ||
770 | my $imagedir = $class->get_subdir($scfg, 'images'); | |
771 | ||
772 | my ($defFmt, $vaidFmts) = default_format($scfg); | |
045ae0a7 | 773 | my $fmts = join ('|', @$vaidFmts); |
1dc01b9f DM |
774 | |
775 | my $res = []; | |
776 | ||
777 | foreach my $fn (<$imagedir/[0-9][0-9]*/*>) { | |
778 | ||
779 | next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!; | |
780 | $fn = $1; # untaint | |
781 | ||
782 | my $owner = $2; | |
783 | my $name = $3; | |
1dc01b9f | 784 | |
2502b33b | 785 | next if !$vollist && defined($vmid) && ($owner ne $vmid); |
1dc01b9f | 786 | |
73b7847e | 787 | my ($size, $format, $used, $parent) = file_size_info($fn); |
35533c68 | 788 | next if !($format && defined($size)); |
2502b33b DM |
789 | |
790 | my $volid; | |
791 | if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) { | |
792 | my ($basevmid, $basename) = ($1, $2); | |
793 | $volid = "$storeid:$basevmid/$basename/$owner/$name"; | |
794 | } else { | |
795 | $volid = "$storeid:$owner/$name"; | |
796 | } | |
1dc01b9f | 797 | |
2502b33b DM |
798 | if ($vollist) { |
799 | my $found = grep { $_ eq $volid } @$vollist; | |
800 | next if !$found; | |
1dc01b9f DM |
801 | } |
802 | ||
2502b33b DM |
803 | push @$res, { |
804 | volid => $volid, format => $format, | |
1a3459ac | 805 | size => $size, vmid => $owner, used => $used, parent => $parent |
2502b33b | 806 | }; |
1dc01b9f DM |
807 | } |
808 | ||
809 | return $res; | |
810 | } | |
811 | ||
812 | sub status { | |
813 | my ($class, $storeid, $scfg, $cache) = @_; | |
814 | ||
815 | my $path = $scfg->{path}; | |
816 | ||
817 | die "storage definintion has no path\n" if !$path; | |
045ae0a7 | 818 | |
1dc01b9f DM |
819 | my $timeout = 2; |
820 | my $res = PVE::Tools::df($path, $timeout); | |
821 | ||
822 | return undef if !$res || !$res->{total}; | |
823 | ||
824 | return ($res->{total}, $res->{avail}, $res->{used}, 1); | |
825 | } | |
826 | ||
aefe82ea WL |
827 | sub volume_snapshot_list { |
828 | my ($class, $scfg, $storeid, $volname, $prefix, $ip) = @_; | |
829 | ||
830 | # implement in subclass | |
831 | die "Volume_snapshot_list is not implemented for $class"; | |
832 | ||
833 | # retrun an empty array if dataset does not exist. | |
834 | } | |
835 | ||
1dc01b9f DM |
836 | sub activate_storage { |
837 | my ($class, $storeid, $scfg, $cache) = @_; | |
838 | ||
839 | my $path = $scfg->{path}; | |
840 | ||
841 | die "storage definintion has no path\n" if !$path; | |
842 | ||
843 | die "unable to activate storage '$storeid' - " . | |
844 | "directory '$path' does not exist\n" if ! -d $path; | |
845 | ||
c7616abc WB |
846 | return if defined($scfg->{mkdir}) && !$scfg->{mkdir}; |
847 | ||
1dc01b9f DM |
848 | if (defined($scfg->{content})) { |
849 | foreach my $vtype (keys %$vtype_subdirs) { | |
6bcc16d7 DM |
850 | # OpenVZMigrate uses backup (dump) dir |
851 | if (defined($scfg->{content}->{$vtype}) || | |
852 | ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) { | |
853 | my $subdir = $class->get_subdir($scfg, $vtype); | |
854 | mkpath $subdir if $subdir ne $path; | |
855 | } | |
1dc01b9f DM |
856 | } |
857 | } | |
858 | } | |
859 | ||
860 | sub deactivate_storage { | |
861 | my ($class, $storeid, $scfg, $cache) = @_; | |
862 | ||
863 | # do nothing by default | |
864 | } | |
865 | ||
866 | sub activate_volume { | |
02e797b8 | 867 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
1dc01b9f | 868 | |
02e797b8 | 869 | my $path = $class->filesystem_path($scfg, $volname, $snapname); |
1dc01b9f DM |
870 | |
871 | # check is volume exists | |
872 | if ($scfg->{path}) { | |
873 | die "volume '$storeid:$volname' does not exist\n" if ! -e $path; | |
874 | } else { | |
875 | die "volume '$storeid:$volname' does not exist\n" if ! -b $path; | |
876 | } | |
877 | } | |
878 | ||
879 | sub deactivate_volume { | |
02e797b8 | 880 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
1dc01b9f DM |
881 | |
882 | # do nothing by default | |
883 | } | |
884 | ||
c9eeac01 AD |
885 | sub check_connection { |
886 | my ($class, $storeid, $scfg) = @_; | |
887 | # do nothing by default | |
888 | return 1; | |
889 | } | |
890 | ||
e47e548e | 891 | |
1dc01b9f | 892 | 1; |