]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
add Storage::get_bandwidth_limit helper
[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 Net::IP;
7 use PVE::Tools qw(run_command trim);
8 use PVE::Storage::Plugin;
9 use PVE::JSONSchema qw(get_standard_option);
10
11 use base qw(PVE::Storage::Plugin);
12
13 my $pveceph_config = '/etc/pve/ceph.conf';
14
15 my $rbd_unittobytes = {
16 "k" => 1024,
17 "M" => 1024*1024,
18 "G" => 1024*1024*1024,
19 "T" => 1024*1024*1024*1024,
20 };
21
22 my $add_pool_to_disk = sub {
23 my ($scfg, $disk) = @_;
24
25 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
26
27 return "$pool/$disk";
28 };
29
30 my $hostlist = sub {
31 my ($list_text, $separator) = @_;
32
33 my @monhostlist = PVE::Tools::split_list($list_text);
34 return join($separator, map {
35 my ($host, $port) = PVE::Tools::parse_host_and_port($_);
36 $port = defined($port) ? ":$port" : '';
37 $host = "[$host]" if Net::IP::ip_is_ipv6($host);
38 "${host}${port}"
39 } @monhostlist);
40 };
41
42 my $build_cmd = sub {
43 my ($binary, $scfg, $storeid, $op, @options) = @_;
44
45 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
46 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
47 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
48
49 my $cmd = [$binary, '-p', $pool];
50 my $pveceph_managed = !defined($scfg->{monhost});
51
52 if ($pveceph_managed) {
53 push @$cmd, '-c', $pveceph_config;
54 } else {
55 push @$cmd, '-m', $hostlist->($scfg->{monhost}, ',');
56 push @$cmd, '--auth_supported', -e $keyring ? 'cephx' : 'none';
57 }
58
59 if (-e $keyring) {
60 push @$cmd, '-n', "client.$username";
61 push @$cmd, '--keyring', $keyring;
62 }
63
64 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
65
66 if (-e $cephconfig) {
67 if ($pveceph_managed) {
68 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
69 } else {
70 push @$cmd, '-c', $cephconfig;
71 }
72 }
73
74 push @$cmd, $op;
75
76 push @$cmd, @options if scalar(@options);
77
78 return $cmd;
79 };
80
81 my $rbd_cmd = sub {
82 my ($scfg, $storeid, $op, @options) = @_;
83
84 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
85 };
86
87 my $rados_cmd = sub {
88 my ($scfg, $storeid, $op, @options) = @_;
89
90 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
91 };
92
93 # needed for volumes created using ceph jewel (or higher)
94 my $krdb_feature_disable = sub {
95 my ($scfg, $storeid, $name) = @_;
96
97 return 1 if !$scfg->{krbd};
98
99 my ($major, undef, undef, undef) = ceph_version();
100 return 1 if $major < 10;
101
102 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock');
103 run_rbd_command($feature_cmd, errmsg => "could not disable krbd-incompatible image features of rbd volume $name");
104 };
105
106 my $ceph_version_parser = sub {
107 my $line = shift;
108 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
109 return ($2, $3, $4, $1);
110 } else {
111 warn "Could not parse Ceph version: '$line'\n";
112 }
113 };
114
115 sub ceph_version {
116 my ($cache) = @_;
117
118 my $version_string = $cache;
119
120 my $major;
121 my $minor;
122 my $bugfix;
123
124 if (defined($version_string)) {
125 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
126 } else {
127 run_command('ceph --version', outfunc => sub {
128 my $line = shift;
129 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
130 });
131 }
132 return undef if !defined($version_string);
133 return wantarray ? ($major, $minor, $bugfix, $version_string) : $version_string;
134 }
135
136 sub run_rbd_command {
137 my ($cmd, %args) = @_;
138
139 my $lasterr;
140 my $errmsg = $args{errmsg} . ": " || "";
141 if (!exists($args{errfunc})) {
142 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
143 # at least 1 child(ren) in pool cephstor1
144 $args{errfunc} = sub {
145 my $line = shift;
146 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
147 $lasterr = "$1\n";
148 } else {
149 $lasterr = $line;
150 }
151 print STDERR $lasterr;
152 *STDERR->flush();
153 };
154 }
155
156 eval { run_command($cmd, %args); };
157 if (my $err = $@) {
158 die $errmsg . $lasterr if length($lasterr);
159 die $err;
160 }
161
162 return undef;
163 }
164
165 sub rbd_ls {
166 my ($scfg, $storeid) = @_;
167
168 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
169 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
170
171 my $list = {};
172
173 my $parser = sub {
174 my $line = shift;
175
176 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) {
177 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
178 return if $image =~ /@/; #skip snapshots
179
180 $list->{$pool}->{$image} = {
181 name => $image,
182 size => $size*$rbd_unittobytes->{$unit},
183 parent => $parent,
184 vmid => $owner
185 };
186 }
187 };
188
189 eval {
190 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
191 };
192 my $err = $@;
193
194 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
195
196 return $list;
197 }
198
199 sub rbd_volume_info {
200 my ($scfg, $storeid, $volname, $snap) = @_;
201
202 my $cmd = undef;
203
204 if($snap){
205 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
206 }else{
207 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
208 }
209
210 my $size = undef;
211 my $parent = undef;
212 my $format = undef;
213 my $protected = undef;
214
215 my $parser = sub {
216 my $line = shift;
217
218 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
219 $size = $1 * $rbd_unittobytes->{$2} if ($1);
220 } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
221 $parent = $2;
222 } elsif ($line =~ m/format:\s(\d+)/) {
223 $format = $1;
224 } elsif ($line =~ m/protected:\s(\S+)/) {
225 $protected = 1 if $1 eq "True";
226 }
227
228 };
229
230 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
231
232 return ($size, $parent, $format, $protected);
233 }
234
235 # Configuration
236
237 sub type {
238 return 'rbd';
239 }
240
241 sub plugindata {
242 return {
243 content => [ {images => 1, rootdir => 1}, { images => 1 }],
244 };
245 }
246
247 sub properties {
248 return {
249 monhost => {
250 description => "IP addresses of monitors (for external clusters).",
251 type => 'string', format => 'pve-storage-portal-dns-list',
252 },
253 pool => {
254 description => "Pool.",
255 type => 'string',
256 },
257 username => {
258 description => "RBD Id.",
259 type => 'string',
260 },
261 authsupported => {
262 description => "Authsupported.",
263 type => 'string',
264 },
265 krbd => {
266 description => "Access rbd through krbd kernel module.",
267 type => 'boolean',
268 },
269 };
270 }
271
272 sub options {
273 return {
274 nodes => { optional => 1 },
275 disable => { optional => 1 },
276 monhost => { optional => 1},
277 pool => { optional => 1 },
278 username => { optional => 1 },
279 content => { optional => 1 },
280 krbd => { optional => 1 },
281 bwlimit => { optional => 1 },
282 };
283 }
284
285 # Storage implementation
286
287 sub parse_volname {
288 my ($class, $volname) = @_;
289
290 if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
291 return ('images', $4, $7, $2, $3, $5, 'raw');
292 }
293
294 die "unable to parse rbd volume name '$volname'\n";
295 }
296
297 sub path {
298 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
299
300 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
301 $name .= '@'.$snapname if $snapname;
302
303 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
304 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd};
305
306 my $username = $scfg->{username} ? $scfg->{username} : 'admin';
307
308 my $path = "rbd:$pool/$name";
309 my $pveceph_managed = !defined($scfg->{monhost});
310 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
311
312 if ($pveceph_managed) {
313 $path .= ":conf=$pveceph_config";
314 } else {
315 my $monhost = $hostlist->($scfg->{monhost}, ';');
316 $monhost =~ s/:/\\:/g;
317 $path .= ":mon_host=$monhost";
318 $path .= -e $keyring ? ":auth_supported=cephx" : ":auth_supported=none";
319 }
320
321 $path .= ":id=$username:keyring=$keyring" if -e $keyring;
322
323 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
324
325 if (-e $cephconfig) {
326 if ($pveceph_managed) {
327 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
328 } else {
329 $path .= ":conf=$cephconfig";
330 }
331 }
332
333 return ($path, $vmid, $vtype);
334 }
335
336 my $find_free_diskname = sub {
337 my ($storeid, $scfg, $vmid) = @_;
338
339 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
340 my $disk_ids = {};
341
342 my $parser = sub {
343 my $line = shift;
344
345 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
346 $disk_ids->{$2} = 1;
347 }
348 };
349
350 eval {
351 run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
352 };
353 my $err = $@;
354
355 die $err if $err && $err !~ m/doesn't contain rbd images/;
356
357 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
358 for (my $i = 1; $i < 100; $i++) {
359 if (!$disk_ids->{$i}) {
360 return "vm-$vmid-disk-$i";
361 }
362 }
363
364 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
365 };
366
367 sub create_base {
368 my ($class, $storeid, $scfg, $volname) = @_;
369
370 my $snap = '__base__';
371
372 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
373 $class->parse_volname($volname);
374
375 die "create_base not possible with base image\n" if $isBase;
376
377 my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
378 die "rbd volume info on '$name' failed\n" if !($size);
379
380 die "rbd image must be at format V2" if $format ne "2";
381
382 die "volname '$volname' contains wrong information about parent $parent $basename\n"
383 if $basename && (!$parent || $parent ne $basename."@".$snap);
384
385 my $newname = $name;
386 $newname =~ s/^vm-/base-/;
387
388 my $newvolname = $basename ? "$basename/$newname" : "$newname";
389
390 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
391 run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
392
393 my $running = undef; #fixme : is create_base always offline ?
394
395 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
396
397 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
398
399 if (!$protected){
400 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
401 run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
402 }
403
404 return $newvolname;
405
406 }
407
408 sub clone_image {
409 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
410
411 my $snap = '__base__';
412 $snap = $snapname if length $snapname;
413
414 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
415 $class->parse_volname($volname);
416
417 die "$volname is not a base image and snapname is not provided\n"
418 if !$isBase && !length($snapname);
419
420 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
421
422 warn "clone $volname: $basename snapname $snap to $name\n";
423
424 if (length($snapname)) {
425 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
426
427 if (!$protected) {
428 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
429 run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
430 }
431 }
432
433 my $newvol = "$basename/$name";
434 $newvol = $name if length($snapname);
435
436 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
437 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
438
439 run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
440
441 &$krdb_feature_disable($scfg, $storeid, $name);
442
443 return $newvol;
444 }
445
446 sub alloc_image {
447 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
448
449
450 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
451 if $name && $name !~ m/^vm-$vmid-/;
452
453 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
454
455 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
456 run_rbd_command($cmd, errmsg => "rbd create $name' error");
457
458 &$krdb_feature_disable($scfg, $storeid, $name);
459
460 return $name;
461 }
462
463 sub free_image {
464 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
465
466 my ($vtype, $name, $vmid, undef, undef, undef) =
467 $class->parse_volname($volname);
468
469 if ($isBase) {
470 my $snap = '__base__';
471 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
472 if ($protected){
473 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
474 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
475 }
476 }
477
478 $class->deactivate_volume($storeid, $scfg, $volname);
479
480 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
481 run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
482
483 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
484 run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
485
486 return undef;
487 }
488
489 sub list_images {
490 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
491
492 $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
493 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
494
495 my $res = [];
496
497 if (my $dat = $cache->{rbd}->{$pool}) {
498 foreach my $image (keys %$dat) {
499
500 my $info = $dat->{$image};
501
502 my $volname = $info->{name};
503 my $parent = $info->{parent};
504 my $owner = $info->{vmid};
505
506 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
507 $info->{volid} = "$storeid:$1/$volname";
508 } else {
509 $info->{volid} = "$storeid:$volname";
510 }
511
512 if ($vollist) {
513 my $found = grep { $_ eq $info->{volid} } @$vollist;
514 next if !$found;
515 } else {
516 next if defined ($vmid) && ($owner ne $vmid);
517 }
518
519 $info->{format} = 'raw';
520
521 push @$res, $info;
522 }
523 }
524
525 return $res;
526 }
527
528 sub status {
529 my ($class, $storeid, $scfg, $cache) = @_;
530
531 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
532
533 my $stats = {};
534
535 my $parser = sub {
536 my $line = shift;
537 if ($line =~ m/^\s*total(?:\s|_)(\S+)\s+(\d+)(k|M|G|T)?/) {
538 $stats->{$1} = $2;
539 # luminous has units here..
540 if ($3) {
541 $stats->{$1} *= $rbd_unittobytes->{$3}/1024;
542 }
543 }
544 };
545
546 eval {
547 run_rbd_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser);
548 };
549
550 my $total = $stats->{space} ? $stats->{space}*1024 : 0;
551 my $free = $stats->{avail} ? $stats->{avail}*1024 : 0;
552 my $used = $stats->{used} ? $stats->{used}*1024: 0;
553 my $active = 1;
554
555 return ($total, $free, $used, $active);
556 }
557
558 sub activate_storage {
559 my ($class, $storeid, $scfg, $cache) = @_;
560 return 1;
561 }
562
563 sub deactivate_storage {
564 my ($class, $storeid, $scfg, $cache) = @_;
565 return 1;
566 }
567
568 sub activate_volume {
569 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
570
571 return 1 if !$scfg->{krbd};
572
573 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
574 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
575
576 my $path = "/dev/rbd/$pool/$name";
577 $path .= '@'.$snapname if $snapname;
578 return if -b $path;
579
580 $name .= '@'.$snapname if $snapname;
581 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
582 run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
583
584 return 1;
585 }
586
587 sub deactivate_volume {
588 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
589
590 return 1 if !$scfg->{krbd};
591
592 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
593 my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
594
595 my $path = "/dev/rbd/$pool/$name";
596 $path .= '@'.$snapname if $snapname;
597 return if ! -b $path;
598
599 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
600 run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name");
601
602 return 1;
603 }
604
605 sub volume_size_info {
606 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
607
608 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
609 my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
610 return $size;
611 }
612
613 sub volume_resize {
614 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
615
616 return 1 if $running && !$scfg->{krbd};
617
618 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
619
620 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
621 run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
622 return undef;
623 }
624
625 sub volume_snapshot {
626 my ($class, $scfg, $storeid, $volname, $snap) = @_;
627
628 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
629
630 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
631 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
632 return undef;
633 }
634
635 sub volume_snapshot_rollback {
636 my ($class, $scfg, $storeid, $volname, $snap) = @_;
637
638 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
639
640 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
641 run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
642 }
643
644 sub volume_snapshot_delete {
645 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
646
647 return 1 if $running && !$scfg->{krbd};
648
649 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
650
651 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
652
653 my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
654 if ($protected){
655 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
656 run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
657 }
658
659 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
660
661 run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
662
663 return undef;
664 }
665
666 sub volume_has_feature {
667 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
668
669 my $features = {
670 snapshot => { current => 1, snap => 1},
671 clone => { base => 1, snap => 1},
672 template => { current => 1},
673 copy => { base => 1, current => 1, snap => 1},
674 sparseinit => { base => 1, current => 1},
675 };
676
677 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
678 $class->parse_volname($volname);
679
680 my $key = undef;
681 if($snapname){
682 $key = 'snap';
683 }else{
684 $key = $isBase ? 'base' : 'current';
685 }
686 return 1 if $features->{$feature}->{$key};
687
688 return undef;
689 }
690
691 1;