new commit

This commit is contained in:
Manuel 2025-04-27 19:16:54 +00:00
parent 39c0d2093f
commit 7d04494cf2

View File

@ -72,71 +72,92 @@ echo "=> Installing QEMU user mode emulators (including aarch64)..."
# 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..."
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
# 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..."
echo "Mounting binfmt_misc filesystem..."
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
# Verificar se o diretório está acessível após tentar carregar/montar
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."
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
# 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
echo "=> Registering QEMU user binfmts from /usr/share/binfmts/ using echo to register..."
# Register all qemu binfmts found in /usr/share/binfmts/
for binfmt_file in /usr/share/binfmts/qemu-*; do
if [ -f "$binfmt_file" ]; then
# Extract the binfmt name (e.g., qemu-aarch64)
# Get the name (e.g., qemu-aarch64)
binfmt_name=$(basename "$binfmt_file")
# Expected path in /proc where it would be registered
# 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)
# Check if it's already registered (the file exists)
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
# Read the content of the configuration file
config_content=$(cat "$binfmt_file")
# Parse the content to build the single-line format for /proc/sys/fs/binfmt_misc/register
# Format: :name:type:offset:magic:mask:interpreter:flags
# This parsing is a bit fragile in pure bash, assuming standard structure
interpreter=$(echo "$config_content" | grep '^interpreter' | awk '{print $2}')
magic=$(echo "$config_content" | grep '^magic' | cut -d' ' -f 2-) # Get hex bytes after 'magic '
mask=$(echo "$config_content" | grep '^mask' | cut -d' ' -f 2-)  # Get hex bytes after 'mask '
# Determine type (M for magic, F for script) - all qemu-user are M
binfmt_type="M"
binfmt_offset="0" # ELF magic is usually at offset 0
# Determine flags (C, P, F)
flags=""
if echo "$config_content" | grep -q '^credentials yes'; then flags+="C"; fi
if echo "$config_content" | grep -q '^preserve yes'; then flags+="P"; fi
if echo "$config_content" | grep -q '^fix_binary yes'; then flags+="F"; fi
# Add other potential flags if needed, but CPF are common for qemu
if [ -z "$interpreter" ] || [ -z "$magic" ] || [ -z "$mask" ]; then
echo "Warning: Could not parse configuration for $binfmt_file. Skipping registration."
continue # Skip to next file
fi
# Construct the registration string
registration_string=":$binfmt_name:$binfmt_type:$binfmt_offset:$magic:$mask:$interpreter:$flags"
# Write the string to the register file using sudo sh -c
# The > /dev/null prevents the echo output from appearing
if sudo sh -c "echo '$registration_string' > /proc/sys/fs/binfmt_misc/register"; then
echo "$binfmt_name registered successfully."
else
echo "Warning: Failed to register $binfmt_name. This may impact builds for this architecture."
# Don't exit, try to register others
fi
fi
fi
done
# Verify specifically if the aarch64 registration was successful
echo "=> Verifying if qemu-aarch64 emulation is active..."
echo "=> Verifying registration for qemu-aarch64..."
if [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then
echo "qemu-aarch64 emulation is active and registered."
echo "Emulação qemu-aarch64 está ativa e registada."
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
echo "ERRO: O registo da emulação qemu-aarch64 falhou!"
echo "A build para aarch64 provavelmente falhará com 'Erro de formato do executável'."
# Don't 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."