]> git.proxmox.com Git - mirror_lxc.git/commitdiff
Re-organize API for global lxc.conf config
authorStéphane Graber <stgraber@ubuntu.com>
Thu, 9 Jan 2014 22:31:52 +0000 (17:31 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Fri, 10 Jan 2014 16:16:06 +0000 (11:16 -0500)
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 <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
16 files changed:
src/lua-lxc/core.c
src/lxc/arguments.c
src/lxc/bdev.c
src/lxc/cgroup.c
src/lxc/commands.c
src/lxc/conf.c
src/lxc/log.c
src/lxc/lxc_config.c
src/lxc/lxccontainer.c
src/lxc/lxccontainer.h
src/lxc/start.c
src/lxc/utils.c
src/lxc/utils.h
src/python-lxc/lxc.c
src/python-lxc/lxc/__init__.py
src/tests/snapshot.c

index aa5831c4f012c3db9d5eaad74af64280c34536eb..ae8ab016ff52bdf591bf34502b44e586d63f8b00 100644 (file)
@@ -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;
index bdde2f719b505b51da32fd0369a3e8aaeb0d3c3f..aaf96341ee1757be4d8a28b1e4058d20dcbccf97 100644 (file)
@@ -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;
        }
index 30b957469537cccb1f8594a8b9ec7cf7514d447f..a6557567f593e1d64f861d87b5261824a85237ba 100644 (file)
@@ -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)
index 946ab0eeb65d581e30f8582a28fe3f09ac3010b1..b1196b4f704e7cca033e45d17ef241d017d2c386 100644 (file)
@@ -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) {
index 07e64b646e846f45d50a6ebc39fabb477d0f0c9f..e993f2e252f4d27d1d57034627b14e7066965a4d 100644 (file)
@@ -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;
index a386d9486148b426a09a80e97c57904729b9fcc9..7e0edddecf4cc811c00ee8445d3b13f6e87dd804 100644 (file)
@@ -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;
index 86f7cd40ca898894e5866b12131b9d018b92b1e8..b09885c1b865a561f985854e05cbd14f3c7ac786 100644 (file)
@@ -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 */
index dcb3a3be4f458566c1c0eb8bb9c6c0ede2d57136..c4797b57ae3e203a636ddb06d36dda2eef1fd2d8 100644 (file)
 
 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);
                }
        }
index f999cfc625d189118807f9c92b93147ea5a28751..faa0a9fde5f6fa12529a595c010865b68d0f99bc 100644 (file)
@@ -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)
index a62411b52646ef0a538234aa9906ef5813e15483..921e47dee0171eabc5220bce77aae656bb7ee2d4 100644 (file)
@@ -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.
index a5d6e56188b25c9ba7d53d4c99d64c0d02875b7f..ce88d5f480dc0c38231172ff2a41d4d1241380bf 100644 (file)
@@ -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";
 
index ac89da13ffb0b02686c4cb28fce89f9087ef78f3..f860421757f3ed2fdb2151c5d72ccc9e7eba2fc3 100644 (file)
@@ -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;
index 847a613d79a28d99ea3024d2ec3416fe0c82aba7..1121d743706f4d6412dd59e39f4e0e8dd351eaec 100644 (file)
@@ -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
index 5ee8dd083a07228f89b55833837406b915bc231f..4381ab82937f93cd8723f44403ded9cbf7ed4c39 100644 (file)
@@ -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"},
index e389abba7e546fca45c9497f844348cf300c3423..0ca3e54c52c79dd87d19e798abbc0605f903a4dc 100644 (file)
@@ -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()
 
 
index da7b40927f546e3eb8ee53e57197a5fe756d89af..fe06077b660e1122699cc3d44becc40523d990a0 100644 (file)
@@ -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__);