Kernel Compilation
Chapter 81: Linux Kernel Compilation
Section titled “Chapter 81: Linux Kernel Compilation”Comprehensive Guide to Building Custom Kernels
Section titled “Comprehensive Guide to Building Custom Kernels”Why This Matters in DevOps/SRE
Section titled “Why This Matters in DevOps/SRE”Kernel compilation is an advanced but sometimes necessary skill:
- Performance: Custom kernels can be optimized for specific workloads
- Security: Apply latest kernel security patches
- Hardware Support: Add support for new or custom hardware
- Troubleshooting: Fix kernel bugs not yet in stable releases
- Certification: RHCSA and RHCE exams cover kernel management
Most production systems use distribution kernels, but understanding compilation helps in embedded systems, security research, and specialized deployments.
81.1 Kernel Compilation Overview
Section titled “81.1 Kernel Compilation Overview”Why Compile a Custom Kernel?
Section titled “Why Compile a Custom Kernel?” Reasons for Custom Kernel+------------------------------------------------------------------+| || Benefits: || +----------------------------------------------------------+ || | • Smaller kernel size (remove unused features) | || | • Better hardware support (latest drivers) | || | • Performance optimization | || | • Security patches (latest) | || | • Learning about kernel internals | || | • Custom patches/features | || +----------------------------------------------------------+ || || When NOT to compile: || +----------------------------------------------------------+ || | • Production systems (use distribution kernel) | || | • Beginners (complex troubleshooting) | || | • Need enterprise support | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+81.2 Preparation
Section titled “81.2 Preparation”Installing Dependencies
Section titled “Installing Dependencies”# Arch Linuxsudo pacman -S base-devel bc cpio elfutils gzip libelf \ openssl pkgconf tar xz
# Ubuntu/Debiansudo apt install build-essential bc cpio flex libelf-dev \ libncurses5-dev libssl-dev pkg-config
# RHEL/CentOSsudo yum groupinstall "Development Tools"sudo yum install ncurses-devel openssl-devel elfutils-libelf-develGetting Kernel Source
Section titled “Getting Kernel Source”# From kernel.orgwget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.tar.xztar -xf linux-6.6.tar.xzcd linux-6.6
# From Gitgit clone --depth 1 https://github.com/torvalds/linux.gitcd linux
# Using apt source (Debian/Ubuntu)apt source linux-image-$(uname -r)81.3 Configuration
Section titled “81.3 Configuration”Configuration Methods
Section titled “Configuration Methods”# Using current config (recommended)cp /boot/config-$(uname -r) .config
# Using localmodconfig (modules from current system)make localmodconfig
# Using localyesconfig (everything currently used as built-in)make localyesconfig
# Using defconfig (default)make defconfig
# Interactive configurationmake menuconfig # NCurses (terminal)make xconfig # Qt (graphical)make gconfig # GTK (graphical)
# Configuration helpersmake olddefconfig # Use existing config, set new defaultsmake savedefconfig # Save minimal configImportant Configuration Options
Section titled “Important Configuration Options” Kernel Configuration+------------------------------------------------------------------+| || Processor Type: || +----------------------------------------------------------+ || | Processor family → Select your CPU architecture | || | General setup → Tickless system (for servers) | || +----------------------------------------------------------+ || || Loadable Module Support: || +----------------------------------------------------------+ || | Enable loadable module support = Y | || | Module unloading = Y | || +----------------------------------------------------------+ || || File Systems: || +----------------------------------------------------------+ || | Ext4, XFS, Btrfs (select needed) | || | Network file systems (NFS, CIFS) | || +----------------------------------------------------------+ || || Device Drivers: || +----------------------------------------------------------+ || | Select hardware drivers for your system | || | Graphics, network, storage | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+81.4 Building
Section titled “81.4 Building”Compilation Process
Section titled “Compilation Process”# Clean before buildmake clean # Remove most build filesmake mrproper # Remove all generated filesmake distclean # mrproper + editor files
# Compilemake -j$(nproc) # Parallel buildmake -j8 # 8 jobs
# Install modulessudo make modules_install
# Install kernelsudo make install
# Update bootloadersudo grub-mkconfig -o /boot/grub/grub.cfg
# Alternative: Using deb-pkg (Debian/Ubuntu)make deb-pkg -j$(nproc)Build Output
Section titled “Build Output” Build Output+------------------------------------------------------------------+| || After compilation: || +----------------------------------------------------------+ || | arch/x86/boot/bzImage | Kernel image | || | vmlinux | uncompressed kernel | || | System.map | Symbol table | || | .config | Configuration | || +----------------------------------------------------------+ || || After modules_install: || +----------------------------------------------------------+ || | /lib/modules/<version>/ | Kernel modules | || +----------------------------------------------------------+ || || After make install: || +----------------------------------------------------------+ || | /boot/vmlinuz-<version> | Kernel image | || | /boot/initrd.img-<version> | Initial ramdisk | || | /boot/System.map-<version> | Symbol table | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+81.5 Post-Installation
Section titled “81.5 Post-Installation”Verifying Installation
Section titled “Verifying Installation”# List installed kernelsls -la /boot/vmlinuz-*ls -la /lib/modules/
# Check boot entrycat /boot/grub/grub.cfg | grep "menuentry"
# Reboot and select new kernelsudo reboot
# Check kernel versionuname -runame -a
# Remove old kernels (Debian/Ubuntu)sudo apt autoremovesudo apt autoclean81.6 Interview Questions
Section titled “81.6 Interview Questions”Basic Questions
Section titled “Basic Questions”-
Why compile a custom kernel?
- Performance, size optimization, custom features
-
What’s the difference between make clean and make mrproper?
- clean removes build files, mrproper removes everything
-
What is localmodconfig?
- Uses current system config as base
-
What is initrd/initramfs?
- Initial ram disk for early boot
Summary
Section titled “Summary” Quick Reference+------------------------------------------------------------------+| || Commands: || +----------------------------------------------------------+ || | make menuconfig | Configure kernel | || | make -j$(nproc) | Compile | || | sudo make modules_install | Install modules | || | sudo make install | Install kernel | || | uname -r | Check kernel version | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+Common Mistakes & Anti-Patterns
Section titled “Common Mistakes & Anti-Patterns”1. Not Keeping Backup Kernel
Section titled “1. Not Keeping Backup Kernel”WRONG:
# Just install new kernel over oldsudo make installCORRECT:
# Keep working kernel in bootloader# Have recovery options# Test new kernel firstWhy: Broken kernel = unbootable system.
Interview Questions
Section titled “Interview Questions”Conceptual Questions
Section titled “Conceptual Questions”-
Q: What’s the difference between monolithic and modular kernels?
- A: Monolithic = all drivers in one binary, faster but less flexible. Modular = drivers as loadable modules, more flexible, easier to update.
-
Q: How do you troubleshoot a kernel panic?
- A: Check panic message for error, note kernel version, look at recent changes, try previous kernel, check hardware, review logs.