如何获取CentOS内核源码。参阅http://wiki.centos.org/HowTos/I_need_the_Kernel_Sourc

本文介绍如何在CentOS 5和CentOS 6上安装内核开发包及完整内核源码,适用于需要编译内核模块或进行内核定制的场景。文章详细解释了安装不同版本内核-devel包的方法,并提供了获取完整内核源码的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Maybe you do not need the full kernel source

If you need to compile a kernel driver module, the chances are you do not really need the full kernel source tree. You might just need the kernel-devel package. (If, however, you are certain that the full source tree is required, please follow the instructions in Section 2.)

In CentOS 6, there is just one kernel-devel package available:

  • kernel-devel (both 32- & 64-bit architectures)

In CentOS 5, there are three kernel-devel packages available:

  • kernel-devel (both 32- & 64-bit architectures)

  • kernel-xen-devel (both 32- & 64-bit architectures)

  • kernel-PAE-devel (32-bit architecture only)

If you are running the standard kernel (for example), you can install the kernel-devel package by:

[root@host]# yum install kernel-devel

You can use this command to determine the version of your running kernel:

[root@host]# uname -r

The result will look similar to this:

2.6.18-348.16.1.el5xen

In this case, the xen kernel is installed and the way to install this specific kernel-devel package is:

[root@host]# yum install kernel-xen-devel

<!> Please ensure that you install the kernel-devel package that matches your running kernel. See this FAQ for details.

If your kernel is not listed by yum because it is in an older tree, you can download it manually from the CentOS Vault. Pick the version of CentOS you are interested in and then, for the arch, look in either the os/arch/CentOS/RPMS/ or the updates/arch/RPMS/ directories for the kernel[-type]-devel-version.arch.rpm

Once you have the proper kernel[-type]-devel-version.arch.rpm installed, try to compile your module. It should work this way. If it does not, please provide feedback to the module's developer as this is the way all new kernel modules should be designed to be built.

2. If you really need the full kernel source

If you really must have the kernel source tree, for whatever reason, it is obtainable.

As an ordinary user, not root, create a build tree based on a ~/rpmbuild/ directory:

[user@host]$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
[user@host]$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

<!> You are strongly advised against package building as root. (See: Building Source RPM as non-root under CentOS)


To install the source package and tools for CentOS 6:

As root, install the rpm-build, redhat-rpm-config, asciidoc, hmaccalc, perl-ExtUtils-Embed, xmlto, binutils-devel, elfutils-libelf-devel, newt-devel, python-devel and zlib-devel packages:

[root@host]# yum install rpm-build redhat-rpm-config asciidoc hmaccalc perl-ExtUtils-Embed xmlto 
[root@host]# yum install binutils-devel elfutils-libelf-devel newt-devel python-devel zlib-devel 

Find the kernel source rpm package in:

(Replace the "N" with the relevant update version number.)

As an ordinary user, not root, install the source package by executing:

[user@host]$ rpm -i http://vault.centos.org/6.4/updates/Source/SPackages/kernel-2.6.32-358.14.1.el6.src.rpm 2>&1 | grep -v mock


To install the source package and tools for CentOS 5:

As root, install the rpm-build, redhat-rpm-config and unifdef packages:

[root@host]# yum install rpm-build redhat-rpm-config unifdef

Find the kernel source rpm package in:

(Replace the "N" with the relevant update version number.)

As an ordinary user, not root, install the source package by executing:

[user@host]$ rpm -i http://vault.centos.org/5.9/updates/SRPMS/kernel-2.6.18-348.16.1.el5.src.rpm 2>&1 | grep -v mock


Now that the source package and tools are installed, unpack and prepare the source files:

[user@host]$ cd ~/rpmbuild/SPECS
[user@host SPECS]$ rpmbuild -bp --target=$(uname -m) kernel.spec

The value of $(uname -m) sets the target to the architecture of your current kernel. This is generally accepted, as most people will need either i686 or x86_64 as the target.

The kernel source tree will now be found under the ~/rpmbuild/BUILD/kernel*/linux*/ directory.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值