@@ -40,7 +40,7 @@ def _check_initiator_config(instance_uuid):
4040 stdout , stderr = processutils .trycmd (cmd , shell = True )
4141 if stderr :
4242 LOG .info ("get iscsid status failed, try to restart iscsid service" )
43- cmd = 'systemctl restart iscsid'
43+ cmd = 'systemctl restart iscsid || service iscsid restart '
4444 processutils .execute (cmd , shell = True )
4545
4646 conf_path = '/etc/iscsi/initiatorname.iscsi'
@@ -82,6 +82,36 @@ def _check_multi_path_config():
8282 processutils .execute (cmd , shell = True )
8383
8484
85+ def rescan_sids_target_name (target_name ):
86+ cmd = "iscsiadm -m session | grep {} | awk '{{print $2}}' | tr -d '[]'" .format (target_name )
87+ LOG .info (cmd )
88+ stdout , stderr = processutils .trycmd (cmd , shell = True )
89+ if stderr :
90+ LOG .info ("iscsiadm -m session grep %s failed, because %s" % (target_name , stderr ))
91+ return
92+ session_ids = stdout .strip ().split ('\n ' )
93+ for session_id in session_ids :
94+ if not session_id :
95+ continue
96+ rescan_cmd = 'iscsiadm -m session -r %s --rescan' % session_id
97+ LOG .info (rescan_cmd )
98+ stdout , stderr = processutils .trycmd (rescan_cmd , shell = True )
99+ if stderr :
100+ LOG .info ("iscsiadm -m session -r %s --rescan fail, because %s" % (session_id , stderr ))
101+
102+
103+ def rescan_for_detach (volume_obj ):
104+ volume_iqn = volume_obj .iscsi_path .replace ('iscsi://' , '' ).split ("/" )[1 ].strip ()
105+ if volume_iqn :
106+ LOG .info ("skip rescan for detach" )
107+ return
108+
109+ stdout , stderr = processutils .trycmd ("timeout 30 iscsiadm -m session -R" , shell = True )
110+ if stderr :
111+ LOG .info ("timeout 30 iscsiadm -m session -R failed, because %s" % stderr )
112+ return
113+
114+
85115class LinuxDriver (base .SystemDriverBase ):
86116 driver_name = 'linux'
87117
@@ -120,7 +150,7 @@ def discovery_target(self, instance_obj):
120150 stdout , stderr = processutils .trycmd (cmd , shell = True )
121151 if not stderr :
122152 LOG .info ("iscsi target:%s has logged" % target_name )
123- return target_name
153+ return
124154
125155 discovery_cmd = 'iscsiadm -m discovery -t sendtargets -p {address}:{port}' .format (
126156 address = instance_obj .gateway_ip ,
@@ -140,10 +170,9 @@ def discovery_target(self, instance_obj):
140170 processutils .execute (target_login_cmd , shell = True )
141171 else :
142172 LOG .info ("discovered targets not contains %s, skip login" % target_name )
143- return target_name
144173 except processutils .ProcessExecutionError :
145174 LOG .info ("no iscsi target found, skip login" )
146- return None
175+ rescan_sids_target_name ( target_name )
147176
148177 def discovery_volume_target (self , instance_obj , volume_obj , volume_access_path_gateway_ips ):
149178 if not volume_obj .iscsi_path :
@@ -153,12 +182,14 @@ def discovery_volume_target(self, instance_obj, volume_obj, volume_access_path_g
153182
154183 def discovery_target_through_access_path_gateway_ips (self , target_name , volume_access_path_gateway_ips ):
155184 for gateway_ip in volume_access_path_gateway_ips :
185+ if not target_name :
186+ discovery_cmd = "timeout 5 iscsiadm -m discovery -t sendtargets -p {address}:{port} | awk '{{print $2}}'" .format (address = gateway_ip , port = 3260 )
187+ stdout , stderr = processutils .trycmd (discovery_cmd , shell = True )
188+ if stderr :
189+ raise Exception ("discovered targets fail, %s" % stderr )
190+ target_name = stdout .strip ()
156191 self .login_target (target_name , gateway_ip )
157-
158- cmd = 'iscsiadm -m session --rescan'
159- stdout , stderr = processutils .trycmd (cmd , shell = True )
160- if stderr :
161- LOG .info ("iscsiadm -m session --rescan fail, because %s" % (stderr ))
192+ rescan_sids_target_name (target_name )
162193
163194 def login_target (self , target_name , address_ip , port = 3260 ):
164195 LOG .info ("start login_target:%s by ip %s" % (target_name , address_ip ))
@@ -196,25 +227,11 @@ def attach_volume(self, instance_obj, volume_obj, volume_access_path_gateway_ips
196227 First check the /etc/iscsi/initiatorname.iscsi whether corrent, if
197228 not, corrent the configuration, then rescan the iscsi session.
198229 """
199- target_name = self .discovery_target (instance_obj )
230+ self .discovery_target (instance_obj )
200231 self .discovery_volume_target (instance_obj , volume_obj , volume_access_path_gateway_ips )
201232 _check_initiator_config (instance_obj .uuid )
202233 _check_multi_path_config ()
203234
204- if not target_name :
205- raise Exception ("instance[%s] target name is not exist, can not rescan session" % instance_obj )
206- cmd = "iscsiadm -m session | grep {} | awk '{{print $2}}' | tr -d '[]'" .format (target_name )
207- LOG .info (cmd )
208- stdout , stderr = processutils .trycmd (cmd , shell = True )
209- if stderr :
210- LOG .info ("iscsiadm -m session grep %s failed, because %s" % (target_name , stderr ))
211- return
212- rescan_cmd = 'iscsiadm -m session -r %s --rescan' % stdout .strip ()
213- LOG .info (rescan_cmd )
214- stdout , stderr = processutils .trycmd (rescan_cmd , shell = True )
215- if stderr :
216- LOG .info ("iscsiadm -m session --rescan fail, because %s" % stderr )
217-
218235 def detach_volume (self , instance_obj , volume_obj , volume_access_path_gateway_ips ):
219236 """ Detach a given iSCSI lun
220237
@@ -238,11 +255,14 @@ def detach_volume(self, instance_obj, volume_obj, volume_access_path_gateway_ips
238255 """
239256 for volume_access_path_gateway_ip in volume_access_path_gateway_ips :
240257 self .detach_volume_for_target_ip (instance_obj , volume_obj , volume_access_path_gateway_ip )
258+ rescan_for_detach (volume_obj )
241259
242260 def detach_volume_for_target_ip (self , instance_obj , volume_obj , target_ip ):
243261 # Get the session id
244262 sid = None
245263 volume_iqn = volume_obj .iscsi_path .replace ('iscsi://' , '' ).split ("/" )[1 ]
264+ if not volume_iqn :
265+ return
246266 if instance_obj .custom_iqn :
247267 iqn = instance_obj .custom_iqn
248268 elif volume_iqn :
0 commit comments