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 check_ceph_installed
{
154 my ($service, $noerr) = @_;
156 $service = 'ceph_bin' if !defined($service);
158 # the flag file is checked as on a new installation, the binary gets
159 # extracted by dpkg before the installation is finished
160 if (! -x
$ceph_service->{$service} || -f
'/run/ceph-install-flag') {
161 die "binary not installed: $ceph_service->{$service}\n" if !$noerr;
169 sub check_ceph_configured
{
173 die "ceph not fully configured - missing '$pve_ckeyring_path'\n"
174 if ! -f
$pve_ckeyring_path;
179 sub check_ceph_inited
{
182 return undef if !check_ceph_installed
('ceph_mon', $noerr);
184 if (! -f
$pve_ceph_cfgpath) {
185 die "pveceph configuration not initialized\n" if !$noerr;
192 sub check_ceph_enabled
{
195 return undef if !check_ceph_inited
($noerr);
197 if (! -f
$ceph_cfgpath) {
198 die "pveceph configuration not enabled\n" if !$noerr;
205 my $set_pool_setting = sub {
206 my ($pool, $setting, $value) = @_;
209 if ($setting eq 'application') {
211 prefix
=> "osd pool application enable",
217 prefix
=> "osd pool set",
225 my $rados = PVE
::RADOS-
>new();
226 eval { $rados->mon_command($command); };
227 return $@ ?
$@ : undef;
231 my ($pool, $param) = @_;
233 # by default, pool size always resets min_size, so set it as first item
234 # https://tracker.ceph.com/issues/44862
235 my $keys = [ grep { $_ ne 'size' } sort keys %$param ];
236 unshift @$keys, 'size' if exists $param->{size
};
238 for my $setting (@$keys) {
239 my $value = $param->{$setting};
241 print "pool $pool: applying $setting = $value\n";
242 if (my $err = $set_pool_setting->($pool, $setting, $value)) {
245 delete $param->{$setting};
249 if (scalar(keys %$param) > 0) {
250 my $missing = join(', ', sort keys %$param );
251 die "Could not set: $missing\n";
257 my ($pool, $param, $rados) = @_;
259 if (!defined($rados)) {
260 $rados = PVE
::RADOS-
>new();
263 my $pg_num = $param->{pg_num
} || 128;
265 $rados->mon_command({
266 prefix
=> "osd pool create",
268 pg_num
=> int($pg_num),
272 set_pool
($pool, $param);
277 my ($pool, $rados) = @_;
279 if (!defined($rados)) {
280 $rados = PVE
::RADOS-
>new();
283 my $res = $rados->mon_command({ prefix
=> "osd lspools" });
289 my ($pool, $rados) = @_;
291 if (!defined($rados)) {
292 $rados = PVE
::RADOS-
>new();
295 # fixme: '--yes-i-really-really-mean-it'
296 $rados->mon_command({
297 prefix
=> "osd pool delete",
300 'yes_i_really_really_mean_it' => JSON
::true
,
305 sub setup_pve_symlinks
{
306 # fail if we find a real file instead of a link
307 if (-f
$ceph_cfgpath) {
308 my $lnk = readlink($ceph_cfgpath);
309 die "file '$ceph_cfgpath' already exists and is not a symlink to $pve_ceph_cfgpath\n"
310 if !$lnk || $lnk ne $pve_ceph_cfgpath;
313 symlink($pve_ceph_cfgpath, $ceph_cfgpath) ||
314 die "unable to create symlink '$ceph_cfgpath' - $!\n";
316 my $ceph_uid = getpwnam('ceph');
317 my $ceph_gid = getgrnam('ceph');
318 chown $ceph_uid, $ceph_gid, $ceph_cfgdir;
321 sub get_or_create_admin_keyring
{
322 if (! -f
$pve_ckeyring_path) {
323 run_command
("ceph-authtool --create-keyring $pve_ckeyring_path " .
324 "--gen-key -n client.admin " .
325 "--cap mon 'allow *' " .
326 "--cap osd 'allow *' " .
327 "--cap mds 'allow *' " .
328 "--cap mgr 'allow *' ");
329 # we do not want to overwrite it
330 if (! -f
$ckeyring_path) {
331 run_command
("cp $pve_ckeyring_path $ckeyring_path");
332 run_command
("chown ceph:ceph $ckeyring_path");
335 return $pve_ckeyring_path;
338 # get ceph-volume managed osds
339 sub ceph_volume_list
{
342 if (!check_ceph_installed
('ceph_volume', 1)) {
347 my $cmd = [ $ceph_service->{ceph_volume
}, 'lvm', 'list', '--format', 'json' ];
348 run_command
($cmd, outfunc
=> sub { $output .= shift });
350 $result = eval { decode_json
($output) };
355 sub ceph_volume_zap
{
356 my ($osdid, $destroy) = @_;
358 die "no osdid given\n" if !defined($osdid);
360 my $cmd = [ $ceph_service->{ceph_volume
}, 'lvm', 'zap', '--osd-id', $osdid ];
361 push @$cmd, '--destroy' if $destroy;
366 sub get_db_wal_sizes
{
369 my $rados = PVE
::RADOS-
>new();
370 my $db_config = $rados->mon_command({ prefix
=> 'config-key dump', key
=> 'config/' });
372 $res->{db
} = $db_config->{"config/osd/bluestore_block_db_size"} //
373 $db_config->{"config/global/bluestore_block_db_size"};
375 $res->{wal
} = $db_config->{"config/osd/bluestore_block_wal_size"} //
376 $db_config->{"config/global/bluestore_block_wal_size"};
378 if (!$res->{db
} || !$res->{wal
}) {
379 my $cfg = cfs_read_file
('ceph.conf');
381 $res->{db
} = $cfg->{osd
}->{bluestore_block_db_size
} //
382 $cfg->{global
}->{bluestore_block_db_size
};
386 $res->{wal
} = $cfg->{osd
}->{bluestore_block_wal_size
} //
387 $cfg->{global
}->{bluestore_block_wal_size
};
393 sub get_possible_osd_flags
{
394 my $possible_flags = {
396 description
=> 'Pauses read and writes.',
401 description
=> 'OSDs are not allowed to start.',
406 description
=> 'OSD failure reports are being ignored, such that the monitors will not mark OSDs down.',
411 description
=> 'OSDs will not automatically be marked out after the configured interval.',
416 description
=> 'OSDs that were previously marked out will not be marked back in when they start.',
421 description
=> 'Backfilling of PGs is suspended.',
426 description
=> 'Rebalancing of PGs is suspended.',
431 description
=> 'Recovery of PGs is suspended.',
436 description
=> 'Scrubbing is disabled.',
441 description
=> 'Deep Scrubbing is disabled.',
446 description
=> 'Cache tiering activity is suspended.',
451 return $possible_flags;
454 sub get_real_flag_name
{
457 # the 'pause' flag gets always set to both 'pauserd' and 'pausewr'
458 # so decide that the 'pause' flag is set if we detect 'pauserd'
460 'pause' => 'pauserd',
463 return $flagmap->{$flag} // $flag;
466 sub ceph_cluster_status
{
468 $rados = PVE
::RADOS-
>new() if !$rados;
470 my $status = $rados->mon_command({ prefix
=> 'status' });
471 $status->{health
} = $rados->mon_command({ prefix
=> 'health', detail
=> 'detail' });
473 if (!exists $status->{monmap
}->{mons
}) { # octopus moved most info out of status, re-add
474 $status->{monmap
} = $rados->mon_command({ prefix
=> 'mon dump' });
475 $status->{mgrmap
} = $rados->mon_command({ prefix
=> 'mgr dump' });