本文主要讲述,如何编译一个linux内核,使其默认执行我们提供的一个hello world程序。
基本思路是,linux内核在启动完毕后,会执行一个用户态的名叫init的程序,作为其1号进程(当前大部分linux发行版的init程序都是systemd),那我们只要把我们写的hello world程序,设置成内核默认使用的init程序,应该就可以了。
下面我们来具体操作试下:
1. 下载内核代码。
$ git clone https://github.com/gregkh/linux.git2. 为确保内核代码是干净的,执行如下命令。
$ cd linux$ make mrproper3. 在当前linux目录写个c语言版的hello world程序,并将其编译以备后续使用。
$ 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.c4. 修改以下文件,告知内核在编译时,将我们的程序作为init程序一起打包到内核中。
$ git status -s M usr/default_cpio_list?? a.out?? hello.c $ git -P diff usr/default_cpio_listdiff --git a/usr/default_cpio_list b/usr/default_cpio_listindex 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 05. 为了方便,我们直接使用以下命令生成默认的内核配置。
$ make defconfig省略部分输出*** Default configuration is based on 'x86_64_defconfig'## configuration written to .config#6. 现在一切都准备好了,我们可以开始编译内核了。
$ make -j4省略部分输出Kernel: arch/x86/boot/bzImage is ready (#1)7. 内核编译好后,我们用qemu测试下。
$ qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -nographic -append "console=ttyS0"省略部分输出[ 2.122598] Run /init as init processhello 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
---来自腾讯云社区的---wangyuntao
微信扫一扫打赏
支付宝扫一扫打赏