-
Notifications
You must be signed in to change notification settings - Fork 3k
fix wsl install workflow on machine init command #26201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,6 @@ import ( | |
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/containers/common/pkg/config" | ||
| "github.com/containers/common/pkg/strongunits" | ||
|
|
@@ -22,7 +21,6 @@ import ( | |
| "github.com/containers/podman/v5/pkg/machine/env" | ||
| "github.com/containers/podman/v5/pkg/machine/ignition" | ||
| "github.com/containers/podman/v5/pkg/machine/vmconfigs" | ||
| "github.com/containers/podman/v5/pkg/machine/wsl/wutil" | ||
| "github.com/containers/podman/v5/utils" | ||
| "github.com/containers/storage/pkg/homedir" | ||
| "github.com/sirupsen/logrus" | ||
|
|
@@ -32,7 +30,8 @@ import ( | |
|
|
||
| var ( | ||
| // vmtype refers to qemu (vs libvirt, krun, etc) | ||
| vmtype = define.WSLVirt | ||
| vmtype = define.WSLVirt | ||
| ErrWslNotSupported = errors.New("wsl features not supported or configured correctly") | ||
| ) | ||
|
|
||
| type ExitCodeError struct { | ||
|
|
@@ -95,7 +94,26 @@ func provisionWSLDist(name string, imagePath string, prompt string) (string, err | |
|
|
||
| dist := env.WithPodmanPrefix(name) | ||
| fmt.Println(prompt) | ||
| if err = runCmdPassThrough("wsl", "--import", dist, distTarget, imagePath, "--version", "2"); err != nil { | ||
|
|
||
| // Run WSL import and analyze output for specific errors. | ||
| // If the 'Virtual Machine Platform' feature is disabled, we expect a failure | ||
| // with HCS service-related errors such as: | ||
| // 1. Wsl/Service/RegisterDistro/CreateVm/HCS/ERROR_NOT_SUPPORTED | ||
| // 2. Wsl/Service/RegisterDistro/CreateVm/HCS/HCS_E_SERVICE_NOT_AVAILABLE | ||
| cmdOutput := &bytes.Buffer{} | ||
| err = runCmdPassThroughTee(cmdOutput, "wsl", "--import", dist, distTarget, imagePath, "--version", "2") | ||
| decoder := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder() | ||
| decoded, _, decodeErr := transform.Bytes(decoder, cmdOutput.Bytes()) | ||
| if decodeErr != nil { | ||
| return "", fmt.Errorf("failed to decode WSL output: %w", decodeErr) | ||
| } | ||
| decodedStr := strings.ToLower(string(decoded)) | ||
| for _, substr := range []string{"hcs/error_not_supported", "hcs/hcs_e_service_not_available"} { | ||
| if strings.Contains(decodedStr, substr) { | ||
| return "", ErrWslNotSupported | ||
| } | ||
| } | ||
| if err != nil { | ||
| return "", fmt.Errorf("the WSL import of guest OS failed: %w", err) | ||
| } | ||
|
|
||
|
|
@@ -310,41 +328,6 @@ func writeWslConf(dist string, user string) error { | |
| return nil | ||
| } | ||
|
|
||
| func checkAndInstallWSL(reExec bool) (bool, error) { | ||
| if wutil.IsWSLInstalled() { | ||
| return true, nil | ||
| } | ||
|
|
||
| admin := HasAdminRights() | ||
|
|
||
| if !wutil.IsWSLFeatureEnabled() { | ||
| return false, attemptFeatureInstall(reExec, admin) | ||
| } | ||
|
|
||
| skip := false | ||
| if !reExec && !admin { | ||
| fmt.Println("Launching WSL Kernel Install...") | ||
| if err := launchElevate(wslInstallKernel); err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| skip = true | ||
| } | ||
|
|
||
| if !skip { | ||
| if err := installWslKernel(); err != nil { | ||
| fmt.Fprintf(os.Stderr, wslKernelError, wslInstallKernel) | ||
| return false, err | ||
| } | ||
|
|
||
| if reExec { | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| return true, nil | ||
| } | ||
|
|
||
| func attemptFeatureInstall(reExec, admin bool) error { | ||
| if !winVersionAtLeast(10, 0, 18362) { | ||
| return errors.New("your version of Windows does not support WSL. Update to Windows 10 Build 19041 or later") | ||
|
|
@@ -364,34 +347,34 @@ func attemptFeatureInstall(reExec, admin bool) error { | |
| "If you prefer, you may abort now, and perform a manual installation using the \"wsl --install\" command." | ||
|
|
||
| if !reExec && MessageBox(message, "Podman Machine", false) != 1 { | ||
| return errors.New("the WSL installation aborted") | ||
| return fmt.Errorf("the WSL installation aborted: %w", define.ErrInitRelaunchAttempt) | ||
| } | ||
|
|
||
| if !reExec && !admin { | ||
| return launchElevate("install the Windows WSL Features") | ||
| } | ||
|
|
||
| return installWsl() | ||
| } | ||
|
|
||
| func launchElevate(operation string) error { | ||
| if err := truncateElevatedOutputFile(); err != nil { | ||
| if err := createOrTruncateElevatedOutputFile(); err != nil { | ||
| return err | ||
| } | ||
| err := relaunchElevatedWait() | ||
| if err != nil { | ||
| if eerr, ok := err.(*ExitCodeError); ok { | ||
| if eerr.code == ErrorSuccessRebootRequired { | ||
| fmt.Println("Reboot is required to continue installation, please reboot at your convenience") | ||
| return nil | ||
| return define.ErrInitRelaunchAttempt | ||
| } | ||
| } | ||
|
|
||
| fmt.Fprintf(os.Stderr, "Elevated process failed with error: %v\n\n", err) | ||
| dumpOutputFile() | ||
| fmt.Fprintf(os.Stderr, wslInstallError, operation) | ||
| return fmt.Errorf("%w: %w", err, define.ErrInitRelaunchAttempt) | ||
| } | ||
| return err | ||
| return define.ErrInitRelaunchAttempt | ||
| } | ||
|
|
||
| func installWsl() error { | ||
|
|
@@ -409,44 +392,10 @@ func installWsl() error { | |
| "/featurename:VirtualMachinePlatform", "/all", "/norestart"); isMsiError(err) { | ||
| return fmt.Errorf("could not enable Virtual Machine Feature: %w", err) | ||
| } | ||
| log.Close() | ||
|
|
||
| return reboot() | ||
| } | ||
|
|
||
| func installWslKernel() error { | ||
| log, err := getElevatedOutputFileWrite() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer log.Close() | ||
|
|
||
| message := "Installing WSL Kernel Update" | ||
| fmt.Println(message) | ||
| fmt.Fprintln(log, message) | ||
|
|
||
| backoff := 500 * time.Millisecond | ||
| for i := 0; i < 5; i++ { | ||
| err = runCmdPassThroughTee(log, "wsl", "--update") | ||
| if err == nil { | ||
| break | ||
| } | ||
| // In case of unusual circumstances (e.g. race with installer actions) | ||
| // retry a few times | ||
| message = "An error occurred attempting the WSL Kernel update, retrying..." | ||
| fmt.Println(message) | ||
| fmt.Fprintln(log, message) | ||
| time.Sleep(backoff) | ||
| backoff *= 2 | ||
| } | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("could not install WSL Kernel: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func getElevatedOutputFileName() (string, error) { | ||
| dir, err := homedir.GetDataHome() | ||
| if err != nil { | ||
|
|
@@ -473,24 +422,14 @@ func getElevatedOutputFileWrite() (*os.File, error) { | |
| return getElevatedOutputFile(os.O_WRONLY | os.O_CREATE | os.O_APPEND) | ||
| } | ||
|
|
||
| func appendOutputIfError(write bool, err error) { | ||
| if write && err == nil { | ||
| return | ||
| } | ||
|
|
||
| if file, check := getElevatedOutputFileWrite(); check == nil { | ||
| defer file.Close() | ||
| fmt.Fprintf(file, "Error: %v\n", err) | ||
| } | ||
| } | ||
|
|
||
| func truncateElevatedOutputFile() error { | ||
| func createOrTruncateElevatedOutputFile() error { | ||
| name, err := getElevatedOutputFileName() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return os.Truncate(name, 0) | ||
| _, err = os.Create(name) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the function is called truncate but now it no longer truncates which is confusing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renamed 👍 |
||
| return err | ||
| } | ||
|
|
||
| func getElevatedOutputFile(mode int) (*os.File, error) { | ||
|
|
@@ -572,7 +511,7 @@ func runCmdPassThroughTee(out io.Writer, name string, arg ...string) error { | |
| cmd.Stdin = os.Stdin | ||
| cmd.Stdout = io.MultiWriter(os.Stdout, out) | ||
| cmd.Stderr = io.MultiWriter(os.Stderr, out) | ||
| if err := cmd.Run(); err != nil { | ||
| if err := cmd.Run(); isMsiError(err) { | ||
| return fmt.Errorf("command %s %v failed: %w", name, arg, err) | ||
| } | ||
| return nil | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any reason of why this was moved. IF the order is important then it needs a big comment explaining why.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment. Basically i want to avoid creating any resources before the CreateVM that could be responsible to invoke a new init that will perform the setup. So i switched the order with the
AddSSHConnectionsToPodmanSocket