I am try to add existing HDD from one virtual machine to other. I use golang and this api: https://github.com/vmware/govmomi
At first i get disks from source vm like this:
for _, device := range devices {
currentDeviceLabel := device.GetVirtualDevice().DeviceInfo.GetDescription().Label
if strings.Contains(strings.ToLower(currentDeviceLabel), "hard disk"){
disks = append(disks, device)
}
return disks
And then i trying to add received disk to other VM:
func addDisk(vm *object.VirtualMachine, disk types.BaseVirtualDevice) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
spec := types.VirtualMachineConfigSpec{
DeviceChange : []types.BaseVirtualDeviceConfigSpec {
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationAdd,
FileOperation: types.VirtualDeviceConfigSpecFileOperationCreate,
Device: disk,
},
},
}
result, err := vm.Reconfigure(ctx, spec)
if err != nil {
log.Fatal(fmt.Sprintf("err: %s", err.Error()))
}
I get error from vSphere:
Cannot complete the operation because the file or folder [xxxxx] xxxxx/xxxxx.vmdk already exists
What i am doing wrong? Thanks!
I got answer here: https://github.com/vmware/govmomi/issues/790
Working code:
spec := types.VirtualMachineConfigSpec{}
config := &types.VirtualDeviceConfigSpec{
Device: disk,
Operation: types.VirtualDeviceConfigSpecOperationAdd,
}
spec.DeviceChange = append(spec.DeviceChange, config)
result, err := vm.Reconfigure(ctx, spec)
if err != nil {
log.Fatal(fmt.Sprintf("err: %s", err.Error()))
}