Draco's Weblog

My Personal Programming & Electronics Journal.

FreeBSD: Build and Install a Custom Kernel

| Comments

FreeBSD is a very powerful OS and you can even achieve higher performance by modifying the Kernel and using the modules you need.

Before playing with the kernel you need to have the full FreeBSD source tree installed. The source tree lives in /usr/src

If you don’t have it then you can get it using subversion svn, follow this guide: Section A.5, “Using Subversion”.

A great resource for building a custom kernel is Chapter 9 of the FreeBSD Handbook, it should be your main guide through the process.

First cd to arch/conf, where arch is the architecture of your machine — use uname -a to display it, we will assume i386 same as the manual. Afterwards copy the GENERIC configuration file to a new folder under /root/kernels/ name it in capital letters (traditions) we will use MYKERNEL here, you could use whatever you prefer. The manual says it’s better to name it after the hostname of your machine if you are working with multiple machines.

— Tip: When finished customizing the kernel configuration file, save a backup copy to a location outside of /usr/src. Do not edit GENERIC directly.

Alternately, keep the kernel configuration file elsewhere and create a symbolic link to the file in i386.

1
2
3
4
# cd /usr/src/sys/i386/conf
# mkdir /root/kernels
# cp GENERIC /root/kernels/MYKERNEL
# ln -s /root/kernels/MYKERNEL

Next start editing your new configuration file MYKERNEL. You are advised to check the Handbook section.

One of the first things to edit is the ident argument, change it to your custom kernel name, in our case MYKERNEL.

1
ident MYKERNEL

When I first built my custom kernel it was for the purpose of optimizing a server for testing IPFW and other firewall functionality, I also removed all the unnecessary jargon and anything I don’t need for the purpose of this server.

You need to spend some time with the configuration file options and the kernel config in the handbook to get a better sense of each configuration.

After you are done, save the file and head over to the next step.

Note: It is required to have the full FreeBSD source tree installed to build the kernel. Now you are ready to compile your new kernel, using buildkernel and specifying your kernel name MYKERNEL.

Next install your new kernel using installkernel command.

The above steps are gonna take some hours depending on your hardware config so be prepared, with your favorite book.

Please make sure you have a recent backup before installing the new kernel.

1
2
3
# cd /usr/src
# make buildkernel KERNCONF=MYKERNEL
# make installkernel KERNCONF=MYKERNEL

That’s it. Now you just need to restart and experience the power!

1
# reboot

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. Now, shutdown the system and reboot into the new kernel. If something goes wrong, refer to the troubleshooting instructions and the section which explains how to recover when the new kernel does not boot.

A great advice by Rhyous worth mentioning is to check your disk space before compiling your new kernel.

If you just want to edit one module, in my case I was working on IPFW, you probably don’t have to recompile the whole system, in order to compile faster you can edit the /etc/make.conf file

The MODULES_OVERRIDE variable will only build the specified modules instead of building everything.

1
MODULES_OVERRIDE = net netinet/ipfw

More information is available from the handbook.

Comments