/pkg/clusterd/disk.go

https://github.com/rook/rook · Go · 204 lines · 144 code · 27 blank · 33 comment · 58 complexity · c6692a164115bab81e0e7706b98f72fc MD5 · raw file

  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. "path"
  18. "regexp"
  19. "strconv"
  20. "github.com/coreos/pkg/capnslog"
  21. "github.com/rook/rook/pkg/util/exec"
  22. "github.com/rook/rook/pkg/util/sys"
  23. )
  24. var (
  25. logger = capnslog.NewPackageLogger("github.com/rook/rook", "inventory")
  26. isRBD = regexp.MustCompile("^rbd[0-9]+p?[0-9]{0,}$")
  27. )
  28. func supportedDeviceType(device string) bool {
  29. return device == sys.DiskType ||
  30. device == sys.SSDType ||
  31. device == sys.CryptType ||
  32. device == sys.LVMType ||
  33. device == sys.MultiPath ||
  34. device == sys.PartType ||
  35. device == sys.LinearType
  36. }
  37. // GetDeviceEmpty check whether a device is completely empty
  38. func GetDeviceEmpty(device *sys.LocalDisk) bool {
  39. return device.Parent == "" && supportedDeviceType(device.Type) && len(device.Partitions) == 0 && device.Filesystem == ""
  40. }
  41. func ignoreDevice(d string) bool {
  42. return isRBD.MatchString(d)
  43. }
  44. // DiscoverDevices returns all the details of devices available on the local node
  45. func DiscoverDevices(executor exec.Executor) ([]*sys.LocalDisk, error) {
  46. var disks []*sys.LocalDisk
  47. devices, err := sys.ListDevices(executor)
  48. if err != nil {
  49. return nil, err
  50. }
  51. for _, d := range devices {
  52. // Ignore RBD device
  53. if ignoreDevice(d) {
  54. // skip device
  55. logger.Warningf("skipping rbd device %q", d)
  56. continue
  57. }
  58. // Populate device information coming from lsblk
  59. disk, err := PopulateDeviceInfo(d, executor)
  60. if err != nil {
  61. logger.Warningf("skipping device %q. %v", d, err)
  62. continue
  63. }
  64. // Populate udev information coming from udev
  65. disk, err = PopulateDeviceUdevInfo(d, executor, disk)
  66. if err != nil {
  67. // go on without udev info
  68. // not ideal for our filesystem check later but we can't really fail either...
  69. logger.Warningf("failed to get udev info for device %q. %v", d, err)
  70. }
  71. // Test if device has child, if so we skip it and only consider the partitions
  72. // which will come in later iterations of the loop
  73. // We only test if the type is 'disk', this is a property reported by lsblk
  74. // and means it's a parent block device
  75. if disk.Type == sys.DiskType {
  76. deviceChild, err := sys.ListDevicesChild(executor, d)
  77. if err != nil {
  78. logger.Warningf("failed to detect child devices for device %q, assuming they are none. %v", d, err)
  79. }
  80. // lsblk will output at least 2 lines if they are partitions, one for the parent
  81. // and N for the child
  82. if len(deviceChild) > 1 {
  83. logger.Infof("skipping device %q because it has child, considering the child instead.", d)
  84. continue
  85. }
  86. }
  87. disks = append(disks, disk)
  88. }
  89. logger.Debugf("discovered disks are %v", disks)
  90. return disks, nil
  91. }
  92. // PopulateDeviceInfo returns the information of the specified block device
  93. func PopulateDeviceInfo(d string, executor exec.Executor) (*sys.LocalDisk, error) {
  94. diskProps, err := sys.GetDeviceProperties(d, executor)
  95. if err != nil {
  96. return nil, err
  97. }
  98. diskType, ok := diskProps["TYPE"]
  99. if !ok {
  100. return nil, errors.New("diskType is empty")
  101. }
  102. if !supportedDeviceType(diskType) {
  103. return nil, fmt.Errorf("unsupported diskType %+s", diskType)
  104. }
  105. // get the UUID for disks
  106. var diskUUID string
  107. if diskType != sys.PartType {
  108. diskUUID, err = sys.GetDiskUUID(d, executor)
  109. if err != nil {
  110. return nil, err
  111. }
  112. }
  113. disk := &sys.LocalDisk{Name: d, UUID: diskUUID}
  114. if val, ok := diskProps["TYPE"]; ok {
  115. disk.Type = val
  116. }
  117. if val, ok := diskProps["SIZE"]; ok {
  118. if size, err := strconv.ParseUint(val, 10, 64); err == nil {
  119. disk.Size = size
  120. }
  121. }
  122. if val, ok := diskProps["ROTA"]; ok {
  123. if rotates, err := strconv.ParseBool(val); err == nil {
  124. disk.Rotational = rotates
  125. }
  126. }
  127. if val, ok := diskProps["RO"]; ok {
  128. if ro, err := strconv.ParseBool(val); err == nil {
  129. disk.Readonly = ro
  130. }
  131. }
  132. if val, ok := diskProps["PKNAME"]; ok {
  133. if val != "" {
  134. disk.Parent = path.Base(val)
  135. }
  136. }
  137. if val, ok := diskProps["NAME"]; ok {
  138. disk.RealPath = val
  139. }
  140. if val, ok := diskProps["KNAME"]; ok {
  141. disk.KernelName = path.Base(val)
  142. }
  143. return disk, nil
  144. }
  145. // PopulateDeviceUdevInfo fills the udev info into the block device information
  146. func PopulateDeviceUdevInfo(d string, executor exec.Executor, disk *sys.LocalDisk) (*sys.LocalDisk, error) {
  147. udevInfo, err := sys.GetUdevInfo(d, executor)
  148. if err != nil {
  149. return disk, err
  150. }
  151. // parse udev info output
  152. if val, ok := udevInfo["DEVLINKS"]; ok {
  153. disk.DevLinks = val
  154. }
  155. if val, ok := udevInfo["ID_FS_TYPE"]; ok {
  156. disk.Filesystem = val
  157. }
  158. if val, ok := udevInfo["ID_SERIAL"]; ok {
  159. disk.Serial = val
  160. }
  161. if val, ok := udevInfo["ID_VENDOR"]; ok {
  162. disk.Vendor = val
  163. }
  164. if val, ok := udevInfo["ID_MODEL"]; ok {
  165. disk.Model = val
  166. }
  167. if val, ok := udevInfo["ID_WWN_WITH_EXTENSION"]; ok {
  168. disk.WWNVendorExtension = val
  169. }
  170. if val, ok := udevInfo["ID_WWN"]; ok {
  171. disk.WWN = val
  172. }
  173. return disk, nil
  174. }