Kernel Compilation without ABS

From ArchWiki
Revision as of 03:00, 23 February 2011 by Fearedbliss (talk | contribs) (1. Manual, Traditional method)
Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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#Using the Arch Build System. 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.

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.32.8; you should need to change only the version to get a different kernel.

For instance:

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

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.

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.

The Traditional Way:

$ make oldconfig         (only if you've copied the old .config file)
$ make menuconfig
Note: oldconfig will run through your config file and prompt you for any new options not previously set. That is for options that would be added to the new kernel you are building.

Kernel version 2.6.32 and higher now provides the more user-friendly localmodconfig script which makes some configuration selections for you, according to the currently running modules. Note: localmodconfig expects lsmod at /sbin/lsmod, thus Arch users must symlink to /bin/lsmod.

As root:

# ln -s /bin/lsmod /sbin/lsmod

As non-root:

$ make localmodconfig

Note: for more information about the new build target "localmodconfig" refer to the 2.6.32 release notes.

Note: There is a companion to localmodconfig called localyesconfig. Running it as a make target will convert all modules in the .config into [*] enabled with the net effect being all modules getting hard-coded into the kernel.

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

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 with makepkg and pacman (covered below).

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.

Use whichever method you feel more comfortable with.

Compilation and installation

Choose one of the following:

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

(Same as make vmlinux && make modules && make bzImage - see make help for more information on this.)

  • If you have a multi-processor computer, you can compile the kernel faster by adding the -j flag.
$ make -j[# of processors + 1]
Note: It's recommended to put one more than how many processors you have. For example: on a dual core machine you could compile the kernel with make -j3.
  • Install modules: (This needs to be done as root.)
# make modules_install

This copies the compiled modules into a directory in /lib/modules named by the kernel version and appended string you set in menuconfig. This way, modules are kept separate from those used by other kernels on your machine.

  • Copy kernel:
# cp -v arch/x86/boot/bzImage /boot/vmlinuz-2.6.32.8-revision1
  • Copy System.map:
# cp -v System.map /boot/System.map-2.6.32.8-revision1

(There seems to be some debate as to whether this step is necessary with modern systems, but since the official Arch kernel packages still include it, it is included here.)

  • If you need any modules loaded in order to mount the root filesystem, build a ramdisk (most users probably want this). The -k parameter accepts the kernel version and appended string you set in menuconfig and is used, in part, to locate the modules in /lib/modules:
# mkinitcpio -k 2.6.32.8-revision1 -g /boot/kernel26-revision1.img


You are, of course, free to change the name of the vmlinuz, System.map and kernel26.img 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.

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

2. With makepkg and pacman (Recommended)

This method has the advantage of creating a package which can be cleanly installed (and removed) and tracked by pacman.

Create following PKGBUILD and kernel26.install files in build directory.

PKGBUILD

Adjust basekernel and pkgver depending on your version.

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

build() {
  LOCAL_VERSION="$(grep "CONFIG_LOCALVERSION=" $startdir/.config | sed 's/.*"\(.*\)"/\1/')"

  cd ..

  # Modify the make line below to make -j[# of processors + 1] if you
  # have a multi-core system. This will enable you to compile the kernel faster.
  make || return 1

  mkdir -p $startdir/pkg/{lib/modules,boot}
  make INSTALL_MOD_PATH=$startdir/pkg modules_install || return 1

  # There's no separation of firmware depending on kernel version - 
  # comment this line if you intend on using the built kernel exclusively,
  # otherwise there'll be file conflicts with the existing kernel
  rm -rf $startdir/pkg/lib/firmware

  install -Dm644 "System.map" "$startdir/pkg/boot/System.map26$LOCAL_VERSION"
  install -Dm644 "arch/x86/boot/bzImage" "$startdir/pkg/boot/vmlinuz26$LOCAL_VERSION"

  # Change the version strings in kernel26.install
  sed -i \
	-e "s/KERNEL_VERSION=.*/KERNEL_VERSION=\"$basekernel\"/" \
	-e "s/LOCAL_VERSION=.*/LOCAL_VERSION=\"$LOCAL_VERSION\"/" \
	$startdir/kernel26.install
}

kernel26.install

KERNEL_VERSION="2.6.32.8"
LOCAL_VERSION="-MINE"

post_install () {
  echo ">>> Updating module dependencies..."
  /sbin/depmod -A -v ${KERNEL_VERSION}${LOCAL_VERSION}
  echo ">>> Creating initial ramdisk..."
  mkinitcpio -k "${KERNEL_VERSION}${LOCAL_VERSION}" -g "/boot/kernel26${LOCAL_VERSION}.img"
}

post_upgrade() {
  echo ">>> Updating module dependencies..."
  /sbin/depmod -A -v ${KERNEL_VERSION}${LOCAL_VERSION}
  echo ">>> Creating initial ramdisk..."
  mkinitcpio -k "${KERNEL_VERSION}${LOCAL_VERSION}" -g "/boot/kernel26${LOCAL_VERSION}.img"
}


You can now build and install the kernel as an Arch package with:

$ makepkg -c -i

(The -c option will clean up leftover work files and directories after a successful build whereas -i specifies that makepkg should immediately invoke pacman to install the resulting package (you can also use just makepkg -ci).)

Bootloader configuration

Add an entry for your amazing new kernel in your bootloader's configuration file - see GRUB or LILO for examples. Note that if you use LILO, the kernel sources include a script to automate the process:

$ arch/i386/boot/install.sh

If you use LILO, 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: Installing the driver for a custom kernel. You can also install nvidia drivers from AUR.