diff --git a/pkg/volume/azure_dd/attacher.go b/pkg/volume/azure_dd/attacher.go index e23a07290b9..c1658de909a 100644 --- a/pkg/volume/azure_dd/attacher.go +++ b/pkg/volume/azure_dd/attacher.go @@ -152,8 +152,6 @@ func (a *azureDiskAttacher) VolumesAreAttached(specs []*volume.Spec, nodeName ty } func (a *azureDiskAttacher) WaitForAttach(spec *volume.Spec, devicePath string, _ *v1.Pod, timeout time.Duration) (string, error) { - var err error - volumeSource, _, err := getVolumeSource(spec) if err != nil { return "", err @@ -167,13 +165,22 @@ func (a *azureDiskAttacher) WaitForAttach(spec *volume.Spec, devicePath string, nodeName := types.NodeName(a.plugin.host.GetHostName()) diskName := volumeSource.DiskName - glog.V(2).Infof("azureDisk - WaitForAttach: begin to GetDiskLun by diskName(%s), DataDiskURI(%s), nodeName(%s), devicePath(%s)", - diskName, volumeSource.DataDiskURI, nodeName, devicePath) - lun, err := diskController.GetDiskLun(diskName, volumeSource.DataDiskURI, nodeName) - if err != nil { - return "", err + var lun int32 + if runtime.GOOS == "windows" { + glog.V(2).Infof("azureDisk - WaitForAttach: begin to GetDiskLun by diskName(%s), DataDiskURI(%s), nodeName(%s), devicePath(%s)", + diskName, volumeSource.DataDiskURI, nodeName, devicePath) + lun, err = diskController.GetDiskLun(diskName, volumeSource.DataDiskURI, nodeName) + if err != nil { + return "", err + } + glog.V(2).Infof("azureDisk - WaitForAttach: GetDiskLun succeeded, got lun(%v)", lun) + } else { + lun, err = getDiskLUN(devicePath) + if err != nil { + return "", err + } } - glog.V(2).Infof("azureDisk - WaitForAttach: GetDiskLun succeeded, got lun(%v)", lun) + exec := a.plugin.host.GetExec(a.plugin.GetPluginName()) io := &osIOHandler{} diff --git a/pkg/volume/azure_dd/azure_common.go b/pkg/volume/azure_dd/azure_common.go index d7a8e2d0cbe..c2b63577adc 100644 --- a/pkg/volume/azure_dd/azure_common.go +++ b/pkg/volume/azure_dd/azure_common.go @@ -21,6 +21,8 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" + "strconv" libstrings "strings" "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2018-07-01/storage" @@ -59,6 +61,8 @@ var ( string(api.AzureSharedBlobDisk), string(api.AzureDedicatedBlobDisk), string(api.AzureManagedDisk)) + + lunPathRE = regexp.MustCompile(`/dev/disk/azure/scsi(?:.*)/lun(.+)`) ) func getPath(uid types.UID, volName string, host volume.VolumeHost) string { @@ -201,3 +205,25 @@ func strFirstLetterToUpper(str string) string { } return libstrings.ToUpper(string(str[0])) + str[1:] } + +// getDiskLUN : deviceInfo could be a LUN number or a device path, e.g. /dev/disk/azure/scsi1/lun2 +func getDiskLUN(deviceInfo string) (int32, error) { + var diskLUN string + if len(deviceInfo) <= 2 { + diskLUN = deviceInfo + } else { + // extract the LUN num from a device path + matches := lunPathRE.FindStringSubmatch(deviceInfo) + if len(matches) == 2 { + diskLUN = matches[1] + } else { + return -1, fmt.Errorf("cannot parse deviceInfo: %s", deviceInfo) + } + } + + lun, err := strconv.Atoi(diskLUN) + if err != nil { + return -1, err + } + return int32(lun), nil +} diff --git a/pkg/volume/azure_dd/azure_common_test.go b/pkg/volume/azure_dd/azure_common_test.go index ec197ed873c..3574f509e4b 100644 --- a/pkg/volume/azure_dd/azure_common_test.go +++ b/pkg/volume/azure_dd/azure_common_test.go @@ -187,3 +187,58 @@ func TestNormalizeStorageAccountType(t *testing.T) { assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err)) } } + +func TestGetDiskLUN(t *testing.T) { + tests := []struct { + deviceInfo string + expectedLUN int32 + expectError bool + }{ + { + deviceInfo: "0", + expectedLUN: 0, + expectError: false, + }, + { + deviceInfo: "10", + expectedLUN: 10, + expectError: false, + }, + { + deviceInfo: "11d", + expectedLUN: -1, + expectError: true, + }, + { + deviceInfo: "999", + expectedLUN: -1, + expectError: true, + }, + { + deviceInfo: "", + expectedLUN: -1, + expectError: true, + }, + { + deviceInfo: "/dev/disk/azure/scsi1/lun2", + expectedLUN: 2, + expectError: false, + }, + { + deviceInfo: "/dev/disk/azure/scsi0/lun12", + expectedLUN: 12, + expectError: false, + }, + { + deviceInfo: "/dev/disk/by-id/scsi1/lun2", + expectedLUN: -1, + expectError: true, + }, + } + + for _, test := range tests { + result, err := getDiskLUN(test.deviceInfo) + assert.Equal(t, result, test.expectedLUN) + assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err)) + } +}