]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
rbd: sub path: add snapname argument
[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, $snapname) = @_;
228
229 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
230 $name .= '@'.$snapname if $snapname;
231
232 my $monhost = addslashes($scfg->{monhost});
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 }
244
245 return ($path, $vmid, $vtype);
246 }
247
248 my $find_free_diskname = sub {
249 my ($storeid, $scfg, $vmid) = @_;
250
251 my $rbd = rbd_ls($scfg, $storeid);
252 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
253 my $disk_ids = {};
254 my $dat = $rbd->{$pool};
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
272 sub create_base {
273 my ($class, $storeid, $scfg, $volname) = @_;
274
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
295 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
296 run_command($cmd, errmsg => "rbd rename $name' error", errfunc => sub {});
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);
306 run_command($cmd, errmsg => "rbd protect $newname snap $snap' error", errfunc => sub {});
307 }
308
309 return $newvolname;
310
311 }
312
313 sub clone_image {
314 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
315
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
329 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', $basename, '--snap', $snap, $name);
330 run_command($cmd, errmsg => "rbd clone $basename' error", errfunc => sub {});
331
332 return $newvol;
333 }
334
335 sub alloc_image {
336 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
337
338
339 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
340 if $name && $name !~ m/^vm-$vmid-/;
341
342 $name = &$find_free_diskname($storeid, $scfg, $vmid);
343
344 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--format' , 2, '--size', int(($size+1023)/1024), $name);
345 run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
346
347 return $name;
348 }
349
350 sub free_image {
351 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
352
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);
361 run_command($cmd, errmsg => "rbd unprotect $name snap $snap' error", errfunc => sub {});
362 }
363 }
364
365 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
366 run_command($cmd, errmsg => "rbd snap purge $volname' error", outfunc => sub {}, errfunc => sub {});
367
368 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
369 run_command($cmd, errmsg => "rbd rm $volname' error", outfunc => sub {}, errfunc => sub {});
370
371 return undef;
372 }
373
374 sub list_images {
375 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
376
377 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
378 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
379
380 my $res = [];
381
382 if (my $dat = $cache->{rbd}->{$pool}) {
383 foreach my $image (keys %$dat) {
384
385 my $volname = $dat->{$image}->{name};
386
387 my $volid = "$storeid:$volname";
388
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;
399 $info->{format} = 'raw';
400
401 push @$res, $info;
402 }
403 }
404
405 return $res;
406 }
407
408 sub status {
409 my ($class, $storeid, $scfg, $cache) = @_;
410
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;
429 my $active = 1;
430
431 return ($total, $free, $used, $active);
432 }
433
434 sub activate_storage {
435 my ($class, $storeid, $scfg, $cache) = @_;
436 return 1;
437 }
438
439 sub deactivate_storage {
440 my ($class, $storeid, $scfg, $cache) = @_;
441 return 1;
442 }
443
444 sub activate_volume {
445 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
446 return 1;
447 }
448
449 sub deactivate_volume {
450 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
451 return 1;
452 }
453
454 sub volume_size_info {
455 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
456
457 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
458 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
459 return $size;
460 }
461
462 sub volume_resize {
463 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
464
465 return 1 if $running;
466
467 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
468
469 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
470 run_command($cmd, errmsg => "rbd resize $volname' error", errfunc => sub {});
471 return undef;
472 }
473
474 sub volume_snapshot {
475 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
476
477 return 1 if $running;
478
479 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
480
481 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
482 run_command($cmd, errmsg => "rbd snapshot $volname' error", errfunc => sub {});
483 return undef;
484 }
485
486 sub volume_snapshot_rollback {
487 my ($class, $scfg, $storeid, $volname, $snap) = @_;
488
489 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
490
491 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
492 run_command($cmd, errmsg => "rbd snapshot $volname to $snap' error", errfunc => sub {});
493 }
494
495 sub volume_snapshot_delete {
496 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
497
498 return 1 if $running;
499
500 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
501
502 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
503 run_command($cmd, errmsg => "rbd snapshot $volname' error", errfunc => sub {});
504 return undef;
505 }
506
507 sub volume_has_feature {
508 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
509
510 my $features = {
511 snapshot => { current => 1, snap => 1},
512 clone => { base => 1},
513 template => { current => 1},
514 copy => { base => 1, current => 1, snap => 1},
515 };
516
517 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
518 $class->parse_volname($volname);
519
520 my $key = undef;
521 if($snapname){
522 $key = 'snap';
523 }else{
524 $key = $isBase ? 'base' : 'current';
525 }
526 return 1 if $features->{$feature}->{$key};
527
528 return undef;
529 }
530
531 1;