| 1 | |
| 2 | |
| 3 | |
| 4 | package wsl2 |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "embed" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "net" |
| 12 | "regexp" |
| 13 | |
| 14 | "github.com/Microsoft/go-winio" |
| 15 | "github.com/Microsoft/go-winio/pkg/guid" |
| 16 | "github.com/sirupsen/logrus" |
| 17 | |
| 18 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 19 | "github.com/lima-vm/lima/v2/pkg/freeport" |
| 20 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 21 | "github.com/lima-vm/lima/v2/pkg/reflectutil" |
| 22 | "github.com/lima-vm/lima/v2/pkg/windows" |
| 23 | ) |
| 24 | |
| 25 | var knownYamlProperties = []string{ |
| 26 | "Arch", |
| 27 | "Audio", |
| 28 | "Containerd", |
| 29 | "CopyToHost", |
| 30 | "CPUType", |
| 31 | "Disk", |
| 32 | "DNS", |
| 33 | "Env", |
| 34 | "HostResolver", |
| 35 | "Images", |
| 36 | "Message", |
| 37 | "Mounts", |
| 38 | "MountType", |
| 39 | "Param", |
| 40 | "Plain", |
| 41 | "PortForwards", |
| 42 | "Probes", |
| 43 | "PropagateProxyEnv", |
| 44 | "Provision", |
| 45 | "SSH", |
| 46 | "TPM", |
| 47 | "VMType", |
| 48 | } |
| 49 | |
| 50 | const Enabled = true |
| 51 | |
| 52 | type LimaWslDriver struct { |
| 53 | Instance *limatype.Instance |
| 54 | |
| 55 | SSHLocalPort int |
| 56 | vSockPort int |
| 57 | virtioPort string |
| 58 | } |
| 59 | |
| 60 | var _ driver.Driver = (*LimaWslDriver)(nil) |
| 61 | |
| 62 | func New() *LimaWslDriver { |
| 63 | port, err := freeport.VSock() |
| 64 | if err != nil { |
| 65 | logrus.WithError(err).Error("failed to get free VSock port") |
| 66 | } |
| 67 | |
| 68 | return &LimaWslDriver{ |
| 69 | vSockPort: port, |
| 70 | virtioPort: "", |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func (l *LimaWslDriver) Configure(ctx context.Context, inst *limatype.Instance) (*driver.ConfiguredDriver, error) { |
| 75 | cfg := inst.Config |
| 76 | if cfg.VMType == nil { |
| 77 | cfg.VMType = new(limatype.WSL2) |
| 78 | } |
| 79 | if cfg.MountType == nil { |
| 80 | cfg.MountType = new(limatype.WSLMount) |
| 81 | } |
| 82 | if err := validateConfig(ctx, cfg); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | l.Instance = inst |
| 87 | l.SSHLocalPort = inst.SSHLocalPort |
| 88 | |
| 89 | return &driver.ConfiguredDriver{ |
| 90 | Driver: l, |
| 91 | }, nil |
| 92 | } |
| 93 | |
| 94 | func (l *LimaWslDriver) Validate(ctx context.Context) error { |
| 95 | return validateConfig(ctx, l.Instance.Config) |
| 96 | } |
| 97 | |
| 98 | func validateConfig(_ context.Context, cfg *limatype.LimaYAML) error { |
| 99 | if cfg == nil { |
| 100 | return errors.New("configuration is nil") |
| 101 | } |
| 102 | if cfg.MountType != nil && *cfg.MountType != limatype.WSLMount { |
| 103 | return fmt.Errorf("field `mountType` must be %#q for WSL2 driver, got %#q", limatype.WSLMount, *cfg.MountType) |
| 104 | } |
| 105 | TODO: |
| 106 | if cfg.VMType != nil { |
| 107 | if unknown := reflectutil.UnknownNonEmptyFields(cfg, knownYamlProperties...); len(unknown) > 0 { |
| 108 | logrus.Warnf("Ignoring: vmType %s: %+v", *cfg.VMType, unknown) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if cfg.OS != nil && *cfg.OS == limatype.WINDOWS { |
| 113 | return errors.New("currently Windows guest OS is only supported on QEMU") |
| 114 | } |
| 115 | |
| 116 | if !limatype.IsNativeArch(*cfg.Arch) { |
| 117 | return fmt.Errorf("unsupported arch: %#q", *cfg.Arch) |
| 118 | } |
| 119 | |
| 120 | if cfg.TPM != nil && *cfg.TPM { |
| 121 | return errors.New("field `tpm` is not supported on WSL2 driver") |
| 122 | } |
| 123 | |
| 124 | if cfg.VMType != nil { |
| 125 | if cfg.Images != nil && cfg.Arch != nil { |
| 126 | TODO: |
| 127 | tarFileRegex := regexp.MustCompile(`\.(tar|tgz|txz|tbz2|tzst|tar\.(gz|xz|bz2|zstd|zst))$`) |
| 128 | unsupportedVMImgRegex := regexp.MustCompile(`\.(qcow2|raw|img|iso|ipsw)(\.(gz|xz|bz2|zstd|zst))?$`) |
| 129 | squashfsRegex := regexp.MustCompile(`\.squashfs(\.(gz|xz|bz2|zstd|zst))?$`) |
| 130 | for i, image := range cfg.Images { |
| 131 | if unknown := reflectutil.UnknownNonEmptyFields(image, "File", "Variant", "ArchVariant"); len(unknown) > 0 { |
| 132 | logrus.Warnf("Ignoring: vmType %s: images[%d]: %+v", *cfg.VMType, i, unknown) |
| 133 | } |
| 134 | if image.Arch == *cfg.Arch { |
| 135 | location := image.Location |
| 136 | if !tarFileRegex.MatchString(location) { |
| 137 | if unsupportedVMImgRegex.MatchString(location) { |
| 138 | return fmt.Errorf("unsupported image type for %s: %q. %s only supports importing tar archive root filesystems, not standard VM disk images", *cfg.VMType, location, *cfg.VMType) |
| 139 | } |
| 140 | if squashfsRegex.MatchString(location) { |
| 141 | return fmt.Errorf("unsupported image type for %s: %q. %s does not natively support importing SquashFS images; please convert the image to a tar archive before importing", *cfg.VMType, location, *cfg.VMType) |
| 142 | } |
| 143 | return fmt.Errorf("unsupported image type for %s: %q. A tar archive root filesystem (.tar, .tar.gz, .tar.xz, etc.) is required", *cfg.VMType, location) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if cfg.Mounts != nil { |
| 150 | for i, mount := range cfg.Mounts { |
| 151 | if unknown := reflectutil.UnknownNonEmptyFields(mount); len(unknown) > 0 { |
| 152 | logrus.Warnf("Ignoring: vmType %s: mounts[%d]: %+v", *cfg.VMType, i, unknown) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if cfg.Networks != nil { |
| 158 | for i, network := range cfg.Networks { |
| 159 | if unknown := reflectutil.UnknownNonEmptyFields(network); len(unknown) > 0 { |
| 160 | logrus.Warnf("Ignoring: vmType %s: networks[%d]: %+v", *cfg.VMType, i, unknown) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | audio := cfg.Audio |
| 166 | if audio.Device != nil && *audio.Device == "" { |
| 167 | audio.Device = nil |
| 168 | } |
| 169 | if audio.Interface != nil && *audio.Interface == "" { |
| 170 | audio.Interface = nil |
| 171 | } |
| 172 | if audio.Microphone != nil && !*audio.Microphone { |
| 173 | audio.Microphone = nil |
| 174 | } |
| 175 | if unknown := reflectutil.UnknownNonEmptyFields(audio); len(unknown) > 0 { |
| 176 | logrus.Warnf("Ignoring: vmType %s: audio: %+v", *cfg.VMType, unknown) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return nil |
| 181 | } |
| 182 | |
| 183 | |
| 184 | var bootLinuxFS embed.FS |
| 185 | |
| 186 | func (l *LimaWslDriver) BootScripts(_ context.Context) (map[string][]byte, error) { |
| 187 | scripts := make(map[string][]byte) |
| 188 | |
| 189 | entries, err := bootLinuxFS.ReadDir("boot.Linux") |
| 190 | if err != nil { |
| 191 | return scripts, err |
| 192 | } |
| 193 | |
| 194 | for _, entry := range entries { |
| 195 | if entry.IsDir() { |
| 196 | continue |
| 197 | } |
| 198 | entryPath := "boot.Linux/" + entry.Name() |
| 199 | |
| 200 | content, err := bootLinuxFS.ReadFile(entryPath) |
| 201 | if err != nil { |
| 202 | return nil, err |
| 203 | } |
| 204 | |
| 205 | scripts[entryPath] = content |
| 206 | } |
| 207 | |
| 208 | return scripts, nil |
| 209 | } |
| 210 | |
| 211 | func (l *LimaWslDriver) InspectStatus(ctx context.Context, inst *limatype.Instance) string { |
| 212 | status, err := getWslStatus(ctx, inst.Name) |
| 213 | if err != nil { |
| 214 | inst.Status = limatype.StatusBroken |
| 215 | inst.Errors = append(inst.Errors, err) |
| 216 | } else { |
| 217 | inst.Status = status |
| 218 | } |
| 219 | |
| 220 | inst.SSHLocalPort = 22 |
| 221 | |
| 222 | if inst.Status == limatype.StatusRunning { |
| 223 | sshAddr, err := getSSHAddress(ctx, inst.Name) |
| 224 | if err == nil { |
| 225 | inst.SSHAddress = sshAddr |
| 226 | } else { |
| 227 | inst.Errors = append(inst.Errors, err) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return inst.Status |
| 232 | } |
| 233 | |
| 234 | func (l *LimaWslDriver) Delete(ctx context.Context) error { |
| 235 | distroName := "lima-" + l.Instance.Name |
| 236 | status, err := getWslStatus(ctx, l.Instance.Name) |
| 237 | if err != nil { |
| 238 | return err |
| 239 | } |
| 240 | switch status { |
| 241 | case limatype.StatusRunning, limatype.StatusStopped, limatype.StatusBroken, limatype.StatusInstalling: |
| 242 | return unregisterVM(ctx, distroName) |
| 243 | } |
| 244 | |
| 245 | logrus.Info("WSL VM is not running or does not exist, skipping deletion") |
| 246 | return nil |
| 247 | } |
| 248 | |
| 249 | func (l *LimaWslDriver) Start(ctx context.Context) (chan error, error) { |
| 250 | if l.Instance.Config.SSH.OverVsock != nil && *l.Instance.Config.SSH.OverVsock { |
| 251 | |
| 252 | logrus.Warn(".ssh.overVsock is not supported for WSL2 driver") |
| 253 | } |
| 254 | logrus.Infof("Starting WSL VM") |
| 255 | status, err := getWslStatus(ctx, l.Instance.Name) |
| 256 | if err != nil { |
| 257 | return nil, err |
| 258 | } |
| 259 | |
| 260 | distroName := "lima-" + l.Instance.Name |
| 261 | |
| 262 | if status == limatype.StatusUninitialized { |
| 263 | if err := EnsureFs(ctx, l.Instance); err != nil { |
| 264 | return nil, err |
| 265 | } |
| 266 | if err := initVM(ctx, l.Instance.Dir, distroName); err != nil { |
| 267 | return nil, err |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | errCh := make(chan error) |
| 272 | |
| 273 | if err := startVM(ctx, distroName); err != nil { |
| 274 | return nil, err |
| 275 | } |
| 276 | |
| 277 | if err := provisionVM( |
| 278 | ctx, |
| 279 | l.Instance.Dir, |
| 280 | l.Instance.Name, |
| 281 | distroName, |
| 282 | errCh, |
| 283 | ); err != nil { |
| 284 | return nil, err |
| 285 | } |
| 286 | |
| 287 | keepAlive(ctx, distroName, errCh) |
| 288 | |
| 289 | return errCh, err |
| 290 | } |
| 291 | |
| 292 | |
| 293 | TODO: |
| 294 | func (l *LimaWslDriver) canRunGUI() bool { |
| 295 | return false |
| 296 | } |
| 297 | |
| 298 | func (l *LimaWslDriver) RunGUI(_ context.Context) error { |
| 299 | return fmt.Errorf("RunGUI is not supported for the given driver '%s' and display '%s'", "wsl", *l.Instance.Config.Video.Display) |
| 300 | } |
| 301 | |
| 302 | func (l *LimaWslDriver) Stop(ctx context.Context) error { |
| 303 | logrus.Info("Shutting down WSL2 VM") |
| 304 | distroName := "lima-" + l.Instance.Name |
| 305 | return stopVM(ctx, distroName) |
| 306 | } |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | func (l *LimaWslDriver) GuestAgentConn(ctx context.Context) (net.Conn, string, error) { |
| 312 | VMIDStr, err := windows.GetInstanceVMID(ctx, fmt.Sprintf("lima-%s", l.Instance.Name)) |
| 313 | if err != nil { |
| 314 | return nil, "", err |
| 315 | } |
| 316 | VMIDGUID, err := guid.FromString(VMIDStr) |
| 317 | if err != nil { |
| 318 | return nil, "", err |
| 319 | } |
| 320 | sockAddr := &winio.HvsockAddr{ |
| 321 | VMID: VMIDGUID, |
| 322 | ServiceID: winio.VsockServiceID(uint32(l.vSockPort)), |
| 323 | } |
| 324 | conn, err := winio.Dial(ctx, sockAddr) |
| 325 | if err != nil { |
| 326 | return nil, "", err |
| 327 | } |
| 328 | |
| 329 | return conn, "vsock", nil |
| 330 | } |
| 331 | |
| 332 | func (l *LimaWslDriver) Info(_ context.Context) driver.Info { |
| 333 | var info driver.Info |
| 334 | info.Name = "wsl2" |
| 335 | if l.Instance != nil { |
| 336 | info.InstanceDir = l.Instance.Dir |
| 337 | } |
| 338 | info.VirtioPort = l.virtioPort |
| 339 | info.VsockPort = l.vSockPort |
| 340 | |
| 341 | info.Features = driver.DriverFeatures{ |
| 342 | DynamicSSHAddress: true, |
| 343 | StaticSSHPort: true, |
| 344 | SkipSocketForwarding: true, |
| 345 | NoCloudInit: true, |
| 346 | CanRunGUI: l.canRunGUI(), |
| 347 | } |
| 348 | return info |
| 349 | } |
| 350 | |
| 351 | func (l *LimaWslDriver) SSHAddress(_ context.Context) (string, error) { |
| 352 | return "127.0.0.1", nil |
| 353 | } |
| 354 | |
| 355 | func (l *LimaWslDriver) Create(_ context.Context) error { |
| 356 | return nil |
| 357 | } |
| 358 | |
| 359 | func (l *LimaWslDriver) CreateDisk(_ context.Context) error { |
| 360 | return nil |
| 361 | } |
| 362 | |
| 363 | func (l *LimaWslDriver) ChangeDisplayPassword(_ context.Context, _ string) error { |
| 364 | return nil |
| 365 | } |
| 366 | |
| 367 | func (l *LimaWslDriver) DisplayConnection(_ context.Context) (string, error) { |
| 368 | return "", nil |
| 369 | } |
| 370 | |
| 371 | func (l *LimaWslDriver) CreateSnapshot(_ context.Context, _ string) error { |
| 372 | return errUnimplemented |
| 373 | } |
| 374 | |
| 375 | func (l *LimaWslDriver) ApplySnapshot(_ context.Context, _ string) error { |
| 376 | return errUnimplemented |
| 377 | } |
| 378 | |
| 379 | func (l *LimaWslDriver) DeleteSnapshot(_ context.Context, _ string) error { |
| 380 | return errUnimplemented |
| 381 | } |
| 382 | |
| 383 | func (l *LimaWslDriver) ListSnapshots(_ context.Context) (string, error) { |
| 384 | return "", errUnimplemented |
| 385 | } |
| 386 | |
| 387 | func (l *LimaWslDriver) ForwardGuestAgent(_ context.Context) bool { |
| 388 | |
| 389 | return l.vSockPort == 0 && l.virtioPort == "" |
| 390 | } |
| 391 | |
| 392 | func (l *LimaWslDriver) AdditionalSetupForSSH(_ context.Context) error { |
| 393 | return nil |
| 394 | } |