raid.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. package raid
  15. import (
  16. "strings"
  17. "github.com/pkg/errors"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/tristate"
  20. "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/compute/baremetal"
  22. "yunion.io/x/onecloud/pkg/util/sysutils"
  23. )
  24. var (
  25. Debug bool
  26. )
  27. const (
  28. MODULE_MEGARAID = "megaraid_sas"
  29. MODULE_HPSA = "hpsa"
  30. MODULE_MPT2SAS = "mpt2sas"
  31. MODULE_MPT3SAS = "mpt3sas"
  32. MODULE_AACRAID = "aacraid"
  33. MODULE_SMARTPQI = "smartpqi"
  34. )
  35. const (
  36. MaxUint = ^uint(0)
  37. MaxInt = int(MaxUint >> 1)
  38. UnknownLogicalVolumeIndex = MaxInt
  39. )
  40. type RaidDriverFactory func(term IExecTerm) IRaidDriver
  41. type sRaidDrivers map[string]RaidDriverFactory
  42. var RaidDrivers sRaidDrivers
  43. func init() {
  44. RaidDrivers = make(map[string]RaidDriverFactory)
  45. }
  46. func GetCommand(bin string, args ...string) string {
  47. cmd := []string{bin}
  48. cmd = append(cmd, args...)
  49. return strings.Join(cmd, " ")
  50. }
  51. func RegisterDriver(name string, drv RaidDriverFactory) {
  52. RaidDrivers[name] = drv
  53. }
  54. type RaidBasePhyDev struct {
  55. Adapter int
  56. Size int64
  57. Model string
  58. Rotate tristate.TriState
  59. Status string
  60. Driver string
  61. }
  62. func NewRaidBasePhyDev(driver string) *RaidBasePhyDev {
  63. return &RaidBasePhyDev{
  64. Size: -1,
  65. Rotate: tristate.None,
  66. Driver: driver,
  67. }
  68. }
  69. func (dev *RaidBasePhyDev) IsComplete() bool {
  70. if dev.Model == "" {
  71. return false
  72. }
  73. if dev.Rotate.IsNone() {
  74. return false
  75. }
  76. if dev.Status == "" {
  77. return false
  78. }
  79. return true
  80. }
  81. func (dev *RaidBasePhyDev) ToBaremetalStorage(index int) *baremetal.BaremetalStorage {
  82. return &baremetal.BaremetalStorage{
  83. Adapter: dev.Adapter,
  84. Status: dev.Status,
  85. Size: dev.Size,
  86. Model: dev.Model,
  87. Rotate: dev.Rotate.Bool(),
  88. Driver: dev.Driver,
  89. }
  90. }
  91. func GetModules(term IExecTerm) []string {
  92. ret := []string{}
  93. lines, err := term.Run("/sbin/lsmod")
  94. if err != nil {
  95. log.Errorf("Remote lsmod error: %v", err)
  96. return ret
  97. }
  98. for _, line := range lines {
  99. if len(line) == 0 {
  100. continue
  101. }
  102. mod := line[:strings.Index(line, " ")]
  103. if mod != "Module" {
  104. ret = append(ret, mod)
  105. }
  106. }
  107. return ret
  108. }
  109. func ReverseLogicalArray(input []*RaidLogicalVolume) []*RaidLogicalVolume {
  110. s := make([]*RaidLogicalVolume, len(input))
  111. for i := range input {
  112. s[i] = input[i]
  113. }
  114. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  115. s[i], s[j] = s[j], s[i]
  116. }
  117. return s
  118. }
  119. type TRaidDriverToolType string
  120. const (
  121. RaidDriverToolMegacli64 = TRaidDriverToolType("megacli64")
  122. RaidDriverToolStorecli = TRaidDriverToolType("storecli")
  123. )
  124. type RaidLogicalVolume struct {
  125. Index int
  126. Adapter int
  127. BlockDev string
  128. IsSSD tristate.TriState
  129. Driver TRaidDriverToolType
  130. }
  131. func SGMap(term IExecTerm) ([]compute.SGMapItem, error) {
  132. lines, err := term.Run("/usr/bin/sg_map -x")
  133. if err != nil {
  134. return nil, errors.Wrap(err, "run sg_map")
  135. }
  136. return sysutils.ParseSGMap(lines), nil
  137. }