Skip to content

Fix incorrect disk sizes during VMware VM import#140

Merged
ibrokethecloud merged 4 commits into
harvester:mainfrom
susesamu:10167-size-fix
Mar 16, 2026
Merged

Fix incorrect disk sizes during VMware VM import#140
ibrokethecloud merged 4 commits into
harvester:mainfrom
susesamu:10167-size-fix

Conversation

@susesamu
Copy link
Copy Markdown
Contributor

@susesamu susesamu commented Mar 11, 2026

Problem:
When importing virtual machines from VMware vCenter, the vm-import-controller determines disk sizes using nfc.LeaseItem.Size. In some VMware environments this value represents the total provisioned capacity of the VM, not the size of the individual disk.

As a result, each imported disk may incorrectly receive the aggregate VM size, causing PVCs to be over-provisioned and migrations to fail due to insufficient storage.

Solution:
Disk capacities are now determined using the VM hardware configuration retrieved from: mo.VirtualMachine.Config.Hardware.Device For each VirtualDisk, the code reads: VirtualDisk.CapacityInKB and maps disks to lease items using the busNumber.

Related Issue:
harvester/harvester#10167

Test plan:

  1. Create a VM with multiple disks with different sizes.
  2. Create a VirtualMachineImport resource
  3. Check the disk sizes created in Harvester
  4. Disk sizes should now match the original VMware disk capacities.

Observations
on the previous PR, we couldn't match the key, so we now matching the busNumber since is unique per controller instance in hardware model.

Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
Copilot AI review requested due to automatic review settings March 11, 2026 14:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts VMware VM import logic so disk sizes are derived from the VM’s hardware configuration (per-disk capacity) rather than relying on NFC lease item sizes, which can represent aggregate VM capacity in some environments.

Changes:

  • Retrieves config.hardware.device from vCenter and builds a lookup of VirtualDisk devices.
  • Determines per-disk size using VirtualDisk.CapacityInKB (bytes = KB * 1024) and falls back to lease Size if mapping fails.
  • Adds parseDeviceId helper to map lease items to hardware disks by (bus, unit).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/source/vmware/client.go
Comment thread pkg/source/vmware/client.go
Comment thread pkg/source/vmware/client.go
Comment thread pkg/source/vmware/client.go
Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
Comment thread pkg/source/vmware/client.go Outdated
Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
ibrokethecloud
ibrokethecloud previously approved these changes Mar 12, 2026
Copy link
Copy Markdown
Collaborator

@ibrokethecloud ibrokethecloud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was able to verify the disk sizes via a simple unit test

    /Users/gauravmehta/go/src/github.com/harvester/vm-import-controller/pkg/source/vmware/client_test.go:621: disk from vm obj disk-0.vmdk: size 42949672960
    /Users/gauravmehta/go/src/github.com/harvester/vm-import-controller/pkg/source/vmware/client_test.go:622: disk from lease disk-0.vmdk: size 48318382080
    /Users/gauravmehta/go/src/github.com/harvester/vm-import-controller/pkg/source/vmware/client_test.go:621: disk from vm obj disk-1.vmdk: size 5368709120
    /Users/gauravmehta/go/src/github.com/harvester/vm-import-controller/pkg/source/vmware/client_test.go:622: disk from lease disk-1.vmdk: size 48318382080

the vm obj does reflect the correct disk size, compared to the lease object.

votdev
votdev previously approved these changes Mar 12, 2026
Copy link
Copy Markdown
Member

@votdev votdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

... but missing some unit tests, e.g. for parseDeviceId and some comments to show what the field contains. E.g. i'm missing the explanation of the schema that is used by DeviceId. Such documentation will make life easier in the future.

Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
@susesamu susesamu dismissed stale reviews from votdev and ibrokethecloud via b52d93b March 12, 2026 13:34

unit64, err := strconv.ParseInt(colon[1], 10, 32)
if err != nil {
return
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit, should we also log the error here so we know which device was skipped?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary because some lines below where the parseDeviceId function is called a log message is generated when the code is using the fallback path:

if diskSize == 0 {
				// fallback to lease size if mapping failed
				logrus.WithFields(logrus.Fields{
					"name": vm.Name, "namespace": vm.Namespace,
					"path":     i.Path,
					"deviceId": i.DeviceId,
					"size":     i.Size,
				}).Warn("Failed to map lease VMDK to VirtualDisk capacity; falling back to lease item size")
				diskSize = i.Size
			}

This log message contains all relevant info that is necessary to know what happened.

@votdev
Copy link
Copy Markdown
Member

votdev commented Mar 16, 2026

@Mergifyio backport v1.8

@mergify
Copy link
Copy Markdown

mergify Bot commented Mar 16, 2026

backport v1.8

✅ Backports have been created

Details

@ibrokethecloud ibrokethecloud merged commit fd6f2e0 into harvester:main Mar 16, 2026
6 checks passed
votdev pushed a commit that referenced this pull request Mar 16, 2026
* vmware: determine disk size from VM hardware instead of NFC lease

Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
(cherry picked from commit d540117)

* apply suggestions

Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
(cherry picked from commit 2852e6a)

* apply suggestions

Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
(cherry picked from commit 6869512)

* add test for parseDeviceID and some comments

Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
(cherry picked from commit fd6f2e0)

---------

Signed-off-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
Co-authored-by: Samuel Vasconcelos <samuel.vasconcelos@suse.com>
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

Successfully merging this pull request may close these issues.

4 participants