Most NetBSD users will, sooner or later, compile a customized kernel. This gives you several benefits:
you can dramatically reduce kernel size and, therefore, memory occupation (for example, from 2.5 MB to 1.2 MB). On version 1.5 of NetBSD compiling a custom kernel reduced the size from 4.7 MB to 1.9 MB.
you can improve performance.
you can tune the system.
you can solve problems of detection/conflicts of peripherals.
you can customize some options (for example keyboard layout, BIOS clock offset, ...)
you can get a deeper knowledge of the system.
You can get the kernel sources from the same source where you got your installed system (for example ftp://ftp.netbsd.org.) Remeber to download the sources for the release you are running. |
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.
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 13.
To recompile the kernel you must have installed the compiler set (comp.tgz).
Basic steps for kernel compilation
Create/modify the kernel configuration file
Configure the kernel
Generate dependencies
"Make" the kernel
Install the kernel
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:
support for hardware devices is included/excluded in the kernel (for example, SCSI support can be removed if it is not needed.)
support for kernel features is enabled/disabled (for example, enable NFS client support, enable Linux compatibility, ...)
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 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, ...)
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.
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
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".)
Reboot the machine
Press the space bar at the boot prompt during the 5 seconds countdown
boot:
Type
> boot netbsd.old -s
Now issue the following commands to restore the previous version of the kernel:
fsck / mount / mv netbsd.old netbsd exit