debian-编译内核 / kernel

  1. 可以从 https://cdn.kernel.org/ 下载想要的内核。
    清华源: https://mirrors.tuna.tsinghua.edu.cn/kernel/
  2. 下载
    1
    2
     curl -O -L https://mirrors.tuna.tsinghua.edu.cn/kernel/v5.x/linux-5.4.98.tar.xz
     curl -O -L https://mirrors.tuna.tsinghua.edu.cn/kernel/v5.x/linux-5.4.98.tar.sign
    
  3. 验证内核签名
    1
    2
     gpg --locate-keys torvalds@kernel.org gregkh@kernel.org
     unxz linux-5.4.98.tar.xz
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     # 使用 TOFU 信任对应的密钥
     ❯ gpg --tofu-policy good 647F28654894E3BD457199BE38DBBDC86092693E
    
     gpg: Setting TOFU trust policy for new binding <key: 647F28654894E3BD457199BE38DBBDC86092693E, user id: Greg Kroah-Hartman <gregkh@kernel.org>> to good.
    
     ❯ gpg --trust-model tofu --verify linux-5.4.98.tar.sign
    
     gpg: assuming signed data in 'linux-5.4.98.tar'
     gpg: Signature made 2021年02月13日 星期六 20时54分47秒 CST
     gpg:                using RSA key 647F28654894E3BD457199BE38DBBDC86092693E
     gpg: Good signature from "Greg Kroah-Hartman <gregkh@kernel.org>" [full]
     gpg: gregkh@kernel.org: Verified 1 signatures in the past 0 seconds.  Encrypted
         0 messages.
    
  4. 解压源码 tar -xf linux-5.4.98.tar
  5. 安装编译工具
    1
     sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev fakeroot bc
    
  6. 配置编译 make menuconfig
  7. 编译
    make -j 参与的线程数目
    make -j 参与的线程数目 deb-pkg

    make deb-pkg 在源码目录的父目录产生 5个 deb 包:

    • linux-image-version that contains the kernel image and the associated modules
    • linux-headers-version which contains the header files required to build external modules
    • linux-firmware-image-version which contains the firmware files needed by some drivers (this package might be missing when you build from the kernel sources provided by Debian)
    • linux-image-version-dbg which contains the debugging symbols for the kernel image and its modules
    • linux-libc-dev which contains headers relevant to some user-space libraries like GNU glibc.
  8. 安装 kernel 的 deb 包
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
     sudo dpkg -i linux-image-5.4.182+_5.4.182+-1_amd64.deb
    
     Selecting previously unselected package linux-image-5.4.182+.
     (Reading database ... 304693 files and directories currently installed.)
     Preparing to unpack linux-image-5.4.182+_5.4.182+-1_amd64.deb ...
     Unpacking linux-image-5.4.182+ (5.4.182+-1) ...
     Setting up linux-image-5.4.182+ (5.4.182+-1) ...
     dkms: running auto installation service for kernel 5.4.182+:
     Kernel preparation unnecessary for this kernel.  Skipping...
    
     Building module:
     cleaning build area...
     'make' all KVER=5.4.182+..............
     cleaning build area...
    
     DKMS: build completed.
    
     8812au.ko:
     Running module version sanity check.
     - Original module
       - No original module exists within this kernel
     - Installation
       - Installing to /lib/modules/5.4.182+/kernel/drivers/net/wireless/
    
     depmod...
    
     DKMS: install completed.
    
     Kernel preparation unnecessary for this kernel.  Skipping...
    
     Building module:
     cleaning build area...
     make -j10 KERNELRELEASE=5.4.182+ KVER=5.4.182+....
     cleaning build area...
    
     DKMS: build completed.
    
     wl.ko:
     Running module version sanity check.
     - Original module
       - No original module exists within this kernel
     - Installation
       - Installing to /lib/modules/5.4.182+/updates/dkms/
    
     depmod...
    
     Backing up initrd.img-5.4.182+ to /boot/initrd.img-5.4.182+.old-dkms
     Making new initrd.img-5.4.182+
     (If next boot fails, revert to initrd.img-5.4.182+.old-dkms image)
     update-initramfs........
    
     DKMS: install completed.
    
     Kernel preparation unnecessary for this kernel.  Skipping...
    
     Building module:
     cleaning build area...
     'make' clean; 'make' -j10 KVER=5.4.182+ KSRC=/lib/modules/5.4.182+/build all; if [[ -x /usr/sbin/kmodsign ]]; then kmodsign sha512 MOK.priv MOK.der 8814au.ko; fi......
     cleaning build area...
    
     DKMS: build completed.
    
     8814au.ko:
     Running module version sanity check.
     - Original module
       - No original module exists within this kernel
     - Installation
       - Installing to /lib/modules/5.4.182+/updates/dkms/
    
     depmod...
    
     DKMS: install completed.
    
     Kernel preparation unnecessary for this kernel.  Skipping...
    
     Building module:
     cleaning build area...
     'make' -j10 KVER=5.4.182+ USER_MODULE_NAME=rtl8821ce CONFIG_RTW_DEBUG=n.......
     cleaning build area...
    
     DKMS: build completed.
    
     rtl8821ce.ko:
     Running module version sanity check.
     - Original module
       - No original module exists within this kernel
     - Installation
       - Installing to /lib/modules/5.4.182+/updates/
    
     depmod...
    
     DKMS: install completed.
    
     Kernel preparation unnecessary for this kernel.  Skipping...
    
     Building module:
     cleaning build area...
     'make' KVER=5.4.182+.................................
     cleaning build area...
    
     DKMS: build completed.
    
     8821cu.ko:
     Running module version sanity check.
     - Original module
       - No original module exists within this kernel
     - Installation
       - Installing to /lib/modules/5.4.182+/kernel/drivers/net/wireless/realtek/rtl8821cu/
    
     depmod...
    
     DKMS: install completed.
     .
     update-initramfs: Generating /boot/initrd.img-5.4.182+
     I: The initramfs will attempt to resume from /dev/sda2
     I: (UUID=1c8a0cc0-57c6-4b27-a7fe-2829905df45b)
     I: Set the RESUME variable to override this.
     VirtualBox Guest Additions: Building the modules for kernel 5.10.0-9-amd64.
    
     VirtualBox Guest Additions: Look at /var/log/vboxadd-setup.log to find out what 
     went wrong
     Generating grub configuration file ...
     Found theme: /boot/grub/themes/mx_linux/theme.txt
     Found linux image: /boot/vmlinuz-5.10.0-9-amd64
     Found initrd image: /boot/initrd.img-5.10.0-9-amd64
     Found linux image: /boot/vmlinuz-5.4.182+
     Found initrd image: /boot/initrd.img-5.4.182+
     Found memtest86+ image: /boot/memtest86+.bin
     done
    
  9. 查看 kernel的安装情况

    1
     dpkg --list | grep linux-image
    
  10. 重启系统,在grub 启动选项中 Advanced 中找到刚刚安装的新kernel,启动之。
    或者,直接修改grub从心kernel启动

    • 直接修改grub从新kernel启动

      1. 检查grub 菜单项
        1
         sudo grep 'menuentry \|submenu ' /boot/grub/grub.cfg | cut -f2 -d "'"
        
      2. 根据grub菜单结构,修改 /etc/default/grub 的启动项
        GRUB_DEFAULT=0 改为
        1
         GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 5.4.0-104-generic"
        
      3. 更新grub并重启
        1
        2
         sudo update-grub
         sudo reboot
        
    • 启动菜单选择从新kernel启动

      1. 将新kernel设置为默认kernel
        1. Add two lines to /etc/default/grub
          1
          2
           GRUB_SAVEDEFAULT=true
           GRUB_DEFAULT=saved
          
        2. sudo update-grub
        3. sudo reboot
      2. 删除其他kernel
        1
        2
        3
        4
         sudo apt list --installed | grep linux-image
         sudo apt remove linux-image-xxx
         sudo update-grub
         sudo reboot
        
1
2
3
4
5
6
7
8
BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
Failed to generate BTF for vmlinux
Try to disable CONFIG_DEBUG_INFO_BTF
make[3]: *** [Makefile:1100: vmlinux] Error 1
make[2]: *** [debian/rules:6: build] Error 2
dpkg-buildpackage: error: debian/rules build subprocess returned exit status 2
make[1]: *** [scripts/Makefile.package:77: deb-pkg] Error 2
make: *** [Makefile:1469: deb-pkg] Error 2
  1. Install the Linux kernel modules
    1
    2
    3
    sudo make modules_install
    sed: can't read modules.order: No such file or directory
    make: *** [Makefile:1356: _modinst_] Error 2