centos7編譯linux的內核源碼


昨天編譯了一個linux 內核源碼,遇到一些問題, 今天把我遇到的問題和解決方法分享給大家。希望可以幫助到需要的人。

1.檢查是否安裝了相應的包

我第一次編譯的時候只安裝的“Development Tools” 包,”Compatibility Libraries“和”Development and Creative Workstation“沒有安裝

[root@localhost yum.repos.d]# yum groups list                       #查看包組列表
Loaded plugins: fastestmirror, langpacks
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done

2.下載源碼從https://www.kernel.org/網站下載需要編譯的內核。

這里直接使用wget命令進行下載

[root@localhost yum.repos.d]# cd /root                                                               #切換到root目錄
[root@localhost ~]# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/testing/linux-4.7-rc7.tar.xz   #使用wget下載文件,這里下載的我下載的不是一個穩定版本的
--2016-07-11 09:59:34--  https://cdn.kernel.org/pub/linux/kernel/v4.x/testing/linux-4.7-rc7.tar.xz
Resolving cdn.kernel.org (cdn.kernel.org)... 151.101.88.69
Connecting to cdn.kernel.org (cdn.kernel.org)|151.101.88.69|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 90401360 (86M) [application/x-xz]
Saving to: ‘linux-4.7-rc7.tar.xz’

100%[===================================================================================>] 90,401,360   392KB/s   in 6m 35s 

2016-07-11 10:06:10 (223 KB/s) - ‘linux-4.7-rc7.tar.xz’ saved [90401360/90401360]

[root@localhost ~]# ls -l | grep linux                                                              #查看下載的文件
-rw-r--r--. 1 root root 90401360 Jul 10 23:41 linux-4.7-rc7.tar.xz

3.解壓文件和創建符號鏈接文件

tar -xf linux-4.7-rc7.tar.xz  -C /usr/src/                                                            #解壓縮到指定目錄
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Music     Public     Videos
Desktop          Downloads  linux-4.7-rc7.tar.xz  Pictures  Templates
[root@localhost ~]# cd /usr/src/                                                                      #進入文件目錄
[root@localhost src]# ls
debug  kernels  linux-4.7-rc7
[root@localhost src]# ln -sv linux-4.7-rc7/ linux                                                     #創建符號鏈接
‘linux’ -> ‘linux-4.7-rc7/’
[root@localhost src]# ls                                                                            #查看
debug  kernels  linux  linux-4.7-rc7

4.創建.config文件

[root@localhost linux]# ls
arch   COPYING  Documentation  fs       ipc      kernel  MAINTAINERS  net             samples   sound  virt
block  CREDITS  drivers        include  Kbuild   lib     Makefile     README          scripts   tools
certs  crypto   firmware       init     Kconfig  linux   mm           REPORTING-BUGS  security  usr
[root@localhost linux]# make menuconfig                                                                 #啟動配置
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/mconf.o
In file included from scripts/kconfig/mconf.c:23:0:
scripts/kconfig/lxdialog/dialog.h:38:20: fatal error: curses.h: No such file or directory
 #include CURSES_LOC
                    ^
compilation terminated.
make[1]: *** [scripts/kconfig/mconf.o] Error 1
make: *** [menuconfig] Error 2

這里報錯誤了。 提示curses.h 這個文件找不到。

[root@localhost linux]# yum search curses                                              #從yum源查找curses相關的包 , 配置本地yum源看我上篇博客。
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
==================================================== N/S matched: curses ====================================================
ncurses.x86_64 : Ncurses support utilities
ncurses-devel.i686 : Development files for the ncurses library
ncurses-devel.x86_64 : Development files for the ncurses library
ncurses-libs.i686 : Ncurses libraries
ncurses-libs.x86_64 : Ncurses libraries
ncurses-static.i686 : Static libraries for the ncurses library
ncurses-static.x86_64 : Static libraries for the ncurses library
ocaml-curses.x86_64 : OCaml bindings for ncurses
ocaml-curses-devel.x86_64 : Development files for ocaml-curses
NetworkManager-tui.x86_64 : NetworkManager curses-based UI
ncurses-base.noarch : Descriptions of common terminals
ncurses-term.noarch : Terminal descriptions

  Name and summary matches only, use "search all" for everything.
[root@localhost linux]# yum install ncurses                                               #安裝ncurses
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package ncurses-5.9-13.20130511.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost linux]# yum install ncurses-devel                                          #安裝ncurses-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ncurses-devel.x86_64 0:5.9-13.20130511.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================
 Package                       Arch                   Version                               Repository                  Size
=============================================================================================================================
Installing:
 ncurses-devel                 x86_64                 5.9-13.20130511.el7                   localcdrom                 713 k

Transaction Summary
=============================================================================================================================
Install  1 Package

Total download size: 713 k
Installed size: 2.1 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ncurses-devel-5.9-13.20130511.el7.x86_64                                                                  1/1 
  Verifying  : ncurses-devel-5.9-13.20130511.el7.x86_64                                                                  1/1 

Installed:
  ncurses-devel.x86_64 0:5.9-13.20130511.el7                                                                                 

Complete!

再次make menuconfig這個命令后,會出現如下的字符界面,我們就可以在這個界面上對內核進行配置。我們這里是第二次使用make menuconfig了,需要先運行:make mrproper來清除以前的配置,回到默認配置,然后再運行:make menuconfig生成.config文件 通過這個工具可以設置內核配置參數。

 

