Files
darkforge/toolchain/scripts/024-chroot-essentials.sh
2026-03-20 13:41:04 +01:00

123 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# DarkForge Linux — Phase 0, Chapter 7: Create Essential Files and Symlinks
# ============================================================================
# Purpose: Create the essential system files (/etc/passwd, /etc/group,
# log files) and symlinks that programs expect to exist.
# This script runs INSIDE the chroot environment.
# Inputs: None (creates files from scratch)
# Outputs: /etc/passwd, /etc/group, log files, essential symlinks
# Assumes: Running inside chroot (script 023 completed)
# Ref: LFS 13.0 §7.6
# ============================================================================
set -euo pipefail
echo "=== DarkForge: Creating essential files and symlinks ==="
# --- Create essential symlinks ------------------------------------------------
# Some programs hardcode paths to /bin/sh, /bin/cat, etc.
# With the unified /usr layout, these are already correct via symlinks
# created in 000-env-setup.sh, but verify:
[ -L /bin ] || { echo "ERROR: /bin should be a symlink to usr/bin"; exit 1; }
[ -L /lib ] || { echo "ERROR: /lib should be a symlink to usr/lib"; exit 1; }
[ -L /sbin ] || { echo "ERROR: /sbin should be a symlink to usr/sbin"; exit 1; }
# --- Create /etc/passwd -------------------------------------------------------
cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/usr/bin/false
daemon:x:6:6:Daemon User:/dev/null:/usr/bin/false
messagebus:x:18:18:D-Bus Message Daemon User:/run/dbus:/usr/bin/false
uuidd:x:80:80:UUID Generation Daemon User:/dev/null:/usr/bin/false
nobody:x:65534:65534:Unprivileged User:/dev/null:/usr/bin/false
EOF
# --- Create /etc/group --------------------------------------------------------
cat > /etc/group << "EOF"
root:x:0:
bin:x:1:daemon
sys:x:2:
kmem:x:3:
tape:x:4:
tty:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
cdrom:x:15:
adm:x:16:
messagebus:x:18:
input:x:24:
mail:x:34:
kvm:x:61:
uuidd:x:80:
wheel:x:97:
users:x:999:
nogroup:x:65534:
EOF
# --- Create log files ---------------------------------------------------------
# These must exist for certain programs to function correctly
install -o root -g utmp -m 664 /dev/null /var/log/lastlog
install -o root -g root -m 600 /dev/null /var/log/btmp
install -o root -g root -m 664 /dev/null /var/log/faillog
install -o root -g utmp -m 664 /dev/null /var/log/wtmp
echo "=== Essential files and symlinks created ==="
echo ""
# --- Verify compiler works inside chroot --------------------------------------
echo ">>> Verifying gcc works inside chroot..."
echo ""
# Show where gcc is
echo " which gcc: $(which gcc 2>&1 || echo 'NOT FOUND')"
echo " which cc: $(which cc 2>&1 || echo 'NOT FOUND')"
echo " which ld: $(which ld 2>&1 || echo 'NOT FOUND')"
echo ""
# Check for the dynamic linker
if [ -e /lib64/ld-linux-x86-64.so.2 ]; then
echo " /lib64/ld-linux-x86-64.so.2: EXISTS"
ls -la /lib64/ld-linux-x86-64.so.2
else
echo " WARNING: /lib64/ld-linux-x86-64.so.2 NOT FOUND"
echo " Checking /usr/lib..."
ls -la /usr/lib/ld-linux-x86-64.so.2 2>/dev/null || echo " /usr/lib/ld-linux-x86-64.so.2 NOT FOUND"
echo " Contents of /lib64/:"
ls -la /lib64/ 2>/dev/null || echo " /lib64 does not exist"
fi
echo ""
# Check for crt files
echo " crt1.o: $(find /usr/lib -name crt1.o 2>/dev/null || echo 'NOT FOUND')"
echo " crti.o: $(find /usr/lib -name crti.o 2>/dev/null || echo 'NOT FOUND')"
echo ""
# Try to compile a test program
echo " Testing: echo 'int main(){}' | gcc -x c - -o /tmp/test-gcc"
if echo 'int main(){}' | gcc -x c - -o /tmp/test-gcc 2>/tmp/gcc-test-error.log; then
echo " PASS: gcc can create executables"
rm -f /tmp/test-gcc
else
echo " FAIL: gcc cannot create executables!"
echo " Error output:"
cat /tmp/gcc-test-error.log
echo ""
echo " Trying with verbose output:"
echo 'int main(){}' | gcc -v -x c - -o /tmp/test-gcc 2>&1 | tail -30
echo ""
echo " gcc search dirs:"
gcc -print-search-dirs 2>&1 | head -10
exit 1
fi
rm -f /tmp/gcc-test-error.log
echo ""
echo "Next: Run the chroot build scripts (025-xxx) to build additional tools."