Script Post Services for Laptop Ministries

Viewing: 1750018766_32.sh

Try wget https://script.laptopministry.org/uploads/1750018766_32.sh from the console

Raw File Link

#!/bin/bash
# Script to modify antiX ISO for 32-bit EFI compatibility on Arch Linux
# for early Intel Apple Products with 32bit EFI despite supporting 64bit
# operating systems.
# This script devised June 14th 2025 for the Laptop Ministry,
# laptopministry.org, copyleft 2025.

# Download the latest Antix Linux distro and execute this script with
# the name of the file download ISO. 

# Variables
ISO_ORIGINAL="$1"  # Path to original antiX ISO
ISO_MODIFIED="32bitEFI$1"  # Output modified ISO name
WORK_DIR="/home/admin/Downloads/antix_iso_work"
MOUNT_DIR="/home/admin/Downloads/mnt"
ISO_DIR="/home/admin/Downloads/iso"

# Function to check command success
check_error() {
    if [ $? -ne 0 ]; then
        echo "Error: $1"
        exit 1
    fi
}

# Function to log debug info
log_debug() {
    echo "DEBUG: $1"
}

# Step 1: Install required tools
log_debug "Checking for required packages..."
sudo pacman -S --needed grub cdrtools mtools dosfstools libisoburn || check_error "Failed to install packages"

# Step 2: Check xorriso version
log_debug "Checking xorriso version..."
xorriso --version > "$WORK_DIR/xorriso_version.txt"
cat "$WORK_DIR/xorriso_version.txt"

# Step 3: Create working directories
log_debug "Creating working directories..."
mkdir -p "$MOUNT_DIR" "$ISO_DIR" "$WORK_DIR/efi" || check_error "Failed to create directories"

# Step 4: Mount the original ISO
log_debug "Mounting original ISO: $ISO_ORIGINAL"
sudo mount -o loop "$ISO_ORIGINAL" "$MOUNT_DIR" || check_error "Failed to mount ISO"

# Step 5: Copy ISO contents to working directory
log_debug "Copying ISO contents from $MOUNT_DIR to $ISO_DIR..."
rsync -av "$MOUNT_DIR/" "$ISO_DIR/" || check_error "Failed to copy ISO contents"

# Step 6: Unmount the original ISO
log_debug "Unmounting original ISO..."
sudo umount "$MOUNT_DIR" || check_error "Failed to unmount ISO"

# Step 7: Find or create the EFI bootloader file
log_debug "Locating EFI bootloader file in $ISO_DIR/boot/efi/EFI..."
EFI_BOOT_PATH=$(find "$ISO_DIR/boot/efi/EFI" -type f -name "*.efi" -print -quit 2>/dev/null)
if [ -z "$EFI_BOOT_PATH" ]; then
    log_debug "No EFI bootloader found. Creating 32-bit EFI GRUB bootloader..."
    grub-mkimage -O i386-efi -o "$WORK_DIR/efi/bootia32.efi" -p /boot/grub \
        normal fat iso9660 part_gpt part_msdos linux echo all_video test search || check_error "Failed to create bootia32.efi"
    EFI_BOOT_PATH="$WORK_DIR/efi/bootia32.efi"
    EFI_BOOT_FILE="bootia32.efi"
else
    EFI_BOOT_FILE=$(basename "$EFI_BOOT_PATH")
    log_debug "Found EFI bootloader: $EFI_BOOT_PATH (filename: $EFI_BOOT_FILE)"
fi

# Step 8: Copy EFI bootloader to ISO
log_debug "Copying EFI bootloader to $ISO_DIR/boot/efi/EFI/BOOT/$EFI_BOOT_FILE..."
mkdir -p "$ISO_DIR/boot/efi/EFI/BOOT" || check_error "Failed to create EFI directory"
cp "$EFI_BOOT_PATH" "$ISO_DIR/boot/efi/EFI/BOOT/$EFI_BOOT_FILE" || check_error "Failed to copy EFI bootloader"

