Project

General

Profile

Support #929

Emulate Raspberry Pi OS on x86_64 Arch Linux

Added by Daniel Curtis over 6 years ago. Updated almost 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Development
Target version:
Start date:
04/08/2018
Due date:
% Done:

100%

Estimated time:
2.50 h
Spent time:

Description

This is a guide on virtualizing an ARM based operating system, in this case Raspbian for the Raspberry Pi 3, on an x86_64 Arch Linux system.

Prepare the environment

  • Make sure the system is up to date:
    sudo pacman -Syu
    
  • Install a few dependencies:
    sudo pacman -S git base-devel wget
    

Prepare Disk Image

  • Create the guest drive:
    dd if=/dev/zero of=~/raspi.img bs=1M count=16384
    
  • Format the guest drive:
    fdisk ~/raspi.img
    
    • Basic format:
      n
      p
      1
      <enter>
      +200M
      n
      p
      <enter>
      <enter>
      <enter>
      w
      
  • Download the latest raspberry pi OS image:
    mkdir ~/raspios && cd ~/raspios
    wget https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2022-01-28/2022-01-28-raspios-bullseye-armhf-lite.zip
    unzip 2022-01-28-raspios-bullseye-armhf-lite.zip
    
  • Populate the guest drive:
    mkdir ~/src_boot && mkdir ~/src_root && mkdir ~/dst_boot && mkdir dst_root
    sudo losetup -f --show ~/raspi.img
    sudo partx -a /dev/loop0
    sudo mkfs.vfat /dev/loop0p1
    sudo mkfs.ext4 /dev/loop0p2
    sudo mount /dev/loop0p1 ~/dst_boot
    sudo mount /dev/loop0p2 ~/dst_root
    sudo losetup -f -o 4194304 --show ~/raspbian/2022-01-28-raspios-bullseye-armhf-lite.img
    sudo mount /dev/loop1 ~/src_boot
    sudo losetup -f -o 272629760 --show ~/raspios/2022-01-28-raspios-bullseye-armhf-lite.img
    sudo mount /dev/loop2 ~/src_root
    sudo cp -r ~/src_boot/*.* ~/dst_boot/
    sudo cp -r ~/src_root/* ~/dst_root/
    
  • Edit the guest drive fstab:
    sudo nano ~/root/etc/fstab
    
    • And change the following:
      /dev/sda1  /boot           vfat    defaults          0       2
      /dev/sda2  /               ext4    defaults,noatime  0       1
      
  • Unmount the guest drive:
    sudo umount ~/src_boot
    sudo umount ~/src_root
    sudo umount ~/dst_boot
    sudo umount ~/dst_root
    sudo losetup -d /dev/loop2
    sudo losetup -d /dev/loop1
    sudo losetup -d /dev/loop1
    

Install Qemu

  • Install qemu and the extra architectures:
    pacman -S git qemu qemu-arch-extra wget
    
  • Download prebuilt kernels:
    mkdir ~/git && cd ~/git
    git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git
    

Run Qemu

  • Run the newly built kernel with the latest raspbian:
    qemu-system-aarch64 -kernel ~/git/qemu-rpi-kernel/kernel-qemu-5.10.63-bullseye -cpu arm1176 -M raspi3b -m 1024 -dtb ~/git/qemu-rpi-kernel/native-emulation/dtbs/bcm2711-rpi-4-b.dtb -serial stdio -append "root=/dev/sda2 rootfstype=ext4 rw" -drive format=raw,file=/media/winshare/raspi.img -net nic,macaddr=20:20:20:67:89:0a -net user,hostfwd=tcp::5022-:22 -no-reboot
    
  • Log in as the user pi with the password raspberry
  • Configure the pi to your use case:
    sudo raspi-config
    
    • Go to Interfacing and enable SSH.
    • Change Localization.

Compiling Latest kernel

Install ARM toolchain:

  • Clone the toolchain repo (using --depth 1 will decrease the overall download size by 400MB):
    mkdir ~/git && cd ~/git
    git clone --depth 1 https://github.com/raspberrypi/tools.git
    
  • Add the tools to your path:
    echo 'export PATH=$PATH:~/git/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin' >> ~/.bash_profile
    
  • Source the .bash_profile:
    source ~/.bash_profile
    

Compile Kernel

  • Download the qemu-rpi-kernel repo:
    cd ~/git
    git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git
    
  • Clone the specific kernel version to use:
    cd qemu-rpi-kernel/tools
    git clone -b rpi-5.10.y --depth 1 https://github.com/raspberrypi/linux.git
    
  • Modify the kernel compile script to use Arch commands:
    sed -i.bak -e's/sudo\ apt-get\ update/sudo\ pacman\ -Syu/g' -e's/sudo\ apt-get\ install\ git\ libncurses5-dev\ gcc-arm-linux-gnueabihf/sudo\ pacman\ -S\ git/g' build-kernel-qemu
    
  • Edit the build-kernel-qemu script:
    vi build-kernel-qemu
    
    • And comment out all COMMIT versions except for the desired commit, and also comment out the exit command before the make commands:
      #COMMIT=6820d0cbec64cfee481b961833feffec8880111e
      #COMMIT=raspberrypi-kernel_1.20171029-1
      #COMMIT="" 
      COMMIT=rpi-5.10.y
      
      #exit
      
  • Run the build script:
    ./build-kernel-qemu
    

Resources

#1

Updated by Daniel Curtis over 6 years ago

  • Subject changed from Run Raspbian on x86_64 Arch Linux to Virtualize Raspbian on x86_64 Arch Linux
  • Description updated (diff)
#2

Updated by Daniel Curtis over 6 years ago

  • Description updated (diff)
  • Category set to Development
  • Status changed from New to In Progress
  • Estimated time set to 2.50 h
#3

Updated by Daniel Curtis over 6 years ago

  • Description updated (diff)
  • % Done changed from 0 to 100
#4

Updated by Daniel Curtis over 6 years ago

  • Status changed from In Progress to Resolved
#5

Updated by Daniel Curtis over 5 years ago

  • Status changed from Resolved to Closed
#6

Updated by Daniel Curtis almost 3 years ago

  • Description updated (diff)
  • Subject changed from Virtualize Raspbian on x86_64 Arch Linux to Emulate Raspberry Pi OS on x86_64 Arch Linux
#7

Updated by Daniel Curtis almost 3 years ago

  • Description updated (diff)
#8

Updated by Daniel Curtis almost 3 years ago

  • Description updated (diff)

Also available in: Atom PDF