Skip to content

cmd/initContainer: Set up QEMU emulation for cross-arch containers#1788

Open
DaliborKr wants to merge 13 commits intocontainers:mainfrom
DaliborKr:pr08-initcontainer
Open

cmd/initContainer: Set up QEMU emulation for cross-arch containers#1788
DaliborKr wants to merge 13 commits intocontainers:mainfrom
DaliborKr:pr08-initcontainer

Conversation

@DaliborKr
Copy link
Copy Markdown
Collaborator

This is PR 8/10 in a series adding cross-architecture container support using QEMU and binfmt_misc.

Depends on: #1787 (cmd/create: Integrate cross-architecture support into container creation)
Please review #1787 first. The new commit in this PR is:

  • cmd/initContainer: Set up QEMU emulation for cross-arch containers

Summary

When a cross-architecture container is created, the init-container
entry point must re-register QEMU emulation inside a sandboxed
binfmt_misc mount to enable the C flag, which cannot be safely set
on the host's registration (as explained in [1]).

  • Add --arch and --arch-emulator-path flags to the init-container command, passed from container creation phase.

  • When the architecture differs from the host, set up emulation in four steps:

    1. validate that the host's QEMU emulation is working by running true and detecting ENOEXEC via RunWithExitCode2() (added in [2]), because host emulation must be functional to perform the sandboxed re-registration in the following step

    2. mount a fresh binfmt_misc filesystem inside the container via MountBinfmtMisc() (added in [1])

    3. validate architecture support via IsArchSupportedOnInitialization() (added in [3])

    4. register the QEMU interpreter with the C flag via RegisterBinfmtMisc() (added in [1])

[1] #1782
[2] #1780
[3] #1783

The existing RunContextWithExitCode() wraps all errors from
exec.Command in generic "failed to invoke" messages, which prevents
callers from distinguishing between actual error types.

Add RunContextWithExitCode2() and RunWithExitCode2() that return
errors with their original types intact, including for ExitError.
This allows callers to use errors.Is() and errors.As() to handle
specific failure modes. For example, detecting a missing skopeo binary
(exec.ErrNotFound) or an ENOEXEC error from inside non native
containers, when an emulator is not set correctly.

These new functions are meant to replace its original versions in the
future.

containers#1780

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
In /src/cmd/create.go, the same pattern of spinner creation and
nil-safe stopping is repeated.

Extract this into startSpinner() and stopSpinner() helper functions so
that future callers can use spinners without duplicating the code.
Replace the existing inline spinner code in createContainer() and
pullImage() with calls to these new helpers.

containers#1781

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
…atching

Add IsSupportedDistroImage(), which iterates over all supported distros
and checks if the image basename matches any of them. This will be used
by the architecture resolution code to decide whether to parse
architecture suffixes from image tags, as this should be done only for
natively supported images [1].

[1] Toolbx supported distributions: https://containertoolbx.org/distros/

containers#1781

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
@DaliborKr DaliborKr requested a review from debarshiray as a code owner April 24, 2026 10:53
DaliborKr added a commit to DaliborKr/toolbox that referenced this pull request Apr 24, 2026
Add the --arch and --arch-emulator-path flags to the init-container
command, passed from the create command when creating a
cross-architecture container. The --arch flag defaults to the host
architecture ID so that existing native containers continue to work
without changes.

When the container's architecture differs from the host, the
init-container entry point configures QEMU emulation inside the
container before any foreign-architecture binaries can run:

1. Validate QEMU emulation by running the 'true' command, which fails
with ENOEXEC if the host's binfmt_misc registration is not working
(detected via RunWithExitCode2() added in [1]), because it is necessary
to have host emulation working to emulate the binfmt_misc registration
in the following step.

2. Mount a fresh binfmt_misc filesystem inside the container via
MountBinfmtMisc() (added in [2]) to create a sandboxed binfmt_misc
registration with the C flag.

