重新编译RedHat的官方内核
作者:sealinger 发布时间:November 4, 2010 分类:混口饭吃
重新编译RedHat的官方内核
RHEL升级内核,当然首选RedHat官方提供的,享受众多补丁和优化的配置。
以RHEL4.7系统升级内核到RHEL4.8的为例。
从这里下载内核源码包:
ftp://updates.redhat.com/enterprise/4AS/en/os/SRPMS/
例如 kernel-2.6.9-89.EL.src.rpm
1、安装内核源码:
[root@localhost rpms]# rpm -ivh kernel-2.6.9-89.EL.src.rpm
(Ignore, group does not exist or user does not exist errors.)
源码被解压至 /usr/src/redhat/SOURCES 目录,并且在 /usr/src/redhat/SPECS 目录中建立 kernel-2.6.spec 文件。
2、Prepare the Source
可以修改一下kernel-2.6.spec 文件(实际测试没有修改):[引用网上的]可以关闭Xen:%define buildxen 1 #改为0 %define buildsource 0------------改为1,这就是为什么没有源代码包的原因,因为默认不生成源代码包。
(ligb:这个没搞懂,默认不改也生成了 src.rpm 的包啊)
---------------开始:-----------------
[root@localhost rpms]# rpmbuild -bp --target=$(uname -m) /usr/src/redhat/SPECS/kernel-2.6.spec
The source files will be properly located in /usr/src/redhat/BUILD/kernel-2.6.18/. There are 2 useful directories:
linux-2.6.18.ARCH/ This will have the standard kernel.org kernel WITH Fedora patches and updates. The ARCH architecture will match the output of uname -m, usually i686. You may use noarch for the target= option if you wish. vanilla/ This will have the standard kernel.org kernel ONLY (no patches or updates).
[root@localhost rpms]# ls /usr/src/redhat/BUILD/
kernel-2.6.9
[root@localhost rpms]# ls /usr/src/redhat/BUILD/kernel-2.6.9/
linux-2.6.9
3、为了方便,移动一下目录:
[root@localhost rpms]# cd /usr/src/
[root@localhost src]# mv /usr/src/redhat/BUILD/kernel-2.6.9/linux-2.6.9 .
打个包,备份一下:
[root@localhost src]# tar zcvf linux-2.6.9.tar.gz linux-2.6.9/
4、创建软连接:
Note: Regardless of which kernel you are compiling make sure to have softlinks set up properly to use /usr/src/linux.
[root@localhost src]# pwd
/usr/src
[root@localhost src]# ls
kernels linux linux-2.6.9 redhat
[root@localhost src]# ln -s linux-2.6.9 linux
5、Setup the Initial .config File
The .config file contains EVERYTHING specific to the kernel you compile. You must have a starting .config file or it will be too difficult to select the right set of options.
[root@localhost src]# pwd
/usr/src
[root@localhost src]# ls linux/configs/
kernel-2.6.9-i686.config kernel-2.6.9-i686-hugemem.config kernel-2.6.9-i686-smp.config kernel-2.6.9-i686-xenU.config
#
[root@localhost src]# uname -m
i686
[root@localhost src]# cd linux
[root@localhost linux]# diff .config configs/kernel-2.6.9-i686.config
# 没有区别,说明默认的 .config 是 kernel-2.6.9-i686.config
但是我们要使用smp的内核:
[root@localhost linux]# mv .config .config.bak
[root@localhost linux]# cp configs/kernel-2.6.9-i686-smp.config .config
#
6、Kernel Name
可以自己修改内核名字的EXTRA name :
As an option you can change the EXTRA name attached to the end of the kernel, for example: 1.1369_FC4. Open /usr/src/linux/Makefile in any editor (gedit, kwrite, nano, etc.), and go to line 4.
For Fedora kernel:
EXTRAVERSION = -prep
For vanilla kernel:
EXTRAVERSION = .1
You can change this value to ANYTHING you like. The result will be that yourkernel name will be something like: 2.6.11-prep or 2.6.12.1.
[root@localhost linux]# vi Makefile
尽可能的保持源码包的版本号:
EXTRAVERSION = -89.UCWEB
7、Configure the .config File
根据代码树根目录下的.config文件 (该命令可以在现有内核设置文件的基础上建立一个新的内核设置文件。)
[root@localhost linux]# make oldconfig
运行完后的区别:
[root@localhost linux]# diff .config configs/kernel-2.6.9-i686-smp.config
3,4c3,4
< # Linux kernel version: 2.6.9-89.UCWEB
< # Sat Dec 13 01:59:49 2008
---
> # Linux kernel version: 2.6.9-prep
> # Fri Dec 12 23:17:05 2008
#
配置必要的选项:
[root@localhost linux]# make menuconfig
为了保险,没有做较大改动,建议的改动如下:
1)Processor type and features 中央处理器(CPU)类型及特性
Processor family选择 Pentium-4/Celeron(P4-based)/Pentium-4 M/Xeon
[ ] Generic x86 support 不选(能选到匹配的CPU,就不用选这个)
2)Kernel Hacking 中
Kernel debugging ---> 不选,不是内核开发者就不用选
[root@localhost linux]# diff .config configs/kernel-2.6.9-i686-smp.config
3,4c3,4
< # Linux kernel version: 2.6.9-89.UCWEB
< # Wed Jul 21 16:08:53 2010
---
> # Linux kernel version: 2.6.9-prep
> # Tue Jul 20 17:53:53 2010
32c32
< CONFIG_LOG_BUF_SHIFT=15
---
> CONFIG_LOG_BUF_SHIFT=17
36a37
> # CONFIG_KALLSYMS_ALL is not set
81c82
< # CONFIG_M686 is not set
---
> CONFIG_M686=y
85c86
< CONFIG_MPENTIUM4=y
---
> # CONFIG_MPENTIUM4 is not set
95c96
< # CONFIG_X86_GENERIC is not set
---
> CONFIG_X86_GENERIC=y
99a101
> CONFIG_X86_PPRO_FENCE=y
299a302
> # CONFIG_DEBUG_DRIVER is not set
2403c2406,2412
< # CONFIG_DEBUG_KERNEL is not set
---
> CONFIG_DEBUG_KERNEL=y
> CONFIG_MAGIC_SYSRQ=y
> # CONFIG_DEBUG_SLAB is not set
> CONFIG_DEBUG_SPINLOCK=y
> CONFIG_DEBUG_SPINLOCK_SLEEP=y
> CONFIG_DEBUG_HIGHMEM=y
> CONFIG_DEBUG_INFO=y
2405a2415,2418
> CONFIG_DEBUG_STACKOVERFLOW=y
> CONFIG_KPROBES=y
> CONFIG_DEBUG_STACK_USAGE=y
> # CONFIG_DEBUG_PAGEALLOC is not set
2406a2420
> # CONFIG_SCHEDSTATS is not set
#
8、Compiling and Installing:
注:如果想生成rpm包,可以直接进入第9节。
1)编译:
[root@localhost linux]# make all
2)安装:
[root@localhost linux]# make modules_install[root@localhost linux]# make install
这样就已经完成了,检查一下:
[root@localhost linux]# ll /boot/ -tr
查看:
[root@localhost linux]# ls /lib/modules/2.6.9-89.UCWEB/
GRUB已经加上了:
[root@localhost linux]# vi /boot/grub/menu.lst
default=1 ---> 可以手动改成0,然后重启系统
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux AS (2.6.9-89.UCWEB)
root (hd0,0)
kernel /vmlinuz-2.6.9-89.UCWEB ro root=LABEL=/
initrd /initrd-2.6.9-89.UCWEB.img
title Red Hat Enterprise Linux AS (2.6.9-55.ELsmp)
root (hd0,0)
kernel /vmlinuz-2.6.9-55.ELsmp ro root=LABEL=/
initrd /initrd-2.6.9-55.ELsmp.img
title Red Hat Enterprise Linux AS-up (2.6.9-55.EL)
root (hd0,0)
kernel /vmlinuz-2.6.9-55.EL ro root=LABEL=/
initrd /initrd-2.6.9-55.EL.img
9、创建rpm包的方法:
make menuconfig 后,可以直接构建内核的rpm包:
1)现在我们简单运行下面命令来构建内核:make rpm
Begin compile: (加入 UTS_MACHINE=i686 参数才能编译i686的出来,否则是i386平台的)
[root@localhost linux]# make UTS_MACHINE=i686 rpm
/bin/sh /usr/src/linux-2.6.9/scripts/package/mkspec > /usr/src/linux-2.6.9/kernel.spec
make clean
set -e; cd ..; ln -sf /usr/src/linux-2.6.9 kernel-2.6.989.UCWEB
set -e; cd ..; tar -cz -f kernel-2.6.989.UCWEB.tar.gz kernel-2.6.989.UCWEB/.
set -e; cd ..; rm kernel-2.6.989.UCWEB
set -e; \
/bin/sh /usr/src/linux-2.6.9/scripts/mkversion > /usr/src/linux-2.6.9/.tmp_version
set -e; \
mv -f /usr/src/linux-2.6.9/.tmp_version /usr/src/linux-2.6.9/.version
rpmbuild --target i686 -ta ../kernel-2.6.989.UCWEB.tar.gz
Building target platforms: i686
Building for target i686
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.9906
+ umask 022
+ cd /usr/src/redhat/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cd /usr/src/redhat/BUILD
+ rm -rf kernel-2.6.989.UCWEB
+ /bin/gzip -dc /usr/src/kernel-2.6.989.UCWEB.tar.gz
+ tar -xf -
...
...
...
INSTALL sound/usb/usx2y/snd-usb-usx2y.ko
if [ -r System.map ]; then /sbin/depmod -ae -F System.map -b /var/tmp/kernel-2.6.989.UCWEB-root -r 2.6.9-89.UCWEB;fi
+ cp arch/i386/boot/bzImage /var/tmp/kernel-2.6.989.UCWEB-root/boot/vmlinuz-2.6.9-89.UCWEB
+ cp System.map /var/tmp/kernel-2.6.989.UCWEB-root/boot/System.map-2.6.9-89.UCWEB
+ cp .config /var/tmp/kernel-2.6.989.UCWEB-root/boot/config-2.6.9-89.UCWEB
+ /usr/lib/rpm/brp-compress
Processing files: kernel-2.6.989.UCWEB-1
Provides: kernel-2.6.9-89.UCWEB kernel-drm
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Checking for unpackaged file(s): /usr/lib/rpm/check-files /var/tmp/kernel-2.6.989.UCWEB-root
Wrote: /usr/src/redhat/SRPMS/kernel-2.6.989.UCWEB-1.src.rpm
Wrote: /usr/src/redhat/RPMS/i686/kernel-2.6.989.UCWEB-1.i686.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.33427
+ umask 022
+ cd /usr/src/redhat/BUILD
+ cd kernel-2.6.989.UCWEB
+ exit 0
rm ../kernel-2.6.989.UCWEB.tar.gz
#
注意rpm包生成的位置:Wrote: /usr/src/redhat/SRPMS/kernel-2.6.989.UCWEB-1.src.rpm
Wrote: /usr/src/redhat/RPMS/i686/kernel-2.6.989.UCWEB-1.i686.rpm
现在可以把rpm包拷贝到其他RHEL系统上安装了。
2)安装:--nodeps忽略lksctp-tools错误:
[root@localhost2 kernel]# rpm -ivh kernel-2.6.989.UCWEB-1.i686.rpm
error: Failed dependencies:
kernel >= 2.6.10 conflicts with lksctp-tools-1.0.2-6.4E.1.i386
[root@localhost2 kernel]# rpm -ivh --nodeps kernel-2.6.989.UCWEB-1.i686.rpm
Preparing... ########################################### [100%]
1:kernel ########################################### [100%]
#
3)Next we create a ramdisk for our new kernel, because otherwise the system will most likely not boot our new kernel:
做 initrd:
[root@localhost2 kernel]# mkinitrd /boot/initrd-2.6.9-89.UCWEB.img 2.6.9-89.UCWEB
# ll /boot/ -htr
total 9.1M
-rw-r--r-- 1 root root 21K Aug 4 2005 message.ja
-rw-r--r-- 1 root root 23K Aug 4 2005 message
-rw-r--r-- 1 root root 1.5M Jul 10 2008 vmlinuz-2.6.9-78.EL
-rw-r--r-- 1 root root 763K Jul 10 2008 System.map-2.6.9-78.EL
-rw-r--r-- 1 root root 51K Jul 10 2008 config-2.6.9-78.EL
-rw-r--r-- 1 root root 65K Jul 10 2008 symvers-2.6.9-78.EL.gz
-rw-r--r-- 1 root root 1.5M Jul 10 2008 vmlinuz-2.6.9-78.ELsmp
-rw-r--r-- 1 root root 779K Jul 10 2008 System.map-2.6.9-78.ELsmp
-rw-r--r-- 1 root root 50K Jul 10 2008 config-2.6.9-78.ELsmp
-rw-r--r-- 1 root root 66K Jul 10 2008 symvers-2.6.9-78.ELsmp.gz
-rw-r--r-- 1 root root 715K May 22 2009 initrd-2.6.9-78.EL.img
-rw-r--r-- 1 root root 704K May 22 2009 initrd-2.6.9-78.ELsmp.img
drwxr-xr-x 2 root root 4.0K May 22 2009 grub
drwx------ 2 root root 16K May 23 2009 lost+found
-rw-r--r-- 1 root root 1.4M Jul 27 13:11 vmlinuz-2.6.9-89.UCWEB
-rw-r--r-- 1 root root 763K Jul 27 13:11 System.map-2.6.9-89.UCWEB
-rw-r--r-- 1 root root 50K Jul 27 13:11 config-2.6.9-89.UCWEB
-rw-r--r-- 1 root root 700K Jul 27 14:32 initrd-2.6.9-89.UCWEB.img
#
4)修改 /boot/grub/menu.list ,然后重启即可。
[root@localhost2 boot]# grubby --add-kernel=/boot/vmlinuz-2.6.9-89.UCWEB \
--title="Red Hat Enterprise Linux AS (2.6.9-89.UCWEB)" \
--copy-default --make-default \
--initrd=/boot/initrd-2.6.9-89.UCWEB.img#
后记:REDHAT官方有不是SRC的RPM包下载,可以直接安装的。
From REISTLIN:
REDHAT官方内核RPM包下载:
good article