25

A few years ago, people were saying they were able to emulate a Raspberry Pi 2 or 3 in QEMU. However as of late 2020, I am unable to do so. Can anyone let me know what the correct command line arguments are to emulate a Raspberry Pi 3 or 4 in either 32- or 64-bit mode is today? My goal is 64-bit Raspberry Pi OS. I have Qemu 5.1.0 installed.

wudude
  • 435
  • 1
  • 5
  • 10

4 Answers4

10

You can use

qemu-system-arm -machine type=raspi2 -m 1024 -kernel vmlinux -initrd initramfs
qemu-system-aarch64 -machine type=raspi3 -m 1024 -kernel vmlinux -initrd initramfs

The kernel and initramfs you can find on the first partition of your SD card or extract from the OS image for a Raspian Pi.

If you want a 64bit OS then you need to use qemu-system-aarch64 with the raspi3 type and a 64bit kernel.

Note: qemu only emulates some aspects of the Rapsberry Pi. Linux kernel can be build generic enough to handle the differences on the fly but that depends on what your distribution has configured for the kernel. You might have to get a more generic kernel for it to boot properly.

5

While the raspi3b model works well, it is quite inefficient and its network speeds are really low because it emulates the USB-Ethernet adapter. It's also locked to 4 CPUs and 1 GB of memory. I've had better success using the virt model which emulates a generic ARM system, but uses virtualization, rather than device emulation, which leads to lower CPU utilization on the host and much better I/O performance. It also supports arbitrary CPU counts and memory sizes.

Getting it running is a bit more work but in the end not too complicated (more instructions here). First install a virtualization-capable kernel, Raspian doesn't provide one but the regular Debian ARM distro does:

wget http://security.debian.org/debian-security/pool/updates/main/l/linux/linux-image-5.10.0-21-armmp-lpae_5.10.162-1_armhf.deb
sudo dpkg --install linux-image-5.10.0-21-armmp-lpae_5.10.162-1_armhf.deb

Copy out the kernel and initrd from /boot to the host, then run QEMU with something like this:

qemu-system-arm \
    -nographic \
    -machine virt \
    -cpu cortex-a7 \
    -m 2G -smp 4 \
    -drive file=/rpi/root.img,format=raw,id=hd,if=none,media=disk \
    -device virtio-scsi-device -device scsi-hd,drive=hd \
    -device virtio-net-device,netdev=net0 \
    -netdev tap,id=net0,ifname=tap0,script=no,downscript=no \
    -kernel /rpi/vmlinuz-5.10.0-21-armmp-lpae \
    -initrd /rpi/initrd.img-5.10.0-21-armmp-lpae \
    -append 'root=/dev/sda1 panic=1 console=ttyAMA0,115200'

The above runs a 32-bit kernel, if you were already running 64-bit Raspbian use the arm64 kernel instead and replace qemu-system-arm with qemu-system-aarch64.

Wim
  • 161
  • 1
  • 2
2

After many trials, I could emulate my Raspberry Pi 2 with this:

qemu-system-arm \
  -M versatilepb \
  -cpu arm1176 \
  -m 256 \
  -drive "file=raspbian_backup-2.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

where raspbian_backup-2.img is the image of my personal Raspberry Pi 2 micro SDcard.

Unfortunately, I did this a few weeks ago and don't remember much more.

I use Ubuntu 20.10 with up to date packages.

One problem when trying this is that QEMU reacts slowly (at least on my old machine) so it may look as if stuck.

1

What you are asking for precisely is apparently not yet possible for a normal user without developments.

Firstly, QEMU 5.0.0 does not support Raspberry Pi 3 or 4:

% qemu-system-arm -version          
QEMU emulator version 5.0.0 (Debian 1:5.0-5ubuntu9.2)

% qemu-system-arm -M help|grep rasp raspi2 Raspberry Pi 2B

See also https://wiki.qemu.org/Documentation/Platforms/ARM#Supported_Machines.

I don't have qemu version 5.1.0, please check.

Secondly, the latest Raspberry Pi OS Buster uses 32 bits, see https://www.raspberrypi.org/software/operating-systems/.

64 bits is unnecessary as long as Raspberry Pi does not have more than 4GB of RAM.

Indeed I see that Raspberry Pi 4 can have up to 8GB of RAM now and that 64 bit OS is coming:

https://www.zdnet.com/article/new-raspberry-pi-os-update-first-8gb-pi-4-now-you-get-these-latest-features/

https://www.raspberrypi.org/forums/viewtopic.php?f=117&t=275370

I can answer for Raspberry Pi 2 with Raspberry Pi OS Buster 32 bits, if you are interested, let me know.