Wednesday, July 14, 2010

Build Linux Kernel from Source in Fedora 9

Ref [1] http://fedoraproject.org/wiki/Docs/CustomKernel#Configure_Kernel_Options
Ref [2] http://www.howtoforge.com/kernel_compilation_fedora

GET KERNEL SOURCE CODE BY EITHER YUM OR DIRECTLY FROM HTTP SITE
--------------------------------------------------------------

WHERE IS KERNEL SOURCE CODE IN FEDORA 9
----------------------------------------------------

JUST GET HEADER FILES OF KERNEL (enough to compile some extra modules)
-----------------------------------------------------------------------
kernel-devel-2.6.25-14.fc9.i686.rpm only gives you the header files of linux kernel
[root@localhost sierra_drv_1.7.32]# yum install kernel-devel-2.6.25-14.fc9.i686

DIRECT DOWNLOAD KERNEL RPM
--------------------------------
cd /usr/src
wget http://download.fedora.redhat.com/pub/fedora/linux/core/9/source/SRPMS/kernel-2.6.25-14.fc9.src.rpm
rpm -ivh kernel-2.6.18-1.2798.fc6.src.rpm

NOTE: .src.rpm is no longer availabe in above path.


GET KERNEL RPM VIA YUM
-----------------------------------
yum install rpmdevtools yum-utils
yumdownloader --destdir /home/q.yang/Download/LinuxSrcRpm2.6.27/ --source kernel
[q.yang@localhost ~]$ rpmdev-setuptree
[q.yang@localhost ~]$ su -c 'yum-builddep Download/LinuxSrcRpm2.6.27/kernel-2.6.27.25-78.2.56.fc9.src.rpm'

[q.yang@localhost ~]$ rpm -ivh /home/q.yang/Download/LinuxSrcRpm2.6.27/kernel-2.6.27.25-78.2.56.fc9.src.rpm

[q.yang@localhost SPECS]$ rpmbuild -bp --target=i686 kernel.spec

BUILD KERNEL ON USER SPACE
-----------------------------
1. Prepare a RPM package building environment in your home directory. Run the following command:
[q.yang@localhost ~]$ rpmdev-setuptree
[q.yang@localhost ~]$ ll
total 40
drwxr-xr-x 2 q.yang q.yang 4096 2010-06-03 17:23 Desktop
drwxr-xr-x 4 q.yang q.yang 4096 2010-07-06 10:54 Documents
drwxr-xr-x 4 q.yang q.yang 4096 2010-07-06 09:54 Download
drwxrwxr-x 3 q.yang q.yang 4096 2010-06-10 11:37 lpc3250
drwxr-xr-x 2 q.yang q.yang 4096 2010-06-03 17:13 Music
drwxr-xr-x 2 q.yang q.yang 4096 2010-06-03 17:13 Pictures
drwxr-xr-x 2 q.yang q.yang 4096 2010-06-03 17:13 Public
drwxrwxr-x 7 q.yang q.yang 4096 2010-07-06 10:55 rpmbuild NEW DIRECTORY
drwxr-xr-x 2 q.yang q.yang 4096 2010-06-03 17:13 Templates
drwxr-xr-x 2 q.yang q.yang 4096 2010-06-03 17:13 Videos

rpmbuild directory automatically only locate in user's home directory (i.e., ~)
and a new .rpmmacros will appear

NOTE: pls run 'rpmdev-setuptree' command with q.yang user instead of root. It will impact where the target directory of 'rpm -ivh ....' command.


2. Install build dependencies for the kernel source with the yum-builddep command (root is required to install these packages):
[q.yang@localhost ~]$ su -c 'yum-builddep Download/LinuxSrcRpm2.6.27/kernel-2.6.27.25-78.2.56.fc9.src.rpm'
Password:
Loaded plugins: refresh-packagekit
module-init-tools-3.4-13.fc9.i386
patch-2.5.4-35.fc9.i386
bash-3.2-23.fc9.i386
coreutils-6.10-35.fc9.i386
2:tar-1.19-6.fc9.i386
bzip2-1.0.5-2.fc9.i386
1:findutils-4.2.33-3.fc9.i386
gzip-1.3.12-6.fc9.i386
m4-1.4.10-3.fc9.i386
4:perl-5.10.0-68.fc9.i386
1:make-3.81-12.fc9.i386
diffutils-2.8.1-21.fc9.i386
gawk-3.1.5-17.fc9.i386
gcc-4.3.0-8.i386
binutils-2.18.50.0.6-7.fc9.i386
redhat-rpm-config-9.0.2-1.fc9.noarch
rpm-build-4.4.2.3-3.fc9.i386
No uninstalled build requires

