特殊:Badtitle/NS100:Kernel/Compile/zh:修订间差异
无编辑摘要 |
无编辑摘要 |
||
(未显示3个用户的37个中间版本) | |||
第1行: | 第1行: | ||
{{Translation}} {{From|https://help.ubuntu.com/community/Kernel/Compile}} {{Languages|UbuntuHelp:Kernel/Compile}} //*^*本人只是个自由软件爱好者,是个菜鸟,译错莫怪。 | |||
[[1 | === 免责声明 === | ||
'''构建和使用一个定制的内核将使你的系统很难获得支持。编译你自己的内核(kernel)只能算是一个学习过程,所以针对你自己编译出来的内核去提交 BUG 是不允许的(如果你这么做,他们就会拒绝这个 BUG,也不做任何解释)'''如果你有一个与 Ubuntu/Canonical 签订的商业支持合同,你的自编译内核也不会在这种合同支持的范围之内。另外还请注意,本页介绍的是针对 Edgy (2.6.17) 或以上版本的内核!直到这个内核源之前,我们没有任何到位的机制来使人们容易的编译他们自己的内核。我们是有意为之。本文并'''没有'''介绍如何编译来自于 kernel.org 的[[UbuntuWiki:Forum/software/CustomKernel|原始内核]],而只是说明如何重建当前的 Ubuntu 内核源。 | |||
=== 编译内核的原因 === | |||
* 你是一个内核开发人员。 | |||
* 你需要以特定的方式编译内核,而官方并没有这样做(例如启用一些实验性的功能)。 | |||
* 你正试图调试一个关于原 Ubuntu 内核中你已经提交或即将提交的问题。 | |||
* 原 Ubuntu 内核不支持你的硬件。 | |||
=== 不建议编译内核的情况 === | |||
* 你只要编译一个特殊的驱动。对于这种情况,你只需安装 linux-headers 包。 | |||
* 你是个外行。要是失败,你自己又修复不了。最坏的情况,你不得不全部重装。<br> | |||
* 你碰巧看到本文,觉得挺有意思。相信我,这一点意思都没有:) | |||
如果你要安装一个新的不需要编译的内核,你可以用[[UbuntuHelp:Synaptic|新立得软件包管理器]]搜索你想要安装的 linux-image 和内核版本。<br> 一个更简单的方法是:单击系统 > 系统管理 > 更新管理器 > 单击“检查”按钮 > 接受包括 kernel 在内的所有更新。 | |||
=== 你需要的工具 === | |||
一开始,你需要安装一些新的包。 | |||
<pre>sudo apt-get install linux-kernel-devel fakeroot build-essential | |||
</pre> | |||
这会安装与编译器有关的包以及内核打包工具。它还会安装 git-core 包,它是处理 Ubuntu 内核源代码的最好工具。 | |||
=== 获得内核源代码 === | |||
这里有几种获得内核源代码的方法: | |||
1、使用 git(你可以在 [[UbuntuWiki:KernelGitGuide|Kernel Git Guide]] 找到详细说明)——这能使你总是保持与最新的 Ubuntu 内核源代码同步。 | |||
2、下载源文件存档——对于那些想要重建包括额外补丁的标准 Ubuntu 包的人,这是最简单和有用的,但要注意,这几乎总是滞后于最新开发源代码。如果你需要带最新补丁的内核源,你应该使用 git(选项1)。下载源文件的操作: | |||
<pre>sudo apt-get build-dep linux-source | |||
apt-get source linux-source | |||
</pre> | |||
对于Hardy (8.04)版本,命令变为: | |||
<pre>sudo apt-get build-dep linux-image-$(uname -r) | |||
apt-get source linux-image-$(uname -r) | |||
</pre> | |||
3、下载源码包(进一步的详细说明在这页下面[[AltBuildMethod Alternate Build Method: The Old-Fashioned Debian Way]])——请注意这跟选项 2 不一样。 | |||
=== 修改你所需的内核源文件 === | |||
对大多数人来说,简单的修改一下配置configs已经足够。如果你需要安装内核补丁,请阅读此补丁提供者的说明,已确定如何使用。 原Ubuntu configs是被安装在(''debian/config/ARCH/'')-ARCH目录是你刚刚建立的。在这个目录下有很多文件。这"config"文件是其架构中所有对象的基础。这儿有几个针对特定目标的config.FLAVOUR(config.xxx)。例如,下面是2.6.20,i386的文件: | |||
<pre>ls -l debian/config/i386/ | |||
total 108 | |||
-rw-r--r-- 1 root src 73962 2007-08-13 01:29 config | |||
-rw-r--r-- 1 root root 1369 2007-08-13 01:29 config.386 | |||
-rw-r--r-- 1 root root 1330 2007-08-13 01:29 config.generic | |||
-rw-r--r-- 1 root root 1395 2007-08-13 01:29 config.server | |||
-rw-r--r-- 1 root root 1756 2007-08-13 01:29 config.server-bigiron | |||
-rw-r--r-- 1 root root 8 2007-08-13 01:25 lowlatency | |||
-rw-r--r-- 1 root root 194 2007-08-13 01:25 vars.386 | |||
-rw-r--r-- 1 root root 218 2007-08-13 01:25 vars.server-bigiron | |||
</pre> | |||
'''如果你在debian/config中没有找到config配置文件,你会在/boot目录中找到它们,例如,/boot/config-2.6.22-14-generic。''' 如果你需要改变某一个配置选项,简单的修改包括这选项的文件。如果你修改"config"文件,会影响这结构中的所有对象。如果你修改其中一个对象配置,它仅仅影响此对象。 当你打了一个补丁,或者调整了配置之後,最好去重新生成配置文件以确保他们是一致的。 去重新生成所有的配置,请运行: | |||
<pre>debian/rules updateconfigs | |||
</pre> | |||
如果你仅仅想要更新一个配置,请运行: | |||
<pre>debian/scripts/misc/oldconfig ARCH | |||
</pre> | |||
=== 编译内核(仅针对内核源码来自于git 仓库,或是apt-get源) === | |||
去编译内核是非常简单的。根据你的需要,你可以编译一个通用内核,也可以编译一个只针对你系统的特殊内核。如果你想与原系统内核不冲突,(注意:这些命令是针对git-tree和从apt-get下载的源码,而不是针对来自于kernel官方源码包)使用以下命令编译一个普通内核: | |||
<pre>AUTOBUILD=1 fakeroot debian/rules binary-debs | |||
</pre> | |||
The ''AUTOBUILD'' environment variable triggers special features in the kernel build. First, it skips normal ABI checks (ABI is the binary compatibility). It can do this because it also creates a unique ABI ID. If you used a git repo, this unique ID is generated from the git HEAD SHA. If not, it is generated from the uuidgen program (which means every time you execute the debian/rules build, the UUID will be different!). Your packages will be named using this ID. To build a specific target, use this command: 在内核编译过程中,这“AUTOBUILD”环境变量具有特殊作用。首先,它跳过了ABI检查(ABI是二进制兼容性的缩写)。它可以这样做,因为它已经生成了一个唯一的ABI的编号。如果你使用一个git repo,这唯一的编号会被git HEAD SHA 生成。如果没有使用,他会被uuidgen程序生成(这就意味着你每次执行debian/rules编译命令,这唯一编号将是随机的!)。你的包将要使用已经生成的编号命名。 | |||
<pre>AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules binary-FLAVOUR | |||
</pre> | |||
Where FLAVOUR is one of the main flavours of the kernel (e.g. generic) To build one of the custom flavours (found in debian/binary-custom.d/), use: | |||
<pre>AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules custom-binary-FLAVOUR | |||
</pre> | |||
As of this documentation, custom flavours include xen and rt. If you have a more than one processor or more than one core, you can speed things up by running concurrent compile commands. Prepend CONCURRENCY_LEVEL=2 for two processors or two cores; replace '2' with whatever number suits your hardware setup (for Gutsy and later, you can alternatively use DEB_BUILD_OPTIONS=parallel=2). | |||
<pre>CONCURRENCY_LEVEL=2 AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules binary-generic | |||
</pre> | |||
To trigger a rebuild, remove the appropriate stamp file from debian/stamps (e.g. stamp-build-server for the server flavour, etc.). The debs are placed in your kernel directory's parent directory. [[Anchor(AltBuildMethod)|Anchor(AltBuildMethod)]] | |||
=== Alternate Build Method: The Old-Fashioned Debian Way === | |||
The new Ubuntu build system is great for developers, for people who need the absolute latest bleeding-edge kernel, and people who need to build a diverse set of kernels (several "flavours"). However it can be a little complex for ordinary users. If you don't need the latest development sources, there is a simpler way to compile your kernel from the linux-source package. As suggested above, all you need for this is: | |||
<pre>sudo apt-get install linux-source | |||
mkdir ~/src | |||
cd ~/src | |||
tar xjvf /usr/src/linux-source-<version-number-here>.tar.bz2 | |||
cd linux-source-<version-number-here> | |||
</pre> | |||
Now you are in the top directory of a kernel source tree. Before building the kernel, you must configure it. If you wish to re-use the configuration of your currently-running kernel, start with | |||
<pre>cp -vi /boot/config-`uname -r` .config | |||
</pre> | |||
Before you run 'make menuconfig' or 'make xconfig' (which is what the next step tells you to do), make sure you have the necessary packages | |||
<pre>sudo apt-get install qt3-dev-tools libqt3-mt-dev # if you plan to use 'make xconfig' | |||
sudo apt-get install libncurses5 libncurses5-dev # if you plan to use 'make menuconfig' | |||
</pre> | |||
Then, regardless of whether you're re-using existing configuration or starting from scratch: | |||
<pre>make menuconfig # or "make xconfig" if you prefer | |||
</pre> | |||
If you re-used the existing configuration, note that Ubuntu kernels build with debugging information on, which makes the resulting kernel modules (*.ko files) much larger than they would otherwise be. To turn this off, go into "Kernel hacking"; then, under "Kernel debugging", turn OFF "Compile the kernel with debug info". Now you can compile the kernel and create the packages: | |||
<pre>make-kpkg clean # only needed if you want to do a "clean" build | |||
fakeroot make-kpkg --initrd --append-to-version=-some-string-here kernel-image kernel-headers | |||
</pre> | |||
The *.deb packages will be created in the parent directory of your Linux source directory (in this example, it would be placed in ~/src because our Linux source directory is ~/src/linux-source-<version-number-here>). | |||
=== Install the new kernel === | |||
If you want to see the ubuntu splash screen (or use text mode) before you get to X instead of just a black screen, you'll want to make sure the framebuffer driver loads: | |||
<pre>echo vesafb | sudo tee -a /etc/initramfs-tools/modules | |||
echo fbcon | sudo tee -a /etc/initramfs-tools/modules | |||
</pre> | |||
Now that you've told initramfs-tools what modules it should include and the build is complete, you can install the generated debs using dpkg: | |||
<pre>sudo dpkg -i linux-image-2.6.20-16-2be-k7_2.6.20-16_i386.deb | |||
sudo dpkg -i linux-headers-2.6.20-16-2be-k7_2.6.20-16_i386.deb | |||
</pre> | |||
If you use modules from ''linux-restricted-modules'', you will need to recompile this against your new linux-headers package. | |||
=== Rebuilding ''linux-restricted-modules'' === | |||
The Linux-Restricted-Modules (l-r-m) package contains a number of non-DFSG-free drivers (as well as some firmware and the ipw3945 wireless networking daemon) which, in a perfect world, wouldn't have to be packaged separately, but which unfortunately are not available under a GPL-compatible license. If you use any of the hardware supported by the l-r-m package, you will likely find that your system does not work as well after switching to a custom kernel. In this case you should try to compile the l-r-m package. You will need to install the linux-headers packages that you built in the previous stage. | |||
<pre>sudo dpkg -i linux-source-2.6.20-2.6.20/debian/build/linux-headers*.deb | |||
</pre> | |||
==== Get the source ==== | |||
First, we make sure we have all the packages necessary to build l-r-m, and then we fetch the source: | |||
<pre>sudo apt-get build-dep linux-restricted-modules-common | |||
apt-get source linux-restricted-modules-common | |||
cd linux-restricted-modules-2.6.* | |||
</pre> | |||
==== Adjust debian/rules ==== | |||
Debian (and hence Ubuntu) packages are built by calling the the debian/rules script from the top source directory; this script is a specialized Makefile with 'targets' you can call as arguments when you run the script. The l-r-m debian/rules script is geared towards developers working with the official builds and makes certain assumptions about kernel version numbers and what header packages are installed; if you need to build l-r-m on your own, you will need to customize it. The full range of customizations are beyond the scope of this guide, but here are a couple examples of problems you're likely to run into and possible ways to solve them: ''Kernel Version Mismatch'' If you built your kernel using one of the recommended methods, your kernel version is probably not precisely what debian/rules expects; for instance, my git-built kernel is called 2.6.19-7-ref-generic instead of 2.6.19-7-generic, while my make-kpkg-built kernel is called 2.6.19-7-suspend2-generic. You can solve this problem by changing this line (the actual value here will be different by the time you read this): | |||
<pre>abi_version = 7 | |||
</pre> | |||
to something like this: | |||
<pre>abi_version = 7-ref | |||
</pre> | |||
After making this change, the control file will have to be rebuilt by running: | |||
<pre>debian/rules debian/control | |||
</pre> | |||
''Too Many Flavours'' debian/rules expects you to have the header files for all possible kernel flavours already installed. If you only compiled one kernel flavour, this is of course impossible. So you need to look in debian/rules and change all the "flavours" lines so that only one flavour is listed -- the one you have already compiled. so for instance in my case, working on the i386 architecture: | |||
<pre>flavours := $(addprefix $(kernel_abi_version)-,generic, server, lowlatency) | |||
</pre> | |||
became: | |||
<pre>flavours := $(addprefix $(kernel_abi_version)-,generic) | |||
</pre> | |||
You may find that further modifications are necessary. ''udebs mess you up'' The standard "binary" target includes building udebs, which look for flavours that you may not have installed. I did two things: | |||
# Modified debian/d-i/kernel-versions.in | |||
# deleted "build-udebs" from the binary target | |||
I'm not sure the latter was necessary, but as you are not likely to need to build any udebs it's probably pretty safe to do. Now cross your fingers and hope! Try to build with: | |||
<pre>fakeroot debian/rules binary | |||
</pre> | |||
Note: you will need around 8 hours of compilation time and around 10 Gb of hard drive space to compile all kernel flavours and a restricted modules. | |||
=== Speeding Up The Build === | |||
Use <code><nowiki>distcc</nowiki></code> and, if you're rebuilding often, <code><nowiki>ccache</nowiki></code>. A good overview of using <code><nowiki>distcc</nowiki></code> on a debian-based system is available at http://myrddin.org/howto/using-distcc-with-debian. If you have AMD64 machines available on your local net, they can still participate in building 32-bit code; <code><nowiki>distcc</nowiki></code> seems to handle that automatically. However, with <code><nowiki>distcc</nowiki></code> taking over all compiles by default, you will need to set <code><nowiki>HOSTCC</nowiki></code> so that when kernel builds want to use the compiler on the host itself, they don't end up distributing jobs to the 64-bit server. If you fail to do that, you'll get link-compatibility failures between 64-bit and 32-bit code. My <code><nowiki>make-kpkg</nowiki></code> command, with /usr/lib/ccache at the head of my <code><nowiki>$PATH</nowiki></code>, looks like: | |||
<pre>MAKEFLAGS="HOSTCC=/usr/bin/gcc CCACHE_PREFIX=distcc" make-kpkg --rootcmd fakeroot --initrd --append-to-version=-suspend2 kernel-image kernel-headers kernel-source | |||
</pre> | |||
=== More documentation === | |||
* https://help.ubuntu.com/community/forum/software/CustomKernel | |||
* [[UbuntuHelp:UpgradeKernel|UpgradeKernel]] , with easier ways. | |||
=== Comments === | |||
Please go to the community wiki page for comments, questions and discussion: https://wiki.ubuntu.com/KernelCustomBuild | |||
=== External information === | |||
* http://www.howtoforge.com/kernel_compilation_ubuntu Compile a kernel from kernel.org source in Ubuntu | |||
---- | |||
[[Category:CategoryKernel]] [[Category:CategoryDocumentation]] [[Category:UbuntuHelp]] |
2008年8月26日 (二) 17:12的最新版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/Kernel/Compile }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/af | • {{#if: UbuntuHelp:Kernel/Compile|Afrikaans| [[::Kernel/Compile/zh/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ar | • {{#if: UbuntuHelp:Kernel/Compile|العربية| [[::Kernel/Compile/zh/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/az | • {{#if: UbuntuHelp:Kernel/Compile|azərbaycanca| [[::Kernel/Compile/zh/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/bcc | • {{#if: UbuntuHelp:Kernel/Compile|جهلسری بلوچی| [[::Kernel/Compile/zh/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/bg | • {{#if: UbuntuHelp:Kernel/Compile|български| [[::Kernel/Compile/zh/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/br | • {{#if: UbuntuHelp:Kernel/Compile|brezhoneg| [[::Kernel/Compile/zh/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ca | • {{#if: UbuntuHelp:Kernel/Compile|català| [[::Kernel/Compile/zh/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/cs | • {{#if: UbuntuHelp:Kernel/Compile|čeština| [[::Kernel/Compile/zh/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/de | • {{#if: UbuntuHelp:Kernel/Compile|Deutsch| [[::Kernel/Compile/zh/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/el | • {{#if: UbuntuHelp:Kernel/Compile|Ελληνικά| [[::Kernel/Compile/zh/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/es | • {{#if: UbuntuHelp:Kernel/Compile|español| [[::Kernel/Compile/zh/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/fa | • {{#if: UbuntuHelp:Kernel/Compile|فارسی| [[::Kernel/Compile/zh/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/fi | • {{#if: UbuntuHelp:Kernel/Compile|suomi| [[::Kernel/Compile/zh/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/fr | • {{#if: UbuntuHelp:Kernel/Compile|français| [[::Kernel/Compile/zh/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/gu | • {{#if: UbuntuHelp:Kernel/Compile|ગુજરાતી| [[::Kernel/Compile/zh/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/he | • {{#if: UbuntuHelp:Kernel/Compile|עברית| [[::Kernel/Compile/zh/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/hu | • {{#if: UbuntuHelp:Kernel/Compile|magyar| [[::Kernel/Compile/zh/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/id | • {{#if: UbuntuHelp:Kernel/Compile|Bahasa Indonesia| [[::Kernel/Compile/zh/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/it | • {{#if: UbuntuHelp:Kernel/Compile|italiano| [[::Kernel/Compile/zh/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ja | • {{#if: UbuntuHelp:Kernel/Compile|日本語| [[::Kernel/Compile/zh/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ko | • {{#if: UbuntuHelp:Kernel/Compile|한국어| [[::Kernel/Compile/zh/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ksh | • {{#if: UbuntuHelp:Kernel/Compile|Ripoarisch| [[::Kernel/Compile/zh/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/mr | • {{#if: UbuntuHelp:Kernel/Compile|मराठी| [[::Kernel/Compile/zh/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ms | • {{#if: UbuntuHelp:Kernel/Compile|Bahasa Melayu| [[::Kernel/Compile/zh/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/nl | • {{#if: UbuntuHelp:Kernel/Compile|Nederlands| [[::Kernel/Compile/zh/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/no | • {{#if: UbuntuHelp:Kernel/Compile|norsk| [[::Kernel/Compile/zh/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/oc | • {{#if: UbuntuHelp:Kernel/Compile|occitan| [[::Kernel/Compile/zh/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/pl | • {{#if: UbuntuHelp:Kernel/Compile|polski| [[::Kernel/Compile/zh/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/pt | • {{#if: UbuntuHelp:Kernel/Compile|português| [[::Kernel/Compile/zh/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ro | • {{#if: UbuntuHelp:Kernel/Compile|română| [[::Kernel/Compile/zh/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/ru | • {{#if: UbuntuHelp:Kernel/Compile|русский| [[::Kernel/Compile/zh/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/si | • {{#if: UbuntuHelp:Kernel/Compile|සිංහල| [[::Kernel/Compile/zh/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/sq | • {{#if: UbuntuHelp:Kernel/Compile|shqip| [[::Kernel/Compile/zh/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/sr | • {{#if: UbuntuHelp:Kernel/Compile|српски / srpski| [[::Kernel/Compile/zh/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/sv | • {{#if: UbuntuHelp:Kernel/Compile|svenska| [[::Kernel/Compile/zh/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/th | • {{#if: UbuntuHelp:Kernel/Compile|ไทย| [[::Kernel/Compile/zh/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/tr | • {{#if: UbuntuHelp:Kernel/Compile|Türkçe| [[::Kernel/Compile/zh/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/vi | • {{#if: UbuntuHelp:Kernel/Compile|Tiếng Việt| [[::Kernel/Compile/zh/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/yue | • {{#if: UbuntuHelp:Kernel/Compile|粵語| [[::Kernel/Compile/zh/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/zh | • {{#if: UbuntuHelp:Kernel/Compile|中文| [[::Kernel/Compile/zh/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/zh-hans | • {{#if: UbuntuHelp:Kernel/Compile|中文(简体)| [[::Kernel/Compile/zh/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:Kernel/Compile | UbuntuHelp:Kernel/Compile | {{#if: | :}}Kernel/Compile/zh}}/zh-hant | • {{#if: UbuntuHelp:Kernel/Compile|中文(繁體)| [[::Kernel/Compile/zh/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:Kernel/Compile|:Kernel/Compile/zh|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :Kernel/Compile/zh/zh | | {{#ifexist: Kernel/Compile/zh/zh | | {{#ifeq: {{#titleparts:Kernel/Compile/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:Kernel/Compile/zh|1|-1|}} | zh | | }}
//*^*本人只是个自由软件爱好者,是个菜鸟,译错莫怪。
免责声明
构建和使用一个定制的内核将使你的系统很难获得支持。编译你自己的内核(kernel)只能算是一个学习过程,所以针对你自己编译出来的内核去提交 BUG 是不允许的(如果你这么做,他们就会拒绝这个 BUG,也不做任何解释)如果你有一个与 Ubuntu/Canonical 签订的商业支持合同,你的自编译内核也不会在这种合同支持的范围之内。另外还请注意,本页介绍的是针对 Edgy (2.6.17) 或以上版本的内核!直到这个内核源之前,我们没有任何到位的机制来使人们容易的编译他们自己的内核。我们是有意为之。本文并没有介绍如何编译来自于 kernel.org 的原始内核,而只是说明如何重建当前的 Ubuntu 内核源。
编译内核的原因
- 你是一个内核开发人员。
- 你需要以特定的方式编译内核,而官方并没有这样做(例如启用一些实验性的功能)。
- 你正试图调试一个关于原 Ubuntu 内核中你已经提交或即将提交的问题。
- 原 Ubuntu 内核不支持你的硬件。
不建议编译内核的情况
- 你只要编译一个特殊的驱动。对于这种情况,你只需安装 linux-headers 包。
- 你是个外行。要是失败,你自己又修复不了。最坏的情况,你不得不全部重装。
- 你碰巧看到本文,觉得挺有意思。相信我,这一点意思都没有:)
如果你要安装一个新的不需要编译的内核,你可以用新立得软件包管理器搜索你想要安装的 linux-image 和内核版本。
一个更简单的方法是:单击系统 > 系统管理 > 更新管理器 > 单击“检查”按钮 > 接受包括 kernel 在内的所有更新。
你需要的工具
一开始,你需要安装一些新的包。
sudo apt-get install linux-kernel-devel fakeroot build-essential
这会安装与编译器有关的包以及内核打包工具。它还会安装 git-core 包,它是处理 Ubuntu 内核源代码的最好工具。
获得内核源代码
这里有几种获得内核源代码的方法:
1、使用 git(你可以在 Kernel Git Guide 找到详细说明)——这能使你总是保持与最新的 Ubuntu 内核源代码同步。
2、下载源文件存档——对于那些想要重建包括额外补丁的标准 Ubuntu 包的人,这是最简单和有用的,但要注意,这几乎总是滞后于最新开发源代码。如果你需要带最新补丁的内核源,你应该使用 git(选项1)。下载源文件的操作:
sudo apt-get build-dep linux-source apt-get source linux-source
对于Hardy (8.04)版本,命令变为:
sudo apt-get build-dep linux-image-$(uname -r) apt-get source linux-image-$(uname -r)
3、下载源码包(进一步的详细说明在这页下面AltBuildMethod Alternate Build Method: The Old-Fashioned Debian Way)——请注意这跟选项 2 不一样。
修改你所需的内核源文件
对大多数人来说,简单的修改一下配置configs已经足够。如果你需要安装内核补丁,请阅读此补丁提供者的说明,已确定如何使用。 原Ubuntu configs是被安装在(debian/config/ARCH/)-ARCH目录是你刚刚建立的。在这个目录下有很多文件。这"config"文件是其架构中所有对象的基础。这儿有几个针对特定目标的config.FLAVOUR(config.xxx)。例如,下面是2.6.20,i386的文件:
ls -l debian/config/i386/ total 108 -rw-r--r-- 1 root src 73962 2007-08-13 01:29 config -rw-r--r-- 1 root root 1369 2007-08-13 01:29 config.386 -rw-r--r-- 1 root root 1330 2007-08-13 01:29 config.generic -rw-r--r-- 1 root root 1395 2007-08-13 01:29 config.server -rw-r--r-- 1 root root 1756 2007-08-13 01:29 config.server-bigiron -rw-r--r-- 1 root root 8 2007-08-13 01:25 lowlatency -rw-r--r-- 1 root root 194 2007-08-13 01:25 vars.386 -rw-r--r-- 1 root root 218 2007-08-13 01:25 vars.server-bigiron
如果你在debian/config中没有找到config配置文件,你会在/boot目录中找到它们,例如,/boot/config-2.6.22-14-generic。 如果你需要改变某一个配置选项,简单的修改包括这选项的文件。如果你修改"config"文件,会影响这结构中的所有对象。如果你修改其中一个对象配置,它仅仅影响此对象。 当你打了一个补丁,或者调整了配置之後,最好去重新生成配置文件以确保他们是一致的。 去重新生成所有的配置,请运行:
debian/rules updateconfigs
如果你仅仅想要更新一个配置,请运行:
debian/scripts/misc/oldconfig ARCH
编译内核(仅针对内核源码来自于git 仓库,或是apt-get源)
去编译内核是非常简单的。根据你的需要,你可以编译一个通用内核,也可以编译一个只针对你系统的特殊内核。如果你想与原系统内核不冲突,(注意:这些命令是针对git-tree和从apt-get下载的源码,而不是针对来自于kernel官方源码包)使用以下命令编译一个普通内核:
AUTOBUILD=1 fakeroot debian/rules binary-debs
The AUTOBUILD environment variable triggers special features in the kernel build. First, it skips normal ABI checks (ABI is the binary compatibility). It can do this because it also creates a unique ABI ID. If you used a git repo, this unique ID is generated from the git HEAD SHA. If not, it is generated from the uuidgen program (which means every time you execute the debian/rules build, the UUID will be different!). Your packages will be named using this ID. To build a specific target, use this command: 在内核编译过程中,这“AUTOBUILD”环境变量具有特殊作用。首先,它跳过了ABI检查(ABI是二进制兼容性的缩写)。它可以这样做,因为它已经生成了一个唯一的ABI的编号。如果你使用一个git repo,这唯一的编号会被git HEAD SHA 生成。如果没有使用,他会被uuidgen程序生成(这就意味着你每次执行debian/rules编译命令,这唯一编号将是随机的!)。你的包将要使用已经生成的编号命名。
AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules binary-FLAVOUR
Where FLAVOUR is one of the main flavours of the kernel (e.g. generic) To build one of the custom flavours (found in debian/binary-custom.d/), use:
AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules custom-binary-FLAVOUR
As of this documentation, custom flavours include xen and rt. If you have a more than one processor or more than one core, you can speed things up by running concurrent compile commands. Prepend CONCURRENCY_LEVEL=2 for two processors or two cores; replace '2' with whatever number suits your hardware setup (for Gutsy and later, you can alternatively use DEB_BUILD_OPTIONS=parallel=2).
CONCURRENCY_LEVEL=2 AUTOBUILD=1 NOEXTRAS=1 fakeroot debian/rules binary-generic
To trigger a rebuild, remove the appropriate stamp file from debian/stamps (e.g. stamp-build-server for the server flavour, etc.). The debs are placed in your kernel directory's parent directory. Anchor(AltBuildMethod)
Alternate Build Method: The Old-Fashioned Debian Way
The new Ubuntu build system is great for developers, for people who need the absolute latest bleeding-edge kernel, and people who need to build a diverse set of kernels (several "flavours"). However it can be a little complex for ordinary users. If you don't need the latest development sources, there is a simpler way to compile your kernel from the linux-source package. As suggested above, all you need for this is:
sudo apt-get install linux-source mkdir ~/src cd ~/src tar xjvf /usr/src/linux-source-<version-number-here>.tar.bz2 cd linux-source-<version-number-here>
Now you are in the top directory of a kernel source tree. Before building the kernel, you must configure it. If you wish to re-use the configuration of your currently-running kernel, start with
cp -vi /boot/config-`uname -r` .config
Before you run 'make menuconfig' or 'make xconfig' (which is what the next step tells you to do), make sure you have the necessary packages
sudo apt-get install qt3-dev-tools libqt3-mt-dev # if you plan to use 'make xconfig' sudo apt-get install libncurses5 libncurses5-dev # if you plan to use 'make menuconfig'
Then, regardless of whether you're re-using existing configuration or starting from scratch:
make menuconfig # or "make xconfig" if you prefer
If you re-used the existing configuration, note that Ubuntu kernels build with debugging information on, which makes the resulting kernel modules (*.ko files) much larger than they would otherwise be. To turn this off, go into "Kernel hacking"; then, under "Kernel debugging", turn OFF "Compile the kernel with debug info". Now you can compile the kernel and create the packages:
make-kpkg clean # only needed if you want to do a "clean" build fakeroot make-kpkg --initrd --append-to-version=-some-string-here kernel-image kernel-headers
The *.deb packages will be created in the parent directory of your Linux source directory (in this example, it would be placed in ~/src because our Linux source directory is ~/src/linux-source-<version-number-here>).
Install the new kernel
If you want to see the ubuntu splash screen (or use text mode) before you get to X instead of just a black screen, you'll want to make sure the framebuffer driver loads:
echo vesafb | sudo tee -a /etc/initramfs-tools/modules echo fbcon | sudo tee -a /etc/initramfs-tools/modules
Now that you've told initramfs-tools what modules it should include and the build is complete, you can install the generated debs using dpkg:
sudo dpkg -i linux-image-2.6.20-16-2be-k7_2.6.20-16_i386.deb sudo dpkg -i linux-headers-2.6.20-16-2be-k7_2.6.20-16_i386.deb
If you use modules from linux-restricted-modules, you will need to recompile this against your new linux-headers package.
Rebuilding linux-restricted-modules
The Linux-Restricted-Modules (l-r-m) package contains a number of non-DFSG-free drivers (as well as some firmware and the ipw3945 wireless networking daemon) which, in a perfect world, wouldn't have to be packaged separately, but which unfortunately are not available under a GPL-compatible license. If you use any of the hardware supported by the l-r-m package, you will likely find that your system does not work as well after switching to a custom kernel. In this case you should try to compile the l-r-m package. You will need to install the linux-headers packages that you built in the previous stage.
sudo dpkg -i linux-source-2.6.20-2.6.20/debian/build/linux-headers*.deb
Get the source
First, we make sure we have all the packages necessary to build l-r-m, and then we fetch the source:
sudo apt-get build-dep linux-restricted-modules-common apt-get source linux-restricted-modules-common cd linux-restricted-modules-2.6.*
Adjust debian/rules
Debian (and hence Ubuntu) packages are built by calling the the debian/rules script from the top source directory; this script is a specialized Makefile with 'targets' you can call as arguments when you run the script. The l-r-m debian/rules script is geared towards developers working with the official builds and makes certain assumptions about kernel version numbers and what header packages are installed; if you need to build l-r-m on your own, you will need to customize it. The full range of customizations are beyond the scope of this guide, but here are a couple examples of problems you're likely to run into and possible ways to solve them: Kernel Version Mismatch If you built your kernel using one of the recommended methods, your kernel version is probably not precisely what debian/rules expects; for instance, my git-built kernel is called 2.6.19-7-ref-generic instead of 2.6.19-7-generic, while my make-kpkg-built kernel is called 2.6.19-7-suspend2-generic. You can solve this problem by changing this line (the actual value here will be different by the time you read this):
abi_version = 7
to something like this:
abi_version = 7-ref
After making this change, the control file will have to be rebuilt by running:
debian/rules debian/control
Too Many Flavours debian/rules expects you to have the header files for all possible kernel flavours already installed. If you only compiled one kernel flavour, this is of course impossible. So you need to look in debian/rules and change all the "flavours" lines so that only one flavour is listed -- the one you have already compiled. so for instance in my case, working on the i386 architecture:
flavours := $(addprefix $(kernel_abi_version)-,generic, server, lowlatency)
became:
flavours := $(addprefix $(kernel_abi_version)-,generic)
You may find that further modifications are necessary. udebs mess you up The standard "binary" target includes building udebs, which look for flavours that you may not have installed. I did two things:
- Modified debian/d-i/kernel-versions.in
- deleted "build-udebs" from the binary target
I'm not sure the latter was necessary, but as you are not likely to need to build any udebs it's probably pretty safe to do. Now cross your fingers and hope! Try to build with:
fakeroot debian/rules binary
Note: you will need around 8 hours of compilation time and around 10 Gb of hard drive space to compile all kernel flavours and a restricted modules.
Speeding Up The Build
Use distcc
and, if you're rebuilding often, ccache
. A good overview of using distcc
on a debian-based system is available at http://myrddin.org/howto/using-distcc-with-debian. If you have AMD64 machines available on your local net, they can still participate in building 32-bit code; distcc
seems to handle that automatically. However, with distcc
taking over all compiles by default, you will need to set HOSTCC
so that when kernel builds want to use the compiler on the host itself, they don't end up distributing jobs to the 64-bit server. If you fail to do that, you'll get link-compatibility failures between 64-bit and 32-bit code. My make-kpkg
command, with /usr/lib/ccache at the head of my $PATH
, looks like:
MAKEFLAGS="HOSTCC=/usr/bin/gcc CCACHE_PREFIX=distcc" make-kpkg --rootcmd fakeroot --initrd --append-to-version=-suspend2 kernel-image kernel-headers kernel-source
More documentation
- https://help.ubuntu.com/community/forum/software/CustomKernel
- UpgradeKernel , with easier ways.
Comments
Please go to the community wiki page for comments, questions and discussion: https://wiki.ubuntu.com/KernelCustomBuild
External information
- http://www.howtoforge.com/kernel_compilation_ubuntu Compile a kernel from kernel.org source in Ubuntu