Compiling a kernel & kernel-devel rpm in Fedora 6 (i686 or x86_64)


Following on from Fedora's official guide on kernel development this is how to compile an i686 kernel and kernel-devel rpm as a non-root user. (For x86_64 just replace all occurrences of i686 with x86_64)

If previously you've been experimenting with compiling as root then you should clean out /var/tmp, so just type   rm -fr /var/tmp/*   (as root)

1. Ensure you have development tools:

sudo yum groupinstall "Development Tools"
sudo yum install rpmdevtools


2. Login as your user (not root) in a terminal and type

rpmdev-setuptree

Get the required kernel source from, eg http://download.fedora.redhat.com/pub/fedora/linux/core/updates/6/SRPMS and install (I used kernel-2.6.20-1.2925.fc6.src.rpm)

rpm -Uhv kernel-<version>.src.rpm

(ignore warnings about 'user brewbuilder')


3. Edit the spec file ~/rpmbuild/SPECS/kernel-2.6.spec and apply these edits (line numbers are approx) to ensure you get just a kernel rpm and a kernel-devel rpm

In recent kernels these first two edits may be unnecessary
~line 13:    %define buildxen 0
~line 15:    %define buildkdump 0


add some specific label to the kernel version such as '.test' by editing the release string as follows:
~line 41:    %define release %(R="$Revision: 1.2925.test $"; RR="${R##: }"; echo ${RR%%?})%{?dist}


find an arch specific line    %ifarch i686    a little further down and change the enclosed defines to
~line 81:    %define builddebug 0
~line 108:   %define buildpae 0

then add this line directly below the   %define buildpae   line:   
%define _enable_debug_packages 0

NOTE1. If you do not apply these edits then the compilation time will increase up to 4-fold, also you will need several gigs of free space available to /var/tmp due to all the debug builds. Obviously, if you specifically need a pae or xen kernel then leave those options at '1'.

NOTE2: These lines and line numbers vary with different versions of the kernel source, but you should be able to find the relevant ones.


4. Build the source tree (This and the following steps requires approx 1 gig free space in your home directory, much more if you enabled debug builds etc.)**

rpmbuild -bp --target=i686 ~/rpmbuild/SPECS/kernel-2.6.spec


5. Create a clean build environment

cd ~/rpmbuild/BUILD/kernel-<version>/linux-<version>/
make mrproper


6. Now copy a config file from a default one in ~/rpmbuild/BUILD/kernel-<version>/linux-<version>/configs/, or elsewhere, and configure it for the compilation

cp configs/kernel-<version>-i686.config .config
make oldconfig

Press return to accept defaults to any questions if prompted (or chose suitable alternatives)

Now apply any custom settings using 'make menuconfig'. Make sure Kernel debugging is NOT selected in the 'Kernel hacking' section (If you need to select this then at least deselect the option to 'Compile the kernel with debug info', otherwise the modules size will be huge)

make menuconfig

Ensure to save this config on exit from menuconfig if you made any changes. (You can use   make xconfig   or   make gconfig   if you want a nice gui interface)


7. Now a fiddly bit (fedora team, why?).   rpmbuild will ignore this .config, so for an i686 build we have to copy it back to ~/rpmbuild/SOURCES/kernel-<version>-i686.config

cp .config ~/rpmbuild/SOURCES/kernel-<version>-i686.config

and then edit ~/rpmbuild/SOURCES/kernel-<version>-i686.config and add '# i386' to the top ('# x86_64' for 64 bit version) so the first 3 lines are
# i386
#
# Automatically generated make config: don't edit


8. Now you can build the kernel

rpmbuild -bb --target=i686 ~/rpmbuild/SPECS/kernel-2.6.spec

If you get a message about entropy being too low, just open any application temporarily and then close it, (the build uses random information from your environment to generate keys for package signing).
You can ignore the multitude of warnings generated.

And after an hour or so (depending on cpu/memory, it took 32mins on an AMD64 3500, 1GB ram) you'll have two rpms in ~/rpmbuild/RPMS/i686, a kernel rpm and a kernel-devel rpm.


9. Install the kernel and kernel-devel

sudo rpm -ihv ~/rpmbuild/RPMS/i686/kernel*.rpm

You can use the '--force' option if this complains that a newer kernel is already installed, but be warned that you'll overwrite the current kernel if the revision name from step 3 matches the current one, so add something like .test at that step if you want to keep the current kernel as a boot option in grub (highly recommended))
grub.conf will automatically be edited to make this kernel the default boot (which you can of course change), it will also create the ram disk image in /boot, so no need to run mkinitrd

The ~/rpmbuild directory will now be a around 1 gig so once you've copied the two rpms somewhere safe you probably should delete all the files in it (unless you are building other rpms or want to do another kernel compile (then go back to step 5))

rpmdev-wipetree

(to just remove the files created during the build steps, use   rm -fr ~/rpmbuild/BUILD/kernel-<buildversion>  , you then have to go back to step 4 to do another build)



NOTE. An alternative method is to use   make UTS_MACHINE=i686 rpm   in place of steps 7 & 8, however this does not produce a kernel-devel rpm which is really needed in Fedora (you do get a kernel src.rpm which you also get if you use rpmbuild -ba). Also, when you install the kernel rpm created this way you have to do the extra steps of editing grub.conf and mkinitrd to generate a ram disk yourself. Thus the rpmbuild -bb method is superior and is the recommended method in Fedora.

** After doing rpmdev-setuptree you can point the build directory to wherever you want by editing ~/.rpmmacros and changing the line which defines %_topdir., then copy the rpmbuild directory structure to that location with 'cp -a ~/rpmbuild <destination>', just make sure your user has full permissions on the directory you point to.


Thanks to folks at fedoraforum.org et al

Contact: jbg AT f2s DOT com