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