disk_agent.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "yunion.io/x/cloudmux/pkg/multicloud/esxi"
  18. "yunion.io/x/cloudmux/pkg/multicloud/esxi/vcenter"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/apis/compute"
  23. deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
  24. "yunion.io/x/onecloud/pkg/hostman/hostdeployer/deployclient"
  25. "yunion.io/x/onecloud/pkg/hostman/hostutils"
  26. "yunion.io/x/onecloud/pkg/httperrors"
  27. )
  28. type SAgentDisk struct {
  29. SLocalDisk
  30. }
  31. func NewAgentDisk(storage IStorage, id string) *SAgentDisk {
  32. return &SAgentDisk{*NewLocalDisk(storage, id)}
  33. }
  34. type PrepareSaveToGlanceParams struct {
  35. TaskId string
  36. DiskInfo jsonutils.JSONObject
  37. }
  38. func (sd *SAgentDisk) PrepareSaveToGlance(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
  39. p, ok := params.(PrepareSaveToGlanceParams)
  40. if !ok {
  41. return nil, errors.Wrap(hostutils.ParamsError, "Resize params format error")
  42. }
  43. storage := sd.Storage.(*SAgentStorage)
  44. return storage.PrepareSaveToGlance(ctx, p.TaskId, p.DiskInfo)
  45. }
  46. func (sd *SAgentDisk) Resize(ctx context.Context, params *SDiskResizeInput) (jsonutils.JSONObject, error) {
  47. body := params.DiskInfo
  48. type sResize struct {
  49. SizeMb int64 `json:"size_mb"`
  50. HostInfo vcenter.SVCenterAccessInfo
  51. VMId string `json:"vm_id"`
  52. DiskId string `json:"disk_id"`
  53. }
  54. resize := sResize{}
  55. err := body.Unmarshal(&resize)
  56. if err != nil {
  57. return nil, errors.Wrapf(err, "%s: unmarshal to sResize", hostutils.ParamsError)
  58. }
  59. esxiClient, err := esxi.NewESXiClientFromAccessInfo(ctx, &resize.HostInfo)
  60. if err != nil {
  61. return nil, httperrors.NewInputParameterError("info of host_info error")
  62. }
  63. host, err := esxiClient.FindHostByIp(resize.HostInfo.PrivateId)
  64. if err != nil {
  65. return nil, errors.Wrapf(err, "fail to find host by ip %s", resize.HostInfo.PrivateId)
  66. }
  67. ivm, err := host.GetIVMById(resize.VMId)
  68. if err != nil {
  69. return nil, errors.Wrapf(err, "fail to find vm by ID %s", resize.VMId)
  70. }
  71. vm := ivm.(*esxi.SVirtualMachine)
  72. iDisk, err := vm.GetIDiskById(resize.DiskId)
  73. if err != nil {
  74. return nil, errors.Wrapf(err, "fail to get idisk %q of vm %s", resize.DiskId, resize.VMId)
  75. }
  76. disk := iDisk.(*esxi.SVirtualDisk)
  77. err = disk.Resize(ctx, resize.SizeMb)
  78. if err != nil {
  79. return nil, errors.Wrapf(err, "unable to resize to %dMB", resize.SizeMb)
  80. }
  81. desc := jsonutils.NewDict()
  82. desc.Add(jsonutils.NewInt(resize.SizeMb), "disk_size")
  83. // try to resize partition
  84. err = resizeESXiPartition(ctx, vm, iDisk.(*esxi.SVirtualDisk), resize.HostInfo)
  85. if err != nil {
  86. log.Errorf("unable to ResizePartition: %v", err)
  87. }
  88. return desc, nil
  89. }
  90. func resizeESXiPartition(ctx context.Context, vm *esxi.SVirtualMachine, disk *esxi.SVirtualDisk, accessInfo vcenter.SVCenterAccessInfo) error {
  91. diskPath := disk.GetFilename()
  92. vmref := vm.GetMoid()
  93. diskInfo := deployapi.DiskInfo{
  94. Path: diskPath,
  95. }
  96. vddkInfo := deployapi.VDDKConInfo{
  97. Host: accessInfo.Host,
  98. Port: int32(accessInfo.Port),
  99. User: accessInfo.Account,
  100. Passwd: accessInfo.Password,
  101. Vmref: vmref,
  102. }
  103. _, err := deployclient.GetDeployClient().ResizeFs(ctx, &deployapi.ResizeFsParams{
  104. DiskInfo: &diskInfo,
  105. Hypervisor: compute.HYPERVISOR_ESXI,
  106. VddkInfo: &vddkInfo,
  107. })
  108. if err != nil {
  109. return errors.Wrap(err, "unable to ResizeFs")
  110. }
  111. return nil
  112. }