Compiling and installing a new kernel

From MEPIS Documentation Wiki

Jump to: navigation, search

Note: There is also an informative writeup on this subject, which is Debian-based, at: "Debian-kernel-compile-howto" You can also follow the Ubuntu Kernel Build and Installation Guide

Another note: there is also a package called kernelcheck available from the Community repository that is designed to automatically build any 2.6 kernel from the upstream source.


Contents

Preconfiguration

Since it's not advisable to compile the kernel as root, you should extract the kernel source to a folder in your /home as a normal user, and then all the commands (except the installation commands) should be run as normal user.

Programs needed

kernel-package - this program is needed for creating kernel deb packages.
build-essential - this program is needed for compiling source code into machine binaries (programs).
fakeroot - this program removes the need to become root for building a package
libncurses5-dev - this program is needed for using make menuconfig or make xconfig.
libqt3-mt-dev libqt3-headers - these programs are needed for running make xconfig with QT 3.
libqt4-dev - these files are needed for running make xconfig with QT 4. The xconfig GUI can be built either with QT 3 or QT 4 (use QT 4 in MEPIS 12)
linux-source-? - this is the kernel source package you need to download in order to build the kernel.
module-assistant - this program is needed to build the missing modules after kernel compilation

Install the required programs

  1. You can apt-get install all the programs above the way they are written, or get them through kpackage. If using a terminal you need to type apt-get update first, then apt-get install programname
apt-get update
apt-get install build-essential fakeroot kernel-package libncurses5-dev libqt3-mt-dev module-assistant

or suggested for MEPIS 11 or 12:

apt-get install build-essential fakeroot kernel-package libncurses5-dev libqt4-dev module-assistant

(module-assistant seems to be of less and less use, being replaced by DKMS packages)

Download kernel source

  1. You can download the kernel source package with apt-get or Synaptic. For example, if you want the 2.6.27 kernel you would write "apt-get install linux-source-2.6.27" without the quotes. Once installed you will find the source file linux-source-(ver).tar.bz2 in /usr/src folder.
  2. If you want a version of a kernel that is not in the Debian or MEPIS repositories you can get your kernel from here: ftp://ftp.kernel.org/pub/linux/kernel/ and choose the version by clicking on one of the folders whose names start with a "v". Note that the filenames in these directory listings are sorted according to the ASCII collating sequence; 2.6.10 comes between 2.6.1 and 2.6.2, not after 2.6.9.
  3. The very latest kernels may require updated versions of kernel-package from upstream Debian to build correctly; usually they can be installed directly from the Sid deb package from packages.debian.org.
  4. Another easy way to get the kernel sources is to use git. Install git-core:
apt-get install git-core

And then run:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

This command will create a linux-2.6 folder with the latest kernel source files in the current directory. If you use this method you'll need to skip the next (extraction) step. If you got the sources using git, you need to run this command in linux-2.6 folder:

sed -rie 's/echo "\+"/#echo "\+"/' scripts/setlocalversion

Extract the Kernel file

  • If you download and install the kernel source package with synaptic, kpackage or apt-get, the linux-source-(ver).tar.gz or tar.bz2 file will be put into your /usr/src folder. It is recommended that you create a new kernel-build folder in your /home, copy the source tarball into that folder, then extract, patch, and build the kernel there as a normal user. You will need to extract that file by right clicking on it and choosing "extract here". You do this using the File Manager that you will find in Kmenu > System > Filesystem menu. Or you can do it manually in a terminal window using tar.

Make sure the file name you write matches what the real file name is!

  • If you download the kernel from the kernel.org ftp site, just extract it in your home per above, and proceed.

Apply kernel patches

1. You would download the patches you need into a folder of your choice.

2. Enter the extracted kernel source directory, and open up a terminal there. It's recommended to put the source somewhere in your /home folder, instead of in /usr/src.

