@@ -7,8 +7,10 @@ import (
7
7
"context"
8
8
_ "embed"
9
9
"fmt"
10
+ "os"
10
11
"os/exec"
11
12
"path/filepath"
13
+ "strconv"
12
14
"strings"
13
15
14
16
"github.com/lima-vm/lima/pkg/executil"
@@ -20,13 +22,14 @@ import (
20
22
21
23
// startVM calls WSL to start a VM.
22
24
func startVM (ctx context.Context , distroName string ) error {
23
- _ , err := executil .RunUTF16leCommand ([]string {
25
+ out , err := executil .RunUTF16leCommand ([]string {
24
26
"wsl.exe" ,
25
27
"--distribution" ,
26
28
distroName ,
27
29
}, executil .WithContext (& ctx ))
28
30
if err != nil {
29
- return err
31
+ return fmt .Errorf ("failed to run `wsl.exe --distribution %s`: %w (out=%q)" ,
32
+ distroName , err , string (out ))
30
33
}
31
34
return nil
32
35
}
@@ -35,28 +38,30 @@ func startVM(ctx context.Context, distroName string) error {
35
38
func initVM (ctx context.Context , instanceDir , distroName string ) error {
36
39
baseDisk := filepath .Join (instanceDir , filenames .BaseDisk )
37
40
logrus .Infof ("Importing distro from %q to %q" , baseDisk , instanceDir )
38
- _ , err := executil .RunUTF16leCommand ([]string {
41
+ out , err := executil .RunUTF16leCommand ([]string {
39
42
"wsl.exe" ,
40
43
"--import" ,
41
44
distroName ,
42
45
instanceDir ,
43
46
baseDisk ,
44
47
}, executil .WithContext (& ctx ))
45
48
if 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 ))
47
51
}
48
52
return nil
49
53
}
50
54
51
55
// stopVM calls WSL to stop a running VM.
52
56
func stopVM (ctx context.Context , distroName string ) error {
53
- _ , err := executil .RunUTF16leCommand ([]string {
57
+ out , err := executil .RunUTF16leCommand ([]string {
54
58
"wsl.exe" ,
55
59
"--terminate" ,
56
60
distroName ,
57
61
}, executil .WithContext (& ctx ))
58
62
if err != nil {
59
- return err
63
+ return fmt .Errorf ("failed to run `wsl.exe --terminate %s`: %w (out=%q)" ,
64
+ distroName , err , string (out ))
60
65
}
61
66
return nil
62
67
}
@@ -70,12 +75,39 @@ func provisionVM(ctx context.Context, instanceDir, instanceName, distroName stri
70
75
m := map [string ]string {
71
76
"CIDataPath" : ciDataPath ,
72
77
}
73
- out , err := textutil .ExecuteTemplate (limaBoot , m )
78
+ limaBootB , err := textutil .ExecuteTemplate (limaBoot , m )
74
79
if err != nil {
75
80
return fmt .Errorf ("failed to construct wsl boot.sh script: %w" , err )
76
81
}
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 ))
79
111
go func () {
80
112
cmd := exec .CommandContext (
81
113
ctx ,
@@ -84,12 +116,15 @@ func provisionVM(ctx context.Context, instanceDir, instanceName, distroName stri
84
116
distroName ,
85
117
"bash" ,
86
118
"-c" ,
87
- outString ,
119
+ limaBootFileLinuxPath ,
88
120
)
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 {
90
125
* 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 ) )
93
128
}
94
129
95
130
for {
@@ -130,13 +165,14 @@ func keepAlive(ctx context.Context, distroName string, errCh *chan error) {
130
165
// unregisterVM calls WSL to unregister a VM.
131
166
func unregisterVM (ctx context.Context , distroName string ) error {
132
167
logrus .Info ("Unregistering WSL2 VM" )
133
- _ , err := executil .RunUTF16leCommand ([]string {
168
+ out , err := executil .RunUTF16leCommand ([]string {
134
169
"wsl.exe" ,
135
170
"--unregister" ,
136
171
distroName ,
137
172
}, executil .WithContext (& ctx ))
138
173
if err != nil {
139
- return err
174
+ return fmt .Errorf ("failed to run `wsl.exe --unregister %s`: %w (out=%q)" ,
175
+ distroName , err , string (out ))
140
176
}
141
177
return nil
142
178
}
0 commit comments