diff --git a/cmd/podman-wslkerninst/main.go b/cmd/podman-wslkerninst/main.go index 76154b8cbd..9a7d36a28d 100644 --- a/cmd/podman-wslkerninst/main.go +++ b/cmd/podman-wslkerninst/main.go @@ -48,7 +48,7 @@ func installWslKernel() error { ) backoff := 500 * time.Millisecond for i := 1; i < 6; i++ { - err = wutil.SilentExec(wutil.FindWSL(), "--update") + err = wutil.SilentExec("wsl", "--update") if err == nil { break } diff --git a/pkg/machine/e2e/config_windows_test.go b/pkg/machine/e2e/config_windows_test.go index 82442ab640..0e80883a70 100644 --- a/pkg/machine/e2e/config_windows_test.go +++ b/pkg/machine/e2e/config_windows_test.go @@ -38,7 +38,8 @@ func getOtherProvider() string { return "" } -func runSystemCommand(binary string, cmdArgs []string) (*machineSession, error) { +func runWslCommand(cmdArgs []string) (*machineSession, error) { + binary := "wsl" GinkgoWriter.Println(binary + " " + strings.Join(cmdArgs, " ")) c := exec.Command(binary, cmdArgs...) session, err := Start(c, GinkgoWriter, GinkgoWriter) diff --git a/pkg/machine/e2e/init_windows_test.go b/pkg/machine/e2e/init_windows_test.go index d09e2477ad..4ffc950061 100644 --- a/pkg/machine/e2e/init_windows_test.go +++ b/pkg/machine/e2e/init_windows_test.go @@ -9,7 +9,6 @@ import ( "github.com/Microsoft/go-winio/vhd" "github.com/containers/libhvee/pkg/hypervctl" "github.com/containers/podman/v5/pkg/machine/define" - "github.com/containers/podman/v5/pkg/machine/wsl/wutil" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" . "github.com/onsi/gomega/gexec" @@ -28,12 +27,12 @@ var _ = Describe("podman machine init - windows only", func() { Expect(session).To(Exit(0)) defer func() { - _, err := runSystemCommand(wutil.FindWSL(), []string{"--terminate", "podman-net-usermode"}) + _, err := runWslCommand([]string{"--terminate", "podman-net-usermode"}) if err != nil { fmt.Println("unable to terminate podman-net-usermode") } - _, err = runSystemCommand(wutil.FindWSL(), []string{"--unregister", "podman-net-usermode"}) + _, err = runWslCommand([]string{"--unregister", "podman-net-usermode"}) if err != nil { fmt.Println("unable to unregister podman-net-usermode") } @@ -106,17 +105,17 @@ var _ = Describe("podman machine init - windows only", func() { // a vm outside the context of podman-machine and also // so we dont have to download a distribution from microsoft // servers - exportSession, err := runSystemCommand(wutil.FindWSL(), []string{"--export", "podman-foobarexport", exportedPath}) + exportSession, err := runWslCommand([]string{"--export", "podman-foobarexport", exportedPath}) Expect(err).ToNot(HaveOccurred()) Expect(exportSession).To(Exit(0)) // importing the machine and creating a vm - importSession, err := runSystemCommand(wutil.FindWSL(), []string{"--import", distName, distrDir, exportedPath}) + importSession, err := runWslCommand([]string{"--import", distName, distrDir, exportedPath}) Expect(err).ToNot(HaveOccurred()) Expect(importSession).To(Exit(0)) defer func() { - _, err := runSystemCommand(wutil.FindWSL(), []string{"--unregister", distName}) + _, err := runWslCommand([]string{"--unregister", distName}) if err != nil { fmt.Println("unable to remove bogus wsl instance") } diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go index 73650c18ea..d840ed3193 100644 --- a/pkg/machine/wsl/machine.go +++ b/pkg/machine/wsl/machine.go @@ -95,7 +95,7 @@ func provisionWSLDist(name string, imagePath string, prompt string) (string, err dist := env.WithPodmanPrefix(name) fmt.Println(prompt) - if err = runCmdPassThrough(wutil.FindWSL(), "--import", dist, distTarget, imagePath, "--version", "2"); err != nil { + if err = runCmdPassThrough("wsl", "--import", dist, distTarget, imagePath, "--version", "2"); err != nil { return "", fmt.Errorf("the WSL import of guest OS failed: %w", err) } @@ -427,7 +427,7 @@ func installWslKernel() error { backoff := 500 * time.Millisecond for i := 0; i < 5; i++ { - err = runCmdPassThroughTee(log, wutil.FindWSL(), "--update") + err = runCmdPassThroughTee(log, "wsl", "--update") if err == nil { break } @@ -537,18 +537,18 @@ func withUser(s string, user string) string { func wslInvoke(dist string, arg ...string) error { newArgs := []string{"-u", "root", "-d", dist} newArgs = append(newArgs, arg...) - return runCmdPassThrough(wutil.FindWSL(), newArgs...) + return runCmdPassThrough("wsl", newArgs...) } func wslPipe(input string, dist string, arg ...string) error { newArgs := []string{"-u", "root", "-d", dist} newArgs = append(newArgs, arg...) - return pipeCmdPassThrough(wutil.FindWSL(), input, newArgs...) + return pipeCmdPassThrough("wsl", input, newArgs...) } //nolint:unused func wslCreateKeys(identityPath string, dist string) (string, error) { - return machine.CreateSSHKeysPrefix(identityPath, true, true, wutil.FindWSL(), "-u", "root", "-d", dist) + return machine.CreateSSHKeysPrefix(identityPath, true, true, "wsl", "-u", "root", "-d", dist) } func runCmdPassThrough(name string, arg ...string) error { @@ -645,7 +645,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) { if running { args = append(args, "--running") } - cmd := exec.Command(wutil.FindWSL(), args...) + cmd := exec.Command("wsl", args...) out, err := cmd.StdoutPipe() if err != nil { return nil, err @@ -674,7 +674,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) { } func isSystemdRunning(dist string) (bool, error) { - cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "sh") + cmd := exec.Command("wsl", "-u", "root", "-d", dist, "sh") cmd.Stdin = strings.NewReader(sysdpid + "\necho $SYSDPID\n") out, err := cmd.StdoutPipe() if err != nil { @@ -704,7 +704,7 @@ func isSystemdRunning(dist string) (bool, error) { } func terminateDist(dist string) error { - cmd := exec.Command(wutil.FindWSL(), "--terminate", dist) + cmd := exec.Command("wsl", "--terminate", dist) out, err := cmd.CombinedOutput() if err != nil { 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 { } func unregisterDist(dist string) error { - cmd := exec.Command(wutil.FindWSL(), "--unregister", dist) + cmd := exec.Command("wsl", "--unregister", dist) out, err := cmd.CombinedOutput() if err != nil { 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) { if run, _ := isWSLRunning(dist); !run { return 0, nil } - cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "nproc") + cmd := exec.Command("wsl", "-u", "root", "-d", dist, "nproc") out, err := cmd.StdoutPipe() if err != nil { return 0, err @@ -791,7 +791,7 @@ func getMem(name string) (strongunits.MiB, error) { if run, _ := isWSLRunning(dist); !run { return 0, nil } - cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "cat", "/proc/meminfo") + cmd := exec.Command("wsl", "-u", "root", "-d", dist, "cat", "/proc/meminfo") out, err := cmd.StdoutPipe() if err != nil { return 0, err diff --git a/pkg/machine/wsl/stubber.go b/pkg/machine/wsl/stubber.go index 75b12fad03..9759512cbe 100644 --- a/pkg/machine/wsl/stubber.go +++ b/pkg/machine/wsl/stubber.go @@ -11,7 +11,6 @@ import ( "strings" "github.com/containers/podman/v5/pkg/machine/env" - "github.com/containers/podman/v5/pkg/machine/wsl/wutil" gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types" "github.com/containers/podman/v5/pkg/machine" @@ -105,7 +104,7 @@ func (w WSLStubber) Remove(mc *vmconfigs.MachineConfig) ([]string, func() error, // below if we wanted to hard error on the wsl unregister // of the vm wslRemoveFunc := func() error { - if err := runCmdPassThrough(wutil.FindWSL(), "--unregister", env.WithPodmanPrefix(mc.Name)); err != nil { + if err := runCmdPassThrough("wsl", "--unregister", env.WithPodmanPrefix(mc.Name)); err != nil { return err } return nil @@ -250,7 +249,7 @@ func (w WSLStubber) StopVM(mc *vmconfigs.MachineConfig, hardStop bool) error { fmt.Fprintf(os.Stderr, "Could not stop API forwarding service (win-sshproxy.exe): %s\n", err.Error()) } - cmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "sh") + cmd := exec.Command("wsl", "-u", "root", "-d", dist, "sh") cmd.Stdin = strings.NewReader(waitTerm) out := &bytes.Buffer{} cmd.Stderr = out @@ -260,7 +259,7 @@ func (w WSLStubber) StopVM(mc *vmconfigs.MachineConfig, hardStop bool) error { return fmt.Errorf("executing wait command: %w", err) } - exitCmd := exec.Command(wutil.FindWSL(), "-u", "root", "-d", dist, "/usr/local/bin/enterns", "systemctl", "exit", "0") + exitCmd := exec.Command("wsl", "-u", "root", "-d", dist, "/usr/local/bin/enterns", "systemctl", "exit", "0") if err = exitCmd.Run(); err != nil { return fmt.Errorf("stopping systemd: %w", err) } diff --git a/pkg/machine/wsl/wutil/wutil.go b/pkg/machine/wsl/wutil/wutil.go index 49bc638ac4..b452d691d1 100644 --- a/pkg/machine/wsl/wutil/wutil.go +++ b/pkg/machine/wsl/wutil/wutil.go @@ -6,21 +6,17 @@ import ( "bufio" "fmt" "io" - "os" "os/exec" - "path/filepath" "strings" "sync" "syscall" - "github.com/containers/storage/pkg/fileutils" "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" ) var ( - onceFind, onceStatus sync.Once - wslPath string + onceStatus sync.Once status wslStatus wslNotInstalledMessages = []string{"kernel file is not found", "The Windows Subsystem for Linux is not installed"} vmpDisabledMessages = []string{"enable the Virtual Machine Platform Windows feature", "Enable \"Virtual Machine Platform\""} @@ -33,53 +29,6 @@ type wslStatus struct { wslFeatureEnabled bool } -func FindWSL() string { - // At the time of this writing, a defect appeared in the OS preinstalled WSL executable - // where it no longer reliably locates the preferred Windows App Store variant. - // - // Manually discover (and cache) the wsl.exe location to bypass the problem - onceFind.Do(func() { - var locs []string - - // Prefer Windows App Store version - if localapp := getLocalAppData(); localapp != "" { - locs = append(locs, filepath.Join(localapp, "Microsoft", "WindowsApps", "wsl.exe")) - } - - // Otherwise, the common location for the legacy system version - root := os.Getenv("SystemRoot") - if root == "" { - root = `C:\Windows` - } - locs = append(locs, filepath.Join(root, "System32", "wsl.exe")) - - for _, loc := range locs { - if err := fileutils.Exists(loc); err == nil { - wslPath = loc - return - } - } - - // Hope for the best - wslPath = "wsl" - }) - - return wslPath -} - -func getLocalAppData() string { - localapp := os.Getenv("LOCALAPPDATA") - if localapp != "" { - return localapp - } - - if user := os.Getenv("USERPROFILE"); user != "" { - return filepath.Join(user, "AppData", "Local") - } - - return localapp -} - func SilentExec(command string, args ...string) error { cmd := exec.Command(command, args...) cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000} @@ -104,7 +53,7 @@ func parseWSLStatus() wslStatus { vmpFeatureEnabled: false, wslFeatureEnabled: false, } - cmd := SilentExecCmd(FindWSL(), "--status") + cmd := SilentExecCmd("wsl", "--status") out, err := cmd.StdoutPipe() cmd.Stderr = nil if err != nil { @@ -130,7 +79,7 @@ func IsWSLInstalled() bool { } func IsWSLFeatureEnabled() bool { - if SilentExec(FindWSL(), "--set-default-version", "2") != nil { + if SilentExec("wsl", "--set-default-version", "2") != nil { return false } status := parseWSLStatus() @@ -138,7 +87,7 @@ func IsWSLFeatureEnabled() bool { } func IsWSLStoreVersionInstalled() bool { - cmd := SilentExecCmd(FindWSL(), "--version") + cmd := SilentExecCmd("wsl", "--version") cmd.Stdout = nil cmd.Stderr = nil if err := cmd.Run(); err != nil {