Define a new lxc.init_cmd config option

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
This commit is contained in:
Stéphane Graber 2014-11-26 14:33:27 -05:00
parent c464fd7e01
commit 67c660d0aa
9 changed files with 56 additions and 10 deletions

View File

@ -80,7 +80,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
</para> </para>
<para> <para>
If no command is specified, <command>lxc-start</command> will If no command is specified, <command>lxc-start</command> will
use the default use the command defined in lxc.init_cmd or if not set, the default
<command>"/sbin/init"</command> command to run a system <command>"/sbin/init"</command> command to run a system
container. container.
</para> </para>

View File

@ -201,6 +201,29 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
</variablelist> </variablelist>
</refsect2> </refsect2>
<refsect2>
<title>Init command</title>
<para>
Sets the command to use as the init system for the containers.
This option is ignored when using lxc-execute.
Defaults to: /sbin/init
</para>
<variablelist>
<varlistentry>
<term>
<option>lxc.init_cmd</option>
</term>
<listitem>
<para>
Absolute path from container rootfs to the binary to use as init.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect2>
<refsect2> <refsect2>
<title>Network</title> <title>Network</title>
<para> <para>

View File

@ -376,7 +376,8 @@ rootfs
command into the container. command into the container.
The pid of the first process is 1. If no command is The pid of the first process is 1. If no command is
specified <command>lxc-start</command> will specified <command>lxc-start</command> will
run <filename>/sbin/init</filename>. run the command defined in lxc.init_cmd or if not set,
<filename>/sbin/init</filename> .
</para> </para>
<para> <para>

View File

@ -4537,6 +4537,8 @@ void lxc_conf_free(struct lxc_conf *conf)
free(conf->fstab); free(conf->fstab);
if (conf->rcfile) if (conf->rcfile)
free(conf->rcfile); free(conf->rcfile);
if (conf->init_cmd)
free(conf->init_cmd);
free(conf->unexpanded_config); free(conf->unexpanded_config);
lxc_clear_config_network(conf); lxc_clear_config_network(conf);
if (conf->lsm_aa_profile) if (conf->lsm_aa_profile)

View File

@ -356,6 +356,9 @@ struct lxc_conf {
/* text representation of the config file */ /* text representation of the config file */
char *unexpanded_config; char *unexpanded_config;
size_t unexpanded_len, unexpanded_alloced; size_t unexpanded_len, unexpanded_alloced;
/* init command */
char *init_cmd;
}; };
int run_lxc_hooks(const char *name, char *hook, struct lxc_conf *conf, int run_lxc_hooks(const char *name, char *hook, struct lxc_conf *conf,

View File

@ -101,6 +101,7 @@ static int config_stopsignal(const char *, const char *, struct lxc_conf *);
static int config_start(const char *, const char *, struct lxc_conf *); static int config_start(const char *, const char *, struct lxc_conf *);
static int config_group(const char *, const char *, struct lxc_conf *); static int config_group(const char *, const char *, struct lxc_conf *);
static int config_environment(const char *, const char *, struct lxc_conf *); static int config_environment(const char *, const char *, struct lxc_conf *);
static int config_init_cmd(const char *, const char *, struct lxc_conf *);
static struct lxc_config_t config[] = { static struct lxc_config_t config[] = {
@ -162,6 +163,7 @@ static struct lxc_config_t config[] = {
{ "lxc.start.order", config_start }, { "lxc.start.order", config_start },
{ "lxc.group", config_group }, { "lxc.group", config_group },
{ "lxc.environment", config_environment }, { "lxc.environment", config_environment },
{ "lxc.init_cmd", config_init_cmd },
}; };
struct signame { struct signame {
@ -965,6 +967,12 @@ static int config_seccomp(const char *key, const char *value,
return config_path_item(&lxc_conf->seccomp, value); return config_path_item(&lxc_conf->seccomp, value);
} }
static int config_init_cmd(const char *key, const char *value,
struct lxc_conf *lxc_conf)
{
return config_path_item(&lxc_conf->init_cmd, value);
}
static int config_hook(const char *key, const char *value, static int config_hook(const char *key, const char *value,
struct lxc_conf *lxc_conf) struct lxc_conf *lxc_conf)
{ {
@ -2327,6 +2335,8 @@ int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv,
v = c->seccomp; v = c->seccomp;
else if (strcmp(key, "lxc.environment") == 0) else if (strcmp(key, "lxc.environment") == 0)
return lxc_get_item_environment(c, retv, inlen); return lxc_get_item_environment(c, retv, inlen);
else if (strcmp(key, "lxc.init_cmd") == 0)
v = c->init_cmd;
else return -1; else return -1;
if (!v) if (!v)

View File

@ -330,10 +330,6 @@ int main(int argc, char *argv[])
struct lxc_container **containers = NULL; struct lxc_container **containers = NULL;
struct lxc_list **c_groups_lists = NULL; struct lxc_list **c_groups_lists = NULL;
struct lxc_list *cmd_group; struct lxc_list *cmd_group;
char *const default_start_args[] = {
"/sbin/init",
NULL,
};
if (lxc_arguments_parse(&my_args, argc, argv)) if (lxc_arguments_parse(&my_args, argc, argv))
return 1; return 1;
@ -470,7 +466,7 @@ int main(int argc, char *argv[])
printf("%s %d\n", c->name, printf("%s %d\n", c->name,
get_config_integer(c, "lxc.start.delay")); get_config_integer(c, "lxc.start.delay"));
else { else {
if (!c->start(c, 0, default_start_args)) if (!c->start(c, 0, NULL))
fprintf(stderr, "Error starting container: %s\n", c->name); fprintf(stderr, "Error starting container: %s\n", c->name);
else else
sleep(get_config_integer(c, "lxc.start.delay")); sleep(get_config_integer(c, "lxc.start.delay"));

View File

@ -336,6 +336,9 @@ int main(int argc, char *argv[])
if (my_args.close_all_fds) if (my_args.close_all_fds)
c->want_close_all_fds(c, true); c->want_close_all_fds(c, true);
if (args == default_args)
err = c->start(c, 0, NULL) ? 0 : 1;
else
err = c->start(c, 0, args) ? 0 : 1; err = c->start(c, 0, args) ? 0 : 1;
if (err) { if (err) {

View File

@ -555,6 +555,7 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv
"/sbin/init", "/sbin/init",
NULL, NULL,
}; };
char *init_cmd[2];
/* container exists */ /* container exists */
if (!c) if (!c)
@ -591,8 +592,15 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv
return ret == 0 ? true : false; return ret == 0 ? true : false;
} }
if (!argv) if (!argv) {
if (conf->init_cmd) {
init_cmd[0] = conf->init_cmd;
init_cmd[1] = NULL;
argv = init_cmd;
}
else
argv = default_args; argv = default_args;
}
/* /*
* say, I'm not sure - what locks do we want here? Any? * say, I'm not sure - what locks do we want here? Any?