KEMBAR78
Expose the VM ID inside user distros by chelnak · Pull Request #13212 · microsoft/WSL · GitHub
Skip to content

Conversation

@chelnak
Copy link
Contributor

@chelnak chelnak commented Jul 2, 2025

Summary of the Pull Request

Prior to this change it was not possible to find the ID of the VM responsible for providing WSL distro instances.

With this change, the VM ID is now exposed inside user distros via wslinfo and as an environment variable.

A benefit of this change is that a listener can be configured host side using the ID allowing connections from inside user distros.

For example, using Go, we can:

hvsock.Listen(hvsock.Addr{
		VMID:      guid,
		ServiceID: svcid,
	})

Where guid is the ID of the VM.

And from inside a distro, we can establish a vsock connection by using the appropriate port.

Looking forward to discussing this PR a bit more!

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

The changes here have been validated by locally testing the change inside a user distro & also by adding and running unit tests.

The vm id can be accessed by:

  • Using wslinfo --vm-id
  • Fetching at the environment variables inside the distro

image

@chelnak chelnak marked this pull request as ready for review July 2, 2025 16:57
@chelnak chelnak requested a review from a team as a code owner July 2, 2025 16:57
@benhillis
Copy link
Member

benhillis commented Jul 2, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@chelnak
Copy link
Contributor Author

chelnak commented Jul 2, 2025

Doh... i'll fix that localisation error shortly!

chelnak added 2 commits July 2, 2025 20:31
This change ensures that we pass the vm id to an
instances init. The id is then set as an environment
variable and can be accessed at runtime.
@chelnak
Copy link
Contributor Author

chelnak commented Jul 2, 2025

Should be fixed now (double checked with the python script locally!)

@chelnak
Copy link
Contributor Author

chelnak commented Jul 3, 2025

/azp run

@azure-pipelines
Copy link

Commenter does not have sufficient privileges for PR 13212 in repo microsoft/WSL

@benhillis
Copy link
Member

I am good with this approach, but I think @OneBlue has some thoughts about not using an environment variable that people might end up taking a dependency on (and might not be present in all situations).

Copy link
Collaborator

@OneBlue OneBlue left a comment

Choose a reason for hiding this comment

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

Overall change looks good !

Architecture wise I we should not expose WSL2_VM_ID to user processes and instead tell users to use wslinfo to fetch the VM ID.

Doing that cleanly would require a bit of a deeper change (since wslg currently depends on it being set inside the system distro), so let's merge this PR, and then we'll do a follow up change so that wslg moves to calling wslinfo, and then we can remove the environment variable from both the user and system distro.

Thank you for your contribution !

@OneBlue
Copy link
Collaborator

OneBlue commented Jul 3, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Add a new argument --vm-id to wslinfo so that
the caller can retrieve the VM id by calling the
binary.

Although it is an environment variable, it can be useful
here too to save additional string parsing from the caller.
@chelnak
Copy link
Contributor Author

chelnak commented Jul 3, 2025

@benhillis @OneBlue Thanks both for the reviews.

On the architecture point - I see what you mean and the plan sounds good!

@chelnak chelnak requested a review from OneBlue July 3, 2025 21:08
@OneBlue
Copy link
Collaborator

OneBlue commented Jul 3, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@OneBlue OneBlue merged commit 4547e2a into microsoft:master Jul 4, 2025
6 checks passed
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.

3 participants