From: Serge Hallyn Date: Tue, 4 Mar 2014 20:54:04 +0000 (-0600) Subject: snapshot: fix overlayfs restore X-Git-Tag: lxc-1.1.0.alpha1~239 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=de269ee8dec1cf2d882de385f8dd6591e97121e2;p=thirdparty%2Flxc.git snapshot: fix overlayfs restore 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 Acked-by: Stéphane Graber --- diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index b7adebeaa..c90b564f8 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -3044,7 +3044,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; @@ -3082,7 +3082,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 fe06077b6..09cee1abe 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; isnapshot_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;