Skip to content

Fail fast when a container machine image lacks /sbin/init - #2025

Open
Jobikinobi wants to merge 1 commit into
apple:mainfrom
Jobikinobi:machine-validate-init
Open

Fail fast when a container machine image lacks /sbin/init#2025
Jobikinobi wants to merge 1 commit into
apple:mainfrom
Jobikinobi:machine-validate-init

Conversation

@Jobikinobi

Copy link
Copy Markdown

Fixes #2024.

Problem

container machine create succeeds for images that contain no /sbin/init (e.g. ubuntu:latest and most application images), but the resulting machine can never boot: the guest init script's exec /sbin/init dies immediately, the user-visible error is the misleading invalidState: "cannot exec: container is not running" from the follow-up user-setup exec, and the permanently unbootable machine record remains in container machine ls. Several users have hit this and needed community help to diagnose it (#1669, #1685, #1689).

Change

  • MachinesService.create now validates the cloned root filesystem right after setMachineRootFs(cloning:), using EXT4Reader.exists("/sbin/init", followSymlinks: true) from ContainerizationEXT4 (already a dependency of the server target). On failure it throws an actionable invalidArgument error naming the image and pointing at the machine image requirements. The check runs inside the existing do/catch, so the partial bundle is deleted and no machine record is left behind.
  • Following symlinks means busybox-based images (/sbin/init -> /bin/busybox, e.g. plain alpine) still validate and boot as before — verified empirically. A dangling /sbin/init symlink is rejected.

New error:

$ container machine create ubuntu:latest --name dev
Error: ... image docker.io/library/ubuntu:latest cannot be used as a container machine: it does not contain /sbin/init. Container machine images must include an init system such as systemd. See "Bring your own container machine image" in the container machine guide for a working Dockerfile.

Tests

  • New MachineAPIServiceTests unit-test target: validateMachineRootfs is exercised against small ext4 fixtures built with EXT4.Formatter — regular-file /sbin/init, symlink to an existing file, missing entirely, and dangling symlink. All pass locally (test-first: watched them fail before implementing).
  • New integration test testCreateRejectsImageWithoutInit: machine create with docker.io/library/hello-world:latest (smallest init-less image) must fail, mention /sbin/init, and leave no machine behind. Written to match the surrounding suite patterns; ran the unit suite locally, relying on CI for the integration suite.
  • Existing machine integration tests use ghcr.io/linuxcontainers/alpine:3.20, which ships /sbin/init and passes the new validation (verified against the real image).

🤖 Generated with Claude Code

https://claude.ai/code/session_01AdcR7a6p56wrAihqNnxeWN

The machine boot process hands off to the image's init system via
`exec /sbin/init`. Creating a machine from an image without one (e.g.
`ubuntu:latest` or most application images) succeeded, but the machine
could never boot: the boot failure surfaced later as the misleading
"cannot exec: container is not running", and the unbootable machine
record was left behind (apple#1669, apple#1685, apple#1689).

- Validate the cloned root filesystem in `MachinesService.create` with
  `EXT4Reader.exists("/sbin/init", followSymlinks: true)` and throw an
  actionable error naming the image and the requirement. The existing
  cleanup path deletes the partial bundle, so no machine record remains.
- Add unit tests covering regular-file, symlink, missing, and dangling
  symlink cases in a new MachineAPIServiceTests target.
- Add an integration test that `machine create` rejects an init-less
  image and leaves no machine behind.

Fixes apple#2024.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AdcR7a6p56wrAihqNnxeWN
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.

[Bug]: container machine create succeeds for images without /sbin/init, then boot fails with a misleading error

1 participant