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