]> git.proxmox.com Git - mirror_lxc.git/commitdiff
snapshot: fix overlayfs restore
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Tue, 4 Mar 2014 20:54:04 +0000 (14:54 -0600)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 4 Mar 2014 22:40:53 +0000 (17:40 -0500)
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@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lxc/lxccontainer.c
src/tests/snapshot.c

index b7adebeaaa0d4815f021c1e79a0411df89e507cf..c90b564f8b882510a95baed196c40e14b83f2a42 100644 (file)
@@ -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;
index fe06077b660e1122699cc3d44becc40523d990a0..09cee1abe5af7510941d6e7423268c36f4c3309a 100644 (file)
@@ -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;