Last week we took a tour of the generic way of building custom Linux kernels. Today we’ll look some nice Debian shortcuts, and Fedora’s unique way of building custom kernels.
The Debian Way of Customizing Kernels
Debian’s kernel source packages are named linux-source-[version]. The current official source package versions are linux-source-2.6.18 (stable), linux-source-2.6.21 (testing and unstable), and linux-source-2.6.22 (unstable and experimental).
Binary Debian kernel packages are named linux-image-[version]. It’s been this way since version 2.6.12, if you’re remembering the olden days of the kernel-source and kernel-image packages. The new naming convention opens the door to using other kernels with Debian, which I don’t know what those would be, but Debian is ready for them.
To get started, install a build environment (see part 1), plus your kernel sources and the fakeroot package:
# aptitude install linux-source-2.6.22 kernel-package fakeroot
This downloads the source tarball into /usr/src/ where we do not want it, so you need to move it to your personal kernel-building directory:
# mv /usr/src/linux-source-2.6.22.tar.bz2 ~/kernel
Change to your personal kernel-building directory and unpack the tarball:
$ tar zxvf linux-source-2.6.22.tar.bz2
Then change to the top-level source directory and start configuring your new kernel:
$ cd linux-source-2.6.22
$ make mrproper
$ make xconfig
When you’re done slogging through configuration, run these commands:
$ make-kpkg clean
$ make-kpkg -rootcmd fakeroot -rev kernel.1 linux_image
fakeroot gives you enough root privileges to build kernels as an ordinary user. It won’t let you run commands that need genuine root privileges, but it’s good enough for kernel-building. This leaves you with a .deb package named linux-image-2.6.22_kernel.1_i686.deb. Of course you may make the -rev option say whatever you want. Then install it in the usual way with dpkg:
# dpkg -i linux-image-2.6.22_kernel.1_i686.deb
This installs the modules and handles module dependencies, creates a boot menu entry, and copies the kernel and related file to the /boot directory. Now you can reboot, select your new kernel from the boot menu, and play with your new kernel.
The Fedora Way of Customizing Kernels
Fedora patches its kernels all to heck, to the point that a vanilla kernel may not work. Of course it costs nothing but a bit of time to try. But if you want to use genuine Fedora kernel sources they come in source RPMs, so you’ll have to build your kernel and then package it into an RPM. First fetch your kernel SRPM from your favorite Fedora mirror, such as:
$ wget http://mirrors.kernel.org/fedora/releases/7/Fedora/source/SRPMS/kernel-2.6.21-1.3194.fc7.src.rpm
Then make sure you have the RPM building tools, plus the normal build environment from part 1:
# yum install rpmdevtools
The next step is to create a build tree in your home directory, and make sure to do this as yourself and not as the root user:
This creates an rpmbuildtree directory populated with BUILD, RPMS, SOURCES, SPECS, and SRPMS directories. Next, install the source RPM. This will unpack files into your new rpmbuildtree directory:
$ rpm -ivh 2.6.21-1.3194.fc7.src.rpm
Ignore the warnings about “group kojibuilder does not exist.”
Next, run the %prep stage of the RPM rebuild, and make the –target option match your CPU type:
$ rpmbuild -bp --target=i686 ~/rpmbuild/SPECS/kernel-2.6.spec
This extracts the kernel tarball and applies all the Fedora patches. Now change to the source directory of your new build tree:
$ cd ~/rpmbuild/BUILD/kernel-2.6.21/linux-2.6.21-1.3194.i686/
And get started with configuring your new kernel:
$ make mrproper
$ make xconfig
Finally, roll it all up into an RPM:
$ rpmbuild --target i686 -ba ~/rpmbuild/SPECS/kernel-2.6.spec
Again, make the –target option match your CPU type. Your new RPM should be in ~/rpmbuild/RPMS/i686/. Grab your new kernel.rpm and install it just like any other RPM:
# rpm -ivh kernel-2.6.21-1.3194.i686.rpm
This handles all the chores of copying files to /boot and creating a boot menu entry, so all you do is reboot and select your new kernel from your boot menu.
What Kernel Options To Use?
When you open xconfig for the first time it’s pretty overwhelming. Linux kernels are rather large now, so it takes awhile to wade through the configuration. As the whole point of this exercise is to wade through the configs and weed out the stuff you don’t want and to include the stuff you do want, just settle in and take your time. After going through it once you’ll have a re-usable custom .config file which will streamline future kernel-buildings. Just copy it to the top-level directory of your source tree every time you build a new kernel.
We’ll take a quick tour of necessary and common options, so you’re still going to have to make some decisions on your own. xconfig gives information on every option, plus the Documentation/ directory in your kernel source tree is cram-full of information.
Your first decision is in the “Code Maturity Level Options -> Prompt for development and/or incomplete code/drivers (EXPERIMENTAL).” If you want a plain old stable kernel without any wacky stuff, uncheck this. If you want to test new experimental features, then include this.
Say no to “Configure standard kernel features (for small systems) (EMBEDDED).” Unless of course you know this is something you want, which means you’re probably a kernel developer.
Include everything under “Loadable module support.”
“Processor Type and Features” is where you can make some noticeable performance tweaks. Turn off SMP if you’re not running a multi-core or multi-processor machine. Select your CPU type. Select memory support for your amount of physical RAM. Turn off all the laptop stuff if you’re not running a laptop.
“Block layer” is where you enable support for large disks and files, which means larger than two terabytes.
Under “Bus options” turn off PCMCIA guff on systems that are not laptops.
Under “Executable File Formats” include everything.
Under “Networking” get rid of all the network protocols you’re not going to use like Appletalk, Bluetooth, radio, or IPX. On machines that are not routers or firewalls, you can also get rid of routing options and QoS, and even Netfilter/iptables if you really want to go lean. Don’t go too crazy wiping out networking options unless you are very sure you do not want them, because you could break some networking feature that you need. Enable “IP: kernel level autoconfiguration (IP_PNP)” for diskless netbooting.
If you’re configuring a kernel for a routerboard like a Soekris or PC Engines WRAP board you might consider removing support for SATA, SCSI, and PATA drives, since they are not supported on most of these.
The rest is reviewing hardware and filesystem support, and cryptography options, plus a few other odds and ends. I recommend supporting all cryptography, which has become an everyday necessity. I probably spend the most time hunting down and getting rid of laptop-related features on desktop systems, or features related to specific laptops such as Dell and Toshiba, and then the dozens of hardware drivers I’ll never need.
As a general rule you can turn off anything related to generating debugging messages; these are interesting mainly to kernel hackers, and sometimes not even then.