3. Type patch -p1 < /path/to/kernel/patch (path to kernel patch is the full path to where you downloaded the patch to, /home/username/Downloads/foo.patch for an example, (foo.patch is what you would replace with the correct name of the patch). You would run the patch command for all patches you want to add to the kernel.

Configuring the Kernel

  1. Enter the newly extracted source folder, open a terminal, and enter make oldconfig. Questions may come up during this and all you really need to do is press your "enter" key after each question to choose the default answer. You don't need to type in an answer. If you do enter an answer and you chose a wrong one it could make your kernel unusable.
  2. After the make oldconfig is finished you will get the command prompt back and you can then choose to change some of your kernel's configuration to remove unnecessary options or to add options that you do want. If you are upgrading the kernel, realize that the layout of kernel options often changes, and you may need to reselect something that was previously selected under a different menu before.
  3. To configure a stripped-down kernel with only support for your current hardware, use make localmodconfig This will configure the kernel based on what modules are currently loaded, plus what CPU features are detected. You should double-check the results in the GUI configurations below, and add any external USB device drivers may be necessary. It may also add virtualization features, based on your CPU, that you may want to disable.
  4. If you want to configure your kernel some more before building it, you can type make menuconfig and then go through all the kernel options to add or remove things.
  5. You can use make xconfig instead of make menuconfig. xconfig is a better-looking and easier-to-use kernel configuration tool, and you can load and save various config files.
  6. Configure the kernel to your liking but make sure that you don't remove something you need. Change the processor to your specific model. Other performance suggestions can be found here. The more fluff you remove, the faster the kernel will be. If you know you have specific hardware, compile it into the kernel instead of a module. If you are unsure leave it as a module. Modules are inserted when needed. For information on what hardware you have, the output of lspci is helpful and installing the app lshw and running as root is also very informative. Practice makes perfect, and the entire process takes much less time with experience.
  7. Sound: The SimplyMEPIS kernel (as of 2.6.32) includes the ALSA sound modules internally, but external "alsa-source" packages are also available in the standard pools. Using alsa-source with module assistant will build alsa-modules that override the kernel's version, once installed. In the kernel configuration section you need to enable the ALSA sound drivers by going to Device Drivers->Sound->Advanced Linux Sound Architecture. You may then simply select the driver of your sound card, or select the whole lot if you are unsure.

Compiling and Building the Kernel

If necessary, clean the source folder:

make-kpkg clean

Build your kernel by typing:

CONCURRENCY_LEVEL=`getconf _NPROCESSORS_ONLN` fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers

Add the -uc -us flags to also build the kernel source deb.

"custom" can be anything, though it must be in lower case and not have any odd characters (the alphabet and the numerals 0-9 are allowed only). The kernel will take a while to build so take a break while you wait for it. It can take an hour, more or less, depending on how big the kernel you compiled is and how fast your computer is, how many drivers you are building, and how many threads of execution and cores your CPU supports. Note that you can customize the number of threads, i.e:

CONCURRENCY_LEVEL=2

will only use two threads, even on a four core machine. The default command uses all cores available. This will generate a lot of heat, so make sure your machine is well ventilated.

Installing the Kernel and the Extra Kernel Deb Files

When you get the command prompt back you can use a file manager to look the next level up from the extracted source directory and see if you have up to four packages with the .deb extension. The kernel-image and kernel-headers packages should be installed, the source and doc packages are optional and usually not needed. The easiest way to install these debs is to type in the terminal as root in the folder with the debs:

dpkg -i *.deb 

This will install all the debs at one time. Gebi will also do the job in Mepis 8.0 and 8.5.

Mepis currently uses the initrd boot method. If you get a failure to build that file, you can create it manually with (as root)

update-initramfs -c -t -k <kernel-version>

<kernel-version> should be of the "2.6.32-custom-my-kernel" format, as an example.

After installing the kernel and rebooting to it, check in /lib/modules/(kernel_version) that you have the symlinks build and source linked to /usr/src/linux-headers-(kernel_version). If not, the following commands in a root terminal will fix it:

cd /lib/modules/$(uname -r)
rm build
rm source
ln -s /usr/src/linux-headers-$(uname -r) build
ln -s /usr/src/linux-headers-$(uname -r) source

If not running the new kernel, replace the string $(uname-r) with the actual kernel version, such as

ln -s /usr/src/linux-headers-2.6.33-custom build

Edit Grub Boot Menu

When you install the kernel package the entry should be created automatically in GRUB. If for some reason it's not there, add it by following these steps:

  1. When you get the command prompt back you will be finished with the compiling and installing part of your new kernel. Now you need to edit your /boot/grub/menu.lst file to add the new kernel as a boot option in the boot menu. You can do this by keeping the terminal window open and staying logged in as root, then typing kwrite /boot/grub/menu.lst and it will bring up the kwrite text editor with that file in it (if you get an xhost error, just type exit and become user, then type xhost +local:0 and then type su to become root. That will allow you to use kwrite as root). You can also edit in terminal by typing this command: nano -w /boot/grub/menu.lst
  2. After you get the file open you need to copy all of the previous kernel's lines and use them for the new kernel too. You should not delete the other kernels (you can do that later if the new one works great and you only want that new one). After you have copied and pasted the kernel boot lines then you need to change the kernel version in the line that has vmlinuz.

Example of addition to Grub menu file (/boot/grub/menu.lst):

title MEPIS at sdb1, kernel 2.6.newversion
root (hd0,1)
kernel /boot/vmlinuz-2.6.newversion root=/dev/sda2 nomce quiet vga=791
initrd /boot/initrd-img-2.6.newversion
boot

The new kernel's entry should be exactly like the old one's except for the title line and the kernel line where it has "vmlinux-", the newversion should be the actual version of the compiled kernel.

Save the file and then close the text editor. Reboot your computer and choose the new kernel from the boot menu.

Press F2 to dismiss the splash screen and watch for errors. Also check /var/log/messages for errors.

Installing Missing Modules

Often after you have compiled the kernel there will be a few missing modules. After rebooting using the new kernel module-assistant needs to be initialized using

m-a update

wireless

To be able to use windows wireless drivers under Linux, you the ndiswrapper module installed. Simply run

m-a a-i ndiswrapper-source
  • Note: You may have to install the ndiswrapper-source package from Debian Sid or Experimental to build with the latest kernels.

Recompiling A Kernel

Same as above really. All you would need to do if you want to compile the same kernel again is to uninstall the kernel using kpackage.

You open kpackage, click on the Installed tab and on the Search Icon on the far left side menu, then type kernel. You look through the kernels you have installed and click on the circle to the right of the kernel you want to uninstall. Then click the uninstall button at the bottom of the screen.

Make sure you click the box for purging files before clicking the final uninstall button you will see. Then follow the guide above to build a newer kernel.

You uninstall a kernel before you compile, build and install again because if you don't you will run into problems because of the previous and the new kernels having the same name. Uninstalling fixes that for you.

Explanation of Terms

cd = change directory
mv = move
ln -s = make a symbolic link
su = become root (will ask for root password)
tar xvzf = extract term used for extracting tar.gz files
tar xvjf = extract term used for extracting tar.bz2 files
/path/to = write out your full path to a file and include the filename
dpkg -i = is the command used to install a deb package
terminal = Konsole or Terminal program that you can find in KMenu > System menu
KMenu = The icon on your desktop that looks like a big K
make kpkg = command to make the kernel into a deb package
make oldconfig = command to set up your kernel to use the options from the kernel you are currently booted into.
make menuconfig = command to open a configuration menu for your kernel so that you can change options in the kernel.
make xconfig = same as the make menuconfig command but the menu is better looking and easier to read.
Kpackage = Package Manager found in Kmenu > System menu.
apt-get update = command used to update your apt sources so that you will then be able to use apt-get install command or apt-get upgrade command.
apt-get install = command used to install a package from the terminal. You would follow the command with the package name. Ex. apt-get install kernel-package
xhost +local:0 = command you need to type as user in the terminal in order to give root permission to use run GUI applications as root.

Optional Terms - If needed

rm = command to remove a file or a symbolic link
rmdir = command to remove a directory or folder
cp = command used to copy a file or folder from one place to another
gzip -d = command to unzip a file with the gz extension
bz2 -d = command to unzip a file with the bz2 extension
tar xvf = command to unzip a file with the tar extension, used after a gzip -d or bz2 -d command. Note that tar does not allow anything but a filename to follow the f switch; all other switches must come before the f.

More Information

You can always check the latest notes on compiling the kernel using the Debian tools at /usr/share/doc/linux-source-version_number/debian.README.gz test edit

Personal tools
In other languages