Skip to content

Commit

Permalink
Re-organize API for global lxc.conf config
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
stgraber committed Jan 10, 2014
1 parent 9e8be78 commit 593e847
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 123 deletions.
2 changes: 1 addition & 1 deletion src/lua-lxc/core.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/arguments.c
Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/lxc/bdev.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/cgroup.c
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/commands.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/conf.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/log.c
Expand Up @@ -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 */
Expand Down
12 changes: 6 additions & 6 deletions src/lxc/lxc_config.c
Expand Up @@ -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, },
};

Expand Down Expand Up @@ -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);
}
}
Expand Down
25 changes: 5 additions & 20 deletions src/lxc/lxccontainer.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 5 additions & 36 deletions src/lxc/lxccontainer.h
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/start.c
Expand Up @@ -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";

Expand Down
32 changes: 1 addition & 31 deletions src/lxc/utils.c
Expand Up @@ -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 },
Expand Down Expand Up @@ -371,36 +371,6 @@ static const char *lxc_global_config_value(const char *option_name)
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;
Expand Down
12 changes: 2 additions & 10 deletions src/lxc/utils.h
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions src/python-lxc/lxc.c
Expand Up @@ -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 *
Expand Down Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion src/python-lxc/lxc/__init__.py
Expand Up @@ -32,7 +32,7 @@
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()


Expand Down
4 changes: 2 additions & 2 deletions src/tests/snapshot.c
Expand Up @@ -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))
Expand Down Expand Up @@ -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__);
Expand Down

0 comments on commit 593e847

Please sign in to comment.