How to Rebuild the FreeBSD Kernel from Source on FreeBSD Operating System

This guide provides a detailed walkthrough on rebuilding the FreeBSD kernel from source.

Introduction

FreeBSD is a powerful, open-source Unix-like operating system renowned for its performance, security, and advanced networking capabilities. One of its key strengths is the ability to rebuild and customize the kernel to better suit specific needs, whether for performance tuning, hardware compatibility, or security hardening. This guide provides a detailed walkthrough on rebuilding the FreeBSD kernel from source.

Prerequisites

Before proceeding, ensure you have:

  • A working FreeBSD system with root privileges.
  • Adequate disk space (at least several gigabytes for source and build artifacts).
  • A backup of important data (kernel modifications can impact system stability).
  • Internet connectivity to fetch the latest source code.

Step 1: Install the FreeBSD Source Tree

First, confirm that the system has the FreeBSD source tree installed. If not, you can obtain it using git.

mkdir -p /usr/src
cd /usr/src
git clone --branch releng/14.0 https://git.FreeBSD.org/src.git .

Replace releng/14.0 with the appropriate branch corresponding to your FreeBSD version.

If you have already installed sources but need to update them:

cd /usr/src
git pull

Step 2: Configure the Kernel

The FreeBSD kernel configuration files are stored in /usr/src/sys/amd64/conf/ (for the amd64 architecture). You can list available kernel configurations with:

ls /usr/src/sys/amd64/conf/

To create a custom kernel configuration, copy the default GENERIC configuration:

cd /usr/src/sys/amd64/conf/
cp GENERIC MYKERNEL

Edit MYKERNEL using a text editor such as vi or nano:

vi MYKERNEL

Modify the file as needed. Common customizations include:

  • Enabling or disabling specific device drivers.
  • Tweaking system parameters for performance.
  • Removing unnecessary components for a minimal build.

Step 3: Build the Kernel

Before building, clean up any previous build artifacts:

cd /usr/src
make clean

Then, start the build process:

make buildkernel KERNCONF=MYKERNEL

This process can take some time, depending on system resources.

Step 4: Install the New Kernel

Once the build completes successfully, install the new kernel:

make installkernel KERNCONF=MYKERNEL

FreeBSD retains the previous kernel in /boot/kernel.old/, which serves as a fallback in case the new kernel fails.

Step 5: Reboot the System

After installing the new kernel, reboot the system:

reboot

Upon booting, verify the running kernel version with:

uname -v

Step 6: Troubleshooting and Recovery

If the new kernel fails to boot, you can revert to the previous kernel by selecting Boot to Single User Mode in the FreeBSD boot menu and running:

mv /boot/kernel /boot/kernel.bad
mv /boot/kernel.old /boot/kernel
reboot

Alternatively, from the bootloader prompt:

boot -s

Then manually replace the faulty kernel with the backup.

Conclusion

Rebuilding the FreeBSD kernel from source provides flexibility in customizing system performance, security, and hardware support. By following this guide, you can safely modify and install a custom kernel tailored to your requirements. Always test changes in a controlled environment before deploying them in production systems.