Chapter 9 Compiling the kernel

Table of Contents
9.1 Installing the kernel sources
9.2 Italian keyboard layout
9.3 Recompiling the kernel
9.4 Build the toolchain
9.5 Creating the kernel configuration file
9.6 Configuring the kernel
9.7 Generating dependencies and recompiling
9.8 If something went wrong

Most NetBSD users will, sooner or later, compile a customized kernel. This gives you several benefits:

9.1 Installing the kernel sources

Be patient: this operation lasts many minutes, because the repository contains hundreds of files. The sources live in /usr/src/sys; the symbolic link sys points to this directory. Therefore the following commands have the same effect:

# cd /usr/src/sys
# cd /sys

Once the sources are checked out, you can create a custom kernel: this is not as difficult as you think. In fact, a new kernel can be created in a few steps which will be described in the following sections.

9.2 Italian keyboard layout

Before compiling the kernel, Italian users should consider modifying the predefined layout for the italian keyboard, which is defined in the source file /sys/dev/pckbc/wskbdmap_mfii.c. In the default layout some characters useful for programmers are missing (for example, left and right braces and tilde). This is an alternative layout:

static const keysym_t pckbd_keydesc_it[] = {
...
KC(8),   KS_7,          KS_slash,       KS_braceleft,
KC(9),   KS_8,          KS_parenleft,   KS_bracketleft,
KC(10),  KS_9,          KS_parenright,  KS_bracketright,
KC(11),  KS_0,          KS_equal,       KS_braceright,
KC(12),  KS_apostrophe, KS_question,    KS_grave,
KC(13),  KS_igrave,     KS_asciicircum, KS_asciitilde,
KC(26),  KS_egrave,     KS_eacute,      KS_bracketleft, KS_braceleft,
KC(27),  KS_plus,       KS_asterisk,    KS_bracketright,KS_braceright,
...

The previous layout defines the following mappings:

Keys Character
Alt Gr + 7 {
Alt Gr + 8 [
Alt Gr + 9 ]
Alt Gr + 0 }
Alt Gr + ' `
Alt Gr + ì ~
Alt Gr + é [
Alt Gr + + ]
Shift + Alt Gr + è {
Shift + Alt Gr + + }

Console driver: starting with version 1.4, NetBSD uses the wscons multiplatform console driver to handle screen, keyboard and mouse. Previous versions used pccons or pcvt. For a detailed description, see Chapter 14.

9.3 Recompiling the kernel

To recompile the kernel you must have installed the compiler set (comp.tgz).

Basic steps for kernel compilation

  1. Build the toolchain

  2. Create/modify the kernel configuration file

  3. Configure the kernel

  4. Generate dependencies

  5. "Make" the kernel

  6. Install the kernel

9.4 Build the toolchain

The NetBSD toolchain provides a simple mechanism for compiling the NetBSD system both natively or when the need arises, cross compiling for other targets. In this example, a native toolchain is built (as root) by simply typing:

# cd /usr/src
#  ./build.sh tools

Once the tools are built, the kernel can be reconfigured and compiled.

9.5 Creating the kernel configuration file

Note: The directories described in this section are i386 specific. Users of other architectures must substitute the appropriate directories (usually subdirectories of arch.)

The kernel configuration file defines the type, the number and the characteristics of the devices supported by the kernel as well as several kernel configuration options. Kernel configuration files are located in the /sys/arch/i386/conf directory. The easiest way to create a new file is to copy an existing one and modify it: usually the best choice on most platforms is the GENERIC configuration. In the configuration file there are comments describing the options; a more detailed description is found in the options(4) man page.

# cd /sys/arch/i386/conf/
# cp GENERIC MYKERNEL
# vi MYKERNEL

Kernel names: the names of the kernel configuration files are historically in all uppercase.

The modification of a kernel configuration file basically involves three operations:

  1. support for hardware devices is included/excluded in the kernel (for example, SCSI support can be removed if it is not needed.)

  2. support for kernel features is enabled/disabled (for example, enable NFS client support, enable Linux compatibility, ...)

  3. tuning kernel parameters.

Lines beginning with "#" are comments; lines are disabled by commenting them and enabled by removing the comment character. It is better to comment lines instead of deleting them; it is always possible uncomment them later.

The output of the dmesg command can be used to determine which lines can be disabled. For each line of the type:

<XXX> at <YYY>

both XXX and YYY must be active in the kernel configuration file. You'll probably have to experiment a bit before achieving a minimal configuration but on a desktop system without SCSI and PCMCIA you can halve the kernel size.

You should also examine the options in the configuration file and disable the ones that you don't need. Each option has a short comment describing it, which is normally sufficient to understand what the option does. Many options have a longer and more detailed description in the options(4) man page. While you are at it you should set correctly the options for the national keyboard support and for local time on the CMOS clock. For example, for Italy:

options RTC_OFFSET=-60
...
options PCKBD_LAYOUT="KB_IT"

The adjustkernel Perl script, which can be found at http://www.feyrer.de/Misc/adjustkernel, analizes the output of dmesg and automatically generates a minimal configuration file. To run it you need to have Perl installed on your system. The installation of new software is described in detail in the Chapter 10. If you want to install Perl now, download the pre-compiled package perl-5.00404.tgz and write the following command:

# pkg_add perl-5.00404.tgz

Now Perl is installed, configured and ready to work: easier than this it's impossible...

You can now run the script with:

# cd /sys/arch/i386/conf
# perl adjustkernel GENERIC > MYKERNEL

I tried this script and it worked very well, saving me a lot of manual editing. Beware that the script only configures the available devices: you must still configure manually the other options (eg. Linux emulation, ...)

9.6 Configuring the kernel

When you've finished modifying the kernel configuration file (which we'll call MYKERNEL), you should issue the following command:

# config MYKERNEL

If MYKERNEL contains no errors, the config program will create the necessary files for the compilation of the kernel, otherwise it will be necessary to correct the errors before running config again.

9.7 Generating dependencies and recompiling

Dependencies generation and kernel compilation is performed by the following commands:

# cd ../compile/MYKERNEL
# make depend
# make

It can happen that the compilation stops with errors; there can be a variety of reasons but the most common cause is an error in the configuration file which didn't get caught by config. Sometimes the failure is caused by a hardware problem (often faulty RAM chips): the compilation puts a higher stress on the system than most applications do. Another typical error is the following: option B, active, requires option A which is not active.

A full compilation of the kernel can last from some minutes to several hours, depending on the hardware. See the following table for some examples:

CPU RAM (MB) Approx. time
486 DX2 50 20 1 hour
P166 96 15 minutes
PIII 128 5 minutes
68030/25 8 4 hours

The output of the make command is the netbsd file in the compile directory: this file should be copied in the root directory, after saving the previous version.

# mv /netbsd /netbsd.old
# mv netbsd /

Customization can considerably reduce the kernel's size. In the following example netbsd.old is the install kernel and netbsd is the new kernel.

-rwxr-xr-x  1 root  wheel  1342567 Nov 13 16:47 /netbsd
-rwxr-xr-x  1 root  wheel  3111739 Sep 27 01:20 /netbsd.old

The new kernel is activated after rebooting:

# reboot

9.8 If something went wrong

When the PC is restarted it can happen that the new new kernel doesn't work as expected or even doesn't boot at all. Don't worry: if this happens, just reboot with the previously saved kernel and remove the new one (it is better to reboot "single user".)