disk.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 proxmox
  15. import (
  16. "context"
  17. "fmt"
  18. "net/url"
  19. "strings"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. "yunion.io/x/jsonutils"
  24. "yunion.io/x/pkg/errors"
  25. )
  26. type SDisk struct {
  27. multicloud.SDisk
  28. ProxmoxTags
  29. storage *SStorage
  30. Format string `json:"format"`
  31. Size int64 `json:"size"`
  32. Vmid string
  33. VolId string `json:"volid"`
  34. Name string `json:"name"`
  35. Parent string `json:"parent"`
  36. Content string `json:"content"`
  37. }
  38. func (self *SDisk) GetName() string {
  39. if len(self.Name) > 0 {
  40. return self.Name
  41. }
  42. info := strings.Split(self.VolId, ":")
  43. if len(info) == 2 {
  44. return info[1]
  45. }
  46. return self.VolId
  47. }
  48. func (self *SDisk) GetId() string {
  49. return self.VolId
  50. }
  51. func (self *SDisk) GetGlobalId() string {
  52. if self.storage.Shared == 1 {
  53. return fmt.Sprintf("%s|%s", self.storage.Storage, self.VolId)
  54. }
  55. return fmt.Sprintf("%s|%s|%s", self.storage.Node, self.storage.Storage, self.VolId)
  56. }
  57. func (self *SDisk) CreateISnapshot(ctx context.Context, name, desc string) (cloudprovider.ICloudSnapshot, error) {
  58. return nil, cloudprovider.ErrNotSupported
  59. }
  60. func (self *SDisk) Refresh() error {
  61. disks, err := self.storage.zone.region.GetDisks(self.storage.Node, self.storage.Storage)
  62. if err != nil {
  63. return err
  64. }
  65. for i := range disks {
  66. disks[i].storage = self.storage
  67. if disks[i].GetGlobalId() == self.GetGlobalId() {
  68. return jsonutils.Update(self, disks[i])
  69. }
  70. }
  71. return errors.Wrapf(cloudprovider.ErrNotFound, "%s", self.VolId)
  72. }
  73. func (self *SDisk) Delete(ctx context.Context) error {
  74. return cloudprovider.ErrNotImplemented
  75. }
  76. func (self *SDisk) GetCacheMode() string {
  77. return "none"
  78. }
  79. func (self *SDisk) GetFsFormat() string {
  80. return ""
  81. }
  82. func (self *SDisk) GetIsNonPersistent() bool {
  83. return false
  84. }
  85. func (self *SDisk) GetDriver() string {
  86. return "virto"
  87. }
  88. func (self *SDisk) GetDiskType() string {
  89. return api.DISK_TYPE_DATA
  90. }
  91. func (self *SDisk) GetDiskFormat() string {
  92. return strings.ToLower(self.Format)
  93. }
  94. func (self *SDisk) GetDiskSizeMB() int {
  95. return int(self.Size / 1024 / 1024)
  96. }
  97. func (self *SDisk) GetIsAutoDelete() bool {
  98. return true
  99. }
  100. func (self *SDisk) GetMountpoint() string {
  101. return ""
  102. }
  103. func (self *SDisk) GetStatus() string {
  104. return api.DISK_READY
  105. }
  106. func (self *SDisk) Rebuild(ctx context.Context) error {
  107. return cloudprovider.ErrNotSupported
  108. }
  109. func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
  110. return "", cloudprovider.ErrNotSupported
  111. }
  112. func (self *SDisk) Resize(ctx context.Context, sizeMb int64) error {
  113. vm, err := self.storage.zone.region.GetInstance(self.Vmid)
  114. if err != nil {
  115. return errors.Wrapf(err, "GetInstance")
  116. }
  117. for _storageName, disks := range vm.QemuDisks {
  118. if _storageName != self.storage.Storage {
  119. continue
  120. }
  121. for _, disk := range disks {
  122. if disk.DiskId != self.VolId {
  123. continue
  124. }
  125. return self.storage.zone.region.ResizeDisk(vm.Node, self.Vmid, disk.Driver, int(sizeMb-int64(self.GetDiskSizeMB()))/1024)
  126. }
  127. }
  128. return errors.Wrapf(cloudprovider.ErrNotFound, "%s", self.VolId)
  129. }
  130. func (self *SDisk) GetTemplateId() string {
  131. return ""
  132. }
  133. func (self *SDisk) GetAccessPath() string {
  134. return ""
  135. }
  136. func (self *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
  137. return self.storage, nil
  138. }
  139. func (self *SDisk) GetISnapshot(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
  140. return nil, cloudprovider.ErrNotSupported
  141. }
  142. func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  143. return []cloudprovider.ICloudSnapshot{}, nil
  144. }
  145. func (self *SRegion) GetDisks(node, storageName string) ([]SDisk, error) {
  146. vols := []SDisk{}
  147. params := url.Values{}
  148. params.Set("content", "images")
  149. res := fmt.Sprintf("/nodes/%s/storage/%s/content", node, storageName)
  150. err := self.get(res, params, &vols)
  151. if err != nil {
  152. return nil, err
  153. }
  154. return vols, nil
  155. }
  156. func (self *SRegion) ResizeDisk(node string, vmId string, driver string, sizeGb int) error {
  157. body := map[string]interface{}{
  158. "disk": driver,
  159. "size": fmt.Sprintf("+%dG", sizeGb),
  160. }
  161. res := fmt.Sprintf("/nodes/%s/qemu/%s/resize", node, vmId)
  162. return self.put(res, nil, jsonutils.Marshal(body))
  163. }