FreeBSD bhyve Live Migration: Moving VMs Between Hosts

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

FreeBSD bhyve Live Migration: Moving VMs Between Hosts

This project presents a first working state for live migration of bhyve guests on FreeBSD 15.1. A running amd64 guest can move between two hosts over TCP while continuing to run; it is paused only for the final stop-and-copy phase. The work builds on bhyve’s experimental snapshot support and adds dirty-page tracking in the vmm kernel module together with a migration protocol in bhyve and bhyvectl.

本项目展示了 FreeBSD 15.1 上 bhyve 客户机实时迁移的初步工作状态。运行中的 amd64 客户机可以在两台主机之间通过 TCP 进行迁移,同时保持运行;仅在最后的“停止并复制”阶段会暂停。该工作基于 bhyve 的实验性快照支持,并在 vmm 内核模块中增加了脏页跟踪功能,同时在 bhyve 和 bhyvectl 中实现了迁移协议。

Live migration of a VM in FreeBSD with bhyve I previously discussed the state of bhyve live migration, its historical development, existing limitations, and why it matters for the future of FreeBSD virtualization in my article bhyve on FreeBSD and VM Live Migration – Quo vadis?. The goal is deliberately practical: make it possible to test, discuss, and improve a real implementation rather than discuss live migration only in the abstract. The current code and test tooling are being published as a WIP RFC so that the next steps can be shaped in the open. A more detailed demonstration of a VM being live migrated can be watched on my YouTube channel.

关于 FreeBSD 中 bhyve 的虚拟机实时迁移,我曾在文章《bhyve on FreeBSD and VM Live Migration – Quo vadis?》中讨论过其现状、历史发展、现有局限性以及它对 FreeBSD 虚拟化未来的重要性。此项目的目标非常务实:让人们能够测试、讨论并改进一个真实的实现,而不是仅仅在抽象层面讨论实时迁移。目前的代码和测试工具正以 WIP RFC(工作进行中征求意见稿)的形式发布,以便在公开环境下规划后续步骤。关于虚拟机实时迁移的更详细演示,可以在我的 YouTube 频道观看。

Why this matters Live migration is an important building block for enterprise virtualisation. It enables host maintenance, hardware replacement, load balancing, and failure-domain management with far less disruption to workloads. It is also central to the Sylve project, which aims to offer a Proxmox-like virtualisation solution for FreeBSD. Migration is not an optional convenience in that kind of platform; it is one of the capabilities that makes clustered operations practical. It is equally important for BoxyBSD, where I offer free BSD-based VPS instances to individuals. Without a credible migration path, routine host work can require a guest outage. That makes live migration a current show stopper for the project, and is why this work is being pushed toward a testable, reviewable state.

为何这很重要:实时迁移是企业级虚拟化的重要基石。它能够以极小的业务中断实现主机维护、硬件更换、负载均衡和故障域管理。它也是 Sylve 项目的核心,该项目旨在为 FreeBSD 提供类似 Proxmox 的虚拟化解决方案。在那种平台中,迁移并非可有可无的便利功能,而是使集群化运维成为现实的关键能力之一。对于我向个人提供免费基于 BSD 的 VPS 实例的 BoxyBSD 项目而言,这一点同样重要。如果没有可靠的迁移路径,常规的主机维护工作可能导致客户机停机。这使得实时迁移成为该项目目前必须解决的瓶颈,也是这项工作被推动至可测试、可审查状态的原因。

How the current implementation works The source starts with a pre-copy phase. It sends populated guest memory, then repeatedly obtains a bitmap of pages changed since the previous scan and sends only those pages. Once the remaining dirty set fits the configured downtime target, stops shrinking, or reaches the configured round limit, bhyve pauses the guest and its devices. It then sends the final dirty pages and the existing snapshot state for the vCPUs, kernel state, and emulated devices. The destination validates the basic VM geometry, receives memory and state, restores it, and reports that it is ready. Only then does the source send GO, the commit point. Before GO, a failed migration can resume the source guest. The destination does not start the guest if it does not receive that commit message. A warm, stop-and-copy mode is available too, which is useful both as a fallback and as a comparison baseline.

当前实现的工作原理:源端从预复制阶段开始。它首先发送已填充的客户机内存,然后重复获取自上次扫描以来发生变化的页面位图,并仅发送这些页面。一旦剩余的脏页集符合配置的停机时间目标、停止缩减或达到配置的轮次限制,bhyve 就会暂停客户机及其设备。随后,它会发送最终的脏页以及 vCPU、内核状态和模拟设备的现有快照状态。目标端验证虚拟机的基本几何结构,接收内存和状态,进行恢复,并报告准备就绪。只有这时,源端才会发送 GO(提交点)。在 GO 之前,如果迁移失败,源端客户机可以恢复运行。如果目标端未收到该提交消息,则不会启动客户机。此外还提供了一种“热”停止并复制模式,既可用作备选方案,也可用作比较基准。

