From c585ae00e034bdd19242d41a0e027b5b525caeb4 Mon Sep 17 00:00:00 2001 From: Manuel Date: Sat, 26 Apr 2025 19:26:16 +0000 Subject: [PATCH] add system setup script --- system-setup.sh | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 system-setup.sh diff --git a/system-setup.sh b/system-setup.sh new file mode 100644 index 00000000..ee992dd0 --- /dev/null +++ b/system-setup.sh @@ -0,0 +1,142 @@ +#!/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. Installs necessary 32-bit libraries (like lzo, libgcc, readline) on the +# x86_64 host. These are required if you execute 32-bit tools (like mksquashfs +# or xorriso from bootstrap environments) directly on the host system. +# NOTE: If you configure your build script to run these tools *inside* +# the target chroot environment using 'chroot' or 'xbps-uchroot', these +# 32-bit host libraries might not be strictly necessary for *that specific* +# reason, but are often needed by other host tools. +# 3. Installs QEMU user mode emulators, including the one for aarch64. +# 4. Ensures the 'binfmt_misc' kernel module is loaded and its filesystem is +# mounted. This kernel feature allows executing binaries for foreign +# architectures using emulators like QEMU. +# 5. Registers the installed QEMU user emulators with the kernel's binfmt_misc +# interface, enabling the execution of foreign binaries (like aarch64 +# executables) via QEMU emulation when run in a chroot or similar environment. +# +# 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. It DOES NOT modify your +# iso_builder.py script. You STILL NEED to modify your iso_builder.py script +# to: +# - Execute cross-architecture commands (like xbps-install for bootstrap, +# mksquashfs, xorriso) *inside* the target rootfs or bootstrap environment +# using 'sudo chroot /path/to/target/rootfs ' or 'xbps-uchroot'. +# - Ensure the correct repository URLs for the target architecture are used +# in your xbps-install commands within the script. +# ============================================================================== + +# 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 + +# 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 +echo "=> Installing 32-bit libraries for i686 execution on the host (if needed)..." +xbps-install -y lzo-32bit libgcc-32bit readline8-32bit || { + echo "Warning: Failed to install one or more 32-bit packages. Continuing." + # Don't exit here, as QEMU packages are still required for aarch64 etc. +} + +# Install QEMU user mode emulators +echo "=> Installing QEMU user mode emulators (including aarch64)..." +# Install the qemu-user-static dummy package, which depends on architecture-specific ones. +# Explicitly installing qemu-user-aarch64 for clarity, though dependency might cover it. +xbps-install -y qemu-user-static qemu-user-aarch64 + +# Ensure binfmt_misc kernel module is loaded and filesystem is mounted +echo "=> Verifying and ensuring binfmt_misc is active in the kernel..." + +# Load module if not loaded +if ! lsmod | grep -q binfmt_misc; then + echo "Loading binfmt_misc kernel module..." + modprobe binfmt_misc + # Give a little time for the /proc filesystem to update + sleep 1 +fi + +# Mount filesystem if not mounted +if ! mountpoint -q /proc/sys/fs/binfmt_misc; then + echo "Mounting binfmt_misc filesystem at /proc/sys/fs/binfmt_misc..." + mount -t binfmt_misc none /proc/sys/fs/binfmt_misc + # Give a little time for the filesystem to be mounted + sleep 1 +fi + +# Verify that the directory is accessible after attempting to load/mount +if [ ! -d "/proc/sys/fs/binfmt_misc" ]; then + echo "FATAL ERROR: The directory /proc/sys/fs/binfmt_misc does not exist or is not accessible." + echo "binfmt_misc setup failed. Cannot proceed with emulation." + exit 1 +fi + +# Register QEMU user binfmts from installed configuration files +echo "=> Registering QEMU user emulators with the kernel (binfmt_misc)..." +# Iterate over all qemu-* configuration files found in the standard location +for binfmt_file in /usr/share/binfmts/qemu-*; do + if [ -f "$binfmt_file" ]; then + # Extract the binfmt name (e.g., qemu-aarch64) + binfmt_name=$(basename "$binfmt_file") + # Expected path in /proc where it would be registered + proc_binfmt_path="/proc/sys/fs/binfmt_misc/$binfmt_name" + + # Check if it's already registered (the file exists in /proc) + if [ -f "$proc_binfmt_path" ]; then + echo "$binfmt_name is already registered." + else + echo "Registering $binfmt_name..." + # Copy the configuration file from /usr/share to /proc to register it. + # We use tee with sudo to ensure the write operation is done as root. + # The > /dev/null prevents tee from printing the content to stdout. + if sudo cp "$binfmt_file" /proc/sys/fs/binfmt_misc/; then + echo "$binfmt_name registered successfully." + else + echo "Warning: Failed to register $binfmt_name. This may impact builds for this architecture." + fi + fi + fi +done + +# Verify specifically if the aarch64 registration was successful +echo "=> Verifying if qemu-aarch64 emulation is active..." +if [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then + echo "qemu-aarch64 emulation is active and registered." +else + echo "ERROR: qemu-aarch64 emulation registration failed!" + echo "aarch64 builds will likely fail with 'Executable format error'." + # Do not exit here, as other registrations might have worked +fi + +echo "=> Host environment setup complete." +echo "You can now try running your iso_builder.py script for aarch64 (or another architecture)." +echo "Remember to modify your iso_builder.py script to:" +echo "1. Execute the initial bootstrap 'xbps-install' command *inside* the chroot for the target rootfs." +echo "2. Use ONLY the repository URLs relevant for the target architecture in that 'xbps-install' command." \ No newline at end of file