]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/RBDPlugin.pm
1 package PVE
::Storage
::RBDPlugin
;
7 use PVE
::Tools
qw(run_command trim);
8 use PVE
::Storage
::Plugin
;
9 use PVE
::JSONSchema
qw(get_standard_option);
11 use base
qw(PVE::Storage::Plugin);
13 my $pveceph_config = '/etc/pve/ceph.conf';
15 my $rbd_unittobytes = {
18 "G" => 1024*1024*1024,
19 "T" => 1024*1024*1024*1024,
22 my $add_pool_to_disk = sub {
23 my ($scfg, $disk) = @_;
25 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
31 my ($list_text, $separator) = @_;
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);
43 my ($binary, $scfg, $storeid, $op, @options) = @_;
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';
49 my $cmd = [$binary, '-p', $pool];
50 my $pveceph_managed = !defined($scfg->{monhost
});
52 if ($pveceph_managed) {
53 push @$cmd, '-c', $pveceph_config;
55 push @$cmd, '-m', $hostlist->($scfg->{monhost
}, ',');
56 push @$cmd, '--auth_supported', -e
$keyring ?
'cephx' : 'none';
60 push @$cmd, '-n', "client.$username";
61 push @$cmd, '--keyring', $keyring;
64 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
67 if ($pveceph_managed) {
68 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
70 push @$cmd, '-c', $cephconfig;
76 push @$cmd, @options if scalar(@options);
82 my ($scfg, $storeid, $op, @options) = @_;
84 return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
88 my ($scfg, $storeid, $op, @options) = @_;
90 return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
93 # needed for volumes created using ceph jewel (or higher)
94 my $krbd_feature_disable = sub {
95 my ($scfg, $storeid, $name) = @_;
97 return 1 if !$scfg->{krbd
};
99 my ($major, undef, undef, undef) = ceph_version
();
100 return 1 if $major < 10;
102 my $krbd_feature_blacklist = ['deep-flatten', 'fast-diff', 'object-map', 'exclusive-lock'];
103 my (undef, undef, undef, undef, $features) = rbd_volume_info
($scfg, $storeid, $name);
105 my $active_features = { map { $_ => 1 } PVE
::Tools
::split_list
($features)};
106 my $incompatible_features = join(',', grep { %$active_features{$_} } @$krbd_feature_blacklist);
108 if ($incompatible_features) {
109 my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, $incompatible_features);
110 run_rbd_command
($feature_cmd, errmsg
=> "could not disable krbd-incompatible image features of rbd volume $name");
114 my $ceph_version_parser = sub {
116 if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) {
117 return ($2, $3, $4, $1);
119 warn "Could not parse Ceph version: '$line'\n";
126 my $version_string = $cache;
132 if (defined($version_string)) {
133 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
135 run_command
('ceph --version', outfunc
=> sub {
137 ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
140 return undef if !defined($version_string);
141 return wantarray ?
($major, $minor, $bugfix, $version_string) : $version_string;
144 sub run_rbd_command
{
145 my ($cmd, %args) = @_;
148 my $errmsg = $args{errmsg
} . ": " || "";
149 if (!exists($args{errfunc
})) {
150 # ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
151 # at least 1 child(ren) in pool cephstor1
152 $args{errfunc
} = sub {
154 if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
159 print STDERR
$lasterr;
164 eval { run_command
($cmd, %args); };
166 die $errmsg . $lasterr if length($lasterr);
174 my ($scfg, $storeid) = @_;
176 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l');
177 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
184 if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm
|base
)-\d
+-\S
+@\S+))?
/) {
185 my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
186 return if $image =~ /@/; #skip snapshots
188 $list->{$pool}->{$image} = {
190 size
=> $size*$rbd_unittobytes->{$unit},
198 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
202 die $err if $err && $err !~ m/doesn't contain rbd images/ ;
207 sub rbd_volume_info
{
208 my ($scfg, $storeid, $volname, $snap) = @_;
213 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
215 $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
221 my $protected = undef;
222 my $features = undef;
227 if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
228 $size = $1 * $rbd_unittobytes->{$2} if ($1);
229 } elsif ($line =~ m/parent:\s(\S+)\/(\S
+)/) {
231 } elsif ($line =~ m/format:\s(\d+)/) {
233 } elsif ($line =~ m/protected:\s(\S+)/) {
234 $protected = 1 if $1 eq "True";
235 } elsif ($line =~ m/features:\s(.+)/) {
241 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
243 return ($size, $parent, $format, $protected, $features);
254 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
261 description
=> "IP addresses of monitors (for external clusters).",
262 type
=> 'string', format
=> 'pve-storage-portal-dns-list',
265 description
=> "Pool.",
269 description
=> "RBD Id.",
273 description
=> "Authsupported.",
277 description
=> "Access rbd through krbd kernel module.",
285 nodes
=> { optional
=> 1 },
286 disable
=> { optional
=> 1 },
287 monhost
=> { optional
=> 1},
288 pool
=> { optional
=> 1 },
289 username
=> { optional
=> 1 },
290 content
=> { optional
=> 1 },
291 krbd
=> { optional
=> 1 },
292 bwlimit
=> { optional
=> 1 },
296 # Storage implementation
299 my ($class, $volname) = @_;
301 if ($volname =~ m/^((base-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
302 return ('images', $4, $7, $2, $3, $5, 'raw');
305 die "unable to parse rbd volume name '$volname'\n";
309 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
311 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
312 $name .= '@'.$snapname if $snapname;
314 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
315 return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd
};
317 my $username = $scfg->{username
} ?
$scfg->{username
} : 'admin';
319 my $path = "rbd:$pool/$name";
320 my $pveceph_managed = !defined($scfg->{monhost
});
321 my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
323 if ($pveceph_managed) {
324 $path .= ":conf=$pveceph_config";
326 my $monhost = $hostlist->($scfg->{monhost
}, ';');
327 $monhost =~ s/:/\\:/g;
328 $path .= ":mon_host=$monhost";
329 $path .= -e
$keyring ?
":auth_supported=cephx" : ":auth_supported=none";
332 $path .= ":id=$username:keyring=$keyring" if -e
$keyring;
334 my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
336 if (-e
$cephconfig) {
337 if ($pveceph_managed) {
338 warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
340 $path .= ":conf=$cephconfig";
344 return ($path, $vmid, $vtype);
347 my $find_free_diskname = sub {
348 my ($storeid, $scfg, $vmid) = @_;
350 my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
356 if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
362 run_rbd_command
($cmd, errmsg
=> "rbd error", errfunc
=> sub {}, outfunc
=> $parser);
366 die $err if $err && $err !~ m/doesn't contain rbd images/;
368 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
369 for (my $i = 1; $i < 100; $i++) {
370 if (!$disk_ids->{$i}) {
371 return "vm-$vmid-disk-$i";
375 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
379 my ($class, $storeid, $scfg, $volname) = @_;
381 my $snap = '__base__';
383 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
384 $class->parse_volname($volname);
386 die "create_base not possible with base image\n" if $isBase;
388 my ($size, $parent, $format, undef) = rbd_volume_info
($scfg, $storeid, $name);
389 die "rbd volume info on '$name' failed\n" if !($size);
391 die "rbd image must be at format V2" if $format ne "2";
393 die "volname '$volname' contains wrong information about parent $parent $basename\n"
394 if $basename && (!$parent || $parent ne $basename."@".$snap);
397 $newname =~ s/^vm-/base-/;
399 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
401 my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname));
402 run_rbd_command
($cmd, errmsg
=> "rbd rename '$name' error");
404 my $running = undef; #fixme : is create_base always offline ?
406 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
408 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $newname, $snap);
411 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
412 run_rbd_command
($cmd, errmsg
=> "rbd protect $newname snap '$snap' error");
420 my ($class, $scfg, $storeid, $volname, $vmid, $snapname) = @_;
422 my $snap = '__base__';
423 $snap = $snapname if length $snapname;
425 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
426 $class->parse_volname($volname);
428 die "$volname is not a base image and snapname is not provided\n"
429 if !$isBase && !length($snapname);
431 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
433 warn "clone $volname: $basename snapname $snap to $name\n";
435 if (length($snapname)) {
436 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $volname, $snapname);
439 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
440 run_rbd_command
($cmd, errmsg
=> "rbd protect $volname snap $snapname error");
444 my $newvol = "$basename/$name";
445 $newvol = $name if length($snapname);
447 my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename),
448 '--snap', $snap, &$add_pool_to_disk($scfg, $name));
450 run_rbd_command
($cmd, errmsg
=> "rbd clone '$basename' error");
452 &$krbd_feature_disable($scfg, $storeid, $name);
458 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
461 die "illegal name '$name' - should be 'vm-$vmid-*'\n"
462 if $name && $name !~ m/^vm-$vmid-/;
464 $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
466 my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
467 run_rbd_command
($cmd, errmsg
=> "rbd create $name' error");
469 &$krbd_feature_disable($scfg, $storeid, $name);
475 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
477 my ($vtype, $name, $vmid, undef, undef, undef) =
478 $class->parse_volname($volname);
481 my $snap = '__base__';
482 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
484 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
485 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
489 $class->deactivate_volume($storeid, $scfg, $volname);
491 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
492 run_rbd_command
($cmd, errmsg
=> "rbd snap purge '$volname' error");
494 $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name);
495 run_rbd_command
($cmd, errmsg
=> "rbd rm '$volname' error");
501 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
503 $cache->{rbd
} = rbd_ls
($scfg, $storeid) if !$cache->{rbd
};
504 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
508 if (my $dat = $cache->{rbd
}->{$pool}) {
509 foreach my $image (keys %$dat) {
511 my $info = $dat->{$image};
513 my $volname = $info->{name
};
514 my $parent = $info->{parent
};
515 my $owner = $info->{vmid
};
517 if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
518 $info->{volid
} = "$storeid:$1/$volname";
520 $info->{volid
} = "$storeid:$volname";
524 my $found = grep { $_ eq $info->{volid
} } @$vollist;
527 next if defined ($vmid) && ($owner ne $vmid);
530 $info->{format
} = 'raw';
540 my ($class, $storeid, $scfg, $cache) = @_;
542 my $cmd = &$rados_cmd($scfg, $storeid, 'df');
548 if ($line =~ m/^\s*total(?:\s|_)(\S+)\s+(\d+)(k|M|G|T)?/) {
550 # luminous has units here..
552 $stats->{$1} *= $rbd_unittobytes->{$3}/1024;
558 run_rbd_command
($cmd, errmsg
=> "rados error", errfunc
=> sub {}, outfunc
=> $parser);
561 my $total = $stats->{space
} ?
$stats->{space
}*1024 : 0;
562 my $free = $stats->{avail
} ?
$stats->{avail
}*1024 : 0;
563 my $used = $stats->{used
} ?
$stats->{used
}*1024: 0;
566 return ($total, $free, $used, $active);
569 sub activate_storage
{
570 my ($class, $storeid, $scfg, $cache) = @_;
574 sub deactivate_storage
{
575 my ($class, $storeid, $scfg, $cache) = @_;
579 sub activate_volume
{
580 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
582 return 1 if !$scfg->{krbd
};
584 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
585 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
587 my $path = "/dev/rbd/$pool/$name";
588 $path .= '@'.$snapname if $snapname;
591 $name .= '@'.$snapname if $snapname;
592 my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
593 run_rbd_command
($cmd, errmsg
=> "can't mount rbd volume $name");
598 sub deactivate_volume
{
599 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
601 return 1 if !$scfg->{krbd
};
603 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
604 my $pool = $scfg->{pool
} ?
$scfg->{pool
} : 'rbd';
606 my $path = "/dev/rbd/$pool/$name";
607 $path .= '@'.$snapname if $snapname;
608 return if ! -b
$path;
610 my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
611 run_rbd_command
($cmd, errmsg
=> "can't unmap rbd volume $name");
616 sub volume_size_info
{
617 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
619 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
620 my ($size, undef) = rbd_volume_info
($scfg, $storeid, $name);
625 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
627 return 1 if $running && !$scfg->{krbd
};
629 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
631 my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
632 run_rbd_command
($cmd, errmsg
=> "rbd resize '$volname' error");
636 sub volume_snapshot
{
637 my ($class, $scfg, $storeid, $volname, $snap) = @_;
639 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
641 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
642 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
646 sub volume_snapshot_rollback
{
647 my ($class, $scfg, $storeid, $volname, $snap) = @_;
649 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
651 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
652 run_rbd_command
($cmd, errmsg
=> "rbd snapshot $volname to '$snap' error");
655 sub volume_snapshot_delete
{
656 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
658 return 1 if $running && !$scfg->{krbd
};
660 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
662 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
664 my (undef, undef, undef, $protected) = rbd_volume_info
($scfg, $storeid, $name, $snap);
666 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
667 run_rbd_command
($cmd, errmsg
=> "rbd unprotect $name snap '$snap' error");
670 my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
672 run_rbd_command
($cmd, errmsg
=> "rbd snapshot '$volname' error");
677 sub volume_has_feature
{
678 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
681 snapshot
=> { current
=> 1, snap
=> 1},
682 clone
=> { base
=> 1, snap
=> 1},
683 template
=> { current
=> 1},
684 copy
=> { base
=> 1, current
=> 1, snap
=> 1},
685 sparseinit
=> { base
=> 1, current
=> 1},
688 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
689 $class->parse_volname($volname);
695 $key = $isBase ?
'base' : 'current';
697 return 1 if $features->{$feature}->{$key};