linux_dist
Markdown

Part 09 Kernel and Bootloader

Part 9: Linux Kernel and Bootloader Installation

Overview

Build and install the Linux kernel, then configure GRUB bootloader to make your LFS system bootable.

Prerequisites

  • Completed Part 8 (system configuration)
  • Still inside chroot
  • /etc/fstab properly configured

Part A: Build Linux Kernel

1. Extract Kernel Source

cd /sources
tar -xf linux-6.16.1.tar.xz
cd linux-6.16.1

2. Clean the Source Tree

make mrproper

3. Configure the Kernel

Option 1: Use default config (Quick)

# For x86_64
make x86_64_defconfig

# For i386 (32-bit)
make i386_defconfig

Option 2: Use menu config (Recommended)

make menuconfig

Essential Kernel Options to Enable:

Navigate using arrow keys, enable with Y, disable with N:

General setup --->
  [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
  [*] Configure standard kernel features (expert users) --->
    [*] Multiple users, groups and capabilities support

Processor type and features --->
  [*] Symmetric multi-processing support
  [*] EFI runtime service support (if using UEFI)

Device Drivers --->
  Generic Driver Options --->
    [*] Maintain a devtmpfs filesystem to mount at /dev
    [*]   Automount devtmpfs at /dev, after the kernel mounted the rootfs
  SCSI device support --->
    <*> SCSI disk support
  [*] Serial ATA and Parallel ATA drivers (libata) --->
    <*> AHCI SATA support (for modern systems)
    <*> Generic ATA support
  [*] Network device support --->
    [*] Ethernet driver support --->
      [*] Intel devices
      <*> Intel(R) PRO/1000 Gigabit Ethernet support (common in VMs)
      <*> Intel(R) PRO/1000 PCI-Express Gigabit Ethernet support

File systems --->
  <*> Second extended fs support
  <*> The Extended 4 (ext4) filesystem
  <*> XFS filesystem support
  Pseudo filesystems --->
    [*] /proc file system support
    [*] sysfs file system support
    [*] Tmpfs virtual memory file system support
    [*]   Tmpfs POSIX Access Control Lists

For Virtual Machines (VirtualBox, VMware, QEMU):

Device Drivers --->
  [*] Block devices --->
    <*> Virtio block driver (for QEMU/KVM)
  [*] Network device support --->
    [*] Ethernet driver support --->
      [*] Intel devices
        <*> Intel(R) PRO/1000 Gigabit Ethernet support
      <*> AMD PCnet32 PCI support (VMware/VirtualBox)
  Graphics support --->
    <*> /dev/agpgart (AGP Support) --->
      <*> Intel 440LX/BX/GX, I8xx and E7x05 chipset support
    <*> Direct Rendering Manager --->
      <*> Intel 8xx/9xx/G3x/G4x/HD Graphics (VMware/VirtualBox)
    Console display driver support --->
      <*> Framebuffer Console support

Save and exit (use Save menu, then Exit).

4. Compile the Kernel

# Check number of cores
nproc

# Compile with parallel jobs
make -j$(nproc)

Build Time: 1-3 hours depending on configuration and CPU.

5. Install Kernel Modules

make modules_install

6. Install Kernel Image

# Copy kernel to /boot
cp -iv arch/x86/boot/bzImage /boot/vmlinuz-6.16.1-lfs-12.4

# Copy System.map for debugging
cp -iv System.map /boot/System.map-6.16.1

# Copy kernel configuration
cp -iv .config /boot/config-6.16.1

# Install documentation
install -d /usr/share/doc/linux-6.16.1
cp -r Documentation/* /usr/share/doc/linux-6.16.1

# Clean up source directory
cd /sources
rm -rf linux-6.16.1

7. Configure Kernel Module Load Order (Optional)

install -v -m755 -d /etc/modprobe.d
cat > /etc/modprobe.d/usb.conf << "EOF"
# Begin /etc/modprobe.d/usb.conf

install ohci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i ohci_hcd ; true
install uhci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i uhci_hcd ; true

# End /etc/modprobe.d/usb.conf
EOF

Part B: Install GRUB Bootloader

1. Ensure You're on BIOS or UEFI

Determine your boot mode:

[ -d /sys/firmware/efi ] && echo "UEFI" || echo "BIOS"

2. Install GRUB (BIOS Systems)

# Install GRUB to disk
# Replace /dev/sda with your actual disk device (NOT partition!)
grub-install /dev/sda

Warning: Use the device name (e.g., /dev/sda), NOT a partition (e.g., /dev/sda1).

3. Install GRUB (UEFI Systems)

# Mount EFI partition (if not already mounted)
mkdir -pv /boot/efi
mount /dev/sda1 /boot/efi  # Replace sda1 with your EFI partition

# Install GRUB for UEFI
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=LFS --recheck

# Create EFI boot entry
efibootmgr --create --disk /dev/sda --part 1 --loader '\EFI\LFS\grubx64.efi' --label "LFS 12.4"

4. Generate GRUB Configuration

cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod part_gpt
insmod ext2
set root=(hd0,2)

menuentry "GNU/Linux, Linux 6.16.1-lfs-12.4" {
        linux   /boot/vmlinuz-6.16.1-lfs-12.4 root=/dev/sda2 ro
}
EOF

Important: Replace:

  • (hd0,2) with your boot partition in GRUB notation
    • hd0 = first disk
    • ,2 = second partition (GRUB counts from 1)
    • Example: If /boot is on /dev/sda2, use (hd0,2)
  • /dev/sda2 with your root partition

GRUB Disk Notation:

  • /dev/sda = (hd0)
  • /dev/sdb = (hd1)
  • /dev/sda1 = (hd0,1)
  • /dev/sda2 = (hd0,2)

5. Alternative: Auto-Generate GRUB Config

If you want GRUB to auto-detect all kernels:

grub-mkconfig -o /boot/grub/grub.cfg

6. Verify GRUB Installation

# Check GRUB files exist
ls -l /boot/grub/grub.cfg
ls -l /boot/vmlinuz-6.16.1-lfs-12.4

# For UEFI, verify EFI files
ls -l /boot/efi/EFI/LFS/  # (UEFI only)

Part C: Additional Bootloader Configuration

Create GRUB Configuration with Multiple Options

cat > /boot/grub/grub.cfg << "EOF"
# Begin /boot/grub/grub.cfg
set default=0
set timeout=5

insmod part_gpt
insmod part_msdos
insmod ext2
insmod ext4

if [ "${grub_platform}" = "efi" ]; then
  insmod efi_gop
  insmod efi_uga
fi

set root=(hd0,2)

menuentry "GNU/Linux, Linux 6.16.1-lfs-12.4" {
        linux   /boot/vmlinuz-6.16.1-lfs-12.4 root=/dev/sda2 ro quiet
}

menuentry "GNU/Linux, Linux 6.16.1-lfs-12.4 (recovery mode)" {
        linux   /boot/vmlinuz-6.16.1-lfs-12.4 root=/dev/sda2 ro single
}
EOF

Set GRUB Boot Parameters

Common kernel parameters:

  • ro - Mount root filesystem read-only initially
  • quiet - Suppress most boot messages
  • splash - Show splash screen (if configured)
  • single - Boot into single-user mode (recovery)
  • 3 - Boot to runlevel 3 (multi-user text mode)
  • 5 - Boot to runlevel 5 (multi-user graphical mode)

Example with more parameters:

linux /boot/vmlinuz-6.16.1-lfs-12.4 root=/dev/sda2 ro quiet splash

Part D: Final Pre-Boot Steps

1. Update /etc/fstab with Boot Partition (if separate)

If you have a separate /boot partition:

# Add this line to /etc/fstab
/dev/sda1     /boot        ext4     defaults        1 2

2. Verify fstab Entries

cat /etc/fstab

# Verify with mount test (careful!)
mount -a
umount /boot  # If you mounted it

3. Create Kernel Module Dependencies

depmod -a

4. Final System Check

# Verify kernel exists
ls -lh /boot/vmlinuz-*

# Verify GRUB config
cat /boot/grub/grub.cfg

# Check GRUB installation
grub-install --version

# Verify root password is set
passwd -S root

Part E: Exit Chroot and Prepare for Reboot

1. Exit Chroot

# Inside chroot, logout
logout

2. Unmount Virtual Filesystems

# As root on host system
umount -v $LFS/dev/pts
mountpoint -q $LFS/dev/shm && umount -v $LFS/dev/shm
umount -v $LFS/dev
umount -v $LFS/run
umount -v $LFS/proc
umount -v $LFS/sys

3. Unmount LFS Partition

# Unmount main partition
umount -v $LFS

4. Reboot into LFS

# Remove or comment out LFS partition in host's /etc/fstab if added
# Then reboot
shutdown -r now

Troubleshooting

Issue: GRUB error: "unknown filesystem"

Solution: Rebuild GRUB with correct filesystem modules:

grub-install --modules="part_msdos part_gpt ext2 ext4" /dev/sda

Issue: Kernel panic: "VFS: Unable to mount root fs"

Solution: Check /boot/grub/grub.cfg has correct root= parameter:

root=/dev/sda2  # Must match your actual root partition

Issue: System boots to GRUB prompt

Solution: Manually boot kernel:

grub> set root=(hd0,2)
grub> linux /boot/vmlinuz-6.16.1-lfs-12.4 root=/dev/sda2 ro
grub> boot

Then regenerate grub.cfg.

Issue: No network interface found

Solution: Rebuild kernel with network drivers enabled.

Next Steps

Kernel and bootloader installed! Proceed to:

  • Part 10: First boot and final configuration

Important Notes

  • Always use device names for GRUB install (/dev/sda), never partitions
  • Verify /boot/grub/grub.cfg has correct partition references
  • Test in virtual machine before installing on real hardware
  • Keep a backup of working configuration

Kernel Customization Tips

To rebuild kernel with different options:

cd /sources
tar -xf linux-6.16.1.tar.xz
cd linux-6.16.1
make mrproper
make menuconfig
make -j$(nproc)
make modules_install
cp arch/x86/boot/bzImage /boot/vmlinuz-6.16.1-lfs-12.4-custom

Update GRUB config to add new entry.