| 1 | |
| 2 | |
| 3 | |
| 4 | package hcs |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/Microsoft/go-winio" |
| 16 | "github.com/Microsoft/hcsshim/hcn" |
| 17 | "github.com/docker/go-units" |
| 18 | "github.com/lima-vm/go-qcow2reader/image/vhdx" |
| 19 | "github.com/sirupsen/logrus" |
| 20 | |
| 21 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 22 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 23 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 24 | "github.com/lima-vm/lima/v2/pkg/reflectutil" |
| 25 | ) |
| 26 | |
| 27 | var knownYamlProperties = []string{ |
| 28 | "AdditionalDisks", |
| 29 | "Arch", |
| 30 | "Audio", |
| 31 | "CACertificates", |
| 32 | "Containerd", |
| 33 | "CopyToHost", |
| 34 | "CPUs", |
| 35 | "CPUType", |
| 36 | "Disk", |
| 37 | "DNS", |
| 38 | "Env", |
| 39 | "Firmware", |
| 40 | "GuestInstallPrefix", |
| 41 | "HostResolver", |
| 42 | "Images", |
| 43 | "Memory", |
| 44 | "Message", |
| 45 | "MinimumLimaVersion", |
| 46 | "Mounts", |
| 47 | "MountType", |
| 48 | "MountTypesUnsupported", |
| 49 | "MountInotify", |
| 50 | "NestedVirtualization", |
| 51 | "Networks", |
| 52 | "OS", |
| 53 | "Param", |
| 54 | "Plain", |
| 55 | "PortForwards", |
| 56 | "Probes", |
| 57 | "PropagateProxyEnv", |
| 58 | "Provision", |
| 59 | "SSH", |
| 60 | "TimeZone", |
| 61 | "TPM", |
| 62 | "UpgradePackages", |
| 63 | "User", |
| 64 | "Video", |
| 65 | "VMType", |
| 66 | "VMOpts", |
| 67 | } |
| 68 | |
| 69 | const Enabled = true |
| 70 | |
| 71 | type LimaHcsDriver struct { |
| 72 | Instance *limatype.Instance |
| 73 | |
| 74 | SSHLocalPort int |
| 75 | vSockPort int |
| 76 | virtioPort string |
| 77 | |
| 78 | system hcsSystem |
| 79 | serialListener net.Listener |
| 80 | sshAddress string |
| 81 | } |
| 82 | |
| 83 | var _ driver.Driver = (*LimaHcsDriver)(nil) |
| 84 | |
| 85 | func New() *LimaHcsDriver { |
| 86 | return &LimaHcsDriver{} |
| 87 | } |
| 88 | |
| 89 | func (l *LimaHcsDriver) Configure(ctx context.Context, inst *limatype.Instance) (*driver.ConfiguredDriver, error) { |
| 90 | cfg := inst.Config |
| 91 | if cfg.VMType == nil { |
| 92 | cfg.VMType = new(limatype.HCS) |
| 93 | } |
| 94 | if cfg.MountType == nil { |
| 95 | cfg.MountType = new(limatype.REVSSHFS) |
| 96 | } |
| 97 | if err := validateConfig(ctx, cfg); err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | |
| 101 | l.Instance = inst |
| 102 | l.SSHLocalPort = inst.SSHLocalPort |
| 103 | l.sshAddress = inst.SSHAddress |
| 104 | |
| 105 | return &driver.ConfiguredDriver{ |
| 106 | Driver: l, |
| 107 | }, nil |
| 108 | } |
| 109 | |
| 110 | func (l *LimaHcsDriver) Validate(ctx context.Context) error { |
| 111 | return validateConfig(ctx, l.Instance.Config) |
| 112 | } |
| 113 | |
| 114 | func validateConfig(_ context.Context, cfg *limatype.LimaYAML) error { |
| 115 | if cfg == nil { |
| 116 | return errors.New("configuration is nil") |
| 117 | } |
| 118 | TODO: |
| 119 | if cfg.VMType != nil { |
| 120 | if unknown := reflectutil.UnknownNonEmptyFields(cfg, knownYamlProperties...); len(unknown) > 0 { |
| 121 | logrus.Warnf("Ignoring: vmType %s: %+v", *cfg.VMType, unknown) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if cfg.OS != nil && *cfg.OS == limatype.WINDOWS { |
| 126 | return errors.New("currently Windows guest OS is only supported on QEMU") |
| 127 | } |
| 128 | |
| 129 | if !limatype.IsNativeArch(*cfg.Arch) { |
| 130 | return fmt.Errorf("unsupported arch: %#q", *cfg.Arch) |
| 131 | } |
| 132 | |
| 133 | if cfg.TPM != nil && *cfg.TPM { |
| 134 | return errors.New("field `tpm` is not supported on HCS driver") |
| 135 | } |
| 136 | |
| 137 | if cfg.VMType != nil { |
| 138 | if cfg.Mounts != nil { |
| 139 | for i, mount := range cfg.Mounts { |
| 140 | if unknown := reflectutil.UnknownNonEmptyFields(mount); len(unknown) > 0 { |
| 141 | logrus.Warnf("Ignoring: vmType %s: mounts[%d]: %+v", *cfg.VMType, i, unknown) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if cfg.Networks != nil { |
| 147 | for i, network := range cfg.Networks { |
| 148 | if unknown := reflectutil.UnknownNonEmptyFields(network); len(unknown) > 0 { |
| 149 | logrus.Warnf("Ignoring: vmType %s: networks[%d]: %+v", *cfg.VMType, i, unknown) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if cfg.Audio.Device != nil { |
| 155 | audioDevice := *cfg.Audio.Device |
| 156 | if audioDevice != "" { |
| 157 | logrus.Warnf("Ignoring: vmType %s: `audio.device`: %+v", *cfg.VMType, audioDevice) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if cfg.HostResolver.Enabled == nil { |
| 162 | cfg.HostResolver.Enabled = new(false) |
| 163 | } |
| 164 | if *cfg.HostResolver.Enabled { |
| 165 | return errors.New("the HCS driver does not support HostResolver.Enabled") |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return nil |
| 170 | } |
| 171 | |
| 172 | func (l *LimaHcsDriver) BootScripts(_ context.Context) (map[string][]byte, error) { |
| 173 | return nil, nil |
| 174 | } |
| 175 | |
| 176 | func (l *LimaHcsDriver) InspectStatus(ctx context.Context, inst *limatype.Instance) string { |
| 177 | inst.SSHLocalPort = 22 |
| 178 | state, err := getComputeSystemState(ctx, "lima-"+inst.Name) |
| 179 | if err != nil { |
| 180 | if errors.Is(err, errNotFound) { |
| 181 | inst.Status = limatype.StatusUnknown |
| 182 | return inst.Status |
| 183 | } |
| 184 | inst.Status = limatype.StatusBroken |
| 185 | inst.Errors = append(inst.Errors, fmt.Errorf("failed to inspect HCS sompute system: %w", err)) |
| 186 | return inst.Status |
| 187 | } |
| 188 | if state == "Running" { |
| 189 | inst.Status = limatype.StatusRunning |
| 190 | e, err := hcn.GetEndpointByName(endpointName(inst.Name)) |
| 191 | if err != nil { |
| 192 | logrus.Errorf("failed to get HCN endpoint: %s", err.Error()) |
| 193 | inst.Status = limatype.StatusBroken |
| 194 | return inst.Status |
| 195 | } |
| 196 | ips := e.IpConfigurations |
| 197 | if len(ips) == 0 { |
| 198 | logrus.Errorf("the HCN endpoint for instance %#q does not have an IP address yet", inst.Name) |
| 199 | inst.Status = limatype.StatusBroken |
| 200 | return inst.Status |
| 201 | } |
| 202 | ip := ips[0] |
| 203 | l.sshAddress = ip.IpAddress |
| 204 | inst.SSHAddress = ip.IpAddress |
| 205 | } else { |
| 206 | inst.Status = limatype.StatusStopped |
| 207 | } |
| 208 | return inst.Status |
| 209 | } |
| 210 | |
| 211 | func (l *LimaHcsDriver) Delete(_ context.Context) error { |
| 212 | if endpoint, err := hcn.GetEndpointByName(endpointName(l.Instance.Name)); err == nil { |
| 213 | if err := endpoint.Delete(); err != nil { |
| 214 | return fmt.Errorf("failed to delete the HCN endpoint: %w", err) |
| 215 | } |
| 216 | } |
| 217 | if network, err := hcn.GetNetworkByName(networkName(l.Instance.Name)); err == nil { |
| 218 | if err := network.Delete(); err != nil { |
| 219 | return fmt.Errorf("failed to delete the HCN network: %w", err) |
| 220 | } |
| 221 | } |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | func (l *LimaHcsDriver) Start(ctx context.Context) (chan error, error) { |
| 226 | inst := l.Instance |
| 227 | diskPath := filepath.Join(inst.Dir, filenames.Disk) + ".vhdx" |
| 228 | isoPath := filepath.Join(inst.Dir, filenames.CIDataISO) |
| 229 | |
| 230 | network, err := ensureNetwork(networkName(inst.Name)) |
| 231 | if err != nil { |
| 232 | return nil, err |
| 233 | } |
| 234 | endpoint, err := ensureEndpoint(network, endpointName(inst.Name), inst.Dir) |
| 235 | if err != nil { |
| 236 | return nil, err |
| 237 | } |
| 238 | |
| 239 | pipeName := `\\.\pipe\lima-` + inst.Name + `-com1` |
| 240 | ln, err := winio.ListenPipe(pipeName, nil) |
| 241 | if err != nil { |
| 242 | return nil, fmt.Errorf("failed to listen on %#q: %w", pipeName, err) |
| 243 | } |
| 244 | l.serialListener = ln |
| 245 | go serveSerial(ln, filepath.Join(inst.Dir, filenames.SerialLog)) |
| 246 | |
| 247 | vmID := "lima-" + inst.Name |
| 248 | for _, p := range []string{diskPath, isoPath} { |
| 249 | if err := hcsGrantVMAccess(vmID, p); err != nil { |
| 250 | return nil, fmt.Errorf("failed to grant VM access to %#q: %w", p, err) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | memBytes, err := units.RAMInBytes(*inst.Config.Memory) |
| 255 | if err != nil { |
| 256 | return nil, err |
| 257 | } |
| 258 | config, err := buildVMConfig(diskPath, isoPath, endpoint.Id, endpoint.MacAddress, pipeName, *inst.Config.CPUs, memBytes>>20) |
| 259 | if err != nil { |
| 260 | return nil, err |
| 261 | } |
| 262 | logrus.Debugf("HCS compute system config: %s", config) |
| 263 | |
| 264 | system, err := createComputeSystem(ctx, "lima-"+inst.Name, config) |
| 265 | if err != nil { |
| 266 | return nil, err |
| 267 | } |
| 268 | l.system = system |
| 269 | if err := startComputeSystem(ctx, system); err != nil { |
| 270 | return nil, err |
| 271 | } |
| 272 | |
| 273 | for i := range 50 { |
| 274 | endpoint, err = hcn.GetEndpointByName(endpointName(inst.Name)) |
| 275 | if err != nil { |
| 276 | logrus.Warnf("failed in GetEndpointByName: %s", err.Error()) |
| 277 | time.Sleep(2 * time.Second) |
| 278 | continue |
| 279 | } |
| 280 | if len(endpoint.IpConfigurations) == 0 { |
| 281 | if i == 49 { |
| 282 | return nil, fmt.Errorf("HNS did not assign an IP address to the endpoint %#q", endpoint.Name) |
| 283 | } |
| 284 | time.Sleep(2 * time.Second) |
| 285 | continue |
| 286 | } |
| 287 | l.sshAddress = endpoint.IpConfigurations[0].IpAddress |
| 288 | logrus.Infof("HCS: the guest is expected to get %s (MAC %s)", l.sshAddress, endpoint.MacAddress) |
| 289 | break |
| 290 | } |
| 291 | |
| 292 | errCh := make(chan error, 1) |
| 293 | go func() { |
| 294 | result, waitErr := hcsWaitForComputeSystemExit(ctx, system, hcsTimeoutInfinite) |
| 295 | if waitErr != nil { |
| 296 | errCh <- fmt.Errorf("HCS compute system exited: %w (result=%s)", waitErr, result) |
| 297 | return |
| 298 | } |
| 299 | errCh <- nil |
| 300 | }() |
| 301 | return errCh, nil |
| 302 | } |
| 303 | |
| 304 | func (l *LimaHcsDriver) RunGUI(_ context.Context) error { |
| 305 | return fmt.Errorf("RunGUI is not supported for the given driver '%s' and display '%s'", "hcs", *l.Instance.Config.Video.Display) |
| 306 | } |
| 307 | |
| 308 | func (l *LimaHcsDriver) Stop(ctx context.Context) error { |
| 309 | if l.system == 0 { |
| 310 | return nil |
| 311 | } |
| 312 | if err := terminateComputeSystem(ctx, l.system); err != nil { |
| 313 | return err |
| 314 | } |
| 315 | if l.serialListener != nil { |
| 316 | _ = l.serialListener.Close() |
| 317 | } |
| 318 | |
| 319 | if endpoint, err := hcn.GetEndpointByName(endpointName(l.Instance.Name)); err == nil { |
| 320 | if err = endpoint.Delete(); err != nil { |
| 321 | return fmt.Errorf("failed to delete the HCN endpoint: %w", err) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if network, err := hcn.GetNetworkByName(networkName(l.Instance.Name)); err == nil { |
| 326 | if err := network.Delete(); err != nil { |
| 327 | return fmt.Errorf("failed to delete the HCN network %w", err) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return nil |
| 332 | } |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | func (l *LimaHcsDriver) GuestAgentConn(_ context.Context) (net.Conn, string, error) { |
| 338 | return nil, "", nil |
| 339 | } |
| 340 | |
| 341 | func (l *LimaHcsDriver) Info(_ context.Context) driver.Info { |
| 342 | var info driver.Info |
| 343 | info.Name = "hcs" |
| 344 | if l.Instance != nil { |
| 345 | info.InstanceDir = l.Instance.Dir |
| 346 | } |
| 347 | info.VirtioPort = l.virtioPort |
| 348 | info.VsockPort = l.vSockPort |
| 349 | |
| 350 | info.Features = driver.DriverFeatures{ |
| 351 | DynamicSSHAddress: true, |
| 352 | StaticSSHPort: true, |
| 353 | SkipSocketForwarding: true, |
| 354 | NoCloudInit: false, |
| 355 | CanRunGUI: false, |
| 356 | SupportedImageFormats: []string{string(vhdx.Type)}, |
| 357 | } |
| 358 | return info |
| 359 | } |
| 360 | |
| 361 | func (l *LimaHcsDriver) SSHAddress(_ context.Context) (string, error) { |
| 362 | if l.sshAddress == "" { |
| 363 | return "", errors.New("the SSH address is not known yet") |
| 364 | } |
| 365 | return l.sshAddress, nil |
| 366 | } |
| 367 | |
| 368 | func (l *LimaHcsDriver) Create(_ context.Context) error { |
| 369 | return nil |
| 370 | } |
| 371 | |
| 372 | func (l *LimaHcsDriver) CreateDisk(ctx context.Context) error { |
| 373 | if err := mayConvertQcow2ToVHDX(ctx, l.Instance.Dir); err != nil { |
| 374 | return err |
| 375 | } |
| 376 | disk := filepath.Join(l.Instance.Dir, filenames.Disk) |
| 377 | link := disk + ".vhdx" |
| 378 | if _, err := os.Stat(link); err == nil { |
| 379 | return nil |
| 380 | } else if !errors.Is(err, os.ErrNotExist) { |
| 381 | return err |
| 382 | } |
| 383 | return os.Link(disk, link) |
| 384 | } |
| 385 | |
| 386 | func (l *LimaHcsDriver) ChangeDisplayPassword(_ context.Context, _ string) error { |
| 387 | return nil |
| 388 | } |
| 389 | |
| 390 | func (l *LimaHcsDriver) DisplayConnection(_ context.Context) (string, error) { |
| 391 | return "", nil |
| 392 | } |
| 393 | |
| 394 | func (l *LimaHcsDriver) CreateSnapshot(_ context.Context, _ string) error { |
| 395 | return errUnimplemented |
| 396 | } |
| 397 | |
| 398 | func (l *LimaHcsDriver) ApplySnapshot(_ context.Context, _ string) error { |
| 399 | return errUnimplemented |
| 400 | } |
| 401 | |
| 402 | func (l *LimaHcsDriver) DeleteSnapshot(_ context.Context, _ string) error { |
| 403 | return errUnimplemented |
| 404 | } |
| 405 | |
| 406 | func (l *LimaHcsDriver) ListSnapshots(_ context.Context) (string, error) { |
| 407 | return "", errUnimplemented |
| 408 | } |
| 409 | |
| 410 | func (l *LimaHcsDriver) ForwardGuestAgent(_ context.Context) bool { |
| 411 | |
| 412 | return l.vSockPort == 0 && l.virtioPort == "" |
| 413 | } |
| 414 | |
| 415 | func (l *LimaHcsDriver) AdditionalSetupForSSH(_ context.Context) error { |
| 416 | return nil |
| 417 | } |