Skip to content

Commit 8b84d9f

Browse files
committed
Add fallback for busybox hostname with wsl2
Signed-off-by: Anders F Björklund <[email protected]>
1 parent df5e35a commit 8b84d9f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pkg/store/instance_windows.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,26 @@ func GetWslStatus(instName string) (string, error) {
121121
// Expected output (whitespace preserved, [] for optional):
122122
// PS > wsl -d <distroName> bash -c hostname -I | cut -d' ' -f1
123123
// 168.1.1.1 [10.0.0.1]
124+
// But busybox hostname does not implement --all-ip-addresses:
125+
// hostname: unrecognized option: I
124126
func GetSSHAddress(instName string) (string, error) {
125127
distroName := "lima-" + instName
128+
// Ubuntu
126129
cmd := exec.Command("wsl.exe", "-d", distroName, "bash", "-c", `hostname -I | cut -d ' ' -f1`)
127130
out, err := cmd.CombinedOutput()
128-
if err != nil {
131+
if err == nil {
132+
return strings.TrimSpace(string(out)), nil
133+
}
134+
// Alpine
135+
cmd = exec.Command("wsl.exe", "-d", distroName, "sh", "-c", `ip route get 1 | awk '{gsub("^.*src ",""); print $1; exit}'`)
136+
out, err = cmd.CombinedOutput()
137+
if err == nil {
138+
return strings.TrimSpace(string(out)), nil
139+
}
140+
// fallback
141+
cmd = exec.Command("wsl.exe", "-d", distroName, "hostname", "-i")
142+
out, err = cmd.CombinedOutput()
143+
if err != nil || strings.HasPrefix(string(out), "127.") {
129144
return "", fmt.Errorf("failed to get hostname for instance %q, err: %w (out=%q)", instName, err, string(out))
130145
}
131146

0 commit comments

Comments
 (0)