天天看點

精簡LINUX核心配置及快速編譯的方法

如果經常編譯新核心(不管是什麼目的),或者需要修改核心的某些代碼做測試,雖然make會選擇的編譯有必要重新編譯的部分,但是如果修改了某個核心的頭檔案。可能需要重新編譯很多内容,是以把不需要使用的子產品不編譯是能節省不少時間的。

編譯核心大部分時間都在編譯子產品上,比如我的機器:

find /lib/modules/2.6.37-rc5+/ -name “*.ko”|wc -l

2374

我的目标就是删除這些不用的子產品, 不是顯示的删除不用的子產品,而是提取出需要的子產品

如果系統連續運作足夠長的時間,現在被加載的就是需要的,否則就不需要, 把該運作的程式運作一下就能做到,比如nf_nat,kvm_intel等。

通過這種方式,新核心至少會包含相應的子產品,如果發現有遺漏(比如iptables提示找不到某個别的子產品),再運作一次就能做到了

1. 首先通過/proc/modules得到所有的子產品名

2. vi 編輯

%s//n/|/g

去掉最後的|

%s/[-_]/[-_]/g

再加點東西, 轉化為如下格式的modules.list

#!/bin/sh

egrep ‘=.*/<(ipt[-_]MASQUERADE|iptable[-_]nat|nf[-_]nat|nf[-_]conntrack[-_]ipv4|nf[-_]conntrack|nf[-_]defrag[-_]ipv4|ip[-_]tables|x[-_]tables|binfmt[-_]misc|rfcomm|sco|bridge|ppdev|stp|bnep|l2cap|nouveau|ttm|drm[-_]kms[-_]helper|drm|i2c[-_]algo[-_]bit|kvm[-_]intel|kvm|nfsd|exportfs|nfs|lockd|nfs[-_]acl|auth[-_]rpcgss|nls[-_]iso8859[-_]1|nls[-_]cp437|vfat|fat|sunrpc|snd[-_]hda[-_]codec[-_]analog|arc4|iwl3945|iwlcore|snd[-_]hda[-_]intel|snd[-_]hda[-_]codec|snd[-_]hwdep|snd[-_]pcm[-_]oss|snd[-_]mixer[-_]oss|mac80211|snd[-_]pcm|snd[-_]seq[-_]dummy|snd[-_]seq[-_]oss|snd[-_]seq[-_]midi|pcmcia|cfg80211|snd[-_]rawmidi|snd[-_]seq[-_]midi[-_]event|joydev|snd[-_]seq|btusb|uvcvideo|snd[-_]timer|bluetooth|snd[-_]seq[-_]device|lp|video|yenta[-_]socket|pcmcia[-_]rsrc|psmouse|tpm[-_]tis|videodev|intel[-_]agp|tpm|usbhid|thinkpad[-_]acpi|pcmcia[-_]core|tpm[-_]bios|nvram|hid|intel[-_]gtt|parport|v4l1[-_]compat|serio[-_]raw|snd|sdhci[-_]pci|sdhci|output|soundcore|snd[-_]page[-_]alloc|e1000e)/.o/>’

3. chmod +x modules.list

4. cd /lib/modules/`uname -r`/source

5. find . -name Makefile|xargs  cat |./modules.list  |egrep -o ‘CONFIG_[A-Za-z_0-9]*’|sort -u |sed -e ‘s/.*/&=m/’ >modules_used

6. cp .config config_new

7. vi config_new, %s/^CONFIG_.*=m$//

8 . cat modules_used >> config_new

config_new 就得到新的配置檔案了。

我實驗了一下, 編譯時間從49下降到11分鐘(real time)

find /lib/modules/2.6.37-rc6+/ -name “*.ko”|wc -l

128

一次之後,就可以在該配置檔案基礎上配置核心了

================================================================

如果你經常編譯大型的C/C++工程,不使用ccache你就out了。

   cache is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Supported languages are C, C++, Objective-C and Objective-C++.

