azure_common_linux.go 5.9 KB

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