§·編譯內核:
※·程序包的編譯安裝:
./configure make make install
前提:開發環境(開發工具,開發庫),頭文件(/usr/include)
開源: 源代碼開發—->可執行格式,(不同Linux的發行版以自己的理解,發行的軟件不同)
發行版:以“通用”的目標,發行編譯好的軟件包。
由于發行版為了通用性,把軟件的很多功能一同編譯好發行。redhat公司后來有發行主包與分包 的可以解決部分用戶對特定軟件功能的需求,但是還是不夠靈活。
※·編譯內核:(Linux有C語言開發的)
前提:
1)準備好開發環境;
2)獲取目標主機上硬件設備的相關信息;
3)獲取目標主機系統功能的相關信息,例如啟用的文件系統;
4)獲取內核源代碼包: www.kenel.org
※·準備開發環境:
Cenots 6 7
包組:
development tools <開發工具>
server platform development <服務器平臺開發>
需要使用圖形就安裝 (桌面圖形開發平臺)
依賴有以下的包 ncurses包需要安裝的
※·獲取目標主機上硬件設備相關信息
1)獲取目標主機上 CPU信息;
~]#cat /proc/info
~]#lscpu
~]#x86info軟件包可以顯示詳細信息
注意:不需要收集內存信息,只需要內存多大即可。
2)PCI設備
~]#lspci 支持 –v -vv
[root@node1 ~]# lspci #顯示PCI設備信息 00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01) 00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01) 00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08) 00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01) 00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08) 00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10) 00:0f.0 VGA compatible controller: VMware SVGA II Adapter #顯卡的設備型號 00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01) #SISC的設備的型號 00:11.0 PCI bridge: VMware PCI bridge (rev 02) ............................................................. 00:18.7 PCI bridge: VMware PCI Express Root Port (rev 01) 02:00.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01) #網卡 02:01.0 Multimedia audio controller: Ensoniq ES1371 / Creative Labs CT2518/ES1373 (rev 02) #聲卡 [root@node1 ~]#
※·內核源代碼包:
獲取內核源代碼包: www.kenel.org
※·內核編譯過程:
◎·準備內核源碼包
1.解壓文件至 /usr/src/目錄,內核編譯時,由于如果新的硬件linux系統會自動到/usr/src/linux/下去查找驅動程序,所以我們需要創建一個連接文件 :ln -sv linux-3.10.67 linux.
[root@node1 linux-2.6.32.61]# ls arch COPYING crypto drivers fs init Kbuild lib Makefile net REPORTING-BUGS scripts sound usr block CREDITS Documentation firmware include ipc kernel MAINTAINERS mm README samples security tools virt [root@node1 linux-2.6.32.61]# pwd /usr/src/linux_bianyi/linux-2.6.32.61 [root@node1 linux-2.6.32.61]# 解釋: arch : 平臺相關 block : 塊設備相關內核級代碼 crypto : 加密庫,各種加密算法 Documentation :幫助文檔 drivers : 設備驅動 firmware : 主板硬件設備固件驅動 fs : 各種文件系統 init : 如何啟動init文件的 ipc : 進程間通信 mm : 內存管理的 lib : 庫文件 include : 頭文件 samples : 事例 tools :內核自帶工具 virt : 虛擬化
◎·使用make命令編譯內核
步驟: ~]# tar xf linux-3.10.67.tar.xz -C /usr/src ~]#cd /usr/src ~]#ln linux-3.10.67 linux ~]#cd linux ~]#make menuconfig #配置內核選項 ~]#make [-j #] #編輯內核,可使用 -j 指定編譯線程數量 ~]#make modules_install #安裝內核模塊 ~]#make install #安裝內核 #內核文件路徑:/usr/src/linux/arch/x86_64/boot,安裝其實就是安裝這個文件
make –help 查看幫助
make menuconfig : 菜單式編譯界面
make xconfig : 圖像編輯界面
make gconfig : 圖像編輯界面
重啟系統,選擇使用新內核
準備模板文件:
1. Centos 提供了一個模板配置文件: /boot/config-3.10.0-229.el7.x86_64
拷貝模板文件:
cp /boot/config-3.10.0-229.el7.x86_64 /usr/src/linux/.config
1)配置內核選項:
支持“更新”模式進行配置,可以在原有 ./config 模板上進行“修改”配置
(a)make config:基于命令行以遍歷的方式去配置內核中可配置的每個選項;
(b)make menuconfig : 基于cureses的文本配置窗口
(c)make gconfig :基于GTK開發環境的窗口界面,包組 “桌面平臺開發”
(d)make xconfig : 基于QT開發環境的窗口界面;
支持“全新配置”模式進行配置:
(a)make defconfig :基于內核為目標平臺的“默認”配置為模板進行配置;
(b)make allnoconfig : 所有選項均為:no
2)編譯
(a)多線程編譯: make 【-j #】 CPU多線程編譯;
(b)編譯內核中的一部分代碼:
(1)只編譯某子目錄中的相關代碼:
~]#cd /usr/src/linux
~]#make path/to/dir
(2)只編譯一個模塊:
編譯好的一個模塊,只能手動的復制到對應的目錄下 /lib/modules
~]#cd /usr/src/linux
~]#make path/to/dir/fike.ko (默認是沒有ko結尾的文件的,原名應該是.c文件)
(3)如何交叉編譯:
目標平臺與當前編譯操作所在的平臺不同:
#make ARCH=arch_name
要獲取特定目標平臺的使用幫助:
#make ARCH=arch_name help
如何在執行過編譯操作的內核源碼樹上做從新編譯:
事先清理操作:
~]# make clean :清理編譯生成的絕大多數文件,但會保留config,及編譯外部模塊所需要的文件;
~]#make mrproper : 清理編譯生成的所有文件,包括配置生成的config文件及其某些備份文件;
~]#make distclean : 相關于 mrproper,額外清理各種pathes及編輯器備份文件。
◎·內核編譯指令詳解
1.解壓文件至 /usr/src/目錄,內核編譯時,由于如果新的硬件linux系統會自動到/usr/src/linux/下去查找驅動程序,所以我們需要創建一個連接文件 :ln -sv linux-3.10.67 linux.
#解壓出來的源代碼基本上都是 什么 *.c *.s *.h 文件,沒有什么中間文件 .o的文件
[root@centos68 src]# tar -Jxvf linux-2.6.34.14.tar.xz linux-2.6.34.14/tools/perf/util/sigchain.h linux-2.6.34.14/tools/perf/util/sort.c linux-2.6.34.14/tools/perf/util/sort.h linux-2.6.34.14/tools/perf/util/strbuf.c linux-2.6.34.14/tools/perf/util/strbuf.h linux-2.6.34.14/tools/perf/util/string.c linux-2.6.34.14/tools/perf/util/string.h linux-2.6.34.14/tools/perf/util/strlist.c linux-2.6.34.14/tools/perf/util/strlist.h linux-2.6.34.14/tools/perf/util/svghelper.c linux-2.6.34.14/tools/perf/util/svghelper.h linux-2.6.34.14/tools/perf/util/symbol.c linux-2.6.34.14/tools/perf/util/symbol.h linux-2.6.34.14/tools/perf/util/thread.c linux-2.6.34.14/tools/perf/util/thread.h linux-2.6.34.14/tools/perf/util/trace-event-info.c linux-2.6.34.14/tools/perf/util/trace-event-parse.c linux-2.6.34.14/tools/perf/util/trace-event-read.c linux-2.6.34.14/tools/perf/util/trace-event-scripting.c linux-2.6.34.14/tools/perf/util/trace-event.h linux-2.6.34.14/tools/perf/util/types.h linux-2.6.34.14/tools/perf/util/usage.c linux-2.6.34.14/tools/perf/util/util.c linux-2.6.34.14/tools/perf/util/util.h linux-2.6.34.14/tools/perf/util/values.c linux-2.6.34.14/tools/perf/util/values.h linux-2.6.34.14/tools/perf/util/wrapper.c linux-2.6.34.14/usr/ linux-2.6.34.14/usr/.gitignore linux-2.6.34.14/usr/Kconfig linux-2.6.34.14/usr/Makefile linux-2.6.34.14/usr/gen_init_cpio.c linux-2.6.34.14/usr/initramfs_data.S linux-2.6.34.14/usr/initramfs_data.bz2.S linux-2.6.34.14/usr/initramfs_data.gz.S linux-2.6.34.14/usr/initramfs_data.lzma.S linux-2.6.34.14/virt/ linux-2.6.34.14/virt/kvm/ linux-2.6.34.14/virt/kvm/Kconfig linux-2.6.34.14/virt/kvm/assigned-dev.c linux-2.6.34.14/virt/kvm/coalesced_mmio.c linux-2.6.34.14/virt/kvm/coalesced_mmio.h linux-2.6.34.14/virt/kvm/eventfd.c linux-2.6.34.14/virt/kvm/ioapic.c linux-2.6.34.14/virt/kvm/ioapic.h linux-2.6.34.14/virt/kvm/iodev.h linux-2.6.34.14/virt/kvm/iommu.c linux-2.6.34.14/virt/kvm/irq_comm.c linux-2.6.34.14/virt/kvm/kvm_main.c [root@centos68 src]#
#解壓內核源代碼的文件大小為 443M
[root@centos68 linux-2.6.34.14]# du -h --max-depth=1 16M ./Documentation 2.1M ./scripts 140K ./init 19M ./include 18M ./net 4.3M ./kernel 496K ./block 1.6M ./crypto 52K ./usr 20M ./sound 108K ./samples 1.4M ./lib 1.4M ./security 116M ./arch 1.6M ./tools 31M ./fs 216K ./ipc 2.0M ./mm 5.7M ./firmware 156K ./virt 205M ./drivers 443M . [root@centos68 linux-2.6.34.14]#
#相關文件的目錄的說明
[root@node1 linux-2.6.32.61]# pwd /usr/src/linux_bianyi/linux-2.6.32.61 [root@node1 linux-2.6.32.61]# 解釋: arch : 平臺相關 block : 塊設備相關內核級代碼 crypto : 加密庫,各種加密算法 Documentation :幫助文檔 drivers : 設備驅動 firmware : 主板硬件設備固件驅動 fs : 各種文件系統 init : 如何啟動init文件的 ipc : 進程間通信 mm : 內存管理的 lib : 庫文件 include : 頭文件 samples : 事例 tools :內核自帶工具 virt : 虛擬化
步驟:
~]# tar xf linux-3.10.67.tar.xz -C /usr/src #解壓內核源代碼到目錄 ~]#cd /usr/src #進入到該目錄 ~]#ln linux-3.10.67 linux #創建Linux連接 ~]#cd linux #進入到鏈接文件目錄
◎·保存干凈的源代碼
make mrproper :此前沒有編譯過,將會清除所有生成的文件,包括配置文件.config
make clear : 僅會刪除類似目標文件之類的編譯過程產生的中間文件,而不會刪除配置文件
了解了硬件相關的數據后,我們還得要處理一下核心原始碼底下的殘留文件才行!假設我們是第一次編譯, 但是我們不清楚到底下載下來的原始碼當中有沒有保留目標文件 (*.o) 以及相關的配置文件存在, 此時我們可以透過底下的方式來處理掉這些『編譯過程的目標文件以及配置文件』:
#能不能認為正常的源代碼中是沒有 .o文件的,也就是說,如果編譯后,有生成的文件大部分為配置文件和.o文件.
C程序的源代碼文件為 .c 結尾的文件,通常情況下 .o文件為gcc編譯過程中生成的臨時文件。
◎·make menuconfig
· 『左右箭頭鍵』:可以移動最底下的 <Select>, <Exit>, <Help>項目;
· 『上下箭頭鍵』:可以移動上面大框框部分的反白光柱,若該行有箭頭 (—>) 則表示該行內部還有其他細項
需要來設定的意思;
· 選定項目:以『上下鍵』選擇好想要設定的項目之后,并以『左右鍵』選擇 <Select> 之后, 按下『 Enter 』
就可以進入該項目去作更進一步的細部設定啰;
· 可挑選之功能:在細部項目的設定當中,如果前面有 [ ] 或 < > 符號時,該項目才可以選擇, 而選擇可以
使用『空格鍵』來選擇;
· 若為 [*] <*> 則表示編譯進核心;若為 <M> 則表示編譯成模塊! 盡量在不知道該項目為何時,且有模塊
可以選,那么就可以直接選擇為模塊啰!
· 當在細項目選擇 <Exit> 后,并按下 Enter ,那么就可以離開該細部項目啰!
啟動一個圖形界面,方便我們選擇相應的模塊
◎·make [ -j #]
[root@study linux-3.10.89]# make vmlinux <==未經壓縮的核心 [root@study linux-3.10.89]# make modules <==僅核心模塊 [root@study linux-3.10.89]# make bzImage <==經壓縮過的核心(預設) [root@study linux-3.10.89]# make all <==進行上述的三個動作 [root@study linux-3.10.89]# make -j 4 clean <==先清除暫存檔 [root@study linux-3.10.89]# make -j 4 bzImage <==先編譯核心 [root@study linux-3.10.89]# make -j 4 modules <==再編譯模塊 [root@study linux-3.10.89]# make -j 4 clean bzImage modules <==連續動作!
開始編譯核心 和 核心模塊:編譯核心和核心模塊就是在 make menuconfig中自己選擇的功能或編譯進核心,把部分功能做成模塊,把某個功能從核心中取出掉等等。
#編譯好當前目錄的文件大小
[root@centos68 linux]# du -h --max-depth=1 18M ./Documentation 3.4M ./scripts 3.6M ./init 24M ./include 510M ./net 86M ./kernel 13M ./block 33M ./crypto 4.5M ./usr 243M ./sound 108K ./samples 6.6M ./.tmp_versions 19M ./lib 16M ./security 271M ./arch 1.6M ./tools 346M ./fs 5.3M ./ipc 37M ./mm 7.5M ./firmware 2.1M ./virt 2.6G ./drivers 4.9G . [root@centos68 linux]#
◎·make modules_install (安裝編譯好的模塊)
編譯生成的模塊路徑文:執行編譯安裝的目錄下
使用的 install 命令 根據 makefile文件的內容執行復制內核模塊到指定的目錄下。
安裝模塊前有個地方得要特別強調喔!我們知道模塊是放置到 /lib/modules/$(uname -r) 目錄下的,那
如果同一個版本的模塊被反復編譯后來安裝時,那如何解決內核版本一直的問題呢?
1.把舊版本的目錄 /lib/modules/$(uname -r) 改名,再使用 make modules_install 安裝新的模塊;
2.編譯的時候選擇 General setup 內的 Local version 修改成新的名稱,生成的模塊目錄就與原來的不一樣
#沒有編譯之前,解壓源文件后查看 /usr/src/linux-2.6.34.14/drivers/xen
該目錄沒有任何的 的 .0文件
[root@centos68 xen]# pwd /usr/src/linux-2.6.34.14/drivers/xen [root@centos68 xen]# ll total 120 -rw-rw-r--. 1 root root 14919 Jan 17 2013 balloon.c -rw-rw-r--. 1 root root 2069 Jan 17 2013 cpu_hotplug.c -rw-rw-r--. 1 root root 21911 Jan 17 2013 events.c -rw-rw-r--. 1 root root 12140 Jan 17 2013 evtchn.c -rw-rw-r--. 1 root root 762 Jan 17 2013 features.c -rw-rw-r--. 1 root root 14562 Jan 17 2013 grant-table.c -rw-rw-r--. 1 root root 2088 Jan 17 2013 Kconfig -rw-rw-r--. 1 root root 403 Jan 17 2013 Makefile -rw-rw-r--. 1 root root 5723 Jan 17 2013 manage.c -rw-rw-r--. 1 root root 9352 Jan 17 2013 sys-hypervisor.c drwxrwxr-x. 2 root root 4096 Jan 17 2013 xenbus -rw-rw-r--. 1 root root 5560 Jan 17 2013 xencomm.c drwxrwxr-x. 2 root root 4096 Jan 17 2013 xenfs
#在編譯以后,再來查看相同目錄下的文件夾
[root@centos68 xen]# pwd /usr/src/kernels/linux/drivers/xen [root@centos68 xen]# ll total 3208 -rw-rw-r--. 1 root root 14919 Jan 17 2013 balloon.c -rw-r--r--. 1 root root 206680 Sep 11 14:09 balloon.o -rw-r--r--. 1 root root 1622944 Sep 11 14:09 built-in.o -rw-rw-r--. 1 root root 2069 Jan 17 2013 cpu_hotplug.c -rw-r--r--. 1 root root 55128 Sep 11 14:09 cpu_hotplug.o -rw-rw-r--. 1 root root 21911 Jan 17 2013 events.c -rw-r--r--. 1 root root 238679 Sep 11 14:09 events.o -rw-rw-r--. 1 root root 12140 Jan 17 2013 evtchn.c -rw-r--r--. 1 root root 254203 Sep 11 14:12 evtchn.ko -rw-r--r--. 1 root root 1920 Sep 11 14:10 evtchn.mod.c -rw-r--r--. 1 root root 54680 Sep 11 14:11 evtchn.mod.o -rw-r--r--. 1 root root 201229 Sep 11 14:09 evtchn.o -rw-rw-r--. 1 root root 762 Jan 17 2013 features.c -rw-r--r--. 1 root root 64834 Sep 11 14:09 features.o -rw-rw-r--. 1 root root 14562 Jan 17 2013 grant-table.c -rw-r--r--. 1 root root 183576 Sep 11 14:09 grant-table.o -rw-rw-r--. 1 root root 2088 Jan 17 2013 Kconfig -rw-rw-r--. 1 root root 403 Jan 17 2013 Makefile -rw-rw-r--. 1 root root 5723 Jan 17 2013 manage.c -rw-r--r--. 1 root root 113824 Sep 11 14:09 manage.o -rw-r--r--. 1 root root 0 Sep 11 14:09 modules.builtin -rw-r--r--. 1 root root 63 Sep 11 14:09 modules.order -rw-rw-r--. 1 root root 9352 Jan 17 2013 sys-hypervisor.c -rw-r--r--. 1 root root 132528 Sep 11 14:09 sys-hypervisor.o drwxrwxr-x. 2 root root 4096 Sep 11 14:09 xenbus -rw-rw-r--. 1 root root 5560 Jan 17 2013 xencomm.c drwxrwxr-x. 2 root root 4096 Sep 11 14:12 xenfs
#使用modules_install 命令后,install 拷貝的文件和原來編譯目錄中的文件類型和名稱還不一樣
[root@centos68 linux]# make modules_install > testmake.log [root@centos68 linux]# less testmake.log [root@centos68 linux]# less testmake.log | grep xen INSTALL drivers/block/xen-blkfront.ko INSTALL drivers/net/netxen/netxen_nic.ko INSTALL drivers/net/xen-netfront.ko INSTALL drivers/xen/evtchn.ko INSTALL drivers/xen/xenfs/xenfs.ko [root@centos68 linux]#
小結: make modules_install 的作用還不是簡單的復制生成的各個目錄下的文件,還是通過一種機制生成最終文件 *.ko
◎·make install (安裝編譯好的內核文件)
#內核文件路徑:/usr/src/linux/arch/x86_64/boot,安裝其實就是安裝這個文件進當前系統
[root@centos68 linux]# make install #安裝內核文件 sh /usr/src/kernels/linux-2.6.34.14/arch/x86/boot/install.sh 2.6.34.141.0-linux arch/x86/boot/bzImage \ System.map "/boot" #使用的腳本文件安裝, ERROR: modinfo: could not find module bnx2fc ERROR: modinfo: could not find module 8021q ERROR: modinfo: could not find module garp ERROR: modinfo: could not find module ip6t_REJECT ERROR: modinfo: could not find module nf_conntrack_ipv6 ERROR: modinfo: could not find module nf_defrag_ipv6 ERROR: modinfo: could not find module ip6table_filter ERROR: modinfo: could not find module ip6_tables ERROR: modinfo: could not find module ipv6 ERROR: modinfo: could not find module vsock ERROR: modinfo: could not find module vmci ERROR: modinfo: could not find module ext4 ERROR: modinfo: could not find module jbd2 ERROR: modinfo: could not find module mbcache ERROR: modinfo: could not find module dm_mirror ERROR: modinfo: could not find module dm_region_hash ERROR: modinfo: could not find module dm_log ERROR: modinfo: could not find module dm_mod #由于刪除了很多額模塊的導致報錯,測試一下看看系統能不能正常的啟動 [root@centos68 linux]#
#在/boot目錄下生產的文件:
[root@centos68 boot]# ll total 44056 -rw-r--r--. 1 root root 108103 May 11 01:32 config-2.6.32-642.el6.x86_64 drwxr-xr-x. 3 root root 1024 Jul 19 18:19 efi drwxr-xr-x. 2 root root 1024 Sep 11 15:26 grub -rw-------. 1 root root 25724517 Sep 7 23:34 initramfs-2.6.32-642.el6.x86_64.img -rw-------. 1 root root 6286176 Sep 11 15:26 initramfs-2.6.34.141.0-linux.img drwx------. 2 root root 12288 Jul 19 18:05 lost+found -rw-r--r--. 1 root root 215559 May 11 01:32 symvers-2.6.32-642.el6.x86_64.gz lrwxrwxrwx. 1 root root 35 Sep 11 15:25 System.map -> /boot/System.map-2.6.34.141.0-linux -rw-r--r--. 1 root root 2615003 May 11 01:32 System.map-2.6.32-642.el6.x86_64 -rw-r--r--. 1 root root 2135308 Sep 11 15:25 System.map-2.6.34.141.0-linux lrwxrwxrwx. 1 root root 32 Sep 11 15:25 vmlinuz -> /boot/vmlinuz-2.6.34.141.0-linux -rwxr-xr-x. 1 root root 4264528 May 11 01:32 vmlinuz-2.6.32-642.el6.x86_64 -rw-r--r--. 1 root root 3732640 Sep 11 15:25 vmlinuz-2.6.34.141.0-linux [root@centos68 boot]#
#查看/boot/grub/grub.conf文件內容
[root@centos68 boot]# cat grub/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/sda2 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=1 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS (2.6.34.141.0-linux) root (hd0,0) kernel /vmlinuz-2.6.34.141.0-linux ro root=UUID=ca4c44c8-1c65-4896-a295-d55e5d5e5c5e rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.34.141.0-linux.img title CentOS 6 (2.6.32-642.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=ca4c44c8-1c65-4896-a295-d55e5d5e5c5e rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img [root@centos68 boot]#
小結:由于編譯的內核刪除了很多的東西,導致系統無法正常的進入系統,不過以上的大概流程有一個比較清晰的思路了,對學習Linux還是有一定的幫助的!
原創文章,作者:linux_root,如若轉載,請注明出處:http://www.www58058.com/45459