#!/bin/bash set -e # Exit on any error # Kernel Build Script for Gentoo # This script manually builds the kernel without using genkernel # Date: 2025-11-05 # Configuration KERNEL_VERSION="6.12.41-gentoo-x86_64" KERNEL_SRC="/usr/src/linux" LOG_DIR="/home/alexander/repository/git.hinrichs.dev/alexander/claude/gentoo-setup/logs" TIMESTAMP=$(date +%Y%m%d-%H%M%S) BUILD_LOG="${LOG_DIR}/kernel-build-${TIMESTAMP}.log" CPU_CORES=16 # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color log() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$BUILD_LOG" } error() { echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1" | tee -a "$BUILD_LOG" exit 1 } warning() { echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] WARNING:${NC} $1" | tee -a "$BUILD_LOG" } # Check if running as root if [ "$EUID" -ne 0 ]; then error "This script must be run as root (use sudo)" fi # Create log directory if it doesn't exist mkdir -p "$LOG_DIR" log "=== Kernel Build Started ===" log "Kernel Version: $KERNEL_VERSION" log "Source Directory: $KERNEL_SRC" log "Build Log: $BUILD_LOG" # Step 1: Navigate to kernel source cd "$KERNEL_SRC" || error "Failed to change to $KERNEL_SRC" log "Changed to kernel source directory" # Step 2: Backup current config log "Backing up current .config to .config.backup" if [ -f .config ]; then cp .config .config.backup || error "Failed to backup .config" log "Config backed up successfully" else error "No .config file found in $KERNEL_SRC" fi # Step 3: Run make mrproper (clean everything) log "Running make mrproper (cleaning build tree)..." make mrproper >> "$BUILD_LOG" 2>&1 || error "make mrproper failed" log "Build tree cleaned" # Step 4: Restore config from backup log "Restoring .config from backup" cp .config.backup .config || error "Failed to restore .config" log "Config restored successfully" # Step 5: Run make oldconfig (handle any new options) log "Running make oldconfig (updating config for new options)..." yes "" | make oldconfig >> "$BUILD_LOG" 2>&1 || error "make oldconfig failed" log "Config updated" # Step 6: Build the kernel log "Building kernel with -j${CPU_CORES}..." log "This may take several minutes..." make -j${CPU_CORES} >> "$BUILD_LOG" 2>&1 || error "Kernel build failed" log "Kernel built successfully" # Step 7: Install kernel modules log "Installing kernel modules..." make modules_install >> "$BUILD_LOG" 2>&1 || error "Module installation failed" log "Modules installed to /lib/modules/${KERNEL_VERSION}" # Step 8: Backup old kernel files log "Creating backups of old kernel files in /boot" BACKUP_SUFFIX="backup-${TIMESTAMP}" if [ -f /boot/vmlinuz-${KERNEL_VERSION} ]; then cp /boot/vmlinuz-${KERNEL_VERSION} /boot/vmlinuz-${KERNEL_VERSION}.${BACKUP_SUFFIX} log "Backed up vmlinuz to vmlinuz-${KERNEL_VERSION}.${BACKUP_SUFFIX}" fi if [ -f /boot/System.map-${KERNEL_VERSION} ]; then cp /boot/System.map-${KERNEL_VERSION} /boot/System.map-${KERNEL_VERSION}.${BACKUP_SUFFIX} log "Backed up System.map to System.map-${KERNEL_VERSION}.${BACKUP_SUFFIX}" fi if [ -f /boot/initramfs-${KERNEL_VERSION}.img ]; then cp /boot/initramfs-${KERNEL_VERSION}.img /boot/initramfs-${KERNEL_VERSION}.img.${BACKUP_SUFFIX} log "Backed up initramfs to initramfs-${KERNEL_VERSION}.img.${BACKUP_SUFFIX}" fi # Step 9: Copy new kernel to /boot log "Copying new kernel to /boot/vmlinuz-${KERNEL_VERSION}" cp arch/x86_64/boot/bzImage /boot/vmlinuz-${KERNEL_VERSION} || error "Failed to copy kernel" log "Kernel copied successfully" # Step 10: Copy System.map log "Copying System.map to /boot/System.map-${KERNEL_VERSION}" cp System.map /boot/System.map-${KERNEL_VERSION} || error "Failed to copy System.map" log "System.map copied successfully" # Step 11: Generate initramfs with dracut log "Generating initramfs with dracut..." dracut --force --kver ${KERNEL_VERSION} /boot/initramfs-${KERNEL_VERSION}.img >> "$BUILD_LOG" 2>&1 || error "dracut failed" log "Initramfs generated successfully" # Step 12: Update GRUB log "Updating GRUB configuration..." grub-mkconfig -o /boot/grub/grub.cfg >> "$BUILD_LOG" 2>&1 || error "GRUB update failed" log "GRUB updated successfully" # Step 13: Verify files exist log "Verifying kernel files in /boot..." if [ ! -f /boot/vmlinuz-${KERNEL_VERSION} ]; then error "vmlinuz-${KERNEL_VERSION} not found in /boot" fi if [ ! -f /boot/System.map-${KERNEL_VERSION} ]; then error "System.map-${KERNEL_VERSION} not found in /boot" fi if [ ! -f /boot/initramfs-${KERNEL_VERSION}.img ]; then error "initramfs-${KERNEL_VERSION}.img not found in /boot" fi log "All kernel files verified" # Summary log "=== Kernel Build Completed Successfully ===" log "Kernel: /boot/vmlinuz-${KERNEL_VERSION}" log "System.map: /boot/System.map-${KERNEL_VERSION}" log "Initramfs: /boot/initramfs-${KERNEL_VERSION}.img" log "Modules: /lib/modules/${KERNEL_VERSION}" log "Build log: ${BUILD_LOG}" log "" log "Next steps:" log "1. Review the build log for any warnings" log "2. Reboot the system: sudo reboot" log "3. After reboot, verify kernel version: uname -r" log "" warning "Don't forget to reboot to load the new kernel!"