Making a Custom Kernel

update source tree
To update your system, you should check /usr/src/UPDATING for any pre-buildworld steps necessary for your version of the sources and then use the following procedure:

make -j4 buildworld
make buildkernel
make installkernel
reboot

Note: The -j4 switch is used to spawn multiple processes and according the FreeBSD Handbook, testing shows that this speeds up building world even on a single CPU system.
Note: There are a few rare cases when an extra run of mergemaster -p is needed before the buildworld step. These are described in UPDATING. In general, though, you can safely omit this step if you are not updating across one or more major FreeBSD versions.
After installkernel finishes successfully, you should boot in single user mode (i.e. using boot -s from the loader prompt). Then run:

mount -a
mergemaster -p
make installworld
mergemaster
reboot

This chapter assumes that you are using the i386 architecture in the examples. If this is not the case for your situation, make appropriate adjustments to the path names for your system’s architecture.

Note: If there is not a /usr/src/sys directory on your system, then the kernel source has not been installed. The easiest way to do this is by running sysinstall (/stand/sysinstall in FreeBSD versions older than 5.2) as root, choosing Configure, then Distributions, then src, then sys. If you have an aversion to sysinstall and you have access to an “official” FreeBSD CDROM, then you can also install the source from the command line:

mount /cdrom
mkdir -p /usr/src/sys
ln -s /usr/src/sys /sys
cat /cdrom/src/ssys.[a-d]* | tar -xzvf –

Next, move to the arch/conf directory and copy the GENERIC configuration file to the name you want to give your kernel. For example:

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

Traditionally, this name is in all capital letters and, if you are maintaining multiple FreeBSD machines with different hardware, it is a good idea to name it after your machine’s hostname. We will call it MYKERNEL for the purpose of this example.

Tip: Storing your kernel configuration file directly under /usr/src can be a bad idea. If you are experiencing problems it can be tempting to just delete /usr/src and start again. After doing this, it usually only takes a few seconds for you to realize that you have deleted your custom kernel configuration file. Also, do not edit GENERIC directly, as it may get overwritten the next time you update your source tree, and your kernel modifications will be lost.

You might want to keep your kernel configuration file elsewhere, and then create a symbolic link to the file in the i386 directory.

For example:

cd /usr/src/sys/i386/conf
mkdir /root/kernels
cp GENERIC /root/kernels/MYKERNEL
ln -s /root/kernels/MYKERNEL

Now, edit MYKERNEL with your favorite text editor. If you are just starting out, the only editor available will probably be vi, which is too complex to explain here, but is covered well in many books in the bibliography. However, FreeBSD does offer an easier editor called ee which, if you are a beginner, should be your editor of choice. Feel free to change the comment lines at the top to reflect your configuration or the changes you have made to differentiate it from GENERIC.
If you have built a kernel under SunOS™ or some other BSD operating system, much of this file will be very familiar to you. If you are coming from some other operating system such as DOS, on the other hand, the GENERIC configuration file might seem overwhelming to you, so follow the descriptions in the Configuration File section slowly and carefully.

Note: If you sync your source tree with the latest sources of the FreeBSD project, be sure to always check the file /usr/src/UPDATING before you perform any update steps. This file describes any important issues or areas requiring special attention within the updated source code. /usr/src/UPDATING always matches your version of the FreeBSD source, and is therefore more up to date with new information than this handbook.

You must now compile the source code for the kernel. There are two procedures you can use to do this, and the one you will use depends on why you are rebuilding the kernel and the version of FreeBSD that you are running.

  • If you have installed only the kernel source code, use procedure 1.
  • If you are running a FreeBSD version prior to 4.0, and you are not upgrading to FreeBSD 4.0 or higher using the make buildworld procedure, use procedure 1.
  • If you are building a new kernel without updating the source code (perhaps just to add a new option, such as IPFIREWALL) you can use either procedure.
  • If you are rebuilding the kernel as part of a make buildworld process, use procedure 2.

If you have not upgraded your source tree in any way since the last time you successfully completed a buildworld-installworld cycle (you have not run CVSup, CTM, or used anoncvs), then it is safe to use the config, make depend, make, make install sequence.

Procedure 1. Building a Kernel the “Traditional” Way
1) Run config(8) to generate the kernel source code.

/usr/sbin/config MYKERNEL

2) Change into the build directory. config(8) will print the name of this directory after being run as above.

cd ../compile/MYKERNEL

For FreeBSD versions prior to 5.0, use the following form instead:

cd ../../compile/MYKERNEL

3) Compile the kernel.

make depend
make

4) Install the new kernel.

make install

Procedure 2. Building a Kernel the “New” Way
1) Change to the /usr/src directory.

# cd /usr/src

2) Compile the kernel.

# make buildkernel KERNCONF=MYKERNEL

3) Install the new kernel.

# make installkernel KERNCONF=MYKERNEL

Note: This method of kernel building requires full source files. If you only installed the kernel source, use the traditional method, as described above.

Tip: By default, when you build a custom kernel, all kernel modules will be rebuilt as well. If you want to update a kernel faster or to build only custom modules, you should edit /etc/make.conf before starting to build the kernel:

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

This variable sets up a list of modules to build instead of all of them. For other variables which you may find useful in the process of building kernel, refer to make.conf(5) manual page.
The new kernel will be copied to the /boot/kernel directory as /boot/kernel/kernel and the old kernel will be moved to /boot/kernel.old/kernel. Now, shutdown the system and reboot to use your new kernel. If something goes wrong, there are some troubleshooting instructions at the end of this chapter that you may find useful. Be sure to read the section which explains how to recover in case your new kernel does not boot.

Note: In FreeBSD 4.X and earlier, kernels are installed in /kernel, modules in /modules, and old kernels are backed up in /kernel.old. Other files relating to the boot process, such as the boot loader(8) and configuration are stored in /boot. Third party or custom modules can be placed in /modules, although users should be aware that keeping modules in sync with the compiled kernel is very important. Modules not intended to run with the compiled kernel may result in instability or incorrectness.

Note: If you have added any new devices (such as sound cards) and you are running FreeBSD 4.X or previous versions, you may have to add some device nodes to your /dev directory before you can use them. For more information, take a look at Making Device Nodes section later on in this chapter.

This entry was posted in FreeBSD -- Setup. Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.