前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >编译一个默认输出hello world的linux内核

编译一个默认输出hello world的linux内核

作者头像
KINGYT
修改2020-05-18 16:24:08
1K0
修改2020-05-18 16:24:08
举报

本文主要讲述,如何编译一个linux内核,使其默认执行我们提供的一个hello world程序。

基本思路是,linux内核在启动完毕后,会执行一个用户态的名叫init的程序,作为其1号进程(当前大部分linux发行版的init程序都是systemd),那我们只要把我们写的hello world程序,设置成内核默认使用的init程序,应该就可以了。

下面我们来具体操作试下:

1. 下载内核代码。

代码语言:javascript
复制
$ git clone https://github.com/gregkh/linux.git

2. 为确保内核代码是干净的,执行如下命令。

代码语言:javascript
复制
$ cd linux
$ make mrproper

3. 在当前linux目录写个c语言版的hello world程序,并将其编译以备后续使用。

代码语言:javascript
复制
$ git status -s
?? hello.c
$ cat hello.c
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
        printf("hello world!\n");
        sleep(1000);
        return 0;
}
$ gcc -static hello.c
$ git status -s
?? a.out
?? hello.c

4. 修改以下文件,告知内核在编译时,将我们的程序作为init程序一起打包到内核中。

代码语言:javascript
复制

$ git status -s
 M usr/default_cpio_list
?? a.out
?? hello.c

$ git -P diff usr/default_cpio_list
diff --git a/usr/default_cpio_list b/usr/default_cpio_list
index 37b3864066e8..9c6b452d4c44 100644
--- a/usr/default_cpio_list
+++ b/usr/default_cpio_list
@@ -4,3 +4,4 @@
 dir /dev 0755 0 0
 nod /dev/console 0600 0 0 c 5 1
 dir /root 0700 0 0
+file /init ./a.out 755 0 0

5. 为了方便,我们直接使用以下命令生成默认的内核配置。

代码语言:javascript
复制

$ make defconfig
省略部分输出
*** Default configuration is based on 'x86_64_defconfig'
#
# configuration written to .config
#

6. 现在一切都准备好了,我们可以开始编译内核了。

代码语言:javascript
复制

$ make -j4
省略部分输出
Kernel: arch/x86/boot/bzImage is ready  (#1)

7. 内核编译好后,我们用qemu测试下。

代码语言:javascript
复制
$ qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -nographic -append "console=ttyS0"
省略部分输出
[    2.122598] Run /init as init process
hello world!
[    2.478037] tsc: Refined TSC clocksource calibration: 2903.981 MHz
[    2.480449] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x29dbf3edc72, max_idle_ns: 440795319519 ns
[    2.483100] clocksource: Switched to clocksource tsc
[    2.643255] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3

由上可见,hello world正确输出,搞定!


以上步骤中有很多知识点我没有讲,如果对此有兴趣,可以留言讨论,或者看下以下代码或文档:

usr/目录下的所有

init/main.c

init/initramfs.c

Documentation/filesystems/ramfs-rootfs-initramfs.rst

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linux内核及JVM底层相关技术研究 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档