How to download the Torvalds Linux Kernel master, (re)compile it, and boot it with QEMU?

后端 未结 2 1784
囚心锁ツ
囚心锁ツ 2020-12-06 15:37

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

相关标签:
2条回答
  • 2020-12-06 15:54

    Good work please document the work and post it on git / blog lot of tweaking required to get linux kernel compile on mac OS X.

    Now to answer your question.

    1. Easiest way to run vmware/virtualbox. as they are straight forward. then scp image and test.
    2. QEMU is best way to test your kernel. but not widely used by general population. Also needs bit of efforts to get it working. Also need to compile Root FS to boot as you want to test syscall.(you can write c code using your syscall and define it as init process)

    I hope I have answered you.

    0 讨论(0)
  • 2020-12-06 16:12

    Buildroot

    https://buildroot.org/

    Buildroot is an awesome way to do this.

    In very few commands it:

    • downloads GCC and generates the cross compiler
    • downloads the Linux kernel, ulibc etc. and generates a root image
    • downloads QEMU and compiles it for you

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

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

    The QEMU boot command is documented in Buildroot itself at: https://github.com/buildroot/buildroot/blob/2018.02/board/qemu/aarch64-virt/readme.txt

    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?

    I have automated and documented all of those things on this GitHub repo. To make kernel development even better, that:

    • repo compiles the kernel directly rather than going through Buildroot
    • provides shortcuts to run and GDB step debug the kernel

    Other ISAs mentioned at: https://cirosantilli.com/linux-kernel-module-cheat/#buildroot-hello-world

    Tested in Ubuntu 18.04.

    0 讨论(0)
提交回复
热议问题