storage_nvme.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 storageman
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  24. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  25. )
  26. type SNVMEStorage struct {
  27. SBaseStorage
  28. sizeMB int
  29. }
  30. func (s *SNVMEStorage) GetAvailSizeMb() int {
  31. return s.sizeMB
  32. }
  33. func (s *SNVMEStorage) GetCapacityMb() int {
  34. return s.GetAvailSizeMb()
  35. }
  36. func (s *SNVMEStorage) SyncStorageSize() (api.SHostStorageStat, error) {
  37. stat := api.SHostStorageStat{
  38. StorageId: s.StorageId,
  39. }
  40. stat.CapacityMb = int64(s.GetAvailSizeMb())
  41. stat.ActualCapacityUsedMb = 0
  42. return stat, nil
  43. }
  44. func (s *SNVMEStorage) SyncStorageInfo() (jsonutils.JSONObject, error) {
  45. content := jsonutils.NewDict()
  46. name := s.GetName(s.GetComposedName)
  47. content.Set("name", jsonutils.NewString(name))
  48. content.Set("storage_type", jsonutils.NewString(s.StorageType()))
  49. content.Set("status", jsonutils.NewString(api.STORAGE_ONLINE))
  50. content.Set("zone", jsonutils.NewString(s.GetZoneId()))
  51. if s.GetAvailSizeMb() > 0 {
  52. content.Set("capacity", jsonutils.NewInt(int64(s.GetAvailSizeMb())))
  53. }
  54. var (
  55. err error
  56. res jsonutils.JSONObject
  57. )
  58. log.Infof("Sync storage info %s/%s", s.StorageId, name)
  59. if len(s.StorageId) > 0 {
  60. res, err = modules.Storages.Put(
  61. hostutils.GetComputeSession(context.Background()),
  62. s.StorageId, content)
  63. } else {
  64. content.Set("medium_type", jsonutils.NewString(api.DISK_TYPE_SSD))
  65. res, err = modules.Storages.Create(
  66. hostutils.GetComputeSession(context.Background()), content)
  67. }
  68. if err != nil {
  69. log.Errorf("SyncStorageInfo Failed: %s: %s", content, err)
  70. }
  71. return res, err
  72. }
  73. func (s *SNVMEStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) error {
  74. s.StorageId = storageId
  75. s.StorageName = storageName
  76. if dconf, ok := conf.(*jsonutils.JSONDict); ok {
  77. s.StorageConf = dconf
  78. }
  79. return nil
  80. }
  81. func (s *SNVMEStorage) GetSnapshotDir() string {
  82. return ""
  83. }
  84. func (s *SNVMEStorage) GetSnapshotPathByIds(diskId, snapshotId string) string {
  85. return ""
  86. }
  87. func (s *SNVMEStorage) DeleteSnapshots(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  88. return nil, errors.Errorf("unsupported operation")
  89. }
  90. func (s *SNVMEStorage) DeleteSnapshot(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  91. return nil, errors.Errorf("unsupported operation")
  92. }
  93. func (s *SNVMEStorage) IsSnapshotExist(diskId, snapshotId string) (bool, error) {
  94. return false, errors.Errorf("unsupported operation")
  95. }
  96. func (s *SNVMEStorage) GetDiskById(diskId string) (IDisk, error) {
  97. s.DiskLock.Lock()
  98. defer s.DiskLock.Unlock()
  99. for i := 0; i < len(s.Disks); i++ {
  100. if s.Disks[i].GetId() == diskId {
  101. err := s.Disks[i].Probe()
  102. if err != nil {
  103. return nil, errors.Wrapf(err, "disk.Prob")
  104. }
  105. return s.Disks[i], nil
  106. }
  107. }
  108. var disk = NewNVMEDisk(s, diskId)
  109. if disk.Probe() == nil {
  110. s.Disks = append(s.Disks, disk)
  111. return disk, nil
  112. }
  113. return nil, errors.ErrNotFound
  114. }
  115. func (s *SNVMEStorage) CreateDisk(diskId string) IDisk {
  116. s.DiskLock.Lock()
  117. defer s.DiskLock.Unlock()
  118. disk := NewNVMEDisk(s, diskId)
  119. s.Disks = append(s.Disks, disk)
  120. return disk
  121. }
  122. func (s *SNVMEStorage) SaveToGlance(context.Context, interface{}) (jsonutils.JSONObject, error) {
  123. return nil, errors.Errorf("unsupported operation")
  124. }
  125. func (s *SNVMEStorage) CreateDiskFromSnapshot(context.Context, IDisk, *SDiskCreateByDiskinfo) (jsonutils.JSONObject, error) {
  126. return nil, errors.Errorf("unsupported operation")
  127. }
  128. func (s *SNVMEStorage) CreateDiskFromExistingPath(context.Context, IDisk, *SDiskCreateByDiskinfo) error {
  129. return errors.Errorf("unsupported operation")
  130. }
  131. func (s *SNVMEStorage) CreateSnapshotFormUrl(ctx context.Context, snapshotUrl, diskId, snapshotPath string) error {
  132. return errors.Errorf("unsupported operation")
  133. }
  134. func (s *SNVMEStorage) GetFuseTmpPath() string {
  135. return ""
  136. }
  137. func (s *SNVMEStorage) GetFuseMountPath() string {
  138. return ""
  139. }
  140. func (s *SNVMEStorage) GetImgsaveBackupPath() string {
  141. return ""
  142. }
  143. func (s *SNVMEStorage) Accessible() error {
  144. return nil
  145. }
  146. func (s *SNVMEStorage) Detach() error {
  147. return nil
  148. }
  149. func newNVMEStorage(manager *SStorageManager, path string, sizeMB int) *SNVMEStorage {
  150. var ret = new(SNVMEStorage)
  151. ret.SBaseStorage = *NewBaseStorage(manager, path)
  152. ret.sizeMB = sizeMB
  153. return ret
  154. }
  155. func (s *SNVMEStorage) StorageType() string {
  156. return api.STORAGE_NVME_PT
  157. }
  158. func (s *SNVMEStorage) IsLocal() bool {
  159. return true
  160. }
  161. func (s *SNVMEStorage) GetComposedName() string {
  162. p := strings.ReplaceAll(s.Path, ".", "_")
  163. p = strings.ReplaceAll(s.Path, ":", "_")
  164. return fmt.Sprintf("host_%s_%s_storage_%s", s.Manager.host.GetMasterIp(), s.StorageType(), p)
  165. }
  166. func (s *SNVMEStorage) CleanRecycleDiskfiles(ctx context.Context) {
  167. log.Infof("SNVMEStorage CleanRecycleDiskfiles do nothing!")
  168. }