Published : 2009-04-28

Compile the kernel

Welcome to this FreeBSD tutorial series! BSD is not widely known by the general public, yet it is a UNIX-like system valued for optimizations and performance. By the end of this tutorial you will know how to rebuild the FreeBSD kernel.

Rebuilding the kernel allows you to optimize low-level routines used by programs and can yield measurable performance improvements.

Obtaining kernel sources

If you do not already have the sources, insert the FreeBSD CD and run sysinstall. In the Configure menu select “Distribution” and enable “All” to install the source tree.

Building the world and kernel

The first step is to build both the world (userland) and the kernel as root from the source tree. Run:

make -j4 buildworld
make -j4 buildkernel

The -j4 option builds files in parallel; allocate roughly one slot per CPU core.

Installing the kernel

After the builds complete (this may take a couple of hours), install the kernel:

make installkernel

Installing the world (userland)

Installing the world is performed from the single-user or maintenance environment. Reboot the machine and choose the boot menu option that drops you into maintenance mode (often option 4).

Note: the keyboard layout may be QWERTY in this environment.

Mount the root and UFS filesystems and enable swap:

mount -u /
mount -a -t ufs
swapon -a

Synchronize the kernel timezone settings:

adjkerntz -i

Then install the world:

make installworld

After installation you can reboot into normal mode.

Building and installing a single module/driver

If you are developing or updating a single kernel module, you do not need to rebuild the entire kernel. From the module directory:

cd /usr/src/sys/modules/<module>/
make
make load

This builds the module and loads it into the running kernel.