实验:centos6编译安装内核
0、介绍:给现有的centos6编译安装一个内核,编译的过程中,编译的作用就是可以选择自己想要的功能模块,此处参考现有安装的centos6中的/boot/config-2.6.32-696.el6.x86_64 的安装模块配置文件,拷贝到/usr/local/src/目录下,另把官网下载的linux-4.16.13.tar.xz也放到此目录,开始编译安装。此处多选了一个一般liunx不编译安装的NTFS文件系统功能模块,具体过程如下

1、-----下载内核
    先下载好linux内核(https://www.kernel.org)

[root@centos6 src ]#uname -r
    2.6.32-696.el6.x86_64
    [root@centos6 ~ ]#cd /usr/local/src
    [root@centos6 src ]#rz     #如图1
    [root@centos6 src ]#ll -h
    total 99M
    drwxr-xr-x. 12 1001 1001 4.0K Jun  7 11:02 httpd-2.2.32
    -rw-r--r--.  1 root root  99M Jun 29 11:35 linux-4.16.13.tar.xz

    #-----解压内核及查看内核信息
    [root@centos6 src ]#tar xvf linux-4.16.13.tar.xz
    [root@centos6 src ]#ll -h
    total 99M
    drwxr-xr-x. 12 1001 1001 4.0K Jun  7 11:02 httpd-2.2.32
    drwxrwxr-x. 25 root root 4.0K May 30 14:17 linux-4.16.13
    -rw-r--r--.  1 root root  99M Jun 29 11:35 linux-4.16.13.tar.xz
    [root@centos6 src ]#ls
    httpd-2.2.32  linux-4.16.13  linux-4.16.13.tar.xz
    [root@centos6 src ]#du -sh linux-4.16.13 linux-4.16.13.tar.xz
    908M    linux-4.16.13
    99M    linux-4.16.13.tar.xz
    [root@centos6 src ]#cd linux-4.16.13
    [root@centos6 linux-4.16.13 ]#yum groupinstall "Development Tools"   #安装开发工具包
    [root@centos6 linux-4.16.13 ]#find -name "*.c" | wc -l
    26194
    [root@centos6 linux-4.16.13 ]#cat `find -name "*.c"` | wc -l
    17917344
    wc -l `find -name "*.c"`  每个文件大小,下面汇总总行数,17917344


2、-----查看编译配置文件信息


   

[root@centos6 linux-4.16.13 ]#pwd
    /usr/local/src/linux-4.16.13
    [root@centos6 linux-4.16.13 ]#cp /boot/config-2.6.32-696.el6.x86_64 .config
    [root@centos6 linux-4.16.13 ]#wc -l .config
    6184 .config

    有的功能是否集成在内核中,可用可不用的功能模块存放在/lib/modules/3.10**.x86_64/


[root@centos6 src ]#locate xfs.ko
    /lib/modules/2.6.32-696.el6.x86_64/kernel/fs/xfs/xfs.ko

    模块启用功能:


        编译到内核中


        放到磁盘中的/lib/modules中,如xfs.ko


    不启用:


        不涉及


[root@centos6 src ]#pwd
    /usr/local/src
    [root@centos6 src ]#less .config
    #
    # Automatically generated make config: don't edit
    # Linux kernel version: 2.6.32-696.el6.x86_64
    # Tue Mar 21 19:28:06 2017
    #
    CONFIG_64BIT=y
    # CONFIG_X86_32 is not set
    *****省略****
    CONFIG_X86_MCE_INJECT=m

    [root@centos6 src ]#wc -l .config
    4726 .config


3、-------开始选择ntfs功能模块


  

[root@centos6 linux-4.16.13 ]#make menuconfig
      HOSTCC  scripts/basic/fixdep
     *** Unable to find the ncurses libraries or the
     *** required header files.
     *** 'make menuconfig' requires the ncurses libraries.
     ***
     *** Install ncurses (ncurses-devel or libncurses-dev
     *** depending on your distribution) and try again.
     ***
    make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
    make: *** [menuconfig] Error 2
    [root@centos6 linux-4.16.13 ]#yum install ncurses-devel    #此处的yum仓库配置见本文最下面
    [root@centos6 linux-4.16.13 ]#make menuconfig

    更改编译内核名称 ,

如图2


    General setup  --->


         ()  Local version - append to kernel release


                 起个名称,

如图3


    --------


    启用哪些特性


    make menuconfig:配置内核选项


        报错缺少工具


yum install ncurses-devel
        make menuconfig

        字符界面  * 表示支持  按空格键切换


                  空: 不支持


                  选项后的箭头表示有更多功能如networking support 回车


                          M表示以模块存放 即/lib/modules


                          *表示集成到内核中


                  内核名称含义:主版本,次版本号,修正版本号。其余是红帽编译加上去的


                  改General setup --->


                          Local version -append to kernel release 可以修改自己的,起名要规范


                  File systems --->


                          DOS/FAT/NT Filesystems --->


                              NTFS file system supoort 选择模块即m


                                  点上写功能,

如图4


    此时的.config已经改变,

如图5


[root@centos6 linux-4.16.13 ]#grep -i "oatmeal-cookie" .config
    CONFIG_LOCALVERSION="Oatmeal-cookie"
    [root@centos6 linux-4.16.13 ]#grep -i "ntfs" .config
    CONFIG_NTFS_FS=y
    CONFIG_NTFS_DEBUG=y
    CONFIG_NTFS_RW=y


4、-----make编译


   

[root@centos6 linux-4.16.13 ]#make -j 8
      HOSTCC  scripts/kconfig/conf.o
      HOSTLD  scripts/kconfig/conf
    scripts/kconfig/conf  --silentoldconfig Kconfig
    Makefile:974: *** "Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel".  Stop.
    调整一个cpu个数,加快速度
    make -j 8  8个cpu同时工作编译
    报错
    [root@centos6 linux-4.16.13 ]#yum install elfutils-libelf-devel
    [root@centos6 linux-4.16.13 ]#make -j 8
    #*****省略****
    scripts/sign-file.c:25:30: error: openssl/opensslv.h: No such file or directory
    #*****省略****
    又报错
    [root@centos6 linux-4.16.13 ]#yum install openssl-devel
    [root@centos6 linux-4.16.13 ]#make -j 8

        make编译开始,此过程时间较长,

如图6,看到物理机cpu已经100%工作



6、------复制驱动模块到/lib/modules


    make modules_install 复制编译完成的到/lib/modules


  

[root@centos6 modules ]#ls
    2.6.32-696.el6.x86_64  4.16.13Oatmeal  4.16.13Oatmeal-cookie
  #4.16.13Oatmeal此处的atmeal起名时为:atmeal cookie 结果报错。make modules_install找不到src/cookie,个人又重启编辑的,命名要规范


 7、--------复制安装vminuz和initramfs


  

[root@centos6 linux-4.16.13 ]#make install
    sh ./arch/x86/boot/install.sh 4.16.13Oatmeal-cookie arch/x86/boot/bzImage \
            System.map "/boot"
    ERROR: modinfo: could not find module ib_sa    #此处正常,这些模块没有安装而已
    ERROR: modinfo: could not find module ib_mad
    ERROR: modinfo: could not find module ib_addr
    ERROR: modinfo: could not find module microcode
    ERROR: modinfo: could not find module snd_page_alloc
    ERROR: modinfo: could not find module vmware_balloon
    ERROR: modinfo: could not find module crc_t10dif
    [root@centos6 linux-4.16.13 ]#cd /boot/
    [root@centos6 boot ]#ls
    config-2.6.32-696.el6.x86_64         System.map
    efi                                  System.map-2.6.32-696.el6.x86_64
    grub                                 System.map-4.16.13Oatmeal-cookie
    initramfs-2.6.32-696.el6.x86_64.img  vmlinuz
    initramfs-4.16.13Oatmeal-cookie.img  vmlinuz-2.6.32-696.el6.x86_64
    lost+found                           vmlinuz-4.16.13Oatmeal-cookie
    symvers-2.6.32-696.el6.x86_64.gz
    [root@centos6 boot ]#reboot


8、-----重启进入centos4.16.13Oatmeal-cookie

如图7



=====================(*)总结===============


    编译内核


    1.tar xvf linux-4.17.3.tar.xz


    2.yum -y groupinstall "Development Tools


         yum -y install ncurses-devel 


         yum -y install elfutils-libelf-devel


         yum -y install openssl-devel


    3.cp /boot/config-2.6.32-696.el6.x86_64 /usr/local/src/linux-4.16.13/.config


    4.make menuconfig


    5.make -j 8


    6.make modules_install


    7.make install


    8.reboot
--------------------------------

编译
    全编译:make [-j #]
    编译内核的一部分功能:
        (a) 只编译某子目录中的相关代码
            cd /usr/src/linux
            make dir/
        (b) 只编译一个特定的模块
            cd /usr/src/linux
            make dir/file.ko
            例如:只为e1000编译驱动:
            make drivers/net/ethernet/intel/e1000/e1000.ko

编译内核
    如何交叉编译内核:
    编译的目标平台与当前平台不相同
        make ARCH=arch_name
    要获取特定目标平台的使用帮助
        make ARCH=arch_name help
        make ARCH=arm help
内核编译
    在已经执行过编译操作的内核源码树做重新编译
    需要事先清理操作:
        make clean:清理大多数编译生成的文件,但会保留config文件等
        make mrproper: 清理所有编译生成的文件、config及某些备份文件
        make distclean:mrproper、patches以及编辑器备份文件
卸载内核
    删除/lib/modules/目录下不需要的内核库文件
    删除/usr/src/linux/目录下不需要的内核源码
    删除/boot目录下启动的内核和内核映像文件
    更改grub的配置文件,删除不需要的内核启动列表

            

centos 安装内核头文件 centos内核编译安装_centos 安装内核头文件


                                    图1

        

centos 安装内核头文件 centos内核编译安装_centos 安装内核头文件_02


                                        图2

    

centos 安装内核头文件 centos内核编译安装_centos内核编译_03


                                        图3

centos 安装内核头文件 centos内核编译安装_centos内核编译_04


                                        图4

centos 安装内核头文件 centos内核编译安装_centos 安装内核头文件_05


                                            图5

centos 安装内核头文件 centos内核编译安装_centos内核编译_06


                                图6

centos 安装内核头文件 centos内核编译安装_centos内核编译_07


                            图7



vim /etc/yum.repos.d/dvd.repo   
[base]                 
name=dhy centos7 $releasever 
baseurl=http://172.18.0.1/centos/$releasever
enabled=1
gpgcheck=1
gpgkey=file:///media/RPM-GPG-KEY-CentOS-7

[epel]							#eple源库
name=education centos7 $releasever dvdepl
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
enbaled=0	                                   #网络较慢,此epel仓库关闭,默认开启key校验
				        
[dvd]                                                     #创建yum库
name=createrepository
baseurl=file:///media/
gepcheck=0

[epel]
name=education centos7 $releasever $basearch dvdepl                                                                                                                                         
baseurl=http://172.18.0.1/epel/$releasever/$basearch
enbabled=1
gpgcheck=0