Introduction
Compiling a kernel is a controlled deployment process. The goal is not merely to make compilation succeed, but to produce matching modules, an initramfs, and a bootloader entry without removing the working kernel.
What you should be able to do after this lesson:
- Prepare a kernel source tree.
- Reuse and adjust an existing configuration.
- Build the image and modules.
- Install modules, create an initramfs, and update the bootloader.
- Build an external module with DKMS.
- Recover when the new kernel does not boot.
Big Idea: Treat a Kernel Build Like a Deployment
The build has four distinct phases:
configure -> compile -> install artifacts -> verify and boot once
Each phase can succeed while the next one fails. A successful compiler run does not prove that modules were installed, the initramfs contains the root-storage driver, or the bootloader points to the correct files. Keep the existing kernel untouched until the complete path has been tested.
Prepare the Source
Install the compiler and build dependencies, then unpack the source under /usr/src/ or a working directory.
tar -xf linux-<version>.tar.xz
cd linux-<version>
Clean targets have different effects:
make clean
make mrproper
mrproper removes more generated state, including .config, so save a required configuration first.
Start from a Known Configuration
cp /boot/config-"$(uname -r)" .config
make oldconfig
Configuration interfaces include:
make menuconfig
make xconfig
make gconfig
For each feature, choose built-in, module, or disabled. Keep drivers needed to reach the root filesystem available during early boot.
Build the Kernel and Modules
make -j"$(nproc)"
make modules
sudo make modules_install
sudo make install
The generic make normally builds the required image and modules. Explicit architecture targets such as bzImage also exist. Package targets such as deb-pkg, rpm-pkg, or binrpm-pkg can produce manageable distribution packages.
modules_install places modules under /lib/modules/<new-release>/. depmod creates dependency and alias indexes used by module tools.
Useful make targets to recognize include:
config,menuconfig,xconfig, andgconfigfor configuration interfacesoldconfigfor accepting new questions based on an existing configurationall,zImage, andbzImagefor build targetsmodulesandmodules_installfor loadable modulesdeb-pkg,rpm-pkg, andbinrpm-pkgfor package-oriented buildscleanandmrproperfor different levels of cleanup
Build the Initramfs
The initramfs provides early userspace drivers and tools needed to find and mount the real root filesystem.
Common commands vary by distribution:
sudo update-initramfs -c -k <new-release>
sudo dracut --force /boot/initramfs-<new-release>.img <new-release>
sudo mkinitcpio -p <preset>
Older environments may use mkinitrd or mkinitramfs. Verify the output exists in /boot before rebooting.
Update and Verify the Bootloader
Distribution tooling may update GRUB automatically, but verify the entry and paths.
ls -lh /boot
ls /lib/modules/<new-release>
Keep the old kernel in the boot menu. Select the new entry once, confirm hardware and services, and only then consider changing the default.
External Modules and DKMS
DKMS rebuilds an external module when a new kernel is installed.
dkms status
dkms autoinstall
The external module still needs compatible source, headers, and a valid DKMS configuration.
Guided Practice: Plan a Safe Build Without Installing It
This exercise stops before privileged installation:
cd /usr/src/linux-<version>
cp /boot/config-"$(uname -r)" .config
make oldconfig
make -s kernelrelease
make listnewconfig
Record the new release string, then inspect three boot-critical settings:
grep -E '^CONFIG_(BLK_DEV_INITRD|DEVTMPFS|EXT4_FS)=' .config
The exact root-storage and filesystem options depend on your machine. Decide whether each required driver is built in (=y) or available as a module (=m) that the initramfs can load.
Estimate the build command but do not run it on a small or production host without checking disk space and CPU impact:
make -j"$(nproc)"
Finally, write a rollback checklist containing the old kernel entry, console access method, and commands needed to rebuild the initramfs and GRUB configuration.
Troubleshooting Scenario
The new kernel boots to an error saying it cannot find the encrypted LVM root. Its image and modules exist, but the initramfs was generated before the new modules were installed.
Boot the old kernel, confirm /lib/modules/<new-release>/, run depmod, and rebuild the initramfs for the exact new release. Verify that encryption, device-mapper, storage-controller, and root-filesystem support is present. The problem is an incomplete artifact set, not necessarily a bad kernel configuration.
Exam Focus
- Know which configuration and build targets preserve or remove
.config. modules_installand initramfs generation are separate operations.- DKMS rebuilds external modules for installed kernels; it does not replace in-tree module builds.
- Always preserve a known-good bootloader entry.
Troubleshooting
If compilation fails, inspect the first meaningful compiler error rather than the final make failure. If boot fails:
- Boot the previous kernel.
- Verify the new image, initramfs, and module directory use the same release.
- Check whether storage, encryption, LVM, and root filesystem drivers are available early.
- Rebuild the initramfs and bootloader configuration.
Recap
- Begin with a working configuration and change only what is required.
- Kernel image, modules, and initramfs must describe the same release.
- Preserve a known-good boot entry.
- Package builds and DKMS make ongoing maintenance safer.
Test Your Knowledge
Complete the quiz to assess your understanding of this course's concepts.
