Rebuilding the Kernel for Non-Experts
Total Page:16
File Type:pdf, Size:1020Kb
KNOW-HOW Kernel Tips Rebuilding the kernel for non-experts Worried about a recent security exploit? Want to take advantage of a new Suspend2 [1]: this patch, which is really useful for laptop users, gets Suspend wor- hardware feature? You don’t need to be a Linux expert to patch and com- king on systems where the plain vanilla kernel fails. pile the Linux kernel. We'll show you how to get started. Keeping track of kernel development is not a trivial task because the changes BY PETER KREUSSEL between versions are just too numerous. The changelog on Kernelnewbies.org [2] he kernel allocates memory to program. To do so, you need the kernel gives you an overview. If you are re- applications, controls access to sources and the GNU C Compiler. building the kernel to address a hard- Tthe filesystem, and performs ware problem, you are more likely to many other important tasks, but for Time for Something New find notes about the problem in the con- most users, the kernel is always hiding Distribution-specific kernels normally text of the hardware device you are try- behind a shell or a GUI. Ordinary users work without causing much trouble, but ing to fix. If a forum tells you that your usually don’t need to tinker with the in some situations, you may find your- DVB card will work with kernel 2.6.13, kernel. self needing to build a new kernel. For but you have 2.6.12 on your system, it The Linux kernel, however, is actually instance, if you have security-critical ap- might be time to fire up your compiler. quite visible – it resides in the /boot/vm- plications, you may want to rebuild the Building a new kernel is not a risky linuz-kernelversion file. Depending on kernel if vulnerabilities are disclosed. process. The bootloader will let you your distribution, the kernel can occupy And in some cases new kernel versions choose between different kernels. After 1 or 2MB on disk. But this single file is have improved support for new hard- installing a new kernel, there is nothing only part of the picture. The files below ware. to stop you booting the older version. /lib/modules/[kernelversion] are just as You may also have occasion to work Figure 1 shows the steps for getting a do- much a part of the kernel. This directory with kernel patches. Patches give you it-yourself kernel build running on your weighs in at no less than 70MB on Suse the ability to add new functions to the machine. 10.0, but not everything in this directory kernel. One example of this is Software- requires memory space. In fact, the mod- Kernel Sources ules in this directory are just loaded on Table 1: Kernel Functions Your distribution might have a package demand. Resource management (memory, CPU with updated kernel sources, but the lat- More or less every distribution comes cycles etc.) est available kernel will always be avail- with ready made binaries for the kernel Filesystem access able at Kernel.org. As the site is often hit and modules, however, it is possible to Network access by capacity problems, make sure you compile the kernel just like any normal Access to hardware components use a mirror [3]. Download the kernel 58 ISSUE 69 AUGUST 2006 WWW.LINUX - MAGAZINE.COM Kernel Tips KNOW-HOW Table 2: Kernel Configuration Main Menu this file to the kernel directory, run gun- zip config.gz to unpack the file, and re- Menu entry Function Default setting name it .config. Code maturity level options Allow selection of experimental Enabled Run all make commands as root and drivers from within the /usr/src/linux-kernelver- Processor Type and features Processor architecture settings Normally, only the Processor sion directory. Start by removing the family will need modifying. traces of the development process from Power management options Useful for laptop owners Can be enabled without any danger the kernel tree you just unpacked by en- Networking For experts only (e.g. Bluetooth Keep the defaults tering make mrproper. Then copy the and infrared support) configuration file to the kernel tree by Device Drivers Configure drivers for hardware Modify as required entering cp /boot/config-kernelversion devices /usr/src/linux-kernelversion/.config, File systems Support for filesystem types May need modifications if you where kernelversion is the version of the are doing without an initial Linux kernel you are building. ramdisk The kernel includes a Qt-based, a Gtk- based, and a console-based configura- with the highest version number, and port for a specific filesystem or a specific tion front-end. These tools help you se- unpack the archive in /usr/src. sound card) can be enabled or disabled lect the functions your do-it-yourself ker- You should be aware that the “plain when you build the kernel. The more nel build will provide. The configuration vanilla” kernel could differ considerably functions you enable, the bigger the ker- menu, which has the same entries no from your distribution kernel. Distribu- nel will become – at least in theory. The matter which front-end you choose, con- tors typically add a large number of compilation time will be longer if you tains several hundred settings – a daunt- patches that add functionality, improve use the kernel’s on-demand load func- ing prospect at first glance. driver support, and resolve known bugs. tion. You just copied the configuration files, The question as to whether the plain Most kernel features can be added to and you will have a pre-configured ker- vanilla kernel from Kernel.org or the dis- the kernel as modules or built in. Only nel after running the configuration tool tribution-specific version provides better the built-in components will be resident by entering make menuconfig (console underpinnings for a stable system is in memory as Linux loads kernel mod- version), make gconfig (Gtk version), or mainly a question of faith. Neither is ules on demand. This is why it does not make xconfig (Qt version) (Figure 2). likely to cause major issues. This said, if make much difference in practical terms The console version requires ncurses- you want the most recent version of the if you enable kernel functions you may devel and is generally regarded as the kernel, you may have no alternative to not need (Figure 2). most reliable tool. the official version. If you copy a .config file from an older This article works with the official re- Preparation kernel version to your source tree, make lease by the kernel developer team. An As Linux distributions are designed to sure you run Menuconfig or another alternative is the so-called mm tree, run on almost any which contains contemporary enhance- kind of hardware, it is Kernel sources Which repository? ments from the kernel development pro- common to include al- download and unpack • Standard • Morton cess. These enhancements can be both most everything the bug fixes and extensions, which need to kernel has to offer in go through final testing before being ad- the form of modules, [Patch] in /usr/src/ download and apply path-pl opted into the standard kernel. Andrew with the exception of path Morton releases the mm tree as a patch some extremely rare against the standard kernel. The “Patch- or problematic compo- ing the Kernel” box describes how to nents. Configuration • Suse: make cloneconfig apply a Linux kernel patch. Most distributions • clone current • others: /boot/config-2.6xxx configuration make oldconfig store the standard ker- • modify Spoiled for Choice nel configuration in The Linux kernel has a modular struc- /boot/config-kernelver- ture: various functions (for example sup- sion. Root can query Compile Successful the current kernel ver- • make • build does not quit with error Patching the Kernel sion by entering • make modules_install • bzImage in arch/i386/boot uname -r. Suse users Copy the patch to /usr/src/ and unpack patches ending in .gz with gunzip. Then will not find the kernel Install Run mkinitrd and enter new change to the kernel directory and use configuration in /boot; • bzImage to /boot kernel image and initrd in the same patch tool to apply the patch: instead the current • entry in menu.lst boot loader kernel dynamically $ cd linux-2.6.[Version] generates a config.gz Figure 1: Step by step to the new kernel: you don't have to be an $ patch -p1 <../[Patchfile] file below /proc. Copy expert to build your own. WWW.LINUX - MAGAZINE.COM ISSUE 69 AUGUST 2006 59 KNOW-HOW Kernel Tips configuration tool at least once before menu.lst file to do this. If you have Lilo, a filesystem on disk. Most distributions you jump into the build. This will add the entries are in /etc/lilo.conf. opt for a so-called initial ramdisk to re- configuration options new to the latest solve this. The kernel first temporarily kernel version and automatically set the Up and Running mounts the initial ramdisk as the root defaults. A classic chicken and egg problem oc- filesystem, and the bootloader places the Now quit the configuration without curs at boot time: to read filesystems, the ramdisk’s content at the kernel’s dis- changing any settings. To quit the con- kernel needs modules that reside within posal. The kernel finds the required figuration, select Exit in Menuconfig or Xconfig until the tool asks you if you would like to save the configuration. Say yes when prompted. If you are working with Gconfig, you will have to save the configuration yourself; failure to do so will result in any changes being dis- carded.