[lxc-devel] [PATCH] use btrfs snapshot feature to restore snapshots (fixes #131)

S.Çağlar Onur caglar at 10ur.org
Fri Feb 28 16:57:47 UTC 2014


Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
---
 src/lxc/bdev.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/lxc/bdev.c b/src/lxc/bdev.c
index 627c09a..a7786c1 100644
--- a/src/lxc/bdev.c
+++ b/src/lxc/bdev.c
@@ -2587,9 +2587,7 @@ struct bdev *bdev_copy(struct lxc_container *c0, const char *cname,
 	if (new->ops->clone_paths(orig, new, oldname, cname, oldpath, lxcpath,
 				snap, newsize, c0->lxc_conf) < 0) {
 		ERROR("failed getting pathnames for cloned storage: %s", src);
-		bdev_put(orig);
-		bdev_put(new);
-		return NULL;
+		goto err;
 	}
 
 	if (am_unpriv() && chown_mapped_root(new->src, c0->lxc_conf) < 0)
@@ -2598,12 +2596,31 @@ struct bdev *bdev_copy(struct lxc_container *c0, const char *cname,
 	if (snap)
 		return new;
 
+	/*
+	 * https://github.com/lxc/lxc/issues/131
+	 * Use btrfs snapshot feature instead of rsync to restore if both orig and new are btrfs
+	 */
+	if (bdevtype && strcmp(orig->type, "btrfs") == 0 && strcmp(new->type, "btrfs") == 0) {
+		if (btrfs_destroy(new) < 0) {
+			ERROR("Error destroying %s subvolume", new->dest);
+			goto err;
+		}
+		if (mkdir_p(new->dest, 0755) < 0) {
+			ERROR("Error creating %s directory", new->dest);
+			goto err;
+		}
+		if (btrfs_snapshot(orig->dest, new->dest) < 0) {
+			ERROR("Error restoring %s to %s", orig->dest, new->dest);
+			goto err;
+		}
+		bdev_put(orig);
+		return new;
+	}
+
 	pid = fork();
 	if (pid < 0) {
 		SYSERROR("fork");
-		bdev_put(orig);
-		bdev_put(new);
-		return NULL;
+		goto err;
 	}
 
 	if (pid > 0) {
@@ -2624,6 +2641,11 @@ struct bdev *bdev_copy(struct lxc_container *c0, const char *cname,
 		ret = rsync_rootfs(&data);
 
 	exit(ret == 0 ? 0 : 1);
+
+err:
+	bdev_put(orig);
+	bdev_put(new);
+	return NULL;
 }
 
 static struct bdev * do_bdev_create(const char *dest, const char *type,
-- 
1.8.3.2



More information about the lxc-devel mailing list