Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / libcephd / libcephd.cc
1 #include "acconfig.h"
2 #include "auth/Auth.h"
3 #include "auth/Crypto.h"
4 #include "auth/KeyRing.h"
5 #include "common/ceph_argparse.h"
6 #include "common/version.h"
7 #include "common/PluginRegistry.h"
8 #include "compressor/snappy/CompressionPluginSnappy.h"
9 #include "compressor/zlib/CompressionPluginZlib.h"
10 #include "compressor/zstd/CompressionPluginZstd.h"
11 #include "erasure-code/ErasureCodePlugin.h"
12 #if __x86_64__ && defined(HAVE_BETTER_YASM_ELF64)
13 #include "erasure-code/isa/ErasureCodePluginIsa.h"
14 #endif
15 #include "erasure-code/jerasure/ErasureCodePluginJerasure.h"
16 #include "erasure-code/jerasure/jerasure_init.h"
17 #include "erasure-code/lrc/ErasureCodePluginLrc.h"
18 #include "erasure-code/shec/ErasureCodePluginShec.h"
19 #include "include/cephd/libcephd.h"
20 #include "global/global_context.h"
21 #include "global/global_init.h"
22 #include "objclass/objclass.h"
23 #include "osd/OSD.h"
24 #include "osd/ClassHandler.h"
25
26 // forward declarations of RADOS class init functions
27 CLS_INIT(cephfs);
28 CLS_INIT(hello);
29 CLS_INIT(journal);
30 CLS_INIT(kvs);
31 CLS_INIT(lock);
32 CLS_INIT(log);
33 CLS_INIT(lua);
34 CLS_INIT(numops);
35 CLS_INIT(rbd);
36 CLS_INIT(refcount);
37 CLS_INIT(replica_log);
38 CLS_INIT(rgw);
39 CLS_INIT(statelog);
40 CLS_INIT(timeindex);
41 CLS_INIT(user);
42 CLS_INIT(version);
43
44 extern "C" void cephd_version(int *pmajor, int *pminor, int *ppatch)
45 {
46   if (pmajor)
47     *pmajor = LIBCEPHD_VER_MAJOR;
48   if (pminor)
49     *pminor = LIBCEPHD_VER_MINOR;
50   if (ppatch)
51     *ppatch = LIBCEPHD_VER_PATCH;
52 }
53
54 extern "C" const char *ceph_version(int *pmajor, int *pminor, int *ppatch)
55 {
56   int major, minor, patch;
57   const char *v = ceph_version_to_str();
58
59   int n = sscanf(v, "%d.%d.%d", &major, &minor, &patch);
60   if (pmajor)
61     *pmajor = (n >= 1) ? major : 0;
62   if (pminor)
63     *pminor = (n >= 2) ? minor : 0;
64   if (ppatch)
65     *ppatch = (n >= 3) ? patch : 0;
66   return v;
67 }
68
69 extern "C" int cephd_generate_fsid(char *buf, size_t len)
70 {
71     if (len < sizeof("b06ad912-70d7-4263-a5ff-011462a5929a")) {
72         return -ERANGE;
73     }
74
75     uuid_d fsid;
76     fsid.generate_random();
77     fsid.print(buf);
78
79     return 0;
80 }
81
82 extern "C" int cephd_generate_secret_key(char *buf, size_t len)
83 {
84     CephInitParameters iparams(CEPH_ENTITY_TYPE_MON);
85     CephContext *cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0);
86     cct->_conf->apply_changes(NULL);
87     cct->init_crypto();
88
89     CryptoKey key;
90     key.create(cct, CEPH_CRYPTO_AES);
91
92     cct->put();
93
94     string keystr;
95     key.encode_base64(keystr);
96     if (keystr.length() >= len) {
97         return -ERANGE;
98     }
99     strcpy(buf, keystr.c_str());
100     return keystr.length();
101 }
102
103 // load the embedded plugins. This is safe to call multiple
104 // times in the same process
105 void cephd_preload_embedded_plugins()
106 {
107   int r;
108
109   // load erasure coding plugins
110   {
111     ErasureCodePlugin* plugin;
112     ErasureCodePluginRegistry& reg = ErasureCodePluginRegistry::instance();
113     Mutex::Locker l(reg.lock);
114     reg.disable_dlclose = true;
115
116     // initialize jerasure (and gf-complete)
117     int w[] = { 4, 8, 16, 32 };
118     r = jerasure_init(4, w);
119     assert(r == 0);
120
121     plugin = new ErasureCodePluginJerasure();
122     r = reg.add("jerasure", plugin);
123     if (r == -EEXIST) {
124       delete plugin;
125     }
126     assert(r == 0);
127
128     plugin = new ErasureCodePluginLrc();
129     r = reg.add("lrc", plugin);
130     if (r == -EEXIST) {
131       delete plugin;
132     }
133     assert(r == 0);
134
135     plugin = new ErasureCodePluginShec();
136     r = reg.add("shec", plugin);
137     if (r == -EEXIST) {
138       delete plugin;
139     }
140     assert(r == 0);
141
142 #if __x86_64__ && defined(HAVE_BETTER_YASM_ELF64)
143     plugin = new ErasureCodePluginIsa();
144     r = reg.add("isa", plugin);
145     if (r == -EEXIST) {
146       delete plugin;
147     }
148     assert(r == 0);
149 #endif
150   }
151
152   // now load the compression plugins
153   {
154     Plugin *plugin;
155     PluginRegistry *reg = g_ceph_context->get_plugin_registry();
156     Mutex::Locker l(reg->lock);
157     reg->disable_dlclose = true;
158
159     plugin = new CompressionPluginSnappy(g_ceph_context);
160     r = reg->add("compressor", "snappy", plugin);
161     if (r == -EEXIST) {
162       delete plugin;
163     }
164     assert(r == 0);
165
166     plugin = new CompressionPluginZlib(g_ceph_context);
167     r = reg->add("compressor", "zlib", plugin);
168     if (r == -EEXIST) {
169       delete plugin;
170     }
171     assert(r == 0);
172
173     plugin = new CompressionPluginZstd(g_ceph_context);
174     r = reg->add("compressor", "zstd", plugin);
175     if (r == -EEXIST) {
176       delete plugin;
177     }
178     assert(r == 0);
179   }
180 }
181
182 void cephd_preload_rados_classes(OSD *osd)
183 {
184   // intialize RADOS classes
185   {
186     ClassHandler  *class_handler = osd->class_handler;
187     Mutex::Locker l(class_handler->mutex);
188
189 #ifdef WITH_CEPHFS
190     class_handler->add_embedded_class("cephfs");
191     cephfs_cls_init();
192 #endif
193     class_handler->add_embedded_class("hello");
194     hello_cls_init();
195     class_handler->add_embedded_class("journal");
196     journal_cls_init();
197 #ifdef WITH_KVS
198     class_handler->add_embedded_class("kvs");
199     kvs_cls_init();
200 #endif
201     class_handler->add_embedded_class("lock");
202     lock_cls_init();
203     class_handler->add_embedded_class("log");
204     log_cls_init();
205     class_handler->add_embedded_class("lua");
206     lua_cls_init();
207     class_handler->add_embedded_class("numops");
208     numops_cls_init();
209 #ifdef WITH_RBD
210     class_handler->add_embedded_class("rbd");
211     rbd_cls_init();
212 #endif
213     class_handler->add_embedded_class("refcount");
214     refcount_cls_init();
215     class_handler->add_embedded_class("replica_log");
216     replica_log_cls_init();
217 #ifdef WITH_RADOSGW
218     class_handler->add_embedded_class("rgw");
219     rgw_cls_init();
220 #endif
221     class_handler->add_embedded_class("statelog");
222     statelog_cls_init();
223     class_handler->add_embedded_class("timeindex");
224     timeindex_cls_init();
225     class_handler->add_embedded_class("user");
226     user_cls_init();
227     class_handler->add_embedded_class("version");
228     version_cls_init();
229   }
230 }
231
232 extern "C" int cephd_mon(int argc, const char **argv);
233 extern "C" int cephd_osd(int argc, const char **argv);
234 extern "C" int cephd_mds(int argc, const char **argv);
235 extern "C" int cephd_rgw(int argc, const char **argv);
236 extern "C" int cephd_rgw_admin(int argc, const char **argv);
237
238 int cephd_run_mon(int argc, const char **argv)
239 {
240     return cephd_mon(argc, argv);
241 }
242
243 int cephd_run_osd(int argc, const char **argv)
244 {
245     return cephd_osd(argc, argv);
246 }
247
248 int cephd_run_mds(int argc, const char **argv)
249 {
250     return cephd_mds(argc, argv);
251 }
252
253
254 int cephd_run_rgw(int argc, const char **argv)
255 {
256     return cephd_rgw(argc, argv);
257 }
258
259 int cephd_run_rgw_admin(int argc, const char **argv)
260 {
261     return cephd_rgw_admin(argc, argv);
262 }