[lxc-devel] [PATCH v2 1/3] lxccontainer.c: split up create_run_template()

TAMUKI Shoichi tamuki at linet.gr.jp
Thu Oct 9 06:52:56 UTC 2014


Split figureout_rootfs() off from create_run_template() to allow
common use of the function.  Also, add the tweak to free the bdev.

Signed-off-by: TAMUKI Shoichi <tamuki at linet.gr.jp>
---
v2:
  - add the tweak to free the bdev.
  - correct misspelling in commit message.

 src/lxc/lxccontainer.c | 108 ++++++++++++++++++++++++++++---------------------
 1 file changed, 62 insertions(+), 46 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 4c3d4d5..07ffc08 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -876,6 +876,9 @@ static char *lxcbasename(char *path)
 	return p;
 }
 
+/* Require that callers free the returned string. */
+static char *figureout_rootfs(struct lxc_conf *conf);
+
 static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
 				char *const argv[])
 {
@@ -891,8 +894,7 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
 	}
 
 	if (pid == 0) { // child
-		char *patharg, *namearg, *rootfsarg, *src;
-		struct bdev *bdev = NULL;
+		char *patharg, *namearg, *rootfsarg, *rootfs;
 		int i;
 		int ret, len, nargs = 0;
 		char **newargv;
@@ -907,49 +909,9 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
 			open("/dev/null", O_RDWR);
 		}
 
-		src = c->lxc_conf->rootfs.path;
-		/*
-		 * for an overlay create, what the user wants is the template to fill
-		 * in what will become the readonly lower layer.  So don't mount for
-		 * the template
-		 */
-		if (strncmp(src, "overlayfs:", 10) == 0)
-			src = overlay_getlower(src+10);
-		if (strncmp(src, "aufs:", 5) == 0)
-			src = overlay_getlower(src+5);
-
-		bdev = bdev_init(c->lxc_conf, src, c->lxc_conf->rootfs.mount, NULL);
-		if (!bdev) {
-			ERROR("Error opening rootfs");
+		rootfs=figureout_rootfs(conf);
+		if (!rootfs)
 			exit(1);
-		}
-
-		if (geteuid() == 0) {
-			if (unshare(CLONE_NEWNS) < 0) {
-				ERROR("error unsharing mounts");
-				exit(1);
-			}
-			if (detect_shared_rootfs()) {
-				if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
-					SYSERROR("Failed to make / rslave to run template");
-					ERROR("Continuing...");
-				}
-			}
-		}
-		if (strcmp(bdev->type, "dir") && strcmp(bdev->type, "btrfs")) {
-			if (geteuid() != 0) {
-				ERROR("non-root users can only create btrfs and directory-backed containers");
-				exit(1);
-			}
-			if (bdev->ops->mount(bdev) < 0) {
-				ERROR("Error mounting rootfs");
-				exit(1);
-			}
-		} else { // TODO come up with a better way here!
-			if (bdev->dest)
-				free(bdev->dest);
-			bdev->dest = strdup(bdev->src);
-		}
 
 		/*
 		 * create our new array, pre-pend the template name and
@@ -981,11 +943,11 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
 			exit(1);
 		newargv[2] = namearg;
 
-		len = strlen("--rootfs=") + 1 + strlen(bdev->dest);
+		len = strlen("--rootfs=") + 1 + strlen(rootfs);
 		rootfsarg = malloc(len);
 		if (!rootfsarg)
 			exit(1);
-		ret = snprintf(rootfsarg, len, "--rootfs=%s", bdev->dest);
+		ret = snprintf(rootfsarg, len, "--rootfs=%s", rootfs);
 		if (ret < 0 || ret >= len)
 			exit(1);
 		newargv[3] = rootfsarg;
@@ -1125,6 +1087,60 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
 	return true;
 }
 
+static char *figureout_rootfs(struct lxc_conf *conf)
+{
+	char *src, *rootfs = NULL;
+	struct bdev *bdev = NULL;
+
+	src = conf->rootfs.path;
+	/*
+	 * for an overlay create, what the user wants is the template to fill
+	 * in what will become the readonly lower layer.  So don't mount for
+	 * the template
+	 */
+	if (strncmp(src, "overlayfs:", 10) == 0)
+		src = overlay_getlower(src+10);
+	if (strncmp(src, "aufs:", 5) == 0)
+		src = overlay_getlower(src+5);
+
+	bdev = bdev_init(conf, src, conf->rootfs.mount, NULL);
+	if (!bdev) {
+		ERROR("Error opening rootfs");
+		return NULL;
+	}
+
+	if (geteuid() == 0) {
+		if (unshare(CLONE_NEWNS) < 0) {
+			ERROR("error unsharing mounts");
+			goto out;
+		}
+		if (detect_shared_rootfs()) {
+			if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
+				SYSERROR("Failed to make / rslave to run template");
+				ERROR("Continuing...");
+			}
+		}
+	}
+	if (strcmp(bdev->type, "dir") && strcmp(bdev->type, "btrfs")) {
+		if (geteuid() != 0) {
+			ERROR("non-root users can only create btrfs and directory-backed containers");
+			goto out;
+		}
+		if (bdev->ops->mount(bdev) < 0) {
+			ERROR("Error mounting rootfs");
+			goto out;
+		}
+	} else { // TODO come up with a better way here!
+		if (bdev->dest)
+			free(bdev->dest);
+		bdev->dest = strdup(bdev->src);
+	}
+	rootfs = strdup(bdev->dest);
+out:
+	bdev_put(bdev);
+	return rootfs;
+}
+
 static bool prepend_lxc_header(char *path, const char *t, char *const argv[])
 {
 	long flen;
-- 
1.9.0


More information about the lxc-devel mailing list