關於選擇這個地方需要說明下。

  • [*]:編譯到內核文件中去,也就是類似這個vmlinuz-3.10.0-693.el7.x86_64文件中去。
  • [m]: 編譯到模塊文件中去,也就是放到類似這個/lib/modules/3.10.0-693.el7.x86_64/目錄中去。

5.執行make 命令

[root@localhost linux]# make 
  HOSTCC  scripts/kconfig/conf.o
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf  --silentoldconfig Kconfig
  SYSTBL  arch/x86/entry/syscalls/../../include/generated/asm/syscalls_32.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/asm/unistd_32_ia32.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/asm/unistd_64_x32.h
  SYSTBL  arch/x86/entry/syscalls/../../include/generated/asm/syscalls_64.h
  HYPERCALLS arch/x86/entry/syscalls/../../include/generated/asm/xen-hypercalls.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_32.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_64.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_x32.h
  HOSTCC  scripts/basic/bin2c
  HOSTCC  arch/x86/tools/relocs_32.o
  HOSTCC  arch/x86/tools/relocs_64.o
  HOSTCC  arch/x86/tools/relocs_common.o
  HOSTLD  arch/x86/tools/relocs
  CHK     include/config/kernel.release
  UPD     include/config/kernel.release
  WRAP    arch/x86/include/generated/asm/clkdev.h
  WRAP    arch/x86/include/generated/asm/cputime.h
  WRAP    arch/x86/include/generated/asm/dma-contiguous.h
  WRAP    arch/x86/include/generated/asm/early_ioremap.h
  WRAP    arch/x86/include/generated/asm/mcs_spinlock.h
  WRAP    arch/x86/include/generated/asm/mm-arch-hooks.h
  CHK     include/generated/uapi/linux/version.h
  UPD     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  UPD     include/generated/utsrelease.h
  CC      arch/x86/purgatory/purgatory.o
  AS      arch/x86/purgatory/stack.o
  AS      arch/x86/purgatory/setup-x86_64.o
  CC      arch/x86/purgatory/sha256.o
  AS      arch/x86/purgatory/entry64.o
  CC      arch/x86/purgatory/string.o
  LD      arch/x86/purgatory/purgatory.ro
  BIN2C   arch/x86/purgatory/kexec-purgatory.c
  CC      kernel/bounds.s
  CHK     include/generated/bounds.h
  UPD     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  UPD     include/generated/timeconst.h
  CC      arch/x86/kernel/asm-offsets.s
  CHK     include/generated/asm-offsets.h
  UPD     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  HOSTCC  scripts/genksyms/genksyms.o
  SHIPPED scripts/genksyms/parse.tab.c
  HOSTCC  scripts/genksyms/parse.tab.o
  SHIPPED scripts/genksyms/lex.lex.c
  SHIPPED scripts/genksyms/keywords.hash.c
  SHIPPED scripts/genksyms/parse.tab.h
  HOSTCC  scripts/genksyms/lex.lex.o
  HOSTLD  scripts/genksyms/genksyms
  CC      scripts/mod/empty.o
  HOSTCC  scripts/mod/mk_elfconfig
  MKELF   scripts/mod/elfconfig.h
  HOSTCC  scripts/mod/modpost.o
  CC      scripts/mod/devicetable-offsets.s
  GEN     scripts/mod/devicetable-offsets.h
  HOSTCC  scripts/mod/file2alias.o
  HOSTCC  scripts/mod/sumversion.o
  HOSTLD  scripts/mod/modpost
  HOSTCC  scripts/selinux/genheaders/genheaders
  HOSTCC  scripts/selinux/mdp/mdp
  HOSTCC  scripts/kallsyms
  HOSTCC  scripts/pnmtologo
  HOSTCC  scripts/conmakehash
  HOSTCC  scripts/recordmcount
  HOSTCC  scripts/sortextable
  HOSTCC  scripts/asn1_compiler
  HOSTCC  scripts/sign-file
scripts/sign-file.c:25:30: fatal error: openssl/opensslv.h: No such file or directory
 #include <openssl/opensslv.h>
                              ^

上面的報錯了, 才發現原來的編譯環境的包沒有安裝好。

執行下面命令安裝包

[root@localhost linux]# yum groups list
Loaded plugins: fastestmirror, langpacks
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done
[root@localhost linux]#yum groups  install  "Development and Creative Workstation" "Compatibility Libraries" -y 

這里可以僅僅安裝openssl相關的基礎包和開發包即可的,但是為了后續其他的編譯安裝,我就直接安裝了"Compatibility Libraries"包。

6.執行 make clean 和make

[root@localhost linux]# make clean                                             # 清空錯誤的編譯環境
  CLEAN   .
  CLEAN   arch/x86/purgatory
  CLEAN   arch/x86/tools
  CLEAN   .tmp_versions
[root@localhost linux]# make                               # 再次編譯 ,這里可以指定並行度的,如果你有4個cpu,可以執行如下命令 make -j 4 來加速編譯進度。

7. 執行 make modules_install

8. 執行make install

[root@localhost linux]# make modules_install 
[root@localhost linux]# make install 

9.重新電腦選擇新編譯好的linux

10 卸載新內核

  • 刪除/lib/modules目錄下對應版本的庫文件
  • 刪除/usr/src/linux目錄下的源碼和壓縮文件
  • 刪除/boot/啟動的內核和內核鏡像文件
  • 刪除grub.conf配置文件新內核對應的條目

 


免責聲明!

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



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