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

Right way to replace/swap attached disk with another existing disk #3367

Open
rickatnight11 opened this issue Feb 21, 2024 · 6 comments
Open

Comments

@rickatnight11
Copy link

rickatnight11 commented Feb 21, 2024

I'm trying to replace the root disk across a bunch of VMs by cloning the disk from a template, then swap the new disk in for the existing one. I'm having a heck of a time figuring out the right way to do this.

I have tried with DetachDisk and AttachDisk, but struggling to determine what disk ID AttachDisk is looking for (tried the UUID, but get The object or item referred to could not be found).

I have also tried a Reconfigure (similar to #790, but I don't just want to add another disk, I want to swap one in (and keep the original one "detached" in the datastore), which looks like a better option (more atomic), but I can't quite figure out the way to construct the desired change.

I basically want the equivalent of this call:

https://developer.vmware.com/apis/vsphere-automation/latest/vcenter/api/vcenter/vm/vm/hardware/disk/disk/patch/

Would love some guidance on how to approach this!

Copy link
Contributor

Howdy 🖐   rickatnight11 ! Thank you for your interest in this project. We value your feedback and will respond soon.

If you want to contribute to this project, please make yourself familiar with the CONTRIBUTION guidelines.

@rickatnight11
Copy link
Author

Digging more into the code, it looks like EditDevice is the helper function I'm looking for. Trying to figure out how to use it correctly.

@rickatnight11
Copy link
Author

So far I've tried swapping out the disk's backing with a bare-bones one that sets a different path:

// vm = object.VirtualMachine
// newdiskpath = "[datastore-name] vm-name/new-disk.vmdk"

vmdevices, err := vm.Device(ctx)
vmrootdisk := vmdevices.FindByKey(2000)

vmrootdisk.GetVirtualDevice().Backing = &types.VirtualDiskFlatVer2BackingInfo{
    VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
        FileName:  newdiskpath,
        Datastore: vmrootdisk.GetVirtualDevice().Backing.(types.BaseVirtualDeviceFileBackingInfo).GetVirtualDeviceFileBackingInfo().Datastore,
    },
}

vm.EditDevice(ctx, vmrootdisk)

...but vCenter is returning:

"error": {
  "fault": {
    "property": "virtualDevice.backing",
    "deviceIndex": 0,
    "badFileOp": "replace"
  },
  "localizedMessage": "Invalid operation for device '0'.",
  "Description": {
    "key": "com.vmware.vim.vpxd.vpx.vmprov.ReconfigureVm",
    "message": "Reconfiguring Virtual Machine on destination host"
  }
}

I feel like I'm close.

@rickatnight11
Copy link
Author

I didn't manage to figure this out, but I did finally get RemoveDevice + AddDevice working, borrowing heavily from govc's device.remove and vm.disk.attach implementations. It's slightly annoying, because I end up needing to removing all disks and adding them back to maintain the original label order (i.e. "Hard disk 1", "Hard disk 2", etc), but it works! Would still love any feedback on cleaner/preferred ways to do this.

Copy link
Contributor

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

@rickatnight11
Copy link
Author

/remove-lifecycle stale

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

1 participant