@@ -132,33 +132,12 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
132132 }
133133 }
134134
135- final boolean sessionAlreadyActive = isIscsiSessionActive (iqn , host , port );
136- logger .debug ("iSCSI session active check for target {} at {}:{}: {}" , iqn , host , port , sessionAlreadyActive );
137-
138- // Always try to login; treat benign outcomes as success (idempotent).
139- // Ubuntu returns ISCSI_ERR_SESS_EXISTS (15) when already logged in; Oracle often returns 0 with no output.
140- iScsiAdmCmd = new Script (true , "iscsiadm" , 0 , logger );
141- iScsiAdmCmd .add ("-m" , "node" );
142- iScsiAdmCmd .add ("-T" , iqn );
143- iScsiAdmCmd .add ("-p" , host + ":" + port );
144- iScsiAdmCmd .add ("--login" );
145-
146- result = iScsiAdmCmd .execute ();
147- final boolean sessionPreExisted = (iScsiAdmCmd .getExitValue () == ISCSI_SESSION_EXISTS_CODE ) || sessionAlreadyActive ;
148-
149- if (!handleLoginResult (result , sessionPreExisted , volumeUuid )) {
135+ // Login is always attempted (idempotent). Rescan runs only if the session already existed
136+ // before login (Oracle re-login exits 0; Ubuntu may return ISCSI_ERR_SESS_EXISTS).
137+ if (!loginOrRescanExistingSession (iqn , host , port , volumeUuid )) {
150138 return false ;
151139 }
152140
153- // Newly mapped LUNs on an existing session are invisible until rescan, while a session
154- // created by the login above already exposes them.
155- // Prefer the pre-login session check (required on Oracle, where re-login exits 0).
156- // Also treat ISCSI_ERR_SESS_EXISTS as a hint (Ubuntu).
157- if (sessionPreExisted ) {
158- logger .debug ("iSCSI session for target {} at {}:{} pre-existed, performing rescan" , iqn , host , port );
159- rescanIscsiSessions (iqn , host , port );
160- }
161-
162141 // There appears to be a race condition where logging in to the iSCSI volume via iscsiadm
163142 // returns success before the device has been added to the OS.
164143 // What happens is you get logged in and the device shows up, but the device may not
@@ -169,9 +148,7 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
169148 // After a certain number of tries and a certain waiting period in between tries,
170149 // this method could still return (it should not block indefinitely) (the race condition
171150 // isn't solved here, but made highly unlikely to be a problem).
172- waitForDiskToBecomeAvailable (volumeUuid , pool );
173-
174- if (getPhysicalDisk (volumeUuid , pool ).getSize () <= 0 ) {
151+ if (!waitForDiskToBecomeAvailable (volumeUuid , pool )) {
175152 logger .warn ("iSCSI device not ready for target {} at {}:{} after wait" , volumeUuid , host , port );
176153 return false ;
177154 }
@@ -198,16 +175,31 @@ boolean handleNodeCreateResult(String result, String volumeUuid) {
198175 }
199176
200177 /**
201- * Checks the result of an iscsiadm login command.
202- * Returns true if the session already existed (pre-check and/or ISCSI_ERR_SESS_EXISTS)
203- * or login succeeded with no error output, false on failure.
178+ * Checks existing session state, performs login, and rescans only if the session already existed.
179+ *
180+ * Login is always attempted (idempotent). A pre-login session check is required on Oracle,
181+ * where re-login often exits 0; Ubuntu may instead return ISCSI_ERR_SESS_EXISTS (15).
182+ * Session-preexisted must be treated as success first: on Ubuntu, re-login exits 15 with a
183+ * non-null error message that would otherwise be treated as failure.
204184 *
205- * sessionPreExisted must be checked first: on Ubuntu, re-login exits 15 with a non-null
206- * error message that would otherwise be treated as failure.
185+ * @return true if login succeeded (and rescan ran when needed), false on login failure
207186 */
208- boolean handleLoginResult (String result , boolean sessionPreExisted , String volumeUuid ) {
187+ private boolean loginOrRescanExistingSession (String iqn , String host , int port , String volumeUuid ) {
188+ boolean sessionAlreadyActive = isIscsiSessionActive (iqn , host , port );
189+ logger .debug ("iSCSI session active check for target {} at {}:{}: {}" , iqn , host , port , sessionAlreadyActive );
190+
191+ Script iScsiAdmCmd = new Script (true , "iscsiadm" , 0 , logger );
192+ iScsiAdmCmd .add ("-m" , "node" );
193+ iScsiAdmCmd .add ("-T" , iqn );
194+ iScsiAdmCmd .add ("-p" , host + ":" + port );
195+ iScsiAdmCmd .add ("--login" );
196+
197+ String result = iScsiAdmCmd .execute ();
198+ boolean sessionPreExisted = (iScsiAdmCmd .getExitValue () == ISCSI_SESSION_EXISTS_CODE ) || sessionAlreadyActive ;
199+
209200 if (sessionPreExisted ) {
210- logger .debug ("iSCSI session already active for target {}" , volumeUuid );
201+ logger .debug ("iSCSI session for target {} at {}:{} pre-existed, performing rescan" , iqn , host , port );
202+ rescanIscsiSessions (iqn , host , port );
211203 return true ;
212204 }
213205 if (result == null ) {
@@ -229,9 +221,7 @@ private boolean isIscsiSessionActive(String iqn, String host, int port) {
229221 sessionCmd .add ("-m" , "session" );
230222
231223 OutputInterpreter .AllLinesParser parser = new OutputInterpreter .AllLinesParser ();
232-
233224 sessionCmd .executeIgnoreExitValue (parser , ISCSI_ERR_NO_OBJS_FOUND );
234-
235225 int exitValue = sessionCmd .getExitValue ();
236226 if (exitValue != 0 && exitValue != ISCSI_ERR_NO_OBJS_FOUND ) {
237227 logger .warn ("Unable to determine iSCSI session state for target {} at {}:{}: 'iscsiadm -m session' exited with {}" ,
@@ -240,13 +230,11 @@ private boolean isIscsiSessionActive(String iqn, String host, int port) {
240230 }
241231
242232 String sessions = parser .getLines ();
243-
244233 if (StringUtils .isBlank (sessions )) {
245234 return false ;
246235 }
247236
248237 String portal = host + ":" + port ;
249-
250238 for (String line : sessions .split ("\n " )) {
251239 if (line .contains (iqn ) && line .contains (portal )) {
252240 return true ;
@@ -270,19 +258,23 @@ private void rescanIscsiSessions(String iqn, String host, int port) {
270258 }
271259 }
272260
273- private void waitForDiskToBecomeAvailable (String volumeUuid , KVMStoragePool pool ) {
261+ private boolean waitForDiskToBecomeAvailable (String volumeUuid , KVMStoragePool pool ) {
274262 int numberOfTries = 10 ;
275263 int timeBetweenTries = 1000 ;
264+ long deviceSize = 0 ;
276265
277- while (getPhysicalDisk (volumeUuid , pool ).getSize () == 0 && numberOfTries > 0 ) {
266+ while (( deviceSize = getPhysicalDisk (volumeUuid , pool ).getSize () ) == 0 && numberOfTries > 0 ) {
278267 numberOfTries --;
279268
280269 try {
281270 Thread .sleep (timeBetweenTries );
282- } catch (Exception ex ) {
283- // don't do anything
271+ } catch (InterruptedException ex ) {
272+ logger .warn ("Interrupted while waiting for iSCSI device {} to become available" , volumeUuid , ex );
273+ return false ;
284274 }
285275 }
276+
277+ return deviceSize > 0 ;
286278 }
287279
288280 private void waitForDiskToBecomeUnavailable (String host , int port , String iqn , String lun ) {
0 commit comments