From: Vincent Hou <sbhou@cn.ibm.com>
Date: Tue, 13 Nov 2012 09:15:41 +0000 (+0800)
Subject: Add the persistency to the volume created by iscsi IET.
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=5023fb58d448ad2baad6139c34353f70b27a7ed2;p=openstack-build%2Fcinder-build.git

Add the persistency to the volume created by iscsi IET.

Bug 1070649.

Change-Id: Ia2e095a8a60a5d5ca028272ed2a35c20b4ad45a7
---

diff --git a/cinder/volume/iscsi.py b/cinder/volume/iscsi.py
index 925921c48..670b0e7ed 100644
--- a/cinder/volume/iscsi.py
+++ b/cinder/volume/iscsi.py
@@ -20,6 +20,7 @@ Helper code for the iSCSI volume driver.
 
 """
 import os
+import re
 
 from cinder import exception
 from cinder import flags
@@ -36,6 +37,9 @@ iscsi_helper_opt = [
         cfg.StrOpt('volumes_dir',
                    default='$state_path/volumes',
                    help='Volume configuration file storage directory'),
+        cfg.StrOpt('iet_conf',
+                   default='/etc/iet/ietd.conf',
+                   help='IET configuration file'),
 ]
 
 FLAGS = flags.FLAGS
@@ -211,12 +215,55 @@ class IetAdm(TargetAdmin):
         if chap_auth is not None:
             (type, username, password) = chap_auth.split()
             self._new_auth(tid, type, username, password, **kwargs)
+
+        conf_file = FLAGS.iet_conf
+        if os.path.exists(conf_file):
+            try:
+                volume_conf = """
+                        Target %s
+                            %s
+                            Lun 0 Path=%s,Type=fileio
+                """ % (name, chap_auth, path)
+
+                with utils.temporary_chown(conf_file):
+                    f = open(conf_file, 'a+')
+                    f.write(volume_conf)
+                    f.close()
+            except exception.ProcessExecutionError, e:
+                vol_id = name.split(':')[1]
+                LOG.error(_("Failed to create iscsi target for volume "
+                        "id:%(vol_id)s.") % locals())
+                raise exception.ISCSITargetCreateFailed(volume_id=vol_id)
         return tid
 
     def remove_iscsi_target(self, tid, lun, vol_id, **kwargs):
         LOG.info(_('Removing volume: %s') % vol_id)
         self._delete_logicalunit(tid, lun, **kwargs)
         self._delete_target(tid, **kwargs)
+        vol_uuid = 'volume-%s' % vol_id
+        conf_file = FLAGS.iet_conf
+        if os.path.exists(conf_file):
+            with utils.temporary_chown(conf_file):
+                try:
+                    iet_conf_text = open(conf_file, 'r+')
+                    full_txt = iet_conf_text.readlines()
+                    new_iet_conf_txt = []
+                    count = 0
+                    for line in full_txt:
+                        if count > 0:
+                            count -= 1
+                            continue
+                        elif re.search(vol_uuid, line):
+                            count = 2
+                            continue
+                        else:
+                            new_iet_conf_txt.append(line)
+
+                    iet_conf_text.seek(0)
+                    iet_conf_text.truncate(0)
+                    iet_conf_text.writelines(new_iet_conf_txt)
+                finally:
+                    iet_conf_text.close()
 
     def _new_target(self, name, tid, **kwargs):
         self._run('--op', 'new',