disk.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package clusterd
  14. import (
  15. "errors"
  16. "fmt"
  17. "os"
  18. "path"
  19. "regexp"
  20. "strconv"
  21. "github.com/coreos/pkg/capnslog"
  22. "github.com/rook/rook/pkg/util/exec"
  23. "github.com/rook/rook/pkg/util/sys"
  24. )
  25. var (
  26. logger = capnslog.NewPackageLogger("github.com/rook/rook", "inventory")
  27. isRBD = regexp.MustCompile("^rbd[0-9]+p?[0-9]{0,}$")
  28. listAllDevices = "all"
  29. // We need to allow dm- devices (Kubernetes mountpoints) because dm- devices name are used as names for meta device
  30. allowDeviceNamePattern = regexp.MustCompile("dm-")
  31. )
  32. func supportedDeviceType(device string) bool {
  33. return device == sys.DiskType ||
  34. device == sys.SSDType ||
  35. device == sys.CryptType ||
  36. device == sys.LVMType ||
  37. device == sys.MultiPath ||
  38. device == sys.PartType ||
  39. device == sys.LinearType ||
  40. (getAllowLoopDevices() && device == sys.LoopType)
  41. }
  42. // GetDeviceEmpty check whether a device is completely empty
  43. func GetDeviceEmpty(device *sys.LocalDisk) bool {
  44. return supportedDeviceType(device.Type) && len(device.Partitions) == 0 && device.Filesystem == ""
  45. }
  46. func ignoreDevice(d string) bool {
  47. return isRBD.MatchString(d)
  48. }
  49. func DiscoverDevicesWithFilter(executor exec.Executor, deviceFilter, metaDevice string) ([]*sys.LocalDisk, error) {
  50. var disks []*sys.LocalDisk
  51. devices, err := sys.ListDevices(executor)
  52. if err != nil {
  53. return nil, err
  54. }
  55. for _, d := range devices {
  56. // Ignore RBD device
  57. if ignoreDevice(d) {
  58. // skip device
  59. logger.Warningf("skipping rbd device %q", d)
  60. continue
  61. }
  62. if deviceFilter != "" && !deviceMatchWithFilter(d, deviceFilter, metaDevice) {
  63. logger.Warningf("device %q skipped due to filter %q", d, deviceFilter)
  64. continue
  65. }
  66. // Populate device information coming from lsblk
  67. disk, err := PopulateDeviceInfo(d, executor)
  68. if err != nil {
  69. logger.Warningf("skipping device %q. %v", d, err)
  70. continue
  71. }
  72. // Populate udev information coming from udev
  73. disk, err = PopulateDeviceUdevInfo(d, executor, disk)
  74. if err != nil {
  75. // go on without udev info
  76. // not ideal for our filesystem check later but we can't really fail either...
  77. logger.Warningf("failed to get udev info for device %q. %v", d, err)
  78. }
  79. // Test if device has child, if so we skip it and only consider the partitions
  80. // which will come in later iterations of the loop
  81. // We only test if the type is 'disk', this is a property reported by lsblk
  82. // and means it's a parent block device
  83. if disk.Type == sys.DiskType {
  84. deviceChild, err := sys.ListDevicesChild(executor, fmt.Sprintf("/dev/%s", d))
  85. if err != nil {
  86. logger.Warningf("failed to detect child devices for device %q, assuming they are none. %v", d, err)
  87. }
  88. // lsblk will output at least 2 lines if they are partitions, one for the parent
  89. // and N for the child
  90. if len(deviceChild) > 1 {
  91. logger.Infof("skipping device %q because it has child, considering the child instead.", d)
  92. continue
  93. }
  94. }
  95. disks = append(disks, disk)
  96. }
  97. logger.Debug("discovered disks are:")
  98. for _, disk := range disks {
  99. logger.Debugf("%+v", disk)
  100. }
  101. return disks, nil
  102. }
  103. func deviceMatchWithFilter(device string, filter, metaDevice string) bool {
  104. if filter == listAllDevices || device == metaDevice {
  105. return true
  106. }
  107. if allowDeviceNamePattern.MatchString(device) {
  108. return true
  109. }
  110. pattern, err := regexp.Compile(filter)
  111. if err != nil {
  112. return false
  113. }
  114. if !pattern.MatchString(device) {
  115. return false
  116. }
  117. return true
  118. }
  119. // DiscoverDevices returns all the details of devices available on the local node
  120. func DiscoverDevices(executor exec.Executor) ([]*sys.LocalDisk, error) {
  121. disks, err := DiscoverDevicesWithFilter(executor, "", "")
  122. if err != nil {
  123. return nil, err
  124. }
  125. return disks, nil
  126. }
  127. // PopulateDeviceInfo returns the information of the specified block device
  128. func PopulateDeviceInfo(d string, executor exec.Executor) (*sys.LocalDisk, error) {
  129. diskProps, err := sys.GetDeviceProperties(d, executor)
  130. if err != nil {
  131. return nil, err
  132. }
  133. diskType, ok := diskProps["TYPE"]
  134. if !ok {
  135. return nil, errors.New("diskType is empty")
  136. }
  137. if !supportedDeviceType(diskType) {
  138. return nil, fmt.Errorf("unsupported diskType %+s", diskType)
  139. }
  140. // get the UUID for disks
  141. var diskUUID string
  142. if diskType == sys.DiskType {
  143. uuid, err := sys.GetDiskUUID(d, executor)
  144. if err != nil {
  145. logger.Warning(err)
  146. } else {
  147. diskUUID = uuid
  148. }
  149. }
  150. disk := &sys.LocalDisk{Name: d, UUID: diskUUID}
  151. if val, ok := diskProps["TYPE"]; ok {
  152. disk.Type = val
  153. }
  154. if val, ok := diskProps["SIZE"]; ok {
  155. if size, err := strconv.ParseUint(val, 10, 64); err == nil {
  156. disk.Size = size
  157. }
  158. }
  159. if val, ok := diskProps["ROTA"]; ok {
  160. if rotates, err := strconv.ParseBool(val); err == nil {
  161. disk.Rotational = rotates
  162. }
  163. }
  164. if val, ok := diskProps["RO"]; ok {
  165. if ro, err := strconv.ParseBool(val); err == nil {
  166. disk.Readonly = ro
  167. }
  168. }
  169. if val, ok := diskProps["PKNAME"]; ok {
  170. if val != "" {
  171. disk.Parent = path.Base(val)
  172. }
  173. }
  174. if val, ok := diskProps["NAME"]; ok {
  175. disk.RealPath = val
  176. }
  177. if val, ok := diskProps["KNAME"]; ok {
  178. disk.KernelName = path.Base(val)
  179. }
  180. if val, ok := diskProps["FSTYPE"]; ok && val != "" {
  181. disk.Filesystem = path.Base(val)
  182. }
  183. if val, ok := diskProps["MOUNTPOINT"]; ok && val != "" {
  184. disk.Mountpoint = path.Base(val)
  185. }
  186. return disk, nil
  187. }
  188. // PopulateDeviceUdevInfo fills the udev info into the block device information
  189. func PopulateDeviceUdevInfo(d string, executor exec.Executor, disk *sys.LocalDisk) (*sys.LocalDisk, error) {
  190. udevInfo, err := sys.GetUdevInfo(d, executor)
  191. if err != nil {
  192. return disk, err
  193. }
  194. // parse udev info output
  195. if val, ok := udevInfo["DEVLINKS"]; ok {
  196. disk.DevLinks = val
  197. }
  198. if val, ok := udevInfo["ID_FS_TYPE"]; ok {
  199. disk.Filesystem = val
  200. }
  201. if val, ok := udevInfo["ID_SERIAL"]; ok {
  202. disk.Serial = val
  203. }
  204. if val, ok := udevInfo["ID_VENDOR"]; ok {
  205. disk.Vendor = val
  206. }
  207. if val, ok := udevInfo["ID_MODEL"]; ok {
  208. disk.Model = val
  209. }
  210. if val, ok := udevInfo["ID_WWN_WITH_EXTENSION"]; ok {
  211. disk.WWNVendorExtension = val
  212. }
  213. if val, ok := udevInfo["ID_WWN"]; ok {
  214. disk.WWN = val
  215. }
  216. return disk, nil
  217. }
  218. func getAllowLoopDevices() bool {
  219. return os.Getenv("CEPH_VOLUME_ALLOW_LOOP_DEVICES") == "true"
  220. }