@@ -7,8 +7,10 @@ import (
77"context"
88_ "embed"
99"fmt"
10+ "os"
1011"os/exec"
1112"path/filepath"
13+ "strconv"
1214"strings"
1315
1416"github.com/lima-vm/lima/pkg/executil"
@@ -20,13 +22,14 @@ import (
2022
2123// startVM calls WSL to start a VM.
2224func startVM (ctx context.Context , distroName string ) error {
23- _ , err := executil .RunUTF16leCommand ([]string {
25+ out , err := executil .RunUTF16leCommand ([]string {
2426"wsl.exe" ,
2527"--distribution" ,
2628distroName ,
2729}, executil .WithContext (& ctx ))
2830if err != nil {
29- return err
31+ return fmt .Errorf ("failed to run `wsl.exe --distribution %s`: %w (out=%q)" ,
32+ distroName , err , string (out ))
3033}
3134return nil
3235}
@@ -35,28 +38,30 @@ func startVM(ctx context.Context, distroName string) error {
3538func initVM (ctx context.Context , instanceDir , distroName string ) error {
3639baseDisk := filepath .Join (instanceDir , filenames .BaseDisk )
3740logrus .Infof ("Importing distro from %q to %q" , baseDisk , instanceDir )
38- _ , err := executil .RunUTF16leCommand ([]string {
41+ out , err := executil .RunUTF16leCommand ([]string {
3942"wsl.exe" ,
4043"--import" ,
4144distroName ,
4245instanceDir ,
4346baseDisk ,
4447}, executil .WithContext (& ctx ))
4548if err != nil {
46- return err
49+ return fmt .Errorf ("failed to run `wsl.exe --import %s %s %s`: %w (out=%q)" ,
50+ distroName , instanceDir , baseDisk , err , string (out ))
4751}
4852return nil
4953}
5054
5155// stopVM calls WSL to stop a running VM.
5256func stopVM (ctx context.Context , distroName string ) error {
53- _ , err := executil .RunUTF16leCommand ([]string {
57+ out , err := executil .RunUTF16leCommand ([]string {
5458"wsl.exe" ,
5559"--terminate" ,
5660distroName ,
5761}, executil .WithContext (& ctx ))
5862if err != nil {
59- return err
63+ return fmt .Errorf ("failed to run `wsl.exe --terminate %s`: %w (out=%q)" ,
64+ distroName , err , string (out ))
6065}
6166return nil
6267}
@@ -70,12 +75,39 @@ func provisionVM(ctx context.Context, instanceDir, instanceName, distroName stri
7075m := map [string ]string {
7176"CIDataPath" : ciDataPath ,
7277}
73- out , err := textutil .ExecuteTemplate (limaBoot , m )
78+ limaBootB , err := textutil .ExecuteTemplate (limaBoot , m )
7479if err != nil {
7580return fmt .Errorf ("failed to construct wsl boot.sh script: %w" , err )
7681}
77- outString := strings .Replace (string (out ), `\r\n` , `\n` , - 1 )
78-
82+ limaBootFile , err := os .CreateTemp ("" , "lima-wsl2-boot-*.sh" )
83+ if err != nil {
84+ return err
85+ }
86+ if _ , err = limaBootFile .Write (limaBootB ); err != nil {
87+ return err
88+ }
89+ limaBootFileWinPath := limaBootFile .Name ()
90+ if err = limaBootFile .Close (); err != nil {
91+ return err
92+ }
93+ // path should be quoted and use \\ as separator
94+ bootFileWSLPath := strconv .Quote (limaBootFileWinPath )
95+ limaBootFilePathOnLinuxB , err := exec .Command (
96+ "wsl.exe" ,
97+ "-d" ,
98+ distroName ,
99+ "bash" ,
100+ "-c" ,
101+ fmt .Sprintf ("wslpath -u %s" , bootFileWSLPath ),
102+ bootFileWSLPath ,
103+ ).Output ()
104+ if err != nil {
105+ os .RemoveAll (limaBootFileWinPath )
106+ // this can return an error with an exit code, which causes it not to be logged
107+ // because main.handleExitCoder() traps it, so wrap the error
108+ return fmt .Errorf ("failed to run wslpath command: %w" , err )
109+ }
110+ limaBootFileLinuxPath := strings .TrimSpace (string (limaBootFilePathOnLinuxB ))
79111go func () {
80112cmd := exec .CommandContext (
81113ctx ,
@@ -84,12 +116,15 @@ func provisionVM(ctx context.Context, instanceDir, instanceName, distroName stri
84116distroName ,
85117"bash" ,
86118"-c" ,
87- outString ,
119+ limaBootFileLinuxPath ,
88120)
89- if _ , err := cmd .CombinedOutput (); err != nil {
121+ out , err := cmd .CombinedOutput ()
122+ os .RemoveAll (limaBootFileWinPath )
123+ logrus .Debugf ("%v: %q" , cmd .Args , string (out ))
124+ if err != nil {
90125* errCh <- fmt .Errorf (
91- "error running wslCommand that executes boot.sh: %w, " +
92- "check /var/log/lima-init.log for more details" , err )
126+ "error running wslCommand that executes boot.sh (%v) : %w, " +
127+ "check /var/log/lima-init.log for more details (out=%q) " , cmd . Args , err , string ( out ) )
93128}
94129
95130for {
@@ -130,13 +165,14 @@ func keepAlive(ctx context.Context, distroName string, errCh *chan error) {
130165// unregisterVM calls WSL to unregister a VM.
131166func unregisterVM (ctx context.Context , distroName string ) error {
132167logrus .Info ("Unregistering WSL2 VM" )
133- _ , err := executil .RunUTF16leCommand ([]string {
168+ out , err := executil .RunUTF16leCommand ([]string {
134169"wsl.exe" ,
135170"--unregister" ,
136171distroName ,
137172}, executil .WithContext (& ctx ))
138173if err != nil {
139- return err
174+ return fmt .Errorf ("failed to run `wsl.exe --unregister %s`: %w (out=%q)" ,
175+ distroName , err , string (out ))
140176}
141177return nil
142178}
0 commit comments