[lxc-devel] [lxd/master] Remove subvolume in zfs.ImageCreate error flow

sean-jc on Github lxc-bot at linuxcontainers.org
Wed Jul 20 17:28:00 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 384 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160720/369e60c0/attachment.bin>
-------------- next part --------------
From 16015f0ab699178f206e4cc9e2c4a4b6e182d976 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <sean.j.christopherson at intel.com>
Date: Wed, 20 Jul 2016 10:18:48 -0700
Subject: [PATCH] Remove subvolume in zfs.ImageCreate error flow

Fixes #2194

Signed-off-by: Sean Christopherson <sean.j.christopherson at intel.com>
---
 lxd/images.go      |  2 +-
 lxd/storage_zfs.go | 21 +++++++++++++++------
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/lxd/images.go b/lxd/images.go
index 916b86a..95ce0f4 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -106,7 +106,7 @@ func unpack(file string, path string) error {
 	if err != nil {
 		shared.Debugf("Unpacking failed")
 		shared.Debugf(string(output))
-		return err
+		return fmt.Errorf("Unpack failed, %s.", err)
 	}
 
 	return nil
diff --git a/lxd/storage_zfs.go b/lxd/storage_zfs.go
index cd6ae52..e755b1a 100644
--- a/lxd/storage_zfs.go
+++ b/lxd/storage_zfs.go
@@ -625,22 +625,31 @@ func (s *storageZfs) ImageCreate(fingerprint string) error {
 		return err
 	}
 
+	cleanup := func(err error) error {
+		if zerr := s.zfsDestroy(fs); zerr != nil {
+			err = fmt.Errorf("%s  During cleanup: %s", err, zerr)
+		}
+		if shared.PathExists(subvol) {
+			if oserr := os.Remove(subvol); oserr != nil {
+				err = fmt.Errorf("%s  During cleanup: Failed to remove sub-volume %s, %s", err, subvol, oserr)
+			}
+		}
+		return err
+	}
+
 	err = unpackImage(imagePath, subvol)
 	if err != nil {
-		s.zfsDestroy(fs)
-		return err
+		return cleanup(err)
 	}
 
 	err = s.zfsSet(fs, "readonly", "on")
 	if err != nil {
-		s.zfsDestroy(fs)
-		return err
+		return cleanup(err)
 	}
 
 	err = s.zfsSnapshotCreate(fs, "readonly")
 	if err != nil {
-		s.zfsDestroy(fs)
-		return err
+		return cleanup(err)
 	}
 
 	return nil


More information about the lxc-devel mailing list