Quick Custom Kernel Compile on Fedora from RPM Source
A few years ago, I'd often make it a point to do a custom compile on the Linux kernel. It was good learning, and an opportunity to improve system performance. Then, for a couple of years - the stock kernel that shipped with whatever distribution I was using seemed adequate. Then I found this thread at FedoraForum.org.
The procedure was pretty much well summed up by user cybrjackle in 2004; the following pages in the thread seems to run over some compile problems - but even through Fedora Core 5 - cybrjackle's instructions are still very useful.
First you have to set up a directory to build RPMs. I recommend installing package fedora-buildrpmtree if you haven't already.
First, cd to your home directory, or whever you'd like to work.
$ cd ~
$ sudo yum install fedora-rpmdevtools
If you haven't set up yourself as a sudo user (enter root password as appropriate):
$ su
Password:
$ yum install fedora-rpmdevtools
$ exit
After yum properly installs the package and you've ensured you're no longer root user, issue the following command:
$ fedora-buildrpmtree
You'll have a directory called rpmbuild off of your home directory, or whatever directory you decided to work in.
Before continuing you may wish to back up your Grub configuration. Installing your custom kernel into grub can be a little problematic, so you may wish to have something to refer to.
$ su
Password:
$ cp /etc/grub.conf /etc/grub.conf.backup
$ exit
Now, go to http://download.fedora.redhat.com/pub/fedora/linux/core/updates/ and select your appropriate version of Fedora. Enter the SRPMS directory. We're looking for the source RPM, not the typical install RPM. Find an appropriate kernel source package. For Fedora Core 5, they follow the naming convention of kernel-version-build_FC5.src.rpm. For this example, let's say we are using kernel-2.6.17-1.2139_FC5.src.rpm. You'll then install the the source package in the same manner you would for any RPM (as a regular user):
$ rpm -ivh kernel-2.6.17-1.2139_FC5.src.rpm
Now cd deeper into the rpmbuild directory you created a few moments ago, into the SPECS directory. The fedora-buildrpmtree also installed a Fedora kernel patch file that we'll integrate into the kernel source.
$ cd rpmbuild/SPECS/
$ rpmbuild -bp --target=i686 kernel-2.6.spec
Note that you may have to adjust the target parameter accordingly on your system, for example change i686 to x86_84 for newer 64 bit systems, or i386 to build for older systems or broader compatibility.
Now cd into the kernel/linux directory as below, except using the appropriate version numbers (and target).
$ cd rpmbuild/BUILD/kernel-2.6.17/linux-2.6.17.i686
Edit the file called Makefile with your preferred editor. Near the top, you'll see a line beginning with EXTRAVERSION. Change the suffix of this line to include whatever clever name you wish to give to this kernel. For this example, let's call it evilmonkey.
Now you're ready to customize the kernel. This is probably the part that requires the most care, and quite possibly also Googling further information about compiling the kernel. For example, for me - I can remove an entire ISDN subsystem, as I'm certain my computer will never ever care about and ISDN service under the current installation. Some people may go a step further and remove device compatibility they don't think they need, but then inadvertently disable something that's required for normal computer operation. Take care during this step to avoid headaches, only disable/enable things you are positive about.
You have a few options available to you for customizing your options in a menu format:
$ make menuconfig
This generates a text/console-based interface you can select options with.
$ make xconfig
Generates a GUI-based point and click configuration system (X-windows).
$ make gconfig
Generates a GUI-based point and click configuration system (Gnome/GTK based).
Make sure to use the "SAVE" option in whatever configuration editor you decide to use. This saved configuration file can let you tweak settings easier in the future, and the settings are required as part of the compile. Also, you can usually "Save as" to move the location of this configuration to an alternate path - in case you reinstall altogether or use a new version of Fedora.
Now it's time to compile. cybrjackle recommended using the time command to see how long the process takes. It's kind of interesting to see, so I'm also including it. You should still be in the ~/rpmbuild/BUILD/kernel-2.6.17/linux-2.6.17.i686 directory (or similar) at this point.
$ time make rpm
I did this on two systems. Looks like the process can run about 30-80 minutes on modern systems. Be patient, regardless of however fast your process runs.
At the end of the process, the output should state the location of your custom RPM. All you need to do is install this freshly-compiled just-for-you version of the kernel. In my case, it's going to be in the rpmbuild/RPMS/i386 directory. Note that it's in the i386 (architecture) despite being compiled for i686 (system optimizations - SSE, MMX, SSE2, etc.).
$ cd ~/rpmbuild/RPMS/i386/
$ rpm -ivh kernel-2.6.17evilmonkey-1.i386.rpm
Do a ls on the /boot directory. You should see your new kernel with a prefix of vmlinux. Also, ensure you have some other kernels available, in case this one didn't work for whatever reason.
$ ls -l /boot
-rw-r--r-- 1 root root 1509474 May 21 12:13 vmlinuz-2.6.16-1.2122_FC5
-rw-r--r-- 1 root root 1519016 Jun 5 22:44 vmlinuz-2.6.16-1.2133_FC5
-rw-r--r-- 1 root root 1730787 Jun 26 19:12 vmlinuz-2.6.17-evilmonkey
Now you may need to make an initrd (Initial Ramdisk). This may or may not be required, but let's be on the safe side. Using my version numbers and naming (vmlinuz-2.6.17-evilmonkey) as an example:
$ mkinitrd /boot/initrd-2.6.17-evilmonkey.img 2.6.17-evilmonkey
Now, you'll have to edit grub to install the new kernel. As root user, edit /etc/grub.conf. If you have kernels in place in your current grub.conf, you should be able to use these as a reference. Mine looks something like this:
default=2
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Fedora Core (2.6.16-1.2133_FC5)
root (hd0,0)
kernel /vmlinuz-2.6.16-1.2133_FC5 ro root=LABEL=/1 rhgb quiet
initrd /initrd-2.6.16-1.2133_FC5.img
title Fedora Core (2.6.16-1.2122_FC5)
root (hd0,0)
kernel /vmlinuz-2.6.16-1.2122_FC5 ro root=LABEL=/1 rhgb quiet
initrd /initrd-2.6.16-1.2122_FC5.img
title FC5-2.6.17-evilmonkey
root (hd0,0)
kernel /vmlinuz-2.6.17-evilmonkey ro root=LABEL=/1 rhgb quiet
initrd /initrd-2.6.17-evilmonkey.img
The default value specifies that the third entry (values 0,1,2) should be the default kernel should no user input be detected within the five second timeout.
Now, the next time you reboot - your new kernel should load by default.