adaptec.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 adaptec
  15. import (
  16. "fmt"
  17. "regexp"
  18. "strconv"
  19. "strings"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/tristate"
  23. "yunion.io/x/pkg/util/stringutils"
  24. api "yunion.io/x/onecloud/pkg/apis/compute"
  25. "yunion.io/x/onecloud/pkg/baremetal/utils/raid"
  26. "yunion.io/x/onecloud/pkg/compute/baremetal"
  27. "yunion.io/x/onecloud/pkg/util/regutils2"
  28. )
  29. func init() {
  30. raid.RegisterDriver(api.DISK_DRIVER_ADAPTECRAID, NewAdaptecRaid)
  31. }
  32. const (
  33. ControllerModeRaidExposeRaw = "RAID (Expose RAW)"
  34. ControllerModeRaidHideRaw = "RAID (Hide RAW)"
  35. ControllerModeMixed = "Mixed"
  36. )
  37. type AdaptecRaid struct {
  38. term raid.IExecTerm
  39. adapters []*AdaptecRaidAdaptor
  40. }
  41. func NewAdaptecRaid(term raid.IExecTerm) raid.IRaidDriver {
  42. return &AdaptecRaid{
  43. term: term,
  44. adapters: make([]*AdaptecRaidAdaptor, 0),
  45. }
  46. }
  47. func GetCommand(args ...string) string {
  48. bin := "/opt/adaptec/arcconf"
  49. return raid.GetCommand(bin, args...)
  50. }
  51. func (r *AdaptecRaid) GetName() string {
  52. return baremetal.DISK_DRIVER_ADAPTECRAID
  53. }
  54. func (r *AdaptecRaid) ParsePhyDevs() error {
  55. cmd := GetCommand("LIST")
  56. ret, err := r.term.Run(cmd)
  57. if err != nil {
  58. return errors.Wrap(err, "list controllers")
  59. }
  60. if err := r.parsePhyDevs(ret); err != nil {
  61. return errors.Wrap(err, "parse physical device")
  62. }
  63. if len(r.adapters) == 0 {
  64. return errors.Errorf("Not found adaptec raid controller")
  65. }
  66. return nil
  67. }
  68. func (r *AdaptecRaid) CleanRaid() error {
  69. for _, ada := range r.adapters {
  70. ada.removeJBODDisks()
  71. ada.RemoveLogicVolumes()
  72. }
  73. return nil
  74. }
  75. func (r *AdaptecRaid) GetAdapters() []raid.IRaidAdapter {
  76. ret := make([]raid.IRaidAdapter, 0)
  77. for _, a := range r.adapters {
  78. ret = append(ret, a)
  79. }
  80. return ret
  81. }
  82. func (r *AdaptecRaid) PreBuildRaid(confs []*api.BaremetalDiskConfig, adapterIdx int) error {
  83. return nil
  84. }
  85. var (
  86. adaptorIDPatter = regexp.MustCompile(`Controller (?P<idx>[0-9]+):`)
  87. )
  88. func getAdaptorIndex(line string) int {
  89. adapStr := regutils2.GetParams(adaptorIDPatter, line)["idx"]
  90. if adapStr == "" {
  91. return -1
  92. }
  93. adapInt, err := strconv.Atoi(adapStr)
  94. if err != nil {
  95. log.Errorf("Parse adapator string %q id error: %v", line, err)
  96. return -1
  97. }
  98. return adapInt
  99. }
  100. func (r *AdaptecRaid) parsePhyDevs(lines []string) error {
  101. for _, line := range lines {
  102. index := getAdaptorIndex(line)
  103. if index == -1 {
  104. continue
  105. }
  106. ada, err := NewAdaptecRaidAdaptor(index, r)
  107. if err != nil {
  108. return errors.Wrapf(err, "New raid adaptor %d", index)
  109. }
  110. r.adapters = append(r.adapters, ada)
  111. }
  112. return nil
  113. }
  114. type AdaptecRaidAdaptor struct {
  115. *adaptorInfo
  116. index int
  117. raid *AdaptecRaid
  118. devs []*AdaptecRaidPhyDev
  119. }
  120. var (
  121. _ raid.IRaidAdapter = new(AdaptecRaidAdaptor)
  122. )
  123. func NewAdaptecRaidAdaptor(index int, raid *AdaptecRaid) (*AdaptecRaidAdaptor, error) {
  124. adaptor := AdaptecRaidAdaptor{
  125. index: index,
  126. raid: raid,
  127. }
  128. if err := adaptor.fillInfo(); err != nil {
  129. return nil, errors.Wrapf(err, "fill adaptor %d info", adaptor.index)
  130. }
  131. if err := adaptor.fillDevices(); err != nil {
  132. return nil, errors.Wrap(err, "fill physical devices")
  133. }
  134. /*
  135. * if !adaptor.isRaidHideRawMode() {
  136. * if err := adaptor.setControllerModeRaidHideRaw(); err != nil {
  137. * return nil, errors.Wrap(err, "set controller mode to raid hide raw")
  138. * }
  139. * }
  140. */
  141. return &adaptor, nil
  142. }
  143. func (ada *AdaptecRaidAdaptor) GetIndex() int {
  144. return ada.index
  145. }
  146. func (ada *AdaptecRaidAdaptor) PreBuildRaid(confs []*api.BaremetalDiskConfig) error {
  147. if !ada.isRaidHideRawMode() {
  148. if err := ada.setControllerModeRaidHideRaw(); err != nil {
  149. return errors.Wrapf(err, "set controller mode to raid hide raw mode")
  150. }
  151. }
  152. if err := ada.removeJBODDisks(); err != nil {
  153. log.Warningf("remove all JBOD disks: %v", err)
  154. }
  155. // uninitialize all device to ready state
  156. if err := ada.uninitializeAllDevice(); err != nil {
  157. // return errors.Wrap(err, "uninitialize all devices")
  158. log.Warningf("uninitializeAllDevice error: %v", err)
  159. }
  160. return nil
  161. }
  162. func (r *AdaptecRaidAdaptor) PostBuildRaid() error {
  163. return nil
  164. }
  165. func (ada *AdaptecRaidAdaptor) getInitializeCmd(args ...string) string {
  166. newArgs := []string{"TASK", "START", fmt.Sprintf("%d", ada.GetIndex()), "DEVICE"}
  167. newArgs = append(newArgs, args...)
  168. newArgs = append(newArgs, "INITIALIZE", "noprompt")
  169. return GetCommand(newArgs...)
  170. }
  171. func (ada *AdaptecRaidAdaptor) initializeDevice(channel string, id string) error {
  172. cmd := ada.getInitializeCmd(channel, id)
  173. _, err := ada.remoteRun(fmt.Sprintf("initialize device %s:%s", channel, id), cmd)
  174. if err != nil {
  175. return err
  176. }
  177. return nil
  178. }
  179. func (ada *AdaptecRaidAdaptor) initializeAllDevice() error {
  180. cmd := ada.getInitializeCmd("ALL")
  181. _, err := ada.remoteRun("initialize all device", cmd)
  182. if err != nil {
  183. return err
  184. }
  185. return nil
  186. }
  187. func (ada *AdaptecRaidAdaptor) uninitializeDevice(channel string, id string) error {
  188. cmd := ada.getUninitializeCmd(channel, id)
  189. _, err := ada.remoteRun(fmt.Sprintf("uninitialize device %s:%s", channel, id), cmd)
  190. if err != nil {
  191. return err
  192. }
  193. return nil
  194. }
  195. func (ada *AdaptecRaidAdaptor) getUninitializeCmd(args ...string) string {
  196. newArgs := []string{"TASK", "START", fmt.Sprintf("%d", ada.GetIndex()), "DEVICE"}
  197. newArgs = append(newArgs, args...)
  198. newArgs = append(newArgs, "UNINITIALIZE", "noprompt")
  199. return GetCommand(newArgs...)
  200. }
  201. func (ada *AdaptecRaidAdaptor) uninitializeAllDevice() error {
  202. cmd := ada.getUninitializeCmd("ALL")
  203. _, err := ada.remoteRun("uninitialize all devices", cmd)
  204. if err != nil {
  205. return err
  206. }
  207. return nil
  208. }
  209. func (ada *AdaptecRaidAdaptor) getTerm() raid.IExecTerm {
  210. return ada.raid.term
  211. }
  212. func (ada *AdaptecRaidAdaptor) remoteRun(hint string, cmd string) ([]string, error) {
  213. out, err := ada.getTerm().Run(cmd)
  214. if err != nil {
  215. return out, errors.Wrapf(err, "%q, out: %v", hint, out)
  216. }
  217. log.Debugf("remote run cmd %s %q successfully: %v", hint, cmd, out)
  218. return out, nil
  219. }
  220. func (ada *AdaptecRaidAdaptor) fillInfo() error {
  221. cmd := GetCommand("GETCONFIG", fmt.Sprintf("%d", ada.index), "AD")
  222. ret, err := ada.remoteRun("get AD config", cmd)
  223. if err != nil {
  224. return err
  225. }
  226. info, err := getAdaptorInfo(ret)
  227. if err != nil {
  228. return errors.Wrap(err, "get adaptor info")
  229. }
  230. ada.adaptorInfo = info
  231. return nil
  232. }
  233. func (ada *AdaptecRaidAdaptor) fillDevices() error {
  234. cmd := GetCommand("GETCONFIG", fmt.Sprintf("%d", ada.index), "PD")
  235. ret, err := ada.remoteRun("get PD config", cmd)
  236. if err != nil {
  237. return err
  238. }
  239. devs := getPhyDevices(ada.GetIndex(), ret)
  240. ada.devs = append(ada.devs, devs...)
  241. return nil
  242. }
  243. func getPhyDevices(adapter int, lines []string) []*AdaptecRaidPhyDev {
  244. dev := newPhyDev(adapter)
  245. devs := make([]*AdaptecRaidPhyDev, 0)
  246. for _, line := range lines {
  247. if dev.parseLine(line) && dev.isComplete() {
  248. tmpDev := dev
  249. devs = append(devs, tmpDev)
  250. dev = newPhyDev(adapter)
  251. }
  252. }
  253. return devs
  254. }
  255. func (ada *AdaptecRaidAdaptor) getChannelId(storage *baremetal.BaremetalStorage) (string, string, error) {
  256. if storage.Addr == "" {
  257. return "", "", errors.Errorf("storage %#v addr is empty", storage)
  258. }
  259. parts := strings.Split(storage.Addr, ":")
  260. if len(parts) != 2 {
  261. return "", "", errors.Errorf("invalid storage %#v addr %q", storage, storage.Addr)
  262. }
  263. return parts[0], parts[1], nil
  264. }
  265. func (ada *AdaptecRaidAdaptor) getCreateCmd(args ...string) string {
  266. newArgs := []string{"CREATE", fmt.Sprintf("%d", ada.GetIndex())}
  267. newArgs = append(newArgs, args...)
  268. return GetCommand(newArgs...)
  269. }
  270. // setControllerMode change adapter controller's mode
  271. //
  272. // Controller Modes : 0 - RAID: Expose RAW
  273. // : 1 - Auto Volume Mode
  274. // : 2 - HBA Mode
  275. // : 3 - RAID: Hide RAW
  276. // : 4 - Simple Volume Mode
  277. // : 5 - Mixed
  278. func (ada *AdaptecRaidAdaptor) setControllerMode(mode int) error {
  279. cmd := GetCommand("SETCONTROLLERMODE", fmt.Sprintf("%d", ada.GetIndex()), fmt.Sprintf("%d", mode), "noprompt")
  280. if _, err := ada.remoteRun(fmt.Sprintf("set controller mode to %d", mode), cmd); err != nil {
  281. return err
  282. }
  283. return nil
  284. }
  285. func (ada *AdaptecRaidAdaptor) setControllerModeRaidExposeRaw() error {
  286. if err := ada.setControllerMode(0); err != nil {
  287. return errors.Wrap(err, "mode raid expose raw")
  288. }
  289. return nil
  290. }
  291. func (ada *AdaptecRaidAdaptor) setControllerModeRaidHideRaw() error {
  292. if err := ada.setControllerMode(3); err != nil {
  293. return errors.Wrap(err, "mode raid hide raw")
  294. }
  295. return nil
  296. }
  297. func (ada *AdaptecRaidAdaptor) setControllerModeMixed() error {
  298. if err := ada.setControllerMode(5); err != nil {
  299. return errors.Wrap(err, "mode mixed")
  300. }
  301. return nil
  302. }
  303. func (ada *AdaptecRaidAdaptor) buildJBOD(dev *baremetal.BaremetalStorage) error {
  304. channel, id, err := ada.getChannelId(dev)
  305. if err != nil {
  306. return errors.Wrap(err, "get channel and id")
  307. }
  308. cmd := ada.getCreateCmd("JBOD", channel, id, "noprompt")
  309. if out, err := ada.remoteRun("create JBOD", cmd); err != nil {
  310. return errors.Wrapf(err, "run cmd %q, out: %q", cmd, out)
  311. }
  312. return nil
  313. }
  314. func (ada *AdaptecRaidAdaptor) buildNonRaid(dev *baremetal.BaremetalStorage) error {
  315. // set to raid expose raw mode or mixed mode
  316. if !ada.isRaidExposeRawMode() && !ada.isMixedMode() {
  317. err := func() error {
  318. errs := []error{}
  319. if err := ada.setControllerModeRaidExposeRaw(); err != nil {
  320. errs = append(errs, err)
  321. } else {
  322. return nil
  323. }
  324. if err := ada.setControllerModeMixed(); err != nil {
  325. errs = append(errs, err)
  326. } else {
  327. return nil
  328. }
  329. return errors.NewAggregate(errs)
  330. }()
  331. if err != nil {
  332. return errors.Wrap(err, "set raid to expose raw or mixed mode")
  333. }
  334. }
  335. // try build JBOD firstly
  336. if err := ada.buildJBOD(dev); err != nil {
  337. log.Warningf("try build JBOD error: %v", err)
  338. } else {
  339. return nil
  340. }
  341. // just uninitialize device when build JBOD fail
  342. channel, id, err := ada.getChannelId(dev)
  343. if err != nil {
  344. return err
  345. }
  346. if err := ada.uninitializeDevice(channel, id); err != nil {
  347. return errors.Wrapf(err, "uninitialize device %v when build jbod fail", dev)
  348. }
  349. return nil
  350. }
  351. func (ada *AdaptecRaidAdaptor) BuildNoneRaid(devs []*baremetal.BaremetalStorage) error {
  352. for _, dev := range devs {
  353. if err := ada.buildNonRaid(dev); err != nil {
  354. return errors.Wrap(err, "build nonraid")
  355. }
  356. }
  357. return nil
  358. }
  359. func (ada *AdaptecRaidAdaptor) getBuildRaidCmd(level int, devs []*baremetal.BaremetalStorage) (string, error) {
  360. if len(devs) == 0 {
  361. return "", errors.Errorf("devices is empty")
  362. }
  363. args := []string{"LOGICALDRIVE", "MAX", fmt.Sprintf("%d", level)}
  364. chIds := []string{}
  365. for _, dev := range devs {
  366. ch, id, err := ada.getChannelId(dev)
  367. if err != nil {
  368. return "", errors.Wrapf(err, "get device %v channel id", dev)
  369. }
  370. chIds = append(chIds, ch, id)
  371. }
  372. args = append(args, chIds...)
  373. cmd := ada.getCreateCmd(args...)
  374. return cmd, nil
  375. }
  376. func (ada *AdaptecRaidAdaptor) buildRaid(level int, devs []*baremetal.BaremetalStorage, conf *api.BaremetalDiskConfig) error {
  377. // initialize each devs
  378. for _, dev := range devs {
  379. channel, id, err := ada.getChannelId(dev)
  380. if err != nil {
  381. return errors.Wrapf(err, "get device %v channel id", dev)
  382. }
  383. if err := ada.initializeDevice(channel, id); err != nil {
  384. // return errors.Wrapf(err, "initialize device")
  385. log.Warningf("initialize device error: %v", err)
  386. }
  387. }
  388. // TODO: support config to build raid params
  389. cmd, err := ada.getBuildRaidCmd(level, devs)
  390. if err != nil {
  391. return err
  392. }
  393. _, err = ada.remoteRun(fmt.Sprintf("build raid %d", level), cmd)
  394. return err
  395. }
  396. func (ada *AdaptecRaidAdaptor) BuildRaid0(devs []*baremetal.BaremetalStorage, conf *api.BaremetalDiskConfig) error {
  397. return ada.buildRaid(0, devs, conf)
  398. }
  399. func (ada *AdaptecRaidAdaptor) BuildRaid1(devs []*baremetal.BaremetalStorage, conf *api.BaremetalDiskConfig) error {
  400. return ada.buildRaid(1, devs, conf)
  401. }
  402. func (ada *AdaptecRaidAdaptor) BuildRaid5(devs []*baremetal.BaremetalStorage, conf *api.BaremetalDiskConfig) error {
  403. return ada.buildRaid(5, devs, conf)
  404. }
  405. func (ada *AdaptecRaidAdaptor) BuildRaid10(devs []*baremetal.BaremetalStorage, conf *api.BaremetalDiskConfig) error {
  406. return ada.buildRaid(10, devs, conf)
  407. }
  408. func (ada *AdaptecRaidAdaptor) GetDevices() []*baremetal.BaremetalStorage {
  409. ret := []*baremetal.BaremetalStorage{}
  410. for idx, dev := range ada.devs {
  411. ret = append(ret, dev.ToBaremetalStorage(idx))
  412. }
  413. return ret
  414. }
  415. func (ada *AdaptecRaidAdaptor) GetLogicVolumes() ([]*raid.RaidLogicalVolume, error) {
  416. cmd := GetCommand("GETCONFIG", fmt.Sprintf("%d", ada.index), "LD")
  417. ret, err := ada.remoteRun("get logic volumes", cmd)
  418. if err != nil {
  419. return nil, fmt.Errorf("Get logic volumes error: %v", err)
  420. }
  421. return getLogicalVolumes(ada.index, ret)
  422. }
  423. func getLogicalVolumes(adapter int, lines []string) ([]*raid.RaidLogicalVolume, error) {
  424. lvs := make([]*raid.RaidLogicalVolume, 0)
  425. for _, line := range lines {
  426. m := regutils2.SubGroupMatch(`Logical Device number\s+(?P<idx>\d+)`, line)
  427. if len(m) > 0 {
  428. idxStr := m["idx"]
  429. idx, err := strconv.Atoi(idxStr)
  430. if err != nil {
  431. return nil, errors.Errorf("%s index str is not digit: %v", idxStr, err)
  432. }
  433. lvs = append(lvs, &raid.RaidLogicalVolume{
  434. Index: idx,
  435. Adapter: adapter,
  436. })
  437. }
  438. }
  439. return lvs, nil
  440. }
  441. func (ada *AdaptecRaidAdaptor) removeJBODDisks() error {
  442. cmd := GetCommand("DELETE", fmt.Sprintf("%d", ada.index), "JBOD", "ALL", "noprompt")
  443. out, err := ada.remoteRun("delete JBOD disks", cmd)
  444. if err != nil {
  445. return errors.Wrapf(err, "delete all JBOD output: %q", out)
  446. }
  447. return nil
  448. }
  449. func (ada *AdaptecRaidAdaptor) RemoveLogicVolumes() error {
  450. lvs, err := ada.GetLogicVolumes()
  451. if err != nil {
  452. return errors.Wrap(err, "get logic volumes")
  453. }
  454. if len(lvs) == 0 {
  455. return nil
  456. }
  457. cmd := GetCommand("DELETE", fmt.Sprintf("%d", ada.index), "LOGICALDRIVE", "ALL", "noprompt")
  458. out, err := ada.remoteRun("delete logical volumes", cmd)
  459. if err != nil {
  460. return errors.Wrapf(err, "delete all logicaldrive output: %q", out)
  461. }
  462. return nil
  463. }
  464. type adaptorInfo struct {
  465. status string
  466. mode string
  467. name string
  468. sn string
  469. wwn string
  470. slot string
  471. }
  472. func (ada *adaptorInfo) key() string {
  473. return ada.name + ada.sn
  474. }
  475. func (ada *adaptorInfo) isRaidExposeRawMode() bool {
  476. return ada.mode == ControllerModeRaidExposeRaw
  477. }
  478. func (ada *adaptorInfo) isRaidHideRawMode() bool {
  479. return ada.mode == ControllerModeRaidHideRaw
  480. }
  481. func (ada *adaptorInfo) isMixedMode() bool {
  482. return ada.mode == ControllerModeMixed
  483. }
  484. func getAdaptorInfo(lines []string) (*adaptorInfo, error) {
  485. ada := new(adaptorInfo)
  486. for _, l := range lines {
  487. key, val := stringutils.SplitKeyValue(l)
  488. if len(key) == 0 {
  489. continue
  490. }
  491. switch key {
  492. case "Controller Status":
  493. ada.status = val
  494. case "Controller Mode":
  495. ada.mode = val
  496. case "Controller Model":
  497. ada.name = val
  498. case "Controller Serial Number":
  499. ada.sn = val
  500. case "Controller World Wide Name":
  501. ada.wwn = val
  502. case "Physical Slot":
  503. ada.slot = val
  504. }
  505. }
  506. if len(ada.key()) == 0 {
  507. return nil, errors.Errorf("Not found SN and model name")
  508. }
  509. return ada, nil
  510. }
  511. type AdaptecRaidPhyDev struct {
  512. *raid.RaidBasePhyDev
  513. // channelId and deviceId is parsed by Reported Channel,Device(T:L)
  514. // e.g.: Reported Channel,Device(T:L) : 0,6(6:0)
  515. channelId string
  516. deviceId string
  517. }
  518. func newPhyDev(adapter int) *AdaptecRaidPhyDev {
  519. dev := &AdaptecRaidPhyDev{
  520. RaidBasePhyDev: raid.NewRaidBasePhyDev(api.DISK_DRIVER_ADAPTECRAID),
  521. }
  522. dev.Adapter = adapter
  523. return dev
  524. }
  525. func (dev *AdaptecRaidPhyDev) isComplete() bool {
  526. if !dev.RaidBasePhyDev.IsComplete() {
  527. return false
  528. }
  529. if dev.channelId == "" || dev.deviceId == "" {
  530. return false
  531. }
  532. return true
  533. }
  534. func (dev *AdaptecRaidPhyDev) ToBaremetalStorage(index int) *baremetal.BaremetalStorage {
  535. s := dev.RaidBasePhyDev.ToBaremetalStorage(index)
  536. s.Addr = fmt.Sprintf("%s:%s", dev.channelId, dev.deviceId)
  537. return s
  538. }
  539. var (
  540. channelDeviceRegexp = regexp.MustCompile(`Reported Channel,Device\(T:L\).*(?P<channel>\d+),(?P<device>\d+)\(\d+:\d+\)`)
  541. )
  542. func (dev *AdaptecRaidPhyDev) parseLine(line string) bool {
  543. chanDevMatch := regutils2.GetParams(channelDeviceRegexp, line)
  544. if len(chanDevMatch) != 0 {
  545. channelId := chanDevMatch["channel"]
  546. deviceId := chanDevMatch["device"]
  547. dev.channelId = channelId
  548. dev.deviceId = deviceId
  549. return true
  550. }
  551. key, val := stringutils.SplitKeyValue(line)
  552. if key == "" {
  553. return false
  554. }
  555. switch key {
  556. case "Total Size":
  557. dat := strings.Split(val, " ")
  558. szStr, unitStr := dat[0], dat[1]
  559. var sz int64
  560. szInt, err := strconv.Atoi(szStr)
  561. if err != nil {
  562. log.Errorf("Parse size string %s: %v", szStr, err)
  563. return false
  564. }
  565. switch unitStr {
  566. case "GB":
  567. sz = int64(szInt * 1000 * 1000 * 100)
  568. case "TB":
  569. sz = int64(szInt * 1000 * 1000 * 1000 * 1000)
  570. case "MB":
  571. sz = int64(szInt * 1000 * 1000)
  572. default:
  573. log.Errorf("Unsupported unit: %s", unitStr)
  574. return false
  575. }
  576. dev.Size = sz / 1024 / 1024
  577. case "Model":
  578. dev.Model = strings.Join(regexp.MustCompile(`\s+`).Split(val, -1), " ")
  579. case "State":
  580. dev.Status = val
  581. case "SSD":
  582. if val == "No" {
  583. dev.Rotate = tristate.True
  584. } else {
  585. dev.Rotate = tristate.False
  586. }
  587. default:
  588. return false
  589. }
  590. return true
  591. }