perccli.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 megactl
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/baremetal/utils/raid"
  20. "yunion.io/x/onecloud/pkg/compute/baremetal"
  21. )
  22. var (
  23. _ iDriver = new(perccliDriver)
  24. )
  25. type perccliDriver struct{}
  26. func newPerccliDriver() iDriver {
  27. return new(perccliDriver)
  28. }
  29. func (_ *perccliDriver) GetName() string {
  30. return "MegaRAID_Perccli"
  31. }
  32. func (_ *perccliDriver) GetAdapterConstructCmd() string {
  33. return getPerccliCmd("/call", "show", "|", "grep", "-iE", `'^(Controller|Product Name|Serial Number|Bus Number|Device Number|Function Number)\s='`)
  34. }
  35. func (_ *perccliDriver) NewAdaptor(term raid.IExecTerm) iAdaptor {
  36. return newPerccliAdaptor(term)
  37. }
  38. func (_ *perccliDriver) ClearForeignState(term raid.IExecTerm) error {
  39. cmd := getPerccliCmd("/call/fall", "delete")
  40. if _, err := term.Run(cmd); err != nil {
  41. return err
  42. }
  43. return nil
  44. }
  45. func getPerccliCmd(args ...string) string {
  46. bin := "/opt/MegaRAID/perccli/perccli"
  47. return raid.GetCommand(bin, args...)
  48. }
  49. type PerccliAdaptor struct {
  50. *StorcliAdaptor
  51. term raid.IExecTerm
  52. }
  53. func newPerccliAdaptor(term raid.IExecTerm) *PerccliAdaptor {
  54. return &PerccliAdaptor{
  55. StorcliAdaptor: newStorcliAdaptor(),
  56. term: term,
  57. }
  58. }
  59. func (a *PerccliAdaptor) ParseLine(line string) {
  60. a.StorcliAdaptor.parseLine(line)
  61. }
  62. func (a *PerccliAdaptor) IsComplete() bool {
  63. return a.isComplete()
  64. }
  65. func (a *PerccliAdaptor) Key() string {
  66. return a.key()
  67. }
  68. func (a *PerccliAdaptor) GetPhyDevs() ([]*MegaRaidPhyDev, error) {
  69. return a.getMegaPhyDevs(a.getCmd, a.term)
  70. }
  71. func (a *PerccliAdaptor) ClearJBODDisks(devs []*MegaRaidPhyDev) {
  72. storcliClearJBODDisks(a.getAdaptorCmd, a.term, devs)
  73. }
  74. func (a *PerccliAdaptor) GetIndex() int {
  75. return a.Controller
  76. }
  77. func (a *PerccliAdaptor) getCmd(args ...string) string {
  78. return getPerccliCmd(args...)
  79. }
  80. func (a *PerccliAdaptor) getAdaptorCmd(args ...string) (string, error) {
  81. nargs := []string{fmt.Sprintf("/c%d", a.GetIndex())}
  82. nargs = append(nargs, args...)
  83. return a.getCmd(nargs...), nil
  84. }
  85. func (a *PerccliAdaptor) GetLogicVolumes() ([]*raid.RaidLogicalVolume, error) {
  86. cmd := a.getCmd(fmt.Sprintf("/c%d/vall", a.GetIndex()), "show")
  87. ret, err := a.term.Run(cmd)
  88. if err != nil {
  89. return nil, errors.Wrap(err, "Get perccli logical volumes")
  90. }
  91. return parseStorcliLogicalVolumes(a.GetIndex(), ret)
  92. }
  93. func (a *PerccliAdaptor) RemoveLogicVolumes() error {
  94. lvIdx, err := a.GetLogicVolumes()
  95. if err != nil {
  96. return errors.Wrap(err, "GetLogicVolumes")
  97. }
  98. for _, i := range raid.ReverseLogicalArray(lvIdx) {
  99. cmd := a.getCmd(fmt.Sprintf("/c%d/v%d", a.GetIndex(), i.Index), "delete", "force")
  100. if _, err := a.term.Run(cmd); err != nil {
  101. return errors.Wrapf(err, "remove adaptor %d lv %d", a.GetIndex(), i.Index)
  102. }
  103. }
  104. return nil
  105. }
  106. func (a *PerccliAdaptor) BuildRaid0(devs []*baremetal.BaremetalStorage, conf *compute.BaremetalDiskConfig) error {
  107. return cliBuildRaid(devs, conf, func(bs []*baremetal.BaremetalStorage, bdc *compute.BaremetalDiskConfig) error {
  108. return storcliBuildRaid(a.getAdaptorCmd, a.term, devs, conf, 0)
  109. })
  110. }
  111. func (a *PerccliAdaptor) BuildRaid1(devs []*baremetal.BaremetalStorage, conf *compute.BaremetalDiskConfig) error {
  112. return cliBuildRaid(devs, conf, func(bs []*baremetal.BaremetalStorage, bdc *compute.BaremetalDiskConfig) error {
  113. return storcliBuildRaid(a.getAdaptorCmd, a.term, devs, conf, 1)
  114. })
  115. }
  116. func (a *PerccliAdaptor) BuildRaid5(devs []*baremetal.BaremetalStorage, conf *compute.BaremetalDiskConfig) error {
  117. return cliBuildRaid(devs, conf, func(bs []*baremetal.BaremetalStorage, bdc *compute.BaremetalDiskConfig) error {
  118. return storcliBuildRaid(a.getAdaptorCmd, a.term, devs, conf, 5)
  119. })
  120. }
  121. func (a *PerccliAdaptor) BuildRaid10(devs []*baremetal.BaremetalStorage, conf *compute.BaremetalDiskConfig) error {
  122. return cliBuildRaid(devs, conf, func(bs []*baremetal.BaremetalStorage, bdc *compute.BaremetalDiskConfig) error {
  123. return storcliBuildRaid(a.getAdaptorCmd, a.term, devs, conf, 10)
  124. })
  125. }
  126. func (a *PerccliAdaptor) BuildNoneRaid(devs []*baremetal.BaremetalStorage) error {
  127. return cliBuildRaid(devs, nil, func(bs []*baremetal.BaremetalStorage, bdc *compute.BaremetalDiskConfig) error {
  128. return storcliBuildNoRaid(a.getAdaptorCmd, a.term, devs)
  129. })
  130. }
  131. type PerccliPhyDev struct {
  132. *StorcliPhysicalDrive
  133. }
  134. func NewPerccliPhyDev(base *StorcliPhysicalDrive) *PerccliPhyDev {
  135. return &PerccliPhyDev{
  136. StorcliPhysicalDrive: base,
  137. }
  138. }