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