Difference between revisions of "Kernel Build Notes"

From eLinux.org
Jump to: navigation, search
(help)
(images and files)
Line 88: Line 88:
  
 
The set of files produced is:
 
The set of files produced is:
- a copy of the config file
+
* a copy of the config file
- the "map" file, which has the addresses of symbols within the kernel image
+
* the "map" file, which has the addresses of symbols within the kernel image
- the kernel image file, which is the kernel executable image itself
+
* the kernel image file, which is the kernel executable image itself
- the kernel modules, which can be loaded dynamically after a kernel is running
+
* the kernel modules, which can be loaded dynamically after a kernel is running
  
 
= packaging kernel output =
 
= packaging kernel output =

Revision as of 11:28, 1 June 2023

Here are some notes on building a kernel for embedded systems:

help

Use "make help" to get a list of targets that are available in the kernel build system (the kernel source's native Makefile-based system)

There are many different make targets that can be used to

  • get additional help
  • perform static checks of the kernel source
  • set the configuration options for the kernel build
  • select the image format for a kernel
  • package the kernel for later installation
  • install the kernel immediately on the system where the build is being performed
  • run tests on the kernel (either the currently executing kernel or the kernel just built)

build variables

When building for embedded boards, you are often cross-compiling from x86_64 to a different processor architecture. The kernel supports this by allowing you to specify certain build variables. These are specified either in the shell environment or on the 'make' command line.

architecture

When you are doing a cross-build (building the kernel for an architecture other than the one you are on), you always need to specify the ARCH and CROSS_COMPILE variables, which indicate the machine architecture.

Common architectures to cross-build are: arm, aarch64, x86, x86_64, riscv, mips

cross-compiler prefix

The CROSS_COMPILE variable should be the prefix of the toolchain you are using. For example if you are building using the GNU toolchain collection, and specifically are using tools with the executable names: arm-linux-gnueabi-gcc, arm-linux-gnueabi-as, arm-linux-gnueabi-ld, etc., then your toolchain prefix is "arm-linux-gnueabi-". In particular, notice that the cross-compile toolchain prefix string includes the dash ("-") at the end.

Here is an example make line:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- zImage

In general these variables need to be set on ever make command line (that is, every time make is invoked).

setting the build output directory

If you don't specify one, the build artifacts are put into the current source directory.

You can use a different directory by setting KBUILD_OUTPUT (either in an environment variable, or on the make command line). This is particularly useful if you build using different configurations or architectures, from the same source tree.

Example: export KBUILD_OUTPUT=../test-build-bbb ; make ARCH=arm CROSS_COMPILE=... zImage

setting config

You can configure the kernel manually, or using a default config ("defconfig") from an existing file, or by automatically probing the system that the build is currently running on. (This can be useful for building only the modules that are in current use on your system - see 'make localmodconfig')

The following targets are for interactively, manually, setting the kernel config: config, nconfig, menuconfig, xconfig, gconfig, oldconfig

You can set the configuration for the kernel using a file using "make {defconfig-name}"

The list of available defconfigs is under arch/$ARCH/configs. In order to figure out the right defconfig for your board, you have to search online for build instructions. It is, unfortunately, not really feasible to figure it out on your own by looking at the config contents. Usually the name is a good hint.

The defconfig for a beaglebone black board is 'omap2plus_defconfig', so to set the configuration for building the kernel for the beaglebone, the command is:

 make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- omap2plus_defconfig

The configuration file is saved in a file named ".config" at the root of the build output directory (which is the root of the current source directory if KBUILD_OUTPUT is not set)

build outputs

choosing a kernel image target format

You can select what type of kernel image to build, using the 'make' target.

Common formats are 'vmlinux' and 'zImage'.

In general, a "vmlinux" file is uncompressed, and a kernel image file with a 'z' somewhere in its name is compressed. Compressed kernels include a bit of code which self-decompresses the kernel when the bootloader loads the kernel image and transfers control to it.

images and files

The kernel build system will produce a set of images that can be used to execute and debug the Linux kernel.

The set of files produced is:

  • a copy of the config file
  • the "map" file, which has the addresses of symbols within the kernel image
  • the kernel image file, which is the kernel executable image itself
  • the kernel modules, which can be loaded dynamically after a kernel is running

packaging kernel output

The kernel build system can package your kernel build artifacts in a number of ways, directly from 'make'.

For example: 'make rpm-pkg' or 'make deb-pkg'

To make a directory of files, suitable for putting into a tarfile or other archive, use:

make ... dir-pkg

This latter command will make a directory called 'tar-install' in the build output directory, with sub-directories: boot, and lib/modules, where the kernel image artifacts and kernel module artifacts are stored, respectively.

kernel release name

The release name of the kernel is used for naming the directory under lib/modules where modules reside, and as suffixes on the names of kernel artifacts in the boot directory.