3. Install kernel-.src.rpm with the following command
rpm -ivh /home/q.yang/Download/LinuxSrcRpm2.6.27/kernel-2.6.27.25-78.2.56.fc9.src.rpm

New installation use:
[q.yang@localhost ~]$ rpm -ivh /home/q.yang/Download/LinuxSrcRpm2.6.27/kernel-2.6.27.25-78.2.56.fc9.src.rpm

To just update the installation use:
[q.yang@localhost ~]$ rpm -Uvh Download/LinuxSrcRpm2.6.27/kernel-2.6.27.25-78.2.56.fc9.src.rpm

1:kernel warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root

( Afterwards, new files appear under path : /home/q.yang/rpmbuild/SOURCES/ )

4. This step expands all of the source code files for the kernel. This is required to view the code, edit the code, or to generate a patch.
Prepare the kernel source tree using the following commands:

[q.yang@localhost SPECS]$ rpmbuild -bp --target=i686 kernel.spec

(
Afterwards, you will see all source codes and new directories under path /BUILD/:

|-- BUILD
| `-- kernel-2.6.27
| |-- linux-2.6.27.i686
| `-- vanilla-2.6.27


vanilla-2.6.27/drivers/rtc/rtc-sysfs.c
..........................
vanilla-2.6.27/drivers/rtc/rtc-bfin.c
................................
vanilla-2.6.27/drivers/rtc/rtc-sh.c
vanilla-2.6.27/drivers/watchdog/alim1535_wdt.c
....................................
vanilla-2.6.27/drivers/watchdog/pcwd_pci.c
vanilla-2.6.27/drivers/watchdog/s3c2410_wdt.c


linux-2.6.27.i686/drivers/rtc/rtc-sysfs.c
..........................
linux-2.6.27.i686/drivers/rtc/rtc-bfin.c
................................
linux-2.6.27.i686/drivers/rtc/rtc-sh.c
linux-2.6.27.i686/drivers/watchdog/alim1535_wdt.c
....................................
linux-2.6.27.i686/drivers/watchdog/pcwd_pci.c
linux-2.6.27.i686/drivers/watchdog/s3c2410_wdt.c



5. Do a source code copy if need to generate kernel patch.

[q.yang@localhost ~]$ cp -r ~/rpmbuild/BUILD/kernel-2.6.27/linux-2.6.27.i686 ~/rpmbuild/BUILD/kernel-2.6.27.orig
[q.yang@localhost ~]$ cp -al ~/rpmbuild/BUILD/kernel-2.6.27.orig/ ~/rpmbuild/BUILD/kernel-2.6.27.new

6. Config customized kernel.

[q.yang@localhost linux-2.6.27.i686]$ pwd
/home/q.yang/rpmbuild/BUILD/kernel-2.6.27/linux-2.6.27.i686
[q.yang@localhost linux-2.6.27.i686]$ make menuconfig
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/mconf.o
HOSTLD scripts/kconfig/mconf
scripts/kconfig/mconf arch/x86/Kconfig
#
# configuration written to .config
#


*** End of Linux kernel configuration.
*** Execute 'make' to build the kernel or try 'make help'.


7. Edit .config and copy to /rpmbuild/SOURCE
Add a new line to the top of the config file that contains the hardware platform the kernel is built for (the output of uname -i).
[q.yang@localhost linux-2.6.27.i686]$ vi .config
Added '# i386' in the first line of .config

[q.yang@localhost linux-2.6.27.i686]$ cp .config ~/rpmbuild/SOURCES/config-i686

===Following steps will Prepare Build Files======

8. Edit /rpmbuild/SPECS/kernel.spec
[q.yang@localhost linux-2.6.27.i686]$ cd ~/rpmbuild/SPECS/
[q.yang@localhost SPECS]$ vi kernel.spec

%define buildid .gsncommsdev

9. If you generated a patch, add the patch to the kernel.spec file, preferably at the end of all the existing patches and clearly commented.

The patch then needs to be applied in the patch application section of the spec file. Again, at the end of the existing patch applications and clearly commented.

===Following steps will build the new kernel=============

10.
To build all kernel flavors:
[q.yang@localhost SPECS]$ rpmbuild -bb --target=i686 kernel.spec

= To disable specific kernel flavors from the build (for a faster build):
rpmbuild -bb --without

No comments: