]> git.proxmox.com Git - ceph.git/blob - ceph/src/mount/conf.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / mount / conf.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <string>
5 #include <vector>
6 #include <cstring>
7 #include <map>
8
9 #include "common/async/context_pool.h"
10 #include "common/ceph_context.h"
11 #include "common/ceph_argparse.h"
12 #include "common/config.h"
13 #include "global/global_init.h"
14
15 #include "auth/KeyRing.h"
16 #include "mon/MonClient.h"
17
18 #include "mount.ceph.h"
19
20 using namespace std;
21
22 extern "C" void mount_ceph_get_config_info(const char *config_file,
23 const char *name,
24 bool v2_addrs,
25 struct ceph_config_info *cci)
26 {
27 int err;
28 KeyRing keyring;
29 CryptoKey secret;
30 std::string secret_str;
31 std::string monaddrs;
32 vector<const char *> args = { "--name", name };
33 bool first = true;
34
35 if (config_file) {
36 args.push_back("--conf");
37 args.push_back(config_file);
38 }
39
40 /* Create CephContext */
41 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
42 CODE_ENVIRONMENT_UTILITY,
43 CINIT_FLAG_NO_DAEMON_ACTIONS|CINIT_FLAG_NO_MON_CONFIG);
44 auto& conf = cct->_conf;
45
46 conf.parse_env(cct->get_module_type()); // environment variables override
47 conf.apply_changes(nullptr);
48
49 auto fsid = conf.get_val<uuid_d>("fsid");
50 fsid.print(cci->cci_fsid);
51
52 ceph::async::io_context_pool ioc(1);
53 MonClient monc = MonClient(cct.get(), ioc);
54 err = monc.build_initial_monmap();
55 if (err)
56 goto scrape_keyring;
57
58 for (const auto& mon : monc.monmap.addr_mons) {
59 auto& eaddr = mon.first;
60
61 /*
62 * Filter v1 addrs if we're running in ms_mode=legacy. Filter
63 * v2 addrs for any other ms_mode.
64 */
65 if (v2_addrs) {
66 if (!eaddr.is_msgr2())
67 continue;
68 } else {
69 if (!eaddr.is_legacy())
70 continue;
71 }
72
73 std::string addr;
74 addr += eaddr.ip_only_to_str();
75 addr += ":";
76 addr += std::to_string(eaddr.get_port());
77 /* If this will overrun cci_mons, stop here */
78 if (monaddrs.length() + 1 + addr.length() + 1 > sizeof(cci->cci_mons))
79 break;
80
81 if (first)
82 first = false;
83 else
84 monaddrs += ",";
85
86 monaddrs += addr;
87 }
88
89 if (monaddrs.length())
90 strcpy(cci->cci_mons, monaddrs.c_str());
91 else
92 mount_ceph_debug("Could not discover monitor addresses\n");
93
94 scrape_keyring:
95 err = keyring.from_ceph_context(cct.get());
96 if (err) {
97 mount_ceph_debug("keyring.from_ceph_context failed: %d\n", err);
98 return;
99 }
100
101 if (!keyring.get_secret(conf->name, secret)) {
102 mount_ceph_debug("keyring.get_secret failed\n");
103 return;
104 }
105
106 secret.encode_base64(secret_str);
107
108 if (secret_str.length() + 1 > sizeof(cci->cci_secret)) {
109 mount_ceph_debug("secret is too long\n");
110 return;
111 }
112 strcpy(cci->cci_secret, secret_str.c_str());
113 }