First Kernal Compilation

From WikiHack

Jump to: navigation, search

Contents

Forward

Compiling a kernel can seem a pretty daunting task for a lot of newer, and even older linux users, but once you've done it once or twice, you'll wonder what you worried about.

I. INTRODUCTION

What Is A Kernel?

The kernel is the heart of your linux system, the computer cannot function without it. Developed as Open Source software, and now at version 2.6, it would cost and estimated $612000000 to redevelop. Whilst I don't intend to go into too much technical detail, you should know that linux uses a monolithic kernel (unlike minix for example, which uses a microkernel), meaning that device drivers and extensions to the kernel are generally run in kernel space (as opposed to user space) giving them full access to hardware. The kernel can have support for various pieces of hardware, either by compiling support directly into the kernel or as separate loadable modules. Most linux distributions will provide a precompiled kernel, with support for most of the hardware the user might have a large amount of modules which it will load automatically depending on the hardware setup, but many users prefer to configure and compile their own kernel.

Why Compile Your Own Kernel?

Whilst many users cope just fine using prebuilt kernels, there are a lot of good reasons for compiling your own. It might be necessary in order to get a specific piece of hardware working, or to make use of certain drivers. You might want to get a better understanding of your system. Precompiled kernels tend to include support for just about everything - one tailored to your needs will be less bulky.

II. THE COMPILATION PROCESS

Checking Your Hardware

If you aren't sure exactly what hardware you are using, there are a few ways to help you find out.

If you are in a linux environment currently using a prebuilt kernel (or you are using a liveCD) 'lsmod' will output the modules that have been loaded. This can help you see which hardware you are believed to have. A cut down example (not from my computer, but similar enough to what I get from a liveCD): Module Size Used by sata_vsc 5284 0 sata_uli 4576 0 sata_sis 4448 0 sata_sx4 9924 0 sata_nv 5988 0 sata_via 5572 0 sata_svw 4868 0 sata_sil 6116 0 sata_promise 7268 0 libata 28036 12 ata_piix,ahci,sata_qstor,sata_vsc,sata_uli,sata_sis,sata_sx4,sata_nv,sata_via,sata_svw,sata_sil,sata_promise sbp2 17160 0 ohci1394 26388 0 ieee1394 59192 2 sbp2,ohci1394 usbhid 26112 0 ehci_hcd 23272 0 usbcore 76892 7 sl811_hcd,ohci_hcd,uhci_hcd,usb_storage,usbhid,ehci_hcd Note all the SATA modules that have been loaded. In reality, I only need to select one particular module when compiling my kernel, so using lsmod is not a sure thing.

Another useful command is 'lspci'. It will output information on PCI buses and devices in your system. This may not come installed by default - use your distro's package manager or google to get hold of it, and you will have to be root to run it. A cut down example from my computer: 00:0e.0 PCI bridge: nVidia Corporation Unknown device 0375 (rev a2) 00:0f.0 PCI bridge: nVidia Corporation Unknown device 0377 (rev a2) 00:18.0 Host bridge: Advanced Micro Devices [AMD] K8 [Athlon64/Opteron] HyperTransport Technology Configuration 00:18.1 Host bridge: Advanced Micro Devices [AMD] K8 [Athlon64/Opteron] Address Map 00:18.2 Host bridge: Advanced Micro Devices [AMD] K8 [Athlon64/Opteron] DRAM Controller 00:18.3 Host bridge: Advanced Micro Devices [AMD] K8 [Athlon64/Opteron] Miscellaneous Control 01:01.0 Ethernet controller: Marvell Technology Group Ltd. 88w8335 [Libertas] 802.11b/g Wireless (rev 03) 01:04.0 FireWire (IEEE 1394): VIA Technologies, Inc. IEEE 1394 Host Controller (rev 80) These outputs can be very useful when selecting options to configure your kernel.

Remember, you can always use either of those commands whilst configuring your kernel from another command prompt, but you might still want to save them so that if you boot your newly compiled kernel you can still check them if you want to make a change. To save them in systemoutput in your current directory, simply use the command:

 #lsmod > systemoutput && lspci >> systemoutput

Some other useful commands you might want to check/save the output of: cat /etc/fstab - file for listing mountable devices, you can find out which file systems you need support for here dmesg - A OT of information about your running kernel and initialized hardware cat /proc/cpuinfo - information about your CPU(s) In fact, browsing though files and directories in /proc/ can give you a lot of information about your system, but it might take a while before you know where to look for what you want.

If you are still unsure about a piece of hardware, don't be a afraid to shut down, open up your pc and have a look at it - almost everything will have a manufacturer name and model number which you can either work with, or google for more information. If you are using a laptop, there is a good chance someone has already been through the compilation process with your exact hardware setup, so again searching the internet could provide a lot of help. This is particularly true of awkward things like the memory card readers on Dell laptops.

Getting A New Kernel

Depending on what distro you are using, you may have a few options here.

