Support attaching /dev/diskN block devices on macOS VZ hosts#4866
Open
pirate wants to merge 1 commit into
Open
Support attaching /dev/diskN block devices on macOS VZ hosts#4866pirate wants to merge 1 commit into
/dev/diskN block devices on macOS VZ hosts#4866pirate wants to merge 1 commit into
Conversation
9e58e98 to
9bf5c16
Compare
pirate
commented
Apr 22, 2026
|
Very cool! |
8b49990 to
7c3f8b2
Compare
/dev/diskN block devices on macOS VZ hosts
This comment was marked as resolved.
This comment was marked as resolved.
255afa8 to
d36259d
Compare
AkihiroSuda
reviewed
Apr 23, 2026
AkihiroSuda
reviewed
Apr 23, 2026
AkihiroSuda
reviewed
Apr 23, 2026
AkihiroSuda
reviewed
Apr 23, 2026
AkihiroSuda
reviewed
Apr 23, 2026
b3029e8 to
5301add
Compare
This comment was marked as resolved.
This comment was marked as resolved.
AkihiroSuda
reviewed
Apr 24, 2026
AkihiroSuda
reviewed
Apr 24, 2026
042a21e to
30325ab
Compare
Member
Please tidy up the commit messages |
d005b6e to
3712ca7
Compare
This comment has been minimized.
This comment has been minimized.
Member
|
Sorry, needs rebasing again |
Signed-off-by: Nick Sweeting <git@sweeting.me>
9088514 to
439bf1f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1314 & #2224
📖 Wrote a blog post based on my learnings too: https://docs.sweeting.me/s/sockets-101
Summary
This adds VZ-only support on macOS for attaching host block devices to Lima VMs.
A VM can now be launched with:
It can also be configured in YAML with the top-level
blockDevicesfield.The attached device is exposed to the guest as a virtio block device, and Lima sets a deterministic block device identifier from the host basename. For example,
/dev/disk4appears in the guest under/dev/disk/by-id/virtio-disk4.Implementation
--block-deviceCLI flag and top-levelblockDevicesconfig supportVZDiskBlockDeviceStorageDeviceAttachmentpkg/driver/vz/block_device_darwin.golimactl sudo-open-block-devicehelper that opens a host block device and passes the file descriptor back to the unprivileged VM processpkg/blockdevicepkg/sudoerspkg/networksnetwork-specific by limiting it to network sudoers entries and network validationlimactl sudoersto compose the shared/etc/sudoers.d/limafile from network entries plus the block-device helper entryblockDevicesintemplates/default.yamland the disk docsblockDevicesexplicitly for nowLayout Notes
The ownership boundaries are now:
pkg/networks: network config, validation, and network-specific sudoers fragmentspkg/sudoers: generic sudo invocation and sudoers file helperspkg/blockdevice: generic block-device helper, request validation, fd handoff, and sudoers fragmentpkg/driver/vz/block_device_darwin.go: VZ block-device attachment logiccmd/limactl sudoers: existing CLI command, combines network sudoers entries with blockdevice entries to create the final/etc/sudoers.d/limafilecmd/limactl sudo-open-block-device: small helper that runs as root to get a file descriptor for the block device before passing it to the still-rootless main Lima VM processThat keeps block-device support separate from network behavior while still reusing the same sudo helpers that both need.
Why
Opening
/dev/disk*on macOS requires elevated privileges, but Lima generally avoids running its normal VM lifecycle as root. This change keeps that behavior intact by escalating only for the narrow helper that opens the requested block device and passes the file descriptor back to the unprivileged VZ process.Adding this feature to Lima unlocks many use-cases around testing and using custom filesystem kernel modules, e.g. ZFS-in-lima on macOS hosts that don't have ZFS/macFUSE installed: https://github.com/pirate/zfsbox
Validation
Automated:
go test ./pkg/limayaml ./cmd/limactl/editflags ./pkg/driver/qemu ./pkg/blockdevice ./pkg/sudoers ./pkg/networksgo test -c ./pkg/driver/vzgo test -c ./pkg/driver/krunkitgo test -c ./cmd/limactlGOOS=windows GOARCH=amd64 go test -c ./pkg/driver/wsl2./hack/bats/lib/bats-core/bin/bats --count ./hack/bats/extras/vz-block-device.batsLive validation on macOS with VZ block-device roundtrip coverage:
--block-device /dev/diskNroundtrip via BATSblockDevices:YAML using/dev/rdiskNroundtrip via BATSGPT, format it asexFAT, write from the guest, stop the VM, mount and read on the host, append on the host, restart, and read back in the guestRound-trip writes succeed and the block device is usable as a normal drive in both OSs.
Related Links
Next Steps
There are more VZ APIs we can wire up to forward things like
/dev/tty0-usbmodem.sock, USB HCI devices, and more.