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"


Contents

Programs needed

kernel-package - this program is needed for creating kernel deb packages.
build-essential - this program is needed for building Debian packages.
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.
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 kernel-package libncurses5-dev libqt3-mt-dev module-assistant
  1. The kernel source package you can download with apt-get or kpackage. The source file name is linux-(ver).tar.bz2 with the version number in place of the (ver). For example, if you want the 2.6.15 kernel you would write it as "linux-2.6.15.tar.bz2" without the quotes.
  2. If you want a version of a kernel that is not in the debian repositories you can get your kernel from here: ftp://ftp.kernel.org/pub/linux/kernel/ and click 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.

Extract the Kernel file

  • If you download and install the kernel source package with synaptic, kpackage or apt-get, the linux-(ver).tar.gz or tar.bz2 file will be put into your /usr/src folder and you will need to extract that file by right clicking on it and choosing "extract here". You do this using the File Manager Super User Mode that you will find in Kmenu > System > Filesystem menu. Or you can do it manually in a terminal window as root 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 then you will need to move the file into your /usr/src directory manually after you download it. You can do that with the File Manager Super User Mode program, or you can do it in a terminal if you type su to become root, then type
mv /path/to/file/linux-(ver).tar.? /usr/src/ 

(the /path/to/file part will need to be your real path to the kernel file). You would then extract it using one of the methods given above. The (ver) and ? marks would need to be replaced with version numbers and file extensions.

Making a Build directory - With Symbolic Link

Once you have the kernel source downloaded and extracted into your /usr/src directory then in a terminal type su to become root. Type:

cd /usr/src 

and then:

ln -s linux-(ver) linux 

Replace (ver) with the actual version of the downloaded kernel (the l in ln is the letter l and ln stands for link and the -s stands for symbolic. The linux word at the end is the name of the new link). Note that "linux-(ver)" is a directory name, not a file name.

Apply kernel patches

If you want to apply kernel patches follow these instructions.

Configuring the Kernel

  1. Type cd linux in the terminal and then type 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. 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.
  4. You can use make xconfig instead of make menuconfig. xconfig is a better-looking and easier-to-use kernel configuration tool. If you get xhost errors when you try to run the command you should first type exit in the terminal to become the user again, then type xhost +local:0 (that's a zero at the end) and then hit enter. Then type su to become root again. You will then be able to type make xconfig and see the menu.
  5. 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.
  6. Sound: The SimplyMepis kernel (as of 6.5) includes the ALSA sound modules separately and sources for those are not available in the standard pools. 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

Build your kernel by typing:

make-kpkg buildpackage kernel_image

Or, if you need initrd (you might need initrd in case you intend to boot the kernel from other devices than a hard disk, for example for booting from USB or CD):

make-kpkg buildpackage kernel_image --initrd

However, if you recompile the same version of your kernel use these commands:

make-kpkg clean
make-kpkg buildpackage -append_to_version -custom kernel_image 

"-custom" can be anything, but it needs to begin with a - (minus sign). You may have to issue the above command twice.

The kernel will take a while to build so take a break while you wait for it. It can take about an hour, more or less, depending on how big the kernel you compiled is and how fast your computer is.

Installing the Kernel and the Extra Kernel Deb Files

When you get the command prompt back you can use a file manager to look inside your /usr/src directory and see if you have four packages with the .deb extension. The kernel-image, linux-source, kernel-headers and kernel-doc debs should all be installed. You really only need the kernel-image to install the new kernel to your system, but you may need the source and headers to compile other things later. So you should install all the debs. The easiest way to install these debs is to type in the terminal:

cd /usr/src 
dpkg -i *.deb 

This will install all the debs at one time.


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 would 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 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
boot

title MEPIS at sdb1, kernel 2.6.15-27-368
root (hd0,1)
kernel /boot/vmlinuz-2.6.15-27-368 root=/dev/sda2 nomce quiet vga=791
boot

As you can see above, the new kernel's entry is exactly like the old one's (2.6.15-27-386) 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

ntfs-3g

To be able to read and write ntfs formated drives you need to have the fuse module installed. To install the neccessary modules from their sources, run

m-a a-i fuse-source 

wireless

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

m-a a-i ndiswrapper-source

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 the make xconfig kernel configuration menu if you are running KDE 3.3.0, 3.3.1 or 3.3.2 versions. Not needed if you are using the 3.2 version of KDE. You can find out what version of kde you have by clicking on Kmenu > System > Settings > Control Center

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 alway check the latest notes on compiling the kernel using the debian tools at /usr/share/doc/linux-source-2.6.15/debian.README.gz

Personal tools
In other languages