From b464bce6e92043e670c5c07b0757961d0c38edf1 Mon Sep 17 00:00:00 2001 From: David Teigland Date: Wed, 11 May 2016 11:06:31 -0500 Subject: lvm2app: connect to lvmetad after init The liblvm python module calls lvm2app lvm_init() whenever the the python module is loaded, which causes any python application that loads the lvm python module to connect to lvmetad. Move the lvmetad connection into a later init stage that's called when lvm2app is actually used. --- daemons/clvmd/lvm-functions.c | 2 +- daemons/lvmetad/testclient.c | 2 +- lib/commands/toolcontext.c | 10 ++++++++-- lib/commands/toolcontext.h | 6 +++++- libdaemon/server/daemon-server.c | 2 ++ liblvm/lvm2app.h | 5 +++++ liblvm/lvm_base.c | 19 ++++++++++++++++++- liblvm/lvm_pv.c | 6 ++++++ liblvm/lvm_vg.c | 12 ++++++++++++ tools/lvmcmdline.c | 3 +-- 10 files changed, 59 insertions(+), 8 deletions(-) diff --git a/daemons/clvmd/lvm-functions.c b/daemons/clvmd/lvm-functions.c index e872fbe49..5cdabc2ca 100644 --- a/daemons/clvmd/lvm-functions.c +++ b/daemons/clvmd/lvm-functions.c @@ -904,7 +904,7 @@ int init_clvm(struct dm_hash_table *excl_uuid) if (!udev_init_library_context()) stack; - if (!(cmd = create_toolcontext(1, NULL, 0, 1, 1, 1))) { + if (!(cmd = create_toolcontext(1, NULL, 0, 1, 1, 1, 0))) { log_error("Failed to allocate command context"); udev_fin_library_context(); return 0; diff --git a/daemons/lvmetad/testclient.c b/daemons/lvmetad/testclient.c index 56940d95b..2a43a7571 100644 --- a/daemons/lvmetad/testclient.c +++ b/daemons/lvmetad/testclient.c @@ -124,7 +124,7 @@ int main(int argc, char **argv) { if (argc > 1) { int i; - struct cmd_context *cmd = create_toolcontext(0, NULL, 0, 0, 1, 1); + struct cmd_context *cmd = create_toolcontext(0, NULL, 0, 0, 1, 1, 0); for (i = 1; i < argc; ++i) { const char *uuid = NULL; scan(h, argv[i]); diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index 90e5d5adc..3f207bf90 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -1727,6 +1727,11 @@ static int _init_lvmetad(struct cmd_context *cmd) return 1; } + if (cmd->is_lvm2app) { + cmd->lvm2app_needs_connect = 1; + return 1; + } + if (!lvmetad_connect(cmd)) { log_warn("WARNING: Failed to connect to lvmetad. Falling back to device scanning."); return 1; @@ -1758,7 +1763,6 @@ static int _init_lvmpolld(struct cmd_context *cmd) int init_connections(struct cmd_context *cmd) { - if (!_init_lvmetad(cmd)) { log_error("Failed to initialize lvmetad connection."); goto bad; @@ -1782,7 +1786,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, unsigned set_buffering, unsigned threaded, unsigned set_connections, - unsigned set_filters) + unsigned set_filters, + unsigned is_lvm2app) { struct cmd_context *cmd; FILE *new_stream; @@ -1805,6 +1810,7 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, log_error("Failed to allocate command context"); return NULL; } + cmd->is_lvm2app = is_lvm2app; cmd->is_long_lived = is_long_lived; cmd->threaded = threaded ? 1 : 0; cmd->handles_missing_pvs = 0; diff --git a/lib/commands/toolcontext.h b/lib/commands/toolcontext.h index c3b9b2ea5..e44bf7c6c 100644 --- a/lib/commands/toolcontext.h +++ b/lib/commands/toolcontext.h @@ -144,6 +144,9 @@ struct cmd_context { unsigned vg_notify:1; unsigned lv_notify:1; unsigned pv_notify:1; + unsigned is_lvm2app:1; + unsigned lvm2app_init_done:1; + unsigned lvm2app_needs_connect:1; /* * Filtering. @@ -208,7 +211,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, unsigned set_buffering, unsigned threaded, unsigned set_connections, - unsigned set_filters); + unsigned set_filters, + unsigned is_lvm2app); void destroy_toolcontext(struct cmd_context *cmd); int refresh_toolcontext(struct cmd_context *cmd); int refresh_filters(struct cmd_context *cmd); diff --git a/libdaemon/server/daemon-server.c b/libdaemon/server/daemon-server.c index d18bd4b3f..347e1cdd2 100644 --- a/libdaemon/server/daemon-server.c +++ b/libdaemon/server/daemon-server.c @@ -489,6 +489,8 @@ static int handle_connect(daemon_state s) client_handle client = { .thread_id = 0 }; socklen_t sl = sizeof(sockaddr); + DEBUGLOG(&s, "handle connect"); + client.socket_fd = accept(s.socket_fd, (struct sockaddr *) &sockaddr, &sl); if (client.socket_fd < 0) return 0; diff --git a/liblvm/lvm2app.h b/liblvm/lvm2app.h index be53b2d40..4b8e7bd22 100644 --- a/liblvm/lvm2app.h +++ b/liblvm/lvm2app.h @@ -261,6 +261,11 @@ typedef struct lvm_property_value { */ lvm_t lvm_init(const char *system_dir); +/** + * Create connections. + */ +void lvm_init_to_use(lvm_t libh); + /** * Destroy a LVM handle allocated with lvm_init(). * diff --git a/liblvm/lvm_base.c b/liblvm/lvm_base.c index 8b4def271..0c7dd40cf 100644 --- a/liblvm/lvm_base.c +++ b/liblvm/lvm_base.c @@ -19,6 +19,7 @@ #include "metadata-exported.h" #include "lvm2app.h" #include "lvm_misc.h" +#include "lvmetad.h" const char *lvm_library_get_version(void) { @@ -45,7 +46,7 @@ static lvm_t _lvm_init(const char *system_dir) /* create context */ /* FIXME: split create_toolcontext */ /* FIXME: make all globals configurable */ - cmd = create_toolcontext(0, system_dir, 0, 0, 1, 1); + cmd = create_toolcontext(0, system_dir, 0, 0, 1, 1, 1); if (!cmd) return NULL; @@ -80,6 +81,20 @@ static lvm_t _lvm_init(const char *system_dir) return (lvm_t) cmd; } +void lvm_init_to_use(lvm_t libh) +{ + struct cmd_context *cmd = (struct cmd_context *)libh; + + if (cmd->lvm2app_init_done) + return; + cmd->lvm2app_init_done = 1; + + /* If _init_lvmetad() skipped lvmetad_connect, it set lvm2app_needs_connect. */ + if (!cmd->lvm2app_needs_connect) + return; + cmd->lvm2app_needs_connect = 0; + lvmetad_connect(cmd); +} lvm_t lvm_init(const char *system_dir) { @@ -170,6 +185,7 @@ const char *lvm_vgname_from_pvid(lvm_t libh, const char *pvid) struct saved_env e = store_user_env((struct cmd_context *)libh); if (id_read_format(&id, pvid)) { + lvm_init_to_use(libh); rc = find_vgname_from_pvid(cmd, (char *)id.uuid); } else { log_error(INTERNAL_ERROR "Unable to convert uuid"); @@ -184,6 +200,7 @@ const char *lvm_vgname_from_device(lvm_t libh, const char *device) const char *rc = NULL; struct cmd_context *cmd = (struct cmd_context *)libh; struct saved_env e = store_user_env(cmd); + lvm_init_to_use(libh); rc = find_vgname_from_pvname(cmd, device); restore_user_env(&e); return rc; diff --git a/liblvm/lvm_pv.c b/liblvm/lvm_pv.c index 4c40b8b78..acac09faa 100644 --- a/liblvm/lvm_pv.c +++ b/liblvm/lvm_pv.c @@ -123,6 +123,8 @@ int lvm_pv_remove(lvm_t libh, const char *pv_name) dm_list_init(&pv_names); + lvm_init_to_use(libh); + if (!str_list_add(cmd->mem, &pv_names, pv_name)) rc = -1; @@ -173,6 +175,7 @@ struct dm_list *lvm_list_pvs(lvm_t libh) { struct dm_list *rc; struct saved_env e = store_user_env((struct cmd_context *)libh); + lvm_init_to_use(libh); rc = _lvm_list_pvs(libh); restore_user_env(&e); return rc; @@ -374,6 +377,7 @@ pv_create_params_t lvm_pv_params_create(lvm_t libh, const char *pv_name) { pv_create_params_t rc; struct saved_env e = store_user_env((struct cmd_context *)libh); + lvm_init_to_use(libh); rc = _lvm_pv_params_create(libh, pv_name, NULL); restore_user_env(&e); return rc; @@ -421,6 +425,8 @@ static int _pv_create(pv_create_params_t params) struct cmd_context *cmd = (struct cmd_context *)params->libh; int rc = 0; + lvm_init_to_use(params->libh); + if (params->pv_p.pva.size) { if (params->pv_p.pva.size % SECTOR_SIZE) { log_errno(EINVAL, "Size not a multiple of 512"); diff --git a/liblvm/lvm_vg.c b/liblvm/lvm_vg.c index 8b3fc91c0..e25e430b3 100644 --- a/liblvm/lvm_vg.c +++ b/liblvm/lvm_vg.c @@ -54,6 +54,8 @@ vg_t lvm_vg_create(lvm_t libh, const char *vg_name) struct volume_group *vg = NULL; struct saved_env e = store_user_env((struct cmd_context *)libh); + lvm_init_to_use(libh); + vg = vg_lock_and_create((struct cmd_context *)libh, vg_name); /* FIXME: error handling is still TBD */ if (vg_read_error(vg)) { @@ -235,6 +237,9 @@ vg_t lvm_vg_open(lvm_t libh, const char *vgname, const char *mode, { vg_t rc; struct saved_env e = store_user_env((struct cmd_context*)libh); + + lvm_init_to_use(libh); + rc = _lvm_vg_open(libh, vgname, mode, flags); restore_user_env(&e); return rc; @@ -484,6 +489,8 @@ struct dm_list *lvm_list_vg_names(lvm_t libh) struct dm_list *rc = NULL; struct saved_env e = store_user_env((struct cmd_context *)libh); + lvm_init_to_use(libh); + if (lvmetad_vg_list_to_lvmcache((struct cmd_context *)libh)) { rc = get_vgnames((struct cmd_context *)libh, 0); } @@ -496,6 +503,7 @@ struct dm_list *lvm_list_vg_uuids(lvm_t libh) struct dm_list *rc = NULL; struct saved_env e = store_user_env((struct cmd_context *)libh); + lvm_init_to_use(libh); if (lvmetad_vg_list_to_lvmcache((struct cmd_context *)libh)) { rc = get_vgids((struct cmd_context *)libh, 0); } @@ -511,6 +519,8 @@ int lvm_scan(lvm_t libh) int rc = 0; struct saved_env e = store_user_env((struct cmd_context *)libh); + lvm_init_to_use(libh); + lvmcache_force_next_label_scan(); if (!lvmcache_label_scan((struct cmd_context *)libh)) rc = -1; @@ -552,6 +562,8 @@ int lvm_vg_name_validate(lvm_t libh, const char *name) struct cmd_context *cmd = (struct cmd_context *)libh; struct saved_env e = store_user_env(cmd); + lvm_init_to_use(libh); + if (validate_new_vg_name(cmd, name)) rc = 0; diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c index fa9cd8fa2..f20259e4d 100644 --- a/tools/lvmcmdline.c +++ b/tools/lvmcmdline.c @@ -1947,8 +1947,7 @@ struct cmd_context *init_lvm(unsigned set_connections, unsigned set_filters) */ dm_set_name_mangling_mode(DM_STRING_MANGLING_NONE); - if (!(cmd = create_toolcontext(0, NULL, 1, 0, - set_connections, set_filters))) { + if (!(cmd = create_toolcontext(0, NULL, 1, 0, set_connections, set_filters, 0))) { udev_fin_library_context(); return_NULL; } -- cgit v1.2.1