avatar

Linux-内核编译

Linux-内核编译

一、下载源码

执行以下命令下载kernel源码

1
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.5.6.tar.xz

然后解压压缩包,接着cd到kernel目录中

二、编译配置环境

编译kernel需要很多库,所以请执行以下命令安装库

1
sudo apt-get install libncurses* build-essential openssl zlibc minizip libidn11-dev libidn11 libssl-dev flex ncurses-devel libncurses5-dev

三、Kernel编译配置

接下来开始配置kernel编译参数

1
make menuconfig

配置好了,保存为.config即可

四、开始编译

1
2
3
make
如果想要编译的更快可以用以下指令
make -j8

五、制作启动程序和文件系统

将下面的c代码静态编译,如果不静态编译的话,由于我们的文件系统中没有lib环境会导致程序没有对应的ld和libc无法启动。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void init(){
setbuf(stdin,0);
setbuf(stdout,0);
setbuf(stderr,0);
}
int main(){
init();
printf("Hello World(Kernel init)");
return 0;
}

制作rootfs

1
echo c程序的名字 | cpio -o --format=newc > rootfs

编译好了之后进入arch目录在x86_64文件夹内找到bzImage,将其单独取出来,然后放到自己的目录里,接下来开始启动kernel

1
2
3
4
5
qemu-system-x86_64   \
-kernel ./bzImage_x86_64 \
-initrd ./rootfs \
-append "root=/dev/ram rdinit=/c程序的名字 console=ttyS0" \
-nographic

当然为了方便起见,可以将上述代码写入到shell脚本中,便于下次的启动。

运行之后你会在log中发现输出Hello World(Kernel init)

拓展指令

1
2
3
4
cd /xxxx/linux-4.18.8	//切换到你解压源码的文件夹
make mrproper //清除编译过程中产生的所有中间文件
make clean //清除上一次产生的编译中间文件
make menuconfig //图形化界面,方便选择一些功能

[本文章可能存在出现的问题]

make menuconfig可能出现的问题

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
Q1: 3.14.38内核
root@simon-virtual-machine:/home/simon/FeiLing/src/linux-3.14.38# make menuconfig
*** Unable to find the ncurses libraries or the
*** required header files.
*** 'make menuconfig' requires the ncurses libraries.
***
*** Install ncurses (ncurses-devel) and try again.
***
/home/simon/FeiLing/src/linux-3.14.38/scripts/kconfig/Makefile:199: recipe for target 'scripts/kconfig/dochecklxdialog' failed
make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
Makefile:512: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2

A1: sudo apt-get install ncurses-devel



Q2: 4.17.2内核
root@simon-virtual-machine:/home/simon/Src/linux-4.17.2# make menuconfig
YACC scripts/kconfig/zconf.tab.c
/bin/sh: 1: bison: not found
scripts/Makefile.lib:196: recipe for target 'scripts/kconfig/zconf.tab.c' failed
make[1]: *** [scripts/kconfig/zconf.tab.c] Error 127
Makefile:528: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2

A2:apt-get install bison -y


Q3:
root@simon-virtual-machine:/home/simon/Src/linux-4.17.2# make menuconfig
YACC scripts/kconfig/zconf.tab.c
LEX scripts/kconfig/zconf.lex.c
/bin/sh: 1: flex: not found
scripts/Makefile.lib:188: recipe for target 'scripts/kconfig/zconf.lex.c' failed
make[1]: *** [scripts/kconfig/zconf.lex.c] Error 127
Makefile:528: recipe for target

A3: sudo apt-get install flex


Q4: 5.5.6内核
root@ubuntu:/home/robye/Kernel/linux-5.5.6# make menuconfig
*
* Unable to find the ncurses package.
* Install ncurses (ncurses-devel or libncurses-dev
* depending on your distribution).
*
* You may also need to install pkg-config to find the
* ncurses installed in a non-default location.
*
scripts/kconfig/Makefile:212: recipe for target 'scripts/kconfig/mconf-cfg' failed
make[1]: *** [scripts/kconfig/mconf-cfg] Error 1
Makefile:568: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2

A4: sudo apt-get install libncurses5-dev
文章作者: 咲夜南梦
文章链接: http://yoursite.com/2020/02/26/Linux-%E5%86%85%E6%A0%B8%E7%BC%96%E8%AF%91/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 咲夜南梦's 博客
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论