1 package PVE
::Ceph
::Tools
;
9 use PVE
::Tools
qw(run_command dir_glob_foreach);
12 my $ccname = 'ceph'; # ceph cluster name
13 my $ceph_cfgdir = "/etc/ceph";
14 my $pve_ceph_cfgpath = "/etc/pve/$ccname.conf";
15 my $ceph_cfgpath = "$ceph_cfgdir/$ccname.conf";
17 my $pve_mon_key_path = "/etc/pve/priv/$ccname.mon.keyring";
18 my $pve_ckeyring_path = "/etc/pve/priv/$ccname.client.admin.keyring";
19 my $ceph_bootstrap_osd_keyring = "/var/lib/ceph/bootstrap-osd/$ccname.keyring";
20 my $ceph_bootstrap_mds_keyring = "/var/lib/ceph/bootstrap-mds/$ccname.keyring";
21 my $ceph_mds_data_dir = '/var/lib/ceph/mds';
24 ceph_bin
=> "/usr/bin/ceph",
25 ceph_mon
=> "/usr/bin/ceph-mon",
26 ceph_mgr
=> "/usr/bin/ceph-mgr",
27 ceph_osd
=> "/usr/bin/ceph-osd",
28 ceph_mds
=> "/usr/bin/ceph-mds",
33 pve_ceph_cfgpath
=> $pve_ceph_cfgpath,
34 pve_mon_key_path
=> $pve_mon_key_path,
35 pve_ckeyring_path
=> $pve_ckeyring_path,
36 ceph_bootstrap_osd_keyring
=> $ceph_bootstrap_osd_keyring,
37 ceph_bootstrap_mds_keyring
=> $ceph_bootstrap_mds_keyring,
38 ceph_mds_data_dir
=> $ceph_mds_data_dir,
39 long_rados_timeout
=> 60,
42 sub get_local_version
{
45 if (check_ceph_installed
('ceph_bin', $noerr)) {
47 run_command
([$ceph_service->{ceph_bin
}, '--version'],
49 outfunc
=> sub { $ceph_version = shift; });
50 if ($ceph_version && $ceph_version =~ /^ceph.*\s((\d+)\.(\d+)\.(\d+))/) {
51 # return (version, major, minor, patch) : major;
52 return wantarray ?
($1, $2, $3, $4) : $2;
62 my $value = $config_hash->{$key};
64 die "no such ceph config '$key'" if !$value;
69 sub purge_all_ceph_files
{
70 # fixme: this is very dangerous - should we really support this function?
74 unlink $pve_ceph_cfgpath;
75 unlink $pve_ckeyring_path;
76 unlink $pve_mon_key_path;
78 unlink $ceph_bootstrap_osd_keyring;
79 unlink $ceph_bootstrap_mds_keyring;
81 system("rm -rf /var/lib/ceph/mon/ceph-*");
86 sub check_ceph_installed
{
87 my ($service, $noerr) = @_;
89 $service = 'ceph_bin' if !defined($service);
91 if (! -x
$ceph_service->{$service}) {
92 die "binary not installed: $ceph_service->{$service}\n" if !$noerr;
99 sub check_ceph_inited
{
102 return undef if !check_ceph_installed
('ceph_bin', $noerr);
104 if (! -f
$pve_ceph_cfgpath) {
105 die "pveceph configuration not initialized\n" if !$noerr;
112 sub check_ceph_enabled
{
115 return undef if !check_ceph_inited
($noerr);
117 if (! -f
$ceph_cfgpath) {
118 die "pveceph configuration not enabled\n" if !$noerr;
126 my ($pool, $param, $rados) = @_;
128 if (!defined($rados)) {
129 $rados = PVE
::RADOS-
>new();
132 my $pg_num = $param->{pg_num
} || 128;
133 my $size = $param->{size
} || 3;
134 my $min_size = $param->{min_size
} || 2;
135 my $application = $param->{application
} // 'rbd';
137 $rados->mon_command({
138 prefix
=> "osd pool create",
140 pg_num
=> int($pg_num),
144 $rados->mon_command({
145 prefix
=> "osd pool set",
152 $rados->mon_command({
153 prefix
=> "osd pool set",
160 if (defined($param->{crush_rule
})) {
161 $rados->mon_command({
162 prefix
=> "osd pool set",
165 val
=> $param->{crush_rule
},
170 $rados->mon_command({
171 prefix
=> "osd pool application enable",
179 my ($pool, $rados) = @_;
181 if (!defined($rados)) {
182 $rados = PVE
::RADOS-
>new();
185 my $res = $rados->mon_command({ prefix
=> "osd lspools" });
191 my ($pool, $rados) = @_;
193 if (!defined($rados)) {
194 $rados = PVE
::RADOS-
>new();
197 # fixme: '--yes-i-really-really-mean-it'
198 $rados->mon_command({
199 prefix
=> "osd pool delete",
202 sure
=> '--yes-i-really-really-mean-it',
207 sub setup_pve_symlinks
{
208 # fail if we find a real file instead of a link
209 if (-f
$ceph_cfgpath) {
210 my $lnk = readlink($ceph_cfgpath);
211 die "file '$ceph_cfgpath' already exists\n"
212 if !$lnk || $lnk ne $pve_ceph_cfgpath;
214 symlink($pve_ceph_cfgpath, $ceph_cfgpath) ||
215 die "unable to create symlink '$ceph_cfgpath' - $!\n";
219 sub ceph_service_cmd
{
220 my ($action, $service) = @_;
222 if (systemd_managed
()) {
224 if ($service && $service =~ m/^(mon|osd|mds|mgr|radosgw)(\.([A-Za-z0-9\-]{1,32}))?$/) {
225 $service = defined($3) ?
"ceph-$1\@$3" : "ceph-$1.target";
227 $service = "ceph.target";
230 PVE
::Tools
::run_command
(['/bin/systemctl', $action, $service]);
233 # ceph daemons does not call 'setsid', so we do that ourself
234 # (fork_worker send KILL to whole process group)
235 PVE
::Tools
::run_command
(['setsid', 'service', 'ceph', '-c', $pve_ceph_cfgpath, $action, $service]);
239 # Ceph versions greater Hammer use 'ceph' as user and group instead
240 # of 'root', and use systemd.
241 sub systemd_managed
{
243 if (-f
"/lib/systemd/system/ceph-osd\@.service") {
250 sub list_local_mds_ids
{
253 PVE
::Tools
::dir_glob_foreach
($ceph_mds_data_dir, qr/$ccname-(\S+)/, sub {
254 my (undef, $mds_id) = @_;
255 push @$mds_list, $mds_id;
261 sub get_cluster_mds_state
{
266 if (!defined($rados)) {
267 $rados = PVE
::RADOS-
>new();
270 my $add_state = sub {
274 $state->{addr
} = $mds->{addr
};
275 $state->{rank
} = $mds->{rank
};
276 $state->{standby_replay
} = $mds->{standby_replay
} ?
1 : 0;
277 $state->{state} = $mds->{state};
279 $mds_state->{$mds->{name
}} = $state;
282 my $mds_dump = $rados->mon_command({ prefix
=> 'mds stat' });
283 my $fsmap = $mds_dump->{fsmap
};
286 foreach my $mds (@{$fsmap->{standbys
}}) {
290 my $fs_info = $fsmap->{filesystems
}->[0];
291 my $active_mds = $fs_info->{mdsmap
}->{info
};
293 # normally there's only one active MDS, but we can have multiple active for
294 # different ranks (e.g., different cephs path hierarchy). So just add all.
295 foreach my $mds (values %$active_mds) {
302 sub is_any_mds_active
{
305 if (!defined($rados)) {
306 $rados = PVE
::RADOS-
>new();
309 my $mds_dump = $rados->mon_command({ prefix
=> 'mds stat' });
310 my $fs = $mds_dump->{fsmap
}->{filesystems
};
312 if (!($fs && scalar(@$fs) > 0)) {
315 my $active_mds = $fs->[0]->{mdsmap
}->{info
};
317 for my $mds (values %$active_mds) {
318 return 1 if $mds->{state} eq 'up:active';
325 my ($id, $rados) = @_;
327 # `ceph fs status` fails with numeric only ID.
328 die "ID: $id, numeric only IDs are not supported\n"
331 if (!defined($rados)) {
332 $rados = PVE
::RADOS-
>new();
335 my $service_dir = "/var/lib/ceph/mds/$ccname-$id";
336 my $service_keyring = "$service_dir/keyring";
337 my $service_name = "mds.$id";
339 die "ceph MDS directory '$service_dir' already exists\n"
342 print "creating MDS directory '$service_dir'\n";
343 eval { File
::Path
::mkpath
($service_dir) };
345 die "creation MDS directory '$service_dir' failed\n" if $err;
347 # http://docs.ceph.com/docs/luminous/install/manual-deployment/#adding-mds
349 mon
=> 'allow profile mds',
354 print "creating keys for '$service_name'\n";
355 my $output = $rados->mon_command({
356 prefix
=> 'auth get-or-create',
357 entity
=> $service_name,
362 PVE
::Tools
::file_set_contents
($service_keyring, $output);
364 print "setting ceph as owner for service directory\n";
365 run_command
(["chown", 'ceph:ceph', '-R', $service_dir]);
367 print "enabling service 'ceph-mds\@$id.service'\n";
368 ceph_service_cmd
('enable', $service_name);
369 print "starting service 'ceph-mds\@$id.service'\n";
370 ceph_service_cmd
('start', $service_name);
376 my ($id, $rados) = @_;
378 if (!defined($rados)) {
379 $rados = PVE
::RADOS-
>new();
382 my $service_name = "mds.$id";
383 my $service_dir = "/var/lib/ceph/mds/$ccname-$id";
385 print "disabling service 'ceph-mds\@$id.service'\n";
386 ceph_service_cmd
('disable', $service_name);
387 print "stopping service 'ceph-mds\@$id.service'\n";
388 ceph_service_cmd
('stop', $service_name);
390 if (-d
$service_dir) {
391 print "removing ceph-mds directory '$service_dir'\n";
392 File
::Path
::remove_tree
($service_dir);
394 warn "cannot cleanup MDS $id directory, '$service_dir' not found\n"
397 print "removing ceph auth for '$service_name'\n";
398 $rados->mon_command({
399 prefix
=> 'auth del',
400 entity
=> $service_name,
407 # wipe the first 200 MB to clear off leftovers from previous use, otherwise a
412 my @wipe_cmd = qw(/bin/dd if=/dev/zero bs=1M count=200 conv=fdatasync);
413 foreach my $devpath (@devs) {
414 print "wipe disk: $devpath\n";
415 eval { run_command
([@wipe_cmd, "of=${devpath}"]) };