Files
darkforge/toolchain/bootstrap.sh
2026-03-20 13:22:01 +01:00

134 lines
5.3 KiB
Bash

#!/bin/bash
# ============================================================================
# DarkForge Linux — Phase 0: Full Bootstrap
# ============================================================================
# Purpose: Single entry point that runs the entire Phase 0 bootstrap:
# 1. Tear down & create fresh loopback filesystem
# 2. Set up directory structure, lfs user, env files
# 3. Download source tarballs
# 4. Copy toolchain scripts to $LFS so lfs user can access them
# 5. Build cross-toolchain as lfs user (Chapters 5+6)
# 6. Enter chroot and build Chapter 7 tools
#
# Usage: sudo -E bash toolchain/bootstrap.sh
# (run from the project root, e.g. /home/danny/darkforge)
#
# Inputs: LFS (default: /mnt/darkforge)
# Outputs: A complete cross-toolchain and temporary tools on $LFS
# Assumes: Running as root on Arch Linux, internet access
# ============================================================================
set -euo pipefail
# --- Configuration -----------------------------------------------------------
export LFS="${LFS:-/mnt/darkforge}"
# Detect the project root (parent of toolchain/)
BOOTSTRAP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "${BOOTSTRAP_DIR}")"
SCRIPT_SRC="${BOOTSTRAP_DIR}/scripts"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
ok() { echo -e "${GREEN}>>> $*${NC}"; }
warn() { echo -e "${YELLOW}>>> $*${NC}"; }
info() { echo -e "${CYAN}>>> $*${NC}"; }
fail() { echo -e "${RED}>>> $*${NC}"; exit 1; }
# --- Verify running as root --------------------------------------------------
[ "$(id -u)" -eq 0 ] || fail "This script must be run as root (use sudo -E)."
echo "============================================================"
echo " DarkForge Linux — Phase 0 Full Bootstrap"
echo "============================================================"
echo ""
echo " Project root: ${PROJECT_ROOT}"
echo " LFS mount: ${LFS}"
echo ""
# =============================================================================
# Step 1: Create fresh loopback filesystem
# =============================================================================
info "STEP 1/6: Setting up build filesystem..."
bash "${SCRIPT_SRC}/000-setup-disk.sh"
echo ""
# =============================================================================
# Step 2: Set up directory structure, lfs user, and env
# =============================================================================
info "STEP 2/6: Setting up environment..."
bash "${SCRIPT_SRC}/000-env-setup.sh"
echo ""
# =============================================================================
# Step 3: Download all source tarballs
# =============================================================================
info "STEP 3/6: Downloading source tarballs..."
bash "${SCRIPT_SRC}/000a-download-sources.sh"
echo ""
# =============================================================================
# Step 4: Copy toolchain scripts to $LFS/sources/toolchain-scripts/
# =============================================================================
info "STEP 4/6: Copying toolchain scripts to ${LFS}/sources/toolchain-scripts/..."
SCRIPTS_DEST="${LFS}/sources/toolchain-scripts"
rm -rf "${SCRIPTS_DEST}"
mkdir -p "${SCRIPTS_DEST}"
cp -v "${SCRIPT_SRC}/"*.sh "${SCRIPTS_DEST}/"
chmod +x "${SCRIPTS_DEST}/"*.sh
chown -R lfs:lfs "${SCRIPTS_DEST}"
ok "Scripts copied and owned by lfs user"
echo ""
# =============================================================================
# Step 5: Run build-all.sh as lfs user
# =============================================================================
info "STEP 5/6: Building cross-toolchain as lfs user..."
echo " This will take a while (30-60+ minutes on 32 threads)."
echo " Logs will be in: ${LFS}/sources/logs/"
echo ""
# Run build-all.sh as the lfs user with a clean environment.
# We use 'su' without -l to avoid the lfs .bash_profile which does
# 'exec env -i /bin/bash' and would swallow our -c command.
# Instead, we build the clean environment ourselves.
env -i HOME=/home/lfs TERM="${TERM}" \
LFS="${LFS}" \
LC_ALL=POSIX \
LFS_TGT=x86_64-darkforge-linux-gnu \
PATH="${LFS}/tools/bin:/usr/bin" \
MAKEFLAGS="-j32" \
su lfs -s /bin/bash -c "bash ${SCRIPTS_DEST}/build-all.sh" || {
fail "Build failed! Check logs in ${LFS}/sources/logs/"
}
ok "Cross-compilation phase (Chapters 5+6) complete!"
echo ""
# =============================================================================
# Step 6: Set up chroot and run Chapter 7 scripts
# =============================================================================
info "STEP 6/6: Setting up chroot and building Chapter 7 tools..."
echo ""
bash "${SCRIPTS_DEST}/023-chroot-setup.sh"
bash "${SCRIPTS_DEST}/023a-chroot-build-all.sh"
echo ""
echo "============================================================"
echo -e "${GREEN} Phase 0 is FULLY COMPLETE!${NC}"
echo "============================================================"
echo ""
echo "The DarkForge toolchain chroot is ready."
echo "To enter the chroot manually:"
echo ""
echo " sudo chroot ${LFS} /usr/bin/env -i \\"
echo " HOME=/root TERM=\${TERM} \\"
echo " PATH=/usr/bin:/usr/sbin:/tools/bin \\"
echo " MAKEFLAGS=\"-j32\" /bin/bash --login"