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