centos7 编译内核的方法

通过源码的方式替换

1.  获取内核源码

获取内核源码的路径:https://www.kernel.org/pub/linux/kernel/

2.  解压内核源码

解压内核源码到路径/usr/src/kernels/

3.  Make mrproper

Make mrproper的作用是在每次配置并重新编译内核前执行该命令。将会清理源代码树,包括过去曾经配置的内核配置文件“.config”都将被清除。以免影响新的内核编译。

4.  Make menuconfig

make menuconfig命令用于生成的内核配置文件

内核配置的方式分为如下几种,选择其中一种即可:

#make menuconfig //基于ncurse库编制的图形工具界面

#make config //基于文本命令行工具,不推荐使用

#make xconfig //基于X11图形工具界面

#make gconfig //基于gtk+的图形工具界面

采用make menuconfig 时出现图形界面的含义如下:

对每一个配置选项,用户有三种选择,它们分别代表的含义如下:

<*>或[*]——将该功能编译进内核

[ ]——不将该功能编译进内核

[M]——将该功能编译成可以在需要时动态插入到内核中的代码

最后要执行save(版本不同可能执行方式不同,3.10.40会有save选项,而2.6.33则是有一个选项:save an alternate configuration file),这样就生成了配置好的.config。

5.  Make

开始编译内核源码,通过make将生成内核模块和vmlinuz,initrd.img,Symtem.map文件

注:#make相当于分别执行#makebzImage 和#make modules。根据需要也可以分开执行

6.  Make modules_install

安装模块:编译成功后,系统会在/lib/modules目录下生成一个****(版本号)子目录,里面存放着新内核的所有可加载模块(即将编译好的modules拷贝到/lib/modules下)

7.  Make install

安装内核,该命令会复制.config,vmlinuz,initrd.img,System.map文件到/boot目录、更新grub。

8.  修改grub.conf

通过更改default的值,来执行内核的默认启动版本。如图,如果default=0,则系统默认启动的内核版本为2.6.31.1

9.  Reboot

重启设备之后,通过命令uname –a 、 uname -r查看系统内核版本。

通过rpm的方式替换

1.  获取内核rpm

//http://vault.centos.org/6.5/centosplus/Source/SPackages/

源代码的官网:http://vault.centos.org/

进入官网后,再一次进入6.7/,进入os/,进入Source/,进入SPackages/,找到kernel-2.6.32-573.el6.src.rpm下载就行了

我使用的是kernel-2.6.32-431.el6.centos.plus.src.rpm

2.  解压内核rpm

rpm -i kernel-2.6.32-431.el6.centos.plus.src.rpm

该步骤结束之后会生成/root/rpmbuild/SPECS/kernel.spec

可能会遇到如下问题:

rpm -ivhkernel-2.6.32-431.el6.centos.plus.src.rpm

1:kernel                 warning: user mockbuild doesnot 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

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

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

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

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

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

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

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

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

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

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

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

warning:user mockbuild does not exist - using root

warning:group mockbuild does not exist - using root

###########################################[100%]

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

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

解决方式:添加用户和组后,再重新执行上述步骤

groupaddmockbuild

useradd mockbuild-g mockbuild

3.  生成源码

执行命令:rpmbuild -bb kernel.spec

① 安装依赖包

可能会遇到如下问题:

[root@ServerSPECS]# rpmbuild -bb kernel.spec

error:Failed build dependencies:

redhat-rpm-config is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

patchutils is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

xmlto is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

asciidoc is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

binutils-devel is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

newt-devel is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

audit-libs-devel is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

perl(ExtUtils::Embed) is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

bison is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

flex is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

hmaccalc is needed bykernel-2.6.32-431.el6.centos.plus.x86_64

解决方式:

说明依赖的包没有安装,需要将下边的包安装上。

bison-2.4.1-5.el6.x86_64.rpm        perl-ExtUtils-Embed-1.28-141.el6_7.1.x86_64.rpm

flex-2.5.35-9.el6.x86_64.rpm        redhat-rpm-config-9.0.3-51.el6.centos.noarch.rpm

asciidoc-8.4.5-4.1.el6.noarch.rpm

hmaccalc-0.9.12-2.el6.x86_64.rpm

slang-devel-2.2.1-1.el6.x86_64.rpm

audit-libs-devel-2.4.5-3.el6.x86_64.rpm         newt-devel-0.52.11-3.el6.x86_64.rpm

xmlto-0.0.23-3.el6.x86_64.rpm

binutils-devel-2.20.51.0.2-5.44.el6.x86_64.rpm

patchutils-0.3.1-3.1.el6.x86_64.rpm

② 其他依赖包

安装这些包的过程中,可能会依赖其他包,如下:

audit-libs-2.4.5-3.el6.x86_64.rpm

lynx-2.8.6-27.el6.x86_64.rpm

perl-libs-5.10.1-141.el6_7.1.x86_64.rpm

centos-indexhtml-6-2.el6.centos.noarch.rpm

perl-5.10.1-141.el6_7.1.x86_64.rpm

安装过程中可能会遇到如下问题:

[root@Server 1]# rpm -ivhperl-5.10.1-141.el6_7.1.x86_64.rpm

error:Failed dependencies:

perl-libs = 4:5.10.1-141.el6_7.1 is needed byperl-4:5.10.1-141.el6_7.1.x86_64

[root@Server1]# rpm -ivh perl-libs-5.10.1-141.el6_7.1.x86_64.rpm

error:Failed dependencies:

perl = 4:5.10.1-141.el6_7.1 is needed byperl-libs-4:5.10.1-141.el6_7.1.x86_64

解决方式:

这是因为安装perl时二者相互依赖,且与之前的包有冲突。可以通过如下方式:

rpm -ivh perl-libs-5.10.1-141.el6_7.1.x86_64.rpmperl-5.10.1-141.el6_7.1.x86_64.rpm --force

③安装rng命令

安装完以上依赖包后,重新执行rpmbuild -bb kernel.spec命令时,可能会出现如下问题:

卡住gpg:keyring `./pubring.gpg' created不动了

解决方式:

执行一下命令:rngd  -r  /dev/urandom

但是rng这个工具在我们的icfs系统中已经被裁减(原版的ceotos上存在该命令,已验证),因此需要手动安装上。步骤如下:

rpm -ivhrng-tools-5-2.el6_7.x86_64.rpm

到此,结束以上所有步骤之后,会在以下目录中生成内核源码:

/root/rpmbuild/BUILD/kernel-2.6.32-431.el6/linux-2.6.32-431.el6.centos.plus.x86_64

4.  拷贝.config

进入生成的源码目录:

cd  /root/rpmbuild/BUILD/kernel-2.6.32-431.el6/linux-2.6.32-431.el6.centos.plus.x86_64

cp  /boot/config-2.6.32-431.el6.x86_64  .config

或者cp/usr/src/kernels/2.6.32-431.el6.x86_64/.config     .config

5.  编译源码

Make –j24

6.    Make modules_install

7.  Make install

8.  修改grub.conf

9.  reboot

说明:在方式二中,在源码init/main.c中添加了打印信息,在内核启动后能够在/var/log/messages中找到。

风险事项:

1.      安装包时有冲突项。

2.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,757评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,478评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,540评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,593评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,903评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,329评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,659评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,383评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,055评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,337评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,864评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,227评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,820评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,999评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,750评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,365评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,260评论 2 258

推荐阅读更多精彩内容