imagecache_rbd.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "sync"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/httputils"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  25. "yunion.io/x/onecloud/pkg/hostman/options"
  26. "yunion.io/x/onecloud/pkg/hostman/storageman/remotefile"
  27. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  28. "yunion.io/x/onecloud/pkg/util/procutils"
  29. "yunion.io/x/onecloud/pkg/util/qemuimg"
  30. "yunion.io/x/onecloud/pkg/util/qemutils"
  31. )
  32. type SRbdImageCache struct {
  33. imageId string
  34. imageName string
  35. cond *sync.Cond
  36. Manager IImageCacheManger
  37. }
  38. func NewRbdImageCache(imageId string, imagecacheManager IImageCacheManger) *SRbdImageCache {
  39. imageCache := new(SRbdImageCache)
  40. imageCache.imageId = imageId
  41. imageCache.Manager = imagecacheManager
  42. imageCache.cond = sync.NewCond(new(sync.Mutex))
  43. return imageCache
  44. }
  45. func (r *SRbdImageCache) GetName() string {
  46. imageCacheManger := r.Manager.(*SRbdImageCacheManager)
  47. return fmt.Sprintf("%s%s", imageCacheManger.Prefix, r.imageId)
  48. }
  49. func (r *SRbdImageCache) GetPath() string {
  50. imageCacheManger := r.Manager.(*SRbdImageCacheManager)
  51. storage := imageCacheManger.storage.(*SRbdStorage)
  52. return fmt.Sprintf("rbd:%s/%s%s", imageCacheManger.GetPath(), r.GetName(), storage.getStorageConfString())
  53. }
  54. func (r *SRbdImageCache) Load() error {
  55. log.Debugf("loading rbd imagecache %s", r.GetPath())
  56. origin, err := qemuimg.NewQemuImage(r.GetPath())
  57. if err != nil {
  58. return errors.Wrapf(err, "NewQemuImage at host %s", options.HostOptions.Hostname)
  59. }
  60. if origin.IsValid() {
  61. return nil
  62. }
  63. return fmt.Errorf("invalid rbd image %s at host %s", origin.String(), options.HostOptions.Hostname)
  64. }
  65. func (r *SRbdImageCache) Acquire(ctx context.Context, input api.CacheImageInput, callback func(progress, progressMbps float64, totalSizeMb int64)) error {
  66. if err := r.Load(); err == nil {
  67. log.Infof("rbd image %s has been cached at pool %s", r.imageId, r.Manager.GetPath())
  68. r.imageName = r.imageId
  69. return nil
  70. }
  71. input.ImageId = r.imageId
  72. localImageCache, err := storageManager.LocalStorageImagecacheManager.AcquireImage(ctx, input, func(progress, progressMbps float64, totalSizeMb int64) {
  73. if len(input.ServerId) > 0 {
  74. hostutils.UpdateServerProgress(context.Background(), input.ServerId, progress/1.2, progressMbps)
  75. }
  76. })
  77. if err != nil {
  78. return errors.Wrapf(err, "LocalStorage.AcquireImage")
  79. }
  80. r.imageName = localImageCache.GetName()
  81. if r.Load() != nil {
  82. log.Infof("convert local image %s to rbd pool %s", r.imageId, r.Manager.GetPath())
  83. err := procutils.NewRemoteCommandAsFarAsPossible(qemutils.GetQemuImg(),
  84. "convert", "-W", "-m", "16", "-O", "raw", localImageCache.GetPath(), r.GetPath()).Run()
  85. if err != nil {
  86. return errors.Wrapf(err, "convert local image %s to rbd pool %s at host %s", r.imageId, r.Manager.GetPath(), options.HostOptions.Hostname)
  87. }
  88. if len(input.ServerId) > 0 {
  89. modules.Servers.Update(hostutils.GetComputeSession(context.Background()), input.ServerId, jsonutils.Marshal(map[string]float32{"progress": 100.0}))
  90. }
  91. }
  92. return r.Load()
  93. }
  94. func (r *SRbdImageCache) Release() {
  95. return
  96. }
  97. func (r *SRbdImageCache) Remove(ctx context.Context) error {
  98. imageCacheManger := r.Manager.(*SRbdImageCacheManager)
  99. cli, err := imageCacheManger.getCephClient()
  100. if err != nil {
  101. return errors.Wrap(err, "getCephClient")
  102. }
  103. img, err := cli.GetImage(r.GetName())
  104. if err != nil {
  105. if errors.Cause(err) == errors.ErrNotFound {
  106. return nil
  107. }
  108. return errors.Wrapf(err, "GetImage")
  109. }
  110. err = img.Delete()
  111. if err != nil {
  112. return errors.Wrap(err, "Delete")
  113. }
  114. go func() {
  115. _, err := modules.Storagecachedimages.Detach(hostutils.GetComputeSession(ctx),
  116. r.Manager.GetId(), r.imageId, nil)
  117. if err != nil && httputils.ErrorCode(err) != 404 {
  118. log.Errorf("Fail to delete host cached image: %s", err)
  119. }
  120. }()
  121. return nil
  122. }
  123. func (r *SRbdImageCache) GetDesc() *remotefile.SImageDesc {
  124. imageCacheManger := r.Manager.(*SRbdImageCacheManager)
  125. desc := &remotefile.SImageDesc{
  126. SizeMb: -1,
  127. Name: r.imageName,
  128. }
  129. cli, err := imageCacheManger.getCephClient()
  130. if err != nil {
  131. log.Errorf("getCephClient fail %s", err)
  132. return desc
  133. }
  134. img, err := cli.GetImage(r.GetName())
  135. if err != nil {
  136. log.Errorf("getName fail %s", err)
  137. return desc
  138. }
  139. info, err := img.GetInfo()
  140. if err != nil {
  141. log.Errorf("GetInfo fail %s", err)
  142. return desc
  143. }
  144. desc.SizeMb = info.SizeByte / 1024 / 1024
  145. desc.AccessAt = info.AccessTimestamp
  146. return desc
  147. }
  148. func (r *SRbdImageCache) GetImageId() string {
  149. return r.imageId
  150. }
  151. func (r *SRbdImageCache) GetAccessDirectory() (string, error) {
  152. return "", errors.ErrNotImplemented
  153. }