From 1ea7937cdd6fc63cdf7309d45ace8c667d611746 Mon Sep 17 00:00:00 2001 From: rainzm Date: Wed, 20 May 2020 15:33:13 +0800 Subject: [PATCH] fix(esxi): Separate detach disk and delete disk when rebuilding. In previous versions, set `removeSpec.FileOperation = types.VirtualDeviceConfigSpecFileOperationDestroy' to delete disk indirectly. But, when its parent has only one child, the parent will be deleted along with it. And the consequence is failure to reinstall the system. Now, detach disk without deleteing backing file and then remove backing --- pkg/multicloud/esxi/storage.go | 22 ++++++++++++++++++++++ pkg/multicloud/esxi/vdisk.go | 2 +- pkg/multicloud/esxi/virtualmachine.go | 9 ++++----- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pkg/multicloud/esxi/storage.go b/pkg/multicloud/esxi/storage.go index 76e34f590e..4e451eb6ce 100644 --- a/pkg/multicloud/esxi/storage.go +++ b/pkg/multicloud/esxi/storage.go @@ -610,6 +610,28 @@ func (self *SDatastore) FilePutContent(ctx context.Context, remotePath string, c return self.Upload(ctx, remotePath, strings.NewReader(content)) } +// Delete2 can delete file from this Datastore. +// isNamespace: remotePath is uuid of namespace on vsan datastore +// force: ignore nonexistent files and arguments +func (self *SDatastore) Delete2(ctx context.Context, remotePath string, isNamespace, force bool) error { + var err error + ds := self.getDatastoreObj() + dc := self.datacenter.getObjectDatacenter() + if isNamespace { + nm := object.NewDatastoreNamespaceManager(self.manager.client.Client) + err = nm.DeleteDirectory(ctx, dc, remotePath) + } else { + fm := ds.NewFileManager(dc, force) + err = fm.Delete(ctx, remotePath) + } + + if err != nil && types.IsFileNotFound(err) && force { + // Ignore error + return nil + } + return err +} + func (self *SDatastore) Delete(ctx context.Context, remotePath string) error { url := self.GetPathUrl(remotePath) diff --git a/pkg/multicloud/esxi/vdisk.go b/pkg/multicloud/esxi/vdisk.go index 74965e9574..a753b4fa01 100644 --- a/pkg/multicloud/esxi/vdisk.go +++ b/pkg/multicloud/esxi/vdisk.go @@ -296,7 +296,7 @@ func (disk *SVirtualDisk) Delete(ctx context.Context) error { return err } ds := istorage.(*SDatastore) - return ds.DeleteVmdk(ctx, disk.getBackingInfo().GetFileName()) + return ds.Delete2(ctx, disk.getBackingInfo().GetFileName(), false, false) } func (disk *SVirtualDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) { diff --git a/pkg/multicloud/esxi/virtualmachine.go b/pkg/multicloud/esxi/virtualmachine.go index 9adaa95037..ce6b265376 100644 --- a/pkg/multicloud/esxi/virtualmachine.go +++ b/pkg/multicloud/esxi/virtualmachine.go @@ -547,10 +547,6 @@ func (self *SVirtualMachine) doDetachDisk(ctx context.Context, vdisk *SVirtualDi removeSpec.Operation = types.VirtualDeviceConfigSpecOperationRemove removeSpec.Device = vdisk.dev - if remove { - removeSpec.FileOperation = types.VirtualDeviceConfigSpecFileOperationDestroy - } - spec := types.VirtualMachineConfigSpec{} spec.DeviceChange = []types.BaseVirtualDeviceConfigSpec{&removeSpec} @@ -568,7 +564,10 @@ func (self *SVirtualMachine) doDetachDisk(ctx context.Context, vdisk *SVirtualDi return err } - return nil + if !remove { + return nil + } + return vdisk.Delete(ctx) } func (self *SVirtualMachine) GetVNCInfo() (jsonutils.JSONObject, error) {