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 ;
@@ -96,10 +98,15 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
9698 String result = iScsiAdmCmd .execute ();
9799
98100 if (result != null ) {
99- logger .debug ("Failed to add iSCSI target " + volumeUuid );
100- System .out .println ("Failed to add iSCSI target " + volumeUuid );
101+ // Node record may already exist from a previous run; accept and proceed
102+ if (isNonFatalNodeCreate (result )) {
103+ logger .debug ("iSCSI node already exists for {}@{}:{}, proceeding" , getIqn (volumeUuid ), pool .getSourceHost (), pool .getSourcePort ());
104+ } else {
105+ logger .debug ("Failed to add iSCSI target " + volumeUuid );
106+ System .out .println ("Failed to add iSCSI target " + volumeUuid );
101107
102- return false ;
108+ return false ;
109+ }
103110 } else {
104111 logger .debug ("Successfully added iSCSI target " + volumeUuid );
105112 System .out .println ("Successfully added to iSCSI target " + volumeUuid );
@@ -123,21 +130,28 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
123130 }
124131 }
125132
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 );
133+ final String host = pool .getSourceHost ();
134+ final int port = pool .getSourcePort ();
135+ final String iqn = getIqn (volumeUuid );
128136
137+ // Always try to login; treat benign outcomes as success (idempotent)
138+ iScsiAdmCmd = new Script (true , "iscsiadm" , 0 , logger );
129139 iScsiAdmCmd .add ("-m" , "node" );
130- iScsiAdmCmd .add ("-T" , getIqn ( volumeUuid ) );
131- iScsiAdmCmd .add ("-p" , pool . getSourceHost () + ":" + pool . getSourcePort () );
140+ iScsiAdmCmd .add ("-T" , iqn );
141+ iScsiAdmCmd .add ("-p" , host + ":" + port );
132142 iScsiAdmCmd .add ("--login" );
133143
134144 result = iScsiAdmCmd .execute ();
135145
136146 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 );
147+ if (isNonFatalLogin (result )) {
148+ logger .debug ("iSCSI login returned benign message for {}@{}:{}: {}" , iqn , host , port , result );
149+ } else {
150+ logger .debug ("Failed to log in to iSCSI target " + volumeUuid + ": " + result );
151+ System .out .println ("Failed to log in to iSCSI target " + volumeUuid );
139152
140- return false ;
153+ return false ;
154+ }
141155 } else {
142156 logger .debug ("Successfully logged in to iSCSI target " + volumeUuid );
143157 System .out .println ("Successfully logged in to iSCSI target " + volumeUuid );
@@ -158,8 +172,23 @@ public boolean connectPhysicalDisk(String volumeUuid, KVMStoragePool pool, Map<S
158172 return true ;
159173 }
160174
175+ // Removed sessionExists() call to avoid noisy sudo/iscsiadm session queries that may fail on some setups
176+
177+ private boolean isNonFatalLogin (String result ) {
178+ if (result == null ) return true ;
179+ String msg = result .toLowerCase ();
180+ // Accept messages where the session already exists
181+ return msg .contains ("already present" ) || msg .contains ("already logged in" ) || msg .contains ("session exists" );
182+ }
183+
184+ private boolean isNonFatalNodeCreate (String result ) {
185+ if (result == null ) return true ;
186+ String msg = result .toLowerCase ();
187+ return msg .contains ("already exists" ) || msg .contains ("database exists" ) || msg .contains ("exists" );
188+ }
189+
161190 private void waitForDiskToBecomeAvailable (String volumeUuid , KVMStoragePool pool ) {
162- int numberOfTries = 10 ;
191+ int numberOfTries = 30 ;
163192 int timeBetweenTries = 1000 ;
164193
165194 while (getPhysicalDisk (volumeUuid , pool ).getSize () == 0 && numberOfTries > 0 ) {
@@ -238,6 +267,15 @@ public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
238267 }
239268
240269 private long getDeviceSize (String deviceByPath ) {
270+ try {
271+ if (!Files .exists (Paths .get (deviceByPath ))) {
272+ logger .debug ("Device by-path does not exist yet: " + deviceByPath );
273+ return 0L ;
274+ }
275+ } catch (Exception ignore ) {
276+ // If FS check fails for any reason, fall back to blockdev call
277+ }
278+
241279 Script iScsiAdmCmd = new Script (true , "blockdev" , 0 , logger );
242280
243281 iScsiAdmCmd .add ("--getsize64" , deviceByPath );
0 commit comments