·5 min read

Run Rust tests in a Linux VM on a Mac (Lima)

How we ran Linux-only Rust tests from a Mac with a Lima VM, and why "No space left on device" in the results was the VM's fault, not our code.

testingworkflowci-cd

Contents

Some of our Rust tests only mean something on Linux. They check file ownership, extended attributes and ACLs on a real ext4 disk, and some of them need to run as root and then act as another user. A Mac cannot do that, and for a stretch we could not use hosted CI either. This post is the local setup we used instead: a Lima virtual machine, the exact commit copied in, and one trap that cost us a failed run. The trap was the disk filling up, which showed as test failures that were not bugs.

Why not Docker

We tried a Docker container first. One test, kill_recovery, could not even start in that harness. It failed to spawn there, and it had done the same before any of our changes. A VM gave us a normal Linux kernel with a normal ext4 root, and that test ran. Our code also refuses overlayfs on purpose, because ownership and inode behavior there depend on layers it cannot check, and containers usually sit on overlayfs. So a container would probably have tested the refusal path, not the real one.

Setup

Install Lima and create an Ubuntu VM:

brew install lima
limactl create --name pp3d --vm-type vz --cpus 4 --memory 6 --disk 20 \
  --mount-none --tty=false template:ubuntu-24.04
limactl start pp3d

--mount-none matters. It keeps the Mac's home directory out of the VM, so the tests run on the VM's own ext4 and not on a shared mount that behaves differently. The VM reported Ubuntu 24.04, kernel 6.8, aarch64, with / and /tmp both on the same ext4 disk.

Inside it, install the compiler and the tools the tests call:

limactl shell pp3d -- bash -lc '
  sudo apt-get install -y build-essential acl curl ca-certificates
  curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal \
    --default-toolchain 1.86.0 -c rustfmt -c clippy'

acl gives you setfacl, which one test uses to create an ACL. setpriv, which we use to drop to another user, is already part of Ubuntu.

Test the exact commit, not the working tree

We copied the code in with git archive, so the VM sees exactly what is committed and nothing else:

git archive --format=tar HEAD | limactl shell pp3d -- bash -lc \
  'rm -rf ~/src && mkdir ~/src && tar -x -C ~/src'

Then, in the VM, with the gate variable that turns a skipped foreign-user test into a failure:

PP3D_REQUIRE_FOREIGN=1 cargo test --workspace --locked

The run passed: 378 tests passed, 0 failed, 7 ignored, across 29 suites. The 7 ignored are the tests that need root. cargo fmt --all -- --check and cargo clippy --workspace --all-targets --locked -- -D warnings passed in the VM too.

The trap: "No space left on device"

Later, the macOS run of the same gate failed in two places. One was a unit test, manifest::tests::entry_count_is_capped_on_disk. The other was a release-gate self-test script. A copy step in the script printed:

could not copy extended attributes to ... : No space left on device

The cause was the disk of the Mac itself. Free space on / had dropped to about 117 MiB. Three things had piled up: the Lima VM had grown to about 5.5 GB, and there were two separate Cargo target/ directories, one on the host and one inside the VM. Debug builds of a Rust workspace are large, and each place you build gets its own copy.

The reason this is a nasty failure is how it looks. A test named for an on-disk cap fails, so you start reading the code under test. Nothing in the code is wrong. The disk is full, and any test that writes will fail.

Why the VM disk grows

A Lima disk image is sparse. The 20 GB figure is a limit. The file on the Mac only holds the blocks the guest has written. That is helpful at first and misleading later: the guest thinks it has plenty of room, and the host quietly pays for every package, toolchain and build artifact. Deleting files inside the VM does not always give the space back to the host either, so the file can stay large after you clean up inside it.

How to tell a full disk from a real failure

  • Look for No space left on device (ENOSPC) in the failure text before you read anything else.
  • Run df -h / on the host. Run it in the VM too, if you have one.
  • Failures that appear in unrelated places at the same moment, here a unit test and a shell script, point at the machine and not the code.

The cleanup

We did not delete anything on our own, since it was the owner's machine, and asked instead. The two commands that freed the most space were:

limactl delete -f pp3d   # about 5.5 GB
cargo clean              # the host target directory

The --disk 20 we set is a ceiling, not a reservation, and the disk file is sparse. It grows as the VM writes. A 20 GB limit still ended up as 5.5 GB on the host after one toolchain and one build. If you will keep the VM around, delete its target/ inside after each run, or build with a CARGO_TARGET_DIR you can wipe in one command.

Summary

  • A Lima VM with --mount-none is a reasonable local stand-in for a Linux CI job when you need real ext4, ACLs or root.
  • Copy the commit with git archive, so the test input is the same as what you would push.
  • Watch free space. Two target/ directories and a VM disk add up faster than you expect, and the failure looks like a bug in your tests.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.