#!/bin/bash # ============================================================================== # SCRIPT: setup_host_build_env.sh # PURPOSE: Prepares a Void Linux host environment for cross-architecture ISO builds. # # This script automates several setup steps required on the host machine # when building ISO images for architectures different from the host (e.g., # building aarch64 or i686 ISOs on an x86_64 host). # # What this script does: # 1. Updates the host's package list. # 2. **Enables the multilib repository on the host.** # 3. Installs necessary 32-bit libraries (like lzo, libgcc, readline) on the # x86_64 host if needed for host-executed tools. # 4. Installs QEMU user mode emulators, including the one for aarch64, and the # binfmt-support package. # 5. Ensures the 'binfmt_misc' kernel module is loaded and its filesystem is # mounted. # 6. Registers the installed QEMU user emulators with the kernel's binfmt_misc # interface using the correct update-binfmts command and ensures the # binfmt-support service is enabled and running for persistence. # # PREREQUISITES: # - A Void Linux host system. # - An active internet connection to download packages. # - sudo privileges to install packages and configure system settings. # # USAGE: # 1. Save this code to a file (e.g., setup_host_build_env.sh). # 2. Make the file executable: chmod +x setup_host_build_env.sh # 3. Run the script with sudo: sudo ./setup_host_build_env.sh # # IMPORTANT NOTE: # This script ONLY sets up the HOST environment dependencies required for # cross-building. It DOES NOT modify your iso_builder.py script logic. # Your iso_builder.py script still needs to be correctly configured to: # - Use the right package names for the target architecture in its YAML configs. # - Execute package management and system configuration commands, directing them #   to the target rootfs (e.g., using -r, XBPS_ARCH, or chroot helpers). # ============================================================================== # Exit immediately if a command exits with a non-zero status. set -e # Check if running as root if [ "$EUID" -ne 0 ]; then echo "Error: Please run this script with sudo." exit 1 fi echo "=> Updating host package list..." xbps-install -S -y # --- START: Enable Multilib Repository on Host --- echo "=> Enabling multilib repository on host..." # Install the void-repo-multilib package to add the multilib repository configuration if xbps-install -y void-repo-multilib; then echo "Multilib repository package installed." # Synchronize repositories again to include the newly added multilib repo echo "Synchronizing repositories to include multilib..." xbps-install -S -y echo "Repositories synchronized with multilib enabled." else echo "Warning: Failed to install void-repo-multilib package. Multilib dependencies may not be available." # Do not exit, continue with the rest of the setup fi # --- END: Enable Multilib Repository on Host --- # Install 32-bit libraries needed for i686 tools executed on the host # These might be needed if you don't run mksquashfs/xorriso etc. in a chroot/uchroot # Ensure these packages are available *after* enabling multilib echo "=> Installing 32-bit libraries for i686 execution on the host (if needed)..." xbps-install -y lzo-32bit libgcc-32bit readline-32bit || { echo "Warning: Failed to install one or more 32-bit packages. This might affect i686 builds if host tools require them." # Don't exit here, as QEMU packages are still required for aarch64 etc. } # Install QEMU user mode emulators and binfmt-support echo "=> Installing QEMU user mode emulators (including aarch64) and binfmt-support..." # Install the qemu-user dummy package (depends on architecture-specific ones) # Explicitly installing qemu-user-aarch64 for clarity and binfmt-support xbps-install -y qemu-user qemu-user-aarch64 binfmt-support echo "=> Ensuring binfmt_misc kernel module is loaded and filesystem is mounted..." # Load module if not loaded if ! lsmod | grep -q binfmt_misc; then echo "Loading binfmt_misc kernel module..." modprobe binfmt_misc sleep 1 fi # Mount filesystem if not mounted if ! mountpoint -q /proc/sys/fs/binfmt_misc; then echo "Mounting binfmt_misc filesystem..." mount -t binfmt_misc none /proc/sys/fs/binfmt_misc sleep 1 fi # Verificar se o diretório está acessível após tentar carregar/montar if [ ! -d "/proc/sys/fs/binfmt_misc" ]; then echo "ERRO FATAL: O diretório /proc/sys/fs/binfmt_misc não existe ou não está acessível." echo "A configuração do binfmt_misc falhou. Não é possível continuar com a emulação." exit 1 fi # --- START CORRECTED BINFMT REGISTRATION --- echo "=> Registering QEMU user binfmts from /usr/share/binfmts/ using update-binfmts --import..." # Use the correct command to import and register all binfmt configurations from /usr/share/binfmts/ # This command reads the config files and writes to /proc/sys/fs/binfmt_misc/register correctly. if /usr/bin/update-binfmts --import; then echo "All binfmt entries from /usr/share/binfmts/ registered successfully." else echo "Warning: Failed to register one or more binfmt entries using update-binfmts --import." echo "Check /proc/sys/fs/binfmt_misc/ for registered entries (e.g., qemu-aarch64)." # The build might still work if the required ones are registered, but this indicates a potential issue. # Don't exit here to allow checking registered entries afterwards. fi # We also need to ensure the runit service is enabled and running so registration # happens automatically on boot. echo "=> Ensuring binfmt-support runit service is enabled and running..." if [ -d "/etc/sv/binfmt-support" ]; then # Check if the symlink in /var/service exists and is correct if [ ! -L "/var/service/binfmt-support" ] || [ "$(readlink /var/service/binfmt-support)" != "/etc/sv/binfmt-support" ]; then echo "Enabling binfmt-support service..." # Create/force symlink in /var/service to enable the service ln -sf /etc/sv/binfmt-support /var/service/ || echo "Warning: Failed to enable binfmt-support service." fi # Restart the service to ensure it runs the (now hopefully corrected run script) # This is important if the script was modified while the service was running. echo "Restarting binfmt-support service to ensure configuration is applied..." sv restart binfmt-support || echo "Warning: Failed to restart binfmt-support service." else echo "Warning: binfmt-support runit service directory /etc/sv/binfmt-support not found." echo "Automatic binfmt registration on boot may not be configured." fi echo "=> Verifying registration for qemu-aarch64 in /proc/sys/fs/binfmt_misc/..." # Check if the specific qemu-aarch64 entry exists after attempting registration if [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then echo "SUCCESS: Emulação qemu-aarch64 está ativa e registada." else echo "ERRO: O registo da emulação qemu-aarch64 falhou!" echo "A build para aarch64 provavelmente falhará com 'Erro de formato do executável'." # Do not exit here. Allow the user to see the check result and potentially debug manually. fi echo "=> Host environment setup complete." echo "You can now try running your iso_builder.py script for aarch64 (or another architecture)."