detect_storages.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 detect_storages
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/baremetal/utils/raid"
  21. "yunion.io/x/onecloud/pkg/baremetal/utils/raid/drivers"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  23. "yunion.io/x/onecloud/pkg/compute/baremetal"
  24. "yunion.io/x/onecloud/pkg/util/sysutils"
  25. )
  26. func GetRaidDevices(drv raid.IRaidDriver) []*baremetal.BaremetalStorage {
  27. devs := make([]*baremetal.BaremetalStorage, 0)
  28. for _, ada := range drv.GetAdapters() {
  29. devs = append(devs, ada.GetDevices()...)
  30. }
  31. return devs
  32. }
  33. func GetRaidLogicVolumes(drv raid.IRaidDriver) ([]*raid.RaidLogicalVolume, error) {
  34. lvs := []*raid.RaidLogicalVolume{}
  35. for _, adapter := range drv.GetAdapters() {
  36. lv, err := adapter.GetLogicVolumes()
  37. if err != nil {
  38. return nil, err
  39. }
  40. lvs = append(lvs, lv...)
  41. }
  42. return lvs, nil
  43. }
  44. func DetectStorageInfo(term raid.IExecTerm, wait bool) ([]*baremetal.BaremetalStorage, []*baremetal.BaremetalStorage, []*baremetal.BaremetalStorage, error) {
  45. raidDiskInfo := make([]*baremetal.BaremetalStorage, 0)
  46. lvDiskInfo := make([]*raid.RaidLogicalVolume, 0)
  47. raidDrivers := []string{}
  48. for _, drv := range drivers.GetDrivers(term) {
  49. if err := drv.ParsePhyDevs(); err != nil {
  50. log.Warningf("Raid driver %s ParsePhyDevs: %v", drv.GetName(), err)
  51. continue
  52. }
  53. raidDiskInfo = append(raidDiskInfo, GetRaidDevices(drv)...)
  54. if drv.GetName() == baremetal.DISK_DRIVER_MARVELRAID {
  55. lvs, err := GetRaidLogicVolumes(drv)
  56. if err != nil {
  57. log.Errorf("GetRaidLogicVolumes: %v", err)
  58. } else {
  59. lvDiskInfo = append(lvDiskInfo, lvs...)
  60. }
  61. }
  62. raidDrivers = append(raidDrivers, drv.GetName())
  63. }
  64. log.Infof("Get Raid drivers: %v, collecting disks info ...", raidDrivers)
  65. pcieRet, err := term.Run("/lib/mos/lsdisk --pcie")
  66. if err != nil {
  67. return nil, nil, nil, fmt.Errorf("Fail to retrieve PCIE DISK info")
  68. }
  69. pcieDiskInfo := sysutils.ParsePCIEDiskInfo(pcieRet)
  70. maxTries := 6
  71. sleep := 10 * time.Second
  72. nonRaidDiskInfo := []*types.SDiskInfo{}
  73. for tried := 0; len(nonRaidDiskInfo) <= len(lvDiskInfo) && tried < maxTries; tried++ {
  74. ret, err := term.Run("/lib/mos/lsdisk --nonraid")
  75. if err != nil {
  76. return nil, nil, nil, fmt.Errorf("Fail to retrieve Non-Raid SCSI DISK info")
  77. }
  78. nonRaidDiskInfo = sysutils.ParseSCSIDiskInfo(ret)
  79. if wait {
  80. time.Sleep(sleep)
  81. } else {
  82. break
  83. }
  84. }
  85. log.Infof("RaidDiskInfo: %s, NonRaidSCSIDiskInfo: %s, PCIEDiskInfo: %s", jsonutils.Marshal(raidDiskInfo), jsonutils.Marshal(nonRaidDiskInfo), jsonutils.Marshal(pcieDiskInfo))
  86. if len(nonRaidDiskInfo) < len(lvDiskInfo) {
  87. return nil, nil, nil, fmt.Errorf("Fail to retrieve disk info")
  88. }
  89. if len(lvDiskInfo) > 0 {
  90. if len(lvDiskInfo) >= len(nonRaidDiskInfo) {
  91. nonRaidDiskInfo = nil
  92. } else {
  93. nonRaidDiskInfo = nonRaidDiskInfo[:len(nonRaidDiskInfo)-len(lvDiskInfo)]
  94. }
  95. }
  96. return raidDiskInfo, convertDiskInfos(nonRaidDiskInfo), convertDiskInfos(pcieDiskInfo), nil
  97. }
  98. func convertDiskInfos(infos []*types.SDiskInfo) []*baremetal.BaremetalStorage {
  99. ret := make([]*baremetal.BaremetalStorage, 0)
  100. for _, info := range infos {
  101. ret = append(ret, convertDiskInfo(info))
  102. }
  103. return ret
  104. }
  105. func convertDiskInfo(info *types.SDiskInfo) *baremetal.BaremetalStorage {
  106. return &baremetal.BaremetalStorage{
  107. Driver: info.Driver,
  108. Size: int64(info.Size),
  109. Rotate: info.Rotate,
  110. Dev: info.Dev,
  111. Sector: info.Sector,
  112. Block: info.Block,
  113. ModuleInfo: info.ModuleInfo,
  114. Kernel: info.Kernel,
  115. PCIClass: info.PCIClass,
  116. }
  117. }