[lxc-devel] current git: lxc-start now requires -f ?

Andrian Nord nightnord at gmail.com
Mon Nov 16 11:42:35 UTC 2009


On Mon, Nov 16, 2009 at 12:17:16PM +0300, Michael Tokarev wrote:
> You mean mount, for 'lxc.mount' keyword?  As I mentioned before, that
> keyword has been extended recently (take a look at current git and at
> recent lxc-devel archives), to not require separate mount file anymore
> and specify lxc.mount.entry in-line in the container config file.

lxc.mount.entry exists for using in scripts, it not replaces lxc.mount
(it can't, due to limitations of mntent.h API). For me it's much
simplier to use fstab syntax as fstab file - at least it will be
highlighted =)

> The new behavour is unexpected.  Right now I have my configs in
> /etc/lxc/$containername - a plain file, not a directory.  That will
> break it.  Here all things are in just testing stage, but I sure it will
> break other configs too.

Traslatilating well-known Russian phrase 'SSZB' =)

Still, your words make sense - if someone (for some strange reason) want
to use only lxc.mount.entry for his mounts, it might be useful to also
check, if LXCPATH/$name is readable file.
 
> Such behavour change should be discussed first IMHO, and thought about.

This is default behaviour, i'm not kidding, really, it was like that
just before forcing -f argument ;)

If you have changed your configs following new behaviour so fast, ok,
but probably I should not count on that ;)

> That's why I didn't sent some similar patch in my first email: writing
> code isn't that difficult, but thinking about how it will work and how
> whole thing works, to have clean and logical interface and structure,
> is much more difficult.

It will work very simple - as before - if you specified any config - it
will use it, if not - it will read 'default' one or fail.

> Besides, your patch will break withOUT config: it does not check if
> /etc/lxc/$name/config exists.

Ah, yes, I've forgoten about configless containers. Really, I don't see
much sense in them, but ok...

This is a patch, that checks all an arbitraty list of 'suffixes' for
LXCPATH/${name}. Currently this is: "" (empty - no suffix), ".conf"
(LXCPATH/${name}.conf file), "/config" - old behaviour

Signed-off-by: Andrian Nord <NightNord at gmail.com>

diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
index ffeb66b..c2e1f53 100644
--- a/src/lxc/lxc_start.c
+++ b/src/lxc/lxc_start.c
@@ -20,9 +20,12 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
+#define _GNU_SOURCE
 #include <stdio.h>
+#undef _GNU_SOURCE
 #include <libgen.h>
 #include <unistd.h>
+#include <stdlib.h>
 #include <string.h>
 #include <termios.h>
 #include <errno.h>
@@ -39,7 +42,9 @@
 
 #include <lxc/lxc.h>
 #include <lxc/log.h>
+
 #include "arguments.h"
+#include "config.h"
 
 lxc_log_define(lxc_start, lxc);
 
@@ -114,17 +119,77 @@ static int restore_tty(struct termios *tios)
 	return ret;
 }
 
+static int check_rcfile(const char *name, const char *suffix, char **rcfile) {
+	struct stat *info;
+	char *path;
+
+	int ret = -1;
+
+	*rcfile = NULL;
+
+	if (!asprintf(&path, LXCPATH "/%s%s", name, suffix)) {
+		SYSERROR("failed to allocate memory");
+		return -1;
+	}
+
+	info = malloc(sizeof(struct stat));
+	if (!info) {
+		SYSERROR("failed to allocate memory");
+
+		free(path);
+		return -1;
+	}
+
+	memset(info, 0, sizeof(struct stat));
+
+	DEBUG("checking if rcfile '%s' is usable", path);
+
+	if (stat(path, info)) {
+		DEBUG("%s: stat failed: %s", path, strerror(errno));
+
+		goto fail;
+	}
+
+	if (!S_ISREG(info->st_mode)) {
+		DEBUG("%s: is not regular file", path);
+
+		goto fail;
+	}
+
+	*rcfile = path;
+	ret = 0;
+
+	goto out;
+
+fail:
+	free(path);
+out:
+	free(info);
+
+	return ret;
+}
+
 int main(int argc, char *argv[])
 {
 	char *const *args;
 	int err = -1;
 	struct termios tios;
+	char *rcfile;
+	int i;
 
 	char *const default_args[] = {
 		"/sbin/init",
 		'\0',
 	};
 
+	char *const rc_suffixes[] = {
+		"",
+		".conf",
+		"/config"
+	};
+
+	const int rc_suffixes_length = sizeof(rc_suffixes)/sizeof(char *);
+
 	if (lxc_arguments_parse(&my_args, argc, argv))
 		return err;
 
@@ -165,7 +230,23 @@ int main(int argc, char *argv[])
 
 	save_tty(&tios);
 
-	err = lxc_start(my_args.name, args, my_args.rcfile);
+	if (my_args.rcfile) {
+		rcfile = my_args.rcfile;
+	} else {
+		for ( i = 0; i < rc_suffixes_length; i++ ) {
+			if (!check_rcfile(my_args.name,
+						rc_suffixes[i], &rcfile))
+				break;
+		}
+
+		if (rcfile)
+			DEBUG("found rcfile '%s'", rcfile);
+	}
+
+	if (!rcfile)
+		INFO("no rcfile specified and non found: running defaults");
+
+	err = lxc_start(my_args.name, args, rcfile);
 
 	restore_tty(&tios);
 




More information about the lxc-devel mailing list