[lxc-devel] [PATCH 2/2] lxc-start: allow configuring explicit path to overlayfs workdir

Marcin Bachry hegel666 at gmail.com
Sun May 3 21:46:30 UTC 2015


At the moment LXC creates an "olwork" directory under upper
directory's parent. In many cases it's going to be /var/lib/lxc/name,
but it's not necessarily the case. The target directory may be a
read-only file system or simply a place where user doesn't want to
have sudden scratch directories created. This commit gives the user
more control by adding an optional third path to existing overlay
rootfs syntax:

    overlayfs:/lower:/upper:/work

Signed-off-by: Marcin Bachry <hegel666 at gmail.com>
---
 doc/lxc.container.conf.sgml.in |  8 ++++++--
 src/lxc/bdev.c                 | 43 +++++++++++++++++++++++++++---------------
 2 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/doc/lxc.container.conf.sgml.in b/doc/lxc.container.conf.sgml.in
index aceeb1e..45dcced 100644
--- a/doc/lxc.container.conf.sgml.in
+++ b/doc/lxc.container.conf.sgml.in
@@ -959,8 +959,12 @@ proc proc proc nodev,noexec,nosuid 0 0
           itself should be mounted.  <filename>overlayfs:/lower:/upper</filename>
           specifies that the rootfs should be an overlay with <filename>/upper</filename>
           being mounted read-write over a read-only mount of <filename>/lower</filename>.
-          <filename>aufs:/lower:/upper</filename> does the same using aufs in place
-          of overlayfs. <filename>loop:/file</filename> tells lxc to attach
+          <filename>aufs:/lower:/upper</filename> does the same using
+          aufs in place of overlayfs.  overlayfs also accepts an
+          optional workdir path separated by colon - if it's not
+          provided, LXC uses <filename>olwork</filename> directory
+          created under the same path where upper directory is present.
+          <filename>loop:/file</filename> tells lxc to attach
           <filename>/file</filename> to a loop device and mount the loop device.
             </para>
           </listitem>
diff --git a/src/lxc/bdev.c b/src/lxc/bdev.c
index abc75f8..f9800df 100644
--- a/src/lxc/bdev.c
+++ b/src/lxc/bdev.c
@@ -2183,27 +2183,36 @@ static int overlayfs_mount(struct bdev *bdev)
 
 	//  separately mount it first
 	//  mount -t overlayfs -oupperdir=${upper},lowerdir=${lower} lower dest
-	dup = alloca(strlen(bdev->src)+1);
-	strcpy(dup, bdev->src);
+	dup = strdupa(bdev->src);
 	if (!(lower = strchr(dup, ':')))
 		return -22;
 	if (!(upper = strchr(++lower, ':')))
 		return -22;
 	*upper = '\0';
 	upper++;
+	// work dir is optional
+	work = strchr(upper, ':');
+	if (work) {
+		*work = '\0';
+		work++;
+	}
+
+	if (!work) {
+		// overlayfs.v22 or higher needs workdir option, if it
+		// wasn't provided in config, infer it in the
+		// following way:
+		// if upper is /var/lib/lxc/c2/delta0,
+		// then workdir is /var/lib/lxc/c2/olwork
+		lastslash = strrchr(upper, '/');
+		if (!lastslash)
+			return -22;
+		lastslash++;
+		lastslashidx = lastslash - upper;
 
-	// overlayfs.v22 or higher needs workdir option
-	// if upper is /var/lib/lxc/c2/delta0,
-	// then workdir is /var/lib/lxc/c2/olwork
-	lastslash = strrchr(upper, '/');
-	if (!lastslash)
-		return -22;
-	lastslash++;
-	lastslashidx = lastslash - upper;
-
-	work = alloca(lastslashidx + 7);
-	strncpy(work, upper, lastslashidx+7);
-	strcpy(work+lastslashidx, "olwork");
+		work = alloca(lastslashidx + 7);
+		strncpy(work, upper, lastslashidx+7);
+		strcpy(work+lastslashidx, "olwork");
+	}
 	if ((mkdir(work, 0755) < 0) && errno != EEXIST) {
 		SYSERROR("error: mkdir %s", work);
 		return -22;
@@ -2385,7 +2394,7 @@ static int overlayfs_clonepaths(struct bdev *orig, struct bdev *new, const char
 		// private delta which is originally rsynced from the
 		// original delta
 		char *osrc, *odelta, *nsrc, *ndelta, *work;
-		char *lastslash;
+		char *lastslash, *x;
 		int len, ret, lastslashidx;
 		if (!(osrc = strdup(orig->src)))
 			return -22;
@@ -2396,6 +2405,10 @@ static int overlayfs_clonepaths(struct bdev *orig, struct bdev *new, const char
 		}
 		*odelta = '\0';
 		odelta++;
+		// ignore optional path to workdir, if present
+		x = strchr(odelta, ':');
+		if (x)
+			*x = '\0';
 		ndelta = dir_new_path(odelta, oldname, cname, oldpath, lxcpath);
 		if (!ndelta) {
 			free(osrc);
-- 
2.1.4



More information about the lxc-devel mailing list