Skip to content

Commit 93c2afd

Browse files
committed
vcsim: copy device list when cloning a VM
The vcsim CloneVM_Task method was sharing the config.hardware.device property between the source and target VMs. The target VM now has a copy of the device list so changes to it won't change the source VM's device list. Closes #2706
1 parent 59582c4 commit 93c2afd

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

govc/test/vm.bats

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,16 @@ load test_helper
818818
backing=$(govc device.info -json -vm "$clone" disk-* | jq .Devices[].Backing)
819819
assert_equal true "$(jq .EagerlyScrub <<<"$backing")"
820820
assert_equal false "$(jq .ThinProvisioned <<<"$backing")"
821+
822+
# test that each vm has a unique vmdk path
823+
for item in FileName Uuid; do
824+
items=$(govc object.collect -json -type m / config.hardware.device | \
825+
jq ".ChangeSet[].Val.VirtualDevice[].Backing.$item | select(. != null)")
826+
827+
nitems=$(wc -l <<<"$items")
828+
uitems=$(sort -u <<<"$items" | wc -l)
829+
assert_equal "$nitems" "$uitems"
830+
done
821831
}
822832

823833
@test "vm.clone change resources" {

simulator/virtual_machine.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,15 @@ type vmFolder interface {
17691769
CreateVMTask(ctx *Context, c *types.CreateVM_Task) soap.HasFault
17701770
}
17711771

1772+
func (vm *VirtualMachine) cloneDevice() []types.BaseVirtualDevice {
1773+
src := types.ArrayOfVirtualDevice{
1774+
VirtualDevice: vm.Config.Hardware.Device,
1775+
}
1776+
dst := types.ArrayOfVirtualDevice{}
1777+
deepCopy(src, &dst)
1778+
return dst.VirtualDevice
1779+
}
1780+
17721781
func (vm *VirtualMachine) CloneVMTask(ctx *Context, req *types.CloneVM_Task) soap.HasFault {
17731782
pool := req.Spec.Location.Pool
17741783
if pool == nil {
@@ -1827,7 +1836,8 @@ func (vm *VirtualMachine) CloneVMTask(ctx *Context, req *types.CloneVM_Task) soa
18271836
config.VirtualSMCPresent = vm.Config.Hardware.VirtualSMCPresent
18281837

18291838
defaultDevices := object.VirtualDeviceList(esx.VirtualDevice)
1830-
devices := vm.Config.Hardware.Device
1839+
devices := vm.cloneDevice()
1840+
18311841
for _, device := range devices {
18321842
var fop types.VirtualDeviceConfigSpecFileOperation
18331843

0 commit comments

Comments
 (0)