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