qemu-rpi-kernel

repository·master·Indexed 24 days ago

https://github.com/dhruvvyas90/qemu-rpi-kernel

Pre-compiled Linux kernel images and device tree blobs (.dtb) designed to run Raspberry Pi (Raspbian) images on QEMU using the versatilepb machine type. Includes guides for native raspi2/raspi3 emulation, libvirt guest creation, and scripts for building custom kernels on Debian/Ubuntu.

Tokens
2.1K
Snippets
6
Records
13
Agent score
80%

What's inside qemu-rpi-kernel

  1. Build the kernel on Windows or macOS

    master

    The build-kernel-qemu script is designed for Debian-based distributions. To build the kernel on Windows or macOS, you must manually replicate the logic found in the build-kernel-qemu script.

    Ensure you have the following installed and available:

    • An ARM toolchain (e.g., gcc-arm-linux-gnueabihf equivalents for your OS).
    • All other dependencies required by the kernel build process.

    Refer to the build-kernel-qemu script line-by-line to guide your manual build process on these platforms.

  2. Emulate Raspberry Pi using pre-compiled versatilepb kernels with QEMU

    master

    To emulate a Raspberry Pi using the versatilepb machine in QEMU, you need a Raspbian .img file, a kernel image from this repository, and a corresponding .dtb (device tree blob) file.

    Prerequisites

    1. Obtain Raspbian: Download a Raspbian image from the official Raspberry Pi website and extract the .zip to get the .img file.
    2. Select Kernel and DTB: Match your Raspbian version to the appropriate kernel and DTB file provided in this repository (see Choosing a kernel image).

    Execution

    Run the qemu-system-arm command, ensuring you adjust the paths to your disk image, .dtb file, and kernel image.

    $ qemu-system-arm \
      -M versatilepb \
      -cpu arm1176 \
      -m 256 \
      -hda /.../2019-09-26-raspbian-buster-lite.img \
      -net user,hostfwd=tcp::5022-:22 \
      -dtb /.../versatile-pb-buster.dtb \
      -kernel /.../kernel-qemu-4.19.50-buster \
      -append 'root=/dev/sda2 panic=1' \
      -no-reboot
  3. Create a Raspberry Pi guest using libvirt

    master

    If you are using libvirt (version 5.0.0 or later), you can use virt-install to create a guest named pi. This allows you to manage the emulation using standard tools like virsh and virt-manager. The kernel and DTB are passed via the --boot flag.

    $ virt-install \
      --name pi \
      --arch armv6l \
      --machine versatilepb \
      --cpu arm1176 \
      --vcpus 1 \
      --memory 256 \
      --import \
      --disk /.../2019-09-26-raspbian-buster-lite.img,format=raw,bus=virtio \
      --network user,model=virtio \
      --video vga \
      --graphics spice \
      --rng device=/dev/urandom,model=virtio \
      --boot 'dtb=/.../versatile-pb-buster.dtb,kernel=/.../kernel-qemu-4.19.50-buster,kernel_args=root=/dev/vda2 panic=1' \
      --events on_reboot=destroy
  4. Build the kernel on Debian/Ubuntu

    master

    To automate the kernel building process on a Debian-based distribution, use the build-kernel-qemu script. Follow these steps:

    1. Clone this repository and navigate to the directory.
    2. Prepare configuration files: Create build-kernel-qemu.conf, config_file, and linux-arm.patch for your target kernel version (you can copy existing versioned files as templates).
    3. Install required dependencies: sudo apt install build-essential gcc-arm-linux-gnueabihf bison flex libncurses-dev
    4. Execute the build script: ./build-kernel-qemu
    5. Configure the kernel: Use the menu configuration interface to make desired choices or save the defaults.

    Note on USB Webcams: If you enable USB webcam modules in the build script, ensure you select the correct drivers in menuconfig. Be aware that while USB generally works in QEMU, USB webcams may encounter issues.

  5. Extract kernel and DTB files from a Raspberry Pi image

    master

    To emulate a Raspberry Pi using QEMU's native raspi2 or raspi3 machines, you must first extract the kernel image and relevant Device Tree Blob (DTB) files from your Raspberry Pi .img file.

    Follow these steps:

    1. Mount the image file: Use losetup to create a loop device and mount the first partition.
    2. Copy files: Copy all files matching kernel* and *.dtb from the mounted partition to your current working directory.
    3. Cleanup: Unmount the partition and detach the loop device.
    # Mount the image file
    sudo mkdir /mnt/rpi
    sudo losetup -f --show -P <path-to-rpi-image-file>
    sudo mount /dev/loop<no>p01 /mnt/rpi
    
    # Copy kernel files / dtb files
    cp /mnt/rpi/kernel* .
    cp /mnt/rpi/*.dtb .
    
    # Unmount the image file
    sudo umount /mnt/rpi
    sudo losetup -d /dev/loop<no>
  6. Use kernel 5.4.51 with QEMU (Tested with Raspbian Buster Lite)

    master

    For kernel version 5.4.51, use a specific QEMU command configuration that utilizes virtio-blk-pci and updated kernel arguments. Ensure the root parameter in -append points to /dev/vda2.

    $ qemu-system-arm \
      -M versatilepb \
      -cpu arm1176 \
      -m 256 \
      -drive "file=/.../2020-05-27-raspios-buster-lite-armhf.img,if=none,index=0,media=disk,format=raw,id=disk0" \
      -device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" \
      -net "user,hostfwd=tcp::5022-:22" \
      -dtb /.../versatile-pb-buster-5.4.51.dtb \
      -kernel /.../kernel-qemu-5.4.51-buster \
      -append 'root=/dev/vda2 panic=1' \
      -no-reboot
  7. Emulate Raspberry Pi 3B+ using QEMU

    master

    Run the following command to start emulation of a Raspberry Pi 3B+ using the qemu-system-aarch64 emulator.

    Note: Ensure you have already extracted the kernel8.img and the appropriate .dtb file from your image, and that the -sd argument points to your actual Raspberry Pi image file.

    sudo qemu-system-aarch64 -M raspi3b -append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1" -dtb ./dtbs/bcm2710-rpi-3-b-plus.dtb -sd 2020-08-20-raspios-buster-armhf-full.img -kernel kernel8.img -m 1G -smp 4 -serial stdio -usb -device usb-mouse -device usb-kbd