kernel定制,編譯內核


定制kernel,就需要手動編譯內核

一,准備開發環境

1,包組:Development Tools,Server Platform Development

2,包:ncurses和ncurses-dev

3,如果是圖形界面,則需要額外的GTK或者QT的包組。

二,獲取目標主機上硬件設備的相關信息

1,獲取cpu的信息

cat /proc/cpuinfo 

方法2:

# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    4
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 142
Model name:            Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
Stepping:              10
CPU MHz:               1896.003
BogoMIPS:              3792.00
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              6144K
NUMA node0 CPU(s):     0-3

方法3:詳細顯示cpu信息。

# yum install x86info
# x86info -a

2,獲取pci總線上的設備信息

顯卡,網卡,鼠標,鍵盤都是連的pci總線。

詳細-v:顯示顯示。

# lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: VMware SVGA II Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:05.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)
00:06.0 USB controller: Apple Inc. KeyLargo/Intrepid USB
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:08.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:0d.0 SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)
# lspci -v

3,獲取usb設備信息

# lsusb
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

4,獲取磁盤設備信息

# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   60G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   59G  0 part
  ├─centos-root 253:0    0   37G  0 lvm  /
  ├─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 18.1G  0 lvm  /home
sdb               8:16   0    1G  0 disk
sdc               8:32   0    1G  0 disk
sdd               8:48   0    1G  0 disk
└─sdd1            8:49   0  900M  0 part
sr0              11:0    1 1024M  0 rom

5,獲取全部硬件設備信息,類似windows的資源管理器

# hal-device

三,編譯內核

1,獲取內核源代碼

本機內核是linux-3.10.0

# wget http://cdn.kernel.org/pub/linux/kernel/v3.x/linux-3.10.67.tar.xz

內源碼放在/usr/src下

# tar xf linux-3.10.67.tar.xz -C /usr/src
# ls /usr/src
debug  kernels  linux-3.10.67

當缺少設備驅動時,我們要編譯內核源碼,以獲得驅動。

為什么編譯內核就能獲得驅動呢?

內核的源代碼,基本包括所有硬件的驅動,但是我們安裝的系統也是編譯出來的,編譯的時候沒有包含你要用的驅動的源代碼。所以只要在編譯的時候,把你要的驅動的源代碼包含進來,編譯出來的系統就支持你要的設備了。當然前提是,kernel源代碼里有你要的驅動的代碼,如果沒有,那么再怎么編譯內核也得不出你要得驅動。

這時編譯會去找/usr/src/linux目錄,但是沒有此目錄,只有/usr/src/linux-3.10.67目錄,不要直接修改linux-3.10.67目錄的名字,建立符號鏈接。

理由是:當又有新版本進來時,直接把符號鏈接改到新版本上就號,這樣一來,新版本不好用了,修改符號鏈接,還能執行舊的

# ln -sv linux-3.10.67 linux
‘linux’ -> ‘linux-3.10.67’
# ls linux
arch      CREDITS      drivers        include   Kbuild   lib      mm           REPORTING-BUGS  
security  usr block    crypto         firmware  init     Kconfig  MAINTAINERS  net             
sound     virt         Documentation  fs        ipc      kernel   Makefile    

目錄arch里,基本包括了所有架構的cpu:

# ls linux-3.10.67/arch/
alpha  arm64     c6x   h8300    Kconfig  metag       mn10300   powerpc  sh     um         xtensa
arc    avr32     cris  hexagon  m32r     microblaze  openrisc  s390     sparc  unicore32
arm    blackfin  frv   ia64     m68k     mips        parisc    score    tile   x86

目錄block里,塊設備驅動:

# ls linux-3.10.67/block/
blk-cgroup.c  blk.h            blk-map.c       blk-tag.c       cfq-iosched.c       ioctl.c   

目錄crypto里,加密算法:

# ls linux-3.10.67/crypto
842.c             asymmetric_keys     cipher.c       eseqiv.c         md4.c              scatterwalk.c      twofish_common.c

目錄Documentation里,kernel的文檔

# ls linux-3.10.67/Documentation/

目錄drivers里,基本上包括了所有設備的驅動:

# ls linux-3.10.67/drivers/
accessibility  cdrom        dma       hwmon       isdn       message   parisc    pwm       

firmware目錄,主板固件驅動:

# ls linux/firmware/
3com          bnx2x     emi26        korg              ositech                tehuti 

fs目錄,文件系統:

# ls linux/fs
9p                  block_dev.c          drop_caches.c  fs_struct.c     Kconfig.binfmt  ocfs2  

init目錄,/sbin/init程序:

# ls linux/init
calibrate.c  do_mounts.h         do_mounts_md.c  initramfs.c  Kconfig  Makefile       version.c
do_mounts.c  do_mounts_initrd.c  do_mounts_rd.c  init_task.c  main.c   noinitramfs.c

ipc目錄,進程間通信:

# ls linux/ipc
compat.c     ipcns_notifier.c  Makefile     mqueue.c  msgutil.c    sem.c  syscall.c  util.h
compat_mq.c  ipc_sysctl.c      mq_sysctl.c  msg.c     namespace.c  shm.c  util.c

kernel目錄,內核自己

lib目錄,庫

mm目錄目錄:內存管理

net目錄目錄:網絡管理

samples目錄:實例

scripts目錄:編譯時,安裝時使用的腳本

security目錄:安全相關

sound目錄:聲音相關

tools目錄:工具

virt目錄:虛擬化

# ls linux/virt/
kvm

2,獲取make幫助:

cd進入/usr/src/linux目錄,執行make help獲取幫助

....
Configuration targets:
  config          - Update current config utilising a line-oriented program
  nconfig         - Update current config utilising a ncurses menu based program
  menuconfig      - Update current config utilising a menu based program
