[lxc-devel] [PATCH] lxc-create: fix that creation fails if lxc.rootfs is set

Serge Hallyn serge.hallyn at ubuntu.com
Fri Oct 25 13:55:44 UTC 2013


Quoting Sheng Yong (shyodx at gmail.com):
> [code] ### lxccontainer.c: 1111
> if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
> 	goto out;
> [/code]
> 
> 	If lxc.rootfs is set and no failure happens before we check
> 	whether the container is already created, the condition will be
> 	always true. In "out" section, lxcapi_destroy() is called to
> 	remove rootfs directory indicated by lxc.rootfs.
> 
> 	If lxc.rootfs is set, we believe that rootfs is ready. Then the
> 	creation should be successful.
> 
> NOTE: the situation where both -t options and lxc.rootfs are set should
> be considered.

Hi,

thanks for the patch.  Please sign off commits in the future (you can do
this using 'git commit -s').  However, nack to this particular version.
How about the below alternative?

Subject: [PATCH 1/1] lxcapi_create: don't delete if container already exists

1. don't save the starting config until we've ensured the container
   does noet yet exist.
2. If the container exists, return false (because creation did not
   happen) but don't delete the container.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
Cc: Sheng Yong <shyodx at gmail.com>
---
 src/lxc/lxccontainer.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 9aea614..6699e9c 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -1090,6 +1090,7 @@ static bool lxcapi_create(struct lxc_container *c, const char *t,
 	pid_t pid;
 	char *tpath = NULL;
 	int partial_fd;
+	bool delete_on_err = false;
 
 	if (!c)
 		return false;
@@ -1102,15 +1103,17 @@ static bool lxcapi_create(struct lxc_container *c, const char *t,
 		}
 	}
 
+	/* container is already created if we have a config and rootfs.path is accessible */
+	if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
+		goto out;
+
+	delete_on_err = true;
+
 	if (!c->save_config(c, NULL)) {
 		ERROR("failed to save starting configuration for %s\n", c->name);
 		goto out;
 	}
 
-	/* container is already created if we have a config and rootfs.path is accessible */
-	if (lxcapi_is_defined(c) && c->lxc_conf && c->lxc_conf->rootfs.path && access(c->lxc_conf->rootfs.path, F_OK) == 0)
-		goto out;
-
 	/* Mark that this container is being created */
 	if ((partial_fd = create_partial(c)) < 0)
 		goto out;
@@ -1183,7 +1186,7 @@ out_unlock:
 out:
 	if (tpath)
 		free(tpath);
-	if (!bret && c)
+	if (delete_on_err && !bret && c)
 		lxcapi_destroy(c);
 	return bret;
 }
-- 
1.8.1.2





More information about the lxc-devel mailing list