# Step 9: Create EFI image for El Torito
log_debug "Creating EFI image: $WORK_DIR/efi.img..."
mkfs.vfat -C "$WORK_DIR/efi.img" 1440 || check_error "Failed to create efi.img"
mmd -i "$WORK_DIR/efi.img" ::/EFI ::/EFI/BOOT || check_error "Failed to create EFI directories in efi.img"
mcopy -i "$WORK_DIR/efi.img" "$ISO_DIR/boot/efi/EFI/BOOT/$EFI_BOOT_FILE" ::/EFI/BOOT/ || check_error "Failed to copy EFI bootloader to efi.img"
cp "$WORK_DIR/efi.img" "$ISO_DIR/efi.img" || check_error "Failed to copy efi.img to ISO"
log_debug "Verifying efi.img contents..."
mdir -i "$ISO_DIR/efi.img" ::/EFI/BOOT || check_error "Failed to verify efi.img contents"

# Step 10: Modify GRUB configuration
log_debug "Creating GRUB configuration at $ISO_DIR/boot/grub/grub.cfg..."
cat << EOF > "$ISO_DIR/boot/grub/grub.cfg"
set timeout=5
set default=0

menuentry "antiX Live" {
    set root=(cd0)
    linux /antiX/vmlinuz quiet
    initrd /antiX/initrd.gz
}
EOF
check_error "Failed to create grub.cfg"

# Step 11: List boot files before building
log_debug "Listing boot files before building ISO..."
ls -l "$ISO_DIR/efi.img" "$ISO_DIR/boot/efi/EFI/BOOT/$EFI_BOOT_FILE" 2>/dev/null

# Step 12: Build the UEFI-only ISO with xorriso
log_debug "Building UEFI-only ISO: $ISO_MODIFIED..."
CONFIGURATIONS=(
    "-eltorito-boot efi.img -no-emul-boot"  # UEFI with efi.img
    "-eltorito-boot boot/efi/EFI/BOOT/$EFI_BOOT_FILE -no-emul-boot"  # Direct UEFI file
)
for CONFIG in "${CONFIGURATIONS[@]}"; do
    log_debug "Attempting build with configuration: $CONFIG"
    XORRISO_CMD="xorriso -as mkisofs -o \"$ISO_MODIFIED\" -J -R -V \"ANTIX_32EFI\" $CONFIG \"$ISO_DIR\" > \"$WORK_DIR/xorriso_log.txt\" 2>&1"
    log_debug "Executing xorriso command: $XORRISO_CMD"
    if eval "$XORRISO_CMD"; then
        cat "$WORK_DIR/xorriso_log.txt"
        break
    else
        log_debug "xorriso failed with configuration: $CONFIG. Log saved to $WORK_DIR/xorriso_log.txt"
        cat "$WORK_DIR/xorriso_log.txt"
        continue
    fi
done

if [ $? -ne 0 ]; then
    check_error "All xorriso UEFI configuration attempts failed. Check $WORK_DIR/xorriso_log.txt"
fi

# Step 13: Verify ISO boot structure
log_debug "Verifying ISO boot structure for $ISO_MODIFIED..."
xorriso -indev "$ISO_MODIFIED" -report_el_torito plain > "$WORK_DIR/el_torito_report.txt" || check_error "Failed to verify ISO boot structure"
cat "$WORK_DIR/el_torito_report.txt"
log_debug "Checking for boot image in El Torito catalog..."
grep -q "El Torito boot img" "$WORK_DIR/el_torito_report.txt" || log_debug "WARNING: No boot image found in El Torito catalog. ISO may not boot."

# Step 14: Clean up
log_debug "Cleaning up directories..."
rm -rf "$MOUNT_DIR" "$ISO_DIR" "$WORK_DIR" || check_error "Failed to clean up"

echo "Modified ISO created: $ISO_MODIFIED"
echo "Write to USB with: sudo dd if=$ISO_MODIFIED of=/dev/sdX bs=4M status=progress && sync"
BASH to Home