]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/Plugin.pm
bump version to 4.0-50
[pve-storage.git] / PVE / Storage / Plugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::Plugin;
2
3use strict;
4use warnings;
7fc619d5 5use File::chdir;
1dc01b9f
DM
6use File::Path;
7use PVE::Tools qw(run_command);
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::Cluster qw(cfs_register_file);
10
11use Data::Dumper;
12
13use base qw(PVE::SectionConfig);
14
045ae0a7 15cfs_register_file ('storage.cfg',
1dc01b9f
DM
16 sub { __PACKAGE__->parse_config(@_); },
17 sub { __PACKAGE__->write_config(@_); });
18
35533c68 19
1dc01b9f
DM
20my $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
56sub 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
67sub 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
77sub 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
94PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
95sub 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
107PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
108sub 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
120PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name);
121sub 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
145PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns);
146sub 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
157PVE::JSONSchema::register_format('pve-storage-content', \&verify_content);
158sub 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
171PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
172sub 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
183PVE::JSONSchema::register_format('pve-storage-options', \&verify_options);
184sub 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
196PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
197sub 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
208sub private {
209 return $defaultData;
210}
211
212sub 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
226sub 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}) {
703de49e
WL
238 warn "storage does not support content type '$c'\n";
239 next;
1dc01b9f
DM
240 }
241 $res->{$c} = 1;
045ae0a7 242 }
1dc01b9f
DM
243
244 if ($res->{none} && scalar (keys %$res) > 1) {
245 die "unable to combine 'none' with other content types\n";
246 }
247
248 return $res;
249 } elsif ($key eq 'format') {
250 my $valid_formats = $def->{format}->[0];
251
252 if (!$valid_formats->{$value}) {
703de49e
WL
253 warn "storage does not support format '$value'\n";
254 next;
1dc01b9f
DM
255 }
256
257 return $value;
258 } elsif ($key eq 'nodes') {
259 my $res = {};
260
261 foreach my $node (PVE::Tools::split_list($value)) {
262 if (PVE::JSONSchema::pve_verify_node_name($node)) {
263 $res->{$node} = 1;
264 }
265 }
266
267 # fixme:
268 # no node restrictions for local storage
269 #if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
270 # die "storage '$storeid' does not allow node restrictions\n";
271 #}
272
273 return $res;
274 }
275
276 return $value;
277}
278
279sub encode_value {
280 my ($class, $type, $key, $value) = @_;
281
282 if ($key eq 'nodes') {
283 return join(',', keys(%$value));
284 } elsif ($key eq 'content') {
285 my $res = content_hash_to_string($value) || 'none';
286 return $res;
287 }
288
289 return $value;
290}
291
292sub parse_config {
293 my ($class, $filename, $raw) = @_;
294
295 my $cfg = $class->SUPER::parse_config($filename, $raw);
296 my $ids = $cfg->{ids};
297
298 # make sure we have a reasonable 'local:' storage
dc6ff39f 299 # we want 'local' to be always the same 'type' (on all cluster nodes)
1dc01b9f
DM
300 if (!$ids->{local} || $ids->{local}->{type} ne 'dir' ||
301 ($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) {
302 $ids->{local} = {
303 type => 'dir',
304 priority => 0, # force first entry
305 path => '/var/lib/vz',
306 maxfiles => 0,
307 content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1},
308 };
309 }
045ae0a7 310
1dc01b9f
DM
311 # make sure we have a path
312 $ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path};
313
314 # remove node restrictions for local storage
315 delete($ids->{local}->{nodes});
316
317 foreach my $storeid (keys %$ids) {
318 my $d = $ids->{$storeid};
319 my $type = $d->{type};
320
321 my $def = $defaultData->{plugindata}->{$type};
322
323 if ($def->{content}) {
324 $d->{content} = $def->{content}->[1] if !$d->{content};
325 }
326
d26e1891 327 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
328 $d->{shared} = 1;
329 }
330 }
331
332 return $cfg;
333}
334
335# Storage implementation
336
337sub cluster_lock_storage {
338 my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
339
340 my $res;
341 if (!$shared) {
342 my $lockid = "pve-storage-$storeid";
343 my $lockdir = "/var/lock/pve-manager";
344 mkdir $lockdir;
045ae0a7 345 $res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
1dc01b9f
DM
346 die $@ if $@;
347 } else {
348 $res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
349 die $@ if $@;
045ae0a7 350 }
1dc01b9f
DM
351 return $res;
352}
353
354sub parse_name_dir {
355 my $name = shift;
356
35533c68
DM
357 if ($name =~ m!^((base-)?[^/\s]+\.(raw|qcow2|vmdk|subvol))$!) {
358 return ($1, $3, $2); # (name, format, isBase)
1dc01b9f
DM
359 }
360
361 die "unable to parse volume filename '$name'\n";
362}
363
364sub parse_volname {
365 my ($class, $volname) = @_;
366
2502b33b
DM
367 if ($volname =~ m!^(\d+)/(\S+)/(\d+)/(\S+)$!) {
368 my ($basedvmid, $basename) = ($1, $2);
369 parse_name_dir($basename);
370 my ($vmid, $name) = ($3, $4);
35533c68
DM
371 my (undef, $format, $isBase) = parse_name_dir($name);
372 return ('images', $name, $vmid, $basename, $basedvmid, $isBase, $format);
2502b33b 373 } elsif ($volname =~ m!^(\d+)/(\S+)$!) {
1dc01b9f 374 my ($vmid, $name) = ($1, $2);
35533c68
DM
375 my (undef, $format, $isBase) = parse_name_dir($name);
376 return ('images', $name, $vmid, undef, undef, $isBase, $format);
1dc01b9f
DM
377 } elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
378 return ('iso', $1);
13d2cb79 379 } elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.[gx]z)$!) {
1dc01b9f
DM
380 return ('vztmpl', $1);
381 } elsif ($volname =~ m!^rootdir/(\d+)$!) {
382 return ('rootdir', $1, $1);
a22854e5 383 } elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo)))$!) {
1dc01b9f 384 my $fn = $1;
4cb6e060 385 if ($fn =~ m/^vzdump-(openvz|lxc|qemu)-(\d+)-.+/) {
1dc01b9f
DM
386 return ('backup', $fn, $2);
387 }
388 return ('backup', $fn);
389 }
390
391 die "unable to parse directory volume name '$volname'\n";
392}
393
045ae0a7 394my $vtype_subdirs = {
1dc01b9f
DM
395 images => 'images',
396 rootdir => 'private',
397 iso => 'template/iso',
398 vztmpl => 'template/cache',
399 backup => 'dump',
400};
401
402sub get_subdir {
403 my ($class, $scfg, $vtype) = @_;
404
405 my $path = $scfg->{path};
406
407 die "storage definintion has no path\n" if !$path;
408
409 my $subdir = $vtype_subdirs->{$vtype};
410
411 die "unknown vtype '$vtype'\n" if !defined($subdir);
412
045ae0a7 413 return "$path/$subdir";
1dc01b9f
DM
414}
415
08480ce7 416sub filesystem_path {
e67069eb 417 my ($class, $scfg, $volname, $snapname) = @_;
1dc01b9f 418
e67069eb
DM
419 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
420 $class->parse_volname($volname);
421
422 # Note: qcow2/qed has internal snapshot, so path is always
423 # the same (with or without snapshot => same file).
424 die "can't snapshot this image format\n"
425 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
1dc01b9f
DM
426
427 my $dir = $class->get_subdir($scfg, $vtype);
428
429 $dir .= "/$vmid" if $vtype eq 'images';
430
431 my $path = "$dir/$name";
432
433 return wantarray ? ($path, $vmid, $vtype) : $path;
434}
435
08480ce7 436sub path {
e67069eb 437 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
08480ce7 438
e67069eb 439 return $class->filesystem_path($scfg, $volname, $snapname);
08480ce7
DM
440}
441
2502b33b
DM
442sub create_base {
443 my ($class, $storeid, $scfg, $volname) = @_;
444
445 # this only works for file based storage types
446 die "storage definintion has no path\n" if !$scfg->{path};
447
35533c68 448 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
2502b33b
DM
449 $class->parse_volname($volname);
450
451 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
452
453 die "create_base not possible with base image\n" if $isBase;
454
08480ce7 455 my $path = $class->filesystem_path($scfg, $volname);
2502b33b 456
35533c68
DM
457 my ($size, undef, $used, $parent) = file_size_info($path);
458 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
2502b33b
DM
459
460 die "volname '$volname' contains wrong information about parent\n"
461 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
462
463 my $newname = $name;
464 $newname =~ s/^vm-/base-/;
465
466 my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" :
467 "$vmid/$newname";
468
08480ce7 469 my $newpath = $class->filesystem_path($scfg, $newvolname);
2502b33b
DM
470
471 die "file '$newpath' already exists\n" if -f $newpath;
472
1a3459ac 473 rename($path, $newpath) ||
2502b33b
DM
474 die "rename '$path' to '$newpath' failed - $!\n";
475
c803c396 476 # We try to protect base volume
2502b33b 477
c803c396
DM
478 chmod(0444, $newpath); # nobody should write anything
479
480 # also try to set immutable flag
481 eval { run_command(['/usr/bin/chattr', '+i', $newpath]); };
482 warn $@ if $@;
1a3459ac 483
2502b33b
DM
484 return $newvolname;
485}
486
487my $find_free_diskname = sub {
488 my ($imgdir, $vmid, $fmt) = @_;
489
490 my $disk_ids = {};
1a3459ac 491 PVE::Tools::dir_glob_foreach($imgdir,
2502b33b
DM
492 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
493 sub {
1a3459ac 494 my ($fn, $type, $disk) = @_;
2502b33b
DM
495 $disk_ids->{$disk} = 1;
496 });
497
498 for (my $i = 1; $i < 100; $i++) {
499 if (!$disk_ids->{$i}) {
500 return "vm-$vmid-disk-$i.$fmt";
501 }
502 }
503
504 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
505};
506
507sub clone_image {
f236eaf8 508 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
2502b33b
DM
509
510 # this only works for file based storage types
511 die "storage definintion has no path\n" if !$scfg->{path};
512
35533c68 513 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
2502b33b
DM
514 $class->parse_volname($volname);
515
516 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
517
f236eaf8
SP
518 die "this storage type does not support clone_image on snapshot\n" if $snap;
519
35533c68
DM
520 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
521
f236eaf8 522 die "clone_image only works on base images\n" if !$isBase;
1dc01b9f
DM
523
524 my $imagedir = $class->get_subdir($scfg, 'images');
525 $imagedir .= "/$vmid";
526
527 mkpath $imagedir;
528
2502b33b
DM
529 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
530
531 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
532
533 my $newvol = "$basevmid/$basename/$vmid/$name";
534
08480ce7 535 my $path = $class->filesystem_path($scfg, $newvol);
2502b33b 536
1a3459ac 537 # Note: we use relative paths, so we need to call chdir before qemu-img
2502b33b 538 eval {
7fc619d5 539 local $CWD = $imagedir;
2502b33b 540
1a3459ac 541 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
2502b33b 542 '-f', 'qcow2', $path];
1a3459ac 543
2502b33b
DM
544 run_command($cmd);
545 };
546 my $err = $@;
1dc01b9f 547
2502b33b
DM
548 die $err if $err;
549
550 return $newvol;
551}
552
553sub alloc_image {
554 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
555
556 my $imagedir = $class->get_subdir($scfg, 'images');
557 $imagedir .= "/$vmid";
558
559 mkpath $imagedir;
560
561 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
1a3459ac 562
1dc01b9f
DM
563 my (undef, $tmpfmt) = parse_name_dir($name);
564
045ae0a7 565 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
1dc01b9f
DM
566 if $tmpfmt ne $fmt;
567
568 my $path = "$imagedir/$name";
569
570 die "disk image '$path' already exists\n" if -e $path;
571
35533c68
DM
572 if ($fmt eq 'subvol') {
573 # only allow this if size = 0, so that user knows what he is doing
574 die "storage does not support subvol quotas\n" if $size != 0;
575
1f5734bb
WB
576 my $old_umask = umask(0022);
577 my $err;
578 mkdir($path) or $err = "unable to create subvol '$path' - $!\n";
579 umask $old_umask;
580 die $err if $err;
35533c68
DM
581 } else {
582 my $cmd = ['/usr/bin/qemu-img', 'create'];
045ae0a7 583
35533c68
DM
584 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
585
586 push @$cmd, '-f', $fmt, $path, "${size}K";
1dc01b9f 587
35533c68
DM
588 run_command($cmd, errmsg => "unable to create image");
589 }
590
1dc01b9f
DM
591 return "$vmid/$name";
592}
593
594sub free_image {
35533c68 595 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
1dc01b9f 596
08480ce7 597 my $path = $class->filesystem_path($scfg, $volname);
1dc01b9f 598
35533c68
DM
599 if ($format eq 'subvol') {
600 File::Path::remove_tree($path);
601 } else {
602
603 if (! -f $path) {
604 warn "disk image '$path' does not exists\n";
605 return undef;
606 }
1dc01b9f 607
35533c68
DM
608 if ($isBase) {
609 # try to remove immutable flag
610 eval { run_command(['/usr/bin/chattr', '-i', $path]); };
611 warn $@ if $@;
612 }
a7f3d909 613
35533c68
DM
614 unlink($path) || die "unlink '$path' failed - $!\n";
615 }
616
1dc01b9f
DM
617 return undef;
618}
619
620sub file_size_info {
621 my ($filename, $timeout) = @_;
622
35533c68
DM
623 if (-d $filename) {
624 return wantarray ? (0, 'subvol', 0, undef) : 1;
625 }
626
1dc01b9f
DM
627 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
628
629 my $format;
73b7847e 630 my $parent;
1dc01b9f
DM
631 my $size = 0;
632 my $used = 0;
633
634 eval {
635 run_command($cmd, timeout => $timeout, outfunc => sub {
636 my $line = shift;
1dc01b9f
DM
637 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
638 $format = $1;
73b7847e
AD
639 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
640 $parent = $1;
1dc01b9f
DM
641 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
642 $size = int($1);
643 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
644 $used = $1;
645 my $u = $3;
646
647 $used *= 1024 if $u eq 'K';
648 $used *= (1024*1024) if $u eq 'M';
649 $used *= (1024*1024*1024) if $u eq 'G';
650 $used *= (1024*1024*1024*1024) if $u eq 'T';
651
652 $used = int($used);
653 }
654 });
655 };
656
73b7847e 657 return wantarray ? ($size, $format, $used, $parent) : $size;
1dc01b9f
DM
658}
659
e47e548e
AD
660sub volume_size_info {
661 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
08480ce7 662 my $path = $class->filesystem_path($scfg, $volname);
e47e548e
AD
663 return file_size_info($path, $timeout);
664
665}
666
81f5058c
AD
667sub volume_resize {
668 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
669
6d788031 670 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
81f5058c
AD
671
672 return 1 if $running;
673
08480ce7 674 my $path = $class->filesystem_path($scfg, $volname);
81f5058c 675
0589e5f9
WL
676 my $format = ($class->parse_volname($volname))[6];
677
678 my $cmd = ['/usr/bin/qemu-img', 'resize', '-f', $format, $path , $size];
81f5058c 679
1059cc99 680 run_command($cmd, timeout => 10);
81f5058c
AD
681
682 return undef;
683}
684
7dcb0697 685sub volume_snapshot {
f5640e7d 686 my ($class, $scfg, $storeid, $volname, $snap) = @_;
7dcb0697 687
6d788031 688 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
7dcb0697 689
08480ce7 690 my $path = $class->filesystem_path($scfg, $volname);
7dcb0697
AD
691
692 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
693
3f13fd7d 694 run_command($cmd);
7dcb0697
AD
695
696 return undef;
697}
698
1597f1f9
WL
699sub volume_rollback_is_possible {
700 my ($class, $scfg, $storeid, $volname, $snap) = @_;
701
702 return 1;
703}
704
41dffa85
AD
705sub volume_snapshot_rollback {
706 my ($class, $scfg, $storeid, $volname, $snap) = @_;
707
6d788031 708 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
41dffa85 709
08480ce7 710 my $path = $class->filesystem_path($scfg, $volname);
41dffa85
AD
711
712 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
713
3f13fd7d 714 run_command($cmd);
41dffa85
AD
715
716 return undef;
717}
718
6000a061
AD
719sub volume_snapshot_delete {
720 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
721
6d788031 722 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
6000a061
AD
723
724 return 1 if $running;
725
08480ce7 726 my $path = $class->filesystem_path($scfg, $volname);
6000a061 727
399581a2
WB
728 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
729
6000a061
AD
730 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
731
3f13fd7d 732 run_command($cmd);
6000a061
AD
733
734 return undef;
735}
736
f884fe11
AD
737sub volume_has_feature {
738 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
739
740 my $features = {
5649ccfe
AD
741 snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
742 clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
35533c68 743 template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} },
5649ccfe 744 copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
22b8cf97
AD
745 current => {qcow2 => 1, raw => 1, vmdk => 1},
746 snap => {qcow2 => 1} },
baafddbd
DC
747 sparseinit => { base => {qcow2 => 1, raw => 1, vmdk => 1},
748 current => {qcow2 => 1, raw => 1, vmdk => 1} },
f884fe11
AD
749 };
750
35533c68 751 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
dc4f2cb3
AD
752 $class->parse_volname($volname);
753
dc4f2cb3
AD
754 my $key = undef;
755 if($snapname){
2c5a7097 756 $key = 'snap';
dc4f2cb3
AD
757 }else{
758 $key = $isBase ? 'base' : 'current';
f884fe11 759 }
dc4f2cb3
AD
760
761 return 1 if defined($features->{$feature}->{$key}->{$format});
762
f884fe11
AD
763 return undef;
764}
765
1dc01b9f
DM
766sub list_images {
767 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
768
769 my $imagedir = $class->get_subdir($scfg, 'images');
770
771 my ($defFmt, $vaidFmts) = default_format($scfg);
045ae0a7 772 my $fmts = join ('|', @$vaidFmts);
1dc01b9f
DM
773
774 my $res = [];
775
776 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
777
778 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
779 $fn = $1; # untaint
780
781 my $owner = $2;
782 my $name = $3;
1dc01b9f 783
2502b33b 784 next if !$vollist && defined($vmid) && ($owner ne $vmid);
1dc01b9f 785
73b7847e 786 my ($size, $format, $used, $parent) = file_size_info($fn);
35533c68 787 next if !($format && defined($size));
2502b33b
DM
788
789 my $volid;
790 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
791 my ($basevmid, $basename) = ($1, $2);
792 $volid = "$storeid:$basevmid/$basename/$owner/$name";
793 } else {
794 $volid = "$storeid:$owner/$name";
795 }
1dc01b9f 796
2502b33b
DM
797 if ($vollist) {
798 my $found = grep { $_ eq $volid } @$vollist;
799 next if !$found;
1dc01b9f
DM
800 }
801
2502b33b
DM
802 push @$res, {
803 volid => $volid, format => $format,
1a3459ac 804 size => $size, vmid => $owner, used => $used, parent => $parent
2502b33b 805 };
1dc01b9f
DM
806 }
807
808 return $res;
809}
810
811sub status {
812 my ($class, $storeid, $scfg, $cache) = @_;
813
814 my $path = $scfg->{path};
815
816 die "storage definintion has no path\n" if !$path;
045ae0a7 817
1dc01b9f
DM
818 my $timeout = 2;
819 my $res = PVE::Tools::df($path, $timeout);
820
821 return undef if !$res || !$res->{total};
822
823 return ($res->{total}, $res->{avail}, $res->{used}, 1);
824}
825
826sub activate_storage {
827 my ($class, $storeid, $scfg, $cache) = @_;
828
829 my $path = $scfg->{path};
830
831 die "storage definintion has no path\n" if !$path;
832
833 die "unable to activate storage '$storeid' - " .
834 "directory '$path' does not exist\n" if ! -d $path;
835
836 if (defined($scfg->{content})) {
837 foreach my $vtype (keys %$vtype_subdirs) {
6bcc16d7
DM
838 # OpenVZMigrate uses backup (dump) dir
839 if (defined($scfg->{content}->{$vtype}) ||
840 ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) {
841 my $subdir = $class->get_subdir($scfg, $vtype);
842 mkpath $subdir if $subdir ne $path;
843 }
1dc01b9f
DM
844 }
845 }
846}
847
848sub deactivate_storage {
849 my ($class, $storeid, $scfg, $cache) = @_;
850
851 # do nothing by default
852}
853
854sub activate_volume {
02e797b8 855 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1dc01b9f 856
02e797b8 857 my $path = $class->filesystem_path($scfg, $volname, $snapname);
1dc01b9f
DM
858
859 # check is volume exists
860 if ($scfg->{path}) {
861 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
862 } else {
863 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
864 }
865}
866
867sub deactivate_volume {
02e797b8 868 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1dc01b9f
DM
869
870 # do nothing by default
871}
872
c9eeac01
AD
873sub check_connection {
874 my ($class, $storeid, $scfg) = @_;
875 # do nothing by default
876 return 1;
877}
878
e47e548e 879
1dc01b9f 8801;