Skip to content

feat(hyperv): add Hyper-V backend and Windows host support - #173

Open
thejoeejoee wants to merge 3 commits into
stacklok:mainfrom
thejoeejoee:feat/hyperv-backend
Open

thejoeejoee wants to merge 3 commits into
stacklok:mainfrom
thejoeejoee:feat/hyperv-backend

Conversation

@thejoeejoee

Copy link
Copy Markdown
Contributor

Summary

Implements Phases 1–4 of #2 to add a Windows Hyper-V HCS v2 backend and cross-platform Windows host support:

  • Phase 1 — Windows host compatibility:
    • Split Unix-specific syscall.Stat_t, Setsid, process-group signaling, and O_NOFOLLOW calls across internal/pathutil, internal/procutil, extract, rootfs, image, hooks, runner, and root options into platform-specific files.
    • Added Windows preflight checks in preflight/hyperv_windows.go and preflight/resources_windows.go verifying vmcompute.dll (HcsEnumerateComputeSystems), disk space (GetDiskFreeSpaceEx), CPU, and memory (GlobalMemoryStatusEx).
    • Added defaultBackend() split (default_backend_unix.go -> libkrun.NewBackend(), default_backend_windows.go -> hyperv.NewBackend()).
  • Phase 2 — Pure-Go ext4 + fixed-VHD rootfs builder (image/vhd):
    • Added image/vhd.BuildFromDir using github.com/Microsoft/hcsshim/ext4/tar2ext4 (ConvertWhiteout, AppendVhdFooter, MaximumDiskSize) to stream a prepared rootfs directory into an ext4 image with a fixed VHD footer without host disk mounting or CGO.
    • Preserves user.containers.override_stat metadata when present and restores executable bits on standard binary paths when extracted on Windows NTFS.
  • Phase 3 — Hyper-V HCS v2 backend (hypervisor/hyperv):
    • Implemented hypervisor.Backend and hypervisor.VMHandle in hypervisor/hyperv.
    • Translates hypervisor.VMConfig into an HCS Schema v2.1 ComputeSystem document (LinuxKernelDirect, SCSI controller 0 with LUN 0 rootfs VHD + extra Disks, Plan9 shares for FilesystemMounts, and HvSocket service table configuration).
    • Binds vmcompute.dll on Windows (HcsCreateComputeSystem, HcsStartComputeSystem, HcsShutdownComputeSystem, HcsTerminateComputeSystem, HcsGetComputeSystemProperties, HcsCloseComputeSystem) with async callback notification handling behind an injectable Runner interface.
  • Phase 4 — AF_HYPERV networking, guest 9p mounts, and guest hv_sock ↔ eth0 TAP relay:
    • Extended net/hosted.Provider with Endpoint() (net.EndpointProvider), injectable listener factories, and Windows hvsock (AF_HYPERV) support feeding vn.AcceptQemu.
    • Added 9p (trans=virtio,version=9p2000.L,msize=524288) mount support in guest/mount and guest/vmconfig.
    • Added guest/netcfg.BridgeStreamToPacket and StartHVSocketTAP to bridge guest AF_VSOCK streams to a Linux eth0 TAP interface using gvisor-tap-vsock's 4-byte big-endian length-prefixed Ethernet frame protocol.

Closes #2

Verification

  • CGO_ENABLED=0 go test ./... (excluding krun and go-microvm-runner) on Linux, including end-to-end ARP round-trip tests through BridgeStreamToPacket + virtualnetwork.AcceptQemu and ext4 superblock/VHD footer verification
  • GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build across all non-CGO packages
  • golangci-lint run on GOOS=linux, GOOS=darwin, and GOOS=windows

@thejoeejoee

Copy link
Copy Markdown
Contributor Author

Remaining Phase 5 Scope, Architectural Gaps & Open Questions

This PR lands Phases 1–4 of #2 (Windows host compatibility & preflight, pure-Go tar2ext4 VHD builder, hypervisor/hyperv HCS Schema v2 backend, AF_HYPERV host listener + guest hv_sock ↔ eth0 TAP L2 relay + guest 9p mounts).

