HOWTO Compile a Kernel Manually
From Gentoo Linux Wiki
| Installation • Kernel & Hardware • Networks • Portage • Software • System • X Server • Gaming • Non-x86 • Emulators • Misc |
[edit] Choosing the Sources:
First off, head over to the Kernel Sources page and figure out which kernel you want to install.
The most commonly used sources are the vanilla and gentoo sources:
- vanilla-sources: sources straight from kernel.org (unpatched)
- gentoo-sources: sources patched for bug fixes and features by Gentoo Developers
- mm-sources: sources of the development version, maintained by Andrew Morton. Use if you want to help with kernel testing or really bleeding-edge material.
- ck-sources: contains gentoo standard patchset as well as patches maintained by Con Kolivas for a low-latency desktop or server system. The ck-server use flag tunes for server applications.
[edit] Installing the Kernel Sources
Download and extract the chosen kernel sources with, for example: emerge gentoo-sources
If a specific version is desired, run, for example: emerge =gentoo-sources-2.6.18-r4
If using a tarball, for example 2.6.14.2 directly from kernel.org, unpack it to /usr/src/linux-2.6.14 using the following instructions.
| Code: Unpacking kernel sources |
cd /usr/src mkdir linux-2.6.14.2 cd linux-2.6.14.2 tar xjvf linux-2.6.14.2.tar.bz2 |
[edit] The /usr/src/linux Symlink
Linux uses a neat method for allowing programs that need to compile against the kernel (such as kernel modules, nvidia-drivers for example) to know which version they should compile against. This is the /usr/src/linux symlink.
Gentoo additionally provides an easy tool for moving this symlink around, called kernel-config (It's actually part of eselect, so make sure that package is installed). To see a quick summary of how to use kernel-config, run it with no other options: kernel-config
Now that the new kernel sources are installed, move the symlink to point to them using the following procedure. First list the available kernel sources with: kernel-config list
| Code: Example output of kernel-config |
Available kernel symlink targets: [1] linux-2.6.17-gentoo-r7 [2] linux-2.6.18.1 [3] linux-2.6.18.1-bb2 [4] linux-2.6.18.5 [5] linux-2.6.18-gentoo-r3 [6] linux-2.6.18-gentoo-r4 [7] linux-2.6.18-gentoo-r6 * [8] linux-2.6.18-usermode [9] linux-2.6.18-usermode_guest [10] linux-2.6.18-usermode-r1 [11] linux-2.6.19.1 [12] linux-2.6.19.2 [13] linux-2.6.19-gentoo-r3 [14] linux-2.6.19-gentoo-r3-guest [15] linux-2.6.19-gentoo-r3-skas-v9-pre9 [16] linux-2.6.19-gentoo-r4 [17] linux-2.6.20-rc5 |
the currently selected sources are marked with an ' * ' (for older versions run : readlink /usr/src/linux)
Now set the symlink to the new kernel sources. For example, if the new sources are vanilla-sources 2.6.20-rc5, then run: kernel-config set 17
Alternatively, the sources directory name can also be used, for example: kernel-config set linux-2.6.20-rc5
[edit] Detecting your Hardware
Thanks to whoever wrote HOWTO Detect your Hardware.
First you can update your hardware-id databases by using 'update-pciids' and 'update-usbids'. Running lspci will give you the basics about what is on your system, if you do not know. The output from lspci looks like this:
| Code: lspci |
0000:00:00.0 Host bridge: Intel Corp. 82845G/GL[Brookdale-G]/GE/PE DRAM Controller/Host-Hub Interface (rev 01) 0000:00:01.0 PCI bridge: Intel Corp. 82845G/GL[Brookdale-G]/GE/PE Host-to-AGP Bridge (rev 01) 0000:00:1d.0 USB Controller: Intel Corp. 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #1 (rev 01) 0000:00:1d.1 USB Controller: Intel Corp. 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #2 (rev 01) 0000:00:1d.2 USB Controller: Intel Corp. 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #3 (rev 01) 0000:00:1d.7 USB Controller: Intel Corp. 82801DB/DBM (ICH4/ICH4-M) USB 2.0 EHCI Controller (rev 01) 0000:00:1e.0 PCI bridge: Intel Corp. 82801 PCI Bridge (rev 81) 0000:00:1f.0 ISA bridge: Intel Corp. 82801DB/DBL (ICH4/ICH4-L) LPC Bridge (rev 01) 0000:00:1f.1 IDE interface: Intel Corp. 82801DB/DBL (ICH4/ICH4-L) UltraATA-100 IDE Controller (rev 01) 0000:00:1f.3 SMBus: Intel Corp. 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) SMBus Controller (rev 01) 0000:01:00.0 VGA compatible controller: ATI Technologies Inc RV350 NJ [Radeon 9800 XT] 0000:01:00.1 Display controller: ATI Technologies Inc RV350 NJ [Radeon 9800 XT] (Secondary) 0000:02:08.0 Ethernet controller: Intel Corp. 82801BD PRO/100 VE (LOM) Ethernet Controller (rev 81) 0000:02:0c.0 Multimedia audio controller: Creative Labs SB Audigy (rev 04) 0000:02:0c.1 Input device controller: Creative Labs SB Audigy MIDI/Game port (rev 04) 0000:02:0c.2 Firewire (IEEE 1394): Creative Labs SB Audigy FireWire Port (rev 04) |
You'll particularly need to know the IDE Chipset ([Intel] 8xx series in the example), the Audio Controller (Creative Labs SB Audigy), and the Ethernet Controller (Intel PRO/100 VE). You won't normally need to know your VGA Controller type when compiling the kernel.
Also you may look at lsusb output, if for some reason you don't know what's hooked up to your computer (...):
| Code: lsusb |
Bus 004 Device 001: ID 0000:0000 Bus 003 Device 001: ID 0000:0000 Bus 002 Device 005: ID 043d:007b Lexmark International, Inc. Bus 002 Device 004: ID 043d:007c Lexmark International, Inc. Bus 002 Device 003: ID 043d:007a Lexmark International, Inc. Bus 002 Device 001: ID 0000:0000 Bus 001 Device 001: ID 0000:0000 |
[edit] Configuring the Kernel
[edit] Starting Kernel Configuration
In your kernel source directory at /usr/src/linux, run the following command:
make menuconfig
If you have X support, then you can run this command instead:
make xconfig
You'll see a bunch of text flying by and then you'll see the ncurses kernel configuration screen.
[edit] Using the Kernel Configuration GUI
When you see an '[M]' next to an option it means it will compile as a module. When you see an '[*]' next to an option it means it's compiled into the kernel. The difference is "modules" are not a part of the kernel; they are similar to what Windows users may think of as `drivers`. Modules must be loaded manually. The built-in parts of the kernel are just that; they do not need to be manually loaded.
[edit] Selecting Kernel Functions and Modules
Generally, the menuconfig defaults are suitable for most users. However, there are a few areas that require our special attention.
[edit] Gentoo Required Options
First off, enable Gentoo required options such as processor type and file system.
| Linux Kernel Configuration: Gentoo required option |
Code maturity level options --->
[*] Prompt for development and/or incomplete code/drivers
Processor type and features --->
Subarchitecture Type (PC Compatible) --->
(Change according to your system)
Processor family (Athlon/Duron/K7) --->
File systems --->
Pseudo filesystems --->
[*] /proc file system support
[*] /dev file system support (OBSOLETE)
[*] Automatically mount at boot
To use udev (if you don't know what udev is, you will use it) make sure the following is blank: [ ] /dev file system support (OBSOLETE) In the same subsection: [*] Virtual memory file system support (former shm fs) |
Select one or more of the following filesystems. Your filesystem, the one on which gentoo is now running, MUST BE COMPILED INTO THE KERNEL, NOT as a module. To find out which filesystem you're running, open the file /etc/mtab in your favorite text editor. The line where the second element is / will be your root filesystem, and you can see what type the filesystem is there. For example:
| File: /etc/mtab |
/dev/hda8 / reiserfs rw,noatime 0 0 proc /proc proc rw 0 0 sysfs /sys sysfs rw 0 0 ... |
The above would indicate that the main filesystem is of type ReiserFS.
If you're still unsure of your filesystem, just enable the most common types - this is perfectly safe to do, but will result in a larger kernel:
| Linux Kernel Configuration: File systems |
File systems ---> <*> Reiserfs support <*> Ext3 journalling file system support <*> JFS filesystem support <*> Second extended fs support # this is the ext2 filesystem <*> XFS filesystem support |
[edit] Configuring Chipset
Recall your chipsets you were directed to find earlier. You might not need them, as by default, the menuconfig program automatically enables the most common hardware chipsets. The IDE Chipset Controller Drivers are under:
| Linux Kernel Configuration: IDE Chipset Controller Drivers |
Device Drivers --->
ATA/ATAPI/MFM/RLL support --->
<*> SiS5513 chipset support (example, check your chipset)
|
[edit] Configuring Audio
Next is Audio. You'll find drivers for the Advanced Linux Sound Architecture (the default; OSS is no longer used) at:
| Linux Kernel Configuration: Audio: ALSA (Advanced Linux Sound Architecture) |
Device Drivers --->
Sound --->
Advanced Linux Sound Architecture --->
Generic devices --->
PCI devices ---> # most sound cards are in here
ALSA USB devices ---> # unless your sound card is external, ignore this section
|
You should only enable one of these. Compile your soundcard INTO the kernel, not as a module, so you won't have to worry about module loading. If you have an Audigy 1, 2, or 4 card, for example, select the `emu10k1` driver.
[edit] Configuring Networking
Next is your Ethernet Controller, which connects your computer to your cable or DSL modem. I'm assuming you have a 10/100 Card (almost everyone does). Gigabit cards are relatively common nowadays, find them under Ethernet (1000Mbit), except for the forcedeth driver for nVidia nForce cards, which is under the 10/100 section despite supporting gigabit on nForce4 chipsets. Choose your driver under:
| Linux Kernel Configuration: Ethernet Controller |
Device Drivers --->
Networking support --->
Ethernet (10 or 100Mbit) --->
EISA, VLB, PCI and on board controllers
<*> RealTek RTL-8139 PCI Fast Ethernet Adapter support
|
The Ethernet Driver can be modular or compiled into the kernel, but again, I recommend you compile it into the kernel for reasons of simplicity.
If your network uses PPP (dial-up), you'll have to enable the following:
| Linux Kernel Configuration: PPP (dial-up) |
Network device support ---> <*> PPP (point-to-point protocol) support <*> PPP support for async serial ports <*> PPP support for sync tty ports |
[edit] Configuring ACPI
If you are compiling your kernel for a laptop computer then you'll probably want to include ACPI to help improve battery life. These options can compiled in or built as modules, either way they should function properly.
| Linux Kernel Configuration: Power management options (ACPI, APM) |
<*> Legacy Power Managment API ACPI (Advanced Configuration and Power Interface) Support ---> <*> ACPI Support <*> Sleep States <*> AC Adapter <*> Battery <*> Button <*> Video <*> Fan <*> Processor <*> Thermal Zone Obviously if you have a laptop from any of the following brands you should enable support for them as well <*> ASUS/Medion Laptop Extras < > IBM Laptop Extras < > Toshiba Laptop Extras When finished with that, you'll also want to enable APM support APM (Advanced Power Management) BIOS Support ---> <*> APM (Advanced Power Managment) BIOS Support |
[edit] Configuring Other Hardware
If you have other things to configure, such as wireless, now is also the time to do it.
[edit] Completing Kernel Configuration
Unless you're into selecting advanced options or are following the ATI Driver guide, you are done configuring the kernel. Keep choosing <Exit> until it asks you if you want to save your new kernel configuration. Choose yes, of course!
[edit] Compiling the kernel
Run the following in the kernel source directory: make clean && make && make modules_install
Voila! Your kernel has been compiled and you are ready to install.
[edit] Installing the kernel
Great Job! You're almost done. Now just copy over the kernel image to /boot. This can be done with the provided kernel image installer available in all linux kernel sources:
mount /boot && make install
This will make a backup of the old kernel under the name /boot/vmlinuz.old and save a copy of the new one with the name /boot/vmlinuz. You can keep your bootloader (either GRUB or LILO) pointed at /boot/vmlinuz at all times and provide for a backup, /boot/vmlinuz.old. That way if something goes wrong, you can boot to the old version of the kernel (vmlinuz.old).
[edit] Manual installation
Mount /boot with
mount /boot
Then copy new kernel in to the boot directory
cp /usr/src/linux/arch/i386/boot/bzImage /boot/YourKernelName_with_BuildDate_if_you_wish
Then install the modules in /lib/modules.
make modules_install
Now configure the boot manager. Make sure to match your chosen filename when you specify the kernel in the GRUB or LILO config file.
To be specific, use a line like:
kernel /boot/vmlinuz ...
in /boot/grub/grub.conf if you use GRUB, or
image=/boot/vmlinuz ...
in /etc/lilo.conf if you use LILO. In either case, replace "..." with suitable kernel options. You can find and copy these from a working grub.conf or lilo.conf.
In both GRUB and LILO, you may wish to give yourself a "fallback" option by creating a separate menu entry pointing to /boot/vmlinuz.old. See the documentation for your bootloader for further details.
[edit] Kernel Modules
Modules are a way to dynamically add functionality to a kernel for things you do not use all the time, such as Firewire support. Building a portion of kernel functionality into modules lowers the amount of memory you need for the kernel. However, accessing these functions is usually slower.
Below are some tasks you made need if you are using kernel modules.
[edit] Autoloading modules at boot
If you compiled any kernel drivers as modules, you'll need to add module name to /etc/modules.autoload.d/kernel-2.6, for example:
echo "nvidia" >> /etc/modules.autoload.d/kernel-2.6
The driver name can be found in the menuconfig help file on the particular driver (pressing "SHIFT" + "/" on the driver, a key combo that otherwise produces a question mark).
[edit] Rebuild modules
Modules provided by other packages, such as nvidia or ipw2200, can be rebuilt by installing the module-rebuild package and then running: module-rebuild rebuild
[edit] Forgetting to build a module when you build a kernel
The simplest way of rebuilding a module you have forgotten to build is to enable its building in the kernel configuration, and to follow the steps for kernel installation mentioned above. However, it is possible to build and install a specific kernel module without needing to modify the kernel configuration by following the information in the next section.
[edit] Build a specific kernel module without updating configuration file
It is possible to build a kernel module, without updating the kernel configuration file by using the make tool with the appropriate switches. For example, supposing that the driver for the Yamaha PCI YMF724 soundcard was forgotten during the original build:
Yamaha YMF724/740/744/754 (SND_YMFPCI) [N/m/?] n # Damn we forgot to include this module
We can build the forgotten module as follows:
make SND_YMFPCI=m modules
The missing module can now be installed:
make SND_YMFPCI=m modules_install
[edit] Automating the compilation with a shell script
After you've figured out which steps are necessary to compile your kernel, you can create a shell script that facilitates future compilations:
#!/bin/sh
mount /boot
# This script assumes a correct /usr/src/linux symlink!
cd /usr/src/linux
make menuconfig && \
make && \
make modules_install && \
# all kernel sources provide an image installer
make install && \
umount /boot
# Add required package remerges here:
# emerge alsa-driver
Or this more comfortable one
#!/bin/bash
#
# (c) Copyright by Legorider (legorider@googlemail.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# ( http://www.fsf.org/licenses/gpl.txt )
#
# first Release: 2007-05-29
# latest update: 2008-02-11
#
#set -xv
trythis () {
echo -en '\E[32m';echo $1; tput sgr0
GOOD=0 && $2 && GOOD=1
[ $GOOD -eq 0 ] && error "...failed"
}
error () {
echo -en '\E[31m';echo $1;tput sgr0
exit 1
}
PORTAGEQ="`which portageq 2>/dev/null`"
if [[ -z ${PORTAGEQ} ]]; then
error " FATAL ERROR: Unable to find portageq"
fi
VDBPATH=`${PORTAGEQ} vdb_path`
MOUNTED=`mount |grep -i " /boot "`
[ "$MOUNTED" = "" ] && trythis "Mounting /boot" "mount /boot"
trythis "Changing to the kernel directory" "cd /usr/src/linux"
trythis "Starting the make process" "make"
trythis "Starting the make modules_install process" "make modules_install"
trythis "Starting the make install process" "make install"
trythis "Umounting /boot" "umount /boot"
MERGETHIS=""
echo -en '\E[32m';echo -n "Searching the enviroment :"; tput sgr0
for currpack in `ls $VDBPATH/`
do
for currfile in `ls $VDBPATH/$currpack/`
do
if [ -f "$VDBPATH/$currpack/$currfile/DEPEND" ] ; then
KERN=$(cat $VDBPATH/$currpack/$currfile/DEPEND|grep virtual/linux-sources)
if [ "$KERN" != "" ] ; then
echo -en '\E[32m';echo -n "*"; tput sgr0
MERGETHIS="$MERGETHIS =$currpack/$currfile"
fi
fi
done
done
echo ""
echo -en '\E[32m';echo -n "Found the following packages :"; tput sgr0; echo $MERGETHIS
[ "$MERGETHIS" != "" ] && GOOD=0 && ACCEPT_KEYWORDS="~x86" emerge $1 $MERGETHIS && GOOD=1
[edit] General Update - the quick version
Note: This section assumes previous experience with updating the kernel and is for when just changing settings on an existing kernel. For detailed instructions on upgrading the kernel, please see TIP Upgrading your kernel.
For those who just need to update your kernel, here's how:
- configure your kernel (cd to /usr/src/linux and run as root make menuconfig).
- choose your updated options.
- run the following (note this is just taken from above)
mount /boot make clean && make && make modules_install && #note: manually copying kernel images is not required as #note: all linux kernel sources provide an image installer #note: in this howto we use make install make install #add any modules to /etc/modules.autoload.d/kernel-2.6 #rebuild any modules using module-rebuild rebuild
Now edit the grub or lilo config files as required.
