1.前言
編譯Linux內核失敗的原因很多時候就是驅動選錯,適合自己本機的驅動沒編譯進去。面對特殊平台(或者有些潔癖者,我就是^_^),要編譯精簡內核,只要本機驅動,其他都不需要。面對內核裡面這麼多驅動,不懂硬件的我們,看到都頭大。本人第一次安裝gentoo的時候,內核編譯了5次,前4次主要失敗原因是以太網卡驅動、無線網卡驅動、顯卡驅動、聲卡驅動都沒一次性配置正確。雖然gentoo提供了genkernel命令,可以自動幫你編譯內核,但還是想自己手工嘗試下。
2.獲取本機驅動信息
既然跟內核掛鉤,我們獲取本機內核驅動信息最直接的辦法就是使用lspci命令,這個命令可以輸出本機驅動信息,配合-k選項可以看到此驅動在內核的位子。
一般編譯內核的場景:更新舊內核,安裝定制版linux發行版(gentoo,arch等),嵌入式平台,其他特殊平台等等...
大部分場景在下都可以使用lspci命令,安裝gentoo的livecd版都提供了該工具,如果其他系統沒有就安裝該工具
debian系列安裝
1 $:sudo apt-get install pciutils
或者源碼編譯安裝,下載地址https://www.kernel.org/pub/software/utils/pciutils/
然後我們獲取本機環境現存的驅動信息,下面是我的機器上面的信息:
1 $:lspci -k 2 00:00.0 Host bridge: Intel Corporation 2nd Generation Core Processor Family DRAM Controller (rev 09) 3 Subsystem: Lenovo Device 21e2 4 00:01.0 PCI bridge: Intel Corporation Xeon E3-1200/2nd Generation Core Processor Family PCI Express Root Port (rev 09) 5 Kernel driver in use: pcieport 6 00:02.0 VGA compatible controller: Intel Corporation 2nd Generation Core Processor Family Integrated Graphics Controller (rev 09) 7 Subsystem: Lenovo Device 21e3 8 Kernel driver in use: i915 9 00:16.0 Communication controller: Intel Corporation 6 Series/C200 Series Chipset Family MEI Controller #1 (rev 04) 10 Subsystem: Lenovo Device 21e2 11 Kernel driver in use: mei_me 12 00:1a.0 USB controller: Intel Corporation 6 Series/C200 Series Chipset Family USB Enhanced Host Controller #2 (rev 04) 13 Subsystem: Lenovo Device 21e2 14 Kernel driver in use: ehci-pci 15 00:1b.0 Audio device: Intel Corporation 6 Series/C200 Series Chipset Family High Definition Audio Controller (rev 04) 16 Subsystem: Lenovo Device 21e2 17 Kernel driver in use: snd_hda_intel 18 00:1c.0 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 1 (rev b4) 19 Kernel driver in use: pcieport 20 00:1c.1 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 2 (rev b4) 21 Kernel driver in use: pcieport 22 00:1c.2 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 3 (rev b4) 23 Kernel driver in use: pcieport 24 00:1c.3 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 4 (rev b4) 25 Kernel driver in use: pcieport 26 00:1c.7 PCI bridge: Intel Corporation 6 Series/C200 Series Chipset Family PCI Express Root Port 8 (rev b4) 27 Kernel driver in use: pcieport 28 00:1d.0 USB controller: Intel Corporation 6 Series/C200 Series Chipset Family USB Enhanced Host Controller #1 (rev 04) 29 Subsystem: Lenovo Device 21e2 30 Kernel driver in use: ehci-pci 31 00:1f.0 ISA bridge: Intel Corporation HM65 Express Chipset Family LPC Controller (rev 04) 32 Subsystem: Lenovo Device 21e2 33 Kernel driver in use: lpc_ich 34 00:1f.2 SATA controller: Intel Corporation 6 Series/C200 Series Chipset Family 6 port SATA AHCI Controller (rev 04) 35 Subsystem: Lenovo Device 21e2 36 Kernel driver in use: ahci 37 00:1f.3 SMBus: Intel Corporation 6 Series/C200 Series Chipset Family SMBus Controller (rev 04) 38 Subsystem: Lenovo Device 21e2 39 01:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Seymour [Radeon HD 6400M/7400M Series] (rev ff) 40 Kernel driver in use: radeon 41 03:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 06) 42 Subsystem: Lenovo Device 21e2 43 Kernel driver in use: r8169 44 04:00.0 System peripheral: Ricoh Co Ltd PCIe SDXC/MMC Host Controller (rev 07) 45 Subsystem: Lenovo Device 21e2 46 Kernel driver in use: sdhci-pci 47 09:00.0 Network controller: Realtek Semiconductor Co., Ltd. RTL8188CE 802.11b/g/n WiFi Adapter (rev 01) 48 Subsystem: Realtek Semiconductor Co., Ltd. Device 8195 49 Kernel driver in use: rtl8192ce
我們可以看到每個驅動後面都有Kernel driver in use:,代表驅動在內核中的鏈接位子。
3.配置內核驅動
下載內核源碼https://www.kernel.org/,我這裡下載的是stable 4.3.3,解壓後進入源碼目錄執行:
1 $:make menuconfig
接下來就會彈出編譯選項界面,如圖:
我們可以看到上面有快捷鍵操作說明,我重點在</> for Search,在界面直接按反斜槓,進入搜索界面。接下來挑個重點驅動說明一下,我們看
Network controller,可以看到型號是Realtek RTL8188CE,然後Kernel driver in use: rtl8192ce,在內核的位子鏈接是rtl8192ce
我們按</>,輸入rtl8192ce,然後回車,就會出現詳細位子,如圖:
我們可以看到Pormt提示上面跟我要的型號一樣,具體選項的前面有個(1),注意這裡同一個Symbol(就是剛剛輸入的rtl8192ce)有的對應很多選項,根據前面的序號,按對應的數字就可以直接進入選項,按<esc>可以返回來這個界面。這裡,我直接按1。按1後,就到具體選項了,如圖:
我們看到上面,發行版為了兼容各種機器,把所有Realtek的驅動都編譯進成了模塊。接下來就保留自己的驅動型號,去掉其他不要的。
剩下的驅動跟上面操作一樣的,還有的不必要驅動是直接編譯進了內核,非模塊。對照自己的驅動,其他都可以不要。配置好後的內核
配置文件最好保存下來,如果驅動沒配置成功,再配置的時候把保存的配置文件復制一份到源碼目錄,命名為.config。繼續make menuconfig配置。