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.
This commit is contained in:
rainzm
2021-04-02 18:35:35 +08:00
parent f71f1d8318
commit 564470de0e
2 changed files with 19 additions and 6 deletions
+4 -1
View File
@@ -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
+15 -5
View File
@@ -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]
}