Skip to content

Commit 811aabd

Browse files
Merge pull request #26079 from mvfc/main
Take WSL path from PATH instead of forcing it to WindowsApps
2 parents e138304 + b6eeaea commit 811aabd

File tree

6 files changed

+26
-78
lines changed

6 files changed

+26
-78
lines changed

cmd/podman-wslkerninst/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func installWslKernel() error {
4848
)
4949
backoff := 500 * time.Millisecond
5050
for i := 1; i < 6; i++ {
51-
err = wutil.SilentExec(wutil.FindWSL(), "--update")
51+
err = wutil.SilentExec("wsl", "--update")
5252
if err == nil {
5353
break
5454
}

pkg/machine/e2e/config_windows_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func getOtherProvider() string {
3838
return ""
3939
}
4040

41-
func runSystemCommand(binary string, cmdArgs []string) (*machineSession, error) {
41+
func runWslCommand(cmdArgs []string) (*machineSession, error) {
42+
binary := "wsl"
4243
GinkgoWriter.Println(binary + " " + strings.Join(cmdArgs, " "))
4344
c := exec.Command(binary, cmdArgs...)
4445
session, err := Start(c, GinkgoWriter, GinkgoWriter)

pkg/machine/e2e/init_windows_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/Microsoft/go-winio/vhd"
1010
"github.com/containers/libhvee/pkg/hypervctl"
1111
"github.com/containers/podman/v5/pkg/machine/define"
12-
"github.com/containers/podman/v5/pkg/machine/wsl/wutil"
1312
. "github.com/onsi/ginkgo/v2"
1413
. "github.com/onsi/gomega"
1514
. "github.com/onsi/gomega/gexec"
@@ -28,12 +27,12 @@ var _ = Describe("podman machine init - windows only", func() {
2827
Expect(session).To(Exit(0))
2928

3029
defer func() {
31-
_, err := runSystemCommand(wutil.FindWSL(), []string{"--terminate", "podman-net-usermode"})
30+
_, err := runWslCommand([]string{"--terminate", "podman-net-usermode"})
3231
if err != nil {
3332
fmt.Println("unable to terminate podman-net-usermode")
3433
}
3534

36-
_, err = runSystemCommand(wutil.FindWSL(), []string{"--unregister", "podman-net-usermode"})
35+
_, err = runWslCommand([]string{"--unregister", "podman-net-usermode"})
3736
if err != nil {
3837
fmt.Println("unable to unregister podman-net-usermode")
3938
}
@@ -106,17 +105,17 @@ var _ = Describe("podman machine init - windows only", func() {
106105
// a vm outside the context of podman-machine and also
107106
// so we dont have to download a distribution from microsoft
108107
// servers
109-
exportSession, err := runSystemCommand(wutil.FindWSL(), []string{"--export", "podman-foobarexport", exportedPath})
108+
exportSession, err := runWslCommand([]string{"--export", "podman-foobarexport", exportedPath})
110109
Expect(err).ToNot(HaveOccurred())
111110
Expect(exportSession).To(Exit(0))
112111

113112
// importing the machine and creating a vm
114-
importSession, err := runSystemCommand(wutil.FindWSL(), []string{"--import", distName, distrDir, exportedPath})
113+
importSession, err := runWslCommand([]string{"--import", distName, distrDir, exportedPath})
115114
Expect(err).ToNot(HaveOccurred())
116115
Expect(importSession).To(Exit(0))
117116

118117
defer func() {
119-
_, err := runSystemCommand(wutil.FindWSL(), []string{"--unregister", distName})
118+
_, err := runWslCommand([]string{"--unregister", distName})
120119
if err != nil {
121120
fmt.Println("unable to remove bogus wsl instance")
122121
}

pkg/machine/wsl/machine.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func provisionWSLDist(name string, imagePath string, prompt string) (string, err
9595

9696
dist := env.WithPodmanPrefix(name)
9797
fmt.Println(prompt)
98-
if err = runCmdPassThrough(wutil.FindWSL(), "--import", dist, distTarget, imagePath, "--version", "2"); err != nil {
98+
if err = runCmdPassThrough("wsl", "--import", dist, distTarget, imagePath, "--version", "2"); err != nil {
9999
return "", fmt.Errorf("the WSL import of guest OS failed: %w", err)
100100
}
101101

@@ -427,7 +427,7 @@ func installWslKernel() error {
427427

428428
backoff := 500 * time.Millisecond
429429
for i := 0; i < 5; i++ {
430-
err = runCmdPassThroughTee(log, wutil.FindWSL(), "--update")
430+
err = runCmdPassThroughTee(log, "wsl", "--update")
431431
if err == nil {
432432
break
433433
}
@@ -537,18 +537,18 @@ func withUser(s string, user string) string {
537537
func wslInvoke(dist string, arg ...string) error {
538538
newArgs := []string{"-u", "root", "-d", dist}
539539
newArgs = append(newArgs, arg...)
540-
return runCmdPassThrough(wutil.FindWSL(), newArgs...)
540+
return runCmdPassThrough("wsl", newArgs...)
541541
}
542542

543543
func wslPipe(input string, dist string, arg ...string) error {
544544
newArgs := []string{"-u", "root", "-d", dist}
545545
newArgs = append(newArgs, arg...)
546-
return pipeCmdPassThrough(wutil.FindWSL(), input, newArgs...)
546+
return pipeCmdPassThrough("wsl", input, newArgs...)
547547
}
548548

549549
//nolint:unused
550550
func wslCreateKeys(identityPath string, dist string) (string, error) {
551-
return machine.CreateSSHKeysPrefix(identityPath, true, true, wutil.FindWSL(), "-u", "root", "-d", dist)
551+
return machine.CreateSSHKeysPrefix(identityPath, true, true, "wsl", "-u", "root", "-d", dist)
552552
}
553553

554554
func runCmdPassThrough(name string, arg ...string) error {
@@ -645,7 +645,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) {
645645
if running {
646646
args = append(args, "--running")
647647
}
648-
cmd := exec.Command(wutil.FindWSL(), args...)
648+
cmd := exec.Command("wsl", args...)
649649
out, err := cmd.StdoutPipe()
650650
if err != nil {
651651
return nil, err
@@ -674,7 +674,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) {
674674
}
675675

676676
func isSystemdRunning(dist string) (bool, error) {
677-
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "sh")
677+
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "sh")
678678
cmd.Stdin = strings.NewReader(sysdpid + "\necho $SYSDPID\n")
679679
out, err := cmd.StdoutPipe()
680680
if err != nil {
@@ -704,7 +704,7 @@ func isSystemdRunning(dist string) (bool, error) {
704704
}
705705

706706
func terminateDist(dist string) error {
707-
cmd := exec.Command(wutil.FindWSL(), "--terminate", dist)
707+
cmd := exec.Command("wsl", "--terminate", dist)
708708
out, err := cmd.CombinedOutput()
709709
if err != nil {
710710
return fmt.Errorf("command %s %v failed: %w (%s)", cmd.Path, cmd.Args, err, strings.TrimSpace(string(out)))
@@ -713,7 +713,7 @@ func terminateDist(dist string) error {
713713
}
714714

715715
func unregisterDist(dist string) error {
716-
cmd := exec.Command(wutil.FindWSL(), "--unregister", dist)
716+
cmd := exec.Command("wsl", "--unregister", dist)
717717
out, err := cmd.CombinedOutput()
718718
if err != nil {
719719
return fmt.Errorf("command %s %v failed: %w (%s)", cmd.Path, cmd.Args, err, strings.TrimSpace(string(out)))
@@ -761,7 +761,7 @@ func getCPUs(name string) (uint64, error) {
761761
if run, _ := isWSLRunning(dist); !run {
762762
return 0, nil
763763
}
764-
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "nproc")
764+
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "nproc")
765765
out, err := cmd.StdoutPipe()
766766
if err != nil {
767767
return 0, err
@@ -791,7 +791,7 @@ func getMem(name string) (strongunits.MiB, error) {
791791
if run, _ := isWSLRunning(dist); !run {
792792
return 0, nil
793793
}
794-
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "cat", "/proc/meminfo")
794+
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "cat", "/proc/meminfo")
795795
out, err := cmd.StdoutPipe()
796796
if err != nil {
797797
return 0, err

pkg/machine/wsl/stubber.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212

1313
"github.com/containers/podman/v5/pkg/machine/env"
14-
"github.com/containers/podman/v5/pkg/machine/wsl/wutil"
1514

1615
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
1716
"github.com/containers/podman/v5/pkg/machine"
@@ -105,7 +104,7 @@ func (w WSLStubber) Remove(mc *vmconfigs.MachineConfig) ([]string, func() error,
105104
// below if we wanted to hard error on the wsl unregister
106105
// of the vm
107106
wslRemoveFunc := func() error {
108-
if err := runCmdPassThrough(wutil.FindWSL(), "--unregister", env.WithPodmanPrefix(mc.Name)); err != nil {
107+
if err := runCmdPassThrough("wsl", "--unregister", env.WithPodmanPrefix(mc.Name)); err != nil {
109108
return err
110109
}
111110
return nil
@@ -250,7 +249,7 @@ func (w WSLStubber) StopVM(mc *vmconfigs.MachineConfig, hardStop bool) error {
250249
fmt.Fprintf(os.Stderr, "Could not stop API forwarding service (win-sshproxy.exe): %s\n", err.Error())
251250
}
252251

253-
cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "sh")
252+
cmd := exec.Command("wsl", "-u", "root", "-d", dist, "sh")
254253
cmd.Stdin = strings.NewReader(waitTerm)
255254
out := &bytes.Buffer{}
256255
cmd.Stderr = out
@@ -260,7 +259,7 @@ func (w WSLStubber) StopVM(mc *vmconfigs.MachineConfig, hardStop bool) error {
260259
return fmt.Errorf("executing wait command: %w", err)
261260
}
262261

263-
exitCmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "/usr/local/bin/enterns", "systemctl", "exit", "0")
262+
exitCmd := exec.Command("wsl", "-u", "root", "-d", dist, "/usr/local/bin/enterns", "systemctl", "exit", "0")
264263
if err = exitCmd.Run(); err != nil {
265264
return fmt.Errorf("stopping systemd: %w", err)
266265
}

pkg/machine/wsl/wutil/wutil.go

+4-55
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@ import (
66
"bufio"
77
"fmt"
88
"io"
9-
"os"
109
"os/exec"
11-
"path/filepath"
1210
"strings"
1311
"sync"
1412
"syscall"
1513

16-
"github.com/containers/storage/pkg/fileutils"
1714
"golang.org/x/text/encoding/unicode"
1815
"golang.org/x/text/transform"
1916
)
2017

2118
var (
22-
onceFind, onceStatus sync.Once
23-
wslPath string
19+
onceStatus sync.Once
2420
status wslStatus
2521
wslNotInstalledMessages = []string{"kernel file is not found", "The Windows Subsystem for Linux is not installed"}
2622
vmpDisabledMessages = []string{"enable the Virtual Machine Platform Windows feature", "Enable \"Virtual Machine Platform\""}
@@ -33,53 +29,6 @@ type wslStatus struct {
3329
wslFeatureEnabled bool
3430
}
3531

36-
func FindWSL() string {
37-
// At the time of this writing, a defect appeared in the OS preinstalled WSL executable
38-
// where it no longer reliably locates the preferred Windows App Store variant.
39-
//
40-
// Manually discover (and cache) the wsl.exe location to bypass the problem
41-
onceFind.Do(func() {
42-
var locs []string
43-
44-
// Prefer Windows App Store version
45-
if localapp := getLocalAppData(); localapp != "" {
46-
locs = append(locs, filepath.Join(localapp, "Microsoft", "WindowsApps", "wsl.exe"))
47-
}
48-
49-
// Otherwise, the common location for the legacy system version
50-
root := os.Getenv("SystemRoot")
51-
if root == "" {
52-
root = `C:\Windows`
53-
}
54-
locs = append(locs, filepath.Join(root, "System32", "wsl.exe"))
55-
56-
for _, loc := range locs {
57-
if err := fileutils.Exists(loc); err == nil {
58-
wslPath = loc
59-
return
60-
}
61-
}
62-
63-
// Hope for the best
64-
wslPath = "wsl"
65-
})
66-
67-
return wslPath
68-
}
69-
70-
func getLocalAppData() string {
71-
localapp := os.Getenv("LOCALAPPDATA")
72-
if localapp != "" {
73-
return localapp
74-
}
75-
76-
if user := os.Getenv("USERPROFILE"); user != "" {
77-
return filepath.Join(user, "AppData", "Local")
78-
}
79-
80-
return localapp
81-
}
82-
8332
func SilentExec(command string, args ...string) error {
8433
cmd := exec.Command(command, args...)
8534
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
@@ -104,7 +53,7 @@ func parseWSLStatus() wslStatus {
10453
vmpFeatureEnabled: false,
10554
wslFeatureEnabled: false,
10655
}
107-
cmd := SilentExecCmd(FindWSL(), "--status")
56+
cmd := SilentExecCmd("wsl", "--status")
10857
out, err := cmd.StdoutPipe()
10958
cmd.Stderr = nil
11059
if err != nil {
@@ -130,15 +79,15 @@ func IsWSLInstalled() bool {
13079
}
13180

13281
func IsWSLFeatureEnabled() bool {
133-
if SilentExec(FindWSL(), "--set-default-version", "2") != nil {
82+
if SilentExec("wsl", "--set-default-version", "2") != nil {
13483
return false
13584
}
13685
status := parseWSLStatus()
13786
return status.vmpFeatureEnabled
13887
}
13988

14089
func IsWSLStoreVersionInstalled() bool {
141-
cmd := SilentExecCmd(FindWSL(), "--version")
90+
cmd := SilentExecCmd("wsl", "--version")
14291
cmd.Stdout = nil
14392
cmd.Stderr = nil
14493
if err := cmd.Run(); err != nil {

0 commit comments

Comments
 (0)