vSphere,通过API将现有HDD添加到VM(govmomi)

时间:2017-07-17 18:57:12

标签: go vmware vsphere

我尝试将现有硬盘从一台虚拟机添加到其他虚拟机。 我使用golang和这个api:https://github.com/vmware/govmomi

首先,我从源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()))

    }

然后我尝试将收到的磁盘添加到其他VM:

Cannot complete the operation because the file or folder [xxxxx] xxxxx/xxxxx.vmdk already exists

我从vSphere收到错误:

&

我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:1)

我在这里得到了答案:https://github.com/vmware/govmomi/issues/790

工作代码:

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()))

}