]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
parse_volname: always return image format
[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 krbd => {
223 description => "Access rbd through krbd kernel module.",
224 type => 'boolean',
225 },
226 };
227 }
228
229 sub options {
230 return {
231 nodes => { optional => 1 },
232 disable => { optional => 1 },
233 monhost => { fixed => 1 },
234 pool => { optional => 1 },
235 username => { optional => 1 },
236 content => { optional => 1 },
237 krbd => { optional => 1 },
238 };
239 }
240
241 # Storage implementation
242
243 sub parse_volname {
244 my ($class, $volname) = @_;
245
246 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
247 return ('images', $4, $7, $2, $3, $5, 'raw');
248 }
249
250 die "unable to parse rbd volume name '$volname'\n";
251 }
252
253 sub path {
254 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
255
256 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
257 $name .= '@'.$snapname if $snapname;
258
259 my $monhost = $scfg->{monhost};
260 $monhost =~ s/:/\\:/g;
261
262 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
263 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
264
265 my $path = "rbd:$pool/$name:mon_host=$monhost";
266 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
267
268 if(-e $keyring ){
269 $path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
270 }else{
271 $path .= ":auth_supported=none";
272 }
273
274 $path = "/dev/rbd/$pool/$name" if $scfg->{krbd};
275
276 return ($path, $vmid, $vtype);
277 }
278
279 my $find_free_diskname = sub {
280 my ($storeid, $scfg, $vmid) = @_;
281
282 my $rbd = rbd_ls($scfg, $storeid);
283 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
284 my $disk_ids = {};
285 my $dat = $rbd->{$pool};
286
287 foreach my $image (keys %$dat) {
288 my $volname = $dat->{$image}->{name};
289 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
290 $disk_ids->{$2} = 1;
291 }
292 }
293 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
294 for (my $i = 1; $i < 100; $i++) {
295 if (!$disk_ids->{$i}) {
296 return "vm-$vmid-disk-$i";
297 }
298 }
299
300 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
301 };
302
303 sub create_base {
304 my ($class, $storeid, $scfg, $volname) = @_;
305
306 my $snap = '__base__';
307
308 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
309 $class->parse_volname($volname);
310
311 die "create_base not possible with base image\n" if $isBase;
312
313 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
314 die "rbd volume info on '$name' failed\n" if !($size);
315
316 die "rbd image must be at format V2" if $format ne "2";
317
318 die "volname '$volname' contains wrong information about parent $parent $basename\n"
319 if $basename && (!$parent || $parent ne $basename."@".$snap);
320
321 my $newname = $name;
322 $newname =~ s/^vm-/base-/;
323
324 my $newvolname = $basename ? "$basename/$newname" : "$newname";
325
326 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
327 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
328
329 my $running = undef; #fixme : is create_base always offline ?
330
331 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
332
333 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
334
335 if (!$protected){
336 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
337 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
338 }
339
340 return $newvolname;
341
342 }
343
344 sub clone_image {
345 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
346
347 my $snap = '__base__';
348 $snap = $snapname if length $snapname;
349
350 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
351 $class->parse_volname($volname);
352
353 die "$volname is not a base image and snapname is not provided\n"
354 if !$isBase && !length($snapname);
355
356 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
357
358 warn "clone $volname: $basename snapname $snap to $name\n";
359
360 if (length($snapname)) {
361 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
362
363 if (!$protected) {
364 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
365 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
366 }
367 }
368
369 my $newvol = "$basename/$name";
370 $newvol = $name if length($snapname);
371
372 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
373 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
374
375 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
376
377 return $newvol;
378 }
379
380 sub alloc_image {
381 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
382
383
384 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
385 if $name && $name !~ m/^vm-$vmid-/;
386
387 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
388
389 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
390 run_rbd_command($cmd, errmsg => "rbd create $name' error");
391
392 return $name;
393 }
394
395 sub free_image {
396 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
397
398 my ($vtype, $name, $vmid, undef, undef, undef) =
399 $class->parse_volname($volname);
400
401 if ($isBase) {
402 my $snap = '__base__';
403 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
404 if ($protected){
405 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
406 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
407 }
408 }
409
410 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
411 run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
412
413 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
414 run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
415
416 return undef;
417 }
418
419 sub list_images {
420 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
421
422 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
423 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
424
425 my $res = [];
426
427 if (my $dat = $cache->{rbd}->{$pool}) {
428 foreach my $image (keys %$dat) {
429
430 my $volname = $dat->{$image}->{name};
431
432 my $volid = "$storeid:$volname";
433
434 my $owner = $dat->{$volname}->{vmid};
435 if ($vollist) {
436 my $found = grep { $_ eq $volid } @$vollist;
437 next if !$found;
438 } else {
439 next if defined ($vmid) && ($owner ne $vmid);
440 }
441
442 my $info = $dat->{$volname};
443 $info->{volid} = $volid;
444 $info->{format} = 'raw';
445
446 push @$res, $info;
447 }
448 }
449
450 return $res;
451 }
452
453 sub status {
454 my ($class, $storeid, $scfg, $cache) = @_;
455
456 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
457
458 my $stats = {};
459
460 my $parser = sub {
461 my $line = shift;
462 if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
463 $stats->{$1} = $2;
464 }
465 };
466
467 eval {
468 run_rbd_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser);
469 };
470
471 my $total = $stats->{space} ? $stats->{space}*1024 : 0;
472 my $free = $stats->{avail} ? $stats->{avail}*1024 : 0;
473 my $used = $stats->{used} ? $stats->{used}*1024: 0;
474 my $active = 1;
475
476 return ($total, $free, $used, $active);
477 }
478
479 sub activate_storage {
480 my ($class, $storeid, $scfg, $cache) = @_;
481 return 1;
482 }
483
484 sub deactivate_storage {
485 my ($class, $storeid, $scfg, $cache) = @_;
486 return 1;
487 }
488
489 sub activate_volume {
490 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
491
492 return 1 if !$scfg->{krbd};
493
494 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
495
496 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
497 run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
498
499 return 1;
500 }
501
502 sub deactivate_volume {
503 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
504
505 return 1 if !$scfg->{krbd};
506
507 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
508 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
509
510 my $path = "/dev/rbd/$pool/$name";
511 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
512 run_rbd_command($cmd, errmsg => "can't unmount rbd volume $name");
513
514 return 1;
515 }
516
517 sub volume_size_info {
518 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
519
520 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
521 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
522 return $size;
523 }
524
525 sub volume_resize {
526 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
527
528 return 1 if $running;
529
530 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
531
532 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
533 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
534 return undef;
535 }
536
537 sub volume_snapshot {
538 my ($class, $scfg, $storeid, $volname, $snap) = @_;
539
540 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
541
542 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
543 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
544 return undef;
545 }
546
547 sub volume_snapshot_rollback {
548 my ($class, $scfg, $storeid, $volname, $snap) = @_;
549
550 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
551
552 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
553 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
554 }
555
556 sub volume_snapshot_delete {
557 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
558
559 return 1 if $running;
560
561 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
562
563 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
564 if ($protected){
565 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
566 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
567 }
568
569 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
570
571 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
572
573 return undef;
574 }
575
576 sub volume_has_feature {
577 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
578
579 my $features = {
580 snapshot => { current => 1, snap => 1},
581 clone => { base => 1, snap => 1},
582 template => { current => 1},
583 copy => { base => 1, current => 1, snap => 1},
584 };
585
586 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
587 $class->parse_volname($volname);
588
589 my $key = undef;
590 if($snapname){
591 $key = 'snap';
592 }else{
593 $key = $isBase ? 'base' : 'current';
594 }
595 return 1 if $features->{$feature}->{$key};
596
597 return undef;
598 }
599
600 1;