First is to download a vanilla-sources kernel from http://kernel.org . Make sure you are downloading the full kernel source, not a patch, but clicking on the letter 'F' next to the latest stable release. If you obtain a kernel this way, extract it to /usr/src/linux (see the tar man pages if you need help with that).

Otherwise, your distro may have another kernel available for you to use - for example Gentoo has an optimised kernel, gentoo-sources, which can be obtained via portage, the gentoo package manager. You may also be able to get a standard vanilla-sources kernel through your distro's package manager. If you obtain a kernel this way, the source should hopefully already be placed in /usr/src/linux.

Note: Unless you have a reason not to, I would recommend using a more recent 2.6 kernel rather than 2.4 or even 2.2 as it is more likely to provide support for more recent hardware.

Configuring Your Kernel Overview

First, if you haven't already, change directory to /usr/src/linux.

 #cd /usr/src/linux

Depending on the environment you are operating in, you have a few options. If you are using kde,

 #make Kconfig

should give you a gui to configure your kernel from.

Using any other desktop environment

 #make xconfig

should do likewise.

Otherwise without a GUI you can use

 #make menuconfig

which should work for everyone.

You should now be looking at a main menu. Selecting options from this with enter will bring up a list of checkboxes, or a further menu. If you are using kconfig or xconfig the commands should be represented by buttons on the toolbar at the top of the screen. Here is an overview of what they do, along with the key shortcuts for menuconfig users: Select (Enter) - navigate through the menus and select from lists. Back (Esc) - will move you back to the previous menu, as will selecting exit at the bottom of the screen. Search (/ key) - allows you to search for a strings Help (? key) - displays information on an option. Generally this help is pretty useful, covering topics like which hardware is likely to need an option selected, whether selecting an option will be safe or is likely to break your system if you didn't need it, and where to find more information on the option. Yes [*] (y key) - select an option to be compiled into the kernel, meaning support for it will always be present. Module [M] (m key) - select an option to be compiled as a module which can either be loaded at startup or when the system is running. More on modules at the endof the guide. No [ ] (n key) - support for this option will not be compiled at all. (Note that the space key can be used to cycle between these states.) You should also find you have the option to save or load a .config file with you current selections.

Making Selections

Now it is time to start configuring your kernel. If this is your first time, I would recommend trying to get your most important hardware working, and leave out things like printers, joypads and sound until you have a basic working system. You can recompile the kernel with support for them later - if you save your current configuration you wont have to select your important hardware again.

Whilst the layout changes between different kernel versions so I can't give exact advice on this, there are some important things you will need support for.

  • Any driver that will be needed at boot time by the computer (such as hard drives) will need to be compiled into the kernel, not as a module.
  • Ensure you have support for testing, development or incomplete drivers if you see an option with a name similar to this. Many important drivers will be missing if you do not.
  • If you see an option to support hot-pluggable devices, it is important to say yes.
  • Compile support for the filesystem your root (and boot if it is separate) partitions are on into the kernel. You can't load a module from a harddrive that hasn't been mounted and you can't mount a harddrive unless it's file system is supported, if you see what I mean.

Generally, I go through each category, taking my time and reading the help file for any driver I'm not sure if I need. Take care that the options selected by defaults aren't necessarily good ones, for example when I compiled my kernel yesterday support for something that explicitly said "DO NOT USE THIS IT WILL BREAK YOUR COMPUTER" was selected already. Also, whilst there are a lot of options that sound awesome and are really tempting to select, think whether you actually *have* hot swappable CPUs or files bigger than 2TB before you select them.

When you think you're all done, save the .config file, and save a backup somewhere safe (I think Ive said that a lot, so maybe you'll actually do it) and then exit.

Now the boring part - compilation. Assuming you are using a 2.6 kernel:

 #make && make modules_install

then sit and wait. Depending on your processor, and how much you selected, this could take a while.

Dealing With Modules

Many distros have different ways of dealing with modules you want loaded at startup. Most systems involve listing them in a file, but you can find out from reading documentation from your specific distribution.

The modules you have compiled can be found in /lib/modules/{your kernel version} with the extension .o or .ko, so you can find the all using a command like

 #find /lib/modules/{your kernel version}/ -type f -iname '*.o' -or -iname '*.ko'

I don't want to go into too much detail on dealing with modules here, but if you simply want to load a module at some point you can use

 # modprobe {module name}

and unload it using

 # modprobe -r {module name}

or

 # rmmod {module name}

Check out the man pages of those command for more information, modprobe can do quite a lot.

Copying The Kernel Manually

You have two options for copying your kernel over to /boot, either doing it manually or using a simple command. In general I would recommend doing it manually even though it requires a little more work, as you can ensure nothing is overwritten and keep track of which version you are using. Simply copy the compiled kernel over to /boot, giving it a version name.

  1. cp arch/{your architecture, ie i386}/boot/bzImage /boot/{kernel version number}

You will then need to adjust grub or lilo to boot this kernel. Don't replace your previous entry, add a new one instead. That way you can boot the older one and make repairs if you cannot boot your new kernel (which, to be honest, is fairly likely on your first attempt).

