Skip to content

Commit

Permalink
Validate current generation of WSL2 with user-mode-networking
Browse files Browse the repository at this point in the history
Fail with a helpful message when older version is present.

[NO NEW TESTS NEEDED]

Signed-off-by: Jason T. Greene <[email protected]>
  • Loading branch information
n1hility committed Aug 19, 2023
1 parent 20f28e5 commit bdc3040
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
6 changes: 6 additions & 0 deletions pkg/machine/wsl/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ func (v *MachineVM) Init(opts machine.InitOptions) (bool, error) {
v.Rootful = opts.Rootful
v.Version = currentMachineVersion

if v.UserModeNetworking {
if err := verifyWSLUserModeCompat(); err != nil {
return false, err
}
}

if err := downloadDistro(v, opts); err != nil {
return false, err
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/machine/wsl/usermodenet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"

"github.com/containers/podman/v4/pkg/machine"
"github.com/containers/podman/v4/pkg/machine/wsl/wutil"
"github.com/containers/podman/v4/pkg/specgen"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -53,6 +54,21 @@ ip route add $ROUTE
rm -rf /mnt/wsl/podman-usermodenet
`

func verifyWSLUserModeCompat() error {
if wutil.IsWSLStoreVersionInstalled() {
return nil
}

prefix := ""
if !winVersionAtLeast(10, 0, 19043) {
prefix = "upgrade to 22H2, "
}

return fmt.Errorf("user-mode networking requires a newer version of WSL: "+
"%sapply all outstanding windows updates, and then run `wsl --update`",
prefix)
}

func (v *MachineVM) startUserModeNetworking() error {
if !v.UserModeNetworking {
return nil
Expand Down Expand Up @@ -299,6 +315,10 @@ func (v *MachineVM) obtainUserModeNetLock() (*fileLock, error) {
}

func changeDistUserModeNetworking(dist string, user string, image string, enable bool) error {
if err := verifyWSLUserModeCompat(); err != nil {
return err
}

// Only install if user-mode is being enabled and there was an image path passed
if enable && len(image) > 0 {
if err := installUserModeDist(dist, image); err != nil {
Expand Down
45 changes: 34 additions & 11 deletions pkg/machine/wsl/wutil/wutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package wutil

import (
"bufio"
"io"
"os/exec"
"strings"
"syscall"
Expand Down Expand Up @@ -37,19 +38,41 @@ func IsWSLInstalled() bool {
if err = cmd.Start(); err != nil {
return false
}
scanner := bufio.NewScanner(transform.NewReader(out, unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()))
result := true
for scanner.Scan() {
line := scanner.Text()
// Windows 11 does not set an error exit code when a kernel is not avail
if strings.Contains(line, "kernel file is not found") {
result = false
break
}

kernelNotFound := matchOutputLine(out, "kernel file is not found")

if err := cmd.Wait(); err != nil {
return false
}
if err := cmd.Wait(); !result || err != nil {

return !kernelNotFound
}

func IsWSLStoreVersionInstalled() bool {
cmd := SilentExecCmd("wsl", "--version")
out, err := cmd.StdoutPipe()
cmd.Stderr = nil
if err != nil {
return false
}
if err = cmd.Start(); err != nil {
return false
}
hasVersion := matchOutputLine(out, "WSL version:")
if err := cmd.Wait(); err != nil {
return false
}

return true
return hasVersion
}

func matchOutputLine(output io.ReadCloser, match string) bool {
scanner := bufio.NewScanner(transform.NewReader(output, unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, match) {
return true
}
}
return false
}

0 comments on commit bdc3040

Please sign in to comment.