Repository navigation
Docker multi-arch build with sudo support #6736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
axelfontaine
wants to merge
5
commits into
omacom:quattro
Choose a base branch
from
axelfontaine:docker-multiarch-sudo
base: quattro
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b9de991
Register QEMU binfmt interpreters with OCF so sudo works under emulation
axelfontaine 6a5e346
Test the QEMU binfmt OCF fix and check every registered arch, not jus…
axelfontaine e0a0f5f
Add O and C to the QEMU binfmt flags instead of replacing them
axelfontaine 8b1ec1f
Keep QEMU binfmt overrides in step with qemu updates via a pacman hook
axelfontaine ed63a4f
Merge remote-tracking branch 'upstream/quattro' into docker-multiarch…
axelfontaine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| #!/bin/bash | ||
|
|
||
| # omarchy:summary=Keep QEMU binfmt registrations usable for setuid binaries in cross-arch containers | ||
| # omarchy:group=apply | ||
| # omarchy:requires-sudo=true | ||
| # omarchy:hidden=true | ||
|
|
||
| # qemu-user-static-binfmt registers its interpreters with flags FP. Setuid | ||
| # binaries such as sudo in an emulated-architecture Docker container only keep | ||
| # their credentials under binfmt_misc emulation when the registration carries | ||
| # C (which implies O, handing the interpreter the binary as an open file). F, | ||
| # already set by the package, is what lets the interpreter run inside a | ||
| # container whose rootfs differs from the host's. | ||
| # | ||
| # This writes an /etc/binfmt.d override per package registration with O and C | ||
| # added to the package's own flags, so P (argv[0] preserved) and anything a | ||
| # later package adds survive. The 30-omarchy-binfmt pacman hook reruns it | ||
| # whenever qemu changes its registrations, so overrides follow interpreter, | ||
| # magic and mask changes, cover newly added architectures, and go away with | ||
| # removed ones. Files without the managed marker belong to the administrator | ||
| # and are left alone. | ||
| # | ||
| # C is machine-wide: QEMU runs a root-owned setuid foreign-architecture binary | ||
| # with root's credentials anywhere on the host, not only inside Docker. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| usage() { | ||
| cat <<USAGE | ||
| Usage: omarchy-apply-binfmt [--check] | ||
|
|
||
| Regenerates /etc/binfmt.d/qemu-*-static.conf from the qemu-user-static-binfmt | ||
| registrations with the O and C flags added, removes managed overrides whose | ||
| registration is gone, and restarts systemd-binfmt when the running | ||
| registrations differ. | ||
|
|
||
| --check exit 1 if anything would change, without changing it | ||
| USAGE | ||
| } | ||
|
|
||
| check=0 | ||
| case "${1:-}" in | ||
| "") ;; | ||
| --check) check=1 ;; | ||
| -h|--help) usage; exit 0 ;; | ||
| *) usage >&2; exit 2 ;; | ||
| esac | ||
|
|
||
| source_dir="${OMARCHY_BINFMT_SOURCE_DIR:-/usr/lib/binfmt.d}" | ||
| dest_dir="${OMARCHY_BINFMT_DIR:-/etc/binfmt.d}" | ||
| proc_dir="${OMARCHY_BINFMT_PROC_DIR:-/proc/sys/fs/binfmt_misc}" | ||
| marker="# Managed by omarchy-apply-binfmt from $source_dir; local edits are overwritten" | ||
|
|
||
| managed() { | ||
| local first_line | ||
| [[ -f $1 ]] && IFS= read -r first_line <"$1" && [[ $first_line == "# Managed by omarchy-apply-binfmt "* ]] | ||
| } | ||
|
|
||
| sorted_letters() { | ||
| grep -o . <<<"$1" | sort | tr -d '\n' | ||
| } | ||
|
|
||
| declare -A writes=() | ||
| declare -A live_flags=() | ||
| removes=() | ||
|
|
||
| for conf in "$source_dir"/qemu-*-static.conf; do | ||
| [[ -e $conf ]] || continue | ||
|
|
||
| line=$(grep -m1 -v -e '^[#;]' -e '^$' "$conf" || true) | ||
| [[ -n $line ]] || continue | ||
|
|
||
| flags=${line##*:} | ||
| for flag in O C; do | ||
| [[ $flags == *$flag* ]] || flags+=$flag | ||
| done | ||
|
|
||
| target="$dest_dir/$(basename "$conf")" | ||
| if [[ -e $target ]] && ! managed "$target"; then | ||
| echo "Leaving unmanaged $target alone" >&2 | ||
| continue | ||
| fi | ||
|
|
||
| content=$(printf '%s\n%s:%s' "$marker" "${line%:*}" "$flags") | ||
| if [[ ! -e $target || $(<"$target") != "$content" ]]; then | ||
| writes[$target]=$content | ||
| fi | ||
|
|
||
| name=${line#:} | ||
| live_flags[${name%%:*}]=$flags | ||
| done | ||
|
|
||
| for target in "$dest_dir"/qemu-*-static.conf; do | ||
| if [[ -e $target && ! -e $source_dir/$(basename "$target") ]] && managed "$target"; then | ||
| removes+=("$target") | ||
| fi | ||
| done | ||
|
|
||
| restart=0 | ||
| if (( ${#writes[@]} || ${#removes[@]} )); then | ||
| restart=1 | ||
| elif [[ -e $proc_dir/status ]]; then | ||
| for name in "${!live_flags[@]}"; do | ||
| live=$(sed -n 's/^flags: //p' "$proc_dir/$name" 2>/dev/null || true) | ||
| if [[ $(sorted_letters "$live") != $(sorted_letters "${live_flags[$name]}") ]]; then | ||
| restart=1 | ||
| break | ||
| fi | ||
| done | ||
| fi | ||
|
|
||
| if (( check )); then | ||
| exit "$restart" | ||
| fi | ||
|
|
||
| if (( ${#writes[@]} )); then | ||
| mkdir -p "$dest_dir" | ||
| for target in "${!writes[@]}"; do | ||
| printf '%s\n' "${writes[$target]}" >"$target" | ||
| done | ||
| fi | ||
|
|
||
| for target in "${removes[@]}"; do | ||
| rm -f "$target" | ||
| done | ||
|
|
||
| # binfmt_misc is not mounted in the ISO's target chroot; the registrations load | ||
| # at first boot there. | ||
| if (( restart )) && [[ -e $proc_dir/status ]]; then | ||
| systemctl restart systemd-binfmt.service | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Sorts after 25-systemd-binfmt.hook, which reloads the package registrations | ||
| # that the overrides written here shadow. | ||
| [Trigger] | ||
| Type = Path | ||
| Operation = Install | ||
| Operation = Upgrade | ||
| Operation = Remove | ||
| Target = usr/lib/binfmt.d/qemu-*-static.conf | ||
|
|
||
| [Action] | ||
| Description = Adding O and C to QEMU binfmt registrations... | ||
| When = PostTransaction | ||
| Depends = omarchy | ||
| Exec = /usr/bin/omarchy-apply-binfmt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Add O and C to the QEMU binfmt registrations so setuid binaries keep their | ||
| # credentials in emulated cross-arch containers; see omarchy-apply-binfmt. | ||
| omarchy-apply-binfmt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| echo "Add O and C to QEMU binfmt registrations so sudo works in emulated Docker cross-arch builds" | ||
|
|
||
| omarchy-apply-binfmt --check || sudo omarchy-apply-binfmt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" | ||
|
|
||
| command="$ROOT/bin/omarchy-apply-binfmt" | ||
|
|
||
| test_tmp=$(mktemp -d) | ||
| trap 'rm -rf "$test_tmp"' EXIT | ||
|
|
||
| source_dir="$test_tmp/lib-binfmt.d" | ||
| dest_dir="$test_tmp/etc-binfmt.d" | ||
| proc_dir="$test_tmp/binfmt_misc" | ||
| fake_bin="$test_tmp/bin" | ||
| calls="$test_tmp/calls.log" | ||
| mkdir -p "$source_dir" "$fake_bin" | ||
|
|
||
| cat >"$fake_bin/systemctl" <<'STUB' | ||
| #!/bin/bash | ||
| printf 'systemctl %s\n' "$*" >>"$TEST_LOG" | ||
| STUB | ||
| cat >"$fake_bin/sudo" <<'STUB' | ||
| #!/bin/bash | ||
| printf 'sudo %s\n' "$*" >>"$TEST_LOG" | ||
| exec "$@" | ||
| STUB | ||
| chmod +x "$fake_bin/systemctl" "$fake_bin/sudo" | ||
|
|
||
| write_conf() { | ||
| # Shaped like the qemu-user-static-binfmt package's own registrations: | ||
| # a magic/mask binfmt_misc line ending in the flags field. | ||
| printf ':qemu-%s:M::magic:mask:/usr/bin/qemu-%s-static:%s\n' "$1" "$1" "${3:-FP}" >"$2" | ||
| } | ||
|
|
||
| registration() { | ||
| printf ':qemu-%s:M::magic:mask:/usr/bin/qemu-%s-static:%s' "$1" "$1" "$2" | ||
| } | ||
|
|
||
| set_live() { | ||
| mkdir -p "$proc_dir" | ||
| : >"$proc_dir/status" | ||
| printf 'enabled\ninterpreter /usr/bin/qemu-%s-static\nflags: %s\n' "$1" "$2" >"$proc_dir/qemu-$1" | ||
| } | ||
|
|
||
| apply() { | ||
| TEST_LOG="$calls" \ | ||
| PATH="$ROOT/bin:$fake_bin:$PATH" \ | ||
| OMARCHY_BINFMT_SOURCE_DIR="$source_dir" \ | ||
| OMARCHY_BINFMT_DIR="$dest_dir" \ | ||
| OMARCHY_BINFMT_PROC_DIR="$proc_dir" \ | ||
| "$@" | ||
| } | ||
|
|
||
| reset() { | ||
| rm -rf "$source_dir" "$dest_dir" "$proc_dir" | ||
| mkdir -p "$source_dir" | ||
| : >"$calls" | ||
| } | ||
|
|
||
| # Every qemu static registration gets O and C added to the package's own flags, | ||
| # with the rest of the line untouched, and anything else is ignored. | ||
| reset | ||
| write_conf aarch64 "$source_dir/qemu-aarch64-static.conf" | ||
| write_conf arm "$source_dir/qemu-arm-static.conf" | ||
| printf 'unrelated\n' >"$source_dir/other.conf" | ||
|
|
||
| apply "$command" --check && fail "--check reports pending registrations" | ||
| [[ ! -e $dest_dir ]] || fail "--check changes nothing" | ||
| apply "$command" | ||
|
|
||
| grep -qFx "$(registration aarch64 FPOC)" "$dest_dir/qemu-aarch64-static.conf" || | ||
| fail "adds O and C to aarch64 flags without dropping FP" | ||
| grep -qFx "$(registration arm FPOC)" "$dest_dir/qemu-arm-static.conf" || | ||
| fail "adds O and C to arm flags without dropping FP" | ||
| grep -q '^# Managed by omarchy-apply-binfmt ' "$dest_dir/qemu-aarch64-static.conf" || | ||
| fail "marks overrides as managed" | ||
| [[ ! -e $dest_dir/other.conf ]] || fail "copies files that aren't qemu static registrations" | ||
| pass "adds O and C to every qemu static registration without dropping existing flags" | ||
|
|
||
| # Re-running neither rewrites nor piles up duplicate letters. | ||
| before=$(cat "$dest_dir"/*.conf) | ||
| apply "$command" | ||
| [[ $(cat "$dest_dir"/*.conf) == "$before" ]] || fail "is idempotent" | ||
| apply "$command" --check || fail "--check passes once applied" | ||
| pass "is idempotent and --check passes once applied" | ||
|
|
||
| # A registration that already carries O and C keeps its flags as they are. | ||
| reset | ||
| write_conf riscv64 "$source_dir/qemu-riscv64-static.conf" FPOCX | ||
| apply "$command" | ||
| grep -qFx "$(registration riscv64 FPOCX)" "$dest_dir/qemu-riscv64-static.conf" || | ||
| fail "leaves flags that already include O and C alone" | ||
| pass "leaves flags that already include O and C alone" | ||
|
|
||
| # A package update that changes a registration, adds an architecture or drops | ||
| # one is followed, which a one-time copy would hide behind a stale override. | ||
| reset | ||
| write_conf aarch64 "$source_dir/qemu-aarch64-static.conf" | ||
| write_conf sparc "$source_dir/qemu-sparc-static.conf" | ||
| apply "$command" | ||
| sed -i 's/magic/magic2/' "$source_dir/qemu-aarch64-static.conf" | ||
| write_conf loongarch64 "$source_dir/qemu-loongarch64-static.conf" | ||
| rm "$source_dir/qemu-sparc-static.conf" | ||
| apply "$command" | ||
| grep -qFx ':qemu-aarch64:M::magic2:mask:/usr/bin/qemu-aarch64-static:FPOC' "$dest_dir/qemu-aarch64-static.conf" || | ||
| fail "follows a changed registration" | ||
| grep -qFx "$(registration loongarch64 FPOC)" "$dest_dir/qemu-loongarch64-static.conf" || | ||
| fail "covers an added architecture" | ||
| [[ ! -e $dest_dir/qemu-sparc-static.conf ]] || fail "removes the override of a dropped architecture" | ||
| pass "follows changed, added and dropped registrations" | ||
|
|
||
| # Administrator-authored overrides are never rewritten or removed. | ||
| reset | ||
| mkdir -p "$dest_dir" | ||
| write_conf aarch64 "$source_dir/qemu-aarch64-static.conf" | ||
| printf ':qemu-aarch64:M::magic:mask:/opt/qemu/qemu-aarch64:F\n' >"$dest_dir/qemu-aarch64-static.conf" | ||
| printf ':qemu-m68k:M::magic:mask:/opt/qemu/qemu-m68k:F\n' >"$dest_dir/qemu-m68k-static.conf" | ||
| apply "$command" 2>/dev/null | ||
| grep -qFx ':qemu-aarch64:M::magic:mask:/opt/qemu/qemu-aarch64:F' "$dest_dir/qemu-aarch64-static.conf" || | ||
| fail "leaves an unmanaged override alone" | ||
| [[ -e $dest_dir/qemu-m68k-static.conf ]] || fail "leaves an unmanaged override without a registration alone" | ||
| pass "leaves administrator-authored overrides alone" | ||
|
|
||
| # An empty source directory, as before qemu-user-static-binfmt is installed. | ||
| reset | ||
| apply "$command" || fail "tolerates a source directory with no qemu registrations" | ||
| pass "tolerates a source directory with no qemu registrations" | ||
|
|
||
| # systemd-binfmt is restarted when overrides change, and when they are already | ||
| # on disk but the running registrations still carry the old flags. | ||
| reset | ||
| write_conf aarch64 "$source_dir/qemu-aarch64-static.conf" | ||
| set_live aarch64 PF | ||
| apply "$command" | ||
| grep -qFx 'systemctl restart systemd-binfmt.service' "$calls" || fail "restarts systemd-binfmt after writing overrides" | ||
|
|
||
| : >"$calls" | ||
| apply "$command" | ||
| grep -qFx 'systemctl restart systemd-binfmt.service' "$calls" || | ||
| fail "restarts systemd-binfmt while running registrations lack O and C" | ||
|
|
||
| : >"$calls" | ||
| set_live aarch64 POCF | ||
| apply "$command" | ||
| [[ ! -s $calls ]] || fail "leaves systemd-binfmt alone once running registrations match" | ||
| pass "restarts systemd-binfmt only while running registrations differ" | ||
|
|
||
| # Without binfmt_misc mounted, as in the ISO's target chroot, overrides are | ||
| # still written and load at boot. | ||
| reset | ||
| write_conf aarch64 "$source_dir/qemu-aarch64-static.conf" | ||
| apply "$command" | ||
| [[ -e $dest_dir/qemu-aarch64-static.conf ]] || fail "writes overrides without binfmt_misc mounted" | ||
| [[ ! -s $calls ]] || fail "skips the restart without binfmt_misc mounted" | ||
| pass "writes overrides without restarting when binfmt_misc is not mounted" | ||
|
|
||
| # The migration escalates only when something is pending, so a second user on | ||
| # an already fixed machine is not prompted. | ||
| migration=$(grep -rl 'omarchy-apply-binfmt' "$ROOT/migrations" | head -n 1 || true) | ||
| [[ -n $migration ]] || fail "binfmt migration exists" | ||
|
|
||
| reset | ||
| write_conf aarch64 "$source_dir/qemu-aarch64-static.conf" | ||
| apply bash -euo pipefail "$migration" >/dev/null | ||
| grep -qFx "$(registration aarch64 FPOC)" "$dest_dir/qemu-aarch64-static.conf" || | ||
| fail "binfmt migration applies pending overrides" | ||
| grep -q '^sudo omarchy-apply-binfmt$' "$calls" || fail "binfmt migration escalates to apply" | ||
|
|
||
| : >"$calls" | ||
| apply bash -euo pipefail "$migration" >/dev/null | ||
| [[ ! -s $calls ]] || fail "binfmt migration does not escalate once applied" | ||
| pass "binfmt migration escalates only when overrides are pending" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.