Kernel Compilation From Source

From ArchWiki

Jump to: navigation, search
i18n
English
简体中文

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.

You may alternatively choose to use ABS to build and install your kernel; See: Kernel Compilation with ABS. Some Arch users prefer the traditional way, however using ABS is helpful for automating certain tasks. The choice is yours, neither way is inherently better than the other.


Contents

[edit] Fetching source

  • Fetch the kernel source from ftp.xx.kernel.org/pub/linux/kernel/, where xx is your country key (f.e. 'us', 'uk', 'de', ... - Check [1] for a complete list of mirrors). If you have no ftp gui, you can use wget. For this example, we will fetch and compile 2.6.25; you should need to change only the version to get a different kernel.

For instance:

$ wget -c ftp://ftp.us.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.tar.bz2
  • Copy the kernel source to your build directory, e.g.:
$ cp linux-2.6.25.tar.bz2 ~/kernelbuild/
  • Unpack it and enter the source directory:
$ cd ~/kernelbuild
$ tar -xvjf linux-2.6.25.tar.bz2
$ cd ~/kernelbuild/linux-2.6.25

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.

[edit] Build configuration

  • (Optional) Copy the .config file from the running kernel, if you want to modify default Arch settings.
 $ zcat /proc/config.gz > .config
  • Configure your kernel.
$ make oldconfig         (only if you've copied the old .config file)
$ make menuconfig

You can also use make xconfig (depends on Qt) or make gconfig (depends on GTK), instead of the console-based make menuconfig.

  • Make your changes to the kernel and save your config. It's a good idea to make a backup copy, since you will likely be doing this multiple times until you get all the options right.
  • 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

[edit] What about /usr/src/ ?

Using the /usr/src/ method of kernel compilation, 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 with makepkg and pacman (covered below).

However, this concept has been the target of debate, and other very experienced hackers consider the practice of using /usr/src/ to be completely safe, acceptable and even preferable.

Use whichever method you feel more comfortable with.

[edit] Compilation and installation

Choose one of the following:

[edit] 1. Manual, Traditional method

WARNING: Don't run 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 make all if you use GRUB!

  • Compile it.
$ make clean 
$ make
$ make bzImage
$ make modules
# make modules_install

(if you are compiling as a normal user, do make clean && make first, then do make modules_install as root.)

  • Copy kernel.
# cp -v arch/x86/boot/bzImage /boot/vmlinuz-2.6.25-revision1

(you can also copy the config file and the System.map to /boot, but this is in general no longer required.)

  • (Optional) Build a ramdisk if necessary (most users probably want this)
# mkinitcpio -k 2.6.25-revision1 -g /boot/kernel26-revision1.img

  • Configure /boot/grub/menu.lst accordingly and you are done.
  • Alternatively, if you run LILO, let the install script copy the files and configure LILO:
$ cd ~/kernelbuild/linux-2.6.25/arch/i386/boot/
# sh ./install.sh

You are, of course, free to change the name of the vmlinuz, Kconfig, and System.map files; however, the name-version-revision system is helpful for keeping track which of several kernel compiles you have made. You could also try including a date or time, or stick to a simpler naming system if you wish.

[edit] 2. With makepkg and pacman (Recommended)

This method will create a .pkg.tar.gz package which can be cleanly installed (and removed) with pacman.

Compile kernel with makepkg:

  • Create following PKGBUILD and kernel26.install files in kernel source directory

File: PKGBUILD

vim PKGBUILD

Add the following:

pkgname=kernel26-my
basekernel=2.6.25
pkgver=2.6.25
pkgrel=1
pkgdesc="The Linux Kernel and modules"
arch=('i686')
url="http://www.kernel.org"
depends=('module-init-tools')
provides=(kernel26)
install=kernel26.install

build() {
  # build!
  cd ..
  _kernver="${basekernel}${CONFIG_LOCALVERSION}"
  make || return 1
  mkdir -p $startdir/pkg/{lib/modules,boot}
  make INSTALL_MOD_PATH=$startdir/pkg modules_install || return 1
  cp System.map $startdir/pkg/boot/System.map26-my
  cp arch/x86/boot/bzImage $startdir/pkg/boot/vmlinuz26-my
  install -D -m644 .config $startdir/pkg/boot/kconfig26-my
  # set correct depmod command for install
  sed -i -e "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/g" $startdir/kernel26.install
}

File: kernel26.install

vim kernel26.install

Add the following:

post_install () {
  echo ">>> Updating module dependencies. Please wait ..."
  KERNEL_VERSION=2.6.25
  /sbin/depmod -A -v $KERNEL_VERSION > /dev/null 2>&1
}

post_upgrade() {
  echo ">>> Updating module dependencies. Please wait ..."
  KERNEL_VERSION=2.6.25
  /sbin/depmod -A -v $KERNEL_VERSION > /dev/null 2>&1
}

op=$1
shift

$op $*
  • Invoke makepkg
makepkg -c

(The -c option will clean up leftover work files and directories after a successful build.)

  • install your own kernel with
pacman -U kernel26-my-2.6.25-1-i686.pkg.tar.gz

[edit] Bootloader configuration

  • Configure GRUB or LILO, if not already done.

If you use LILO, remember to type lilo at the prompt to update it.

[edit] 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

[edit] General Kernel Howto

There is a general comprehensive HOWTO on kernel compilation; look here for more information: Kernel-Build-HOWTO

Personal tools