Skip to content

Fix GuestAgentConn vsock support #2118

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

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/windows/registry_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
guestCommunicationsPrefix = `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices`
MagicVSOCKSuffix = "-facb-11e6-bd58-64006a7986d3"
magicVSOCKSuffix = "-facb-11e6-bd58-64006a7986d3"
wslDistroInfoPrefix = `SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss`
)

Expand All @@ -34,7 +34,7 @@ func AddVSockRegistryKey(port int) error {
return fmt.Errorf("port %q in use", port)
}

vsockKeyPath := fmt.Sprintf(`%x%s`, port, MagicVSOCKSuffix)
vsockKeyPath := fmt.Sprintf(`%x%s`, port, magicVSOCKSuffix)
vSockKey, _, err := registry.CreateKey(
rootKey,
vsockKeyPath,
Expand All @@ -61,7 +61,7 @@ func RemoveVSockRegistryKey(port int) error {
}
defer rootKey.Close()

vsockKeyPath := fmt.Sprintf(`%x%s`, port, MagicVSOCKSuffix)
vsockKeyPath := fmt.Sprintf(`%x%s`, port, magicVSOCKSuffix)
if err := registry.DeleteKey(rootKey, vsockKeyPath); err != nil {
return fmt.Errorf(
"failed to create new key (%s%s): %w",
Expand Down Expand Up @@ -215,7 +215,7 @@ func getUsedPorts(key registry.Key) ([]int, error) {

out := []int{}
for _, k := range keys {
split := strings.Split(k, MagicVSOCKSuffix)
split := strings.Split(k, magicVSOCKSuffix)
if len(split) == 2 {
i, err := strconv.Atoi(split[0])
if err != nil {
Expand Down
23 changes: 20 additions & 3 deletions pkg/wsl2/wsl_driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"net"
"regexp"

"github.com/Microsoft/go-winio"
"github.com/Microsoft/go-winio/pkg/guid"
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/reflectutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/mdlayher/vsock"
"github.com/lima-vm/lima/pkg/windows"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -173,6 +175,21 @@ func (l *LimaWslDriver) Unregister(ctx context.Context) error {
return nil
}

func (l *LimaWslDriver) GuestAgentConn(_ context.Context) (net.Conn, error) {
return vsock.Dial(2, uint32(l.VSockPort), nil)
// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
// As of 08-01-2024, github.com/mdlayher/vsock does not natively support vsock on
// Windows, so use the winio library to create the connection.
func (l *LimaWslDriver) GuestAgentConn(ctx context.Context) (net.Conn, error) {
VMIDStr, err := windows.GetInstanceVMID(fmt.Sprintf("lima-%s", l.Instance.Name))
if err != nil {
return nil, err
}
VMIDGUID, err := guid.FromString(VMIDStr)
if err != nil {
return nil, err
}
sockAddr := &winio.HvsockAddr{
VMID: VMIDGUID,
ServiceID: winio.VsockServiceID(uint32(l.VSockPort)),
}
return winio.Dial(ctx, sockAddr)
}