]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/Plugin.pm
iSCSI ipv6 support
[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
1689e627 136 if (!PVE::Tools::parse_host_and_port($portal)) {
1dc01b9f
DM
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 {
e67069eb 406 my ($class, $scfg, $volname, $snapname) = @_;
1dc01b9f 407
e67069eb
DM
408 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
409 $class->parse_volname($volname);
410
411 # Note: qcow2/qed has internal snapshot, so path is always
412 # the same (with or without snapshot => same file).
413 die "can't snapshot this image format\n"
414 if defined($snapname) && $format !~ m/^(qcow2|qed)$/;
1dc01b9f
DM
415
416 my $dir = $class->get_subdir($scfg, $vtype);
417
418 $dir .= "/$vmid" if $vtype eq 'images';
419
420 my $path = "$dir/$name";
421
422 return wantarray ? ($path, $vmid, $vtype) : $path;
423}
424
08480ce7 425sub path {
e67069eb 426 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
08480ce7 427
e67069eb 428 return $class->filesystem_path($scfg, $volname, $snapname);
08480ce7
DM
429}
430
2502b33b
DM
431sub create_base {
432 my ($class, $storeid, $scfg, $volname) = @_;
433
434 # this only works for file based storage types
435 die "storage definintion has no path\n" if !$scfg->{path};
436
35533c68 437 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
2502b33b
DM
438 $class->parse_volname($volname);
439
440 die "create_base on wrong vtype '$vtype'\n" if $vtype ne 'images';
441
442 die "create_base not possible with base image\n" if $isBase;
443
08480ce7 444 my $path = $class->filesystem_path($scfg, $volname);
2502b33b 445
35533c68
DM
446 my ($size, undef, $used, $parent) = file_size_info($path);
447 die "file_size_info on '$volname' failed\n" if !($format && defined($size));
2502b33b
DM
448
449 die "volname '$volname' contains wrong information about parent\n"
450 if $basename && (!$parent || $parent ne "../$basevmid/$basename");
451
452 my $newname = $name;
453 $newname =~ s/^vm-/base-/;
454
455 my $newvolname = $basename ? "$basevmid/$basename/$vmid/$newname" :
456 "$vmid/$newname";
457
08480ce7 458 my $newpath = $class->filesystem_path($scfg, $newvolname);
2502b33b
DM
459
460 die "file '$newpath' already exists\n" if -f $newpath;
461
1a3459ac 462 rename($path, $newpath) ||
2502b33b
DM
463 die "rename '$path' to '$newpath' failed - $!\n";
464
c803c396 465 # We try to protect base volume
2502b33b 466
c803c396
DM
467 chmod(0444, $newpath); # nobody should write anything
468
469 # also try to set immutable flag
470 eval { run_command(['/usr/bin/chattr', '+i', $newpath]); };
471 warn $@ if $@;
1a3459ac 472
2502b33b
DM
473 return $newvolname;
474}
475
476my $find_free_diskname = sub {
477 my ($imgdir, $vmid, $fmt) = @_;
478
479 my $disk_ids = {};
1a3459ac 480 PVE::Tools::dir_glob_foreach($imgdir,
2502b33b
DM
481 qr!(vm|base)-$vmid-disk-(\d+)\..*!,
482 sub {
1a3459ac 483 my ($fn, $type, $disk) = @_;
2502b33b
DM
484 $disk_ids->{$disk} = 1;
485 });
486
487 for (my $i = 1; $i < 100; $i++) {
488 if (!$disk_ids->{$i}) {
489 return "vm-$vmid-disk-$i.$fmt";
490 }
491 }
492
493 die "unable to allocate a new image name for VM $vmid in '$imgdir'\n";
494};
495
496sub clone_image {
f236eaf8 497 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
2502b33b
DM
498
499 # this only works for file based storage types
500 die "storage definintion has no path\n" if !$scfg->{path};
501
35533c68 502 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
2502b33b
DM
503 $class->parse_volname($volname);
504
505 die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
506
f236eaf8
SP
507 die "this storage type does not support clone_image on snapshot\n" if $snap;
508
35533c68
DM
509 die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
510
f236eaf8 511 die "clone_image only works on base images\n" if !$isBase;
1dc01b9f
DM
512
513 my $imagedir = $class->get_subdir($scfg, 'images');
514 $imagedir .= "/$vmid";
515
516 mkpath $imagedir;
517
2502b33b
DM
518 my $name = &$find_free_diskname($imagedir, $vmid, "qcow2");
519
520 warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
521
522 my $newvol = "$basevmid/$basename/$vmid/$name";
523
08480ce7 524 my $path = $class->filesystem_path($scfg, $newvol);
2502b33b 525
1a3459ac 526 # Note: we use relative paths, so we need to call chdir before qemu-img
2502b33b 527 eval {
7fc619d5 528 local $CWD = $imagedir;
2502b33b 529
1a3459ac 530 my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
2502b33b 531 '-f', 'qcow2', $path];
1a3459ac 532
2502b33b
DM
533 run_command($cmd);
534 };
535 my $err = $@;
1dc01b9f 536
2502b33b
DM
537 die $err if $err;
538
539 return $newvol;
540}
541
542sub alloc_image {
543 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
544
545 my $imagedir = $class->get_subdir($scfg, 'images');
546 $imagedir .= "/$vmid";
547
548 mkpath $imagedir;
549
550 $name = &$find_free_diskname($imagedir, $vmid, $fmt) if !$name;
1a3459ac 551
1dc01b9f
DM
552 my (undef, $tmpfmt) = parse_name_dir($name);
553
045ae0a7 554 die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
1dc01b9f
DM
555 if $tmpfmt ne $fmt;
556
557 my $path = "$imagedir/$name";
558
559 die "disk image '$path' already exists\n" if -e $path;
560
35533c68
DM
561 if ($fmt eq 'subvol') {
562 # only allow this if size = 0, so that user knows what he is doing
563 die "storage does not support subvol quotas\n" if $size != 0;
564
565 (mkdir $path) || die "unable to create subvol '$path' - $!\n";
566 } else {
567 my $cmd = ['/usr/bin/qemu-img', 'create'];
045ae0a7 568
35533c68
DM
569 push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
570
571 push @$cmd, '-f', $fmt, $path, "${size}K";
1dc01b9f 572
35533c68
DM
573 run_command($cmd, errmsg => "unable to create image");
574 }
575
1dc01b9f
DM
576 return "$vmid/$name";
577}
578
579sub free_image {
35533c68 580 my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
1dc01b9f 581
08480ce7 582 my $path = $class->filesystem_path($scfg, $volname);
1dc01b9f 583
35533c68
DM
584 if ($format eq 'subvol') {
585 File::Path::remove_tree($path);
586 } else {
587
588 if (! -f $path) {
589 warn "disk image '$path' does not exists\n";
590 return undef;
591 }
1dc01b9f 592
35533c68
DM
593 if ($isBase) {
594 # try to remove immutable flag
595 eval { run_command(['/usr/bin/chattr', '-i', $path]); };
596 warn $@ if $@;
597 }
a7f3d909 598
35533c68
DM
599 unlink($path) || die "unlink '$path' failed - $!\n";
600 }
601
1dc01b9f
DM
602 return undef;
603}
604
605sub file_size_info {
606 my ($filename, $timeout) = @_;
607
35533c68
DM
608 if (-d $filename) {
609 return wantarray ? (0, 'subvol', 0, undef) : 1;
610 }
611
1dc01b9f
DM
612 my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
613
614 my $format;
73b7847e 615 my $parent;
1dc01b9f
DM
616 my $size = 0;
617 my $used = 0;
618
619 eval {
620 run_command($cmd, timeout => $timeout, outfunc => sub {
621 my $line = shift;
1dc01b9f
DM
622 if ($line =~ m/^file format:\s+(\S+)\s*$/) {
623 $format = $1;
73b7847e
AD
624 } elsif ($line =~ m/^backing file:\s(\S+)\s/) {
625 $parent = $1;
1dc01b9f
DM
626 } elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
627 $size = int($1);
628 } elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
629 $used = $1;
630 my $u = $3;
631
632 $used *= 1024 if $u eq 'K';
633 $used *= (1024*1024) if $u eq 'M';
634 $used *= (1024*1024*1024) if $u eq 'G';
635 $used *= (1024*1024*1024*1024) if $u eq 'T';
636
637 $used = int($used);
638 }
639 });
640 };
641
73b7847e 642 return wantarray ? ($size, $format, $used, $parent) : $size;
1dc01b9f
DM
643}
644
e47e548e
AD
645sub volume_size_info {
646 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
08480ce7 647 my $path = $class->filesystem_path($scfg, $volname);
e47e548e
AD
648 return file_size_info($path, $timeout);
649
650}
651
81f5058c
AD
652sub volume_resize {
653 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
654
6d788031 655 die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
81f5058c
AD
656
657 return 1 if $running;
658
08480ce7 659 my $path = $class->filesystem_path($scfg, $volname);
81f5058c
AD
660
661 my $cmd = ['/usr/bin/qemu-img', 'resize', $path , $size];
662
1059cc99 663 run_command($cmd, timeout => 10);
81f5058c
AD
664
665 return undef;
666}
667
7dcb0697 668sub volume_snapshot {
f5640e7d 669 my ($class, $scfg, $storeid, $volname, $snap) = @_;
7dcb0697 670
6d788031 671 die "can't snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
7dcb0697 672
08480ce7 673 my $path = $class->filesystem_path($scfg, $volname);
7dcb0697
AD
674
675 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-c', $snap, $path];
676
3f13fd7d 677 run_command($cmd);
7dcb0697
AD
678
679 return undef;
680}
681
1597f1f9
WL
682sub volume_rollback_is_possible {
683 my ($class, $scfg, $storeid, $volname, $snap) = @_;
684
685 return 1;
686}
687
41dffa85
AD
688sub volume_snapshot_rollback {
689 my ($class, $scfg, $storeid, $volname, $snap) = @_;
690
6d788031 691 die "can't rollback snapshot this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
41dffa85 692
08480ce7 693 my $path = $class->filesystem_path($scfg, $volname);
41dffa85
AD
694
695 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-a', $snap, $path];
696
3f13fd7d 697 run_command($cmd);
41dffa85
AD
698
699 return undef;
700}
701
6000a061
AD
702sub volume_snapshot_delete {
703 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
704
6d788031 705 die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
6000a061
AD
706
707 return 1 if $running;
708
08480ce7 709 my $path = $class->filesystem_path($scfg, $volname);
6000a061
AD
710
711 my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
712
3f13fd7d 713 run_command($cmd);
6000a061
AD
714
715 return undef;
716}
717
f884fe11
AD
718sub volume_has_feature {
719 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
720
721 my $features = {
5649ccfe
AD
722 snapshot => { current => { qcow2 => 1}, snap => { qcow2 => 1} },
723 clone => { base => {qcow2 => 1, raw => 1, vmdk => 1} },
35533c68 724 template => { current => {qcow2 => 1, raw => 1, vmdk => 1, subvol => 1} },
5649ccfe 725 copy => { base => {qcow2 => 1, raw => 1, vmdk => 1},
22b8cf97
AD
726 current => {qcow2 => 1, raw => 1, vmdk => 1},
727 snap => {qcow2 => 1} },
f884fe11
AD
728 };
729
35533c68 730 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
dc4f2cb3
AD
731 $class->parse_volname($volname);
732
dc4f2cb3
AD
733 my $key = undef;
734 if($snapname){
2c5a7097 735 $key = 'snap';
dc4f2cb3
AD
736 }else{
737 $key = $isBase ? 'base' : 'current';
f884fe11 738 }
dc4f2cb3
AD
739
740 return 1 if defined($features->{$feature}->{$key}->{$format});
741
f884fe11
AD
742 return undef;
743}
744
1dc01b9f
DM
745sub list_images {
746 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
747
748 my $imagedir = $class->get_subdir($scfg, 'images');
749
750 my ($defFmt, $vaidFmts) = default_format($scfg);
045ae0a7 751 my $fmts = join ('|', @$vaidFmts);
1dc01b9f
DM
752
753 my $res = [];
754
755 foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
756
757 next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
758 $fn = $1; # untaint
759
760 my $owner = $2;
761 my $name = $3;
1dc01b9f 762
2502b33b 763 next if !$vollist && defined($vmid) && ($owner ne $vmid);
1dc01b9f 764
73b7847e 765 my ($size, $format, $used, $parent) = file_size_info($fn);
35533c68 766 next if !($format && defined($size));
2502b33b
DM
767
768 my $volid;
769 if ($parent && $parent =~ m!^../(\d+)/([^/]+\.($fmts))$!) {
770 my ($basevmid, $basename) = ($1, $2);
771 $volid = "$storeid:$basevmid/$basename/$owner/$name";
772 } else {
773 $volid = "$storeid:$owner/$name";
774 }
1dc01b9f 775
2502b33b
DM
776 if ($vollist) {
777 my $found = grep { $_ eq $volid } @$vollist;
778 next if !$found;
1dc01b9f
DM
779 }
780
2502b33b
DM
781 push @$res, {
782 volid => $volid, format => $format,
1a3459ac 783 size => $size, vmid => $owner, used => $used, parent => $parent
2502b33b 784 };
1dc01b9f
DM
785 }
786
787 return $res;
788}
789
790sub status {
791 my ($class, $storeid, $scfg, $cache) = @_;
792
793 my $path = $scfg->{path};
794
795 die "storage definintion has no path\n" if !$path;
045ae0a7 796
1dc01b9f
DM
797 my $timeout = 2;
798 my $res = PVE::Tools::df($path, $timeout);
799
800 return undef if !$res || !$res->{total};
801
802 return ($res->{total}, $res->{avail}, $res->{used}, 1);
803}
804
805sub activate_storage {
806 my ($class, $storeid, $scfg, $cache) = @_;
807
808 my $path = $scfg->{path};
809
810 die "storage definintion has no path\n" if !$path;
811
812 die "unable to activate storage '$storeid' - " .
813 "directory '$path' does not exist\n" if ! -d $path;
814
815 if (defined($scfg->{content})) {
816 foreach my $vtype (keys %$vtype_subdirs) {
6bcc16d7
DM
817 # OpenVZMigrate uses backup (dump) dir
818 if (defined($scfg->{content}->{$vtype}) ||
819 ($vtype eq 'backup' && defined($scfg->{content}->{'rootdir'}))) {
820 my $subdir = $class->get_subdir($scfg, $vtype);
821 mkpath $subdir if $subdir ne $path;
822 }
1dc01b9f
DM
823 }
824 }
825}
826
827sub deactivate_storage {
828 my ($class, $storeid, $scfg, $cache) = @_;
829
830 # do nothing by default
831}
832
833sub activate_volume {
834 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
835
08480ce7 836 my $path = $class->filesystem_path($scfg, $volname);
1dc01b9f
DM
837
838 # check is volume exists
839 if ($scfg->{path}) {
840 die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
841 } else {
842 die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
843 }
844}
845
846sub deactivate_volume {
847 my ($class, $storeid, $scfg, $volname, $cache) = @_;
848
849 # do nothing by default
850}
851
c9eeac01
AD
852sub check_connection {
853 my ($class, $storeid, $scfg) = @_;
854 # do nothing by default
855 return 1;
856}
857
e47e548e 858
1dc01b9f 8591;