Usage

使用ccache

  • 編譯指令前增加ccache. $ ccache gcc xxx
  • 建立軟連結。 $ ln -s ccache /usr/local/bin/gcc

建議使用第一種方式,因為ccache偶爾也犯暈,當由于它出現錯誤的時候, 很難看出端倪。曾在某次編譯某份代碼時,ccache對某個編譯選項的判斷失誤導緻編譯失敗,死活查不出來為什麼原因。是以當出現某些怪異的現象時,請用正常的方式編譯試試。

Example

  • 編譯軟體包
  1. [/tmp/bash-4.1 0]$ uname -a
  2. Linux AP 2.6.37-gentoo #1 SMP PREEMPT Sun Jan 16 14:55:15 CST 2011 i686 Intel(R) Core(TM)2 Duo CPU T8100 @ 2.10GHz GenuineIntel GNU/Linux
  3. [/tmp/bash-4.1 0]$ CC=”ccache gcc” ./configure
  4. [/tmp/bash-4.1 0]$ time make
  5. real 0m47.343s
  6. user 0m39.572s
  7. sys 0m3.244s
  8. [/tmp/bash-4.1 0]$ make clean
  9. [/tmp/bash-4.1 0]$ time make
  10. real 0m10.131s
  11. user 0m5.597s
  12. sys 0m1.077s

由上可以看出,使用ccache後, 編譯速度快了5倍(中間很長一段時間不是gcc在編譯,否則更快)。Wonderful..

  • 編譯核心
  1. [/tmp/linux-2.6.34 0]$ uname -a
  2. Linux boeye-AP 2.6.37-gentoo #1 SMP PREEMPT Wed Jan 12 20:06:14 CST 2011 x86_64 AMD Athlon(tm) II X4 630 Processor AuthenticAMD GNU/Linux
  3. [/tmp/linux-2.6.34 0]$ grep “make” build
  4. 28:make -j4 ARCH=arm CROSS_COMPILE=”ccache arm-linux-” O=$outdir $@
  5. [/tmp/linux-2.6.34 0]$ time ./build
  6. real 3m4.146s
  7. user 10m30.640s
  8. sys 0m37.138s
  9. [/tmp/linux-2.6.34 0]$ ./build clean
  10. [/tmp/linux-2.6.34 0]$ time ./build
  11. real 0m23.714s
  12. user 0m31.603s
  13. sys 0m12.777s

在交叉編譯核心時,編譯速度也快了近9倍。

  • 編譯Android

Android中,使用ccache,隻需要添加環境變量’$ export USE_CCACHE=1′, 不同的是,預設它不用HOST的ccache程式,而使用自帶的ccache. 編譯android需要較大的緩沖區:

  1. $ ccache -M 3G    // 将緩沖區設定為3G
  1. Conclude

看過以上三個例子,如果你仍沒有使用ccache的欲望,歡迎告訴我理由:)

================================================================

Linux 核心編譯 —— make localmodconfig 簡化核心配置流程

簡介:

前些天才知道, Linux 2.6.32 開始引入了一個 make localmodconfig 用于簡化 kernel 的配置。

剛剛找了一下這個方面的資料,分享一下。

Most people uses the kernel shipped by distros – and that’s good. But some people like to compile their own kernels from kernel.org, or maybe they like following the Linux development and want to try it. Configuring your own kernel, however, has become a very difficult and tedious task – there’re too many options, and some times userspace software will stop working if you don’t enable some key option. You can use a standard distro .config file, but it takes too much time to compile all the options it enables.

To make the process of configuration easier, a new build target has been added: make localmodconfig. It runs “lsmod” to find all the modules loaded on the current running system. It will read all the Makefiles to map which CONFIG enables a module. It will read the Kconfig files to find the dependencies and selects that may be needed to support a CONFIG. Finally, it reads the .config file and removes any module “=m” that is not needed to enable the currently loaded modules. With this tool, you can strip a distro .config of all the unuseful drivers that are not needed in our machine, and it will take much less time to build the kernel. There’s an additional “make localyesconfig” target, in case you don’t want to use modules and/or initrds.

