Difference between revisions of "Kernels/Traditional compilation"

From ArchWiki
Jump to: navigation, search
(Configure your kernel: Add tip for intermediate users.)
(Fetching source: updated path to downloadable kernel source)
(24 intermediate revisions by 5 users not shown)
Line 3: Line 3:
 
[[ru:Kernel Compilation without ABS]]
 
[[ru:Kernel Compilation without ABS]]
 
[[zh-CN:Kernels/Compilation/Traditional]]
 
[[zh-CN:Kernels/Compilation/Traditional]]
The summary below is helpful for building custom kernels from '''kernel.org sources'''.  This method of compiling kernels is the traditional method common to all distros; however, an excellent method of cleanly installing the custom kernel with makepkg and pacman is also included.  
+
This article is an introduction to building custom kernels from '''kernel.org sources'''.  This method of compiling kernels is the traditional method common to all distributions. If this seems too complicated, see some alternatives at: [[Kernels#Compilation]]
 
+
Alternatively, you can use ABS to build and install your kernel; see: [[Kernels#Compilation]]. Using the existing {{Pkg|linux}} PKGBUILD will automate most of the process and will result in a package. However, some Arch users prefer the traditional way.
+
  
 
== Fetching source ==
 
== Fetching source ==
* Fetch the kernel source from {{ic|ftp.xx.kernel.org/pub/linux/kernel/}}, where xx is your country key (e.g. 'us', 'uk', 'de', ... - Check [http://www.kernel.org/mirrors/] for a complete list of mirrors). If you have no ftp gui, you can use {{ic|wget}}. For this example, we will fetch and compile 3.2.9; you should need to change only the version to get a different kernel.
+
* Fetch the kernel source from http://www.kernel.org. This can be done with GUI or text-based tools that utilize: HTTP, [[Ftp#FTP|FTP]], [[Rsync|RSYNC]], or [[Git]].
For instance:
+
For instance, using wget via http:
  $ wget -c http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.2.9.tar.bz2
+
  $ wget -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.4.tar.xz
 
* It is always a good idea to verify the signature for any downloaded tarball. See [http://kernel.org/signature.html#using-gnupg-to-verify-kernel-signatures kernel.org/signature] for how this works and other details.   
 
* It is always a good idea to verify the signature for any downloaded tarball. See [http://kernel.org/signature.html#using-gnupg-to-verify-kernel-signatures kernel.org/signature] for how this works and other details.   
  
 
* Copy the kernel source to your build directory, e.g.:
 
* Copy the kernel source to your build directory, e.g.:
  $ cp linux-3.2.9.tar.bz2 ~/kernelbuild/
+
  $ cp linux-3.10.4.tar.xz ~/kernelbuild/
  
 
* Unpack it and enter the source directory:
 
* Unpack it and enter the source directory:
 
  $ cd ~/kernelbuild
 
  $ cd ~/kernelbuild
  $ tar -xvjf linux-3.2.9.tar.bz2
+
  $ tar -xvJf linux-3.10.4.tar.xz
  $ cd linux-3.2.9
+
  $ cd linux-3.10.4
 
Prepare for compilation by running the following command:
 
Prepare for compilation by running the following command:
 
  make mrproper
 
  make mrproper
Line 25: Line 23:
  
 
=== What about /usr/src/ ? ===
 
=== What about /usr/src/ ? ===
Using the /usr/src/ directory for kernel compilation as root, along with the creation of the corresponding symlink, is considered poor practice by some kernel hackers. They consider the cleanest method to simply use your home directory. If you subscribe to this point of view, build and configure your kernel as normal user, and install as root, or [[Kernels/Compilation/Arch Build System|with makepkg and pacman]].  
+
Using {{ic|/usr/src/}} for compilation as root, along with the creation of the corresponding symlink, has been the target of much debate.
  
However, this concept has been the target of debate, and other very experienced hackers consider the practice of compiling as root under /usr/src/ to be completely safe, acceptable and even preferable.  
+
* It is considered poor practice by some users. They consider the cleanest method to simply use your home directory for configuring and compiling. Then installing as root.  
  
Use whichever method you feel more comfortable with.
+
* Other experienced users consider the practice of the entire compiling process as root to be completely safe, acceptable, and even preferable.
 +
 
 +
Use whichever method you feel more comfortable with.  The following instructions can be interchangeable for either method.
  
 
== Build configuration ==
 
== Build configuration ==
This is the most crucial step in customizing the kernel to reflect your computer's precise specifications. By setting the configurations in 'menuconfig' properly, your kernel and computer will function most efficiently.
+
This is the most crucial step in customizing the kernel to reflect your computer's precise specifications. By setting the options in {{ic|.config}} properly, your kernel and computer will function most efficiently.
 
+
=== Pre-configuration ===
+
Optional, but strongly recommended for first-timers:
+
*Copy the .config file from the running kernel, if you want to modify default Arch settings.
+
  $ zcat /proc/config.gz > .config
+
*Note the output of currently loaded modules with {{ic|lsmod}}.  This will be specific to each system.
+
  
 
=== Configure your kernel ===
 
=== Configure your kernel ===
Line 44: Line 38:
 
{{Tip| It is possible, to configure a kernel without initramfs on '''''simple configurations'''''.  Ensure that all your modules required for video/input/disks/fs are compiled into the kernel.  As well as support for DEVTMPFS_MOUNT, TMPFS, AUTOFS4_FS at the very least. If in doubt, learn about these options and what they mean ''before'' attempting.}}
 
{{Tip| It is possible, to configure a kernel without initramfs on '''''simple configurations'''''.  Ensure that all your modules required for video/input/disks/fs are compiled into the kernel.  As well as support for DEVTMPFS_MOUNT, TMPFS, AUTOFS4_FS at the very least. If in doubt, learn about these options and what they mean ''before'' attempting.}}
  
There are two main choices:
+
==== First-timers ====
 +
Two options for beginners to ease use or save time:
 +
* Copy the .config file from the running kernel, if you want to modify default Arch settings.
 +
  $ zcat /proc/config.gz > .config
 +
* '''localmodconfig''' Since kernel 2.6.32, this should only select those options which are currently being used.
 +
*# Boot into stock {{ic|-ARCH}} kernel, and plug in all devices that you expect to use on the system.
 +
*# {{ic|cd}} into your source directory and run: {{ic|$ make localmodconfig}}
 +
*# The resulting configuration file will be written to {{ic|.config}}. You can then compile and install as stated below.
 +
 
 
==== Traditional menuconfig ====
 
==== Traditional menuconfig ====
$ make menuconfig       (Will start with a fresh '.config'. Option dependencies are usually automatically selected.)
+
{{ic|$ make menuconfig}}  
  
Make your changes to the kernel and save your config file. It is a good idea to make a backup copy, since you could be doing this multiple times until you get all the options right. If unsure, only change a few options between compilesIf you cannot boot your newly built kernel, see the list of necessary config items [https://www.archlinux.org/news/users-of-unofficial-kernels-must-enable-devtmpfs-support/ here]. Running {{ic|$ lspci -k #}}  from liveCD lists names of kernel modules in use. Most importantly, you must maintain {{ic|cgroup}} support.  This is necessary for [[systemd]].
+
This will start with a fresh {{ic|.config}}, unless one already exists (e.g. copied over)Option dependencies are automatically selected. And new options (i.e. with an older kernel {{ic|.config}}) may or may not be automatically selected.
  
==== localmodconfig ====
+
Make your changes to the kernel and save your config file. It is a good idea to make a backup copy outside the source directory, since you could be doing this multiple times until you get all the options right. If unsure, only change a few options between compiles. If you cannot boot your newly built kernel, see the list of necessary config items [https://www.archlinux.org/news/users-of-unofficial-kernels-must-enable-devtmpfs-support/ here]. Running {{ic|$ lspci -k #}} from liveCD lists names of kernel modules in use. Most importantly, you must maintain CGROUPS support. This is necessary for [[systemd]].
Since kernel 2.6.32, localmodconfig is provided to ease kernel configuration. To use this option:
+
# Boot the distribution kernel, and plug in any devices that you expect to use on the system, which will load the kernel drivers for them. # Go into your kernel source directory, and run {{bc|$make localmodconfig}}
+
# That option will dig through your system and find the kernel configuration for the running kernel (which is usually at /proc/config.gz, but can sometimes be located in the boot partition, depending on the distribution).  
+
# Then, the script will remove all options for kernel modules that are not currently loaded, stripping down the number of drivers that will be built significantly.
+
# The resulting configuration file will be written to the .config file, and then you can build the kernel and install it as normal.
+
  
==== Local version ====
+
==== Versioning ====
 
If you are compiling a kernel using your current config file, do not forget to rename your kernel version, or you may replace your existing one by mistake.
 
If you are compiling a kernel using your current config file, do not forget to rename your kernel version, or you may replace your existing one by mistake.
  
Line 65: Line 62:
  
 
== Compilation and installation ==
 
== Compilation and installation ==
To compile kernel manually, follow these steps:
 
  
 
=== Compile ===
 
=== Compile ===
{{Warning | Do not run {{ic|make all}} if you use GRUB and still have LILO installed; it will configure LILO in the end, and you may no longer be able to boot your machine! Remove LILO (pacman -R lilo) before running {{ic|make all}} if you use GRUB!}}
+
Compilation time will vary from 15 minutes to over an hour. This is largely based on how many options/modules are selected, as well as processor capability. See [[Makepkg#MAKEFLAGS|Makeflags]] for details.
$ make      (Same as make vmlinux && make modules && make bzImage - see 'make help' for more information on this.)
+
 
or
+
{{Warning | If you use GRUB and still have LILO installed; {{ic|make all}} will configure LILO, and may result in an unbootable system.}}
$ make -jN  (N = # of processors + 1) (This utilizes all CPUs at 100%.)
+
Run {{ic| $ make }}.
Compilation time will vary from 15 minutes to over an hour. This is largely based on how many options/modules are selected, as well as processor capability.
+
  
 
=== Install modules ===
 
=== Install modules ===
This needs to be done as root.
 
 
  # make modules_install
 
  # make modules_install
  
This copies the compiled modules into <code>/lib/modules/</code>[kernel version + CONFIG_LOCALVERSION]. This way, modules can be kept separate from those used by other kernels on your machine.
+
This copies the compiled modules into {{ic|/lib/modules/[kernel version + CONFIG_LOCALVERSION]}}. This way, modules can be kept separate from those used by other kernels on your machine.
  
 
=== Copy kernel to /boot directory ===
 
=== Copy kernel to /boot directory ===
Line 84: Line 78:
  
 
=== Make initial RAM disk ===
 
=== Make initial RAM disk ===
The initial RAM disk (initrd option in the GRUB menu, or, the file "initramfs-YourKernelName.img") is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system. The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel. In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system.
+
If you do not know what this is, please see: [[wikipedia:Initrd| Initramfs on Wikipedia]] and [[mkinitcpio]].
 
+
If you need any modules loaded in order to mount the root filesystem, build a ramdisk (most users need this). The -k parameter accepts the kernel version and appended string you set in menuconfig and is used to locate the corresponding modules directory in '/usr/lib/modules':
+
 
+
# mkinitcpio -k FullKernelName -g /boot/initramfs-YourKernelName.img
+
 
+
Alternatively, if that command doesn't render a bootable kernel, attempt the following:
+
 
+
 
  # mkinitcpio -k FullKernelName -c /etc/mkinitcpio.conf -g /boot/initramfs-YourKernelName.img
 
  # mkinitcpio -k FullKernelName -c /etc/mkinitcpio.conf -g /boot/initramfs-YourKernelName.img
  
You are free to name the /boot files anything you want.  However, using the [kernel-major-minor-revision] naming scheme helps to keep order if you: Keep multiple kernels/ Use mkinitcpio often/ Build third-party modules.
+
You are free to name the {{ic|/boot}} files anything you want.  However, using the [kernel-major-minor-revision] naming scheme helps to keep order if you:  
 +
* Keep multiple kernels
 +
* Use mkinitcpio often
 +
* Build third-party modules.
 
{{Tip| If rebuilding images often, it might be helpful to create a separate preset file resulting in the command being something like:<code># mkinitcpio -p custom</code>. See [[mkinitcpio#Image_creation_and_activation| here]]}}
 
{{Tip| If rebuilding images often, it might be helpful to create a separate preset file resulting in the command being something like:<code># mkinitcpio -p custom</code>. See [[mkinitcpio#Image_creation_and_activation| here]]}}
  
Line 100: Line 90:
  
 
=== Copy System.map ===
 
=== Copy System.map ===
The System.map file is not required for booting Linux. It is a type of "phone directory" list of functions in a particular build of a kernel. The System.map contains a list of kernel symbols (i.e function names, variable names etc) and their corresponding addresses. This "symbol-name to address mapping" is used by:
+
The {{ic|System.map}} file is not required for booting Linux. It is a type of "phone directory" list of functions in a particular build of a kernel. The {{ic|System.map}} contains a list of kernel symbols (i.e function names, variable names etc) and their corresponding addresses. This "symbol-name to address mapping" is used by:
  
 
* Some processes like klogd, ksymoops etc
 
* Some processes like klogd, ksymoops etc
Line 108: Line 98:
 
  # cp System.map /boot/System.map-YourKernelName
 
  # cp System.map /boot/System.map-YourKernelName
  
After completing all steps above, you should have the following 3 files and 1 soft symlink in your /boot directory along with any other previously existing files:
+
After completing all steps above, you should have the following 3 files and 1 soft symlink in your {{ic|/boot}} directory along with any other previously existing files:
vmlinuz-YourKernelName         (Kernel)
+
* Kernel: vmlinuz-YourKernelName
initramfs-YourKernelName.img   (Ramdisk)
+
* Initramfs-YourKernelName.img
System.map-YourKernelName       (System Map)
+
* System Map: System.map-YourKernelName
  
 
== Bootloader configuration ==
 
== Bootloader configuration ==
  
Add an entry for your amazing new kernel in your bootloader's configuration file - see [[GRUB]], [[LILO]], [[GRUB2]] or [[Syslinux]] for examples. Note that if you use LILO, the kernel sources include a script to automate the process:
+
Add an entry for your amazing new kernel in your bootloader's configuration file - see [[GRUB]], [[LILO]], [[GRUB2]] or [[Syslinux]] for examples.  
 
+
$ arch/x86/boot/install.sh
+
 
+
If you use LILO, remember to type {{ic|lilo}} as root at the prompt to update it.
+
 
+
If you use Grub 2, you will probably want to add a menuentry to '/etc/grub.d/40_custom'. The simplest way is to copy an existing menuentry found in your '/boot/grub/grub.cfg' file, modify the entry to include the name of the kernel and image you created, and update grub ('grub-mkconfig -o /boot/grub/grub.cfg').
+
 
+
Grub 2 is also able to automatically locate the new kernel and add an entry for it.  You can do this by running:
+
  
# grub-mkconfig -o /boot/grub/grub.cfg
+
{{Tip| Kernel sources include a script to automate the process for LILO: {{ic|$ arch/x86/boot/install.sh}}. Remember to type {{ic|lilo}} as root at the prompt to update it.}}
  
 
== Using the NVIDIA video driver with your custom kernel ==
 
== Using the NVIDIA video driver with your custom kernel ==
 
To use the NVIDIA driver with your new custom kernel, see: [[NVIDIA#Alternate install: custom kernel|How to install nVIDIA driver with custom kernel]]. You can also install nvidia drivers from AUR.
 
To use the NVIDIA driver with your new custom kernel, see: [[NVIDIA#Alternate install: custom kernel|How to install nVIDIA driver with custom kernel]]. You can also install nvidia drivers from AUR.

Revision as of 08:26, 29 July 2013

This article is an introduction to building custom kernels from kernel.org sources. This method of compiling kernels is the traditional method common to all distributions. If this seems too complicated, see some alternatives at: Kernels#Compilation

Fetching source

For instance, using wget via http:

$ wget -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.4.tar.xz
  • It is always a good idea to verify the signature for any downloaded tarball. See kernel.org/signature for how this works and other details.
  • Copy the kernel source to your build directory, e.g.:
$ cp linux-3.10.4.tar.xz ~/kernelbuild/
  • Unpack it and enter the source directory:
$ cd ~/kernelbuild
$ tar -xvJf linux-3.10.4.tar.xz
$ cd linux-3.10.4

Prepare for compilation by running the following command:

make mrproper

This ensures that the kernel tree is absolutely clean. The kernel team recommends that this command be issued prior to each kernel compilation. Do not rely on the source tree being clean after un-tarring.

What about /usr/src/ ?

Using /usr/src/ for compilation as root, along with the creation of the corresponding symlink, has been the target of much debate.

  • It is considered poor practice by some users. They consider the cleanest method to simply use your home directory for configuring and compiling. Then installing as root.
  • Other experienced users consider the practice of the entire compiling process as root to be completely safe, acceptable, and even preferable.

Use whichever method you feel more comfortable with. The following instructions can be interchangeable for either method.

Build configuration

This is the most crucial step in customizing the kernel to reflect your computer's precise specifications. By setting the options in .config properly, your kernel and computer will function most efficiently.

Configure your kernel

Warning: If compiling the radeon driver into the kernel(>3.3.3) for early KMS with a newer video card, you must include the firmware files for your card. Otherwise acceleration will be crippled. See here
Tip: It is possible, to configure a kernel without initramfs on simple configurations. Ensure that all your modules required for video/input/disks/fs are compiled into the kernel. As well as support for DEVTMPFS_MOUNT, TMPFS, AUTOFS4_FS at the very least. If in doubt, learn about these options and what they mean before attempting.

First-timers

Two options for beginners to ease use or save time:

  • Copy the .config file from the running kernel, if you want to modify default Arch settings.
 $ zcat /proc/config.gz > .config
  • localmodconfig Since kernel 2.6.32, this should only select those options which are currently being used.
    1. Boot into stock -ARCH kernel, and plug in all devices that you expect to use on the system.
    2. cd into your source directory and run: $ make localmodconfig
    3. The resulting configuration file will be written to .config. You can then compile and install as stated below.

Traditional menuconfig

$ make menuconfig

This will start with a fresh .config, unless one already exists (e.g. copied over). Option dependencies are automatically selected. And new options (i.e. with an older kernel .config) may or may not be automatically selected.

Make your changes to the kernel and save your config file. It is a good idea to make a backup copy outside the source directory, since you could be doing this multiple times until you get all the options right. If unsure, only change a few options between compiles. If you cannot boot your newly built kernel, see the list of necessary config items here. Running $ lspci -k # from liveCD lists names of kernel modules in use. Most importantly, you must maintain CGROUPS support. This is necessary for systemd.

Versioning

If you are compiling a kernel using your current config file, do not forget to rename your kernel version, or you may replace your existing one by mistake.

$ make menuconfig
General setup  --->
 (-ARCH) Local version - append to kernel release '3.n.n-RCn'

Compilation and installation

Compile

Compilation time will vary from 15 minutes to over an hour. This is largely based on how many options/modules are selected, as well as processor capability. See Makeflags for details.

Warning: If you use GRUB and still have LILO installed; make all will configure LILO, and may result in an unbootable system.

Run $ make .

Install modules

# make modules_install

This copies the compiled modules into /lib/modules/[kernel version + CONFIG_LOCALVERSION]. This way, modules can be kept separate from those used by other kernels on your machine.

Copy kernel to /boot directory

# cp -v arch/x86/boot/bzImage /boot/vmlinuz-YourKernelName

Make initial RAM disk

If you do not know what this is, please see: Initramfs on Wikipedia and mkinitcpio.

# mkinitcpio -k FullKernelName -c /etc/mkinitcpio.conf -g /boot/initramfs-YourKernelName.img

You are free to name the /boot files anything you want. However, using the [kernel-major-minor-revision] naming scheme helps to keep order if you:

  • Keep multiple kernels
  • Use mkinitcpio often
  • Build third-party modules.
Tip: If rebuilding images often, it might be helpful to create a separate preset file resulting in the command being something like:# mkinitcpio -p custom. See here

If you are using LILO and it cannot communicate with the kernel device-mapper driver, you have to run modprobe dm-mod first.

Copy System.map

The System.map file is not required for booting Linux. It is a type of "phone directory" list of functions in a particular build of a kernel. The System.map contains a list of kernel symbols (i.e function names, variable names etc) and their corresponding addresses. This "symbol-name to address mapping" is used by:

  • Some processes like klogd, ksymoops etc
  • By OOPS handler when information has to be dumped to the screen during a kernel crash (i.e info like in which function it has crashed).

Copy System.map to /boot and create symlink

# cp System.map /boot/System.map-YourKernelName

After completing all steps above, you should have the following 3 files and 1 soft symlink in your /boot directory along with any other previously existing files:

  • Kernel: vmlinuz-YourKernelName
  • Initramfs-YourKernelName.img
  • System Map: System.map-YourKernelName

Bootloader configuration

Add an entry for your amazing new kernel in your bootloader's configuration file - see GRUB, LILO, GRUB2 or Syslinux for examples.

Tip: Kernel sources include a script to automate the process for LILO: $ arch/x86/boot/install.sh. Remember to type lilo as root at the prompt to update it.

Using the NVIDIA video driver with your custom kernel

To use the NVIDIA driver with your new custom kernel, see: How to install nVIDIA driver with custom kernel. You can also install nvidia drivers from AUR.