Add optional Virtio memory balloon support#1276
Conversation
Add a per-VM "memoryBalloon" setting (disabled by default) that, when enabled via "tart set <name> --memory-balloon true", attaches a VZVirtioTraditionalMemoryBalloonDeviceConfiguration to the VM, allowing supported guest operating systems to participate in best-effort memory reclaim. A balloon target can be requested at run time via "tart run --balloon-target-memory <MB>". The target is re-applied periodically, since a target set before the guest's virtio-balloon driver probes the device is lost when the guest resets the device while booting (the same applies to guest reboots). The balloon device is skipped for suspendable VMs (similarly to the entropy device) to not interfere with the save/restore support. VM.craftConfiguration() is split into a buildConfiguration() part and a validation part, so that the device wiring can be unit-tested without the "com.apple.security.virtualization" entitlement. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3678f45d82
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
For macOS guests, "tart run --balloon-target-memory" now also enforces the VM's own minimum supported memory size (dictated by the restore image), similarly to how "tart set --memory" restricts the configured memory size. Addresses a PR review comment. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
This PR adds optional support for Apple Virtualization.framework's virtio memory balloon device. When enabled, Tart attaches a
VZVirtioTraditionalMemoryBalloonDeviceConfigurationto the VM configuration, allowing supported guest operating systems to participate in best-effort memory reclaim. The feature is disabled by default and does not change existing VM behavior.Usage
What changed
VMConfiggains amemoryBalloon: Boolsetting (defaultfalse). The key is only written toconfig.jsonwhen enabled, so configurations of VMs that don't use the feature remain byte-identical to those produced by older Tart versions.tart set <vm> --memory-balloon true|falsetoggles the setting.VM.craftConfiguration()attaches aVZVirtioTraditionalMemoryBalloonDeviceConfiguration. The device is skipped for suspendable VMs (similarly to the entropy device) to not interfere with the save/restore support.tart run --balloon-target-memory <MB>setsVZVirtioTraditionalMemoryBalloonDevice.targetVirtualMachineMemorySizeat runtime, with validation: the balloon device must be enabled, the target cannot exceed the VM's configured memory size, cannot be belowVZVirtualMachineConfiguration.minimumAllowedMemorySize, and is rejected together with--suspendable.start()is lost when the guest's virtio-balloon driver resets the device during boot (the same applies to guest reboots).tart balloon <vm>command: Apple's runtime API is only reachable from the process that owns the VM, and Tart has no host-side control channel to a runningtart runprocess (the control socket proxies into the guest agent). Rather than fake it, the target is atart runoption and the docs explain why.VM.craftConfiguration()is split intobuildConfiguration()+ validation, becauseVZVirtualMachineConfiguration.validate()requires thecom.apple.security.virtualizationentitlement that unit tests don't have.docs/quick-start.md.Tests
New
MemoryBalloonTestssuite (all passing):tart set --memory-balloonargument parsing (including rejection of non-boolean values)VZVirtualMachineConfigurationwhen enabled, and never for suspendable VMsThe rest of the unit test suite passes unchanged (except the pre-existing
LayerizerTestscrash on machines without a local Docker registry, which also occurs onmain).Manual validation (ARM64 Ubuntu guest, 4096 MB VM, macOS 26 host)
virtio_balloondriver binds to the device (/sys/bus/virtio/drivers/virtio_balloonpresent).--balloon-target-memory 1024, the guest ballooned from ~3.5 GB available down to ~505 MB available within one re-apply cycle, and shutdown remained clean.Limitations
This is not dynamic memory hot-add, not transparent host memory overcommit, and not a guarantee that guests will return memory. It does not make a VM with 2 GB of real host memory appear as 32 GB to the guest — the guest still sees the configured memory size. Guest support is required, and practical results may vary.
🤖 Generated with Claude Code