From: Stéphane Graber Date: Thu, 9 Jan 2014 22:31:52 +0000 (-0500) Subject: Re-organize API for global lxc.conf config X-Git-Tag: lxc-1.0.0.beta2~65 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=593e84786e2b4709059989bee489deab5c923779;p=thirdparty%2Flxc.git Re-organize API for global lxc.conf config Instead of having one function for each possible key in lxc.conf which doesn't really scale and requires an API update for every new key, switch to a generic lxc_get_global_config_item() function which takes a key name as argument. Signed-off-by: Stéphane Graber Acked-by: Serge E. Hallyn --- diff --git a/src/lua-lxc/core.c b/src/lua-lxc/core.c index aa5831c4f..ae8ab016f 100644 --- a/src/lua-lxc/core.c +++ b/src/lua-lxc/core.c @@ -412,7 +412,7 @@ static int lxc_version_get(lua_State *L) { } static int lxc_default_config_path_get(lua_State *L) { - const char *lxcpath = lxc_get_default_config_path(); + const char *lxcpath = lxc_get_global_config_item("lxc.lxcpath"); lua_pushstring(L, lxcpath); return 1; diff --git a/src/lxc/arguments.c b/src/lxc/arguments.c index bdde2f719..aaf96341e 100644 --- a/src/lxc/arguments.c +++ b/src/lxc/arguments.c @@ -222,7 +222,7 @@ extern int lxc_arguments_parse(struct lxc_arguments *args, /* If no lxcpaths were given, use default */ if (!args->lxcpath_cnt) { - ret = lxc_arguments_lxcpath_add(args, default_lxc_path()); + ret = lxc_arguments_lxcpath_add(args, lxc_global_config_value("lxc.lxcpath")); if (ret < 0) return ret; } diff --git a/src/lxc/bdev.c b/src/lxc/bdev.c index 30b957469..a6557567f 100644 --- a/src/lxc/bdev.c +++ b/src/lxc/bdev.c @@ -565,7 +565,7 @@ static int zfs_clone(const char *opath, const char *npath, const char *oname, return -1; *p = '\0'; } else - zfsroot = default_zfs_root(); + zfsroot = lxc_global_config_value("lxc.zfsroot"); ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s/%s/rootfs", lxcpath, nname); @@ -695,7 +695,7 @@ static int zfs_create(struct bdev *bdev, const char *dest, const char *n, pid_t pid; if (!specs || !specs->zfs.zfsroot) - zfsroot = default_zfs_root(); + zfsroot = lxc_global_config_value("lxc.zfsroot"); else zfsroot = specs->zfs.zfsroot; @@ -982,7 +982,7 @@ static int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldna orig->type); return -1; } - vg = default_lvm_vg(); + vg = lxc_global_config_value("lxc.lvm_vg"); len = strlen("/dev/") + strlen(vg) + strlen(cname) + 2; if ((new->src = malloc(len)) == NULL) return -1; @@ -1032,7 +1032,7 @@ static int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldna return -1; } } else { - if (do_lvm_create(new->src, size, default_lvm_thin_pool()) < 0) { + if (do_lvm_create(new->src, size, lxc_global_config_value("lxc.lvm_thin_pool")) < 0) { ERROR("Error creating new lvm blockdev"); return -1; } @@ -1071,11 +1071,11 @@ static int lvm_create(struct bdev *bdev, const char *dest, const char *n, vg = specs->lvm.vg; if (!vg) - vg = default_lvm_vg(); + vg = lxc_global_config_value("lxc.lvm_vg"); thinpool = specs->lvm.thinpool; if (!thinpool) - thinpool = default_lvm_thin_pool(); + thinpool = lxc_global_config_value("lxc.lvm_thin_pool"); /* /dev/$vg/$lv */ if (specs->lvm.lv) diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c index 946ab0eeb..b1196b4f7 100644 --- a/src/lxc/cgroup.c +++ b/src/lxc/cgroup.c @@ -89,7 +89,7 @@ struct cgroup_meta_data *lxc_cgroup_load_meta() int saved_errno; errno = 0; - cgroup_use = default_cgroup_use(); + cgroup_use = lxc_global_config_value("lxc.cgroup.use"); if (!cgroup_use && errno != 0) return NULL; if (cgroup_use) { diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 07e64b646..e993f2e25 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -80,7 +80,7 @@ static int fill_sock_name(char *path, int len, const char *name, int ret; if (!inpath) { - lxcpath = default_lxc_path(); + lxcpath = lxc_global_config_value("lxc.lxcpath"); if (!lxcpath) { ERROR("Out of memory getting lxcpath"); return -1; diff --git a/src/lxc/conf.c b/src/lxc/conf.c index a386d9486..7e0edddec 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -1921,7 +1921,7 @@ static int mount_entry_on_absolute_rootfs(const struct mntent *mntent, return -1; } - lxcpath = default_lxc_path(); + lxcpath = lxc_global_config_value("lxc.lxcpath"); if (!lxcpath) { ERROR("Out of memory"); return -1; diff --git a/src/lxc/log.c b/src/lxc/log.c index 86f7cd40c..b09885c1b 100644 --- a/src/lxc/log.c +++ b/src/lxc/log.c @@ -324,7 +324,7 @@ extern int lxc_log_init(const char *name, const char *file, lxcpath = LOGPATH; /* try LOGPATH if lxcpath is the default */ - if (strcmp(lxcpath, default_lxc_path()) == 0) + if (strcmp(lxcpath, lxc_global_config_value("lxc.lxcpath")) == 0) ret = _lxc_log_set_file(name, NULL, 0); /* try in lxcpath */ diff --git a/src/lxc/lxc_config.c b/src/lxc/lxc_config.c index dcb3a3be4..c4797b57a 100644 --- a/src/lxc/lxc_config.c +++ b/src/lxc/lxc_config.c @@ -27,15 +27,15 @@ struct lxc_config_items { char *name; - const char *(*fn)(void); }; static struct lxc_config_items items[] = { - { .name = "lxc.lxcpath", .fn = &lxc_get_default_config_path, }, - { .name = "lxc.lvm_vg", .fn = &lxc_get_default_lvm_vg, }, - { .name = "lxc.lvm_thin_pool", .fn = &lxc_get_default_lvm_thin_pool, }, - { .name = "lxc.zfsroot", .fn = &lxc_get_default_zfs_root, }, + { .name = "lxc.default_config", }, + { .name = "lxc.lxcpath", }, + { .name = "lxc.lvm_vg", }, + { .name = "lxc.lvm_thin_pool", }, + { .name = "lxc.zfsroot", }, { .name = NULL, }, }; @@ -65,7 +65,7 @@ int main(int argc, char *argv[]) list_config_items(); for (i = &items[0]; i->name; i++) { if (strcmp(argv[1], i->name) == 0) { - printf("%s\n", i->fn()); + printf("%s\n", lxc_get_global_config_item(i->name)); exit(0); } } diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index f999cfc62..faa0a9fde 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -2035,24 +2035,9 @@ static int lxcapi_get_cgroup_item(struct lxc_container *c, const char *subsys, c return ret; } -const char *lxc_get_default_config_path(void) +const char *lxc_get_global_config_item(const char *key) { - return default_lxc_path(); -} - -const char *lxc_get_default_lvm_vg(void) -{ - return default_lvm_vg(); -} - -const char *lxc_get_default_lvm_thin_pool(void) -{ - return default_lvm_thin_pool(); -} - -const char *lxc_get_default_zfs_root(void) -{ - return default_zfs_root(); + return lxc_global_config_value(key); } const char *lxc_get_version(void) @@ -3035,7 +3020,7 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath if (configpath) c->config_path = strdup(configpath); else - c->config_path = strdup(default_lxc_path()); + c->config_path = strdup(lxc_global_config_value("lxc.lxcpath")); if (!c->config_path) { fprintf(stderr, "Out of memory"); @@ -3157,7 +3142,7 @@ int list_defined_containers(const char *lxcpath, char ***names, struct lxc_conta struct lxc_container *c; if (!lxcpath) - lxcpath = default_lxc_path(); + lxcpath = lxc_global_config_value("lxc.lxcpath"); dir = opendir(lxcpath); if (!dir) { @@ -3247,7 +3232,7 @@ int list_active_containers(const char *lxcpath, char ***nret, struct lxc_container *c; if (!lxcpath) - lxcpath = default_lxc_path(); + lxcpath = lxc_global_config_value("lxc.lxcpath"); lxcpath_len = strlen(lxcpath); if (cret) diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h index a62411b52..921e47dee 100644 --- a/src/lxc/lxccontainer.h +++ b/src/lxc/lxccontainer.h @@ -786,45 +786,14 @@ int lxc_container_put(struct lxc_container *c); */ int lxc_get_wait_states(const char **states); -/*! - * \brief Determine path to default configuration file. - * - * \return Static string representing full path to default configuration - * file. - * - * \note Returned string must not be freed. - */ -const char *lxc_get_default_config_path(void); - -/*! - * \brief Determine default LVM volume group. - * - * \return Static string representing default volume group, - * or \c NULL on error. - * - * \note Returned string must not be freed. - */ -const char *lxc_get_default_lvm_vg(void); - -/*! - * \brief Determine default LVM thin pool. - * - * \return Static string representing default lvm thin pool, - * or \c NULL on error. - * - * \note Returned string must not be freed. - */ -const char *lxc_get_default_lvm_thin_pool(void); - -/*! - * \brief Determine default ZFS root. +/* + * \brief Get the value for a global config key * - * \return Static string representing default ZFS root, - * or \c NULL on error. + * \param key The name of the config key * - * \note Returned string must not be freed. + * \return String representing the current value for the key. */ -const char *lxc_get_default_zfs_root(void); +const char *lxc_get_global_config_item(const char *key); /*! * \brief Determine version of LXC. diff --git a/src/lxc/start.c b/src/lxc/start.c index a5d6e5618..ce88d5f48 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -767,7 +767,7 @@ static int lxc_spawn(struct lxc_handler *handler) * default value is available */ if (getuid() == 0) - cgroup_pattern = default_cgroup_pattern(); + cgroup_pattern = lxc_global_config_value("lxc.cgroup.pattern"); if (!cgroup_pattern) cgroup_pattern = "%n"; diff --git a/src/lxc/utils.c b/src/lxc/utils.c index ac89da13f..f86042175 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -238,7 +238,7 @@ static char *copy_global_config_value(char *p) #define DEFAULT_THIN_POOL "lxc" #define DEFAULT_ZFSROOT "lxc" -static const char *lxc_global_config_value(const char *option_name) +const char *lxc_global_config_value(const char *option_name) { static const char * const options[][2] = { { "lxc.lvm_vg", DEFAULT_VG }, @@ -371,36 +371,6 @@ out: return values[i]; } -const char *default_lvm_vg(void) -{ - return lxc_global_config_value("lxc.lvm_vg"); -} - -const char *default_lvm_thin_pool(void) -{ - return lxc_global_config_value("lxc.lvm_thin_pool"); -} - -const char *default_zfs_root(void) -{ - return lxc_global_config_value("lxc.zfsroot"); -} - -const char *default_lxc_path(void) -{ - return lxc_global_config_value("lxc.lxcpath"); -} - -const char *default_cgroup_use(void) -{ - return lxc_global_config_value("lxc.cgroup.use"); -} - -const char *default_cgroup_pattern(void) -{ - return lxc_global_config_value("lxc.cgroup.pattern"); -} - const char *get_rundir() { const char *rundir; diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 847a613d7..1121d7437 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -41,16 +41,8 @@ extern int mkdir_p(const char *dir, mode_t mode); extern void remove_trailing_slashes(char *p); extern const char *get_rundir(void); -/* - * Return a buffer containing the default container path. - * Caller must NOT free this buffer, since it may be static. - */ -extern const char *default_lxc_path(void); -extern const char *default_zfs_root(void); -extern const char *default_lvm_vg(void); -extern const char *default_lvm_thin_pool(void); -extern const char *default_cgroup_use(void); -extern const char *default_cgroup_pattern(void); +extern const char *lxc_global_config_value(const char *option_name); + /* Define getline() if missing from the C library */ #ifndef HAVE_GETLINE #ifdef HAVE_FGETLN diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c index 5ee8dd083..4381ab829 100644 --- a/src/python-lxc/lxc.c +++ b/src/python-lxc/lxc.c @@ -325,9 +325,16 @@ LXC_attach_run_shell(PyObject *self, PyObject *arg) } static PyObject * -LXC_get_default_config_path(PyObject *self, PyObject *args) +LXC_get_global_config_item(PyObject *self, PyObject *args, PyObject *kwds) { - return PyUnicode_FromString(lxc_get_default_config_path()); + static char *kwlist[] = {"key", NULL}; + char* key = NULL; + + if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|", kwlist, + &key)) + return NULL; + + return PyUnicode_FromString(lxc_get_global_config_item(key)); } static PyObject * @@ -1670,8 +1677,8 @@ static PyMethodDef LXC_methods[] = { {"attach_run_shell", (PyCFunction)LXC_attach_run_shell, METH_O, "Starts up a shell when attaching, to use as the run parameter for " "attach or attach_wait"}, - {"get_default_config_path", (PyCFunction)LXC_get_default_config_path, - METH_NOARGS, + {"get_global_config_item", (PyCFunction)LXC_get_global_config_item, + METH_VARARGS|METH_KEYWORDS, "Returns the current LXC config path"}, {"get_version", (PyCFunction)LXC_get_version, METH_NOARGS, "Returns the current LXC library version"}, diff --git a/src/python-lxc/lxc/__init__.py b/src/python-lxc/lxc/__init__.py index e389abba7..0ca3e54c5 100644 --- a/src/python-lxc/lxc/__init__.py +++ b/src/python-lxc/lxc/__init__.py @@ -32,7 +32,7 @@ import warnings warnings.warn("The python-lxc API isn't yet stable " "and may change at any point in the future.", Warning, 2) -default_config_path = _lxc.get_default_config_path() +default_config_path = _lxc.get_global_config_item("lxc.lxcpath") version = _lxc.get_version() diff --git a/src/tests/snapshot.c b/src/tests/snapshot.c index da7b40927..fe06077b6 100644 --- a/src/tests/snapshot.c +++ b/src/tests/snapshot.c @@ -38,7 +38,7 @@ static void try_to_remove(void) c->destroy(c); lxc_container_put(c); } - snprintf(snappath, 1024, "%ssnaps/%s", lxc_get_default_config_path(), MYNAME); + snprintf(snappath, 1024, "%ssnaps/%s", lxc_get_global_config_item("lxc.lxcpath"), MYNAME); c = lxc_container_new("snap0", snappath); if (c) { if (c->is_defined(c)) @@ -92,7 +92,7 @@ int main(int argc, char *argv[]) struct stat sb; int ret; char path[1024]; - snprintf(path, 1024, "%ssnaps/%s/snap0/rootfs", lxc_get_default_config_path(), MYNAME); + snprintf(path, 1024, "%ssnaps/%s/snap0/rootfs", lxc_get_global_config_item("lxc.lxcpath"), MYNAME); ret = stat(path, &sb); if (ret != 0) { fprintf(stderr, "%s: %d: snapshot was not actually created\n", __FILE__, __LINE__);