....

3,配置kernel安裝選項,類似源碼安裝里的configure

# make menuconfig

如果沒有安裝ncurses-devel會出錯誤:unable to find the ncurses library,所以需要安裝:yum install ncurses-devel

執行成功后,顯示menu界面:

配置內核安裝選項過於復雜,找個模板使用,centos的/boot目錄下有個文件:config-3.10.0-957.el7.x86_64。

此文件是,centos做的配置文件,可以參考使用。

make menuconfig執行后,會在/usr/src/linux/目錄下生成.config文件,所以我們不做make menuconfig了,直接把config-3.10.0-957.el7.x86_64復制從.config文件。

# cp /boot/config-3.10.0-957.el7.x86_64 /usr/src/linux/.config
# cat /usr/src/linux/.config
CONFIG_64BIT=y
CONFIG_X86_64=y
CONFIG_X86=y
CONFIG_INSTRUCTION_DECODER=y
CONFIG_OUTPUT_FORMAT="elf64-x86-64"
...

支持在已有的.config文件基礎上進行修改

  • make config:基於命令行,以遍歷的方式配置。(輸入yes,no等)
  • make menuconfig:基於curesesd的文本界面配置
  • make gconfig:基於GTK桌面開發環境的界面進行配置
  • make xconfig:基於QT桌面開發環境的界面進行配置

支持從0開始配置

  • make defconfig:基於內核為目標平台提供的默認配置為模板進行配置
  • make allnoocnfig:所以選項均為no

centos做的配置文件,有很多是用不到的,再使用make menuconfig,關閉不要的功能。

# make menuconfig

[ ]:不編譯,不使用

[M]:編譯成模塊,不編譯進內核

[*]:直接編譯進kernel

(1)修改cpu:

默認的cpu驅動是General-x86-64。是通用的驅動,但不能完全發揮酷睿cpu的作用,所以修改成core(酷睿)

修改后:

(2)添加release號

(3)關閉藍牙功能:

(4)添加ntfs文件系統的驅動(默認配置里是沒有添加ntfs的):

4,編譯內核:make [-j #]。多核心可以使用-j,指定編譯線程的數量,編譯非常消耗cpu。

由於編譯很花時間,如果是遠程連接的話,ssh萬一斷開了,編譯一般就前功盡棄了。所以要利用一個叫screen的工具。

screen的好處是,當ssh斷開后,編譯還會繼續。

  • screen安裝:yum install screen
  • 在終端上打開一個screen:screen
  • 從screen回到終端:ctrl+a d
  • 查看打開了哪些scren:screen -ls
  • 從終端回到screen:screen -r screen號
  • 終止screen:在screen里面輸入:exit

使用screen編譯:

# screen
進入了screen,在screen里執行編譯
# make -j 4

編譯時,可以退出screen。在終端使用'htop'看看,發現cpu基本都是100%運行了。

部分編譯:當發現漏了某個模塊或者驅動等,我們不需要重新編譯,只需編譯缺少的部分,部分編譯的方法:

  • 只編譯某子目錄種的相關代碼:

    # cd /usr/src/linux
    # make path/to/dir/
    
  • 只編譯一個特定的模塊

    # cd /usr/src/linux
    # make path/to/dir/file.ko
    

    file其實是xxxx.c,要把.c去掉,換成.ko

交叉編譯:目標平台與當前正在執行編譯所在的平台不同。

# make ARCH=arch_name

獲取特定目標平台的使用幫助:

# make ARCH=arch_name help

arch_name可以是arm等。

如何在執行過編譯的內核源碼目錄上做重新編譯?

需要先執行清理:

  • make clean:刪除編譯生成的絕大多數文件,但保留.config
  • make mrproper:刪除編譯生成的絕大多數文件,也刪除.config
  • make distclean:基本刪除所有。

然后從新編譯。

5,安裝模塊(modules):make modules_instal

模塊安裝完成后,在目錄/lib/modules里多出3.10.67-1.el7。【-1.el7】是在配置編譯選項是加的。

# ll /lib/modules
total 4
drwxr-xr-x. 8 root root 4096 Feb 23 09:18 3.10.0-957.el7.x86_64
drwxr-xr-x. 3 root root   91 Feb 23 22:14 3.10.67-1.el7

6,安裝內核:make install。會自動生成grub

安裝成功后,在boot目錄生成了:

kernel:vmlinuz-3.10.67-1.el7

ramdisk:initramfs-3.10.67-1.el7.img

各種映射:System.map-3.10.67-1.el7

# ls -lrt /boot
-rw-r--r--. 1 root root  4776128 Feb 23 22:18 vmlinuz-3.10.67-1.el7
-rw-r--r--. 1 root root  2828222 Feb 23 22:18 System.map-3.10.67-1.el7
lrwxrwxrwx. 1 root root       27 Feb 23 22:18 vmlinuz -> /boot/vmlinuz-3.10.67-1.el7
lrwxrwxrwx. 1 root root       30 Feb 23 22:18 System.map -> /boot/System.map-3.10.67-1.el7
-rw-------. 1 root root 48045100 Feb 23 22:19 initramfs-3.10.67-1.el7.img

並且在/etc/grub2.cfg文件里多出了:

### BEGIN /etc/grub.d/10_linux ###
menuentry 'CentOS Linux (3.10.67-1.el7) 7 (Core)' --class cento

7,重啟系統選擇新的內核。

# c/c++ 學習互助QQ群:877684253 ![](https://img2018.cnblogs.com/blog/1414315/201811/1414315-20181106214320230-961379709.jpg) # 本人微信:xiaoshitou5854


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM