disk_slvm.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. "strings"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/onecloud/pkg/apis"
  24. api "yunion.io/x/onecloud/pkg/apis/compute"
  25. "yunion.io/x/onecloud/pkg/hostman/storageman/lvmutils"
  26. "yunion.io/x/onecloud/pkg/util/fileutils2"
  27. "yunion.io/x/onecloud/pkg/util/qemuimg"
  28. "yunion.io/x/onecloud/pkg/util/seclib2"
  29. )
  30. // shared lvm
  31. type SSLVMDisk struct {
  32. SLVMDisk
  33. }
  34. func NewSLVMDisk(storage IStorage, id string) *SSLVMDisk {
  35. return &SSLVMDisk{
  36. SLVMDisk: *NewLVMDisk(storage, id),
  37. }
  38. }
  39. func (d *SSLVMDisk) GetType() string {
  40. return api.STORAGE_SLVM
  41. }
  42. func (d *SSLVMDisk) GetPath() string {
  43. return path.Join("/dev", d.Storage.GetPath(), d.Id)
  44. }
  45. func (d *SSLVMDisk) Probe() error {
  46. if err := lvmutils.LvScan(); err != nil {
  47. return errors.Wrap(err, "lv scan")
  48. }
  49. var lvPath = d.GetPath()
  50. if err := lvmutils.LVActive(lvPath, d.Storage.Lvmlockd(), false); err != nil {
  51. return errors.Wrap(err, "lv active")
  52. }
  53. diskPath := d.GetPath()
  54. storageBasePath := path.Join("/dev", d.Storage.GetPath())
  55. for diskPath != "" {
  56. qemuImg, err := qemuimg.NewQemuImage(diskPath)
  57. if err != nil {
  58. log.Errorln(err)
  59. return err
  60. }
  61. if qemuImg.BackFilePath != "" && strings.HasPrefix(qemuImg.BackFilePath, storageBasePath) {
  62. diskPath = qemuImg.BackFilePath
  63. originActivated, err := lvmutils.LvIsActivated(qemuImg.BackFilePath)
  64. if err != nil {
  65. return errors.Wrap(err, "check lv is activated")
  66. }
  67. if !originActivated {
  68. if err = lvmutils.LVActive(qemuImg.BackFilePath, d.Storage.Lvmlockd(), false); err != nil {
  69. return errors.Wrap(err, "lv active origin")
  70. }
  71. }
  72. } else {
  73. diskPath = ""
  74. }
  75. }
  76. if !fileutils2.Exists(d.GetPath()) {
  77. return errors.Wrapf(cloudprovider.ErrNotFound, "%s", d.GetPath())
  78. }
  79. return nil
  80. }
  81. func (d *SSLVMDisk) CreateRaw(ctx context.Context, sizeMb int, diskFormat string, fsFormat string, fsFeatures *api.DiskFsFeatures, encryptInfo *apis.SEncryptInfo, diskId string, back string) (jsonutils.JSONObject, error) {
  82. ret, err := d.SLVMDisk.CreateRaw(ctx, sizeMb, diskFormat, fsFormat, nil, encryptInfo, diskId, back)
  83. if err != nil {
  84. return ret, err
  85. }
  86. err = lvmutils.LVDeactivate(d.GetPath())
  87. if err != nil {
  88. return ret, errors.Wrap(err, "LVDeactivate")
  89. }
  90. return ret, nil
  91. }
  92. func (d *SSLVMDisk) CreateFromTemplate(
  93. ctx context.Context, imageId, format string, sizeMb int64, encryptInfo *apis.SEncryptInfo,
  94. ) (jsonutils.JSONObject, error) {
  95. ret, err := d.SLVMDisk.CreateFromTemplate(ctx, imageId, format, sizeMb, encryptInfo)
  96. if err != nil {
  97. return ret, err
  98. }
  99. err = lvmutils.LVDeactivate(d.GetPath())
  100. if err != nil {
  101. return ret, errors.Wrap(err, "LVDeactivate")
  102. }
  103. return ret, nil
  104. }
  105. func (d *SSLVMDisk) PreResize(ctx context.Context, sizeMb int64) error {
  106. if ok, err := lvmutils.LvIsActivated(d.GetPath()); err != nil {
  107. return err
  108. } else if ok && d.Storage.Lvmlockd() {
  109. err = lvmutils.LVActive(d.GetPath(), false, d.Storage.Lvmlockd())
  110. if err != nil {
  111. return errors.Wrap(err, "lvactive shared")
  112. }
  113. }
  114. err := d.SLVMDisk.PreResize(ctx, sizeMb)
  115. if err != nil {
  116. return err
  117. }
  118. err = lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false)
  119. if err != nil {
  120. return errors.Wrap(err, "lvactive shared")
  121. }
  122. return nil
  123. }
  124. func (d *SSLVMDisk) Resize(ctx context.Context, params *SDiskResizeInput) (jsonutils.JSONObject, error) {
  125. if ok, err := lvmutils.LvIsActivated(d.GetPath()); err != nil {
  126. return nil, err
  127. } else if ok && d.Storage.Lvmlockd() {
  128. err = lvmutils.LVActive(d.GetPath(), false, d.Storage.Lvmlockd())
  129. if err != nil {
  130. return nil, errors.Wrap(err, "lvactive shared")
  131. }
  132. }
  133. ret, err := d.SLVMDisk.Resize(ctx, params)
  134. if err != nil {
  135. return ret, err
  136. }
  137. err = lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false)
  138. if err != nil {
  139. return ret, errors.Wrap(err, "lvactive shared")
  140. }
  141. return ret, nil
  142. }
  143. func TryDeactivateBackingLvs(backingFile string) {
  144. if backingFile == "" {
  145. return
  146. }
  147. qemuImg, err := qemuimg.NewQemuImage(backingFile)
  148. if err != nil {
  149. log.Errorf("tryDeactivateBackingLvs NewQemuImage %s", err)
  150. return
  151. }
  152. err = lvmutils.LVDeactivate(backingFile)
  153. if err != nil {
  154. log.Errorf("tryDeactivateBackingLvs LVDeactivate %s", err)
  155. return
  156. }
  157. TryDeactivateBackingLvs(qemuImg.BackFilePath)
  158. }
  159. func (d *SSLVMDisk) Delete(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  160. var lvPath = d.GetPath()
  161. activated, err := lvmutils.LvIsActivated(lvPath)
  162. if err != nil {
  163. return nil, errors.Wrap(err, "check lv is activated")
  164. }
  165. if !activated {
  166. if err := lvmutils.LVActive(lvPath, d.Storage.Lvmlockd(), false); err != nil {
  167. return nil, errors.Wrap(err, "lv active")
  168. }
  169. }
  170. qemuImg, err := qemuimg.NewQemuImage(lvPath)
  171. if err != nil {
  172. return nil, errors.Wrap(err, "NewQemuImage")
  173. }
  174. TryDeactivateBackingLvs(qemuImg.BackFilePath)
  175. return d.SLVMDisk.Delete(ctx, params)
  176. }
  177. func (d *SSLVMDisk) CreateSnapshot(snapshotId string, encryptKey string, encFormat qemuimg.TEncryptFormat, encAlg seclib2.TSymEncAlg) error {
  178. err := lvmutils.LVActive(d.GetPath(), false, d.Storage.Lvmlockd())
  179. if err != nil {
  180. return errors.Wrap(err, "lvactive exclusive")
  181. }
  182. err = d.SLVMDisk.CreateSnapshot(snapshotId, encryptKey, encFormat, encAlg)
  183. if err != nil {
  184. e3 := lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false)
  185. if e3 != nil {
  186. log.Errorf("failed lvactive share %s", e3)
  187. }
  188. return err
  189. }
  190. // active disk share mode
  191. err = lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false)
  192. if err != nil {
  193. return errors.Wrap(err, "lvactive snapshot share")
  194. }
  195. // active snapshot active mode
  196. snapPath := d.GetSnapshotPath(snapshotId)
  197. err = lvmutils.LVActive(snapPath, d.Storage.Lvmlockd(), false)
  198. if err != nil {
  199. return errors.Wrap(err, "lvactive snapshot share")
  200. }
  201. return nil
  202. }
  203. func (d *SSLVMDisk) PostCreateFromRemoteHostImage(string) {
  204. log.Infof("slvm post create from fuse do nothing")
  205. }
  206. func (d *SSLVMDisk) ResetFromSnapshot(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  207. err := lvmutils.LVActive(d.GetPath(), false, d.Storage.Lvmlockd())
  208. if err != nil {
  209. return nil, errors.Wrap(err, "lvactive exclusive")
  210. }
  211. ret, err := d.SLVMDisk.ResetFromSnapshot(ctx, params)
  212. if err != nil {
  213. err := lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false)
  214. if err != nil {
  215. log.Errorf("failed lvactive share %s", err)
  216. }
  217. return nil, err
  218. }
  219. return ret, nil
  220. }
  221. func (d *SSLVMDisk) GetDiskDesc() jsonutils.JSONObject {
  222. active, err := lvmutils.LvIsActivated(d.GetPath())
  223. if err != nil {
  224. log.Errorf("failed check active of %s: %s", d.GetPath(), err)
  225. return nil
  226. }
  227. if !active {
  228. if err := lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false); err != nil {
  229. log.Errorf("failed active lv %s: %s", d.GetPath(), err)
  230. return nil
  231. }
  232. }
  233. res := d.SLVMDisk.GetDiskDesc()
  234. if !active {
  235. if err := lvmutils.LVDeactivate(d.GetPath()); err != nil {
  236. log.Errorf("failed deactivate lv %s: %s", d.GetPath(), err)
  237. }
  238. }
  239. return res
  240. }
  241. func (d *SSLVMDisk) PrepareSaveToGlance(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  242. active, err := lvmutils.LvIsActivated(d.GetPath())
  243. if err != nil {
  244. return nil, errors.Wrap(err, "LvIsActivated")
  245. }
  246. if !active {
  247. if err := lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false); err != nil {
  248. return nil, errors.Wrap(err, "LVActive")
  249. }
  250. }
  251. res, e := d.SLVMDisk.PrepareSaveToGlance(ctx, params)
  252. if !active {
  253. if err := lvmutils.LVDeactivate(d.GetPath()); err != nil {
  254. log.Errorf("failed deactivate lv %s: %s", d.GetPath(), err)
  255. }
  256. }
  257. return res, e
  258. }
  259. func (d *SSLVMDisk) CreateFromSnapshotLocation(ctx context.Context, snapshotLocation string, size int64, encryptInfo *apis.SEncryptInfo) (jsonutils.JSONObject, error) {
  260. ret, err := d.SLVMDisk.CreateRaw(ctx, int(size), "", "", nil, encryptInfo, d.Id, snapshotLocation)
  261. if err != nil {
  262. return nil, err
  263. }
  264. qemuImg, err := qemuimg.NewQemuImage(d.GetPath())
  265. if err != nil {
  266. return nil, errors.Wrap(err, "NewQemuImage")
  267. }
  268. TryDeactivateBackingLvs(qemuImg.BackFilePath)
  269. err = lvmutils.LVDeactivate(d.GetPath())
  270. if err != nil {
  271. return nil, errors.Wrap(err, "LVDeactivate")
  272. }
  273. return ret, nil
  274. }
  275. func (d *SSLVMDisk) DeleteSnapshot(snapshotId, convertSnapshot string, blockStream bool, encryptInfo apis.SEncryptInfo) error {
  276. err := lvmutils.LVActive(d.GetPath(), false, d.Storage.Lvmlockd())
  277. if err != nil {
  278. return errors.Wrap(err, "LVActive")
  279. }
  280. var convertSnapshotPath string
  281. if len(convertSnapshot) > 0 {
  282. convertSnapshotPath = d.GetSnapshotPath(convertSnapshot)
  283. err = lvmutils.LVActive(convertSnapshotPath, false, d.Storage.Lvmlockd())
  284. if err != nil {
  285. return errors.Wrap(err, "LVActive convert snapshot")
  286. }
  287. }
  288. err = d.SLVMDisk.DeleteSnapshot(snapshotId, convertSnapshot, blockStream, encryptInfo)
  289. // active disk share mode
  290. e := lvmutils.LVActive(d.GetPath(), d.Storage.Lvmlockd(), false)
  291. if e != nil {
  292. log.Errorf("failed active with share mode: %s", e)
  293. }
  294. if len(convertSnapshot) > 0 {
  295. e = lvmutils.LVActive(convertSnapshotPath, d.Storage.Lvmlockd(), false)
  296. if e != nil {
  297. log.Errorf("failed active convert snapshot %s with share mode: %s", convertSnapshotPath, e)
  298. }
  299. }
  300. return err
  301. }