[lxc-devel] [PATCH] snapshot: fix overlayfs restore

Serge Hallyn serge.hallyn at ubuntu.com
Tue Mar 4 20:54:04 UTC 2014


And add a testcase to catch regressions.

Without this patch, restoring a snapshot of an overlayfs based
container fails, because we do not pass in LXC_CLONE_SNAPSHOT,
and overlayfs does not support clone without snapshot.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/lxccontainer.c |  7 +++++--
 src/tests/snapshot.c   | 53 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index b0ae44b..877e491 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -3045,7 +3045,7 @@ out_free:
 static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapname, const char *newname)
 {
 	char clonelxcpath[MAXPATHLEN];
-	int ret;
+	int flags = 0,ret;
 	struct lxc_container *snap, *rest;
 	struct bdev *bdev;
 	bool b = false;
@@ -3083,7 +3083,10 @@ static bool lxcapi_snapshot_restore(struct lxc_container *c, const char *snapnam
 		return false;
 	}
 
-	rest = lxcapi_clone(snap, newname, c->config_path, 0, bdev->type, NULL, 0, NULL);
+	if (strcmp(bdev->type, "dir") != 0 && strcmp(bdev->type, "loop") != 0)
+		flags = LXC_CLONE_SNAPSHOT | LXC_CLONE_MAYBE_SNAPSHOT;
+	rest = lxcapi_clone(snap, newname, c->config_path, flags,
+			bdev->type, NULL, 0, NULL);
 	bdev_put(bdev);
 	if (rest && lxcapi_is_defined(rest))
 		b = true;
diff --git a/src/tests/snapshot.c b/src/tests/snapshot.c
index fe06077..09cee1a 100644
--- a/src/tests/snapshot.c
+++ b/src/tests/snapshot.c
@@ -26,6 +26,7 @@
 #include "lxc/lxc.h"
 
 #define MYNAME "snapxxx1"
+#define MYNAME2 "snapxxx3"
 #define RESTNAME "snapxxx2"
 
 static void try_to_remove(void)
@@ -45,6 +46,12 @@ static void try_to_remove(void)
 			c->destroy(c);
 		lxc_container_put(c);
 	}
+	c = lxc_container_new(MYNAME2, NULL);
+	if (c) {
+		if (c->is_defined(c))
+			c->destroy(c);
+		lxc_container_put(c);
+	}
 	c = lxc_container_new(MYNAME, NULL);
 	if (c) {
 		if (c->is_defined(c))
@@ -55,7 +62,7 @@ static void try_to_remove(void)
 
 int main(int argc, char *argv[])
 {
-	struct lxc_container *c;
+	struct lxc_container *c, *c2 = NULL;
 	char *template = "busybox";
 
 	if (argc > 1)
@@ -84,7 +91,7 @@ int main(int argc, char *argv[])
 	c->load_config(c, NULL);
 
 	if (c->snapshot(c, NULL) != 0) {
-		fprintf(stderr, "%s: %d: failed to create snapsot\n", __FILE__, __LINE__);
+		fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__);
 		goto err;
 	}
 
@@ -126,6 +133,48 @@ int main(int argc, char *argv[])
 		goto err;
 	}
 
+
+	c2 = c->clone(c, MYNAME2, NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL);
+	if (!c2) {
+		fprintf(stderr, "%d: %s overlayfs clone failed\n", __LINE__, MYNAME2);
+		goto good;
+	}
+
+	if (c2->snapshot(c2, NULL) != 0) {
+		fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__);
+		goto err;
+	}
+
+	n = c2->snapshot_list(c2, &s);
+	if (n < 1) {
+		fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__);
+		goto err;
+	}
+	if (strcmp(s->name, "snap0") != 0) {
+		fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__);
+		goto err;
+	}
+	for (i=0; i<n; i++) {
+		s[i].free(&s[i]);
+	}
+	free(s);
+
+	if (!c2->snapshot_restore(c2, "snap0", NULL)) {
+		fprintf(stderr, "%s: %d: failed to restore overlayfs snapshot\n", __FILE__, __LINE__);
+		goto err;
+	}
+
+	if (!c2->snapshot_destroy(c2, "snap0")) {
+		fprintf(stderr, "%s: %d: failed to destroy overlayfs snapshot\n", __FILE__, __LINE__);
+		goto err;
+	}
+
+	if (!c2->destroy(c2)) {
+		fprintf(stderr, "%s: %d: failed to destroy container\n", __FILE__, __LINE__);
+		goto err;
+	}
+
+good:
 	if (!c->destroy(c)) {
 		fprintf(stderr, "%s: %d: failed to destroy container\n", __FILE__, __LINE__);
 		goto err;
-- 
1.9.0



More information about the lxc-devel mailing list