]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/RBDPlugin.pm
rbd: allow to use client custom ceph conf for each storeid
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
CommitLineData
0509010d
AD
1package PVE::Storage::RBDPlugin;
2
3use strict;
4use warnings;
5use IO::File;
e858048f 6use Net::IP;
0509010d
AD
7use PVE::Tools qw(run_command trim);
8use PVE::Storage::Plugin;
9use PVE::JSONSchema qw(get_standard_option);
10
11use base qw(PVE::Storage::Plugin);
12
249cb647
SP
13sub rbd_unittobytes {
14 {
15 "M" => 1024*1024,
16 "G" => 1024*1024*1024,
17 "T" => 1024*1024*1024*1024,
18 }
19}
20
8897f5dc
SP
21my $add_pool_to_disk = sub {
22 my ($scfg, $disk) = @_;
23
24 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
25
26 return "$pool/$disk";
27};
28
e858048f
WB
29my $hostlist = sub {
30 my ($list_text, $separator) = @_;
31 my @monhostlist = PVE::Tools::split_list($list_text);
32 return join($separator, map {
33 my ($host, $port) = PVE::Tools::parse_host_and_port($_);
34 $port = defined($port) ? ":$port" : '';
35 $host = "[$host]" if Net::IP::ip_is_ipv6($host);
36 "${host}${port}"
37 } @monhostlist);
38};
39
411476cd
DM
40my $rbd_cmd = sub {
41 my ($scfg, $storeid, $op, @options) = @_;
0509010d 42
e858048f 43 my $monhost = &$hostlist($scfg->{monhost}, ',');
0509010d 44
1440604a
AD
45 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
46 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
47 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
48
49 my $cmd = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost];
50
51 if(-e $keyring){
52 push @$cmd, '-n', "client.$username";
53 push @$cmd, '--keyring', $keyring;
54 push @$cmd, '--auth_supported', 'cephx';
55 }else{
56 push @$cmd, '--auth_supported', 'none';
57 }
58
13417225
AD
59 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
60
61 if(-e $cephconfig){
62 push @$cmd, '-c', $cephconfig;
63 }
64
1440604a 65 push @$cmd, $op;
3e195ccc 66
411476cd 67 push @$cmd, @options if scalar(@options);
3e195ccc 68
411476cd
DM
69 return $cmd;
70};
0509010d 71
69589444
AD
72my $rados_cmd = sub {
73 my ($scfg, $storeid, $op, @options) = @_;
74
e858048f 75 my $monhost = &$hostlist($scfg->{monhost}, ',');
69589444 76
1440604a
AD
77 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
78 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
79 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
80
81 my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost];
82
83 if(-e $keyring){
84 push @$cmd, '-n', "client.$username";
85 push @$cmd, '--keyring', $keyring;
86 push @$cmd, '--auth_supported', 'cephx';
87 }else{
88 push @$cmd, '--auth_supported', 'none';
89 }
90
13417225
AD
91 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
92
93 if(-e $cephconfig){
94 push @$cmd, '-c', $cephconfig;
95 }
96
1440604a 97 push @$cmd, $op;
69589444
AD
98
99 push @$cmd, @options if scalar(@options);
100
101 return $cmd;
102};
103
d86fd0a4
FG
104# needed for volumes created using ceph jewel (or higher)
105my $krdb_feature_disable = sub {
106 my ($scfg, $storeid, $name) = @_;
107
108 return 1 if !$scfg->{krbd};
109
110 my ($major, undef, undef, undef) = ceph_version();
111 return 1 if $major < 10;
112
113 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock');
114 run_rbd_command($feature_cmd, errmsg => "could not disable krbd-incompatible image features of rbd volume $name");
115};
116
7aeda033
FG
117my $ceph_version_parser = sub {
118 my $line = shift;
119 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))?$/) {
120 return ($2, $3, $4, $1);
121 } else {
122 warn "Could not parse Ceph version: '$line'\n";
123 }
124};
125
126sub ceph_version {
127 my ($cache) = @_;
128
129 my $version_string = $cache;
130
131 my $major;
132 my $minor;
133 my $bugfix;
134
135 if (defined($version_string)) {
0be02e0f 136 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
7aeda033 137 } else {
e76dbd92 138 run_command('ceph --version', outfunc => sub {
7aeda033 139 my $line = shift;
0be02e0f 140 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
7aeda033
FG
141 });
142 }
143 return undef if !defined($version_string);
144 return wantarray ? ($major, $minor, $bugfix, $version_string) : $version_string;
145}
146
c693f749
SP
147sub run_rbd_command {
148 my ($cmd, %args) = @_;
149
150 my $lasterr;
151 my $errmsg = $args{errmsg} . ": " || "";
c97c5b3b 152 if (!exists($args{errfunc})) {
c693f749
SP
153 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
154 # at least 1 child(ren) in pool cephstor1
155 $args{errfunc} = sub {
c97c5b3b
DM
156 my $line = shift;
157 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
158 $lasterr = "$1\n";
159 } else {
160 $lasterr = $line;
161 }
162 print STDERR $lasterr;
163 *STDERR->flush();
164 };
165 }
166
167 eval { run_command($cmd, %args); };
168 if (my $err = $@) {
169 die $errmsg . $lasterr if length($lasterr);
170 die $err;
c693f749
SP
171 }
172
c97c5b3b 173 return undef;
c693f749
SP
174}
175
411476cd
DM
176sub rbd_ls {
177 my ($scfg, $storeid) = @_;
d70e7f6c 178
7cb2889a 179 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
1440604a 180 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
d70e7f6c 181
411476cd 182 my $list = {};
0509010d 183
8c3abf12 184 my $parser = sub {
411476cd 185 my $line = shift;
0509010d 186
ca1e168a
AD
187 if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) {
188 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
0509010d 189
1440604a 190 $list->{$pool}->{$image} = {
411476cd 191 name => $image,
249cb647 192 size => $size*rbd_unittobytes()->{$unit},
62b98a65 193 parent => $parent,
411476cd
DM
194 vmid => $owner
195 };
196 }
8c3abf12
DM
197 };
198
199 eval {
c693f749 200 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
8c3abf12
DM
201 };
202 my $err = $@;
203
204 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
411476cd
DM
205
206 return $list;
0509010d
AD
207}
208
62b98a65 209sub rbd_volume_info {
992e6835
AD
210 my ($scfg, $storeid, $volname, $snap) = @_;
211
212 my $cmd = undef;
213
214 if($snap){
215 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
216 }else{
217 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
218 }
e110213e 219
e110213e 220 my $size = undef;
62b98a65 221 my $parent = undef;
992e6835
AD
222 my $format = undef;
223 my $protected = undef;
62b98a65 224
e110213e
AD
225 my $parser = sub {
226 my $line = shift;
227
249cb647
SP
228 if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) {
229 $size = $1 * rbd_unittobytes()->{$2} if ($1);
62b98a65
AD
230 } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
231 $parent = $2;
992e6835
AD
232 } elsif ($line =~ m/format:\s(\d+)/) {
233 $format = $1;
234 } elsif ($line =~ m/protected:\s(\S+)/) {
235 $protected = 1 if $1 eq "True";
e110213e 236 }
992e6835 237
e110213e
AD
238 };
239
c693f749 240 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
e110213e 241
992e6835 242 return ($size, $parent, $format, $protected);
e110213e
AD
243}
244
e5427b00 245# Configuration
0509010d 246
0509010d
AD
247sub type {
248 return 'rbd';
249}
250
251sub plugindata {
252 return {
1f79bb07 253 content => [ {images => 1, rootdir => 1}, { images => 1 }],
0509010d
AD
254 };
255}
256
257sub properties {
258 return {
e5427b00 259 monhost => {
0509010d 260 description => "Monitors daemon ips.",
e858048f 261 type => 'string', format => 'pve-storage-portal-dns-list',
0509010d 262 },
e5427b00
AD
263 pool => {
264 description => "Pool.",
0509010d
AD
265 type => 'string',
266 },
e5427b00
AD
267 username => {
268 description => "RBD Id.",
0509010d
AD
269 type => 'string',
270 },
e5427b00 271 authsupported => {
0509010d
AD
272 description => "Authsupported.",
273 type => 'string',
274 },
9f20a8a6
AD
275 krbd => {
276 description => "Access rbd through krbd kernel module.",
277 type => 'boolean',
278 },
0509010d
AD
279 };
280}
281
282sub options {
283 return {
35d6dfaf
AD
284 nodes => { optional => 1 },
285 disable => { optional => 1 },
e5427b00 286 monhost => { fixed => 1 },
1440604a
AD
287 pool => { optional => 1 },
288 username => { optional => 1 },
0509010d 289 content => { optional => 1 },
9f20a8a6 290 krbd => { optional => 1 },
0509010d
AD
291 };
292}
293
294# Storage implementation
295
296sub parse_volname {
297 my ($class, $volname) = @_;
298
d04c7e55 299 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
7800e84d 300 return ('images', $4, $7, $2, $3, $5, 'raw');
0509010d
AD
301 }
302
303 die "unable to parse rbd volume name '$volname'\n";
304}
305
306sub path {
38e6ec3f 307 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
0509010d
AD
308
309 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
38e6ec3f 310 $name .= '@'.$snapname if $snapname;
0509010d 311
33cef4c8
WB
312 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
313 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd};
314
e858048f 315 my $monhost = &$hostlist($scfg->{monhost}, ';');
a8176f54
AD
316 $monhost =~ s/:/\\:/g;
317
1440604a
AD
318 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
319
320 my $path = "rbd:$pool/$name:mon_host=$monhost";
321 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
322
323 if(-e $keyring ){
324 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
325 }else{
326 $path .= ":auth_supported=none";
327 }
0509010d 328
13417225
AD
329 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
330
331 if(-e $cephconfig){
332 $path .= ":conf=$cephconfig";
333 }
334
0509010d
AD
335 return ($path, $vmid, $vtype);
336}
337
5b9b9b14
AD
338my $find_free_diskname = sub {
339 my ($storeid, $scfg, $vmid) = @_;
340
341 my $rbd = rbd_ls($scfg, $storeid);
1440604a 342 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
5b9b9b14 343 my $disk_ids = {};
1440604a 344 my $dat = $rbd->{$pool};
5b9b9b14
AD
345
346 foreach my $image (keys %$dat) {
347 my $volname = $dat->{$image}->{name};
348 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
349 $disk_ids->{$2} = 1;
350 }
351 }
352 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
353 for (my $i = 1; $i < 100; $i++) {
354 if (!$disk_ids->{$i}) {
355 return "vm-$vmid-disk-$i";
356 }
357 }
358
359 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
360};
361
5eab0272
DM
362sub create_base {
363 my ($class, $storeid, $scfg, $volname) = @_;
364
992e6835
AD
365 my $snap = '__base__';
366
367 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
368 $class->parse_volname($volname);
369
370 die "create_base not possible with base image\n" if $isBase;
371
372 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
373 die "rbd volume info on '$name' failed\n" if !($size);
374
375 die "rbd image must be at format V2" if $format ne "2";
376
377 die "volname '$volname' contains wrong information about parent $parent $basename\n"
378 if $basename && (!$parent || $parent ne $basename."@".$snap);
379
380 my $newname = $name;
381 $newname =~ s/^vm-/base-/;
382
383 my $newvolname = $basename ? "$basename/$newname" : "$newname";
384
8897f5dc 385 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
c693f749 386 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
992e6835
AD
387
388 my $running = undef; #fixme : is create_base always offline ?
389
390 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
391
392 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
393
394 if (!$protected){
395 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
2362bc87 396 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
992e6835
AD
397 }
398
399 return $newvolname;
400
5eab0272
DM
401}
402
403sub clone_image {
f236eaf8 404 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
5eab0272 405
f2708285 406 my $snap = '__base__';
f236eaf8 407 $snap = $snapname if length $snapname;
f2708285
AD
408
409 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
410 $class->parse_volname($volname);
411
63da6d79
DM
412 die "$volname is not a base image and snapname is not provided\n"
413 if !$isBase && !length($snapname);
f2708285
AD
414
415 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
416
f236eaf8
SP
417 warn "clone $volname: $basename snapname $snap to $name\n";
418
63da6d79 419 if (length($snapname)) {
f236eaf8
SP
420 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
421
63da6d79 422 if (!$protected) {
f236eaf8
SP
423 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
424 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
425 }
426 }
f2708285
AD
427
428 my $newvol = "$basename/$name";
63da6d79
DM
429 $newvol = $name if length($snapname);
430
431 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
432 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
f2708285 433
c693f749 434 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
f2708285 435
d86fd0a4
FG
436 &$krdb_feature_disable($scfg, $storeid, $name);
437
f2708285 438 return $newvol;
5eab0272
DM
439}
440
0509010d
AD
441sub alloc_image {
442 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
443
444
e5427b00 445 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
0509010d 446 if $name && $name !~ m/^vm-$vmid-/;
0509010d 447
3fad2603 448 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
0509010d 449
a8c3f8f6 450 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
c693f749 451 run_rbd_command($cmd, errmsg => "rbd create $name' error");
0509010d 452
d86fd0a4
FG
453 &$krdb_feature_disable($scfg, $storeid, $name);
454
0509010d
AD
455 return $name;
456}
457
458sub free_image {
32437ed2 459 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
0509010d 460
42d07b9a
AD
461 my ($vtype, $name, $vmid, undef, undef, undef) =
462 $class->parse_volname($volname);
463
464 if ($isBase) {
465 my $snap = '__base__';
466 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
467 if ($protected){
468 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
2362bc87 469 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
42d07b9a
AD
470 }
471 }
472
515ef80b
WL
473 $class->deactivate_volume($storeid, $scfg, $volname);
474
42d07b9a 475 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
c693f749 476 run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
c30470a3 477
42d07b9a 478 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
c693f749 479 run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
0509010d
AD
480
481 return undef;
482}
483
484sub list_images {
485 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
486
e5427b00 487 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
1440604a 488 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
411476cd 489
0509010d
AD
490 my $res = [];
491
1440604a 492 if (my $dat = $cache->{rbd}->{$pool}) {
0509010d
AD
493 foreach my $image (keys %$dat) {
494
495 my $volname = $dat->{$image}->{name};
496
497 my $volid = "$storeid:$volname";
498
0509010d
AD
499 my $owner = $dat->{$volname}->{vmid};
500 if ($vollist) {
501 my $found = grep { $_ eq $volid } @$vollist;
502 next if !$found;
503 } else {
504 next if defined ($vmid) && ($owner ne $vmid);
505 }
506
507 my $info = $dat->{$volname};
508 $info->{volid} = $volid;
411476cd 509 $info->{format} = 'raw';
0509010d
AD
510
511 push @$res, $info;
512 }
513 }
514
411476cd 515 return $res;
0509010d
AD
516}
517
0509010d
AD
518sub status {
519 my ($class, $storeid, $scfg, $cache) = @_;
520
69589444
AD
521 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
522
523 my $stats = {};
524
525 my $parser = sub {
526 my $line = shift;
527 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
528 $stats->{$1} = $2;
529 }
530 };
531
532 eval {
c693f749 533 run_rbd_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser);
69589444
AD
534 };
535
536 my $total = $stats->{space} ? $stats->{space}*1024 : 0;
537 my $free = $stats->{avail} ? $stats->{avail}*1024 : 0;
538 my $used = $stats->{used} ? $stats->{used}*1024: 0;
0509010d 539 my $active = 1;
0509010d 540
411476cd 541 return ($total, $free, $used, $active);
0509010d
AD
542}
543
544sub activate_storage {
545 my ($class, $storeid, $scfg, $cache) = @_;
546 return 1;
547}
548
549sub deactivate_storage {
550 my ($class, $storeid, $scfg, $cache) = @_;
551 return 1;
552}
553
554sub activate_volume {
02e797b8 555 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6
AD
556
557 return 1 if !$scfg->{krbd};
558
559 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
4f6a99d8
DM
560 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
561
562 my $path = "/dev/rbd/$pool/$name";
02e797b8 563 $path .= '@'.$snapname if $snapname;
4f6a99d8 564 return if -b $path;
9f20a8a6 565
02e797b8 566 $name .= '@'.$snapname if $snapname;
9f20a8a6
AD
567 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
568 run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
569
0509010d
AD
570 return 1;
571}
572
573sub deactivate_volume {
02e797b8 574 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
9f20a8a6
AD
575
576 return 1 if !$scfg->{krbd};
577
578 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
579 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
580
581 my $path = "/dev/rbd/$pool/$name";
02e797b8 582 $path .= '@'.$snapname if $snapname;
b50812f9
WL
583 return if ! -b $path;
584
9f20a8a6 585 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
72e743bd 586 run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name");
9f20a8a6 587
0509010d
AD
588 return 1;
589}
590
0002d9cc
AD
591sub volume_size_info {
592 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
593
81d1d017
AD
594 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
595 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
62b98a65 596 return $size;
0002d9cc
AD
597}
598
e7a42a76
AD
599sub volume_resize {
600 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
601
602 return 1 if $running;
603
478fc06c
AD
604 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
605
606 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
c693f749 607 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
e7a42a76
AD
608 return undef;
609}
610
788dd8e1 611sub volume_snapshot {
f5640e7d 612 my ($class, $scfg, $storeid, $volname, $snap) = @_;
788dd8e1 613
9af33ed0
AD
614 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
615
616 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
c693f749 617 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
788dd8e1
AD
618 return undef;
619}
620
5a2b2e2f
AD
621sub volume_snapshot_rollback {
622 my ($class, $scfg, $storeid, $volname, $snap) = @_;
623
c6ce2cc8
AD
624 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
625
626 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
2362bc87 627 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
5a2b2e2f
AD
628}
629
cce29bcd
AD
630sub volume_snapshot_delete {
631 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
632
633 return 1 if $running;
634
399581a2
WB
635 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
636
c78cb110
AD
637 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
638
f90a0a20
SP
639 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
640 if ($protected){
641 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
642 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
643 }
644
c78cb110 645 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
c693f749
SP
646
647 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
648
cce29bcd
AD
649 return undef;
650}
651
774f21b9
AD
652sub volume_has_feature {
653 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
654
655 my $features = {
5649ccfe 656 snapshot => { current => 1, snap => 1},
44c3689a 657 clone => { base => 1, snap => 1},
5649ccfe
AD
658 template => { current => 1},
659 copy => { base => 1, current => 1, snap => 1},
baafddbd 660 sparseinit => { base => 1, current => 1},
774f21b9
AD
661 };
662
1e7ae581
AD
663 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
664 $class->parse_volname($volname);
665
666 my $key = undef;
667 if($snapname){
2c5a7097 668 $key = 'snap';
1e7ae581
AD
669 }else{
670 $key = $isBase ? 'base' : 'current';
671 }
672 return 1 if $features->{$feature}->{$key};
774f21b9
AD
673
674 return undef;
675}
676
0509010d 6771;