From 6b310577e9ef8b24eb50df5fe5bcef28e1d260a7 Mon Sep 17 00:00:00 2001
From: John Griffith <john.griffith@solidfire.com>
Date: Wed, 19 Sep 2012 11:18:51 -0600
Subject: [PATCH] Correct IetAdm remove_iscsi_target

Remove logical units should be done before delete the target while
unexport iSCSI volume. Or ietd will show "No such file or directory"
error.

Fix bug 1043107

Change-Id: Iab5b97044c5a30c788fcabce827923ff82a84b91
(cherry picked from commit f14e955735065a998cd78280661f6d33c850d9e2)
---
 cinder/tests/test_iscsi.py |  4 ++--
 cinder/utils.py            | 15 +++++++++++++++
 cinder/volume/iscsi.py     |  5 ++---
 3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/cinder/tests/test_iscsi.py b/cinder/tests/test_iscsi.py
index 32a1309dd..04ebf233e 100644
--- a/cinder/tests/test_iscsi.py
+++ b/cinder/tests/test_iscsi.py
@@ -118,5 +118,5 @@ class IetAdmTestCase(test.TestCase, TargetAdminTestCase):
         'ietadm --op new --tid=%(tid)s --lun=%(lun)s '
                 '--params Path=%(path)s,Type=fileio',
         'ietadm --op show --tid=%(tid)s',
-        'ietadm --op delete --tid=%(tid)s',
-        'ietadm --op delete --tid=%(tid)s --lun=%(lun)s'])
+        'ietadm --op delete --tid=%(tid)s --lun=%(lun)s',
+        'ietadm --op delete --tid=%(tid)s'])
diff --git a/cinder/utils.py b/cinder/utils.py
index 87b59ae64..bfcda366a 100644
--- a/cinder/utils.py
+++ b/cinder/utils.py
@@ -1029,3 +1029,18 @@ class UndoManager(object):
                 LOG.exception(msg, **kwargs)
 
             self._rollback()
+
+
+def ensure_tree(path):
+    """Create a directory (and any ancestor directories required)
+
+    :param path: Directory to create
+    """
+    try:
+        os.makedirs(path)
+    except OSError as exc:
+        if exc.errno == errno.EEXIST:
+            if not os.path.isdir(path):
+                raise
+        else:
+            raise
diff --git a/cinder/volume/iscsi.py b/cinder/volume/iscsi.py
index 2bdf0dc35..ec6e40894 100644
--- a/cinder/volume/iscsi.py
+++ b/cinder/volume/iscsi.py
@@ -109,8 +109,7 @@ class TgtAdm(TargetAdmin):
         # Note(jdg) tid and lun aren't used by TgtAdm but remain for
         # compatibility
 
-        if not os.path.exists(FLAGS.volumes_dir):
-            os.makedirs(FLAGS.volumes_dir)
+        utils.ensure_tree(FLAGS.volumes_dir)
 
         vol_id = name.split(':')[1]
         volume_conf = """
@@ -194,8 +193,8 @@ class IetAdm(TargetAdmin):
 
     def remove_iscsi_target(self, tid, lun, vol_id, **kwargs):
         LOG.info(_('Removing volume: %s') % vol_id)
-        self._delete_target(tid, **kwargs)
         self._delete_logicalunit(tid, lun, **kwargs)
+        self._delete_target(tid, **kwargs)
 
     def _new_target(self, name, tid, **kwargs):
         self._run('--op', 'new',
-- 
2.45.2