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