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