Raspbian Hangs in Qemu

qemu-system-arm hangs with Raspberry Pi 2 image

"Nothing happens" and "tried to execute from a bogus address" are often the result of either:

  • misconfigured kernel (is this kernel definitely intended to boot on the raspi2 board, and not on something else?)
  • something goes wrong in early boot before the kernel manages to produce output (though usually this causes a hang rather than a bad-address output)

For the latter, assuming this really is a raspi2 kernel, you might try using
earlycon=pl011,0x3f201000
in your kernel append arguments. (The Linux kernel can produce earlycon output for the PL011 UART, but not for the raspi-specific 'mini UART'.)

I would suggest also dropping "-nographic" and "-serial mon:stdio" for the moment. Then you can use the graphical UI to check both UART outputs. (You can do this without using the GUI by redirecting them both correctly using two lots of -serial command line options, but then you have to figure out sensible places to send them; the GUI's simpler.) The first serial port will be the PL011, and the second the mini-UART, so if you only tell QEMU where to send the first serial port output and the guest is writing to the second, you'll never see it.

How to emulate Raspberry Pi Raspbian with QEMU?

You should expand the Raspbian Image file before starting

Mount the Raspbian Image file with kpartx (might have to install kpartx)

$ sudo kpartx -av your-image.img
add map loop0p1 (252:5): 0 117187 linear /dev/loop0 1
add map loop0p2 (252:6): 0 3493888 linear /dev/loop0 118784

$ sudo mount /dev/mapper/loop0p2 /mnt/img1
$ cd /mnt/img1

Modify the /etc/fstab and comment out the MMCBLK mounts

$ sudo nano etc/fstab

proc /proc proc defaults 0 0
#/dev/mmcblk0p1 /boot vfat defaults 0 2
#/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap[on|off] for that

Modify /etc/ld.so.preload and comment out lines ...

$ sudo nano etc/ld.so.preload

#/usr/lib/arm-linux-gnueabihf/libarmmem.so

Unmount and destroy loops from kpartx

$ sudo umount /mnt/img1
$ sudo kpartx -d your-image.img

Get the Qemu kernel matching the Raspbian image here...

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

I used this command to successfully emulate Raspbian Jessie

qemu-system-arm -kernel kernel-qemu-4.4.12-jessie -cpu arm1176 -m 256 -M versatilepb \
-no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-redir tcp:5022::22 \
-hda 2016-05-27-raspbian-jessie-lite.img

Trying to run Raspberry-Pi image under QEMU, but VM memory is limited to 256MB

The problem here is that a lot of people claim to be running "raspberry pi emulation in QEMU" when they're actually just running Raspbian userspace on top of a kernel for a different machine emulation. So it's easy to be confused if you look at several different tutorials that are really describing entirely different emulation setups. Look for what machine type they pass QEMU.

The "versatilepb" machine type gets used in a lot of tutorials, especially older ones, because it has been in QEMU a long time and it is possible to get it to work with the 1176 CPU that the classic Raspberry Pi boards used. This specific machine has a 256MB maximum memory size, because the real hardware it's emulating has that restriction (it's imposed by the way the physical memory address space is designed). This machine type will never be able to support more RAM, so if you need more then you should ignore any tutorial or setup that uses it.

More recent versions of QEMU really do emulate the actual raspberry pi hardware; these are the raspi0, raspi1ap, raspi2b, raspi3ap, raspi3b machine types. These will have the same amount of RAM as the real raspi hardware they're emulating (either 512MB or 1GB). The downside of these board models is that some of the device emulation is lacking features -- so older QEMU will often not correctly boot a newer kernel, and sometimes devices you would like to use are not present. Also, because the raspi boards hang their ethernet device off the USB controller, the only way to get ethernet on these QEMU models would also be to use a USB ethernet device, eg with:

-device usb-net,netdev=eth0 -netdev user,id=eth0

This probably needs a recent QEMU version to get a working USB controller.

I don't know if there are any tutorials/recipes for running Raspbian on top of the QEMU "virt" board. If there are, this would probably be the best experience, because the virt board permits lots of memory, PCI devices, virtio devices, and is well maintained.



Related Topics



Leave a reply



Submit