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