[lxc-devel] [lxc/master] add more error handling + cleanup copy_storage()

brauner on Github lxc-bot at linuxcontainers.org
Tue Apr 5 22:08:10 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160405/d4a86b88/attachment.bin>
-------------- next part --------------
From 763429b6bf45991c4171ee3434013769e5e99063 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at mailbox.org>
Date: Tue, 5 Apr 2016 23:53:49 +0200
Subject: [PATCH] add more error handling + cleanup copy_storage()

Signed-off-by: Christian Brauner <christian.brauner at mailbox.org>
---
 src/lxc/lxccontainer.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index a6003a6..9f12ca2 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -2829,7 +2829,8 @@ bool should_default_to_snapshot(struct lxc_container *c0,
 }
 
 static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
-		const char *newtype, int flags, const char *bdevdata, uint64_t newsize)
+			const char *newtype, int flags, const char *bdevdata,
+			uint64_t newsize)
 {
 	struct bdev *bdev;
 	int need_rdep;
@@ -2837,38 +2838,53 @@ static int copy_storage(struct lxc_container *c0, struct lxc_container *c,
 	if (should_default_to_snapshot(c0, c))
 		flags |= LXC_CLONE_SNAPSHOT;
 
-	bdev = bdev_copy(c0, c->name, c->config_path, newtype, flags,
-			bdevdata, newsize, &need_rdep);
+	bdev = bdev_copy(c0, c->name, c->config_path, newtype, flags, bdevdata,
+			 newsize, &need_rdep);
 	if (!bdev) {
-		ERROR("Error copying storage");
+		ERROR("Error copying storage.");
 		return -1;
 	}
+
+	/* Set new rootfs. */
 	free(c->lxc_conf->rootfs.path);
 	c->lxc_conf->rootfs.path = strdup(bdev->src);
+
+	/* Set new bdev type. */
 	free(c->lxc_conf->rootfs.bdev_type);
 	c->lxc_conf->rootfs.bdev_type = strdup(bdev->type);
 	bdev_put(bdev);
+
 	if (!c->lxc_conf->rootfs.path) {
-		ERROR("Out of memory while setting storage path");
+		ERROR("Out of memory while setting storage path.");
+		return -1;
+	}
+	if (!c->lxc_conf->rootfs.bdev_type) {
+		ERROR("Out of memory while setting rootfs backend.");
 		return -1;
 	}
-	// We will simply append a new lxc.rootfs entry to the unexpanded config
+
+	/* Append a new lxc.rootfs entry to the unexpanded config. */
 	clear_unexp_config_line(c->lxc_conf, "lxc.rootfs", false);
-	if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs", c->lxc_conf->rootfs.path)) {
-		ERROR("Error saving new rootfs to cloned config");
+	if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs",
+					 c->lxc_conf->rootfs.path)) {
+		ERROR("Error saving new rootfs to cloned config.");
 		return -1;
 	}
+
+	/* Append a new lxc.rootfs.backend entry to the unexpanded config. */
 	clear_unexp_config_line(c->lxc_conf, "lxc.rootfs.backend", false);
-	if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs.backend", c->lxc_conf->rootfs.bdev_type)) {
-		ERROR("Error saving new rootfs to cloned config");
+	if (!do_append_unexp_config_line(c->lxc_conf, "lxc.rootfs.backend",
+					 c->lxc_conf->rootfs.bdev_type)) {
+		ERROR("Error saving new rootfs backend to cloned config.");
 		return -1;
 	}
+
 	if (flags & LXC_CLONE_SNAPSHOT)
 		copy_rdepends(c, c0);
 	if (need_rdep) {
 		if (!add_rdepends(c, c0))
 			WARN("Error adding reverse dependency from %s to %s",
-				c->name, c0->name);
+			     c->name, c0->name);
 	}
 
 	mod_all_rdeps(c, true);


More information about the lxc-devel mailing list