Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update controller's device list in AssignController #3588

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion object/virtual_device_list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015-2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -545,6 +545,8 @@ func (l VirtualDeviceList) AssignController(device types.BaseVirtualDevice, c ty
if d.Key == 0 {
d.Key = l.newRandomKey()
}

c.GetVirtualController().Device = append(c.GetVirtualController().Device, d.Key)
}

// newRandomKey returns a random negative device key.
Expand Down
8 changes: 7 additions & 1 deletion object/virtual_device_list_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -744,6 +744,12 @@ func TestAssignController(t *testing.T) {
if disk.Key >= 0 {
t.Errorf("device key %d should be negative", disk.Key)
}

// AssignController should add the disk to the controller's device list.
cd := scsi.(*types.VirtualLsiLogicController).Device[0]
if cd != disk.Key {
t.Errorf("expected controller device key: %d, got: %d\n", disk.Key, cd)
}
}

func TestCreateSCSIController(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,12 +1279,26 @@ func (vm *VirtualMachine) configureDevice(
d := device.GetVirtualDevice()
var controller types.BaseVirtualController

key := d.Key
if d.Key <= 0 {
// Keys can't be negative; Key 0 is reserved
d.Key = devices.NewKey()
d.Key *= -1
}

// Update device controller's key reference
if key != d.Key {
if device := devices.FindByKey(d.ControllerKey); device != nil {
c := device.(types.BaseVirtualController).GetVirtualController()
for i := range c.Device {
if c.Device[i] == key {
c.Device[i] = d.Key
break
}
}
}
}

// Choose a unique key
for {
if devices.FindByKey(d.Key) == nil {
Expand Down
Loading