如何下载Torvalds Linux Kernel母版,(重新)编译它,并使用QEMU引导它? [英] How to download the Torvalds Linux Kernel master, (re)compile it, and boot it with QEMU?

查看:162
本文介绍了如何下载Torvalds Linux Kernel母版,(重新)编译它,并使用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 up
    • vagrant ssh
    • Download kernel's sources: https://github.com/torvalds/linux
    • Unzipped it
    • Launch a terminal and run:
      • vagrant init
      • vagrant up
      • vagrant ssh
      • cd /vagrant/linux 为了进入Linux源的未压缩文件夹
      • make menuconfig 我刚刚按了确定/保存
      • sudo make -j 4 && sudo make modules_install -j 4 && sudo make install -j 4
      • cd /vagrant/linux In order to get into the Linux sources unzipped folder
      • make menuconfig I just pressed ok/save
      • 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编译"?因为我添加了一个syscall,并且我想测试它是否有效...我发现了关于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

      https://buildroot.org/

      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 \
      ;
      

      您现在位于外壳上,可以使用用户名root(无密码)登录.

      You are now left on a shell and you can login with username root (no password).

      QEMU引导命令在Buildroot本身中记录在:

      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,如以下说明所述:

      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:

      • cross compile userland tests to see what your kernel is doing by creating a hello world package like this
      • cross compile kernel modules out of tree, which is a good way to interact with the kernel directly without touching the mainline tree: How to add a Linux kernel driver module as a Buildroot package?

      我已经自动化并记录了所有这些内容在此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母版,(重新)编译它,并使用QEMU引导它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