1919import java .util .HashMap ;
2020import java .util .List ;
2121import java .util .Map ;
22+ import java .nio .file .Files ;
23+ import java .nio .file .Paths ;
2224
2325import org .apache .cloudstack .utils .qemu .QemuImg ;
2426import org .apache .cloudstack .utils .qemu .QemuImg .PhysicalDiskFormat ;
3537import com .cloud .storage .Storage .StoragePoolType ;
3638import com .cloud .utils .exception .CloudRuntimeException ;
3739import com .cloud .utils .script .OutputInterpreter ;
40+ import com .cloud .utils .script .OutputInterpreter .AllLinesParser ;
3841import com .cloud .utils .script .Script ;
3942
4043public class IscsiAdmStorageAdaptor implements StorageAdaptor {
@@ -96,10 +99,15 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
9699 String result = iScsiAdmCmd .execute ();
97100
98101 if (result != null ) {
99- logger .debug ("Failed to add iSCSI target " + volumeUuid );
100- System .out .println ("Failed to add iSCSI target " + volumeUuid );
102+ // Node record may already exist from a previous run; accept and proceed
103+ if (isNonFatalNodeCreate (result )) {
104+ logger .debug ("iSCSI node already exists for {}@{}:{}, proceeding" , getIqn (volumeUuid ), pool .getSourceHost (), pool .getSourcePort ());
105+ } else {
106+ logger .debug ("Failed to add iSCSI target " + volumeUuid );
107+ System .out .println ("Failed to add iSCSI target " + volumeUuid );
101108
102- return false ;
109+ return false ;
110+ }
103111 } else {
104112 logger .debug ("Successfully added iSCSI target " + volumeUuid );
105113 System .out .println ("Successfully added to iSCSI target " + volumeUuid );
@@ -123,21 +131,28 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
123131 }
124132 }
125133
126- // ex. sudo iscsiadm -m node -T iqn.2012-03.com.test:volume1 -p 192.168.233.10:3260 --login
127- iScsiAdmCmd = new Script (true , "iscsiadm" , 0 , logger );
134+ final String host = pool .getSourceHost ();
135+ final int port = pool .getSourcePort ();
136+ final String iqn = getIqn (volumeUuid );
128137
138+ // Always try to login; treat benign outcomes as success (idempotent)
139+ iScsiAdmCmd = new Script (true , "iscsiadm" , 0 , logger );
129140 iScsiAdmCmd .add ("-m" , "node" );
130- iScsiAdmCmd .add ("-T" , getIqn ( volumeUuid ) );
131- iScsiAdmCmd .add ("-p" , pool . getSourceHost () + ":" + pool . getSourcePort () );
141+ iScsiAdmCmd .add ("-T" , iqn );
142+ iScsiAdmCmd .add ("-p" , host + ":" + port );
132143 iScsiAdmCmd .add ("--login" );
133144
134145 result = iScsiAdmCmd .execute ();
135146
136147 if (result != null ) {
137- logger .debug ("Failed to log in to iSCSI target " + volumeUuid );
138- System .out .println ("Failed to log in to iSCSI target " + volumeUuid );
148+ if (isNonFatalLogin (result )) {
149+ logger .debug ("iSCSI login returned benign message for {}@{}:{}: {}" , iqn , host , port , result );
150+ } else {
151+ logger .debug ("Failed to log in to iSCSI target " + volumeUuid + ": " + result );
152+ System .out .println ("Failed to log in to iSCSI target " + volumeUuid );
139153
140- return false ;
154+ return false ;
155+ }
141156 } else {
142157 logger .debug ("Successfully logged in to iSCSI target " + volumeUuid );
143158 System .out .println ("Successfully logged in to iSCSI target " + volumeUuid );
@@ -158,8 +173,23 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
158173 return true ;
159174 }
160175
176+ // Removed sessionExists() call to avoid noisy sudo/iscsiadm session queries that may fail on some setups
177+
178+ private boolean isNonFatalLogin (String result ) {
179+ if (result == null ) return true ;
180+ String msg = result .toLowerCase ();
181+ // Accept messages where the session already exists
182+ return msg .contains ("already present" ) || msg .contains ("already logged in" ) || msg .contains ("session exists" );
183+ }
184+
185+ private boolean isNonFatalNodeCreate (String result ) {
186+ if (result == null ) return true ;
187+ String msg = result .toLowerCase ();
188+ return msg .contains ("already exists" ) || msg .contains ("database exists" ) || msg .contains ("exists" );
189+ }
190+
161191 private void waitForDiskToBecomeAvailable (String volumeUuid , KVMStoragePool pool ) {
162- int numberOfTries = 10 ;
192+ int numberOfTries = 30 ;
163193 int timeBetweenTries = 1000 ;
164194
165195 while (getPhysicalDisk (volumeUuid , pool ).getSize () == 0 && numberOfTries > 0 ) {
@@ -238,6 +268,15 @@ public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
238268 }
239269
240270 private long getDeviceSize (String deviceByPath ) {
271+ try {
272+ if (!Files .exists (Paths .get (deviceByPath ))) {
273+ logger .debug ("Device by-path does not exist yet: " + deviceByPath );
274+ return 0L ;
275+ }
276+ } catch (Exception ignore ) {
277+ // If FS check fails for any reason, fall back to blockdev call
278+ }
279+
241280 Script iScsiAdmCmd = new Script (true , "blockdev" , 0 , logger );
242281
243282 iScsiAdmCmd .add ("--getsize64" , deviceByPath );
0 commit comments