如何下载 Torvalds Linux Kernel master,(重新)编译它,并用 QEMU 引导它? [英] How to download the Torvalds Linux Kernel master, (re)compile it, and boot it with QEMU?
问题描述
这几天,我正在做一个小项目,以了解内核编程.但是,我在网上看了很多,之前问过一个关于内核编译的问题,我还是觉得有些不知所措.
Since few days, I am working on a little project in order to discover the kernel programming. However, I read a lot on the internet and asked a question before about kernel compilation and I feel still lost at some point.
如果我需要进行一些内核编程,我认为使用 Linus Torvalds 的 Linux 源代码是最好的起点.所以我做了以下(来自 MacOS High Sierra):
If I need to do some kernel programming, I thought that use the Linus Torvalds's Linux sources would be the best point to start. So I did the following (From MacOS High Sierra):
- 下载内核源:https://github.com/torvalds/linux
- 解压
- 启动终端并运行:
vagrant init
流浪
vagrant ssh
cd/vagrant/linux
为了进入Linux源码解压文件夹make menuconfig
我刚刚按下了确定/保存sudo make -j 4 &&sudo make modules_install -j 4 &&sudo make install -j 4
现在,我得到一个小错误:
Now, I just get a little error:
agrant @vagrant-ubuntu-trusty-64:/vagrant/Kernel-Programming/linx-kernel$ sudo make -j 4 && sudo make modules_install -j 4 && sudo make install -j 4 CHK include/config/kernel.release CHK include/generated/uapi/linux/version.h CHK include/generated/utsrelease.h CHK scripts/mod/devicetable-offsets.h CHK include/generated/timeconst.h CHK include/generated/bounds.h CHK include/generated/asm-offsets.h CALL scripts/checksyscalls.sh CHK include/generated/compile.h SKIPPED include/generated/compile.h CC my_syscalls/uptime.o AR my_syscalls/built-in.o CC[M] net/ipv4/netfilter/ipt_ECN.o In file included from net/ipv4/netfilter/ipt_ECN.c:20:0: net/ipv4/netfilter/ipt_ECN.c: In function 'set_ect_ip': ./include/uapi/linux/netfilter_ipv4/ipt_ECN.h:16:27: error: 'XT_DSCP_MASK' undeclared(first use in this function) #define IPT_ECN_IP_MASK (~XT_DSCP_MASK) ^ net/ipv4/netfilter/ipt_ECN.c:33:18: note: in expansion of macro 'IPT_ECN_IP_MASK' if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) { ^ ./include/uapi/linux/netfilter_ipv4/ipt_ECN.h:16:27: note: each undeclared identifier is reported only once for each function it appears in #define IPT_ECN_IP_MASK (~XT_DSCP_MASK) ^ net/ipv4/netfilter/ipt_ECN.c:33:18: note: in expansion of macro 'IPT_ECN_IP_MASK' if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) { ^ net/ipv4/netfilter/ipt_ECN.c: In function 'ecn_tg_check': ./include/uapi/linux/netfilter_ipv4/ipt_ECN.h:16:27: error: 'XT_DSCP_MASK' undeclared(first use in this function) #define IPT_ECN_IP_MASK (~XT_DSCP_MASK) ^ net/ipv4/netfilter/ipt_ECN.c:105:23: note: in expansion of macro 'IPT_ECN_IP_MASK' if (einfo->ip_ect & ~IPT_ECN_IP_MASK) { ^ make[3]: *** [net / ipv4 / netfilter / ipt_ECN.o] Error 1 make[2]: *** [net / ipv4 / netfilter] Error 2 make[1]: *** [net / ipv4] Error 2 make: *** [net] Error 2 make: ***Waiting for unfinished jobs....
但是,我不知道这个错误是否会破坏一切.另外,即使编译完全正常,我如何测试我的新编译的 Linux"?因为我添加了一个系统调用,我想测试它是否有效......我发现了一些关于
update-initramfs
的信息,但我没有看到链接/点,因为我编译的内核不是一个 Ubuntu.However, I don't know if this error breaks everything at all or not. Also, even if the compilation is fully ok, how can I test my "new Linux compiled"? Because I added a syscall and I would like to test if it works or no ... I found something about
update-initramfs
but I don't see the link/point since my compiled kernel isn't an Ubuntu.推荐答案
Buildroot
Buildroot 是一个很棒的方法.
Buildroot is an awesome way to do this.
在很少的命令中:
- 下载 GCC 并生成交叉编译器
- 下载 Linux 内核、ulibc 等并生成根映像
- 下载 QEMU 并为您编译
对于 aarch64,命令是:
For aarch64 the commands are:
git clone https://github.com/buildroot/buildroot cd buildroot git checkout 2018.02 make qemu_aarch64_virt_defconfig printf ' BR2_CCACHE=y BR2_PACKAGE_HOST_QEMU=y BR2_PACKAGE_HOST_QEMU_LINUX_USER_MODE=n BR2_PACKAGE_HOST_QEMU_SYSTEM_MODE=y BR2_PACKAGE_HOST_QEMU_VDE2=y ' >>.config make olddefconfig time make BR2_JLEVEL="$(nproc)" HOST_QEMU_OPTS='--enable-sdl --with-sdlabi=2.0' ./output/host/usr/bin/qemu-system-aarch64 -M virt -cpu cortex-a57 -nographic -smp 1 -kernel output/images/Image -append "root=/dev/vda console=ttyAMA0" -netdev user,id=eth0 -device virtio-net-device,netdev=eth0 -drive file=output/images/rootfs.ext4,if=none,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 ;
您现在处于 shell 状态,您可以使用用户名
root
(无密码)登录.You are now left on a shell and you can login with username
root
(no password).QEMU 启动命令记录在 Buildroot 中:https://github.com/buildroot/buildroot/blob/2018.02/board/qemu/aarch64-virt/readme.txt
The QEMU boot command is documented in Buildroot itself at: https://github.com/buildroot/buildroot/blob/2018.02/board/qemu/aarch64-virt/readme.txt
然后要使用您自己的 Linux 内核源代码,您基本上只需要使用
LINUX_OVERRIDE_SRCDIR
如在以下位置解释的:如何修改Buildroot包的源码进行包开发?Then to use your own Linux kernel source, you basically just have to use
LINUX_OVERRIDE_SRCDIR
as explained at: How to modify the source of Buildroot packages for package development?然后基本上免费,您还可以:
And then basically for free you also get a way to:
- 通过创建一个hello world 这样的包
- 从树中交叉编译内核模块,这是一种无需接触主线树而直接与内核交互的好方法:如何将Linux内核驱动模块添加为Buildroot包?
我已经自动化并记录了所有这些事情在此 GitHub 存储库上.为了使内核开发更好,那:
I have automated and documented all of those things on this GitHub repo. To make kernel development even better, that:
- repo 直接编译内核而不是通过 Buildroot
- 提供运行和 GDB 单步调试内核的快捷方式
在以下位置提到的其他 ISA:https://cirosantilli.com/linux-kernel-module-cheat/#buildroot-hello-world
Other ISAs mentioned at: https://cirosantilli.com/linux-kernel-module-cheat/#buildroot-hello-world
在 Ubuntu 18.04 中测试.
Tested in Ubuntu 18.04.
这篇关于如何下载 Torvalds Linux Kernel master,(重新)编译它,并用 QEMU 引导它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!