From: Bassam Tabbara Date: Sat, 5 Nov 2016 01:10:08 +0000 (-0700) Subject: embedded: Support for running MON and OSD from libcephd X-Git-Tag: v11.1.0~128^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=06647591273a514c3b89b20a97efab5b8583f632;p=ceph.git embedded: Support for running MON and OSD from libcephd Added supported for running MON and OSD daemon code almost untouched directly from libcephd. Also added two API functions for generating an FSID and secret key that can be used to bootstrap a new cluster. Signed-off-by: Bassam Tabbara --- diff --git a/src/ceph_mon.cc b/src/ceph_mon.cc index d592b6804127..d253390d5619 100644 --- a/src/ceph_mon.cc +++ b/src/ceph_mon.cc @@ -156,7 +156,7 @@ int check_mon_data_empty() return code; } -void usage() +static void usage() { cerr << "usage: ceph-mon -i monid [flags]" << std::endl; cerr << " --debug_mon n\n"; @@ -180,7 +180,12 @@ void usage() generic_server_usage(); } -int main(int argc, const char **argv) +#ifdef BUILDING_FOR_EMBEDDED +void cephd_preload_embedded_plugins(); +extern "C" int cephd_mon(int argc, const char **argv) +#else +int main(int argc, const char **argv) +#endif { int err; @@ -492,8 +497,12 @@ int main(int argc, const char **argv) } common_init_finish(g_ceph_context); global_init_chdir(g_ceph_context); +#ifndef BUILDING_FOR_EMBEDDED if (global_init_preload_erasure_code(g_ceph_context) < 0) prefork.exit(1); +#else + cephd_preload_embedded_plugins(); +#endif } MonitorDBStore *store = new MonitorDBStore(g_conf->mon_data); diff --git a/src/ceph_osd.cc b/src/ceph_osd.cc index 5f1f26e16d67..404fcd430a3a 100644 --- a/src/ceph_osd.cc +++ b/src/ceph_osd.cc @@ -66,7 +66,7 @@ void handle_osd_signal(int signum) osd->handle_signal(signum); } -void usage() +static void usage() { cout << "usage: ceph-osd -i \n" << " --osd-data PATH data directory\n" @@ -90,7 +90,12 @@ void usage() generic_server_usage(); } -int main(int argc, const char **argv) +#ifdef BUILDING_FOR_EMBEDDED +void cephd_preload_embedded_plugins(); +extern "C" int cephd_osd(int argc, const char **argv) +#else +int main(int argc, const char **argv) +#endif { vector args; argv_to_vec(argc, argv, args); @@ -569,8 +574,10 @@ int main(int argc, const char **argv) return -1; global_init_chdir(g_ceph_context); +#ifndef BUILDING_FOR_EMBEDDED if (global_init_preload_erasure_code(g_ceph_context) < 0) return -1; +#endif osd = new OSD(g_ceph_context, store, diff --git a/src/include/cephd/libcephd.h b/src/include/cephd/libcephd.h index 6bc7871c852a..d8f90c6ddd3d 100644 --- a/src/include/cephd/libcephd.h +++ b/src/include/cephd/libcephd.h @@ -36,6 +36,46 @@ CEPH_LIBCEPHD_API void cephd_version(int *pmajor, int *pminor, int *ppatch); */ CEPH_LIBCEPHD_API const char *ceph_version(int *pmajor, int *pminor, int *ppatch); +/** + * Generates a new cluster id (fsid) and returns a hexadecimal string. + * + * @param context where to the store the handle + * @param buf where to write the fsid + * @param len the size of buf in bytes (should be at least 37) + * @returns 0 on success, negative error code on failure + * @returns -ERANGE if the buffer is too short to contain the key + */ +CEPH_LIBCEPHD_API int cephd_generate_fsid(char *buf, size_t len); + +/** + * Generates a new secret key and returns a base64 encoded string. + * + * @param context where to the store the handle + * @param buf where to write the fsid + * @param len the size of buf in bytes + * @returns 0 on success, negative error code on failure + * @returns -ERANGE if the buffer is too short to contain the key + */ +CEPH_LIBCEPHD_API int cephd_generate_secret_key(char *buf, size_t len); + +/** + * Runs ceph-mon passing in command line args + * + * @param argc number of parameters + * @param argv array of string arguments + * @returns 0 on success, negative error code on failure + */ +CEPH_LIBCEPHD_API int cephd_run_mon(int argc, const char **argv); + +/** + * Runs ceph-osd passing in command line args + * + * @param argc number of parameters + * @param argv array of string arguments + * @returns 0 on success, negative error code on failure + */ +CEPH_LIBCEPHD_API int cephd_run_osd(int argc, const char **argv); + #ifdef __cplusplus } #endif diff --git a/src/libcephd/CMakeLists.txt b/src/libcephd/CMakeLists.txt index 675e2d386a55..31ce2886834a 100644 --- a/src/libcephd/CMakeLists.txt +++ b/src/libcephd/CMakeLists.txt @@ -1,7 +1,11 @@ include(MergeStaticLibraries) add_library(cephd_base STATIC - libcephd.cc) + libcephd.cc + ../ceph_mon.cc + ../ceph_osd.cc) + +set_target_properties(cephd_base PROPERTIES COMPILE_DEFINITIONS BUILDING_FOR_EMBEDDED) set(merge_libs cephd_base diff --git a/src/libcephd/libcephd.cc b/src/libcephd/libcephd.cc index f360017f5b69..4a75e433c4d4 100644 --- a/src/libcephd/libcephd.cc +++ b/src/libcephd/libcephd.cc @@ -1,4 +1,8 @@ #include "acconfig.h" +#include "auth/Auth.h" +#include "auth/Crypto.h" +#include "auth/KeyRing.h" +#include "common/ceph_argparse.h" #include "common/version.h" #include "common/PluginRegistry.h" #include "compressor/snappy/CompressionPluginSnappy.h" @@ -12,6 +16,8 @@ #include "erasure-code/lrc/ErasureCodePluginLrc.h" #include "erasure-code/shec/ErasureCodePluginShec.h" #include "include/cephd/libcephd.h" +#include "global/global_context.h" +#include "global/global_init.h" extern "C" void cephd_version(int *pmajor, int *pminor, int *ppatch) { @@ -38,6 +44,40 @@ extern "C" const char *ceph_version(int *pmajor, int *pminor, int *ppatch) return v; } +extern "C" int cephd_generate_fsid(char *buf, size_t len) +{ + if (len < sizeof("b06ad912-70d7-4263-a5ff-011462a5929a")) { + return -ERANGE; + } + + uuid_d fsid; + fsid.generate_random(); + fsid.print(buf); + + return 0; +} + +extern "C" int cephd_generate_secret_key(char *buf, size_t len) +{ + CephInitParameters iparams(CEPH_ENTITY_TYPE_MON); + CephContext *cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0); + cct->_conf->apply_changes(NULL); + cct->init_crypto(); + + CryptoKey key; + key.create(cct, CEPH_CRYPTO_AES); + + cct->put(); + + string keystr; + key.encode_base64(keystr); + if (keystr.length() >= len) { + return -ERANGE; + } + strcpy(buf, keystr.c_str()); + return keystr.length(); +} + // load the embedded plugins. This is safe to call multiple // times in the same process void cephd_preload_embedded_plugins() @@ -109,3 +149,16 @@ void cephd_preload_embedded_plugins() assert(r == 0); } } + +extern "C" int cephd_mon(int argc, const char **argv); +extern "C" int cephd_osd(int argc, const char **argv); + +int cephd_run_mon(int argc, const char **argv) +{ + return cephd_mon(argc, argv); +} + +int cephd_run_osd(int argc, const char **argv) +{ + return cephd_osd(argc, argv); +}