[lxc-devel] [PATCH] Store alien config lines

Stéphane Graber stgraber at ubuntu.com
Wed Jun 18 20:52:21 UTC 2014


On Thu, Jun 12, 2014 at 01:46:37PM +0000, Serge Hallyn wrote:
> Any config lines not starting with 'lxc.*' are ignored by lxc.  That
> can be useful for third party tools, however lxc-clone does not copy such
> lines.
> 
> Fix that by tracking such lines in our unexpanded config file and
> printing them out at write_config().  Note two possible shortcomings here:
> 
> 1. we always print out all includes followed by all aliens.  They are
> not kept in order, nor ordered with respect to lxc.* lines.
> 
> 2. we're still not storing comments. these could easily be added to
> the alien lines, but i chose not to in particular since comments are
> usually associated with other lines, so destroying the order would
> destroy their value.  I could be wrong about that, and if I am it's
> a trivial fix.
> 
> Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>

Acked-by: Stéphane Graber <stgraber at ubuntu.com>

> ---
>  src/lxc/conf.c    | 12 ++++++++++++
>  src/lxc/conf.h    |  2 ++
>  src/lxc/confile.c | 43 +++++++++++++++++++++++++++++++++++++++----
>  3 files changed, 53 insertions(+), 4 deletions(-)
> 
> diff --git a/src/lxc/conf.c b/src/lxc/conf.c
> index 521a48d..23ed7f3 100644
> --- a/src/lxc/conf.c
> +++ b/src/lxc/conf.c
> @@ -2690,6 +2690,7 @@ struct lxc_conf *lxc_conf_init(void)
>  	lxc_list_init(&new->keepcaps);
>  	lxc_list_init(&new->id_map);
>  	lxc_list_init(&new->includes);
> +	lxc_list_init(&new->aliens);
>  	for (i=0; i<NUM_LXC_HOOKS; i++)
>  		lxc_list_init(&new->hooks[i]);
>  	lxc_list_init(&new->groups);
> @@ -4365,6 +4366,17 @@ static void lxc_clear_saved_nics(struct lxc_conf *conf)
>  	free(conf->saved_nics);
>  }
>  
> +static inline void lxc_clear_aliens(struct lxc_conf *conf)
> +{
> +	struct lxc_list *it,*next;
> +
> +	lxc_list_for_each_safe(it, &conf->aliens, next) {
> +		lxc_list_del(it);
> +		free(it->elem);
> +		free(it);
> +	}
> +}
> +
>  static inline void lxc_clear_includes(struct lxc_conf *conf)
>  {
>  	struct lxc_list *it,*next;
> diff --git a/src/lxc/conf.h b/src/lxc/conf.h
> index 53590bc..ace1da0 100644
> --- a/src/lxc/conf.h
> +++ b/src/lxc/conf.h
> @@ -342,6 +342,8 @@ struct lxc_conf {
>  
>  	/* list of included files */
>  	struct lxc_list includes;
> +	/* config entries which are not "lxc.*" are aliens */
> +	struct lxc_list aliens;
>  };
>  
>  int run_lxc_hooks(const char *name, char *hook, struct lxc_conf *conf,
> diff --git a/src/lxc/confile.c b/src/lxc/confile.c
> index 2ab3fcb..32f08c7 100644
> --- a/src/lxc/confile.c
> +++ b/src/lxc/confile.c
> @@ -1632,6 +1632,31 @@ static int config_utsname(const char *key, const char *value,
>  	return 0;
>  }
>  
> +static int store_martian_option(char *line, void *data)
> +{
> +	struct lxc_conf *conf = data;
> +	char *str;
> +	struct lxc_list *list;
> +	size_t len = strlen(line);
> +
> +	if (!conf->unexpanded)
> +		return 0;
> +	list = malloc(sizeof(*list));
> +	if (!list)
> +		return -1;
> +	lxc_list_init(list);
> +	str = malloc(len+1);
> +	if (!str) {
> +		free(list);
> +		return -1;
> +	}
> +	strncpy(str, line, len);
> +	len[len] = '\0';
> +	list->elem = str;
> +	lxc_list_add_tail(&conf->aliens, list);
> +	return 0;
> +}
> +
>  static int parse_line(char *buffer, void *data)
>  {
>  	struct lxc_config_t *config;
> @@ -1656,12 +1681,16 @@ static int parse_line(char *buffer, void *data)
>  
>  	line += lxc_char_left_gc(line, strlen(line));
>  
> -	/* martian option - ignoring it, the commented lines beginning by '#'
> -	 * fall in this case
> -	 */
> -	if (strncmp(line, "lxc.", 4))
> +	/* ignore comments */
> +	if (line[0] == '#')
>  		goto out;
>  
> +	/* martian option - save it in the unexpanded config only */
> +	if (strncmp(line, "lxc.", 4)) {
> +		ret = store_martian_option(line, data);
> +		goto out;
> +	}
> +
>  	ret = -1;
>  
>  	dot = strstr(line, "=");
> @@ -2258,10 +2287,16 @@ void write_config(FILE *fout, struct lxc_conf *c)
>  	struct lxc_list *it;
>  	int i;
>  
> +	/* first write any includes */
>  	lxc_list_for_each(it, &c->includes) {
>  		fprintf(fout, "lxc.include = %s\n", (char *)it->elem);
>  	}
>  
> +	/* now write any aliens */
> +	lxc_list_for_each(it, &c->aliens) {
> +		fprintf(fout, "%s\n", (char *)it->elem);
> +	}
> +
>  	if (c->fstab)
>  		fprintf(fout, "lxc.mount = %s\n", c->fstab);
>  	lxc_list_for_each(it, &c->mount_list) {
> -- 
> 1.9.1
> 
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel

-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20140618/9972e321/attachment-0001.sig>


More information about the lxc-devel mailing list