Below is a summary of what remains for Phase 5 (end-to-end runtime & CI) along with open design questions for maintainers.


1. Remaining Gaps (Phase 5)

  1. Linux Kernel & Guest Init Packaging (vmlinux + initrd.img / /.krun_init)
    • hyperv.Backend resolves vmlinux and initrd.img from WithKernelPath / WithInitrdPath, WithRuntime(extract.Source), or <DataDir>/vmlinux, and writes /.krun_config.json + /etc/go-microvm.json into rootfs.vhd.
    • Unlike libkrun (where libkrunfw embeds its own guest /init that reads /.krun_config.json), a stock OCI image (alpine:latest) booted via Hyper-V LinuxKernelDirect does not include a guest init binary that reads /etc/go-microvm.json + /.krun_config.json, starts netcfg.StartHVSocketTAP, runs guest/boot.Run(), and execs the container entrypoint under guest/reaper.
  2. Host-Side WithVsockPort (AF_HYPERV ↔ AF_UNIX) Bridge
    • BuildComputeSystemDoc registers each configured VsockPort (VsockPortToServiceGUID(port.Port)) in the HCS HvSocket ServiceTable, which authorizes the port in Hyper-V.
    • However, callers of WithVsockPort(Port, SocketPath) (e.g. ttrpc over vsock) expect a host Unix socket at SocketPath. A small host-side proxy is still needed on Windows to bridge SocketPath (AF_UNIX) ↔ AF_HYPERV (vmID, serviceGUID).
  3. Multi-VM Dynamic HVSocketNetPort Coordination
    • net/hosted on Windows probes ports 2048..2111 when binding (GUIDWildcard, ServiceID) so concurrent VMs on the same host do not collide on port 2048, and BuildComputeSystemDoc registers the bound service GUID from NetEndpoint.Path.
    • Because /etc/go-microvm.json is written into rootfs.vhd during PrepareRootFS (before netProvider.Start runs), a second concurrent VM binding port 2049+ will need either netProvider.Start to run before step 3b (InjectVMConfig) or the selected port to be passed via KernelCmdLine (microvm.hvsock_net_port=<port>).
  4. Crash-Recovery State Persistence for Non-PID Backends
    • state.State currently tracks PID int for terminateStaleRunner. Since Hyper-V HCS compute systems are identified by compute system ID rather than a child runner PID (and already set ShouldTerminateOnLastHandleClosed: true), State may optionally record the backend ID string if explicit HcsOpenComputeSystem + HcsTerminateComputeSystem cleanup on stale dataDir is desired.

2. Open Questions for Maintainers

  1. Guest init delivery mechanism:
    • Should the Hyper-V runtime bundle an initrd.img containing a static Linux PID 1 binary (built from guest/boot + guest/reaper + /.krun_config.json runner) that mounts /dev/sda (rootfs.vhd) and switch_roots into the container workload?
    • Or should PrepareRootFS inject that static Linux init binary directly into rootfs.vhd (e.g. /.krun_init) and set init=/.krun_init on KernelCmdLine without requiring an initrd.img?
  2. Kernel artifact distribution:
    • How should go-microvm distribute the minimal Linux kernel (vmlinux with CONFIG_HYPERV=y, CONFIG_HYPERV_STORAGE=y, CONFIG_HYPERV_VSOCKETS=y, CONFIG_NET_9P_VIRTIO=y, CONFIG_TUN=y, CONFIG_EXT4_FS=y) — as a release asset fetched via extract.Source (similar to libkrun runtime extraction), or via an OCI artifact?
  3. Plan9 ownership overrides (OverrideUID / OverrideGID):
    • Windows HCS Plan9 shares do not interpret user.containers.override_stat xattrs. Should OverrideUID / OverrideGID on VirtioFSMount translate to uid=<uid>,gid=<gid> mount options in guest/mount for 9p, or should they be rejected on Windows like SimplifiedOwnership?
  4. Windows CI workflow:
    • Should we add a windows-latest job (go test ./... and go build ./...) to .github/workflows/ci.yml in this PR so Windows build and unit-test coverage are enforced on every PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Hyper-V backend for Windows support

1 participant