1 package PVE
::Ceph
::Tools
;
11 use PVE
::Tools
qw(run_command dir_glob_foreach);
12 use PVE
::Cluster
qw(cfs_read_file);
14 use PVE
::Ceph
::Services
;
17 my $ccname = 'ceph'; # ceph cluster name
18 my $ceph_cfgdir = "/etc/ceph";
19 my $pve_ceph_cfgpath = "/etc/pve/$ccname.conf";
20 my $ceph_cfgpath = "$ceph_cfgdir/$ccname.conf";
22 my $pve_mon_key_path = "/etc/pve/priv/$ccname.mon.keyring";
23 my $pve_ckeyring_path = "/etc/pve/priv/$ccname.client.admin.keyring";
24 my $ckeyring_path = "/etc/ceph/ceph.client.admin.keyring";
25 my $ceph_bootstrap_osd_keyring = "/var/lib/ceph/bootstrap-osd/$ccname.keyring";
26 my $ceph_bootstrap_mds_keyring = "/var/lib/ceph/bootstrap-mds/$ccname.keyring";
27 my $ceph_mds_data_dir = '/var/lib/ceph/mds';
30 ceph_bin
=> "/usr/bin/ceph",
31 ceph_mon
=> "/usr/bin/ceph-mon",
32 ceph_mgr
=> "/usr/bin/ceph-mgr",
33 ceph_osd
=> "/usr/bin/ceph-osd",
34 ceph_mds
=> "/usr/bin/ceph-mds",
35 ceph_volume
=> '/usr/sbin/ceph-volume',
40 pve_ceph_cfgpath
=> $pve_ceph_cfgpath,
41 pve_mon_key_path
=> $pve_mon_key_path,
42 pve_ckeyring_path
=> $pve_ckeyring_path,
43 ceph_bootstrap_osd_keyring
=> $ceph_bootstrap_osd_keyring,
44 ceph_bootstrap_mds_keyring
=> $ceph_bootstrap_mds_keyring,
45 ceph_mds_data_dir
=> $ceph_mds_data_dir,
46 long_rados_timeout
=> 60,
47 ceph_cfgpath
=> $ceph_cfgpath,
50 sub get_local_version
{
53 if (check_ceph_installed
('ceph_bin', $noerr)) {
56 [ $ceph_service->{ceph_bin
}, '--version' ],
58 outfunc
=> sub { $ceph_version = shift if !defined $ceph_version },
60 return undef if !defined $ceph_version;
62 if ($ceph_version =~ /^ceph.*\sv?(\d+(?:\.\d+)+(?:-pve\d+)?)\s+(?:\(([a-zA-Z0-9]+)\))?/) {
63 my ($version, $buildcommit) = ($1, $2);
64 my $subversions = [ split(/\.|-/, $version) ];
66 # return (version, buildid, major, minor, ...) : major;
68 ?
($version, $buildcommit, $subversions)
76 sub get_cluster_versions
{
77 my ($service, $noerr) = @_;
79 my $rados = PVE
::RADOS-
>new();
80 my $cmd = $service ?
"$service versions" : 'versions';
81 return $rados->mon_command({ prefix
=> $cmd });
87 my $value = $config_hash->{$key};
89 die "no such ceph config '$key'" if !$value;
94 sub purge_all_ceph_files
{
97 my $monlist = [ split(',', PVE
::CephConfig
::get_monaddr_list
($pve_ceph_cfgpath)) ];
99 foreach my $service (keys %$services) {
100 my $type = $services->{$service};
103 foreach my $name (keys %$type) {
104 my $dir_exists = $type->{$name}->{direxists
};
106 $is_local_mon = grep($type->{$name}->{addr
}, @$monlist)
107 if $service eq 'mon';
109 my $path = "/var/lib/ceph/$service";
110 $path = '/var/log/ceph' if $service eq 'logs';
113 File
::Path
::remove_tree
($path, {
117 warn "Error removing path, '$path'\n" if @$err;
122 if (scalar @$monlist > 0 && !$is_local_mon) {
123 warn "Foreign MON address in ceph.conf. Keeping config & keyrings\n"
125 print "Removing config & keyring files\n";
126 foreach my $file (%$config_hash) {
127 unlink $file if (-e
$file);
132 sub purge_all_ceph_services
{
135 foreach my $service (keys %$services) {
136 my $type = $services->{$service};
139 foreach my $name (keys %$type) {
140 my $service_exists = $type->{$name}->{service
};
142 if ($service_exists) {
143 eval { PVE
::Ceph
::Services
::ceph_service_cmd
('disable', "$service.$name") };
144 warn "Could not disable ceph-$service\@$name, error: $@\n" if $@;
146 eval { PVE
::Ceph
::Services
::ceph_service_cmd
('stop', "$service.$name") };
147 warn "Could not stop ceph-$service\@$name, error: $@\n" if $@;
153 sub ceph_install_flag_file
{ return '/run/pve-ceph-install-flag' };
155 sub check_ceph_installed
{
156 my ($service, $noerr) = @_;
158 $service = 'ceph_bin' if !defined($service);
160 # NOTE: the flag file is checked as on a new installation, the binary gets
161 # extracted by dpkg before the installation is finished
162 if (! -x
$ceph_service->{$service} || -f ceph_install_flag_file
()) {
163 die "binary not installed: $ceph_service->{$service}\n" if !$noerr;
171 sub check_ceph_configured
{
175 die "ceph not fully configured - missing '$pve_ckeyring_path'\n"
176 if ! -f
$pve_ckeyring_path;
181 sub check_ceph_inited
{
184 return undef if !check_ceph_installed
('ceph_mon', $noerr);
186 if (! -f
$pve_ceph_cfgpath) {
187 die "pveceph configuration not initialized\n" if !$noerr;
194 sub check_ceph_enabled
{
197 return undef if !check_ceph_inited
($noerr);
199 if (! -f
$ceph_cfgpath) {
200 die "pveceph configuration not enabled\n" if !$noerr;
207 my $set_pool_setting = sub {
208 my ($pool, $setting, $value) = @_;
211 if ($setting eq 'application') {
213 prefix
=> "osd pool application enable",
219 prefix
=> "osd pool set",
227 my $rados = PVE
::RADOS-
>new();
228 eval { $rados->mon_command($command); };
229 return $@ ?
$@ : undef;
233 my ($pool, $param) = @_;
235 # by default, pool size always resets min_size, so set it as first item
236 # https://tracker.ceph.com/issues/44862
237 my $keys = [ grep { $_ ne 'size' } sort keys %$param ];
238 unshift @$keys, 'size' if exists $param->{size
};
240 for my $setting (@$keys) {
241 my $value = $param->{$setting};
243 print "pool $pool: applying $setting = $value\n";
244 if (my $err = $set_pool_setting->($pool, $setting, $value)) {
247 delete $param->{$setting};
251 if (scalar(keys %$param) > 0) {
252 my $missing = join(', ', sort keys %$param );
253 die "Could not set: $missing\n";
258 sub get_pool_properties
{
261 prefix
=> "osd pool get",
267 my $rados = PVE
::RADOS-
>new();
268 return $rados->mon_command($command);
272 my ($pool, $param, $rados) = @_;
273 $rados = PVE
::RADOS-
>new() if !defined($rados);
275 my $pg_num = $param->{pg_num
} || 128;
277 $rados->mon_command({
278 prefix
=> "osd pool create",
280 pg_num
=> int($pg_num),
284 set_pool
($pool, $param);
289 my ($pool, $rados) = @_;
290 $rados = PVE
::RADOS-
>new() if !defined($rados);
292 my $res = $rados->mon_command({ prefix
=> "osd lspools" });
298 my ($pool, $rados) = @_;
299 $rados = PVE
::RADOS-
>new() if !defined($rados);
301 # fixme: '--yes-i-really-really-mean-it'
302 $rados->mon_command({
303 prefix
=> "osd pool delete",
306 'yes_i_really_really_mean_it' => JSON
::true
,
311 # we get something like:
313 # 'metadata_pool_id' => 2,
314 # 'data_pool_ids' => [ 1 ],
315 # 'metadata_pool' => 'cephfs_metadata',
316 # 'data_pools' => [ 'cephfs_data' ],
317 # 'name' => 'cephfs',
321 $rados = PVE
::RADOS-
>new() if !defined($rados);
323 my $res = $rados->mon_command({ prefix
=> "fs ls" });
329 my ($fs, $param, $rados) = @_;
331 if (!defined($rados)) {
332 $rados = PVE
::RADOS-
>new();
335 $rados->mon_command({
338 metadata
=> $param->{pool_metadata
},
339 data
=> $param->{pool_data
},
345 my ($fs, $rados) = @_;
346 $rados = PVE
::RADOS-
>new() if !defined($rados);
348 $rados->mon_command({
351 'yes_i_really_mean_it' => JSON
::true
,
356 sub setup_pve_symlinks
{
357 # fail if we find a real file instead of a link
358 if (-f
$ceph_cfgpath) {
359 my $lnk = readlink($ceph_cfgpath);
360 die "file '$ceph_cfgpath' already exists and is not a symlink to $pve_ceph_cfgpath\n"
361 if !$lnk || $lnk ne $pve_ceph_cfgpath;
364 symlink($pve_ceph_cfgpath, $ceph_cfgpath) ||
365 die "unable to create symlink '$ceph_cfgpath' - $!\n";
367 my $ceph_uid = getpwnam('ceph');
368 my $ceph_gid = getgrnam('ceph');
369 chown $ceph_uid, $ceph_gid, $ceph_cfgdir;
372 sub get_or_create_admin_keyring
{
373 if (! -f
$pve_ckeyring_path) {
374 run_command
("ceph-authtool --create-keyring $pve_ckeyring_path " .
375 "--gen-key -n client.admin " .
376 "--cap mon 'allow *' " .
377 "--cap osd 'allow *' " .
378 "--cap mds 'allow *' " .
379 "--cap mgr 'allow *' ");
380 # we do not want to overwrite it
381 if (! -f
$ckeyring_path) {
382 run_command
("cp $pve_ckeyring_path $ckeyring_path");
383 run_command
("chown ceph:ceph $ckeyring_path");
386 return $pve_ckeyring_path;
389 # get ceph-volume managed osds
390 sub ceph_volume_list
{
393 if (!check_ceph_installed
('ceph_volume', 1)) {
398 my $cmd = [ $ceph_service->{ceph_volume
}, 'lvm', 'list', '--format', 'json' ];
399 run_command
($cmd, outfunc
=> sub { $output .= shift });
401 $result = eval { decode_json
($output) };
406 sub ceph_volume_zap
{
407 my ($osdid, $destroy) = @_;
409 die "no osdid given\n" if !defined($osdid);
411 my $cmd = [ $ceph_service->{ceph_volume
}, 'lvm', 'zap', '--osd-id', $osdid ];
412 push @$cmd, '--destroy' if $destroy;
417 sub get_db_wal_sizes
{
420 my $rados = PVE
::RADOS-
>new();
421 my $db_config = $rados->mon_command({ prefix
=> 'config-key dump', key
=> 'config/' });
423 $res->{db
} = $db_config->{"config/osd/bluestore_block_db_size"} //
424 $db_config->{"config/global/bluestore_block_db_size"};
426 $res->{wal
} = $db_config->{"config/osd/bluestore_block_wal_size"} //
427 $db_config->{"config/global/bluestore_block_wal_size"};
429 if (!$res->{db
} || !$res->{wal
}) {
430 my $cfg = cfs_read_file
('ceph.conf');
432 $res->{db
} = $cfg->{osd
}->{bluestore_block_db_size
} //
433 $cfg->{global
}->{bluestore_block_db_size
};
437 $res->{wal
} = $cfg->{osd
}->{bluestore_block_wal_size
} //
438 $cfg->{global
}->{bluestore_block_wal_size
};
444 sub get_possible_osd_flags
{
445 my $possible_flags = {
447 description
=> 'Pauses read and writes.',
452 description
=> 'OSDs are not allowed to start.',
457 description
=> 'OSD failure reports are being ignored, such that the monitors will not mark OSDs down.',
462 description
=> 'OSDs will not automatically be marked out after the configured interval.',
467 description
=> 'OSDs that were previously marked out will not be marked back in when they start.',
472 description
=> 'Backfilling of PGs is suspended.',
477 description
=> 'Rebalancing of PGs is suspended.',
482 description
=> 'Recovery of PGs is suspended.',
487 description
=> 'Scrubbing is disabled.',
492 description
=> 'Deep Scrubbing is disabled.',
497 description
=> 'Cache tiering activity is suspended.',
502 return $possible_flags;
505 sub get_real_flag_name
{
508 # the 'pause' flag gets always set to both 'pauserd' and 'pausewr'
509 # so decide that the 'pause' flag is set if we detect 'pauserd'
511 'pause' => 'pauserd',
514 return $flagmap->{$flag} // $flag;
517 sub ceph_cluster_status
{
519 $rados = PVE
::RADOS-
>new() if !$rados;
521 my $status = $rados->mon_command({ prefix
=> 'status' });
522 $status->{health
} = $rados->mon_command({ prefix
=> 'health', detail
=> 'detail' });
524 if (!exists $status->{monmap
}->{mons
}) { # octopus moved most info out of status, re-add
525 $status->{monmap
} = $rados->mon_command({ prefix
=> 'mon dump' });
526 $status->{mgrmap
} = $rados->mon_command({ prefix
=> 'mgr dump' });
532 sub ecprofile_exists
{
535 my $rados = PVE
::RADOS-
>new();
536 my $res = $rados->mon_command({ prefix
=> 'osd erasure-code-profile ls' });
538 my $profiles = { map { $_ => 1 } @$res };
539 return $profiles->{$name};
542 sub create_ecprofile
{
543 my ($name, $k, $m, $failure_domain, $device_class) = @_;
545 $failure_domain = 'host' if !$failure_domain;
548 "crush-failure-domain=${failure_domain}",
553 push(@$profile, "crush-device-class=${device_class}") if $device_class;
555 my $rados = PVE
::RADOS-
>new();
556 $rados->mon_command({
557 prefix
=> 'osd erasure-code-profile set',
563 sub destroy_ecprofile
{
566 my $rados = PVE
::RADOS-
>new();
568 prefix
=> 'osd erasure-code-profile rm',
572 return $rados->mon_command($command);
575 sub get_ecprofile_name
{
577 return "pve_ec_${name}";
580 sub destroy_crush_rule
{
582 my $rados = PVE
::RADOS-
>new();
584 prefix
=> 'osd crush rule rm',
588 return $rados->mon_command($command);