]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph_mon.cc
4410e0dcb567b9a79b8222f792a2566b29ae67d8
[ceph.git] / ceph / src / ceph_mon.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18
19 #include <iostream>
20 #include <string>
21
22 #include "common/config.h"
23 #include "include/ceph_features.h"
24
25 #include "mon/MonMap.h"
26 #include "mon/Monitor.h"
27 #include "mon/MonitorDBStore.h"
28 #include "mon/MonClient.h"
29
30 #include "msg/Messenger.h"
31
32 #include "include/CompatSet.h"
33
34 #include "common/ceph_argparse.h"
35 #include "common/pick_address.h"
36 #include "common/Throttle.h"
37 #include "common/Timer.h"
38 #include "common/errno.h"
39 #include "common/Preforker.h"
40
41 #include "global/global_init.h"
42 #include "global/signal_handler.h"
43
44 #include "perfglue/heap_profiler.h"
45
46 #include "include/ceph_assert.h"
47
48 #define dout_subsys ceph_subsys_mon
49
50 using std::cerr;
51 using std::cout;
52 using std::list;
53 using std::map;
54 using std::ostringstream;
55 using std::string;
56 using std::vector;
57
58 using ceph::bufferlist;
59 using ceph::decode;
60 using ceph::encode;
61 using ceph::JSONFormatter;
62
63 Monitor *mon = NULL;
64
65
66 void handle_mon_signal(int signum)
67 {
68 if (mon)
69 mon->handle_signal(signum);
70 }
71
72
73 int obtain_monmap(MonitorDBStore &store, bufferlist &bl)
74 {
75 dout(10) << __func__ << dendl;
76 /*
77 * the monmap may be in one of three places:
78 * 'mon_sync:temp_newer_monmap' - stashed newer map for bootstrap
79 * 'monmap:<latest_version_no>' - the monmap we'd really like to have
80 * 'mon_sync:latest_monmap' - last monmap backed up for the last sync
81 * 'mkfs:monmap' - a monmap resulting from mkfs
82 */
83
84 if (store.exists("monmap", "last_committed")) {
85 version_t latest_ver = store.get("monmap", "last_committed");
86 if (store.exists("monmap", latest_ver)) {
87 int err = store.get("monmap", latest_ver, bl);
88 ceph_assert(err == 0);
89 ceph_assert(bl.length() > 0);
90 dout(10) << __func__ << " read last committed monmap ver "
91 << latest_ver << dendl;
92
93 // see if there is stashed newer map (see bootstrap())
94 if (store.exists("mon_sync", "temp_newer_monmap")) {
95 bufferlist bl2;
96 int err = store.get("mon_sync", "temp_newer_monmap", bl2);
97 ceph_assert(err == 0);
98 ceph_assert(bl2.length() > 0);
99 MonMap b;
100 b.decode(bl2);
101 if (b.get_epoch() > latest_ver) {
102 dout(10) << __func__ << " using stashed monmap " << b.get_epoch()
103 << " instead" << dendl;
104 bl = std::move(bl2);
105 } else {
106 dout(10) << __func__ << " ignoring stashed monmap " << b.get_epoch()
107 << dendl;
108 }
109 }
110 return 0;
111 }
112 }
113
114 if (store.exists("mon_sync", "in_sync")
115 || store.exists("mon_sync", "force_sync")) {
116 dout(10) << __func__ << " detected aborted sync" << dendl;
117 if (store.exists("mon_sync", "latest_monmap")) {
118 int err = store.get("mon_sync", "latest_monmap", bl);
119 ceph_assert(err == 0);
120 ceph_assert(bl.length() > 0);
121 dout(10) << __func__ << " read backup monmap" << dendl;
122 return 0;
123 }
124 }
125
126 if (store.exists("mon_sync", "temp_newer_monmap")) {
127 dout(10) << __func__ << " found temp_newer_monmap" << dendl;
128 int err = store.get("mon_sync", "temp_newer_monmap", bl);
129 ceph_assert(err == 0);
130 ceph_assert(bl.length() > 0);
131 return 0;
132 }
133
134 if (store.exists("mkfs", "monmap")) {
135 dout(10) << __func__ << " found mkfs monmap" << dendl;
136 int err = store.get("mkfs", "monmap", bl);
137 ceph_assert(err == 0);
138 ceph_assert(bl.length() > 0);
139 return 0;
140 }
141
142 derr << __func__ << " unable to find a monmap" << dendl;
143 return -ENOENT;
144 }
145
146 int check_mon_data_exists()
147 {
148 string mon_data = g_conf()->mon_data;
149 struct stat buf;
150 if (::stat(mon_data.c_str(), &buf)) {
151 if (errno != ENOENT) {
152 derr << "stat(" << mon_data << ") " << cpp_strerror(errno) << dendl;
153 }
154 return -errno;
155 }
156 return 0;
157 }
158
159 /** Check whether **mon data** is empty.
160 *
161 * Being empty means mkfs has not been run and there's no monitor setup
162 * at **g_conf()->mon_data**.
163 *
164 * If the directory g_conf()->mon_data is not empty we will return -ENOTEMPTY.
165 * Otherwise we will return 0. Any other negative returns will represent
166 * a failure to be handled by the caller.
167 *
168 * @return **0** on success, -ENOTEMPTY if not empty or **-errno** otherwise.
169 */
170 int check_mon_data_empty()
171 {
172 string mon_data = g_conf()->mon_data;
173
174 DIR *dir = ::opendir(mon_data.c_str());
175 if (!dir) {
176 derr << "opendir(" << mon_data << ") " << cpp_strerror(errno) << dendl;
177 return -errno;
178 }
179 int code = 0;
180 struct dirent *de = nullptr;
181 errno = 0;
182 while ((de = ::readdir(dir))) {
183 if (string(".") != de->d_name &&
184 string("..") != de->d_name &&
185 string("kv_backend") != de->d_name) {
186 code = -ENOTEMPTY;
187 break;
188 }
189 }
190 if (!de && errno) {
191 derr << "readdir(" << mon_data << ") " << cpp_strerror(errno) << dendl;
192 code = -errno;
193 }
194
195 ::closedir(dir);
196
197 return code;
198 }
199
200 static void usage()
201 {
202 cout << "usage: ceph-mon -i <ID> [flags]\n"
203 << " --debug_mon n\n"
204 << " debug monitor level (e.g. 10)\n"
205 << " --mkfs\n"
206 << " build fresh monitor fs\n"
207 << " --force-sync\n"
208 << " force a sync from another mon by wiping local data (BE CAREFUL)\n"
209 << " --yes-i-really-mean-it\n"
210 << " mandatory safeguard for --force-sync\n"
211 << " --compact\n"
212 << " compact the monitor store\n"
213 << " --osdmap <filename>\n"
214 << " only used when --mkfs is provided: load the osdmap from <filename>\n"
215 << " --inject-monmap <filename>\n"
216 << " write the <filename> monmap to the local monitor store and exit\n"
217 << " --extract-monmap <filename>\n"
218 << " extract the monmap from the local monitor store and exit\n"
219 << " --mon-data <directory>\n"
220 << " where the mon store and keyring are located\n"
221 << " --set-crush-location <bucket>=<foo>"
222 << " sets monitor's crush bucket location (only for stretch mode)"
223 << std::endl;
224 generic_server_usage();
225 }
226
227 entity_addrvec_t make_mon_addrs(entity_addr_t a)
228 {
229 entity_addrvec_t addrs;
230 if (a.get_port() == 0) {
231 a.set_type(entity_addr_t::TYPE_MSGR2);
232 a.set_port(CEPH_MON_PORT_IANA);
233 addrs.v.push_back(a);
234 a.set_type(entity_addr_t::TYPE_LEGACY);
235 a.set_port(CEPH_MON_PORT_LEGACY);
236 addrs.v.push_back(a);
237 } else if (a.get_port() == CEPH_MON_PORT_LEGACY) {
238 a.set_type(entity_addr_t::TYPE_LEGACY);
239 addrs.v.push_back(a);
240 } else if (a.get_type() == entity_addr_t::TYPE_ANY) {
241 a.set_type(entity_addr_t::TYPE_MSGR2);
242 addrs.v.push_back(a);
243 } else {
244 addrs.v.push_back(a);
245 }
246 return addrs;
247 }
248
249 int main(int argc, const char **argv)
250 {
251 // reset our process name, in case we did a respawn, so that it's not
252 // left as "exe".
253 ceph_pthread_setname(pthread_self(), "ceph-mon");
254
255 int err;
256
257 bool mkfs = false;
258 bool compact = false;
259 bool force_sync = false;
260 bool yes_really = false;
261 std::string osdmapfn, inject_monmap, extract_monmap, crush_loc;
262
263 auto args = argv_to_vec(argc, argv);
264 if (args.empty()) {
265 cerr << argv[0] << ": -h or --help for usage" << std::endl;
266 exit(1);
267 }
268 if (ceph_argparse_need_usage(args)) {
269 usage();
270 exit(0);
271 }
272
273 // We need to specify some default values that may be overridden by the
274 // user, that are specific to the monitor. The options we are overriding
275 // are also used on the OSD (or in any other component that uses leveldb),
276 // so changing the global defaults is not an option.
277 // This is not the prettiest way of doing this, especially since it has us
278 // having a different place defining default values, but it's not horribly
279 // wrong enough to prevent us from doing it :)
280 //
281 // NOTE: user-defined options will take precedence over ours.
282 //
283 // leveldb_write_buffer_size = 32*1024*1024 = 33554432 // 32MB
284 // leveldb_cache_size = 512*1024*1204 = 536870912 // 512MB
285 // leveldb_block_size = 64*1024 = 65536 // 64KB
286 // leveldb_compression = false
287 // leveldb_log = ""
288 map<string,string> defaults = {
289 { "leveldb_write_buffer_size", "33554432" },
290 { "leveldb_cache_size", "536870912" },
291 { "leveldb_block_size", "65536" },
292 { "leveldb_compression", "false"},
293 { "leveldb_log", "" },
294 { "keyring", "$mon_data/keyring" },
295 };
296
297 int flags = 0;
298 {
299 vector<const char*> args_copy = args;
300 std::string val;
301 for (std::vector<const char*>::iterator i = args_copy.begin();
302 i != args_copy.end(); ) {
303 if (ceph_argparse_double_dash(args_copy, i)) {
304 break;
305 } else if (ceph_argparse_flag(args_copy, i, "--mkfs", (char*)NULL)) {
306 flags |= CINIT_FLAG_NO_DAEMON_ACTIONS;
307 } else if (ceph_argparse_witharg(args_copy, i, &val, "--inject_monmap", (char*)NULL)) {
308 flags |= CINIT_FLAG_NO_DAEMON_ACTIONS;
309 } else if (ceph_argparse_witharg(args_copy, i, &val, "--extract-monmap", (char*)NULL)) {
310 flags |= CINIT_FLAG_NO_DAEMON_ACTIONS;
311 } else {
312 ++i;
313 }
314 }
315 }
316
317 // don't try to get config from mon cluster during startup
318 flags |= CINIT_FLAG_NO_MON_CONFIG;
319
320 auto cct = global_init(&defaults, args,
321 CEPH_ENTITY_TYPE_MON, CODE_ENVIRONMENT_DAEMON,
322 flags);
323 ceph_heap_profiler_init();
324
325 std::string val;
326 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
327 if (ceph_argparse_double_dash(args, i)) {
328 break;
329 } else if (ceph_argparse_flag(args, i, "--mkfs", (char*)NULL)) {
330 mkfs = true;
331 } else if (ceph_argparse_flag(args, i, "--compact", (char*)NULL)) {
332 compact = true;
333 } else if (ceph_argparse_flag(args, i, "--force-sync", (char*)NULL)) {
334 force_sync = true;
335 } else if (ceph_argparse_flag(args, i, "--yes-i-really-mean-it", (char*)NULL)) {
336 yes_really = true;
337 } else if (ceph_argparse_witharg(args, i, &val, "--osdmap", (char*)NULL)) {
338 osdmapfn = val;
339 } else if (ceph_argparse_witharg(args, i, &val, "--inject_monmap", (char*)NULL)) {
340 inject_monmap = val;
341 } else if (ceph_argparse_witharg(args, i, &val, "--extract-monmap", (char*)NULL)) {
342 extract_monmap = val;
343 } else if (ceph_argparse_witharg(args, i, &val, "--set-crush-location", (char*)NULL)) {
344 crush_loc = val;
345 } else {
346 ++i;
347 }
348 }
349 if (!args.empty()) {
350 cerr << "too many arguments: " << args << std::endl;
351 exit(1);
352 }
353
354 if (force_sync && !yes_really) {
355 cerr << "are you SURE you want to force a sync? this will erase local data and may\n"
356 << "break your mon cluster. pass --yes-i-really-mean-it if you do." << std::endl;
357 exit(1);
358 }
359
360 if (g_conf()->mon_data.empty()) {
361 cerr << "must specify '--mon-data=foo' data path" << std::endl;
362 exit(1);
363 }
364
365 if (g_conf()->name.get_id().empty()) {
366 cerr << "must specify id (--id <id> or --name mon.<id>)" << std::endl;
367 exit(1);
368 }
369
370 // -- mkfs --
371 if (mkfs) {
372
373 int err = check_mon_data_exists();
374 if (err == -ENOENT) {
375 if (::mkdir(g_conf()->mon_data.c_str(), 0755)) {
376 derr << "mkdir(" << g_conf()->mon_data << ") : "
377 << cpp_strerror(errno) << dendl;
378 exit(1);
379 }
380 } else if (err < 0) {
381 derr << "error opening '" << g_conf()->mon_data << "': "
382 << cpp_strerror(-err) << dendl;
383 exit(-err);
384 }
385
386 err = check_mon_data_empty();
387 if (err == -ENOTEMPTY) {
388 // Mon may exist. Let the user know and exit gracefully.
389 derr << "'" << g_conf()->mon_data << "' already exists and is not empty"
390 << ": monitor may already exist" << dendl;
391 exit(0);
392 } else if (err < 0) {
393 derr << "error checking if '" << g_conf()->mon_data << "' is empty: "
394 << cpp_strerror(-err) << dendl;
395 exit(-err);
396 }
397
398 // resolve public_network -> public_addr
399 pick_addresses(g_ceph_context, CEPH_PICK_ADDRESS_PUBLIC);
400
401 dout(10) << "public_network " << g_conf()->public_network << dendl;
402 dout(10) << "public_addr " << g_conf()->public_addr << dendl;
403 dout(10) << "public_addrv " << g_conf()->public_addrv << dendl;
404
405 common_init_finish(g_ceph_context);
406
407 bufferlist monmapbl, osdmapbl;
408 std::string error;
409 MonMap monmap;
410
411 // load or generate monmap
412 const auto monmap_fn = g_conf().get_val<string>("monmap");
413 if (monmap_fn.length()) {
414 int err = monmapbl.read_file(monmap_fn.c_str(), &error);
415 if (err < 0) {
416 derr << argv[0] << ": error reading " << monmap_fn << ": " << error << dendl;
417 exit(1);
418 }
419 try {
420 monmap.decode(monmapbl);
421
422 // always mark seed/mkfs monmap as epoch 0
423 monmap.set_epoch(0);
424 } catch (const ceph::buffer::error& e) {
425 derr << argv[0] << ": error decoding monmap " << monmap_fn << ": " << e.what() << dendl;
426 exit(1);
427 }
428
429 dout(1) << "imported monmap:\n";
430 monmap.print(*_dout);
431 *_dout << dendl;
432
433 } else {
434 ostringstream oss;
435 int err = monmap.build_initial(g_ceph_context, true, oss);
436 if (oss.tellp())
437 derr << oss.str() << dendl;
438 if (err < 0) {
439 derr << argv[0] << ": warning: no initial monitors; must use admin socket to feed hints" << dendl;
440 }
441
442 dout(1) << "initial generated monmap:\n";
443 monmap.print(*_dout);
444 *_dout << dendl;
445
446 // am i part of the initial quorum?
447 if (monmap.contains(g_conf()->name.get_id())) {
448 // hmm, make sure the ip listed exists on the current host?
449 // maybe later.
450 } else if (!g_conf()->public_addrv.empty()) {
451 entity_addrvec_t av = g_conf()->public_addrv;
452 string name;
453 if (monmap.contains(av, &name)) {
454 monmap.rename(name, g_conf()->name.get_id());
455 dout(0) << argv[0] << ": renaming mon." << name << " " << av
456 << " to mon." << g_conf()->name.get_id() << dendl;
457 }
458 } else if (!g_conf()->public_addr.is_blank_ip()) {
459 entity_addrvec_t av = make_mon_addrs(g_conf()->public_addr);
460 string name;
461 if (monmap.contains(av, &name)) {
462 monmap.rename(name, g_conf()->name.get_id());
463 dout(0) << argv[0] << ": renaming mon." << name << " " << av
464 << " to mon." << g_conf()->name.get_id() << dendl;
465 }
466 } else {
467 // is a local address listed without a name? if so, name myself.
468 list<entity_addr_t> ls;
469 monmap.list_addrs(ls);
470 dout(0) << " monmap addrs are " << ls << ", checking if any are local"
471 << dendl;
472
473 entity_addr_t local;
474 if (have_local_addr(g_ceph_context, ls, &local)) {
475 dout(0) << " have local addr " << local << dendl;
476 string name;
477 local.set_type(entity_addr_t::TYPE_MSGR2);
478 if (!monmap.get_addr_name(local, name)) {
479 local.set_type(entity_addr_t::TYPE_LEGACY);
480 if (!monmap.get_addr_name(local, name)) {
481 dout(0) << "no local addresses appear in bootstrap monmap"
482 << dendl;
483 }
484 }
485 if (name.compare(0, 7, "noname-") == 0) {
486 dout(0) << argv[0] << ": mon." << name << " " << local
487 << " is local, renaming to mon." << g_conf()->name.get_id()
488 << dendl;
489 monmap.rename(name, g_conf()->name.get_id());
490 } else if (name.size()) {
491 dout(0) << argv[0] << ": mon." << name << " " << local
492 << " is local, but not 'noname-' + something; "
493 << "not assuming it's me" << dendl;
494 }
495 } else {
496 dout(0) << " no local addrs match monmap" << dendl;
497 }
498 }
499 }
500
501 const auto fsid = g_conf().get_val<uuid_d>("fsid");
502 if (!fsid.is_zero()) {
503 monmap.fsid = fsid;
504 dout(0) << argv[0] << ": set fsid to " << fsid << dendl;
505 }
506
507 if (monmap.fsid.is_zero()) {
508 derr << argv[0] << ": generated monmap has no fsid; use '--fsid <uuid>'" << dendl;
509 exit(10);
510 }
511
512 //monmap.print(cout);
513
514 // osdmap
515 if (osdmapfn.length()) {
516 err = osdmapbl.read_file(osdmapfn.c_str(), &error);
517 if (err < 0) {
518 derr << argv[0] << ": error reading " << osdmapfn << ": "
519 << error << dendl;
520 exit(1);
521 }
522 }
523
524 // go
525 MonitorDBStore store(g_conf()->mon_data);
526 ostringstream oss;
527 int r = store.create_and_open(oss);
528 if (oss.tellp())
529 derr << oss.str() << dendl;
530 if (r < 0) {
531 derr << argv[0] << ": error opening mon data directory at '"
532 << g_conf()->mon_data << "': " << cpp_strerror(r) << dendl;
533 exit(1);
534 }
535 ceph_assert(r == 0);
536
537 Monitor mon(g_ceph_context, g_conf()->name.get_id(), &store, 0, 0, &monmap);
538 r = mon.mkfs(osdmapbl);
539 if (r < 0) {
540 derr << argv[0] << ": error creating monfs: " << cpp_strerror(r) << dendl;
541 exit(1);
542 }
543 store.close();
544 dout(0) << argv[0] << ": created monfs at " << g_conf()->mon_data
545 << " for " << g_conf()->name << dendl;
546 return 0;
547 }
548
549 err = check_mon_data_exists();
550 if (err < 0 && err == -ENOENT) {
551 derr << "monitor data directory at '" << g_conf()->mon_data << "'"
552 << " does not exist: have you run 'mkfs'?" << dendl;
553 exit(1);
554 } else if (err < 0) {
555 derr << "error accessing monitor data directory at '"
556 << g_conf()->mon_data << "': " << cpp_strerror(-err) << dendl;
557 exit(1);
558 }
559
560 err = check_mon_data_empty();
561 if (err == 0) {
562 derr << "monitor data directory at '" << g_conf()->mon_data
563 << "' is empty: have you run 'mkfs'?" << dendl;
564 exit(1);
565 } else if (err < 0 && err != -ENOTEMPTY) {
566 // we don't want an empty data dir by now
567 derr << "error accessing '" << g_conf()->mon_data << "': "
568 << cpp_strerror(-err) << dendl;
569 exit(1);
570 }
571
572 {
573 // check fs stats. don't start if it's critically close to full.
574 ceph_data_stats_t stats;
575 int err = get_fs_stats(stats, g_conf()->mon_data.c_str());
576 if (err < 0) {
577 derr << "error checking monitor data's fs stats: " << cpp_strerror(err)
578 << dendl;
579 exit(-err);
580 }
581 if (stats.avail_percent <= g_conf()->mon_data_avail_crit) {
582 derr << "error: monitor data filesystem reached concerning levels of"
583 << " available storage space (available: "
584 << stats.avail_percent << "% " << byte_u_t(stats.byte_avail)
585 << ")\nyou may adjust 'mon data avail crit' to a lower value"
586 << " to make this go away (default: " << g_conf()->mon_data_avail_crit
587 << "%)\n" << dendl;
588 exit(ENOSPC);
589 }
590 }
591
592 // we fork early to prevent leveldb's environment static state from
593 // screwing us over
594 Preforker prefork;
595 if (!(flags & CINIT_FLAG_NO_DAEMON_ACTIONS)) {
596 if (global_init_prefork(g_ceph_context) >= 0) {
597 string err_msg;
598 err = prefork.prefork(err_msg);
599 if (err < 0) {
600 derr << err_msg << dendl;
601 prefork.exit(err);
602 }
603 if (prefork.is_parent()) {
604 err = prefork.parent_wait(err_msg);
605 if (err < 0)
606 derr << err_msg << dendl;
607 prefork.exit(err);
608 }
609 setsid();
610 global_init_postfork_start(g_ceph_context);
611 }
612 common_init_finish(g_ceph_context);
613 global_init_chdir(g_ceph_context);
614 if (global_init_preload_erasure_code(g_ceph_context) < 0)
615 prefork.exit(1);
616 }
617
618 // set up signal handlers, now that we've daemonized/forked.
619 init_async_signal_handler();
620
621 MonitorDBStore *store = new MonitorDBStore(g_conf()->mon_data);
622
623 // make sure we aren't upgrading too fast
624 {
625 string val;
626 int r = store->read_meta("min_mon_release", &val);
627 if (r >= 0 && val.size()) {
628 ceph_release_t from_release = ceph_release_from_name(val);
629 ostringstream err;
630 if (!can_upgrade_from(from_release, "min_mon_release", err)) {
631 derr << err.str() << dendl;
632 prefork.exit(1);
633 }
634 }
635 }
636
637 {
638 ostringstream oss;
639 err = store->open(oss);
640 if (oss.tellp())
641 derr << oss.str() << dendl;
642 if (err < 0) {
643 derr << "error opening mon data directory at '"
644 << g_conf()->mon_data << "': " << cpp_strerror(err) << dendl;
645 prefork.exit(1);
646 }
647 }
648
649 bufferlist magicbl;
650 err = store->get(Monitor::MONITOR_NAME, "magic", magicbl);
651 if (err || !magicbl.length()) {
652 derr << "unable to read magic from mon data" << dendl;
653 prefork.exit(1);
654 }
655 string magic(magicbl.c_str(), magicbl.length()-1); // ignore trailing \n
656 if (strcmp(magic.c_str(), CEPH_MON_ONDISK_MAGIC)) {
657 derr << "mon fs magic '" << magic << "' != current '" << CEPH_MON_ONDISK_MAGIC << "'" << dendl;
658 prefork.exit(1);
659 }
660
661 err = Monitor::check_features(store);
662 if (err < 0) {
663 derr << "error checking features: " << cpp_strerror(err) << dendl;
664 prefork.exit(1);
665 }
666
667 // inject new monmap?
668 if (!inject_monmap.empty()) {
669 bufferlist bl;
670 std::string error;
671 int r = bl.read_file(inject_monmap.c_str(), &error);
672 if (r) {
673 derr << "unable to read monmap from " << inject_monmap << ": "
674 << error << dendl;
675 prefork.exit(1);
676 }
677
678 // get next version
679 version_t v = store->get("monmap", "last_committed");
680 dout(0) << "last committed monmap epoch is " << v << ", injected map will be " << (v+1)
681 << dendl;
682 v++;
683
684 // set the version
685 MonMap tmp;
686 tmp.decode(bl);
687 if (tmp.get_epoch() != v) {
688 dout(0) << "changing monmap epoch from " << tmp.get_epoch()
689 << " to " << v << dendl;
690 tmp.set_epoch(v);
691 }
692 bufferlist mapbl;
693 tmp.encode(mapbl, CEPH_FEATURES_ALL);
694 bufferlist final;
695 encode(v, final);
696 encode(mapbl, final);
697
698 auto t(std::make_shared<MonitorDBStore::Transaction>());
699 // save it
700 t->put("monmap", v, mapbl);
701 t->put("monmap", "latest", final);
702 t->put("monmap", "last_committed", v);
703 store->apply_transaction(t);
704
705 dout(0) << "done." << dendl;
706 prefork.exit(0);
707 }
708
709 // monmap?
710 MonMap monmap;
711 {
712 // note that even if we don't find a viable monmap, we should go ahead
713 // and try to build it up in the next if-else block.
714 bufferlist mapbl;
715 int err = obtain_monmap(*store, mapbl);
716 if (err >= 0) {
717 try {
718 monmap.decode(mapbl);
719 } catch (const ceph::buffer::error& e) {
720 derr << "can't decode monmap: " << e.what() << dendl;
721 }
722 } else {
723 derr << "unable to obtain a monmap: " << cpp_strerror(err) << dendl;
724 }
725
726 dout(10) << __func__ << " monmap:\n";
727 JSONFormatter jf(true);
728 jf.dump_object("monmap", monmap);
729 jf.flush(*_dout);
730 *_dout << dendl;
731
732 if (!extract_monmap.empty()) {
733 int r = mapbl.write_file(extract_monmap.c_str());
734 if (r < 0) {
735 r = -errno;
736 derr << "error writing monmap to " << extract_monmap << ": " << cpp_strerror(r) << dendl;
737 prefork.exit(1);
738 }
739 derr << "wrote monmap to " << extract_monmap << dendl;
740 prefork.exit(0);
741 }
742 }
743
744 // this is what i will bind to
745 entity_addrvec_t ipaddrs;
746
747 if (monmap.contains(g_conf()->name.get_id())) {
748 ipaddrs = monmap.get_addrs(g_conf()->name.get_id());
749
750 // print helpful warning if the conf file doesn't match
751 std::vector<std::string> my_sections = g_conf().get_my_sections();
752 std::string mon_addr_str;
753 if (g_conf().get_val_from_conf_file(my_sections, "mon addr",
754 mon_addr_str, true) == 0) {
755 entity_addr_t conf_addr;
756 if (conf_addr.parse(mon_addr_str)) {
757 entity_addrvec_t conf_addrs = make_mon_addrs(conf_addr);
758 if (ipaddrs != conf_addrs) {
759 derr << "WARNING: 'mon addr' config option " << conf_addrs
760 << " does not match monmap file" << std::endl
761 << " continuing with monmap configuration" << dendl;
762 }
763 } else
764 derr << "WARNING: invalid 'mon addr' config option" << std::endl
765 << " continuing with monmap configuration" << dendl;
766 }
767 } else {
768 dout(0) << g_conf()->name << " does not exist in monmap, will attempt to join an existing cluster" << dendl;
769
770 pick_addresses(g_ceph_context, CEPH_PICK_ADDRESS_PUBLIC);
771 if (!g_conf()->public_addrv.empty()) {
772 ipaddrs = g_conf()->public_addrv;
773 dout(0) << "using public_addrv " << ipaddrs << dendl;
774 } else if (!g_conf()->public_addr.is_blank_ip()) {
775 ipaddrs = make_mon_addrs(g_conf()->public_addr);
776 dout(0) << "using public_addr " << g_conf()->public_addr << " -> "
777 << ipaddrs << dendl;
778 } else {
779 MonMap tmpmap;
780 ostringstream oss;
781 int err = tmpmap.build_initial(g_ceph_context, true, oss);
782 if (oss.tellp())
783 derr << oss.str() << dendl;
784 if (err < 0) {
785 derr << argv[0] << ": error generating initial monmap: "
786 << cpp_strerror(err) << dendl;
787 prefork.exit(1);
788 }
789 if (tmpmap.contains(g_conf()->name.get_id())) {
790 ipaddrs = tmpmap.get_addrs(g_conf()->name.get_id());
791 } else {
792 derr << "no public_addr or public_network specified, and "
793 << g_conf()->name << " not present in monmap or ceph.conf" << dendl;
794 prefork.exit(1);
795 }
796 }
797 }
798
799 // bind
800 int rank = monmap.get_rank(g_conf()->name.get_id());
801 std::string public_msgr_type = g_conf()->ms_public_type.empty() ? g_conf().get_val<std::string>("ms_type") : g_conf()->ms_public_type;
802 Messenger *msgr = Messenger::create(g_ceph_context, public_msgr_type,
803 entity_name_t::MON(rank), "mon", 0);
804 if (!msgr)
805 exit(1);
806 msgr->set_cluster_protocol(CEPH_MON_PROTOCOL);
807 msgr->set_default_send_priority(CEPH_MSG_PRIO_HIGH);
808
809 msgr->set_default_policy(Messenger::Policy::stateless_server(0));
810 msgr->set_policy(entity_name_t::TYPE_MON,
811 Messenger::Policy::lossless_peer_reuse(
812 CEPH_FEATURE_SERVER_LUMINOUS));
813 msgr->set_policy(entity_name_t::TYPE_OSD,
814 Messenger::Policy::stateless_server(
815 CEPH_FEATURE_SERVER_LUMINOUS));
816 msgr->set_policy(entity_name_t::TYPE_CLIENT,
817 Messenger::Policy::stateless_server(0));
818 msgr->set_policy(entity_name_t::TYPE_MDS,
819 Messenger::Policy::stateless_server(0));
820
821 // throttle client traffic
822 Throttle *client_throttler = new Throttle(g_ceph_context, "mon_client_bytes",
823 g_conf()->mon_client_bytes);
824 msgr->set_policy_throttlers(entity_name_t::TYPE_CLIENT,
825 client_throttler, NULL);
826
827 // throttle daemon traffic
828 // NOTE: actual usage on the leader may multiply by the number of
829 // monitors if they forward large update messages from daemons.
830 Throttle *daemon_throttler = new Throttle(g_ceph_context, "mon_daemon_bytes",
831 g_conf()->mon_daemon_bytes);
832 msgr->set_policy_throttlers(entity_name_t::TYPE_OSD, daemon_throttler,
833 NULL);
834 msgr->set_policy_throttlers(entity_name_t::TYPE_MDS, daemon_throttler,
835 NULL);
836
837 entity_addrvec_t bind_addrs = ipaddrs;
838 entity_addrvec_t public_addrs = ipaddrs;
839
840 // check if the public_bind_addr option is set
841 if (!g_conf()->public_bind_addr.is_blank_ip()) {
842 bind_addrs = make_mon_addrs(g_conf()->public_bind_addr);
843 }
844
845 dout(0) << "starting " << g_conf()->name << " rank " << rank
846 << " at public addrs " << public_addrs
847 << " at bind addrs " << bind_addrs
848 << " mon_data " << g_conf()->mon_data
849 << " fsid " << monmap.get_fsid()
850 << dendl;
851
852 Messenger *mgr_msgr = Messenger::create(g_ceph_context, public_msgr_type,
853 entity_name_t::MON(rank), "mon-mgrc",
854 Messenger::get_pid_nonce());
855 if (!mgr_msgr) {
856 derr << "unable to create mgr_msgr" << dendl;
857 prefork.exit(1);
858 }
859
860 mon = new Monitor(g_ceph_context, g_conf()->name.get_id(), store,
861 msgr, mgr_msgr, &monmap);
862
863 mon->orig_argc = argc;
864 mon->orig_argv = argv;
865
866 if (force_sync) {
867 derr << "flagging a forced sync ..." << dendl;
868 ostringstream oss;
869 JSONFormatter jf(true);
870 mon->sync_force(&jf);
871 derr << "out:\n";
872 jf.flush(*_dout);
873 *_dout << dendl;
874 }
875
876 err = mon->preinit();
877 if (err < 0) {
878 derr << "failed to initialize" << dendl;
879 prefork.exit(1);
880 }
881
882 if (compact || g_conf()->mon_compact_on_start) {
883 derr << "compacting monitor store ..." << dendl;
884 mon->store->compact();
885 derr << "done compacting" << dendl;
886 }
887
888 // bind
889 err = msgr->bindv(bind_addrs);
890 if (err < 0) {
891 derr << "unable to bind monitor to " << bind_addrs << dendl;
892 prefork.exit(1);
893 }
894
895 // if the public and bind addr are different set the msgr addr
896 // to the public one, now that the bind is complete.
897 if (public_addrs != bind_addrs) {
898 msgr->set_addrs(public_addrs);
899 }
900
901 if (g_conf()->daemonize) {
902 global_init_postfork_finish(g_ceph_context);
903 prefork.daemonize();
904 }
905
906 msgr->start();
907 mgr_msgr->start();
908
909 mon->set_mon_crush_location(crush_loc);
910 mon->init();
911
912 register_async_signal_handler_oneshot(SIGINT, handle_mon_signal);
913 register_async_signal_handler_oneshot(SIGTERM, handle_mon_signal);
914 register_async_signal_handler(SIGHUP, handle_mon_signal);
915
916 if (g_conf()->inject_early_sigterm)
917 kill(getpid(), SIGTERM);
918
919 msgr->wait();
920 mgr_msgr->wait();
921
922 store->close();
923
924 unregister_async_signal_handler(SIGHUP, handle_mon_signal);
925 unregister_async_signal_handler(SIGINT, handle_mon_signal);
926 unregister_async_signal_handler(SIGTERM, handle_mon_signal);
927 shutdown_async_signal_handler();
928
929 delete mon;
930 delete store;
931 delete msgr;
932 delete mgr_msgr;
933 delete client_throttler;
934 delete daemon_throttler;
935
936 // cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
937 char s[20];
938 snprintf(s, sizeof(s), "gmon/%d", getpid());
939 if ((mkdir(s, 0755) == 0) && (chdir(s) == 0)) {
940 dout(0) << "ceph-mon: gmon.out should be in " << s << dendl;
941 }
942
943 prefork.signal_exit(0);
944 return 0;
945 }