What is implemented today Pre-copy and warm migration: live pre-copy is configurable with downtime and maximum-round controls; explicit stop-and-copy migration is also supported. Multi-vCPU protocol support: the migration handshake carries the guest vCPU count, the destination refuses a count mismatch, and all guest vCPUs are paused and resumed as part of migration. The published test bed has primarily exercised a one-vCPU guest, so multi-vCPU stress and race testing remain required before making broader claims. Memory consistency checks: dirty-page tracking covers guest and mapped emulation writes through page dirty state, while vmm write holds are recorded separately. The test workload rewrites and verifies guest memory and reports any missing or stale page as a mismatch. Repeated ping-pong migrations have been used to check that the workload continues without errors. Disk-write validation: a guest-side workload repeatedly writes and fsyncs files, verifies checksums, and is checked again after a cold reboot. This is intended to exercise the device pause, drain, and restore path. Failure handling: destination refusal, an unreachable target, and a target disappearing during both pre-copy and stop-and-copy have been tested. Before commit, the source is intended to resume the paused guest after failure. Basic destination checks: protocol version, page size, memory layout, and vCPU count are checked before data transfer. The migration socket is opened by bhyvectl and passed into bhyve so that bhyve can remain inside its Capsicum sandbox. Supported scope: this is amd64-only and depends on the experimental BHYVE_SNAPSHOT support. It relies on snapshot-capable emulated devices; PCI passthrough is not migratablem, yet.

目前已实现的功能:预复制和热迁移:实时预复制可通过停机时间和最大轮次控制进行配置;同时也支持显式的停止并复制迁移。多 vCPU 协议支持:迁移握手包含客户机 vCPU 数量,目标端会拒绝数量不匹配的情况,且所有客户机 vCPU 都会作为迁移的一部分被暂停和恢复。已发布的测试平台主要针对单 vCPU 客户机进行了测试,因此在做出更广泛的结论之前,仍需进行多 vCPU 的压力和竞争测试。内存一致性检查:脏页跟踪通过页面脏状态覆盖客户机和映射的模拟写入,而 vmm 写入保持则单独记录。测试工作负载会重写并验证客户机内存,并将任何丢失或过期的页面报告为不匹配。重复的往返迁移已被用于检查工作负载是否能无错误地持续运行。磁盘写入验证:客户机端的工作负载会重复写入并同步文件、验证校验和,并在冷重启后再次检查。这旨在测试设备的暂停、排空和恢复路径。故障处理:已测试了目标端拒绝、目标不可达以及在预复制和停止并复制期间目标消失的情况。在提交之前,源端旨在故障后恢复已暂停的客户机。基本目标端检查:在数据传输前会检查协议版本、页面大小、内存布局和 vCPU 数量。迁移套接字由 bhyvectl 打开并传递给 bhyve,以便 bhyve 能够保持在其 Capsicum 沙盒内。支持范围:仅限 amd64,并依赖于实验性的 BHYVE_SNAPSHOT 支持。它依赖于支持快照的模拟设备;PCI 直通目前尚不可迁移。

Observed results In the current two-host test environment, repeated live migrations, memory-rewrite workloads, and fsync-heavy disk workloads have completed successfully. For a slowly dirtying 512 MiB guest, observed downtime has been roughly 138–171 ms. A rapidly dirtying guest may not converge during pre-copy, but still falls back to a correct stop-and-copy completion with a longer pause. These are encouraging engineering results, not a production support promise or a complete correctness proof.

观察到的结果:在当前的两主机测试环境中,重复的实时迁移、内存重写工作负载以及高 fsync 磁盘工作负载均已成功完成。对于一个缓慢脏化的 512 MiB 客户机,观察到的停机时间大约为 138–171 毫秒。快速脏化的客户机可能无法在预复制期间收敛,但仍会回退到正确的“停止并复制”完成方式,只是停机时间会更长。这些是令人鼓舞的工程结果,而非生产环境支持承诺或完整的正确性证明。

Blockers before upstreaming can be evaluated further The following are neutral engineering requirements, not criticism of the value of the prototype. They need to be addressed first before further upstreaming into FreeBSD can be evaluated responsibly. Prove and harden dirty-log correctness. The kernel-side dirty-page algorithm must be demonstrated not to lose a write while dirty bits are examined and cleared. It needs focused concurrency, paging, emulation-write, and multi-vCPU stress coverage, plus a reviewable locking and memory-ordering argument. Define a complete compatibility contract. Matching memory size and vCPU count is not enough. A destination should verify guest identity, CPU capabilities, bhyve/vmm and snapshot format compatibility, device topology and capabilities, and shared-storage identity before accepting a migration. Harden the transport and protocol. The current TCP transport has no authentication or encryption and uses a first-version native pr

在进一步评估上游合并前的阻碍:以下是中立的工程需求,而非对原型价值的批评。在负责任地评估将其进一步合并到 FreeBSD 上游之前,必须先解决这些问题。证明并加固脏日志的正确性:必须证明内核端的脏页算法在检查和清除脏位时不会丢失写入。这需要重点关注并发性、分页、模拟写入和多 vCPU 压力覆盖,以及可审查的锁定和内存排序论证。定义完整的兼容性契约:仅匹配内存大小和 vCPU 数量是不够的。目标端在接受迁移前,应验证客户机身份、CPU 能力、bhyve/vmm 和快照格式兼容性、设备拓扑和能力以及共享存储身份。加固传输和协议:当前的 TCP 传输没有身份验证或加密,并使用第一版原生 pr