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