feat: add user-specified mounts to container machines
Type of Change
- [ ] Bug fix
- [x] New feature
- [ ] Breaking change
- [ ] Documentation update
Motivation and Context
Closes #1805.
container machine currently mounts only the user's home directory, configurable through --home-mount. There is no way to bind-mount additional arbitrary host directories into a machine, which the issue requests for parity with the container --volume workflow.
This change adds a repeatable --mount host:guest[:ro|rw] option to container machine create:
container machine create --mount /Volumes/Project:/Project --mount /tmp/data:/data:ro alpine:3.22
Summary of changes:
MachineConfiggains a self-containedMounttype (source,destination,readOnly) and amountsfield. Specifications are parsed and validated inwith(_:mounts:): the host path must be an existing directory, the guest path must be absolute, the mode must beroorrw(defaultrw), and duplicate destinations are rejected. Paths are resolved to absolute form at parse time.- The field is Codable with backward compatibility. Boot configurations written before this change decode to an empty list, and an empty list is omitted on encode. The field is intentionally skipped on the
ConfigSnapshotDecoderpath, because the system-wide[machine]TOML section cannot represent arrays of structs; mounts are a per-machine value carried inboot-config.json(JSON) only. MachinesService.toContainerConfigappends each configured mount as a virtiofs share at boot, alongside the existing home mount.container machine inspectsurfaces the configured mounts.- Documentation updated in
docs/command-reference.mdanddocs/container-machine.md.
Scope is limited to create time for this revision; mounts are fixed for the lifetime of a machine. Adding or removing mounts on an existing machine via container machine set is deferred, as the current key=value last-wins semantics do not fit a repeatable list.
Testing
- [ ] Tested locally
- [x] Added/updated tests
- [x] Added/updated docs
Ten unit tests were added to MachineConfigTests covering specification parsing (read-write, read-only, multiple mounts), validation failures (missing source, non-directory source, invalid mode, relative destination, malformed input, duplicate destinations), JSON round-tripping, and backward-compatible decoding of configurations without the field. The full unit suite (swift test --skip TestCLI, 573 tests) passes. The integration suite, which boots a virtual machine, has not been run in this environment and should be exercised on an Apple Silicon host before merge.
This change was implemented by Claude (Anthropic), at the request of and reviewed by @danielsyauqi.
@danielsyauqi can you describe how to test these changes locally? You didn't check the Tested locally box so I'm not sure if these changes have been actually locally tested. I'm very interested in this feature and I have tried these changes in my local build and I have run into a couple of problems.
- The
mountsfield is always set to[](an empty array). The values get lost somewhere in encode/decode translation. I tried
and./bin/container machine create --name test-machine --home-mount=none --mount '/Volumes/src/:/src/' alpine
The volume/mount is not in the inspect list, and not actually mounted in the machine container../bin/container machine inspect test-machine - I tried hard coding the volume, that still didn't get any mounts into the machine. Inspect shows the volume/mount if hard coded, but it does not actually mount anything.
I have also tried it with running a local ./bin/container-apiserver.
Since I'm not familiar with this code base I might be using the local binary wrong. Could you clarify how to test these changes?
This feature is great and exactly what I wanted. I hope it continues to be developed to meet the requirements for merging pull requests.
@mareksapota Thanks for testing this, and sorry for the slow follow-up.
The important detail is that machine operations go through the launchd-managed machine-apiserver plugin. Starting ./bin/container-apiserver by itself does not replace that plugin, so it is easy to run a newly built CLI against an older machine service.
I rebased the branch and retested it with the CLI and every service plugin built from the same revision. machine inspect reports both mounts correctly, a file written by the guest through the read-write mount appears on the host, and writing through the read-only mount fails with Read-only file system.
Here is the matched-stack procedure I used:
make container
bin/container system stop
TEST_ROOT="$(mktemp -d)"
mkdir -p "$TEST_ROOT/rw" "$TEST_ROOT/ro" "$TEST_ROOT/logs"
bin/container --debug system start \
--app-root "$TEST_ROOT/app" \
--install-root "$PWD" \
--log-root "$TEST_ROOT/logs" \
--enable-kernel-install \
--timeout 90
bin/container machine create --no-boot \
--name test-machine \
--home-mount=none \
--mount "$TEST_ROOT/rw:/audit-rw:rw" \
--mount "$TEST_ROOT/ro:/audit-ro:ro" \
ghcr.io/linuxcontainers/alpine:3.20
bin/container machine inspect test-machine | jq '.[0].mounts'
bin/container machine run --root -n test-machine -- touch /audit-rw/from-guest
test -f "$TEST_ROOT/rw/from-guest"
# This command should fail with "Read-only file system".
bin/container machine run --root -n test-machine -- touch /audit-ro/blocked
bin/container machine stop test-machine
bin/container machine rm test-machine
bin/container system stop
I also added integration coverage for the create and inspect path plus the actual guest read-write and read-only behavior. If you still get an empty mount list with this setup, please share the output from bin/container system version and bin/container machine inspect test-machine, and I will dig into it. Thanks again for calling out the testing gap.
Hi @katiewasnothere and @mareksapota! This PR adds user-specified mounts for container machines. It has been rebased and now includes matched-stack integration coverage plus manual guest read-write/read-only verification. Could you take a look when you have time? Thanks!
I've been using this PR for a few days without any problems. This seems like it should be an included feature.
I've been using this PR for a few days without any problems. This seems like it should be an included feature.
Great! Hope the maintainer can approve this PR feature to be deployed 😢