Link
  1. How to emulate Rasbpian Buster with qemu
  2. How to emulate Raspbian Jessie with qemu

Before starting:

  • Download raspbian image from the official repository here
  • Download custom kernel AND .dtb file here

How to emulate Rasbpian Buster with qemu

  • mount the image temporarily
# calculate the offset. multiply the start sector * 512 so we can skip over the boot partition (512)
fdisk -l 2019-07-10-raspbian-buster-lite.img
  Disk 2019-07-10-raspbian-buster-lite.img: 2 GiB, 2197815296 bytes, 4292608 sectors
  Units: sectors of 1 * 512 = 512 bytes
  Sector size (logical/physical): 512 bytes / 512 bytes
  I/O size (minimum/optimal): 512 bytes / 512 bytes
  Disklabel type: dos
  Disk identifier: 0x17869b7d

  Device                               Boot  Start     End Sectors  Size Id Type
  2019-07-10-raspbian-buster-lite.img1        8192  532480  524289  256M  c W95 FA
  2019-07-10-raspbian-buster-lite.img2      540672 4292607 3751936  1.8G 83 Linux
# in the case above, multiply 540672 times 512 and the offset equals 276824064
# below is a one-liner to calculate the offset (...):
SECTOR=$( fdisk -l 2019-07-10-raspbian-buster-lite.img | grep Linux | awk '{ print $2 }' );OFFSET=$(( SECTOR * 512 ))&&echo $OFFSET

# now mount the image temporarily and edit the necessary files
sudo mount -v -o offset=276824064 -t ext4 2019-07-10-raspbian-buster-lite.img /mnt/tmp
  • edit etc/ld.so.preload and comment out the first and only line
  • now change the following lines in /etc/fstab:
### change these lines
> PARTUUID=17869b7d-01  /boot           vfat    defaults          0       2      
> PARTUUID=17869b7d-02  /               ext4    defaults,noatime  0       1

### to this:  (filesystem definitions are changed to /dev/sda1 and /dev/sda2)
> /dev/sda1  /boot           vfat    defaults          0       2      
> /dev/sda2  /               ext4    defaults,noatime  0       1

### save and exit
  • Now start QEMU:
    Note: the following will spawn a new QEMU window , but will fail to initialize the GUI display. I have not found a workaround at this time. Instead, it should boot directly into a shell in the same window the command is executed from.
    qemu-system-arm \
      -kernel kernel-qemu-4.19.50-buster \
      -dtb ../versatile-pb.dtb \
      -hda 2019-07-10-raspbian-buster-lite.img \
      -cpu arm1176 \
      -m 256 \
      -M versatilepb \
      -serial stdio \
      -net nic \
      -net user,hostfwd=tcp::5022-:22 \
      -append "root=/dev/sda2 rootfstype=ext4 rw" \
      -no-reboot
    
    • -serial stdio : redirects the boot output messages and the console to your terminal.
    • -kernel kernel-qemu-4.19.50-buster : relative path to kernel file
    • -dtb ../versatile-pb.dtb : relative path to dtb file
    • -hda 2019-07-10-raspbian-buster-lite.img : relative path to raspbian image file
    • -cpu arm1176 : select CPU model
    • -m 256 : guest RAM size in megabytes
    • -M versatilepb : general-purpose Linux target ; disadvantage is that it has a limit of only 256MB of RAM
    • append "root=/dev/sda2 rootfstype=ext4 rw" : contains all options to be passed to the kernel at boot time
    • -net nic -net user,hostfwd=tcp::5022-:22 : forwards the guest port 22 to local port 5022, for ssh’ing to the machine

How to emulate Raspbian Jessie with qemu

Note: the following will spawn a new QEMU window with the expected Rasbpian GUI

qemu-system-arm \
  -kernel kernel-qemu-4.4.34-jessie \
  -cpu arm1176 \
  -m 256 \
  -M versatilepb \
  -serial stdio \
  -append "root=/dev/sda2 rootfstype=ext4 rw" \
  -hda 2017-04-10-raspbian-jessie.img \
  -net nic -net user,hostfwd=tcp::5022-:22 \
  -no-reboot

Resources and More