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