以上内容摘自:Kernel Newbies。

大概意思是說, make localmodconfig 會執行 lsmod 指令檢視目前系統中加載了哪些子產品 (Modules), 并最後将原來的 .config 中不需要的子產品去掉,僅保留前面 lsmod 出來的這些子產品,進而簡化了核心的配置過程。 

這樣做确實友善了很多,但是也有個缺點:該方法僅能使編譯出的核心支援目前核心已經加載的子產品。因為該方法使用的是 lsmod 的結果,如果有的子產品目前沒有加載,那麼就不會編到新的核心中。

例如,我有的時候需要制作 squashfs , 是以在目前的核心中,将 squashfs 編譯成了子產品。 當使用 make localmodconfig 來配置 Kernel 的時候,如果目前系統中沒有加載這個子產品, 那麼新編出來的核心中就不會将 squashfs 編譯成子產品,在新的核心中就沒辦法使用這個子產品了。

是以建議在使用 make localmodconfig 之前,首先折騰一下系統,插個優盤,開開攝像頭之類, 以便讓核心加載上平時使用時候所需要的子產品;執行 make localmodconfig 之後,再執行一下 make menuconfig 來手動檢查一下, 是否還有其他子產品需要手動選擇。

這樣,核心的編譯可以分成如下幾個步驟來進行:

  1. 下載下傳解壓核心源碼:

    http://www.kernel.org, 不必多言。 下載下傳之後解壓到自己的目錄,例如 /usr/src/linux-2.6.35/ , 後文中将以 $SRC 代表這個目錄。

  2. 找一個現成的 kconfig 檔案作為模版:

    可以從自己的 Linux 發行版中拷貝一個出來,拷貝到為 $SRC/中, 并重命名為.config 。

  3. 折騰一下系統,讓它将合适的 module 都加載上。
  4. 執行 make localmodconfig 精減不需要的子產品。
  5. 執行 make menuconfig ,檢查一下是否有自己需要的子產品沒有選上。
  6. 執行 make 進行編譯
  7. 執行 make modules_install 安裝子產品
  8. 執行 make install 安裝核心
  9. 編輯 /boot/grub/grub.conf 或者 /boot/grub/menu.lst 添加新的引導菜單。
  10. 重新開機并以新的核心啟動。

OK, that’s all.

================================================================

今天仔細看了看文檔 居然發現這個好東西 建議大家試試.

介紹 http://kernelnewbies.org/Linux_2_6_32#head-11f54cdac41ad6150ef817fd68597554d9d05a5f

1.8. Easy local kernel configuration

Most people uses the kernel shipped by distros – and that’s good. But some people like to compile their own kernels from kernel.org, or maybe they like following the Linux development and want to try it. Configuring your own kernel, however, has become a very difficult and tedious task – there’re too many options, and some times userspace software will stop working if you don’t enable some key option. You can use a standard distro .config file, but it takes too much time to compile all the options it enables.

To make the process of configuration easier, a new build target has been added: make localmodconfig. It runs “lsmod” to find all the modules loaded on the current running system. It will read all the Makefiles to map which CONFIG enables a module. It will read the Kconfig files to find the dependencies and selects that may be needed to support a CONFIG. Finally, it reads the .config file and removes any module “=m” that is not needed to enable the currently loaded modules. With this tool, you can strip a distro .config of all the unuseful drivers that are not needed in our machine, and it will take much less time to build the kernel. There’s an additional “make localyesconfig” target, in case you don’t want to use modules and/or initrds.

使用lsmod分析目前的系統加載了哪些子產品,把沒有加載的子產品都去掉。

分享到:

  • 上一篇:Linux 2.6.36核心優化指南
  • 下一篇:linux下fork的運作機制詳細解析

繼續閱讀