From c8de900f4740458d528283e31289516e6c20d608 Mon Sep 17 00:00:00 2001 From: Justin Alvarez Date: Mon, 8 Jan 2024 16:02:24 -0500 Subject: [PATCH] fix(wsl): GuestAgentConn vsock support Signed-off-by: Justin Alvarez --- pkg/windows/registry_windows.go | 8 ++++---- pkg/wsl2/wsl_driver_windows.go | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/pkg/windows/registry_windows.go b/pkg/windows/registry_windows.go index 2005fb5bed4..307bfadf528 100644 --- a/pkg/windows/registry_windows.go +++ b/pkg/windows/registry_windows.go @@ -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` ) @@ -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, @@ -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", @@ -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 { diff --git a/pkg/wsl2/wsl_driver_windows.go b/pkg/wsl2/wsl_driver_windows.go index 3cfa58900b0..8d508e7059d 100644 --- a/pkg/wsl2/wsl_driver_windows.go +++ b/pkg/wsl2/wsl_driver_windows.go @@ -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" ) @@ -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) }