Chapter 7. Compiling the kernel

Table of Contents
7.1. Installing the kernel sources
7.2. Italian keyboard layout
7.3. Recompiling the kernel
7.4. Creating the kernel configuration file
7.5. Configuring the kernel
7.6. Generating dependencies and recompiling
7.7. If something went wrong

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

7.1. Installing the kernel sources

Sysinst, the installation program, does not copy the kernel sources to the hard disk, therefore they must be extracted manually. The archive for the kernel sources is in the source/sets directory and is called syssrc.tgz.

# gzip -dc syssrc.tgz | (cd / ; tar xvf -)    

Be patient: this operation lasts many minutes, because the archive 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
    

After extracting the kernel sources, you can save hard disk space by removing the directories for the architectures that you don't need. Just cd to /sys/arch and delete the directories you don't need. On the i386 port, for example, you can keep only the i386 directory. For other architectures it might be necessary to keep more than one source directory.

Once the sources are installed, you can create a custom kernel: this is not difficult as you think. In fact, a new kernel can be created in only four or five steps, which will be described in the following sections.

7.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:

7.3. Recompiling the kernel

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

7.4. Creating the kernel configuration file

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
    

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 is available in the package system (pkgsrc/sysutils/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 8. 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, ...)

7.5. 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.

7.6. 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:

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    

7.7. 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".)