From 564470de0ef9106cd67936aaa01761e096c6554f Mon Sep 17 00:00:00 2001 From: rainzm Date: Fri, 2 Apr 2021 17:19:35 +0800 Subject: [PATCH] fix(esxi): determine whether the disk is a root disk more reasonably The order of the device is often sorted according to the size of the key. The disk pointed to by the smallest key is not necessarily the root disk or the first disk which often has the label of 'Hard disk 1'. We try to find the label, if not, use the Key again to determine who is the root disk. --- pkg/multicloud/esxi/vdisk.go | 5 ++++- pkg/multicloud/esxi/virtualmachine.go | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/multicloud/esxi/vdisk.go b/pkg/multicloud/esxi/vdisk.go index 644ad5c016..8189ab404a 100644 --- a/pkg/multicloud/esxi/vdisk.go +++ b/pkg/multicloud/esxi/vdisk.go @@ -48,12 +48,15 @@ type SVirtualDisk struct { multicloud.SDisk SVirtualDevice + IsRoot bool } func NewVirtualDisk(vm *SVirtualMachine, dev types.BaseVirtualDevice, index int) SVirtualDisk { + isRoot := dev.GetVirtualDevice().DeviceInfo.GetDescription().Label == rootDiskMark return SVirtualDisk{ multicloud.SDisk{}, NewVirtualDevice(vm, dev, index), + isRoot, } } @@ -378,7 +381,7 @@ func (disk *SVirtualDisk) GetTemplateId() string { } func (disk *SVirtualDisk) GetDiskType() string { - if disk.index == 0 { + if disk.IsRoot { return api.DISK_TYPE_SYS } return api.DISK_TYPE_DATA diff --git a/pkg/multicloud/esxi/virtualmachine.go b/pkg/multicloud/esxi/virtualmachine.go index d73cdefb7f..39582a8fe0 100644 --- a/pkg/multicloud/esxi/virtualmachine.go +++ b/pkg/multicloud/esxi/virtualmachine.go @@ -254,7 +254,6 @@ func (self *SVirtualMachine) getIHost() cloudprovider.ICloudHost { func (self *SVirtualMachine) GetIDisks() ([]cloudprovider.ICloudDisk, error) { idisks := make([]cloudprovider.ICloudDisk, len(self.vdisks)) - sort.Sort(byDiskType(self.vdisks)) for i := 0; i < len(self.vdisks); i += 1 { idisks[i] = &(self.vdisks[i]) } @@ -770,13 +769,24 @@ func (self *SVirtualMachine) fetchHardwareInfo() error { vdev := NewVirtualDevice(self, dev, 0) self.devs[vdev.getKey()] = vdev } - // sort disk based on index - sort.Slice(self.vdisks, func(i, j int) bool { - return self.vdisks[i].GetIndex() < self.vdisks[j].GetIndex() - }) + self.rigorous() + sort.Sort(byDiskType(self.vdisks)) return nil } +func (self *SVirtualMachine) rigorous() { + hasRoot := false + for i := range self.vdisks { + if self.vdisks[i].IsRoot { + hasRoot = true + break + } + } + if !hasRoot && len(self.vdisks) > 0 { + self.vdisks[0].IsRoot = true + } +} + func (self *SVirtualMachine) getVdev(key int32) SVirtualDevice { return self.devs[key] }