Skip to content

Commit a3d9d5a

Browse files
committed
update wsl2 to new changes
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent 11e0b05 commit a3d9d5a

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

pkg/driver/wsl2/vm_windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import (
1616
"github.com/sirupsen/logrus"
1717

1818
"github.com/lima-vm/lima/v2/pkg/executil"
19+
"github.com/lima-vm/lima/v2/pkg/limatype"
20+
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
1921
"github.com/lima-vm/lima/v2/pkg/store"
20-
"github.com/lima-vm/lima/v2/pkg/store/filenames"
2122
"github.com/lima-vm/lima/v2/pkg/textutil"
2223
)
2324

@@ -133,7 +134,7 @@ func provisionVM(ctx context.Context, instanceDir, instanceName, distroName stri
133134
<-ctx.Done()
134135
logrus.Info("Context closed, stopping vm")
135136
if status, err := store.GetWslStatus(instanceName); err == nil &&
136-
status == store.StatusRunning {
137+
status == limatype.StatusRunning {
137138
_ = stopVM(ctx, distroName)
138139
}
139140
}

pkg/driver/wsl2/wsl_driver_windows.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515

1616
"github.com/lima-vm/lima/v2/pkg/driver"
1717
"github.com/lima-vm/lima/v2/pkg/freeport"
18+
"github.com/lima-vm/lima/v2/pkg/limatype"
1819
"github.com/lima-vm/lima/v2/pkg/limayaml"
20+
"github.com/lima-vm/lima/v2/pkg/ptr"
1921
"github.com/lima-vm/lima/v2/pkg/reflectutil"
2022
"github.com/lima-vm/lima/v2/pkg/store"
2123
"github.com/lima-vm/lima/v2/pkg/windows"
@@ -47,7 +49,7 @@ var knownYamlProperties = []string{
4749
const Enabled = true
4850

4951
type LimaWslDriver struct {
50-
Instance *store.Instance
52+
Instance *limatype.Instance
5153

5254
SSHLocalPort int
5355
vSockPort int
@@ -68,7 +70,7 @@ func New() *LimaWslDriver {
6870
}
6971
}
7072

71-
func (l *LimaWslDriver) Configure(inst *store.Instance) *driver.ConfiguredDriver {
73+
func (l *LimaWslDriver) Configure(inst *limatype.Instance) *driver.ConfiguredDriver {
7274
l.Instance = inst
7375
l.SSHLocalPort = inst.SSHLocalPort
7476

@@ -77,9 +79,33 @@ func (l *LimaWslDriver) Configure(inst *store.Instance) *driver.ConfiguredDriver
7779
}
7880
}
7981

82+
func (l *LimaWslDriver) AcceptConfig(cfg *limatype.LimaYAML, filepath string) error {
83+
if l.Instance == nil {
84+
l.Instance = &limatype.Instance{}
85+
}
86+
l.Instance.Config = cfg
87+
88+
if err := l.Validate(); err != nil {
89+
return fmt.Errorf("config not supported by the WSL2 driver: %w", err)
90+
}
91+
92+
return nil
93+
}
94+
95+
func (l *LimaWslDriver) FillConfig(cfg *limatype.LimaYAML, filePath string) error {
96+
if cfg.VMType == nil {
97+
cfg.VMType = ptr.Of(limatype.WSL2)
98+
}
99+
if cfg.MountType == nil {
100+
cfg.MountType = ptr.Of(limatype.WSLMount)
101+
}
102+
103+
return nil
104+
}
105+
80106
func (l *LimaWslDriver) Validate() error {
81-
if *l.Instance.Config.MountType != limayaml.WSLMount {
82-
return fmt.Errorf("field `mountType` must be %q for WSL2 driver, got %q", limayaml.WSLMount, *l.Instance.Config.MountType)
107+
if *l.Instance.Config.MountType != limatype.WSLMount {
108+
return fmt.Errorf("field `mountType` must be %q for WSL2 driver, got %q", limatype.WSLMount, *l.Instance.Config.MountType)
83109
}
84110
// TODO: revise this list for WSL2
85111
if unknown := reflectutil.UnknownNonEmptyFields(l.Instance.Config, knownYamlProperties...); len(unknown) > 0 {
@@ -131,7 +157,7 @@ func (l *LimaWslDriver) Start(ctx context.Context) (chan error, error) {
131157

132158
distroName := "lima-" + l.Instance.Name
133159

134-
if status == store.StatusUninitialized {
160+
if status == limatype.StatusUninitialized {
135161
if err := EnsureFs(ctx, l.Instance); err != nil {
136162
return nil, err
137163
}
@@ -184,7 +210,7 @@ func (l *LimaWslDriver) Unregister(ctx context.Context) error {
184210
return err
185211
}
186212
switch status {
187-
case store.StatusRunning, store.StatusStopped, store.StatusBroken, store.StatusInstalling:
213+
case limatype.StatusRunning, limatype.StatusStopped, limatype.StatusBroken, limatype.StatusInstalling:
188214
return unregisterVM(ctx, distroName)
189215
}
190216

pkg/limayaml/defaults.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,6 @@ func FillDefault(y, d, o *limatype.LimaYAML, filePath string, warn bool) {
208208
if o.VMType != nil {
209209
y.VMType = o.VMType
210210
}
211-
// if y.VMType != nil && *y.VMType != "" && *y.VMType != "default" {
212-
// logrus.Debugf("ResolveVMType: VMType %q is explicitly specified in %q", *y.VMType, filePath)
213-
// _, _, exists := registry.Get(*y.VMType)
214-
// if !exists {
215-
// logrus.Warnf("ResolveVMType: VMType %q is not registered", *y.VMType)
216-
// }
217-
// *y.VMType = limatype.NewVMType(*y.VMType)
218-
// }
219211

220212
if y.OS == nil {
221213
y.OS = d.OS

pkg/store/instance_windows.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99
"strings"
1010

1111
"github.com/lima-vm/lima/v2/pkg/executil"
12-
"github.com/lima-vm/lima/v2/pkg/limayaml"
12+
"github.com/lima-vm/lima/v2/pkg/limatype"
1313
)
1414

15-
func inspectStatus(instDir string, inst *Instance, y *limayaml.LimaYAML) {
16-
if inst.VMType == limayaml.WSL2 {
15+
func inspectStatus(instDir string, inst *limatype.Instance, y *limatype.LimaYAML) {
16+
if inst.VMType == limatype.WSL2 {
1717
status, err := GetWslStatus(inst.Name)
1818
if err != nil {
19-
inst.Status = StatusBroken
19+
inst.Status = limatype.StatusBroken
2020
inst.Errors = append(inst.Errors, err)
2121
} else {
2222
inst.Status = status
2323
}
2424

2525
inst.SSHLocalPort = 22
2626

27-
if inst.Status == StatusRunning {
27+
if inst.Status == limatype.StatusRunning {
2828
sshAddr, err := GetSSHAddress(inst.Name)
2929
if err == nil {
3030
inst.SSHAddress = sshAddr
@@ -76,17 +76,17 @@ func GetWslStatus(instName string) (string, error) {
7676
}
7777

7878
if out == "" {
79-
return StatusBroken, fmt.Errorf("failed to read instance state for instance %q, try running `wsl --list --verbose` to debug, err: %w", instName, err)
79+
return limatype.StatusBroken, fmt.Errorf("failed to read instance state for instance %q, try running `wsl --list --verbose` to debug, err: %w", instName, err)
8080
}
8181

8282
// Check for edge cases first
8383
if strings.Contains(out, "Windows Subsystem for Linux has no installed distributions.") {
8484
if strings.Contains(out, "Wsl/WSL_E_DEFAULT_DISTRO_NOT_FOUND") {
85-
return StatusBroken, fmt.Errorf(
85+
return limatype.StatusBroken, fmt.Errorf(
8686
"failed to read instance state for instance %q because no distro is installed,"+
8787
"try running `wsl --install -d Ubuntu` and then re-running Lima", instName)
8888
}
89-
return StatusBroken, fmt.Errorf(
89+
return limatype.StatusBroken, fmt.Errorf(
9090
"failed to read instance state for instance %q because there is no WSL kernel installed,"+
9191
"this usually happens when WSL was installed for another user, but never for your user."+
9292
"Try running `wsl --install -d Ubuntu` and `wsl --update`, and then re-running Lima", instName)
@@ -109,7 +109,7 @@ func GetWslStatus(instName string) (string, error) {
109109
}
110110

111111
if instState == "" {
112-
return StatusUninitialized, nil
112+
return limatype.StatusUninitialized, nil
113113
}
114114

115115
return instState, nil

0 commit comments

Comments
 (0)