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