Skip to content

Commit bd7442e

Browse files
authored
Merge pull request #2118 from pendo324/fix-wsl-vsock
Fix GuestAgentConn vsock support
2 parents 7b74282 + c8de900 commit bd7442e

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

pkg/windows/registry_windows.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
const (
1515
guestCommunicationsPrefix = `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices`
16-
MagicVSOCKSuffix = "-facb-11e6-bd58-64006a7986d3"
16+
magicVSOCKSuffix = "-facb-11e6-bd58-64006a7986d3"
1717
wslDistroInfoPrefix = `SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss`
1818
)
1919

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

37-
vsockKeyPath := fmt.Sprintf(`%x%s`, port, MagicVSOCKSuffix)
37+
vsockKeyPath := fmt.Sprintf(`%x%s`, port, magicVSOCKSuffix)
3838
vSockKey, _, err := registry.CreateKey(
3939
rootKey,
4040
vsockKeyPath,
@@ -61,7 +61,7 @@ func RemoveVSockRegistryKey(port int) error {
6161
}
6262
defer rootKey.Close()
6363

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

216216
out := []int{}
217217
for _, k := range keys {
218-
split := strings.Split(k, MagicVSOCKSuffix)
218+
split := strings.Split(k, magicVSOCKSuffix)
219219
if len(split) == 2 {
220220
i, err := strconv.Atoi(split[0])
221221
if err != nil {

pkg/wsl2/wsl_driver_windows.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"net"
77
"regexp"
88

9+
"github.com/Microsoft/go-winio"
10+
"github.com/Microsoft/go-winio/pkg/guid"
911
"github.com/lima-vm/lima/pkg/driver"
1012
"github.com/lima-vm/lima/pkg/limayaml"
1113
"github.com/lima-vm/lima/pkg/reflectutil"
1214
"github.com/lima-vm/lima/pkg/store"
13-
"github.com/mdlayher/vsock"
15+
"github.com/lima-vm/lima/pkg/windows"
1416
"github.com/sirupsen/logrus"
1517
)
1618

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

176-
func (l *LimaWslDriver) GuestAgentConn(_ context.Context) (net.Conn, error) {
177-
return vsock.Dial(2, uint32(l.VSockPort), nil)
178+
// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
179+
// As of 08-01-2024, github.com/mdlayher/vsock does not natively support vsock on
180+
// Windows, so use the winio library to create the connection.
181+
func (l *LimaWslDriver) GuestAgentConn(ctx context.Context) (net.Conn, error) {
182+
VMIDStr, err := windows.GetInstanceVMID(fmt.Sprintf("lima-%s", l.Instance.Name))
183+
if err != nil {
184+
return nil, err
185+
}
186+
VMIDGUID, err := guid.FromString(VMIDStr)
187+
if err != nil {
188+
return nil, err
189+
}
190+
sockAddr := &winio.HvsockAddr{
191+
VMID: VMIDGUID,
192+
ServiceID: winio.VsockServiceID(uint32(l.VSockPort)),
193+
}
194+
return winio.Dial(ctx, sockAddr)
178195
}

0 commit comments

Comments
 (0)