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

Add existing HDD to VM #790

Closed
byumov opened this issue Jul 18, 2017 · 2 comments
Closed

Add existing HDD to VM #790

byumov opened this issue Jul 18, 2017 · 2 comments

Comments

@byumov
Copy link

byumov commented Jul 18, 2017

Hi! Sorry for i am asking here =\

I want to add existind HDD from one VM to another.

I am trying something like this. Get disks:

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 try connect received disk to another 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: Cannot complete the operation because the file or folder [xxxxxx] xxxxxx/xxxxxx.vmdk already exists
How i can add existing disk? Thanks!

@dougm
Copy link
Member

dougm commented Jul 18, 2017

Just don't specify FileOperation if the disk already exists.

The govc vm.disk.attach command does this using the VirtualDeviceList helpers: https://github.com/vmware/govmomi/blob/toolbox-archive/govc/vm/disk/attach.go#L64

In that command, CapacityInKB == 0, but in vm.disk.create CapacityInKB > 0:
https://github.com/vmware/govmomi/blob/master/object/virtual_machine.go#L390

@byumov
Copy link
Author

byumov commented Jul 19, 2017

@dougm, it helped, thank you!

Working code, maybe it will be helpful for somebody:

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants