From: Daniel Lezcaon Date: Fri, 13 Nov 2009 10:48:29 +0000 (+0100) Subject: Allows a container to run without previous creation X-Git-Tag: lxc_0_6_4~36 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=488624016575d092d56211347b2bbe8367cd339a;p=thirdparty%2Flxc.git Allows a container to run without previous creation When a container was created, its configuration is used. When a container was not created, the configuration specified in the command line is used, if not configuration file is used, default values are used. That allows to create 'volatile' container, like tmp files. It is useful for example to spawn different container with the same generic configuration file. That let the user to have its own repository of configuration files. And, more important, that fix temporary created container with lxc-execute to be not deleted when the host crash or the command is killed. Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/lxc.h b/src/lxc/lxc.h index 4693f43f0..95e2347ba 100644 --- a/src/lxc/lxc.h +++ b/src/lxc/lxc.h @@ -71,7 +71,7 @@ extern int lxc_destroy(const char *name); * @argv : an array of char * corresponding to the commande line * Returns 0 on sucess, < 0 otherwise */ -extern int lxc_start(const char *name, char *const argv[]); +extern int lxc_start(const char *name, char *const argv[], const char *rcfile); /* * Stop the container previously started with lxc_start, all diff --git a/src/lxc/lxc_execute.c b/src/lxc/lxc_execute.c index 226c6ec8c..746cb4e8b 100644 --- a/src/lxc/lxc_execute.c +++ b/src/lxc/lxc_execute.c @@ -44,6 +44,7 @@ static int my_checker(const struct lxc_arguments* args) lxc_error(args, "missing command to execute !"); return -1; } + return 0; } @@ -79,39 +80,18 @@ Options :\n\ int main(int argc, char *argv[]) { static char **args; - char path[MAXPATHLEN]; - int autodestroy = 0; - int ret = -1; if (lxc_arguments_parse(&my_args, argc, argv)) - goto out; + return -1; if (lxc_log_init(my_args.log_file, my_args.log_priority, my_args.progname, my_args.quiet)) - goto out; - - /* the container is not created, let's create it */ - snprintf(path, MAXPATHLEN, LXCPATH "/%s", my_args.name); - if (access(path, R_OK)) { - if (lxc_create(my_args.name, my_args.rcfile)) - goto out; - autodestroy = 1; - } + return -1; args = lxc_arguments_dup(LXCLIBEXECDIR "/lxc-init", &my_args); if (!args) - goto out; - - ret = lxc_start(my_args.name, args); -out: - if (autodestroy) { - if (lxc_destroy(my_args.name)) { - ERROR("failed to destroy '%s'", my_args.name); - if (!ret) - ret = -1; - } - } + return -1; - return ret; + return lxc_start(my_args.name, args, my_args.rcfile); } diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c index 1d3c81953..ffeb66bae 100644 --- a/src/lxc/lxc_start.c +++ b/src/lxc/lxc_start.c @@ -47,12 +47,14 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) { switch (c) { case 'd': args->daemonize = 1; break; + case 'f': args->rcfile = arg; break; } return 0; } static const struct option my_longopts[] = { {"daemon", no_argument, 0, 'd'}, + {"rcfile", required_argument, 0, 'f'}, LXC_COMMON_OPTIONS }; @@ -65,7 +67,8 @@ lxc-start start COMMAND in specified container NAME\n\ \n\ Options :\n\ -n, --name=NAME NAME for name of the container\n\ - -d, --daemon daemonize the container", + -d, --daemon daemonize the container\n\ + -f, --rcfile=FILE Load configuration file FILE\n", .options = my_longopts, .parser = my_parser, .checker = NULL, @@ -102,7 +105,6 @@ static int restore_tty(struct termios *tios) if (!memcmp(tios, ¤t_tios, sizeof(*tios))) return 0; - oldhandler = signal(SIGTTOU, SIG_IGN); ret = tcsetattr(0, TCSADRAIN, tios); if (ret) @@ -163,7 +165,7 @@ int main(int argc, char *argv[]) save_tty(&tios); - err = lxc_start(my_args.name, args); + err = lxc_start(my_args.name, args, my_args.rcfile); restore_tty(&tios); diff --git a/src/lxc/start.c b/src/lxc/start.c index dd81fccbd..90300719c 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -235,7 +235,7 @@ static int console_init(char *console, size_t size) return 0; } -struct lxc_handler *lxc_init(const char *name) +struct lxc_handler *lxc_init(const char *name, const char *rcfile) { struct lxc_handler *handler; char path[MAXPATHLEN]; @@ -257,11 +257,14 @@ struct lxc_handler *lxc_init(const char *name) goto out_aborting; } - snprintf(path, sizeof(path), LXCPATH "/%s/config", name); - - if (!access(path, F_OK) && lxc_config_read(path, &handler->conf)) { - ERROR("failed to read the configuration file"); + if (rcfile && access(path, F_OK)) { + ERROR("failed to access rcfile"); goto out_aborting; + + if (lxc_config_read(path, &handler->conf)) { + ERROR("failed to read the configuration file"); + goto out_aborting; + } } if (console_init(handler->conf.console, @@ -488,13 +491,13 @@ out_abort: goto out_close; } -int lxc_start(const char *name, char *const argv[]) +int lxc_start(const char *name, char *const argv[], const char *rcfile) { struct lxc_handler *handler; int err = -1; int status; - handler = lxc_init(name); + handler = lxc_init(name, rcfile); if (!handler) { ERROR("failed to initialize the container"); return -1; diff --git a/src/lxc/start.h b/src/lxc/start.h index 8b46b66b4..03d8762ee 100644 --- a/src/lxc/start.h +++ b/src/lxc/start.h @@ -32,7 +32,7 @@ struct lxc_handler { struct lxc_conf conf; }; -extern struct lxc_handler *lxc_init(const char *name); +extern struct lxc_handler *lxc_init(const char *name, const char *rcfile); extern int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]);