Take path for wsl instead of forcing through WindowsApps

Signed-off-by: Matheus Cunha <matheusvflor@hotmail.com>
This commit is contained in:
Matheus Vitório 2025-05-06 11:59:01 +02:00 committed by Matheus Cunha
parent ebbf90decc
commit b6eeaea7f3
6 changed files with 26 additions and 78 deletions

View File

@ -48,7 +48,7 @@ func installWslKernel() error {
) )
backoff := 500 * time.Millisecond backoff := 500 * time.Millisecond
for i := 1; i < 6; i++ { for i := 1; i < 6; i++ {
err = wutil.SilentExec(wutil.FindWSL(), "--update") err = wutil.SilentExec("wsl", "--update")
if err == nil { if err == nil {
break break
} }

View File

@ -38,7 +38,8 @@ func getOtherProvider() string {
return "" return ""
} }
func runSystemCommand(binary string, cmdArgs []string) (*machineSession, error) { func runWslCommand(cmdArgs []string) (*machineSession, error) {
binary := "wsl"
GinkgoWriter.Println(binary + " " + strings.Join(cmdArgs, " ")) GinkgoWriter.Println(binary + " " + strings.Join(cmdArgs, " "))
c := exec.Command(binary, cmdArgs...) c := exec.Command(binary, cmdArgs...)
session, err := Start(c, GinkgoWriter, GinkgoWriter) session, err := Start(c, GinkgoWriter, GinkgoWriter)

View File

@ -9,7 +9,6 @@ import (
"github.com/Microsoft/go-winio/vhd" "github.com/Microsoft/go-winio/vhd"
"github.com/containers/libhvee/pkg/hypervctl" "github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/podman/v5/pkg/machine/define" "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/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec" . "github.com/onsi/gomega/gexec"
@ -28,12 +27,12 @@ var _ = Describe("podman machine init - windows only", func() {
Expect(session).To(Exit(0)) Expect(session).To(Exit(0))
defer func() { defer func() {
_, err := runSystemCommand(wutil.FindWSL(), []string{"--terminate", "podman-net-usermode"}) _, err := runWslCommand([]string{"--terminate", "podman-net-usermode"})
if err != nil { if err != nil {
fmt.Println("unable to terminate podman-net-usermode") 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 { if err != nil {
fmt.Println("unable to unregister podman-net-usermode") 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 // a vm outside the context of podman-machine and also
// so we dont have to download a distribution from microsoft // so we dont have to download a distribution from microsoft
// servers // servers
exportSession, err := runSystemCommand(wutil.FindWSL(), []string{"--export", "podman-foobarexport", exportedPath}) exportSession, err := runWslCommand([]string{"--export", "podman-foobarexport", exportedPath})
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(exportSession).To(Exit(0)) Expect(exportSession).To(Exit(0))
// importing the machine and creating a vm // 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(err).ToNot(HaveOccurred())
Expect(importSession).To(Exit(0)) Expect(importSession).To(Exit(0))
defer func() { defer func() {
_, err := runSystemCommand(wutil.FindWSL(), []string{"--unregister", distName}) _, err := runWslCommand([]string{"--unregister", distName})
if err != nil { if err != nil {
fmt.Println("unable to remove bogus wsl instance") fmt.Println("unable to remove bogus wsl instance")
} }

View File

@ -95,7 +95,7 @@ func provisionWSLDist(name string, imagePath string, prompt string) (string, err
dist := env.WithPodmanPrefix(name) dist := env.WithPodmanPrefix(name)
fmt.Println(prompt) 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) return "", fmt.Errorf("the WSL import of guest OS failed: %w", err)
} }
@ -427,7 +427,7 @@ func installWslKernel() error {
backoff := 500 * time.Millisecond backoff := 500 * time.Millisecond
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
err = runCmdPassThroughTee(log, wutil.FindWSL(), "--update") err = runCmdPassThroughTee(log, "wsl", "--update")
if err == nil { if err == nil {
break break
} }
@ -537,18 +537,18 @@ func withUser(s string, user string) string {
func wslInvoke(dist string, arg ...string) error { func wslInvoke(dist string, arg ...string) error {
newArgs := []string{"-u", "root", "-d", dist} newArgs := []string{"-u", "root", "-d", dist}
newArgs = append(newArgs, arg...) newArgs = append(newArgs, arg...)
return runCmdPassThrough(wutil.FindWSL(), newArgs...) return runCmdPassThrough("wsl", newArgs...)
} }
func wslPipe(input string, dist string, arg ...string) error { func wslPipe(input string, dist string, arg ...string) error {
newArgs := []string{"-u", "root", "-d", dist} newArgs := []string{"-u", "root", "-d", dist}
newArgs = append(newArgs, arg...) newArgs = append(newArgs, arg...)
return pipeCmdPassThrough(wutil.FindWSL(), input, newArgs...) return pipeCmdPassThrough("wsl", input, newArgs...)
} }
//nolint:unused //nolint:unused
func wslCreateKeys(identityPath string, dist string) (string, error) { 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 { func runCmdPassThrough(name string, arg ...string) error {
@ -645,7 +645,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) {
if running { if running {
args = append(args, "--running") args = append(args, "--running")
} }
cmd := exec.Command(wutil.FindWSL(), args...) cmd := exec.Command("wsl", args...)
out, err := cmd.StdoutPipe() out, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return nil, err return nil, err
@ -674,7 +674,7 @@ func getAllWSLDistros(running bool) (map[string]struct{}, error) {
} }
func isSystemdRunning(dist string) (bool, 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") cmd.Stdin = strings.NewReader(sysdpid + "\necho $SYSDPID\n")
out, err := cmd.StdoutPipe() out, err := cmd.StdoutPipe()
if err != nil { if err != nil {
@ -704,7 +704,7 @@ func isSystemdRunning(dist string) (bool, error) {
} }
func terminateDist(dist string) error { func terminateDist(dist string) error {
cmd := exec.Command(wutil.FindWSL(), "--terminate", dist) cmd := exec.Command("wsl", "--terminate", dist)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return fmt.Errorf("command %s %v failed: %w (%s)", cmd.Path, cmd.Args, err, strings.TrimSpace(string(out))) 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 { func unregisterDist(dist string) error {
cmd := exec.Command(wutil.FindWSL(), "--unregister", dist) cmd := exec.Command("wsl", "--unregister", dist)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return fmt.Errorf("command %s %v failed: %w (%s)", cmd.Path, cmd.Args, err, strings.TrimSpace(string(out))) 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 { if run, _ := isWSLRunning(dist); !run {
return 0, nil 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() out, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return 0, err return 0, err
@ -791,7 +791,7 @@ func getMem(name string) (strongunits.MiB, error) {
if run, _ := isWSLRunning(dist); !run { if run, _ := isWSLRunning(dist); !run {
return 0, nil 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() out, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -11,7 +11,6 @@ import (
"strings" "strings"
"github.com/containers/podman/v5/pkg/machine/env" "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" gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/containers/podman/v5/pkg/machine" "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 // below if we wanted to hard error on the wsl unregister
// of the vm // of the vm
wslRemoveFunc := func() error { 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 err
} }
return nil 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()) 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) cmd.Stdin = strings.NewReader(waitTerm)
out := &bytes.Buffer{} out := &bytes.Buffer{}
cmd.Stderr = out 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) 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 { if err = exitCmd.Run(); err != nil {
return fmt.Errorf("stopping systemd: %w", err) return fmt.Errorf("stopping systemd: %w", err)
} }

View File

@ -6,21 +6,17 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
"github.com/containers/storage/pkg/fileutils"
"golang.org/x/text/encoding/unicode" "golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform" "golang.org/x/text/transform"
) )
var ( var (
onceFind, onceStatus sync.Once onceStatus sync.Once
wslPath string
status wslStatus status wslStatus
wslNotInstalledMessages = []string{"kernel file is not found", "The Windows Subsystem for Linux is not installed"} 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\""} vmpDisabledMessages = []string{"enable the Virtual Machine Platform Windows feature", "Enable \"Virtual Machine Platform\""}
@ -33,53 +29,6 @@ type wslStatus struct {
wslFeatureEnabled bool 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 { func SilentExec(command string, args ...string) error {
cmd := exec.Command(command, args...) cmd := exec.Command(command, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000} cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
@ -104,7 +53,7 @@ func parseWSLStatus() wslStatus {
vmpFeatureEnabled: false, vmpFeatureEnabled: false,
wslFeatureEnabled: false, wslFeatureEnabled: false,
} }
cmd := SilentExecCmd(FindWSL(), "--status") cmd := SilentExecCmd("wsl", "--status")
out, err := cmd.StdoutPipe() out, err := cmd.StdoutPipe()
cmd.Stderr = nil cmd.Stderr = nil
if err != nil { if err != nil {
@ -130,7 +79,7 @@ func IsWSLInstalled() bool {
} }
func IsWSLFeatureEnabled() bool { func IsWSLFeatureEnabled() bool {
if SilentExec(FindWSL(), "--set-default-version", "2") != nil { if SilentExec("wsl", "--set-default-version", "2") != nil {
return false return false
} }
status := parseWSLStatus() status := parseWSLStatus()
@ -138,7 +87,7 @@ func IsWSLFeatureEnabled() bool {
} }
func IsWSLStoreVersionInstalled() bool { func IsWSLStoreVersionInstalled() bool {
cmd := SilentExecCmd(FindWSL(), "--version") cmd := SilentExecCmd("wsl", "--version")
cmd.Stdout = nil cmd.Stdout = nil
cmd.Stderr = nil cmd.Stderr = nil
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {