Skip to content

feat: add user-specified mounts to container machines - #1837

Open
danielsyauqi wants to merge 6 commits into
apple:mainfrom
danielsyauqi:feat/machine-user-mounts
Open

danielsyauqi wants to merge 6 commits into
apple:mainfrom
danielsyauqi:feat/machine-user-mounts

Conversation

@danielsyauqi

@danielsyauqi danielsyauqi commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix
  • 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:

  • MachineConfig gains a self-contained Mount type (source, destination, readOnly) and a mounts field. Specifications are parsed and validated in with(_:mounts:): the host path must be an existing directory, the guest path must be absolute, the mode must be ro or rw (default rw), 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 ConfigSnapshotDecoder path because the system-wide [machine] TOML section cannot represent arrays of structs. Mounts are a per-machine value carried in boot-config.json only.
  • MachinesService.toContainerConfig appends each configured mount as a virtiofs share at boot, alongside the existing home mount.
  • container machine inspect surfaces the configured mounts.
  • Documentation and regression tests cover configuration parsing, CLI persistence, and guest runtime behavior.

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 through container machine set is deferred because the current key=value last-wins semantics do not fit a repeatable list.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

Tested on Apple M5, macOS 26.5, Swift 6.3.3, using a debug build with all CLI and service components built from the same revision.

  • make container: passed and packaged the CLI and service plugins.
  • make test: warnings-as-errors build passed, followed by all 765 non-integration tests.
  • swift test --filter MachineConfigTests: all 22 tests passed.
  • TestCLIMachineCommand/testCreateWithMounts: passed. It verifies that repeated read-write and read-only mount options survive create, persistence, and inspect.
  • TestCLIMachineRuntimeSerial/testUserMountsReadWriteAndReadOnly: passed. It boots the machine, writes from the guest through the read-write mount, verifies the file on the host, and confirms that the read-only mount rejects writes.

The same read-write propagation and read-only enforcement were also verified manually against an isolated application root.

@mareksapota

Copy link
Copy Markdown
Contributor

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

  1. The mounts field is always set to [] (an empty array). The values get lost somewhere in encode/decode translation. I tried
    ./bin/container machine create --name test-machine --home-mount=none --mount '/Volumes/src/:/src/' alpine
    
    and
    ./bin/container machine inspect test-machine
    
    The volume/mount is not in the inspect list, and not actually mounted in the machine container.
  2. 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?

@ha-ni-cc

Copy link
Copy Markdown

This feature is great and exactly what I wanted. I hope it continues to be developed to meet the requirements for merging pull requests.

@danielsyauqi
danielsyauqi force-pushed the feat/machine-user-mounts branch from cee5710 to eb6be3b Compare August 10, 2026 12:58
@danielsyauqi

Copy link
Copy Markdown
Contributor Author

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

@danielsyauqi

Copy link
Copy Markdown
Contributor Author

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!

@conklech

Copy link
Copy Markdown

I've been using this PR for a few days without any problems. This seems like it should be an included feature.

@danielsyauqi

Copy link
Copy Markdown
Contributor Author

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 😢

@jcbl1

jcbl1 commented Sep 9, 2026

Copy link
Copy Markdown

Sorry if this isn't the right place or time to ask. Is it possible to add additional binds to an existing machine?

@danielsyauqi

Copy link
Copy Markdown
Contributor Author

Yes, this should be possible without recreating the machine, although it would require stopping and restarting it, the mounts are applied at boot rather than hot-plugged into a running machine.

I can look into adding support for updating an existing machine if the maintainers would prefer it in this PR.

Add a repeatable --mount host:guest[:ro|rw] option to container machine create. Persist validated mounts in the machine boot configuration, expose them through inspect, and add them as virtiofs shares at boot. Include backward-compatible decoding, documentation, and unit and integration coverage.
@danielsyauqi
danielsyauqi force-pushed the feat/machine-user-mounts branch from 2332c52 to aba26f5 Compare September 14, 2026 10:36
@danielsyauqi

Copy link
Copy Markdown
Contributor Author

Hi @jglogan, could you help triage this PR or point me to the appropriate reviewer? It implements #1805 and is rebased onto current main.

The branch now complies with the signed-commit requirement, and the resulting tree is identical to the version covered by the unit, integration, and manual runtime tests described above.

I’m happy to adjust the create-time-only scope if the team prefers.

Thanks!

@danielsyauqi

Copy link
Copy Markdown
Contributor Author

@katiewasnothere @egernst, could either of you help review or route this PR when you have time? The multiple-mount capability being independently requested in #2278.

The scope remains limited to create-time, repeatable --mount configuration. The branch is current with main and mergeable, with unit, integration, and manual read-write/read-only verification documented above.

I’m happy to adjust the scope or implementation if the maintainers prefer a different direction.

@deepio-oc

Copy link
Copy Markdown

@danielsyauqi I tried using your branch but it didnt work for me. I wanted to mount a sub-folder of my home directory as home directory inside machine (do not want to expose full home inside machine). Is this use case supported in your PR?

@danielsyauqi

Copy link
Copy Markdown
Contributor Author

Thanks for trying it, @deepio-oc. Yes, that use case should be supported at machine creation by disabling the default full-home mount and mounting the selected subfolder at the guest home path:

mkdir -p "$HOME/machine-home"

bin/container machine create \
  --name test-machine \
  --home-mount=none \
  --mount "$HOME/machine-home:/home/$(id -un):rw" \
  alpine:3.22

This currently requires a new machine because mounts are fixed at creation time. Also, please ensure the CLI and machine-apiserver plugin are built and running from the same revision.

If this still fails, could you share the exact command, bin/container system version, and bin/container machine inspect test-machine output? I’ll reproduce this specific home-replacement case and add coverage if needed.

@deepio-oc

deepio-oc commented Oct 1, 2026 •

Copy link
Copy Markdown

@danielsyauqi I just tried with your latest code and I dont think it is working.

pkgbuild: Wrote package to bin/release/container-installer-unsigned.pkg
Installing container installer package
Building initfs if containerization is in edit mode

➜  container git:(feat/machine-user-mounts) mkdir -p "$HOME/machine-home"

bin/container machine create \
  --name test-machine \
  --home-mount=none \
  --mount "$HOME/machine-home:/home/$(id -un):rw" \
  alpine:3.22
test-machine
➜  container git:(feat/machine-user-mounts) container machine run --name test-machine
~ $ touch 1
~ $ 
➜  container git:(feat/machine-user-mounts) ls -l $HOME/machine-home
total 0

➜  container git:(feat/machine-user-mounts) bin/container system version             
COMPONENT            VERSION             BUILD    COMMIT
container            1.0.0-142-g93fb2ec  release  93fb2ec32b684acd4f3e6989262977f7bae349dc
container-apiserver  1.4.1               release  unspecified

➜  container git:(feat/machine-user-mounts) bin/container machine inspect test-machine       
[
  {
    "containerId" : "test-machine-c2ac22",
    "cpus" : 9,
    "createdDate" : "2026-10-01T17:48:46Z",
    "diskSize" : 1169309696,
    "homeMount" : "none",
    "id" : "test-machine",
    "image" : {
      "descriptor" : {
        "digest" : "sha256:5291449c3df73caf6ed85e649dec1b9e818b39a5d8c871e97afc13e9cd5e8fa8",
        "mediaType" : "application\/vnd.oci.image.index.v1+json",
        "size" : 9218
      },
      "reference" : "docker.io\/library\/alpine:3.22"
    },
    "ipAddress" : "192.168.64.9",
    "memory" : 68719476736,
    "mounts" : [

    ],
    "platform" : {
      "architecture" : "arm64",
      "os" : "linux"
    },
    "startedDate" : "2026-10-01T17:48:47Z",
    "status" : "running",
    "userSetup" : {
      "gid" : 20,
      "uid" : 501,
      "username" : "x"
    }
  }
]

how do I update the apiserver from the generated binaries?

@danielsyauqi

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed output. Your CLI is built from the PR branch, but system version shows the running container-apiserver is still 1.4.1. From the PR branch’s root, please run:

make container
bin/container system stop
bin/container system start --install-root "$PWD" --enable-kernel-install --timeout 90
bin/container system version
bin/container system status

In system status, paths.installRoot should point to this checkout, and the server commit should match the CLI commit. Then create a new machine with the mount and use bin/container for the run and inspect commands too. The existing machine was created while the old service was running, so its empty mount configuration will persist. If the new machine still shows mounts: [], please share its inspect output and the new status output.

@deepio-oc

Copy link
Copy Markdown

@danielsyauqi I updated the apiserver. One issue I see with your example command is that machine is failing to create user (since home is already mounted I think)

➜  ~ container system version 
COMPONENT            VERSION             BUILD    COMMIT
container            1.0.0-142-g93fb2ec  release  93fb2ec32b684acd4f3e6989262977f7bae349dc
container-apiserver  1.0.0-142-g93fb2ec  release  93fb2ec32b684acd4f3e6989262977f7bae349dc
➜  ~ container machine create \ 
  --name test-machine \
  --home-mount=none \
  --mount "$HOME/machine-home:/home/$(id -un):rw" \
  alpine:3.22
Error: container machine failed to create user

@danielsyauqi

Copy link
Copy Markdown
Contributor Author

Thanks for confirming that the CLI and apiserver now match. You're right: my example exposed a separate first-boot user setup bug. The guest home mount is present before user creation, and setup was trying to initialize the host-backed directory. Sorry for the misleading example.

I pushed fe3f9b8c to this PR. User setup now recognizes an explicit mount at the configured guest home and creates the account without copying /etc/skel into the mounted directory or recursively changing its ownership.

The test bundle builds and the non-integration tests pass. I added regression cases for a nonempty home directory and for the full machine create/boot path; the VM cases have not been run locally. Could you rebuild the CLI and restart the machine-apiserver from this updated branch, confirm both report fe3f9b8c, and retry with a fresh machine? Please put a small file in $HOME/machine-home first, then check that it stays unchanged and that writes from the guest appear on the host. If creation still fails, please share the setup error and container system version output.

@bubuche79

Copy link
Copy Markdown

Thanks for picking this up. Could the same flag also accept a named volume as the source, the way container run -v myvol:/data does (#2278, in addition to #1805)?

Various use cases, for this: data that must survive machine delete and recreate; a case-sensitive ext4 filesystem instead of a case-insensitive APFS share; native block-device I/O instead of virtiofs for build or database workloads; sharing the same volume with a regular container.

  1. Attach path: containers attach a named volume as a virtio block device and mount it as ext4, not as a virtiofs share. Is the same path available to machines, which boot their own /sbin/init?
  2. Syntax: if both kinds share src:dst, telling a volume name from a relative host path needs a rule. -v already has that rule; it seems worth reusing it rather than inventing a new one.

This also connects to the flag name, which is not consistent with containers. container run --mount takes type=…,source=…,target=…, while this --mount takes the -v syntax. Naming it -v/--volume would make named volumes fit naturally and avoid one flag name taking two incompatible syntaxes.

Hoping for a review on this!

This branch has not been deployed

No deployments
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.

[Request]: User-specified mounts in machines.

7 participants