nvme.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 isolated_device
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/fileutils"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. )
  22. type sNVMEDevice struct {
  23. *SBaseDevice
  24. sizeMB int
  25. }
  26. func (dev *sNVMEDevice) GetVGACmd() string {
  27. return ""
  28. }
  29. func (dev *sNVMEDevice) GetCPUCmd() string {
  30. return ""
  31. }
  32. func (dev *sNVMEDevice) GetQemuId() string {
  33. return fmt.Sprintf("dev_%s", strings.ReplaceAll(dev.GetAddr(), ":", "_"))
  34. }
  35. func (dev *sNVMEDevice) GetNVMESizeMB() int {
  36. return dev.sizeMB
  37. }
  38. func newNVMEDevice(dev *PCIDevice, devType string, sizeMB int) *sNVMEDevice {
  39. return &sNVMEDevice{
  40. SBaseDevice: NewBaseDevice(dev, devType),
  41. sizeMB: sizeMB,
  42. }
  43. }
  44. func getPassthroughNVMEDisks(nvmePciDisks []string) ([]*sNVMEDevice, error) {
  45. devs := make([]*sNVMEDevice, 0)
  46. for _, conf := range nvmePciDisks {
  47. diskConf := strings.Split(conf, "/")
  48. if len(diskConf) != 2 {
  49. return nil, fmt.Errorf("bad nvme config %s", conf)
  50. }
  51. var pciAddr, size = diskConf[0], diskConf[1]
  52. sizeMb, err := fileutils.GetSizeMb(size, 'M', 1024)
  53. if err != nil {
  54. return nil, errors.Wrapf(err, "failed parse pci device %s size %s", pciAddr, size)
  55. }
  56. dev, err := detectPCIDevByAddrWithoutIOMMUGroup(pciAddr)
  57. if err != nil {
  58. return nil, errors.Wrap(err, "detectPCIDevByAddrWithoutIOMMUGroup")
  59. }
  60. devs = append(devs, newNVMEDevice(dev, api.NVME_PT_TYPE, sizeMb))
  61. }
  62. return devs, nil
  63. }