Kernels/Compilation/Traditional (简体中文)

From ArchWiki
< Kernels‎ | Compilation
Revision as of 01:32, 6 December 2012 by (talk | contribs) (use https for links to
Jump to: navigation, search


附注: please use the first argument of the template to provide more detailed indications.

以下的一些步骤有助于你从 源代码 编译自己的内核。这种方法是一种对所有发行版均适用的传统方法;但同时,本文也包含了用 makepkg 和 pacman 干净安装自己的内核的优秀方法。

另外你可以选择用 ABS 工具 建立并安装你的内核;参见 用ABS工具编译内核。使用已有的 linux PKGBUILD 文件可以自动化大多数过程并生成一个软件包,然而,一些 Arch 使用者更喜欢用传统方法。



$ wget -c
  • 将源码拷贝到你的家目录:
$ cp linux-3.2.9.tar.bz2 ~/kernelbuild/
  • 解压源码:
$ cd ~/kernelbuild
$ tar -xvjf linux-3.2.9.tar.bz2
$ cd ~/kernelbuild/linux-3.2.9


This is the most crucial step in customizing the kernel to reflect your computer's precise specifications. By setting the configurations in 'menuconfig' properly, your kernel and computer will function most efficiently.

获取 .config 文件

如果你想使用Arch 的默认配置,可以拷贝你现在正在运行的内核的 .config 设置文件。

 $ zcat /proc/config.gz > .config


There are two choice:

  • Traditional menuconfig
$ make menuconfig        (Will start with a fresh '.config'.  Option dependencies are usually automatically selected.)

适当更改内核设置并保存。如果你希望通过多次配置探索达到最优配置的话,每次将你的内核配置文件备份将是明智之举。 If you cannot boot your newly built kernel see the list of necessary config items here. Running $ lspci -k # from liveCD lists names of kernel modules in use.

  • localmodconfig

Since kernel 2.6.32, localmodconfig is provided to ease kernel configuration:

$ make oldconfig         (仅当你已经拷贝了内核配置文件。Also marks previously unused options as 'NEW'.)
$ make localmodconfig    (Tries to extract /proc/config.gz from running kernel. Pre-selecting options/modules in use.)
$ make localyesconfig    (Same as above, except that as many modules as possible compiled into the kernel.)
$ make xconfig           (Depends on Qt. A nicer interface. Dependency checking not verified.)
$ make gconfig           (Depends on GTK.  Otherwise same as xconfig.)
$ make help              (Lists ALL targets available.)

For more information about the build target "localmodconfig" refer to the 2.6.32 release notes.

Local version


$ make menuconfig
一般设置  --->
 (-ARCH) Local version - append to kernel release

What about /usr/src/ ?

用/usr/src/目录编译,连同建立连接这种方法已经不用了,很多 hackers 并不赞同这种方法。最洁净的方法是简单地利用自己的家目录,作为一个普通用户设置并编译自己的内核,以root身份安装,或者用 makepkg 和pacman (上面提过)




警告: 如果你用的是 GRUB,并且同时装有 LILO,不要运行 make all ; 因它最后会设置 LILO,这样你可能再也不能启动你的机器了。如果你使用的是 GRUB, 先卸载 LILO(pacman -R lilo),然后执行make all
  • 开始编译。
$ make      (Same as make vmlinux && make modules && make bzImage - see 'make help' for more information on this.)


$ make -jN   (N = # of processors + 1) (This utilizes all CPUs at 100%  A Dual-core[-j3] 2.8Ghz compiled in less than 15 minutes.)



# make modules_install

This copies the compiled modules into a directory in /lib/modules named by the kernel version and appended string you set in menuconfig. This way, modules are kept separate from those used by other kernels on your machine.

拷贝内核到 /boot 目录

# cp -v arch/i386/boot/bzImage /boot/vmlinuz-2.6.27-revision1


The initial RAM disk (initrd option in the GRUB menu, or, the file "initramfs-YourKernelName.img") is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system. The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel. In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system.

If you need any modules loaded in order to mount the root filesystem, build a ramdisk (most users need this). The -k parameter accepts the kernel version and appended string you set in menuconfig and is used to locate the modules in /lib/modules:

# mkinitcpio -k FullKernelName -g /boot/initramfs-YourKernelName.img

You are free to name the /boot files anything you want. However, using the [kernel-major-minor-revision] naming scheme helps to keep order if you: Keep multiple kernels/ Use mkinitcpio often/ Build third-party modules.

If you are using LILO and it cannot communicate with the kernel device-mapper driver, you have to run modprobe dm-mod first.


The file is not required for booting Linux. It is a type of "phone directory" list of functions in a particular build of a kernel. The contains a list of kernel symbols (i.e function names, variable names etc) and their corresponding addresses. This "symbol-name to address mapping" is used by:

  • Some processes like klogd, ksymoops etc
  • By OOPS handler when information has to be dumped to the screen during a kernel crash (i.e info like in which function it has crashed).

Copy to /boot and create symlink

# cp /boot/

After completing all steps above, you should have the following 3 files and 1 soft symlink in your /boot directory along with any other previously existing files:

vmlinuz-YourKernelName          (Kernel)
initramfs-YourKernelName.img    (Ramdisk)       (System Map)

Bootloader 设置

Add an entry for your amazing new kernel in your bootloader's configuration file - see GRUB or LILO for examples. 另外,如果你用的是 LILO,则安装脚本直接会自动拷贝相应文件并且设置LILO:

$ arch/i386/boot/


Using the NVIDIA video driver with your custom kernel

想在你自己编译的内核下用nVIDIA 视频驱动,参见: 如何为定制内核安装 nVIDIA 驱动.