lxc/0028-Supporting-workdir-configuration.patch

168 lines
5.2 KiB
Diff
Raw Normal View History

From cbe77bd42528e92d9e3871a36133a2a11f5a3f21 Mon Sep 17 00:00:00 2001
From: wujing <wujing50@huawei.com>
Date: Wed, 15 Apr 2020 00:28:40 -0400
Subject: [PATCH 28/49] Supporting workdir configuration
2019-09-30 11:03:07 -04:00
Signed-off-by: wujing <wujing50@huawei.com>
2019-09-30 11:03:07 -04:00
---
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(-)
2019-09-30 11:03:07 -04:00
diff --git a/src/lxc/attach.c b/src/lxc/attach.c
index 231fa5f..cb480ed 100644
2019-09-30 11:03:07 -04:00
--- a/src/lxc/attach.c
+++ b/src/lxc/attach.c
@@ -708,6 +708,24 @@ static int attach_child_main(struct attach_clone_payload *payload)
2019-09-30 11:03:07 -04:00
TRACE("Dropped capabilities");
}
+#ifdef HAVE_ISULAD
2019-09-30 11:03:07 -04:00
+ /* isulad: set workdir */
+ if (init_ctx->container->lxc_conf->init_cwd) {
2019-09-30 11:03:07 -04:00
+ 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) {
2019-09-30 11:03:07 -04:00
+ SYSERROR("Try to create directory \"%s\" as workdir failed when attach", init_cwd);
+ goto on_error;
+ }
+ if (chdir(init_cwd)) {
2019-09-30 11:03:07 -04:00
+ SYSERROR("Could not change directory to \"%s\" when attach", init_cwd);
+ goto on_error;
+ }
2019-09-30 11:03:07 -04:00
+ }
+#endif
2019-09-30 11:03:07 -04:00
+
/* 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
2019-09-30 11:03:07 -04:00
--- 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);
*/
2019-09-30 11:03:07 -04:00
extern struct lxc_container *lxc_container_new(const char *name, const char *configpath);
+#ifdef HAVE_ISULAD
+/*
2019-09-30 11:03:07 -04:00
+ * Create a new container without loading config.
+ */
+extern struct lxc_container *lxc_container_without_config_new(const char *name, const char *configpath);
+#endif
2019-09-30 11:03:07 -04:00
+
/*
2019-09-30 11:03:07 -04:00
* Returns 1 on success, 0 on failure.
*/
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 9b3ab75..ce2b2bf 100644
2019-09-30 11:03:07 -04:00
--- 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
2019-09-30 11:03:07 -04:00
+#ifdef HAVE_ISULAD
2019-09-30 11:03:07 -04:00
+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
2019-09-30 11:03:07 -04:00
{
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");
2019-09-30 11:03:07 -04:00
goto err;
}
-#endif
2019-09-30 11:03:07 -04:00
+ if (load_config && file_exists(c->configfile)) {
+ if (!lxcapi_load_config(c, NULL)) {
2019-09-30 11:03:07 -04:00
+ 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;
2019-09-30 11:03:07 -04:00
}
+#endif
2019-09-30 11:03:07 -04:00
rc = ongoing_create(c);
switch (rc) {
@@ -5761,6 +5772,19 @@ err:
2019-09-30 11:03:07 -04:00
return NULL;
}
+#ifdef HAVE_ISULAD
2019-09-30 11:03:07 -04:00
+// 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
2019-09-30 11:03:07 -04:00
+
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
2019-09-30 11:03:07 -04:00
--- a/src/lxc/lxccontainer.h
+++ b/src/lxc/lxccontainer.h
@@ -1097,6 +1097,20 @@ struct lxc_console_log {
*/
2019-09-30 11:03:07 -04:00
struct lxc_container *lxc_container_new(const char *name, const char *configpath);
+#ifdef HAVE_ISULAD
+/*!
2019-09-30 11:03:07 -04:00
+ * \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
2019-09-30 11:03:07 -04:00
+
/*!
2019-09-30 11:03:07 -04:00
* \brief Add a reference to the specified container.
*
diff --git a/src/lxc/start.c b/src/lxc/start.c
index bb2e74a..70ce1bd 100644
2019-09-30 11:03:07 -04:00
--- 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);
2019-09-30 11:03:07 -04:00
if (handler->conf->init_cwd) {
+#ifdef HAVE_ISULAD
+ /* try to craete workdir if not exist */
2019-09-30 11:03:07 -04:00
+ 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));
2019-09-30 11:03:07 -04:00
+ goto out_warn_father;
+ }
+#endif
ret = chdir(handler->conf->init_cwd);
if (ret < 0) {
SYSERROR("Could not change directory to \"%s\"",
2019-09-30 11:03:07 -04:00
--
1.8.3.1
2019-09-30 11:03:07 -04:00