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