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