【问题标题】:vSphere, add existing HDD to VM via API(govmomi)vSphere,通过 API(govmomi) 将现有 HDD 添加到 VM
【发布时间】:2017-07-17 18:57:12
【问题描述】:

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

一开始我是这样从源虚拟机获取磁盘的:

for _, device := range devices {
        currentDeviceLabel := device.GetVirtualDevice().DeviceInfo.GetDescription().Label
        if strings.Contains(strings.ToLower(currentDeviceLabel), "hard disk"){
        disks = append(disks, device)
    }    
return disks

然后我尝试将收到的磁盘添加到其他 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()))

    }

我从 vSphere 收到错误:

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

我做错了什么?谢谢!

【问题讨论】:

    标签: go vmware vsphere


    【解决方案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()))
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      相关资源
      最近更新 更多