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