From cbe77bd42528e92d9e3871a36133a2a11f5a3f21 Mon Sep 17 00:00:00 2001 From: wujing Date: Wed, 15 Apr 2020 00:28:40 -0400 Subject: [PATCH 28/49] Supporting workdir configuration Signed-off-by: wujing --- src/lxc/attach.c | 18 ++++++++++++++++++ src/lxc/lxc.h | 7 +++++++ src/lxc/lxccontainer.c | 26 +++++++++++++++++++++++++- src/lxc/lxccontainer.h | 14 ++++++++++++++ src/lxc/start.c | 10 ++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/lxc/attach.c b/src/lxc/attach.c index 231fa5f..cb480ed 100644 --- a/src/lxc/attach.c +++ b/src/lxc/attach.c @@ -708,6 +708,24 @@ static int attach_child_main(struct attach_clone_payload *payload) TRACE("Dropped capabilities"); } +#ifdef HAVE_ISULAD + /* isulad: set workdir */ + if (init_ctx->container->lxc_conf->init_cwd) { + char *init_cwd; + init_cwd = init_ctx->container->lxc_conf->init_cwd; + /* try to create workdir if not exist */ + struct stat st; + if (stat(init_cwd, &st) < 0 && mkdir_p(init_cwd, 0750) < 0) { + SYSERROR("Try to create directory \"%s\" as workdir failed when attach", init_cwd); + goto on_error; + } + if (chdir(init_cwd)) { + SYSERROR("Could not change directory to \"%s\" when attach", init_cwd); + goto on_error; + } + } +#endif + /* Always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL) * if you want this to be a no-op). */ diff --git a/src/lxc/lxc.h b/src/lxc/lxc.h index 630eff0..99fd422 100644 --- a/src/lxc/lxc.h +++ b/src/lxc/lxc.h @@ -83,6 +83,13 @@ extern lxc_state_t lxc_state(const char *name, const char *lxcpath); */ extern struct lxc_container *lxc_container_new(const char *name, const char *configpath); +#ifdef HAVE_ISULAD +/* + * Create a new container without loading config. + */ +extern struct lxc_container *lxc_container_without_config_new(const char *name, const char *configpath); +#endif + /* * Returns 1 on success, 0 on failure. */ diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 9b3ab75..ce2b2bf 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -5595,7 +5595,11 @@ static bool do_lxcapi_get_container_pids(struct lxc_container *c, pid_t **pids,s WRAP_API_2(bool, lxcapi_get_container_pids, pid_t **,size_t *) #endif +#ifdef HAVE_ISULAD +static struct lxc_container *do_lxc_container_new(const char *name, const char *configpath, bool load_config) +#else struct lxc_container *lxc_container_new(const char *name, const char *configpath) +#endif { struct lxc_container *c; size_t len; @@ -5653,12 +5657,19 @@ struct lxc_container *lxc_container_new(const char *name, const char *configpath fprintf(stderr, "Error allocating oci hooks file pathname\n"); goto err; } -#endif + if (load_config && file_exists(c->configfile)) { + if (!lxcapi_load_config(c, NULL)) { + fprintf(stderr, "Failed to load config for %s\n", name); + goto err; + } + } +#else if (file_exists(c->configfile) && !lxcapi_load_config(c, NULL)) { fprintf(stderr, "Failed to load config for %s\n", name); goto err; } +#endif rc = ongoing_create(c); switch (rc) { @@ -5761,6 +5772,19 @@ err: return NULL; } +#ifdef HAVE_ISULAD +// isulad: new container without load config to save time +struct lxc_container *lxc_container_without_config_new(const char *name, const char *configpath) +{ + return do_lxc_container_new(name, configpath, false); +} + +struct lxc_container *lxc_container_new(const char *name, const char *configpath) +{ + return do_lxc_container_new(name, configpath, true); +} +#endif + int lxc_get_wait_states(const char **states) { int i; diff --git a/src/lxc/lxccontainer.h b/src/lxc/lxccontainer.h index f1621f9..e69be8f 100644 --- a/src/lxc/lxccontainer.h +++ b/src/lxc/lxccontainer.h @@ -1097,6 +1097,20 @@ struct lxc_console_log { */ struct lxc_container *lxc_container_new(const char *name, const char *configpath); +#ifdef HAVE_ISULAD +/*! + * \brief Create a new container without loading config. + * + * \param name Name to use for container. + * \param configpath Full path to configuration file to use. + * + * \return Newly-allocated container, or \c NULL on error. + * + * \note This function can only used for listing container. + */ +struct lxc_container *lxc_container_without_config_new(const char *name, const char *configpath); +#endif + /*! * \brief Add a reference to the specified container. * diff --git a/src/lxc/start.c b/src/lxc/start.c index bb2e74a..70ce1bd 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -1609,6 +1609,16 @@ static int do_start(void *data) close_prot_errno_disarm(devnull_fd); if (handler->conf->init_cwd) { +#ifdef HAVE_ISULAD + /* try to craete workdir if not exist */ + struct stat st; + if (stat(handler->conf->init_cwd, &st) < 0 && mkdir_p(handler->conf->init_cwd, 0755) < 0) { + SYSERROR("Try to create directory \"%s\" as workdir failed", handler->conf->init_cwd); + lxc_write_error_message(handler->conf->errpipe[1], "%s:%d: Failed to create workdir: %s.", + __FILE__, __LINE__, strerror(errno)); + goto out_warn_father; + } +#endif ret = chdir(handler->conf->init_cwd); if (ret < 0) { SYSERROR("Could not change directory to \"%s\"", -- 1.8.3.1