3. Validate architecture support via IsArchSupportedOnInitialization()
(added in [3]), which verifies the QEMU interpreter at the host-mounted
path under /run/host.

4. Register the QEMU interpreter with the C flag via
RegisterBinfmtMisc() (added and explained in [2])

The binfmt_misc registration is performed inside the container rather
than relying on the host's registration, as explained in [2].

Update showEntryPointLog() in run.go to propagate lines prefixed with
'Warning:' to stderr on the host, instead of treating them as errors.
This is needed because the cross-architecture initialization may emit
warnings that should be visible to the user but are not fatal.

[1] containers#1780
[2] containers#1782
[3] containers#1783

containers#1788

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
@DaliborKr DaliborKr force-pushed the pr08-initcontainer branch from 1ad526f to b6c4803 Compare April 24, 2026 10:55
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for creating and running Toolbx containers for non-native architectures using QEMU user-mode emulation and binfmt_misc. Key changes include the addition of an --arch flag to the create command, logic to pull architecture-specific images using skopeo, and the automatic registration of emulators during container initialization. A new architecture package centralizes architecture definitions and binfmt_misc management. Review feedback identifies minor typos in an error message and a debug log.

Comment thread src/cmd/create.go
Comment on lines +796 to +797
return false, false, fmt.Errorf("failed to verify: image %s does not support architecture %s or the image does not exists at all",
imageFull, expectedArchName)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There's a typo in the error message. 'exists' should be 'exist'.

Suggested change
return false, false, fmt.Errorf("failed to verify: image %s does not support architecture %s or the image does not exists at all",
imageFull, expectedArchName)
return false, false, fmt.Errorf("failed to verify: image %s does not support architecture %s or the image does not exist at all",
imageFull, expectedArchName)

Comment thread src/cmd/initContainer.go
return errNotSupported
}

logrus.Debugf("Registering QEMU emulator for architecture %s in binfmt_mist", archName)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There's a typo in this log message. 'binfmt_mist' should be 'binfmt_misc'.

Suggested change
logrus.Debugf("Registering QEMU emulator for architecture %s in binfmt_mist", archName)
logrus.Debugf("Registering QEMU emulator for architecture %s in binfmt_misc", archName)

@DaliborKr
Copy link
Copy Markdown
Collaborator Author

I didn't realize that the architecture package variable HostArchID, initialized in root.go's setUpGlobals(), doesn't get assigned a value before it is used as a default value for the new flag --arch in initContainer.go.

This causes a bug for containers created with Toolbx before cross-architecture support was implemented, because the default value is always 0 (NotSpecified), which is evaluated as a non-native container when compared with the already initialized HostArchID later in the initContainer() function.

