fix(esxi): rebuild root by template (#15825)

This commit is contained in:
屈轩
2023-01-18 23:35:50 +08:00
committed by GitHub
parent 3db8a61d16
commit dab791c50b
8 changed files with 54 additions and 24 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ require (
k8s.io/client-go v0.19.3
k8s.io/cluster-bootstrap v0.19.3
moul.io/http2curl/v2 v2.3.0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230114081825-c673c4f83be3
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230118073927-53e52d3127cb
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656
yunion.io/x/jsonutils v1.0.1-0.20220819091305-3bab322ab4fd
yunion.io/x/log v1.0.0
+2 -2
View File
@@ -1158,8 +1158,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230114081825-c673c4f83be3 h1:UfALzdMyOysQkuEVLEjb2Je7sO0XzKWiOFQJtXzhgGA=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230114081825-c673c4f83be3/go.mod h1:yEMFtIKPyIdIfPzsw7oTcH41JXc8G5StUAQw3ZoGj5I=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230118073927-53e52d3127cb h1:kyCp49u5lWCqhmLLC17KyszRAt92YNtaLa5i5yucKT0=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230118073927-53e52d3127cb/go.mod h1:yEMFtIKPyIdIfPzsw7oTcH41JXc8G5StUAQw3ZoGj5I=
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656 h1:0zlZD5uhZoIHgLVAWCz2aHaYk2ZrNsACCYD7R6EIBII=
yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
+19 -4
View File
@@ -100,6 +100,9 @@ func (as *SAgentStorage) agentRebuildRoot(ctx context.Context, data jsonutils.JS
Disks []struct {
DiskId string
ImagePath string
ImageInfo struct {
ImageExternalId string
}
}
}
}
@@ -120,11 +123,23 @@ func (as *SAgentStorage) agentRebuildRoot(ctx context.Context, data jsonutils.JS
return errors.Wrap(hostutils.ParamsError, "agentRebuildRoot data.desc.disks is empty")
}
imagePath := rp.Desc.Disks[0].ImagePath
diskId := rp.Desc.Disks[0].DiskId
newPath, err := host.FileUrlPathToDsPath(imagePath)
if err != nil {
return err
var newPath string
if len(imagePath) == 0 {
vm, err := host.SearchTemplateVM(rp.Desc.Disks[0].ImageInfo.ImageExternalId)
if err != nil {
return errors.Wrapf(err, "SearchTemplateVM(%s)", rp.Desc.Disks[0].ImageInfo.ImageExternalId)
}
newPath, err = vm.GetRootImagePath()
if err != nil {
return errors.Wrapf(err, "GetRootImagePath")
}
} else {
newPath, err = host.FileUrlPathToDsPath(imagePath)
if err != nil {
return err
}
}
diskId := rp.Desc.Disks[0].DiskId
vm := ivm.(*esxi.SVirtualMachine)
return vm.DoRebuildRoot(ctx, newPath, diskId)
}
+1 -1
View File
@@ -1439,7 +1439,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.2.0
## explicit; go 1.12
sigs.k8s.io/yaml
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230114081825-c673c4f83be3
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230118073927-53e52d3127cb
## explicit; go 1.18
yunion.io/x/cloudmux/pkg/apis
yunion.io/x/cloudmux/pkg/apis/billing
+4
View File
@@ -770,6 +770,10 @@ func (self *SHost) CreateVM2(ctx context.Context, ds *SDatastore, params SCreate
return self.DoCreateVM(ctx, ds, params)
}
func (self *SHost) SearchTemplateVM(id string) (*SVirtualMachine, error) {
return self.manager.SearchTemplateVM(id)
}
func (self *SHost) needScsi(disks []SDiskInfo) bool {
if len(disks) == 0 {
return false
+2 -3
View File
@@ -229,10 +229,9 @@ func (cli *SESXiClient) connect() error {
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
IdleConnTimeout: time.Minute * 1,
TLSHandshakeTimeout: time.Minute * 1,
ResponseHeaderTimeout: time.Minute * 1,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 5 * time.Second,
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
}
httpClient.Transport = cloudprovider.GetCheckTransport(transport, func(req *http.Request) (func(resp *http.Response), error) {
+1 -1
View File
@@ -810,7 +810,7 @@ func (self *SDatastore) Delete2(ctx context.Context, remotePath string, isNamesp
// Ignore error
return nil
}
return err
return errors.Wrapf(err, "remove %s", remotePath)
}
func (self *SDatastore) Delete(ctx context.Context, remotePath string) error {
+24 -12
View File
@@ -224,7 +224,7 @@ func (self *SVirtualMachine) RebuildRoot(ctx context.Context, desc *cloudprovide
func (self *SVirtualMachine) DoRebuildRoot(ctx context.Context, imagePath string, uuid string) error {
if len(self.vdisks) == 0 {
return errors.ErrNotFound
return errors.Wrapf(errors.ErrNotFound, "empty vdisks")
}
return self.rebuildDisk(ctx, &self.vdisks[0], imagePath)
}
@@ -623,14 +623,12 @@ func (self *SVirtualMachine) doDetachDisk(ctx context.Context, vdisk *SVirtualDi
task, err := vm.Reconfigure(ctx, spec)
if err != nil {
log.Errorf("vm.Reconfigure fail %s", err)
return err
return errors.Wrapf(err, "Reconfigure remove disk %s", vdisk.GetName())
}
err = task.Wait(ctx)
if err != nil {
log.Errorf("task.Wait(ctx) fail %s", err)
return err
return errors.Wrapf(err, "wait remove disk %s task", vdisk.GetName())
}
if !remove {
@@ -1053,17 +1051,17 @@ func (self *SVirtualMachine) createDriverAndDisk(ctx context.Context, ds *SDatas
}, true)
}
func (self *SVirtualMachine) copyRootDisk(ctx context.Context, imagePath string) (string, error) {
func (self *SVirtualMachine) getDatastoreAndRootImagePath() (string, *SDatastore, error) {
layoutEx := self.getLayoutEx()
if layoutEx == nil || len(layoutEx.File) == 0 {
return "", fmt.Errorf("invalid LayoutEx")
return "", nil, fmt.Errorf("invalid LayoutEx")
}
file := layoutEx.File[0].Name
// find stroage
host := self.GetIHost()
storages, err := host.GetIStorages()
if err != nil {
return "", errors.Wrap(err, "host.GetIStorages")
return "", nil, errors.Wrap(err, "host.GetIStorages")
}
var datastore *SDatastore
for i := range storages {
@@ -1074,17 +1072,31 @@ func (self *SVirtualMachine) copyRootDisk(ctx context.Context, imagePath string)
}
}
if datastore == nil {
return "", fmt.Errorf("can't find storage associated with vm %q", self.GetName())
return "", nil, fmt.Errorf("can't find storage associated with vm %q", self.GetName())
}
path := datastore.cleanPath(file)
vmDir := strings.Split(path, "/")[0]
// TODO find a non-conflicting path
newImagePath := datastore.getPathString(fmt.Sprintf("%s/%s.vmdk", vmDir, vmDir))
return datastore.getPathString(fmt.Sprintf("%s/%s.vmdk", vmDir, vmDir)), datastore, nil
}
func (self *SVirtualMachine) GetRootImagePath() (string, error) {
path, _, err := self.getDatastoreAndRootImagePath()
if err != nil {
return "", err
}
return path, nil
}
func (self *SVirtualMachine) CopyRootDisk(ctx context.Context, imagePath string) (string, error) {
newImagePath, datastore, err := self.getDatastoreAndRootImagePath()
if err != nil {
return "", errors.Wrapf(err, "GetRootImagePath")
}
fm := datastore.getDatastoreObj().NewFileManager(datastore.datacenter.getObjectDatacenter(), true)
err = fm.Copy(ctx, imagePath, newImagePath)
if err != nil {
return "", errors.Wrap(err, "unable to copy system disk")
return "", errors.Wrapf(err, "unable to copy system disk %s -> %s", imagePath, newImagePath)
}
return newImagePath, nil
}
@@ -1094,7 +1106,7 @@ func (self *SVirtualMachine) createDiskWithDeviceChange(ctx context.Context, dev
// copy disk
if len(config.ImagePath) > 0 {
config.IsRoot = true
config.ImagePath, err = self.copyRootDisk(ctx, config.ImagePath)
config.ImagePath, err = self.CopyRootDisk(ctx, config.ImagePath)
if err != nil {
return errors.Wrap(err, "unable to copyRootDisk")
}