Guide to FreeBSD Kernel Compilation

Home / Guide to FreeBSD Kernel Compilation

Guide to FreeBSD Kernel Compilation

December 5, 2015 | Article | No Comments

On previous articles, we have discuss about how to compile kernel in Linux. So far we also have discuss about how to add our own system call. But how about other Operating System?

Today, most of the functionality in the FreeBSD kernel is contained in modules which can be dynamically loaded and unloaded from the kernel as necessary. This allows the running kernel to adapt immediately to new hardware or for new functionality to be brought into the kernel. This is also known as modular kernel.

Unless we have built custom kernel in FreeBSD, we are using GENERIC kernel on default. The GENERIC kernel support a wide range of hardware, contains everything we need to get machine up and running. The custom kernel can stripped down to only provide support for specific hardware of the machine, thus increasing speed and efficiency.

The common reasons for rebuilding the kernel could be fall into these category:

  1. Speed it up by taking out unused modules.
  2. Add support for new hardware
  3. Update the kernel with the new source code during a Make World.

We can also get some benefits, such as:

  1. Faster boot time. Since the kernel will only probe the hardware on the system thus make booting time faster
  2. Lower memory usage. Kernel memory remains resident in physical memory until system shut down. Thus, a minimal modules loaded can gives more memory to be used by another applications.

Now, this article will discuss about how to do a kernel recompilation on FreeBSD. On this article I will use FreeBSD 8.3 amd64 version (another version should be okay).

Finding the System Hardware

Before continuing to compile kernel, it is recommended to find and list all the system hardware of our machine. Make a list of what device is used by system so the corresponding driver can be included on custom kernel.

During boot probe, found hardware will be listed. We can use dmesg command to see what hardware had already identified by system. For example the following lines indicate that the psm driver found a mouse:

psm0: <PS/2 Mouse> irq 12 on atkbdc0
psm0: [GIANT-LOCKED]
psm0: [ITHREAD]
psm0: model Generic PS/2 mouse, device ID0

On occasion, the data from dmesg will only show system message instead of the boot probe output. In these situations, the output may be obtained by reading /var/run/dmesg.boot file.

Another method for finding hardware is to use pciconf which provides more verbose output. For example invoking command “pciconf -lv” will show ath driver located a wireless Ethernet device, such as:

[email protected]:3:0:0: card=0x058a1014 chip=0x1014168c rev=0x01 hdr=0x00
vendor = 'Atheros Communications Inc.'
device = 'AR5212 Atheros AR5212 802.11abg wireless'
class = network
subclass = ethernet

A comprehensive information can be obtained by typing man ath to read ath device manual page.

Creating the Kernel Configuration

Like all kernel compilation’s first step, we need to bring a configuration or a list of what our new kernel would be. In this article we will take a sample from our good old GENERIC config from our machine. We do this by invoking following (be sure you have root privileges):

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

If the architecture you use is not FreeBSD amd64, then replace amd64 with your corresponding architecture.

Once you have done this, open the new kernel config (the one we have copied) with text editor such as vim or ee and begin “hack” it.

The general format of a configuration file is quite simple. Each line contains a keyword and one or more arguments. For simplicity, most lines only contain one argument. Anything following a # is considered a commend and will be ignored.

An include directive is available for use in configuration file. This allows another configuration file to be included in the current one and make it easy to maintain small changes relative to an existing file.

The machine directive is telling what machine architecture we used. The argument is either amd64, i386, ia64, pc98, powerpc, or sparc64.

Exclude modules you do not needed by write ‘#’ (hash) character on the beginning of the line.

You should also change the ident directive’s argument to the name of the file. This value will be printed when the kernel boots and a good message to identify what kernel version is running now..

List of kernel configuration can be found here.

By default, when a custom kernel is compiled, all kernel modules are rebuilt as well. To update a kernel faster or to build only custom modules, we can edit /etc/make.conf before proceed to next stage. To build a kernel with a list of modules, we can write something like this:

MODULES_OVERRIDE = linux acpi sound/sound sound/driver/ds1 ntfs

In other hand, if we want to exclude list of modules from rebuilding, we can use:

WITHOUT_MODULES = linux acpi sound ntfs

After saving the edits, we can proceed to next stage.

Building and Installing

Before proceeding, it is required to have the full FreeBSD source tree installed

cd /usr/src
make buildkernel KERNCONF=NEWKERNEL
make installkernel KERNCONF=NEWKERNEL

The new kernel will be copied to /boot/kernel as /boot/kernel/kernel and the old kernel will be moved to /boot/kernel.old/kernel

Reboot

When finished customizing the kernel, save a backup copy to a location outside of /usr/src.

Reboot the machine by invoking reboot command. If the machine successfully reboot but gives errors, mostly because the kernel was compiled with the source code that is newer than that of the world.

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial