Skip to content

Commit e086dfe

Browse files
committed
fix: generate negative device key in AssignController
Closes: #2881 Signed-off-by: syuparn <[email protected]>
1 parent 7556da8 commit e086dfe

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

object/virtual_device_list.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,7 @@ func EthernetCardTypes() VirtualDeviceList {
6969
&types.VirtualSriovEthernetCard{},
7070
}).Select(func(device types.BaseVirtualDevice) bool {
7171
c := device.(types.BaseVirtualEthernetCard).GetVirtualEthernetCard()
72-
73-
key := rand.Int31() * -1
74-
if key == 0 {
75-
key = -1
76-
}
77-
78-
c.GetVirtualDevice().Key = key
72+
c.GetVirtualDevice().Key = VirtualDeviceList{}.newRandomKey()
7973
return true
8074
})
8175
}
@@ -463,10 +457,22 @@ func (l VirtualDeviceList) AssignController(device types.BaseVirtualDevice, c ty
463457
d.UnitNumber = new(int32)
464458
*d.UnitNumber = l.newUnitNumber(c)
465459
if d.Key == 0 {
466-
d.Key = int32(rand.Uint32()) * -1
460+
d.Key = l.newRandomKey()
467461
}
468462
}
469463

464+
// newRandomKey returns a random negative device key.
465+
// The generated key can be used for devices you want to add so that it does not collide with existing ones.
466+
func (l VirtualDeviceList) newRandomKey() int32 {
467+
// NOTE: rand.Uint32 cannot be used here because conversion from uint32 to int32 may change the sign
468+
key := rand.Int31() * -1
469+
if key == 0 {
470+
return -1
471+
}
472+
473+
return key
474+
}
475+
470476
// CreateDisk creates a new VirtualDisk device which can be added to a VM.
471477
func (l VirtualDeviceList) CreateDisk(c types.BaseVirtualController, ds types.ManagedObjectReference, name string) *types.VirtualDisk {
472478
// If name is not specified, one will be chosen for you.

object/virtual_device_list_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,29 @@ func TestPickController(t *testing.T) {
723723
}
724724
}
725725

726+
func TestAssignController(t *testing.T) {
727+
scsi, _ := devices.CreateSCSIController("scsi")
728+
disk := &types.VirtualDisk{
729+
CapacityInBytes: 512 * 1024,
730+
VirtualDevice: types.VirtualDevice{
731+
Backing: new(types.VirtualDiskFlatVer2BackingInfo), // Leave fields empty to test defaults
732+
},
733+
}
734+
735+
devices := VirtualDeviceList([]types.BaseVirtualDevice{scsi})
736+
devices.AssignController(disk, scsi.(*types.VirtualLsiLogicController))
737+
738+
if disk.ControllerKey != scsi.GetVirtualDevice().Key {
739+
t.Errorf("expected controller key: %d, got: %d\n", scsi.GetVirtualDevice().Key, disk.ControllerKey)
740+
}
741+
742+
// if disk does not have device key, AssignController gives a random negative key to the disk
743+
// so that it will not collide with existing device keys
744+
if disk.Key >= 0 {
745+
t.Errorf("device key %d should be negative", disk.Key)
746+
}
747+
}
748+
726749
func TestCreateSCSIController(t *testing.T) {
727750
for _, l := range []VirtualDeviceList{SCSIControllerTypes(), devices} {
728751
_, err := l.CreateSCSIController("enoent")

0 commit comments

Comments
 (0)