Commit 48862401 by Daniel Lezcaon Committed by Daniel Lezcano

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: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 6dadd7dc
......@@ -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
......
......@@ -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);
}
......@@ -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, &current_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);
......
......@@ -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;
......
......@@ -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[]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment