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