Skip to content

Support attaching /dev/diskN block devices on macOS VZ hosts#4866

Open
pirate wants to merge 1 commit into
lima-vm:masterfrom
pirate:vz-block-device-sharing
Open

Support attaching /dev/diskN block devices on macOS VZ hosts#4866
pirate wants to merge 1 commit into
lima-vm:masterfrom
pirate:vz-block-device-sharing

Conversation

@pirate
Copy link
Copy Markdown

@pirate pirate commented Apr 22, 2026

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:

limactl start --block-device=/dev/disk4 ...

It can also be configured in YAML with the top-level blockDevices field.

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/disk4 appears in the guest under /dev/disk/by-id/virtio-disk4.

Implementation

  • added the --block-device CLI flag and top-level blockDevices config support
  • attached host block devices in the VZ driver with VZDiskBlockDeviceStorageDeviceAttachment
  • kept block-device-specific VZ runtime logic in pkg/driver/vz/block_device_darwin.go
  • added a hidden limactl sudo-open-block-device helper that opens a host block device and passes the file descriptor back to the unprivileged VM process
  • kept the helper path backend-neutral in pkg/blockdevice
  • moved generic sudo execution and sudoers rendering helpers into pkg/sudoers
  • kept pkg/networks network-specific by limiting it to network sudoers entries and network validation
  • updated limactl sudoers to compose the shared /etc/sudoers.d/lima file from network entries plus the block-device helper entry
  • documented top-level blockDevices in templates/default.yaml and the disk docs
  • non-VZ backends reject blockDevices explicitly for now

Layout Notes

The ownership boundaries are now:

  • pkg/networks: network config, validation, and network-specific sudoers fragments
  • pkg/sudoers: generic sudo invocation and sudoers file helpers
  • pkg/blockdevice: generic block-device helper, request validation, fd handoff, and sudoers fragment
  • pkg/driver/vz/block_device_darwin.go: VZ block-device attachment logic
  • cmd/limactl sudoers: existing CLI command, combines network sudoers entries with blockdevice entries to create the final /etc/sudoers.d/lima file
  • cmd/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 process

That 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/networks
  • go test -c ./pkg/driver/vz
  • go test -c ./pkg/driver/krunkit
  • go test -c ./cmd/limactl
  • GOOS=windows GOARCH=amd64 go test -c ./pkg/driver/wsl2
  • ./hack/bats/lib/bats-core/bin/bats --count ./hack/bats/extras/vz-block-device.bats

Live validation on macOS with VZ block-device roundtrip coverage:

  • --block-device /dev/diskN roundtrip via BATS
  • top-level blockDevices: YAML using /dev/rdiskN roundtrip via BATS
  • both cases create a ramdisk, partition it as GPT, format it as exFAT, write from the guest, stop the VM, mount and read on the host, append on the host, restart, and read back in the guest

Round-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.

@pirate pirate force-pushed the vz-block-device-sharing branch from 9e58e98 to 9bf5c16 Compare April 22, 2026 21:00
@AkihiroSuda AkihiroSuda added this to the v2.2.0 milestone Apr 22, 2026
Comment thread pkg/driver/external/client/client.go Outdated
@whoschek
Copy link
Copy Markdown

Very cool!
Would love to see functionality like this to enable backup to ZFS pools on external drives!

@pirate pirate force-pushed the vz-block-device-sharing branch 3 times, most recently from 8b49990 to 7c3f8b2 Compare April 22, 2026 23:36
@pirate pirate marked this pull request as ready for review April 22, 2026 23:39
@pirate pirate changed the title Support host block device sharing for VZ on macOS Support attaching /dev/diskN block devices on macOS VZ hosts Apr 23, 2026
@pirate

This comment was marked as resolved.

@pirate pirate force-pushed the vz-block-device-sharing branch 6 times, most recently from 255afa8 to d36259d Compare April 23, 2026 06:12
Comment thread templates/default.yaml Outdated
Comment thread templates/default.yaml Outdated
Comment thread pkg/driver/vz/block_device_darwin.go
Comment thread hack/test-templates.sh
Comment thread templates/default.yaml Outdated
@pirate pirate force-pushed the vz-block-device-sharing branch 7 times, most recently from b3029e8 to 5301add Compare April 24, 2026 02:41
@pirate

This comment was marked as resolved.

Comment thread website/content/en/docs/config/disk.md Outdated
Comment thread website/content/en/docs/config/disk.md Outdated
@pirate pirate force-pushed the vz-block-device-sharing branch 2 times, most recently from 042a21e to 30325ab Compare April 24, 2026 08:27
@AkihiroSuda
Copy link
Copy Markdown
Member

Merge branch 'master' into vz-block-device-sharing

Please tidy up the commit messages
https://lima-vm.io/docs/dev/git/#squashing-commits

@pirate pirate force-pushed the vz-block-device-sharing branch 2 times, most recently from d005b6e to 3712ca7 Compare May 10, 2026 19:52
@pirate

This comment has been minimized.

@AkihiroSuda
Copy link
Copy Markdown
Member

Sorry, needs rebasing again

Signed-off-by: Nick Sweeting <git@sweeting.me>
@pirate pirate force-pushed the vz-block-device-sharing branch from 9088514 to 439bf1f Compare June 2, 2026 02:27
Copy link
Copy Markdown
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda requested a review from a team June 2, 2026 06:28
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.

3 participants