You are now ready to reboot - cross your fingers - and see troubleshooting later in the guide if you need more help.

Copying The Kernel Using 'make install'

When you are used to compiling your own kernel and only need to do it occasionally this is a good method to save time and hassle. Basically you enter the command 'make install' after compiling the kernel, and the older one will be backed up, and the newer kernel copied over to /boot. Whilst this might sound ideal, if you then work from your new kernel to make minor tweaks, if you use make install again you will lose your original kernel. If you are only making minor changes though, there is less chance of messing up twice in a row and if you do, there will be fewer things to change back. I guess you could use this option and make your own backups, but as far as I'm concerned that kind of defies the point.

The other advantage of this method is in your bootloader. Since the compiled kernel and older backup are always given the same names, you simply need two entries, one pointing at each, and you are unlikely to need to modify your bootloader configuration again.

III. OTHER TOPICS

Troubleshooting

If you restart your computer and everything fails, dont worry.

Grub:

  • If you press 'e' on an entry in grub you can edit it, for example if you have mistyped the kernel version.
  • If you neglected to make an entry for your old kernel, and booting your new one fails, you can edit the entry to boot your previous kernel instead so you can try and fix what went wrong.

Kernel configuration:

  • Boot an older kernel or chroot into your environment from a liveCD, cd to /usr/src/linux, make menu/k/xconfig and if necessary load the .config file I told you to save. Don't overwrite the backup copy you saved somewhere safe, you might want to revert to it if you mess up some more.
  • If you got an error so you know where the problem lies, compile support in or remove support for whatever was wrong.
  • If everything just failed, check you have the right drivers for your harddrive and any other critical components, and ensure they are built into the kernel.
  • Check you have the filesystems of anything you want mounted at boot (ESPECIALLY your root + boot partitions) compiled into the kernel.
  • Make sure no drivers, either compiled into the kernel, as modules or that you installed after conflict with each other.
  • Recompile and copy the new kernel (with a different version number)

Drivers - If a driver you compiled and installed separately (eg proprietary graphics card drivers) no longer exists, works or loads, remove and reinstall it. You will most likely have to do this every time you compile a new kernel.

Other Errors:

  • Don't expect to get every piece of hardware working yourself first time.
  • Check linux forums and mailing lists for further help - you may need to select something unexpected or even use a different kernel version.

Modules vs Built In Support

The first time I compiled a kernel I was pretty unsure what I wanted compiled into the kernel and what I wanted compiled as a module. Here is some information to hopefully help you decide. 1. Apparently, you can't compile support for every single thing there is an option for into the kernel. It will get too large and just wont boot. But then, I really doubt anyone could need support for anything approaching all the stuff on offer... 2. Loading modules at boot time is slow, but compiling them into the kernel will make it larger. Generally, if you need support for something often enough that you want to automatically load it at boot, you'd be better compiling support for it into your kernel. 3. Compiling modules for things you only use occasionally will lower the size of your kernel and cut down on boot time. For example, I only occasionally need the various encryption algorithms, so I compiled them as modules which I load when I need them.

Using A Script

Once you have done it a few times, you may wish to automate the compile process with a shell script. This uses make install, so ensure you have grub set up for that. Whilst you *could* write something longer and more complicated which would manually copy the kernel over and modify grub.conf/menu.lst it seems unnecessary, if you are worried about losing a working kernel version manually make a backup first.

 #!/bin/sh

If /boot is on a separate partition and not already mounted

 mount /boot

This script assumes a correct /usr/src/linux symlink!

 cd /usr/src/linux

You may change menuconfig to kconfig or xconfig if running thins from console, gnome-terminal etc

 make menuconfig &&
 make &&
 make modules_install &&

All kernel sources provide an image installer

 make install &&

If you mounted boot and want it unmounted again

 umount /boot

Some drivers must be reinstalled for a new kernel. Add commands here to install them. An example using the gentoo package manager:

 emerge nvidia

Other Resources

Here are a few websites you might find useful:

linuxgazette.net/issue37/holmwood.html

  • Using modules with linux.

http://www.bitbenderforums.com/vb22/showthread.php?postid=296104#post296104

  • Guide to compiling a custom linux kernel. Very in depth, with lots of help if you are struggling to find information about your hardware.

http://gentoo-wiki.com/HOWTO_Compile_a_Kernel_Manually

  • Gentoo specific but fairly brief if you need a reference.

http://www.linuxquestions.org/

  • many guides to getting your hardware working and a great community - search if you have a question

http://kernel.org

  • download kernels, amongst many other things, from here.

Final Word

Thanks for reading - hopefully compiling your kernel will go well. If you have any questions, comments or advice on the guide, it would be greatly appretiated so don't hesitate to leave a comment or message me - particularly if I've made a mistake. If you have a problem with kernel compilation or are unsure about something post on H2H and I'm sure myself or another user will be able to help you.

By: Dream_is_Dead

Personal tools