23

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.

2
  • 5
    "I am unable to do so" - what have you tried? What command line do you use, that fails? Please edit the question and add the information there. Please don't use a comment.
    – Ingo
    Sep 30, 2020 at 10:52
  • I usually use Docker to manage my images. If you install QEMU and the right binfmt configuration, you can simply run the ARM version of Raspbian inside of a Docker container, and you don't have to configure QEMU manually. I explained the process in this answer. It works great for running applications or compiling code natively. If you want to emulate the entire boot process and the kernel etc., you'll probably have to use QEMU from the command line.
    – tttapa
    Sep 30, 2020 at 15:05

4 Answers 4

7

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.

6
  • What processor (intel, arm) is used to run the guests kernel?
    – Ingo
    Sep 30, 2020 at 16:34
  • A cortex-a52 or cortex-a72 I believe. You can set the processor independently using -mcpu=.... qemu has a long list of options. Sep 30, 2020 at 16:37
  • 2
    There is no obvious initial RAM disk in /boot. Do you mean bootcode.bin? There are 4 kernels in /boot. (kernel7.img kernel7l.img kernel8.img kernel.img) Which one to use?
    – wudude
    Oct 1, 2020 at 14:49
  • Maybe your distribution has none. You can boot linux without one. It's just not very common to do so. Oct 1, 2020 at 16:53
  • 1
    How do you attack a disk image to raspi2?
    – OrangeDog
    Aug 24 at 11:07
2

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.

3
  • Yes if you can prove that I can emulate the Rpi 2 in 32-bit mode that would be helpful. So far I have had no luck. I'm running Qemu on the Rpi4.
    – wudude
    Jan 31, 2021 at 21:25
  • I have stock qemu 4.2.1 from Ubuntu 20.04 repositories. It seems to support Raspberry Pi 3 emulation, but only with qemu-system-aarch64. Jun 19, 2021 at 19:40
  • 6
    qemu-system-arm only emulates 32-bit CPUs, which is why it only supports up to the Pi 2. Since the Pi 3 and later have 64-bit CPUs, you need to use qemu-system-aarch64 instead. 64-bit is backwards compatible with 32-bit programs, so you can still use qemu-system-aarch64 to run a 32-bit OS if you wish, just like on the real hardware.
    – Malvineous
    Jul 3, 2021 at 14:59
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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.