@@ -318,8 +318,47 @@ private String getComponent(String path, int index) {
318318 return tmp [index ].trim ();
319319 }
320320
321+ /**
322+ * Check if there are other LUNs on the same iSCSI target (IQN) that are still
323+ * visible as block devices. This is needed because ONTAP uses a single IQN per
324+ * SVM — logging out of the target would kill ALL LUNs, not just the one being
325+ * disconnected.
326+ *
327+ * Checks /dev/disk/by-path/ for symlinks matching the same host:port + IQN but
328+ * with a different LUN number.
329+ */
330+ private boolean hasOtherActiveLuns (String host , int port , String iqn , String lun ) {
331+ String prefix = "ip-" + host + ":" + port + "-iscsi-" + iqn + "-lun-" ;
332+ java .io .File byPathDir = new java .io .File ("/dev/disk/by-path" );
333+ if (!byPathDir .exists () || !byPathDir .isDirectory ()) {
334+ return false ;
335+ }
336+ java .io .File [] entries = byPathDir .listFiles ();
337+ if (entries == null ) {
338+ return false ;
339+ }
340+ for (java .io .File entry : entries ) {
341+ String name = entry .getName ();
342+ if (name .startsWith (prefix ) && !name .equals (prefix + lun )) {
343+ logger .debug ("Found other active LUN on same target: " + name );
344+ return true ;
345+ }
346+ }
347+ return false ;
348+ }
349+
321350 private boolean disconnectPhysicalDisk (String host , int port , String iqn , String lun ) {
322- // use iscsiadm to log out of the iSCSI target and un-discover it
351+ // Check if other LUNs on the same IQN target are still in use.
352+ // ONTAP (and similar) uses a single IQN per SVM with multiple LUNs.
353+ // Doing iscsiadm --logout tears down the ENTIRE target session,
354+ // which would destroy access to ALL LUNs — not just the one being disconnected.
355+ if (hasOtherActiveLuns (host , port , iqn , lun )) {
356+ logger .info ("Skipping iSCSI logout for /" + iqn + "/" + lun +
357+ " — other LUNs on the same target are still active" );
358+ return true ;
359+ }
360+
361+ // No other LUNs active on this target — safe to logout and delete the node record.
323362
324363 // ex. sudo iscsiadm -m node -T iqn.2012-03.com.test:volume1 -p 192.168.233.10:3260 --logout
325364 Script iScsiAdmCmd = new Script (true , "iscsiadm" , 0 , logger );
0 commit comments