I moved the initialization of HostArchID directly to the init() function in architecture.go, which is always evaluated before its dependents (see commit e18be06 in PR #1782).

DaliborKr added a commit to DaliborKr/toolbox that referenced this pull request Apr 24, 2026
Add the --arch and --arch-emulator-path flags to the init-container
command, passed from the create command when creating a
cross-architecture container. The --arch flag defaults to the host
architecture ID so that existing native containers continue to work
without changes.

When the container's architecture differs from the host, the
init-container entry point configures QEMU emulation inside the
container before any foreign-architecture binaries can run:

1. Validate QEMU emulation by running the 'true' command, which fails
with ENOEXEC if the host's binfmt_misc registration is not working
(detected via RunWithExitCode2() added in [1]), because it is necessary
to have host emulation working to emulate the binfmt_misc registration
in the following step.

2. Mount a fresh binfmt_misc filesystem inside the container via
MountBinfmtMisc() (added in [2]) to create a sandboxed binfmt_misc
registration with the C flag.

3. Validate architecture support via IsArchSupportedOnInitialization()
(added in [3]), which verifies the QEMU interpreter at the host-mounted
path under /run/host.

4. Register the QEMU interpreter with the C flag via
RegisterBinfmtMisc() (added and explained in [2])

The binfmt_misc registration is performed inside the container rather
than relying on the host's registration, as explained in [2].

Update showEntryPointLog() in run.go to propagate lines prefixed with
'Warning:' to stderr on the host, instead of treating them as errors.
This is needed because the cross-architecture initialization may emit
warnings that should be visible to the user but are not fatal.

[1] containers#1780
[2] containers#1782
[3] containers#1783

containers#1788

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
@DaliborKr DaliborKr force-pushed the pr08-initcontainer branch from b6c4803 to 111802a Compare April 24, 2026 19:52
@softwarefactory-project-zuul
Copy link
Copy Markdown

Introduce the architecture package that represents the core of the
Toolbx cross-architecture support, which is based on user-mode emulation
using QEMU and binfmt_misc.

The Architecture struct collects all per-architecture data (ELF
magic/mask, OCI and binfmt naming, aliases, binfmt registration
parameters) into a single map. Architectures present in the
supportedArchitectures map represent the set of supported
architectures within Toolbx.

Define architecture ID constants NotSpecified, Aarch64, Ppc64le, and
X86_64, along with their supportedArchitectures entries.

Add core query functions:
- ParseArgArchValue() for resolving user-supplied architecture strings

- GetArchNameBinfmt() and GetArchNameOCI() for name
  lookups (one architecture can have multiple valid names [1])

- HasContainerNativeArch() for comparing against the host

- ImageReferenceGetArchFromTag() for extracting architecture
  from image tag suffixes like "42-aarch64" for architecture detection

Expose the HostArchID package variable, which is set in the init()
function, so the variable can be accessed in the early init() state
from every inheritor that utilizes the architecture package (HostArchID
serves as a default value for initContainer --arch flag), and the Config
struct for preserving the architecture ID and the QEMU emulator path,
through the call chain.

[1] https://itsfoss.com/arm-aarch64-x86_64/

containers#1782

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Cross-architecture containers need QEMU binfmt_misc handlers registered
within the container so that non-native architecture binaries can be
executed via the host's kernel.

Add the Registration struct that models a binfmt_misc registration
entry, including name, magic type, offset, ELF magic/mask bytes,
interpreter path, and flags.

Add functions:
- MountBinfmtMisc() to mount the sanboxed binfmt_misc filesystem inside
  a container, which enables setting the C flag in binfmt_misc
  registration without affecting the host system. The C flag presents a
  threat of privilege escalation when registered on the host, that why
  we want to have it isolated [1]

- getDefaultRegistration() to fill a Registration struct containing all
  necessary binfmt_misc information taken from the
  architecture.supportedArchitectures data

- RegisterBinfmtMisc() to write the registration string to
  /proc/sys/fs/binfmt_misc/register, which makes the non-native binary
  perception active

- bytesToEscapedString() helper that formats byte slices into the
  \xHH-escaped string format required by the binfmt_misc register
  interface

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=21ca59b365c0

containers#1782

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Before creating or initializing a cross-architecture container, the
system must be checked for the required QEMU emulator and binfmt_misc
registration. This prevents users from creating or running non-native
containers when their host system doesn't meet the requirements, and
provides users with an informative error message referring to the
problem.

Add IsArchSupportedOnCreation(), which searches for a statically linked
QEMU binary on the host using exec.LookPath() and verifies that a
matching binfmt_misc registration exists. It returns the path to the
QEMU binary for use during container creation, which is meant to be
passed to the init-container and registered through sandboxed
binfmt_misc within the container.

Add IsArchSupportedOnInitialization() which performs similar checks
from inside the container, looking at the interpreter path passed from
the host and falling back to standard host-mounted locations under
/run/host/usr/bin/.

Add isStaticallyLinkedELF() helper that uses debug/elf to verify a
binary is statically linked. Only a statically linked QEMU interpreter
can be used, because a dynamically linked one would cause the kernel
to attempt to resolve its host-native shared libraries (such as libc.so)
within the container, resulting in an immediate crash.

Add validateBinfmtRegistration(), which checks for the presence of
qemu-<arch> entries in binfmt_misc (or qemu-<arch>-static, since it can
differ based on the system).

containers#1783

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Add Architecture and NameFull fields to the Skopeo Image struct so that
callers can inspect the architecture of a remote image. Move the image
size computation from the /cmd layer into GetSize() and GetSizeHuman()
methods on Image, since the skopeo package owns the layer data.

Add VerifyArchitectureMatch() method to Image that validates the image's
architecture field against an expected architecture ID. The purpose of
this function is to check whether the image architecture matches the
demanded architecture before it is pulled. Specifically, this
verification applies to the images that support only a single
architecture (they are not part of a multi-platform manifest list),
because the skopeo inspect proceeds successfully even when the value
of a flag --override-arch does not match the actual image architecture
(for a multi-architecture image the skopeo inspect with not-matching
--override-arch would fail). Like this, the user can be prevented from
incompatible images.

containers#1784

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Change Inspect() to accept archID and authfile parameters. When the
requested architecture differs from the host's, --override-arch is
passed to skopeo, which then inspects the correct manifest in a
multi-arch image (if it exists for the given architecture, otherwise
the inspection fails). It also uses RunContextWithExitCode2() so callers
can detect a missing skopeo binary via errors.Is(err, exec.ErrNotFound),
which is only a soft dependency of the Toolbx package, as it is not
required for running native containers.

Add CopyOverrideArch(), which uses 'skopeo copy --override-arch' to pull
a specific architecture variant of a multi-arch image into Podman's
local container storage. This is used instead of 'podman pull' because
Podman does not support pulling a foreign architecture image into a
locally addressable name. The way in which the cross-arch extension
chooses the name for non-native images (and also containers) is
described in the discussion at [1]

[1] containers/podman#27780 (comment)

containers#1784

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
…and names

Add resolveArchitectureID(), which combines the --arch command-line
flag with architecture detection from image tag suffixes
(e.g., "fedora-toolbox:42-aarch64"). This detection applies only
to images from distributions that Toolbx explicitly supports (see [1]),
to avoid a false architecture approach on custom images where a
dash-separated component might not represent an architecture, since
there is no standard set regarding preserving architecture in the tag
(see detailed explanation at [2]). When both sources specify an
architecture, it validates that they do not conflict.

Add resolveImageNameWithArchitectureSuffix(), which appends the OCI
architecture name to supported distro image references when the target
architecture differs from the host, to ensure the local Toolbx images
naming convention [2]. Again, this applies only to supported distros.

[1] https://containertoolbx.org/distros/
[2] containers/podman#27780 (comment)

containers#1786

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Change resolveContainerAndImageNames() to accept an archID parameter.
When the target architecture is non-native, and the container name was
auto-generated (was not set by a user), append the architecture suffix
to the container name (e.g., "fedora-toolbox-arm64") to distinguish it
from native containers.

Temporarily update the callers of resolveContainerAndImageNames() to
pass in architecture.HostArchID to the updated signature, to maintain
a default native behavior. Once implemented, the --arch argument in
the callers will pass the actual architecture information.

containers#1786

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Add the --arch flag to the 'create' command, allowing users to create
Toolbx containers for architectures different from the host
(e.g., 'toolbox create --arch arm64').

Utilize the architecture resolution pipeline in create() by using
resolveArchitectureID() (added in [1]) to determine the target
architecture from the --arch flag and image tags.

Validate host support via IsArchSupportedOnCreation() (added in [2]),
which checks for the required QEMU emulator and binfmt_misc
registration.

Pass architecture ID to resolveContainerAndImageNames() (updated in [1])
so that non-native containers get architecture-suffixed names.

Update pullImage() to handle cross-architecture image pulling: when the
target architecture is non-native, use skopeo.CopyOverrideArch()
(added in [3]) instead of podman.Pull(), since Podman does not support
pulling foreign architecture images into locally addressable names.
The need for this is explained in a discussion in [4].

Add a 'toolbox-arch' label to created containers to record the target
architecture in OCI format.

Extract the image pull error formatting into createErrorImagePull() in
utils.go to avoid duplication between the native and cross-arch pull
paths.

Update the createContainer() call in run.go to pass the default
architecture config via GetArchConfigDefault(), maintaining the
existing native-architecture behavior.

[1] containers#1786
[2] containers#1783
[3] containers#1784
[4] containers/podman#27780

containers#1787

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Rework the image download prompt flow to support architecture
verification before pulling non-native images. The new implementation
ensures that the image inspection completes for the non-native creation
path before it is pulled, so the image's architecture can be verified.

The previous implementation used promptForDownloadError as a control
flow mechanism between the first and second download prompts. Replace
this with the pullImageDecision enum (pullNo, pullYes, pullUnknown)
for clearer three-state signaling.

Replaced getImageSizeFromRegistryAsync() with
getImageFromRegistryAsync(), which now returns the full skopeo.Image
struct instead of just the image size string. It calls skopeo.Inspect()
(updated in [1]), making image metadata available throughout the
download prompt flow for both size display and architecture verification
in a single inspect call.

Use Image.GetSizeHuman() (added in [1]) for image size display in the
second download prompt, replacing the local size computation.

Update showPromptForDownloadFirst() to return
(pullImageDecision, *skopeo.Image, error). For non-native architectures,
when the user confirms the download, the function now waits for the
skopeo inspect to complete (with a spinner) before returning, ensuring
that architecture verification can happen before the pull begins.

Update pullImage() to verify the image architecture before pulling
non-native images by calling VerifyArchitectureMatch() (added in [1])
to catch incompatible single-architecture images. Handle the case where
the inspect returns nil (multi-arch manifest has no matching variant)
with an explicit error.

Detect a missing skopeo binary via exec.ErrNotFound, which is only a
soft dependency of the Toolbx package, as it is not required for
running non-native containers, and report it through
createErrorSkopeoNotFound() added in utils.go.

[1] containers#1784

containers#1787

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
Add the --arch and --arch-emulator-path flags to the init-container
command, passed from the create command when creating a
cross-architecture container. The --arch flag defaults to the host
architecture ID so that existing native containers continue to work
without changes.

When the container's architecture differs from the host, the
init-container entry point configures QEMU emulation inside the
container before any foreign-architecture binaries can run:

1. Validate QEMU emulation by running the 'true' command, which fails
with ENOEXEC if the host's binfmt_misc registration is not working
(detected via RunWithExitCode2() added in [1]), because it is necessary
to have host emulation working to emulate the binfmt_misc registration
in the following step.

2. Mount a fresh binfmt_misc filesystem inside the container via
MountBinfmtMisc() (added in [2]) to create a sandboxed binfmt_misc
registration with the C flag.

3. Validate architecture support via IsArchSupportedOnInitialization()
(added in [3]), which verifies the QEMU interpreter at the host-mounted
path under /run/host.

4. Register the QEMU interpreter with the C flag via
RegisterBinfmtMisc() (added and explained in [2])

The binfmt_misc registration is performed inside the container rather
than relying on the host's registration, as explained in [2].

Update showEntryPointLog() in run.go to propagate lines prefixed with
'Warning:' to stderr on the host, instead of treating them as errors.
This is needed because the cross-architecture initialization may emit
warnings that should be visible to the user but are not fatal.

[1] containers#1780
[2] containers#1782
[3] containers#1783

containers#1788

Signed-off-by: Dalibor Kricka <dalidalk@seznam.cz>
@softwarefactory-project-zuul
Copy link
Copy Markdown

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.

1 participant