CentOS 中如何快速定制二进制的内核 RPM 包
随着Linux服务器越来越多了,底层系统内核想要保持版本统一就需要定制专门的二进制安装包来便捷的升级和管理。 RedHat系那当然就是使用rpmbuild来做定制化管理了。
今天我们分俩个部分(root和普通用户)来讲解如何制作内核的二进制rpm包。
Building主机系统环境约定:
OS : CentOS release 6.6
Arch : i686
一,rpm包简单定制
注意: 这里我们首先要预留至少20GB的临时文件空间。
1、rpm 制作前的环境准备:
<span class="pln">yum </span><span class="pun">-</span><span class="pln">y groupinstall </span><span class="str">"Development Tools"</span>
<span class="pln">yum </span><span class="pun">-</span><span class="pln">y install ncurses</span><span class="pun">-</span><span class="pln">devel qt</span><span class="pun">-</span><span class="pln">devel</span>
<span class="pln">yum </span><span class="pun">-</span><span class="pln">y install rpm</span><span class="pun">-</span><span class="pln">build redhat</span><span class="pun">-</span><span class="pln">rpm</span><span class="pun">-</span><span class="pln">config asciidoc hmaccalc perl</span><span class="pun">-</span><span class="typ">ExtUtils</span><span class="pun">-</span><span class="typ">Embed</span><span class="pln"> xmlto</span>
<span class="pln">yum </span><span class="pun">-</span><span class="pln">y install audit</span><span class="pun">-</span><span class="pln">libs</span><span class="pun">-</span><span class="pln">devel binutils</span><span class="pun">-</span><span class="pln">devel elfutils</span><span class="pun">-</span><span class="pln">devel elfutils</span><span class="pun">-</span><span class="pln">libelf</span><span class="pun">-</span><span class="pln">devel</span>
<span class="pln">yum </span><span class="pun">-</span><span class="pln">y install newt</span><span class="pun">-</span><span class="pln">devel Python</span><span class="pun">-</span><span class="pln">devel zlib</span><span class="pun">-</span><span class="pln">devel bc</span>
2、准备内核的源代码组件
<span class="pln">cd </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">src</span>
<span class="pln">wget </span><span class="pun">-</span><span class="pln">c https</span><span class="pun">:</span><span class="com">//www.kernel.org/pub/linux/kernel/v3.x/linux-3.19.1.tar.xz</span>
<span class="pln">tar </span><span class="pun">-</span><span class="typ">Jxvf</span><span class="pln"> linux</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1.tar</span><span class="pun">.</span><span class="pln">xz</span>
<span class="pln">ln </span><span class="pun">-</span><span class="pln">s linux</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1</span><span class="pln"> linux</span>
<span class="pln">cd linux</span>
<span class="pln">make oldconfig </span><span class="com"># 验证和更新配置.config配置文件(如果只想在原来内核配置的基础上修改一些小地方)</span>
<span class="pln">make menuconfig </span><span class="com"># 简单图形化的内核配置管理</span>
3、此时,我们就可以开始编译新内核rpm包了
<span class="pln">make rpm </span><span class="com"># 自动生成rpmtree,kernel.spec文件,自动编译, 会生成debug包在内的所有rpm包组件。</span>
这个时候我们就等着吧,或者喝杯下午茶什么的。
好了, 我们找一下已经编译好的内核 rpm 包。
<span class="pln">cd </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">rpmbuild</span><span class="pun">/</span><span class="pln">RPMS</span><span class="pun">/</span><span class="str">`uname -m`</span><span class="pun">/</span>
<span class="pln">tree RPMS</span><span class="pun">/</span>
<span class="pln">RPMS</span><span class="pun">/</span>
<span class="pun">└──</span><span class="pln"> i386</span>
<span class="pun">├──</span><span class="pln"> kernel</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1</span><span class="pun">-</span><span class="lit">2.i386.rpm</span>
<span class="pun">├──</span><span class="pln"> kernel</span><span class="pun">-</span><span class="pln">devel</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1</span><span class="pun">-</span><span class="lit">2.i386.rpm</span>
<span class="pun">└──</span><span class="pln"> kernel</span><span class="pun">-</span><span class="pln">headers</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1</span><span class="pun">-</span><span class="lit">2.i386.rpm</span>
<span class="lit">1</span><span class="pln"> directory</span><span class="pun">,</span><span class="lit">3</span><span class="pln"> files</span>
4、安装新内核
<span class="pln">rpm </span><span class="pun">-</span><span class="typ">Uvh</span><span class="pln"> kernel</span><span class="pun">-*-.</span><span class="pln">rpm</span>
5、重启,验证内核版本
<span class="pln">uname </span><span class="pun">-</span><span class="pln">a </span>
二, rpm包深度定制化
1、我们先创建好rpm的“制作车间”(我觉得这样说可能更形象一些)。
<span class="pln">yum install rpmdevtools </span><span class="com"># rpm 制作车间快速创建工具</span>
<span class="pln">useradd </span><span class="pun">-</span><span class="pln">m builder </span><span class="com"># 新建一个临时用户以便创建 RPM 包,如果有错误发生,构建程序不会破坏现有的系统环境。</span>
<span class="pln">su </span><span class="pun">-</span><span class="pln"> builder</span>
<span class="pln">rpmdev</span><span class="pun">-</span><span class="pln">setuptree </span><span class="com"># 创建”制作车间“</span>
将会在builder用户的家目录下生成如下目录结构:
<span class="pln">tree </span><span class="pun">-</span><span class="pln">L </span><span class="lit">1</span><span class="pun">~</span><span class="str">/rpmbuild/</span>
<span class="pln">rpmbuild</span><span class="pun">/</span>
<span class="pun">├──</span><span class="pln"> BUILD</span>
<span class="pun">├──</span><span class="pln"> RPMS</span>
<span class="pun">├──</span><span class="pln"> SOURCES</span>
<span class="pun">├──</span><span class="pln"> SPECS</span>
<span class="pun">└──</span><span class="pln"> SRPMS</span>
2、编译前准备
此时我们需要包收集来的新版内核源码包放到 /home/builder/rpmbuild/SOURCES 目录下
<span class="pln">su </span><span class="pun">-</span><span class="pln"> builder</span>
<span class="pln">cd </span><span class="pun">~</span><span class="str">/rpmbuild/</span><span class="pln">SOURCES </span>
<span class="pln">wget </span><span class="pun">-</span><span class="pln">c https</span><span class="pun">:</span><span class="com">//www.kernel.org/pub/linux/kernel/v3.x/linux-3.19.1.tar.xz </span>
<span class="com"># 注意: rpmbuild 是通过gzip来做building前源码包解压工作的,所以需要把这个压缩包转换成.tar.gz格式。</span>
<span class="com"># 这里就不做记录了</span>
<span class="pln">cp </span><span class="pun">/</span><span class="pln">boot</span><span class="pun">/</span><span class="pln">config</span><span class="pun">-</span><span class="str">`uname -r`</span><span class="pun">.</span><span class="com"># 将现有的内核配置文件 config-`uname -r` 拷贝到 SOURCES 目录下</span>
切换到SPECS目录下, 生成 kernel.spec:
<span class="pln">tar </span><span class="typ">Jxvf</span><span class="pun">../</span><span class="pln">SOURCES</span><span class="pun">/</span><span class="pln">linux</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1.tar</span><span class="pun">.</span><span class="pln">xz </span><span class="pun">-</span><span class="pln">C </span><span class="pun">.</span>
<span class="pln">bash </span><span class="pun">../</span><span class="pln">linux</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1</span><span class="pun">/</span><span class="pln">scripts</span><span class="pun">/</span><span class="kwd">package</span><span class="pun">/</span><span class="pln">mkspec </span><span class="pun">>./</span><span class="pln">kernel</span><span class="pun">.</span><span class="pln">spec</span>
<span class="pln">rm </span><span class="pun">-</span><span class="pln">rf linux</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1</span>
<span class="com"># 这里需要我们手动利用源码包中提供的脚本生成 kernel.spec 文件</span>
<span class="com"># 记得生成kernel.spec完之后,清理掉 linux-3.19.1 文件夹</span>
<span class="pln">vim kernel</span><span class="pun">.</span><span class="pln">spec </span><span class="com"># 可以根据个人情况做深度定制化</span>
到此,我们的准备工作基本完成,目录结构及内容如下:
<span class="pln">tree rpmbuild</span><span class="pun">/</span><span class="pun">-</span><span class="pln">a</span>
<span class="pln">rpmbuild</span><span class="pun">/</span>
<span class="pun">├──</span><span class="pln"> BUILD</span>
<span class="pun">├──</span><span class="pln"> RPMS</span>
<span class="pun">├──</span><span class="pln"> SOURCES</span>
<span class="pun">│&</span><span class="pln">nbsp</span><span class="pun">;&</span><span class="pln">nbsp</span><span class="pun">;</span><span class="pun">├──</span><span class="pln"> config</span><span class="pun">-</span><span class="str">`uname -r`</span>
<span class="pun">│&</span><span class="pln">nbsp</span><span class="pun">;&</span><span class="pln">nbsp</span><span class="pun">;</span><span class="pun">└──</span><span class="pln"> linux</span><span class="pun">-</span><span class="lit">3.19</span><span class="pun">.</span><span class="lit">1.tar</span><span class="pun">.</span><span class="pln">gz</span>
<span class="pun">├──</span><span class="pln"> SPECS</span>
<span class="pun">│&</span><span class="pln">nbsp</span><span class="pun">;&</span><span class="pln">nbsp</span><span class="pun">;</span><span class="pun">└──</span><span class="pln"> kernel</span><span class="pun">.</span><span class="pln">spec</span>
<span class="pun">└──</span><span class="pln"> SRPMS</span>
<span class="lit">5</span><span class="pln"> directories</span><span class="pun">,</span><span class="lit">3</span><span class="pln"> files</span>
3、好了,现在我们正式开始编译内核的二进制rpm包:
<span class="pln">rpmbuild </span><span class="pun">-</span><span class="pln">bb </span><span class="pun">--</span><span class="kwd">with</span><span class="pln"> baseonly </span><span class="pun">--</span><span class="pln">without debug </span><span class="pun">--</span><span class="pln">without debuginfo </span><span class="pun">--</span><span class="pln">target</span><span class="pun">=</span><span class="str">`uname -m`</span><span class="pln"> kernel</span><span class="pun">.</span><span class="pln">spec</span>
rpmbuild 参数说明:
(1) --with <option> 指明编译某一种 kernel,可选的 option 有 baseonly、smponly 和 xenonly 等。
(2) --without <option> 指明去除一些功能模块,可选的 option 有 xen、smp、up、pae、kdump、debug 和 debuginfo 等。其中,debug 选项会剔除内核中的调试代码,debuginfo 选项会禁止建立 kernel-debuginfo 包。
完成以后,请到 ~/rpmbuild/RPMS/`uname -m`/ 目录中获取 rpm 安装包。
rpm 包 building 参考文档:
相关推荐
summerinsist 2020-08-21
窃破天道 2020-07-18
yishouwangnian 2020-06-21
OnMyHeart 2020-06-17
LychieFan 2020-06-16
服务器开发的日子 2020-06-14
Wytheme 2020-06-11
waitzkj 2020-06-09
邓博学习笔记 2020-06-08
secondid 2020-06-05
昭君出塞 2020-06-04
fushilin 2020-06-03
87901735 2020-06-02
heheeheh 2020-05-31
简单的快乐 2020-05-30
FCLAMP 2020-05-20
RayCongLiang 2020-05-18
zjuwangleicn 2020-05-18