azure_common_linux.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // +build linux
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package azure_dd
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "strconv"
  19. libstrings "strings"
  20. "k8s.io/klog"
  21. "k8s.io/kubernetes/pkg/util/mount"
  22. )
  23. // exclude those used by azure as resource and OS root in /dev/disk/azure
  24. func listAzureDiskPath(io ioHandler) []string {
  25. azureDiskPath := "/dev/disk/azure/"
  26. var azureDiskList []string
  27. if dirs, err := io.ReadDir(azureDiskPath); err == nil {
  28. for _, f := range dirs {
  29. name := f.Name()
  30. diskPath := azureDiskPath + name
  31. if link, linkErr := io.Readlink(diskPath); linkErr == nil {
  32. sd := link[(libstrings.LastIndex(link, "/") + 1):]
  33. azureDiskList = append(azureDiskList, sd)
  34. }
  35. }
  36. }
  37. klog.V(12).Infof("Azure sys disks paths: %v", azureDiskList)
  38. return azureDiskList
  39. }
  40. // getDiskLinkByDevName get disk link by device name from devLinkPath, e.g. /dev/disk/azure/, /dev/disk/by-id/
  41. func getDiskLinkByDevName(io ioHandler, devLinkPath, devName string) (string, error) {
  42. dirs, err := io.ReadDir(devLinkPath)
  43. klog.V(12).Infof("azureDisk - begin to find %s from %s", devName, devLinkPath)
  44. if err == nil {
  45. for _, f := range dirs {
  46. diskPath := devLinkPath + f.Name()
  47. klog.V(12).Infof("azureDisk - begin to Readlink: %s", diskPath)
  48. link, linkErr := io.Readlink(diskPath)
  49. if linkErr != nil {
  50. klog.Warningf("azureDisk - read link (%s) error: %v", diskPath, linkErr)
  51. continue
  52. }
  53. if libstrings.HasSuffix(link, devName) {
  54. return diskPath, nil
  55. }
  56. }
  57. return "", fmt.Errorf("device name(%s) is not found under %s", devName, devLinkPath)
  58. }
  59. return "", fmt.Errorf("read %s error: %v", devLinkPath, err)
  60. }
  61. func scsiHostRescan(io ioHandler, exec mount.Exec) {
  62. scsi_path := "/sys/class/scsi_host/"
  63. if dirs, err := io.ReadDir(scsi_path); err == nil {
  64. for _, f := range dirs {
  65. name := scsi_path + f.Name() + "/scan"
  66. data := []byte("- - -")
  67. if err = io.WriteFile(name, data, 0666); err != nil {
  68. klog.Warningf("failed to rescan scsi host %s", name)
  69. }
  70. }
  71. } else {
  72. klog.Warningf("failed to read %s, err %v", scsi_path, err)
  73. }
  74. }
  75. func findDiskByLun(lun int, io ioHandler, exec mount.Exec) (string, error) {
  76. azureDisks := listAzureDiskPath(io)
  77. return findDiskByLunWithConstraint(lun, io, azureDisks)
  78. }
  79. // finds a device mounted to "current" node
  80. func findDiskByLunWithConstraint(lun int, io ioHandler, azureDisks []string) (string, error) {
  81. var err error
  82. sys_path := "/sys/bus/scsi/devices"
  83. if dirs, err := io.ReadDir(sys_path); err == nil {
  84. for _, f := range dirs {
  85. name := f.Name()
  86. // look for path like /sys/bus/scsi/devices/3:0:0:1
  87. arr := libstrings.Split(name, ":")
  88. if len(arr) < 4 {
  89. continue
  90. }
  91. if len(azureDisks) == 0 {
  92. klog.V(4).Infof("/dev/disk/azure is not populated, now try to parse %v directly", name)
  93. target, err := strconv.Atoi(arr[0])
  94. if err != nil {
  95. klog.Errorf("failed to parse target from %v (%v), err %v", arr[0], name, err)
  96. continue
  97. }
  98. // as observed, targets 0-3 are used by OS disks. Skip them
  99. if target <= 3 {
  100. continue
  101. }
  102. }
  103. // extract LUN from the path.
  104. // LUN is the last index of the array, i.e. 1 in /sys/bus/scsi/devices/3:0:0:1
  105. l, err := strconv.Atoi(arr[3])
  106. if err != nil {
  107. // unknown path format, continue to read the next one
  108. klog.V(4).Infof("azure disk - failed to parse lun from %v (%v), err %v", arr[3], name, err)
  109. continue
  110. }
  111. if lun == l {
  112. // find the matching LUN
  113. // read vendor and model to ensure it is a VHD disk
  114. vendorPath := filepath.Join(sys_path, name, "vendor")
  115. vendorBytes, err := io.ReadFile(vendorPath)
  116. if err != nil {
  117. klog.Errorf("failed to read device vendor, err: %v", err)
  118. continue
  119. }
  120. vendor := libstrings.TrimSpace(string(vendorBytes))
  121. if libstrings.ToUpper(vendor) != "MSFT" {
  122. klog.V(4).Infof("vendor doesn't match VHD, got %s", vendor)
  123. continue
  124. }
  125. modelPath := filepath.Join(sys_path, name, "model")
  126. modelBytes, err := io.ReadFile(modelPath)
  127. if err != nil {
  128. klog.Errorf("failed to read device model, err: %v", err)
  129. continue
  130. }
  131. model := libstrings.TrimSpace(string(modelBytes))
  132. if libstrings.ToUpper(model) != "VIRTUAL DISK" {
  133. klog.V(4).Infof("model doesn't match VIRTUAL DISK, got %s", model)
  134. continue
  135. }
  136. // find a disk, validate name
  137. dir := filepath.Join(sys_path, name, "block")
  138. if dev, err := io.ReadDir(dir); err == nil {
  139. found := false
  140. devName := dev[0].Name()
  141. for _, diskName := range azureDisks {
  142. klog.V(12).Infof("azureDisk - validating disk %q with sys disk %q", devName, diskName)
  143. if devName == diskName {
  144. found = true
  145. break
  146. }
  147. }
  148. if !found {
  149. devLinkPaths := []string{"/dev/disk/azure/scsi1/", "/dev/disk/by-id/"}
  150. for _, devLinkPath := range devLinkPaths {
  151. diskPath, err := getDiskLinkByDevName(io, devLinkPath, devName)
  152. if err == nil {
  153. klog.V(4).Infof("azureDisk - found %s by %s under %s", diskPath, devName, devLinkPath)
  154. return diskPath, nil
  155. }
  156. klog.Warningf("azureDisk - getDiskLinkByDevName by %s under %s failed, error: %v", devName, devLinkPath, err)
  157. }
  158. return "/dev/" + devName, nil
  159. }
  160. }
  161. }
  162. }
  163. }
  164. return "", err
  165. }