From 268f5d61fcae2d252e7eaa1ba8e20fceed61a71f Mon Sep 17 00:00:00 2001 From: Caspar van Leeuwen Date: Tue, 1 Dec 2026 15:26:38 +0100 Subject: [PATCH] Add support for QEMU-emulated builds in the EESSI bot Allow e.g. 'bot:build on:arch=icelake for:arch=aarch64/neoverse_n1' to build aarch64/riscv64 software on an x86_64 node using qemu-user-static + binfmt_misc. - scripts/qemu_emulation.sh: eessi_setup_emulation checks the binfmt entry (incl. F flag), maps the target subdir to a QEMU CPU model, and exports EESSI_EMULATED, EESSI_TARGET_CPU_FAMILY, EESSI_CONTAINER_ARCH and EESSI_QEMU_CPU - bot/build.sh, bot/test.sh: call it, use a container cache per target CPU family, and use the target (not host) CPU family for EESSI_COMPAT_LAYER_DIR_OVERRIDE - eessi_container.sh: pull container for the target arch and pass QEMU_CPU into the container - EESSI-install-software.sh: verify archspec detects the target CPU on the emulated CPU Co-Authored-By: Claude Sonnet 5.5 --- EESSI-install-software.sh | 13 ++++ bot/build.sh | 15 ++++- bot/test.sh | 10 +++ eessi_container.sh | 21 ++++++- scripts/qemu_emulation.sh | 129 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 scripts/qemu_emulation.sh diff --git a/EESSI-install-software.sh b/EESSI-install-software.sh index 7801a70c3..6f52f262f 100755 --- a/EESSI-install-software.sh +++ b/EESSI-install-software.sh @@ -295,6 +295,19 @@ else echo_green ">> Using ${EESSI_SOFTWARE_SUBDIR} as software subdirectory!" fi +# For builds under QEMU emulation ($QEMU_CPU is set by eessi_container.sh, see scripts/qemu_emulation.sh), +# the software subdirectory is taken from EESSI_SOFTWARE_SUBDIR_OVERRIDE, so the check above passes regardless +# of which CPU QEMU emulates. Compilers (-mcpu=native) however use the emulated CPU, so verify that archspec +# detects the target CPU on the emulated CPU as well +if [[ -n "${QEMU_CPU}" && ${GENERIC} -eq 0 ]]; then + detected_subdir=$(env -u EESSI_SOFTWARE_SUBDIR_OVERRIDE python3 $TOPDIR/eessi_software_subdir.py) + if [[ "${detected_subdir}" != "${EESSI_SOFTWARE_SUBDIR_OVERRIDE}" ]]; then + fatal_error "Emulated CPU (QEMU_CPU=${QEMU_CPU}) is detected as '${detected_subdir}', not as '${EESSI_SOFTWARE_SUBDIR_OVERRIDE}'!" + else + echo_green ">> Emulated CPU (QEMU_CPU=${QEMU_CPU}) is detected as ${detected_subdir}, OK!" + fi +fi + # avoid that pyc files for EasyBuild are stored in EasyBuild installation directory export PYTHONPYCACHEPREFIX=$TMPDIR/pycache # force Python's stdout and stderr streams to be unbuffered diff --git a/bot/build.sh b/bot/build.sh index 2a7500bd8..6c851aa3a 100755 --- a/bot/build.sh +++ b/bot/build.sh @@ -48,6 +48,7 @@ software_layer_dir=$(dirname $(dirname $(realpath $0))) # source utils.sh and cfg_files.sh source $software_layer_dir/scripts/utils.sh source $software_layer_dir/scripts/cfg_files.sh +source $software_layer_dir/scripts/qemu_emulation.sh # defaults export JOB_CFG_FILE="${JOB_CFG_FILE_OVERRIDE:=cfg/job.cfg}" @@ -212,6 +213,17 @@ EESSI_SOFTWARE_SUBDIR_OVERRIDE=${EESSI_SOFTWARE_SUBDIR_OVERRIDE:-${CPU_TARGET}} export EESSI_SOFTWARE_SUBDIR_OVERRIDE echo "bot/build.sh: EESSI_SOFTWARE_SUBDIR_OVERRIDE='${EESSI_SOFTWARE_SUBDIR_OVERRIDE}'" +# if we build for a different CPU family than the one of the build host (e.g. 'on:arch=icelake for:arch=aarch64/neoverse_n1'), +# use QEMU user-mode emulation; sets EESSI_EMULATED, EESSI_TARGET_CPU_FAMILY, EESSI_CONTAINER_ARCH and EESSI_QEMU_CPU +eessi_setup_emulation "${EESSI_SOFTWARE_SUBDIR_OVERRIDE}" "${HOST_ARCH}" +# keep cached container images separate per CPU family of the *container* +if [[ -n "${SINGULARITY_CACHEDIR}" && -n "${EESSI_EMULATED}" ]]; then + SINGULARITY_CACHEDIR="${SINGULARITY_CACHEDIR%/${HOST_ARCH}}/${EESSI_TARGET_CPU_FAMILY}" + export SINGULARITY_CACHEDIR + mkdir -p ${SINGULARITY_CACHEDIR} + echo "bot/build.sh: SINGULARITY_CACHEDIR='${SINGULARITY_CACHEDIR}' (emulated build)" +fi + # Set timezone (TZ) to avoid issues like https://github.com/EESSI/software-layer/pull/1930#issuecomment-7101975801 # Note that both GLIBC and GLib respect TZ export EESSI_PROPAGATE_INTO_PREFIX_TZ=${EESSI_PROPAGATE_INTO_PREFIX_TZ:-UTC} @@ -259,7 +271,8 @@ if [[ -n "${EESSI_CVMFS_COMPAT_REPO}" && -n "${EESSI_VERSION_OVERRIDE:-$EESSI_VE EESSI_COMPAT_VERSION=${EESSI_VERSION_OVERRIDE:-$EESSI_VERSION} # Cut off any version suffix (2025.06-001 -> 2025.06) EESSI_COMPAT_VERSION=${EESSI_COMPAT_VERSION%-*} - export EESSI_COMPAT_LAYER_DIR_OVERRIDE="${EESSI_CVMFS_COMPAT_REPO}/versions/${EESSI_COMPAT_VERSION}/compat/linux/$(uname -m)" + # use the CPU family of the target (not of the build host), they differ for emulated builds + export EESSI_COMPAT_LAYER_DIR_OVERRIDE="${EESSI_CVMFS_COMPAT_REPO}/versions/${EESSI_COMPAT_VERSION}/compat/linux/${EESSI_TARGET_CPU_FAMILY}" msg="bot:build.sh: Set EESSI_COMPAT_LAYER_DIR_OVERRIDE to $EESSI_COMPAT_LAYER_DIR_OVERRIDE since both EESSI_CVMFS_COMPAT_REPO" msg="$msg (${EESSI_CVMFS_COMPAT_REPO}) and EESSI_VERSION_OVERRIDE (${EESSI_VERSION_OVERRIDE}) are defined" echo "$msg" diff --git a/bot/test.sh b/bot/test.sh index 947919ceb..0d98f3fbb 100755 --- a/bot/test.sh +++ b/bot/test.sh @@ -35,6 +35,7 @@ set -e # source utils.sh and cfg_files.sh source scripts/utils.sh source scripts/cfg_files.sh +source scripts/qemu_emulation.sh # defaults export JOB_CFG_FILE="${JOB_CFG_FILE_OVERRIDE:=./cfg/job.cfg}" @@ -174,6 +175,15 @@ EESSI_SOFTWARE_SUBDIR_OVERRIDE=${EESSI_SOFTWARE_SUBDIR_OVERRIDE:-${CPU_TARGET}} export EESSI_SOFTWARE_SUBDIR_OVERRIDE echo "bot/test.sh: EESSI_SOFTWARE_SUBDIR_OVERRIDE='${EESSI_SOFTWARE_SUBDIR_OVERRIDE}'" +# use QEMU emulation if the target is for a different CPU family than the test host (see bot/build.sh) +eessi_setup_emulation "${EESSI_SOFTWARE_SUBDIR_OVERRIDE}" "${HOST_ARCH}" +if [[ -n "${SINGULARITY_CACHEDIR}" && -n "${EESSI_EMULATED}" ]]; then + SINGULARITY_CACHEDIR="${SINGULARITY_CACHEDIR%/${HOST_ARCH}}/${EESSI_TARGET_CPU_FAMILY}" + export SINGULARITY_CACHEDIR + mkdir -p ${SINGULARITY_CACHEDIR} + echo "bot/test.sh: SINGULARITY_CACHEDIR='${SINGULARITY_CACHEDIR}' (emulated test)" +fi + # determine accelerator target (if any) from .architecture in ${JOB_CFG_FILE} ACCEL_OVERRIDE=$(cfg_get_value "architecture" "accelerator") if [[ -n "$ACCEL_OVERRIDE" ]]; then diff --git a/eessi_container.sh b/eessi_container.sh index 5dbbbc24a..08047aa07 100755 --- a/eessi_container.sh +++ b/eessi_container.sh @@ -602,11 +602,19 @@ CONTAINER_URL_FMT=".*://(.*)" if [[ ${CONTAINER} =~ ${CONTAINER_URL_FMT} ]]; then # replace ':', '-', '/' with '_' in match (everything after ://) and append .sif CONTAINER_IMG="$(echo ${BASH_REMATCH[1]} | sed 's/[:\/-]/_/g').sif" + # $EESSI_CONTAINER_ARCH (e.g. arm64, set by bot/build.sh for emulated builds) selects the + # architecture to pull; include it in the image name so an image for another architecture + # (e.g. from a resumed session) is never reused + PULL_ARCH_ARGS=() + if [[ -n "${EESSI_CONTAINER_ARCH}" ]]; then + CONTAINER_IMG="${CONTAINER_IMG%.sif}_${EESSI_CONTAINER_ARCH}.sif" + PULL_ARCH_ARGS=("--arch" "${EESSI_CONTAINER_ARCH}") + fi # pull container to ${EESSI_TMPDIR} if it is not there yet (i.e. when # resuming from a previous session) if [[ ! -x ${EESSI_TMPDIR}/${CONTAINER_IMG} ]]; then - echo "Pulling container image from ${CONTAINER} to ${EESSI_TMPDIR}/${CONTAINER_IMG}" - singularity ${RUN_QUIET} pull ${EESSI_TMPDIR}/${CONTAINER_IMG} ${CONTAINER} + echo "Pulling container image from ${CONTAINER} to ${EESSI_TMPDIR}/${CONTAINER_IMG} ${PULL_ARCH_ARGS[*]}" + singularity ${RUN_QUIET} pull "${PULL_ARCH_ARGS[@]}" ${EESSI_TMPDIR}/${CONTAINER_IMG} ${CONTAINER} else echo "Reusing existing container image ${EESSI_TMPDIR}/${CONTAINER_IMG}" fi @@ -1040,6 +1048,15 @@ if [ ! -z ${EESSI_SOFTWARE_SUBDIR_OVERRIDE} ]; then export APPTAINERENV_EESSI_SOFTWARE_SUBDIR_OVERRIDE=${EESSI_SOFTWARE_SUBDIR_OVERRIDE} fi +# pass the CPU model to emulate into the container (emulated builds, see scripts/qemu_emulation.sh). +# QEMU user-mode emulation (started by the kernel via binfmt_misc for every binary in the container) +# reads $QEMU_CPU; it is inherited by all child processes +if [[ -n "${EESSI_QEMU_CPU}" ]]; then + export SINGULARITYENV_QEMU_CPU="${EESSI_QEMU_CPU}" + export APPTAINERENV_QEMU_CPU="${EESSI_QEMU_CPU}" + [[ ${VERBOSE} -eq 1 ]] && echo "QEMU_CPU=${EESSI_QEMU_CPU} (passed into container)" +fi + # For Apptainer 1.5+, Apptainer includes system library locations in LD_LIBRARY_PATH # if host libraries are bind mounted to /.singularity/libs (as is done for GPU support). # This breaks our startprefix script in 2023.06/2025.06 in general, so we override this diff --git a/scripts/qemu_emulation.sh b/scripts/qemu_emulation.sh new file mode 100644 index 000000000..18e776c49 --- /dev/null +++ b/scripts/qemu_emulation.sh @@ -0,0 +1,129 @@ +# Helper functions to build for a CPU family other than the one of the build host, +# using QEMU user-mode emulation (qemu-user-static + binfmt_misc). +# +# Intended to be sourced by bot/build.sh and bot/test.sh (after scripts/utils.sh). +# +# How it works: +# - the build host must have binfmt_misc entries for the target CPU family, registered +# with the 'F' (fix-binary) flag and a statically linked interpreter (e.g. via the +# qemu-user-static package). Then the kernel runs every binary of the target family, +# also those inside the build container, through QEMU automatically. +# - the build container is pulled for the target CPU family (apptainer pull --arch ...) +# - the CPU model that QEMU emulates is selected via $QEMU_CPU, which is passed into the +# container. QEMU fakes /proc/cpuinfo for aarch64 and riscv64 accordingly, so that +# archspec and compilers (-mcpu=native) detect the target CPU. +# +# Note: x86_64 as target (e.g. on an aarch64 build host) is NOT supported: QEMU does not +# fake /proc/cpuinfo for x86_64 targets, so archspec would see the host CPU. + +# Map an EESSI CPU family (as in 'uname -m') to the architecture name used by +# container registries / 'apptainer pull --arch' +function eessi_container_arch_for_family() { + case "$1" in + x86_64) echo "amd64" ;; + aarch64) echo "arm64" ;; + riscv64) echo "riscv64" ;; + *) return 1 ;; + esac +} + +# Map an EESSI software subdir (e.g. aarch64/neoverse_n1) to a QEMU CPU model. +# Can be overruled by setting $EESSI_QEMU_CPU_OVERRIDE (e.g. via 'exportvariable' in the bot command). +# Run 'qemu-aarch64-static -cpu help' / 'qemu-riscv64-static -cpu help' to see what your QEMU supports. +function eessi_qemu_cpu_for_subdir() { + local subdir="$1" + if [[ -n "${EESSI_QEMU_CPU_OVERRIDE}" ]]; then + echo "${EESSI_QEMU_CPU_OVERRIDE}" + return 0 + fi + case "${subdir}" in + aarch64/neoverse_n1) echo "neoverse-n1" ;; + aarch64/neoverse_v1) echo "neoverse-v1" ;; + aarch64/neoverse_v2) echo "neoverse-v2" ;; + aarch64/nvidia/grace) echo "neoverse-v2" ;; + aarch64/a64fx) echo "a64fx" ;; + # Armv8.0 baseline for generic builds + aarch64/generic) echo "cortex-a57" ;; + # RV64GC baseline: QEMU's 'rv64' enables many extensions by default, so switch those off + riscv64/generic) echo "rv64,v=false,h=false,zba=false,zbb=false,zbc=false,zbs=false,zfa=false,zicbom=false,zicboz=false,zihintntl=false,zihintpause=false,zawrs=false,sstc=false,svadu=false" ;; + *) return 1 ;; + esac +} + +# Check that binfmt_misc has a usable entry for the given CPU family. +# Prints the interpreter on success, returns non-zero (with an explanation on stderr) otherwise. +function eessi_check_binfmt_for_family() { + local family="$1" + # EESSI_BINFMT_MISC_DIR only exists to allow testing these functions + local entry="${EESSI_BINFMT_MISC_DIR:-/proc/sys/fs/binfmt_misc}/qemu-${family}" + if [[ ! -r "${entry}" ]]; then + echo "no binfmt_misc entry '${entry}' found; install qemu-user-static (or equivalent) on the build host" >&2 + return 1 + fi + if ! grep -q '^enabled' "${entry}"; then + echo "binfmt_misc entry '${entry}' is not enabled" >&2 + return 1 + fi + # The 'F' flag is required: it makes the kernel open the interpreter when the entry is + # registered, so it also works inside containers that do not contain QEMU + if ! grep -E '^flags:' "${entry}" | grep -q 'F'; then + echo "binfmt_misc entry '${entry}' lacks the 'F' (fix-binary) flag, it will not work inside containers" >&2 + return 1 + fi + grep '^interpreter' "${entry}" | awk '{print $2}' +} + +# Set up QEMU emulation if the target software subdir is for a different CPU family than the host. +# Sets & exports: +# - EESSI_EMULATED (1 if emulation is used, empty otherwise) +# - EESSI_TARGET_CPU_FAMILY (CPU family of the target, e.g. aarch64) +# - EESSI_CONTAINER_ARCH (architecture to pull the build container for, picked up by eessi_container.sh) +# - EESSI_QEMU_CPU (QEMU CPU model, passed into the container as $QEMU_CPU by eessi_container.sh) +# Arguments: $1 = target software subdir (e.g. aarch64/neoverse_n1), $2 = host CPU family (uname -m) +function eessi_setup_emulation() { + local subdir="$1" + local host_family="$2" + local target_family="${subdir%%/*}" + export EESSI_TARGET_CPU_FAMILY="${target_family:-${host_family}}" + export EESSI_EMULATED= + + if [[ -z "${subdir}" || "${target_family}" == "${host_family}" ]]; then + echo "qemu_emulation: native build (host: ${host_family}, target: ${subdir:-})" + return 0 + fi + + echo "qemu_emulation: host CPU family '${host_family}' differs from target '${subdir}', using QEMU emulation" + + if [[ "${target_family}" == "x86_64" ]]; then + fatal_error "qemu_emulation: emulating x86_64 is not supported (QEMU does not fake /proc/cpuinfo for x86_64)" + fi + + local interpreter + interpreter=$(eessi_check_binfmt_for_family "${target_family}") \ + || fatal_error "qemu_emulation: cannot emulate ${target_family} on this build host (see message above)" + echo "qemu_emulation: binfmt_misc interpreter for ${target_family}: ${interpreter}" + + EESSI_CONTAINER_ARCH=$(eessi_container_arch_for_family "${target_family}") \ + || fatal_error "qemu_emulation: unknown container architecture for CPU family '${target_family}'" + export EESSI_CONTAINER_ARCH + + EESSI_QEMU_CPU=$(eessi_qemu_cpu_for_subdir "${subdir}") \ + || fatal_error "qemu_emulation: no QEMU CPU model known for '${subdir}'; set EESSI_QEMU_CPU_OVERRIDE" + export EESSI_QEMU_CPU + + # check that the installed QEMU knows this CPU model (models differ between QEMU versions, e.g. + # neoverse-v2 is not in QEMU 8.2); otherwise every process in the container would fail to start. + # The interpreter is often a '*-binfmt-P' symlink, which expects binfmt-style arguments, so resolve it first + local qemu_bin cpu_model="${EESSI_QEMU_CPU%%,*}" + qemu_bin=$(readlink -f "${interpreter}") + if [[ -x "${qemu_bin}" ]]; then + if ! "${qemu_bin}" -cpu help 2>/dev/null | grep -qw -- "${cpu_model}"; then + fatal_error "qemu_emulation: CPU model '${cpu_model}' is not supported by ${qemu_bin} ('${qemu_bin} -cpu help' lists the supported ones)" + fi + else + echo_yellow "qemu_emulation: could not run ${qemu_bin} to check whether it supports CPU model '${cpu_model}'" + fi + + export EESSI_EMULATED=1 + echo "qemu_emulation: EESSI_CONTAINER_ARCH='${EESSI_CONTAINER_ARCH}', EESSI_QEMU_CPU='${EESSI_QEMU_CPU}'" +}