disk_nas.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "path"
  19. "github.com/pkg/errors"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/util/qemuimg"
  27. )
  28. var _ IDisk = (*SNasDisk)(nil)
  29. type SNasDisk struct {
  30. SLocalDisk
  31. }
  32. func NewNasDisk(storage IStorage, id string) *SNasDisk {
  33. return &SNasDisk{*NewLocalDisk(storage, id)}
  34. }
  35. func (d *SNasDisk) CreateFromTemplate(ctx context.Context, imageId, format string, size int64, encryptInfo *apis.SEncryptInfo) (jsonutils.JSONObject, error) {
  36. imageCacheManager := storageManager.GetStoragecacheById(d.Storage.GetStoragecacheId())
  37. return d.SLocalDisk.createFromTemplateAndResize(ctx, imageId, format, imageCacheManager, encryptInfo, size)
  38. }
  39. func (d *SNasDisk) CreateFromRemoteHostImage(ctx context.Context, url string, size int64, encryptInfo *apis.SEncryptInfo) error {
  40. return fmt.Errorf("Not implemented")
  41. }
  42. func (d *SNasDisk) CreateFromSnapshotLocation(ctx context.Context, snapshotLocation string, size int64, encryptInfo *apis.SEncryptInfo) (jsonutils.JSONObject, error) {
  43. snapshotPath := path.Join(d.Storage.GetPath(), snapshotLocation)
  44. newImg, err := qemuimg.NewQemuImage(d.GetPath())
  45. if err != nil {
  46. return nil, errors.Wrap(err, "new image from snapshot")
  47. }
  48. if newImg.IsValid() {
  49. if err := newImg.Delete(); err != nil {
  50. log.Errorln(err)
  51. return nil, err
  52. }
  53. }
  54. if encryptInfo != nil {
  55. err = newImg.CreateQcow2(0, false, snapshotPath, encryptInfo.Key, qemuimg.EncryptFormatLuks, encryptInfo.Alg)
  56. } else {
  57. err = newImg.CreateQcow2(0, false, snapshotPath, "", "", "")
  58. }
  59. if err != nil {
  60. return nil, errors.Wrap(err, "create image from snapshot")
  61. }
  62. retSize, _ := d.GetDiskDesc().Int("disk_size")
  63. log.Infof("REQSIZE: %d, RETSIZE: %d", size, retSize)
  64. if size > retSize {
  65. params := new(SDiskResizeInput)
  66. diskInfo := jsonutils.NewDict()
  67. diskInfo.Set("size", jsonutils.NewInt(size))
  68. if encryptInfo != nil {
  69. diskInfo.Set("encrypt_info", jsonutils.Marshal(encryptInfo))
  70. }
  71. params.DiskInfo = diskInfo
  72. return d.Resize(ctx, params)
  73. }
  74. return d.GetDiskDesc(), nil
  75. }
  76. func (d *SNasDisk) ResetFromSnapshot(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  77. resetParams, ok := params.(*SDiskReset)
  78. if !ok {
  79. return nil, hostutils.ParamsError
  80. }
  81. outOfChain, err := resetParams.Input.Bool("out_of_chain")
  82. if err != nil {
  83. return nil, httperrors.NewMissingParameterError("out_of_chain")
  84. }
  85. location, err := resetParams.Input.GetString("location")
  86. if err != nil {
  87. return nil, httperrors.NewMissingParameterError("location")
  88. }
  89. snapshotPath := path.Join(d.Storage.GetPath(), location)
  90. log.Infof("Snapshot path is %s", snapshotPath)
  91. var encryptInfo *apis.SEncryptInfo
  92. if resetParams.Input.Contains("encrypt_info") {
  93. encInfo := apis.SEncryptInfo{}
  94. err := resetParams.Input.Unmarshal(&encInfo, "encrypt_info")
  95. if err != nil {
  96. log.Errorf("unmarshal encrypt_info fail %s", err)
  97. } else {
  98. encryptInfo = &encInfo
  99. }
  100. }
  101. return d.resetFromSnapshot(snapshotPath, outOfChain, encryptInfo)
  102. }
  103. func (d *SNasDisk) GetSnapshotLocation() string {
  104. return d.GetSnapshotDir()[len(d.Storage.GetPath())+1:]
  105. }
  106. type SNFSDisk struct {
  107. SNasDisk
  108. }
  109. func NewNFSDisk(storage IStorage, id string) *SNFSDisk {
  110. return &SNFSDisk{
  111. SNasDisk: *NewNasDisk(storage, id),
  112. }
  113. }
  114. func (d *SNFSDisk) GetType() string {
  115. return api.STORAGE_NFS
  116. }
  117. type SGPFSDisk struct {
  118. SNasDisk
  119. }
  120. func NewGPFSDisk(storage IStorage, id string) *SGPFSDisk {
  121. return &SGPFSDisk{
  122. SNasDisk: *NewNasDisk(storage, id),
  123. }
  124. }
  125. func (d *SGPFSDisk) GetType() string {
  126. return api.STORAGE_GPFS
  127. }