storage_slvm.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "path"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  23. "yunion.io/x/onecloud/pkg/hostman/storageman/lvmutils"
  24. )
  25. func init() {
  26. registerStorageFactory(&SSLVMStorageFactory{})
  27. }
  28. type SSLVMStorageFactory struct {
  29. }
  30. func (factory *SSLVMStorageFactory) NewStorage(manager *SStorageManager, mountPoint string) IStorage {
  31. return NewSLVMStorage(manager, mountPoint)
  32. }
  33. func (factory *SSLVMStorageFactory) StorageType() string {
  34. return api.STORAGE_SLVM
  35. }
  36. type SSLVMStorage struct {
  37. lvmlockd bool
  38. *SLVMStorage
  39. }
  40. func NewSLVMStorage(manager *SStorageManager, vgName string) *SSLVMStorage {
  41. var ret = new(SSLVMStorage)
  42. ret.SLVMStorage = NewLVMStorage(manager, vgName)
  43. return ret
  44. }
  45. func (s *SSLVMStorage) newDisk(diskId string) IDisk {
  46. return NewSLVMDisk(s, diskId)
  47. }
  48. func (s *SSLVMStorage) StorageType() string {
  49. return api.STORAGE_SLVM
  50. }
  51. func (s *SSLVMStorage) IsLocal() bool {
  52. return false
  53. }
  54. func (s *SSLVMStorage) Lvmlockd() bool {
  55. return s.lvmlockd
  56. }
  57. func (s *SSLVMStorage) CreateDisk(diskId string) IDisk {
  58. s.DiskLock.Lock()
  59. defer s.DiskLock.Unlock()
  60. disk := NewSLVMDisk(s, diskId)
  61. s.Disks = append(s.Disks, disk)
  62. return disk
  63. }
  64. func (s *SSLVMStorage) GetDiskById(diskId string) (IDisk, error) {
  65. s.DiskLock.Lock()
  66. defer s.DiskLock.Unlock()
  67. for i := 0; i < len(s.Disks); i++ {
  68. if s.Disks[i].GetId() == diskId {
  69. err := s.Disks[i].Probe()
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "disk.Probe")
  72. }
  73. return s.Disks[i], nil
  74. }
  75. }
  76. var disk = NewSLVMDisk(s, diskId)
  77. err := disk.Probe()
  78. if err == nil {
  79. s.Disks = append(s.Disks, disk)
  80. return disk, nil
  81. } else {
  82. log.Errorf("failed probe slvm disk %s: %s", diskId, err)
  83. }
  84. return nil, errors.ErrNotFound
  85. }
  86. func (s *SSLVMStorage) CreateDiskFromSnapshot(ctx context.Context, disk IDisk, input *SDiskCreateByDiskinfo) (jsonutils.JSONObject, error) {
  87. snapshotLocation := disk.GetSnapshotPath(input.DiskInfo.SnapshotId)
  88. return disk.CreateFromSnapshotLocation(ctx, snapshotLocation, int64(input.DiskInfo.DiskSizeMb), &input.DiskInfo.EncryptInfo)
  89. }
  90. func (s *SSLVMStorage) DeleteSnapshot(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  91. input, ok := params.(*SStorageDeleteSnapshot)
  92. if !ok {
  93. return nil, hostutils.ParamsError
  94. }
  95. if input.BlockStream {
  96. diskLvPath := path.Join("/dev", s.GetPath(), input.DiskId)
  97. err := lvmutils.LVActive(diskLvPath, false, s.Lvmlockd())
  98. if err != nil {
  99. return nil, errors.Wrap(err, "lvactive exclusive")
  100. }
  101. err = ConvertLVMDisk(s.GetPath(), input.DiskId, input.EncryptInfo)
  102. if err != nil {
  103. return nil, err
  104. }
  105. } else if len(input.ConvertSnapshot) > 0 {
  106. convertSnapshotName := "snap_" + input.ConvertSnapshot
  107. convertSnapshotPath := path.Join("/dev", s.GetPath(), convertSnapshotName)
  108. err := lvmutils.LVActive(convertSnapshotPath, false, s.Lvmlockd())
  109. if err != nil {
  110. return nil, errors.Wrap(err, "lvactive exclusive")
  111. }
  112. if err := ConvertLVMDisk(s.GetPath(), convertSnapshotName, input.EncryptInfo); err != nil {
  113. return nil, err
  114. }
  115. }
  116. snapName := "snap_" + input.SnapshotId
  117. snapId := path.Join("/dev", s.GetPath(), snapName)
  118. err := lvmutils.LvRemove(snapId)
  119. if err != nil {
  120. return nil, err
  121. }
  122. res := jsonutils.NewDict()
  123. res.Set("deleted", jsonutils.JSONTrue)
  124. return res, nil
  125. }
  126. func (s *SSLVMStorage) Accessible() error {
  127. if err := lvmutils.VgActive(s.Path, true, true); err != nil {
  128. log.Warningf("vgactive got %s", err)
  129. }
  130. if err := lvmutils.VgDisplay(s.Path); err != nil {
  131. return err
  132. }
  133. return nil
  134. }
  135. func (s *SSLVMStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) error {
  136. s.StorageId = storageId
  137. s.StorageName = storageName
  138. if dconf, ok := conf.(*jsonutils.JSONDict); ok {
  139. if jsonutils.QueryBoolean(dconf, "enabled_lvmlockd", false) {
  140. log.Infof("storage %s(%s) enabled lvmlockd", s.StorageId, s.StorageName)
  141. s.lvmlockd = true
  142. }
  143. s.StorageConf = dconf
  144. }
  145. if err := s.Accessible(); err != nil {
  146. return err
  147. }
  148. return nil
  149. }
  150. func (s *SSLVMStorage) CreateDiskFromBackup(ctx context.Context, disk IDisk, input *SDiskCreateByDiskinfo) error {
  151. err := s.SLVMStorage.CreateDiskFromBackup(ctx, disk, input)
  152. if err != nil {
  153. return err
  154. }
  155. err = lvmutils.LVDeactivate(disk.GetPath())
  156. if err != nil {
  157. return errors.Wrap(err, "LVDeactivate")
  158. }
  159. return nil
  160. }