@@ -1158,7 +1158,7 @@ public List<Pair<String, ManagedObjectReference>> detachDisk(String vmdkDatastor
11581158
11591159 // Note: if VM has been taken snapshot, original backing file will be renamed, therefore, when we try to find the matching
11601160 // VirtualDisk, we only perform prefix matching
1161- Pair <VirtualDisk , String > deviceInfo = getDiskDevice (vmdkDatastorePath , false );
1161+ Pair <VirtualDisk , String > deviceInfo = getDiskDevice (vmdkDatastorePath );
11621162 if (deviceInfo == null ) {
11631163 s_logger .warn ("vCenter API trace - detachDisk() done (failed)" );
11641164 throw new Exception ("No such disk device: " + vmdkDatastorePath );
@@ -1964,18 +1964,15 @@ public void ensureScsiDeviceController() throws Exception {
19641964 }
19651965
19661966 // return pair of VirtualDisk and disk device bus name(ide0:0, etc)
1967- public Pair <VirtualDisk , String > getDiskDevice (String vmdkDatastorePath , boolean matchExactly ) throws Exception {
1967+ public Pair <VirtualDisk , String > getDiskDevice (String vmdkDatastorePath ) throws Exception {
19681968 List <VirtualDevice > devices = _context .getVimClient ().getDynamicProperty (_mor , "config.hardware.device" );
1969+ ArrayList <Pair <VirtualDisk , String >> partialMatchingDiskDevices = new ArrayList <Pair <VirtualDisk , String >>();
19691970
19701971 DatastoreFile dsSrcFile = new DatastoreFile (vmdkDatastorePath );
19711972 String srcBaseName = dsSrcFile .getFileBaseName ();
19721973 String trimmedSrcBaseName = VmwareHelper .trimSnapshotDeltaPostfix (srcBaseName );
19731974
1974- if (matchExactly ) {
1975- s_logger .info ("Look for disk device info from volume : " + vmdkDatastorePath + " with base name: " + srcBaseName );
1976- } else {
1977- s_logger .info ("Look for disk device info from volume : " + vmdkDatastorePath + " with trimmed base name: " + trimmedSrcBaseName );
1978- }
1975+ s_logger .info ("Look for disk device info for volume : " + vmdkDatastorePath + " with base name: " + srcBaseName );
19791976
19801977 if (devices != null && devices .size () > 0 ) {
19811978 for (VirtualDevice device : devices ) {
@@ -1990,20 +1987,14 @@ public Pair<VirtualDisk, String> getDiskDevice(String vmdkDatastorePath, boolean
19901987
19911988 DatastoreFile dsBackingFile = new DatastoreFile (diskBackingInfo .getFileName ());
19921989 String backingBaseName = dsBackingFile .getFileBaseName ();
1993- if (matchExactly ) {
1994- if (backingBaseName .equalsIgnoreCase (srcBaseName )) {
1995- String deviceNumbering = getDeviceBusName (devices , device );
1996-
1997- s_logger .info ("Disk backing : " + diskBackingInfo .getFileName () + " matches ==> " + deviceNumbering );
1998- return new Pair <VirtualDisk , String >((VirtualDisk )device , deviceNumbering );
1999- }
2000- } else {
2001- if (backingBaseName .contains (trimmedSrcBaseName )) {
2002- String deviceNumbering = getDeviceBusName (devices , device );
2003-
2004- s_logger .info ("Disk backing : " + diskBackingInfo .getFileName () + " matches ==> " + deviceNumbering );
2005- return new Pair <VirtualDisk , String >((VirtualDisk )device , deviceNumbering );
2006- }
1990+ if (backingBaseName .equalsIgnoreCase (srcBaseName )) {
1991+ String deviceNumbering = getDeviceBusName (devices , device );
1992+ s_logger .info ("Disk backing : " + diskBackingInfo .getFileName () + " matches ==> " + deviceNumbering );
1993+ return new Pair <VirtualDisk , String >((VirtualDisk )device , deviceNumbering );
1994+ }
1995+ if (backingBaseName .contains (trimmedSrcBaseName )) {
1996+ String deviceNumbering = getDeviceBusName (devices , device );
1997+ partialMatchingDiskDevices .add (new Pair <VirtualDisk , String >((VirtualDisk )device , deviceNumbering ));
20071998 }
20081999
20092000 diskBackingInfo = diskBackingInfo .getParent ();
@@ -2013,6 +2004,21 @@ public Pair<VirtualDisk, String> getDiskDevice(String vmdkDatastorePath, boolean
20132004 }
20142005 }
20152006
2007+ // No disk device was found with an exact match for the volume path, hence look for disk device that matches the trimmed name.
2008+ s_logger .info ("No disk device with an exact match found for volume : " + vmdkDatastorePath + ". Look for disk device info against trimmed base name: " + srcBaseName );
2009+ if (partialMatchingDiskDevices != null ) {
2010+ if (partialMatchingDiskDevices .size () == 1 ) {
2011+ VirtualDiskFlatVer2BackingInfo matchingDiskBackingInfo = (VirtualDiskFlatVer2BackingInfo )partialMatchingDiskDevices .get (0 ).first ().getBacking ();
2012+ s_logger .info ("Disk backing : " + matchingDiskBackingInfo .getFileName () + " matches ==> " + partialMatchingDiskDevices .get (0 ).second ());
2013+ return partialMatchingDiskDevices .get (0 );
2014+ } else if (partialMatchingDiskDevices .size () > 1 ) {
2015+ s_logger .warn ("Disk device info lookup for volume: " + vmdkDatastorePath + " failed as multiple disk devices were found to match" +
2016+ " volume's trimmed base name: " + trimmedSrcBaseName );
2017+ return null ;
2018+ }
2019+ }
2020+
2021+ s_logger .warn ("Disk device info lookup for volume: " + vmdkDatastorePath + " failed as no matching disk device found" );
20162022 return null ;
20172023 }
20182024
0 commit comments