5454 // PS C:\disks> (Get-Disk -Number 1 | Get-Partition | Get-Volume).UniqueId
5555 // \\?\Volume{452e318a-5cde-421e-9831-b9853c521012}\
5656 VolumeRegexp = regexp .MustCompile (`Volume\{[\w-]*\}` )
57+
5758)
5859
5960// New - Construct a new Volume API Implementation.
@@ -268,7 +269,7 @@ func (VolumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {
268269
269270// GetVolumeIDFromTargetPath - gets the volume ID given a mount point, the function is recursive until it find a volume or errors out
270271func (VolumeAPI ) GetVolumeIDFromTargetPath (mount string ) (string , error ) {
271- volumeString , err := getTarget (mount )
272+ volumeString , err := getTarget (mount , 0 )
272273
273274 if err != nil {
274275 return "" , fmt .Errorf ("error getting the volume for the mount %s, internal error %v" , mount , err )
@@ -277,22 +278,28 @@ func (VolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
277278 return volumeString , nil
278279}
279280
280- func getTarget (mount string ) (string , error ) {
281+ func getTarget (mount string , retry int ) (string , error ) {
281282 cmd := `(Get-Item -Path $Env:mountpath).Target`
282283 cmdEnv := fmt .Sprintf ("mountpath=%s" , mount )
283284 out , err := utils .RunPowershellCmd (cmd , cmdEnv )
284- if err != nil || len (out ) == 0 {
285+ if err != nil || len (out ) == 0 {
285286 return "" , fmt .Errorf ("error getting volume from mount. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
286287 }
288+ if retry >= 256 {
289+ return "" , fmt .Errorf ("maximum recursion reached, cmd: %s, output: %s, :retry %d" , cmd , string (out ), retry )
290+ }
291+
287292 volumeString := strings .TrimSpace (string (out ))
293+ klog .V (8 ).Infof ("retry: %d, volumeString: %s" , retry , volumeString )
294+
288295 if ! strings .HasPrefix (volumeString , "Volume" ) {
289- return getTarget (volumeString )
296+ return getTarget (volumeString , retry + 1 )
290297 }
291298
292299 return ensureVolumePrefix (volumeString ), nil
293300}
294301
295- // GetVolumeIDFromTargetPath returns the volume id of a given target path.
302+ // GetClosestVolumeIDFromTargetPath returns the volume id of a given target path.
296303func (VolumeAPI ) GetClosestVolumeIDFromTargetPath (targetPath string ) (string , error ) {
297304 volumeString , err := findClosestVolume (targetPath )
298305
0 commit comments