beissemj
2007-01-10, 07:37 AM CST
Many people think that creating a custom kernel is difficult, when in fact nothing could be farther from the truth. Compiling your own kernel is rather simple, and is much easier than the Fedora way (http://forums.fedoraforum.org/showthread.php?t=144066) (imho). Compiling a kernel the Fedora way gets you the bonus of the Fedora patches and a nice easy RPM. However, it is rather simple to compile a kernel generically.
Why would I want to do this?
Well, I think that building a kernel manually takes away some of the magic in using linux. You will also get a smaller kernel, which will result in a faster boot time (most likely), and a smaller kernel compile time (mine was 12m27.094s). Finally, sometimes you need to apply a patch to the kernel to enable support for a particular device, and that can be done quite easily this way.
1. Straight to the source.
Assuming you don't want Fedora patches (as I would say the average user doesn't use them anyways), then you'll need to download the kernel source from kernel.org (http://www.kernel.org). They provide both stable and testing kernels, so take your pick. The kernels are available in either tar.bz2 format or tar.gz format and are usually around 30-40MB. Either format will do, however you might as well get the tar.bz2 format as it is smaller.
2. Unpack the source
Now that you have your kernel source e.g. linux-2.6.19.1.tar.bz2 we need to unpack the kernel sources and create a symlink to the kernel sources directory:
# cp /location/to/kernel/linux-2.6.19.1.tar.bz2 /usr/src
# cd /usr/src
# tar -xjpf linux-2.6.19.1.tar.bz2
# ln -s linux-2.6.19.1 linux
3. (Optional) Applying patches
Move the downloaded kernel patch to the /usr/src/linux directory.
#cd /usr/src/linux
If you downloaded a patch with a .gz extension, execute the following command: gunzip patch-2.2.x.gz
If you downloaded a patch with a .bz2 extension, execute the following command: bunzip2 patch-2.2.x.bz2
There should now be a file called patch-x.x.xx in the /usr/src/linux directory. Apply the patch to the kernel source tree with the following command: # patch -p1 --dry-run < patch-x.x.xx
# patch -p1 -s < patch-x.x.xxThe first command is just a test, it does nothing to your sources. If it doesn't show errors, you can run the second command which actually applies the patch. Don't do it if the first command shows errors!
4. Configure The Kernel
It's a good idea to clean out any old stuff, and then start with your current configuration so: # make mrproper
# cp /boot/config-your-config /usr/src/linux/.config
# cd /usr/src linux ; make oldconfig
(just keep hitting enter until it's done)
# make menuconfig
(if you get an error you probably need to install ncurses-devel)Now all that's left is to configure your kernel. Press 'h' to get help on a particular item, and press '/' to search for something. When you are done save and exit.
5. Compile and install
To compile and install your kernel along with copying all important data to /boot/ do: # make prepare
# make all
# make modules_install
# make install
# cp Module.symvers /boot/symvers-2.6.19.fc6.gz
# cp .config /boot/config-2.6.19 Now you are all set. Your grub/menu.lst will automatically be updated, and you will have an initrd.img (provided you have that kernel option enabled). So now reboot and try out your new kernel.
Troubleshooting
Q. I get a kernel panic when I try to boot?
A. There are many reasons for this, however the most common are lack of support for ramdisks, initrd, and/or the filesystems that you use (e.g. ext2, ext3, reiserfs, etc)
Advanced
So you want to use an .rpm and you want the Fedora patches, and you don't want to build it the Fedora way. Okay, you can do that too.
A. Using an .rpm
Get your rpm (http://download.fedora.redhat.com/pub/fedora/linux/core/development/source/SRPMS/) (e.g. kernel-2.6.19-1.2906.fc7.src.rpm).
Now extract the contents and install the patches.
# mkdir /usr/src/rpm
# mv /your/location/kernel-2.6.19-1.2906.fc7.src.rpm /usr/src/rpm/
# cd /usr/src/rpm/
# rpm2cpio kernel-2.6.19-1.2906.fc7.src.rpm | cpio -idmv
# tar -xvjpf linux-2.6.19.tar.bz
# ln -s /usr/src/rpm/linux-2.6.19 /usr/src/linuxMove and apply Fedora patches# mv /usr/src/rpm/*.patch /usr/src/linux/
Now we need a short script to apply the patches because there are a lot of them. This script uses quilt (yum-getable), as that was the first thing I thought of.#!/bin/csh
#/usr/src/linux/patch.sh
foreach i ( `ls *.patch | sed s/\.patch//` )
quilt add $i.patch
end# sh ./patch.shNow proceed to configure your kernel as you normally would.
Why would I want to do this?
Well, I think that building a kernel manually takes away some of the magic in using linux. You will also get a smaller kernel, which will result in a faster boot time (most likely), and a smaller kernel compile time (mine was 12m27.094s). Finally, sometimes you need to apply a patch to the kernel to enable support for a particular device, and that can be done quite easily this way.
1. Straight to the source.
Assuming you don't want Fedora patches (as I would say the average user doesn't use them anyways), then you'll need to download the kernel source from kernel.org (http://www.kernel.org). They provide both stable and testing kernels, so take your pick. The kernels are available in either tar.bz2 format or tar.gz format and are usually around 30-40MB. Either format will do, however you might as well get the tar.bz2 format as it is smaller.
2. Unpack the source
Now that you have your kernel source e.g. linux-2.6.19.1.tar.bz2 we need to unpack the kernel sources and create a symlink to the kernel sources directory:
# cp /location/to/kernel/linux-2.6.19.1.tar.bz2 /usr/src
# cd /usr/src
# tar -xjpf linux-2.6.19.1.tar.bz2
# ln -s linux-2.6.19.1 linux
3. (Optional) Applying patches
Move the downloaded kernel patch to the /usr/src/linux directory.
#cd /usr/src/linux
If you downloaded a patch with a .gz extension, execute the following command: gunzip patch-2.2.x.gz
If you downloaded a patch with a .bz2 extension, execute the following command: bunzip2 patch-2.2.x.bz2
There should now be a file called patch-x.x.xx in the /usr/src/linux directory. Apply the patch to the kernel source tree with the following command: # patch -p1 --dry-run < patch-x.x.xx
# patch -p1 -s < patch-x.x.xxThe first command is just a test, it does nothing to your sources. If it doesn't show errors, you can run the second command which actually applies the patch. Don't do it if the first command shows errors!
4. Configure The Kernel
It's a good idea to clean out any old stuff, and then start with your current configuration so: # make mrproper
# cp /boot/config-your-config /usr/src/linux/.config
# cd /usr/src linux ; make oldconfig
(just keep hitting enter until it's done)
# make menuconfig
(if you get an error you probably need to install ncurses-devel)Now all that's left is to configure your kernel. Press 'h' to get help on a particular item, and press '/' to search for something. When you are done save and exit.
5. Compile and install
To compile and install your kernel along with copying all important data to /boot/ do: # make prepare
# make all
# make modules_install
# make install
# cp Module.symvers /boot/symvers-2.6.19.fc6.gz
# cp .config /boot/config-2.6.19 Now you are all set. Your grub/menu.lst will automatically be updated, and you will have an initrd.img (provided you have that kernel option enabled). So now reboot and try out your new kernel.
Troubleshooting
Q. I get a kernel panic when I try to boot?
A. There are many reasons for this, however the most common are lack of support for ramdisks, initrd, and/or the filesystems that you use (e.g. ext2, ext3, reiserfs, etc)
Advanced
So you want to use an .rpm and you want the Fedora patches, and you don't want to build it the Fedora way. Okay, you can do that too.
A. Using an .rpm
Get your rpm (http://download.fedora.redhat.com/pub/fedora/linux/core/development/source/SRPMS/) (e.g. kernel-2.6.19-1.2906.fc7.src.rpm).
Now extract the contents and install the patches.
# mkdir /usr/src/rpm
# mv /your/location/kernel-2.6.19-1.2906.fc7.src.rpm /usr/src/rpm/
# cd /usr/src/rpm/
# rpm2cpio kernel-2.6.19-1.2906.fc7.src.rpm | cpio -idmv
# tar -xvjpf linux-2.6.19.tar.bz
# ln -s /usr/src/rpm/linux-2.6.19 /usr/src/linuxMove and apply Fedora patches# mv /usr/src/rpm/*.patch /usr/src/linux/
Now we need a short script to apply the patches because there are a lot of them. This script uses quilt (yum-getable), as that was the first thing I thought of.#!/bin/csh
#/usr/src/linux/patch.sh
foreach i ( `ls *.patch | sed s/\.patch//` )
quilt add $i.patch
end# sh ./patch.shNow proceed to configure your kernel as you normally would.