Skip to content

Commit

Permalink
confile:add lxc.init.cwd
Browse files Browse the repository at this point in the history
Signed-off-by: LiFeng <lifeng68@huawei.com>
  • Loading branch information
lifeng68 committed Dec 5, 2017
1 parent 7ded3c1 commit 3c49155
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions doc/lxc.container.conf.sgml.in
Expand Up @@ -284,6 +284,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
</variablelist>
</refsect2>

<refsect2>
<title>Init working directory</title>
<para>
Sets the absolute path inside the container as the working directory for the containers.
LXC will switch to this directory before executing init.
</para>
<variablelist>
<varlistentry>
<term>
<option>lxc.init.cwd</option>
</term>
<listitem>
<para>
Absolute path inside the container to use as the working directory.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect2>

<refsect2>
<title>Init ID</title>
<para>
Expand Down
1 change: 1 addition & 0 deletions src/lxc/conf.c
Expand Up @@ -3432,6 +3432,7 @@ void lxc_conf_free(struct lxc_conf *conf)
free(conf->rcfile);
free(conf->execute_cmd);
free(conf->init_cmd);
free(conf->init_cwd);
free(conf->unexpanded_config);
free(conf->pty_names);
free(conf->syslog);
Expand Down
4 changes: 4 additions & 0 deletions src/lxc/conf.h
Expand Up @@ -359,6 +359,10 @@ struct lxc_conf {
struct lxc_cgroup cgroup_meta;

char *inherit_ns[LXC_NS_MAX];

/* init working directory */
char* init_cwd;

};

#ifdef HAVE_TLS
Expand Down
22 changes: 22 additions & 0 deletions src/lxc/confile.c
Expand Up @@ -95,6 +95,7 @@ lxc_config_define(hooks);
lxc_config_define(idmaps);
lxc_config_define(includefiles);
lxc_config_define(init_cmd);
lxc_config_define(init_cwd);
lxc_config_define(init_gid);
lxc_config_define(init_uid);
lxc_config_define(log_file);
Expand Down Expand Up @@ -176,6 +177,7 @@ static struct lxc_config_t config[] = {
{ "lxc.init.cmd", false, set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
{ "lxc.init.gid", false, set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
{ "lxc.init.uid", false, set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
{ "lxc.init.cwd", false, set_config_init_cwd, get_config_init_cwd, clr_config_init_cwd, },
{ "lxc.log.file", false, set_config_log_file, get_config_log_file, clr_config_log_file, },
{ "lxc.log.level", false, set_config_log_level, get_config_log_level, clr_config_log_level, },
{ "lxc.log.syslog", false, set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
Expand Down Expand Up @@ -945,6 +947,12 @@ static int set_config_init_cmd(const char *key, const char *value,
return set_config_path_item(&lxc_conf->init_cmd, value);
}

static int set_config_init_cwd(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data)
{
return set_config_path_item(&lxc_conf->init_cwd, value);
}

static int set_config_init_uid(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data)
{
Expand Down Expand Up @@ -3249,6 +3257,12 @@ static int get_config_init_cmd(const char *key, char *retv, int inlen,
return lxc_get_conf_str(retv, inlen, c->init_cmd);
}

static int get_config_init_cwd(const char *key, char *retv, int inlen,
struct lxc_conf *c, void *data)
{
return lxc_get_conf_str(retv, inlen, c->init_cwd);
}

static int get_config_init_uid(const char *key, char *retv, int inlen,
struct lxc_conf *c, void *data)
{
Expand Down Expand Up @@ -3665,6 +3679,14 @@ static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
return 0;
}

static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
void *data)
{
free(c->init_cwd);
c->init_cwd = NULL;
return 0;
}

static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
void *data)
{
Expand Down
5 changes: 5 additions & 0 deletions src/lxc/start.c
Expand Up @@ -979,6 +979,11 @@ static int do_start(void *data)

setsid();

if (handler->conf->init_cwd && chdir(handler->conf->init_cwd)) {
SYSERROR("Could not change directory to \"%s\"", handler->conf->init_cwd);
goto out_warn_father;
}

if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS))
goto out_warn_father;

Expand Down

0 comments on commit 3c49155

Please sign in to comment.