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