Skip to content

Instantly share code, notes, and snippets.

@JasonGhent
Last active March 24, 2024 14:36
Show Gist options
  • Save JasonGhent/e7deab904b30cbc08a7d to your computer and use it in GitHub Desktop.
Save JasonGhent/e7deab904b30cbc08a7d to your computer and use it in GitHub Desktop.
OSX raspberry pi emulation via QEMU. v2 attempt @ https://gist.github.com/JasonGhent/922f38f57c8cb77b10f3
# pulled from http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/
# expanded via http://superuser.com/questions/690060/how-to-enable-network-with-a-raspberry-pi-emulated-on-qemu
# tested with 2015-02-16-raspbian-wheezy.zip on OSX Mavericks
# OSX terminal
brew install qemu
# kernel-qemu is a linux kernel compiled with ARM1176 support.
# learn more here: http://xecdesign.com/compiling-a-kernel/
curl -OL http://xecdesign.com/downloads/linux-qemu/kernel-qemu
curl -o raspbian_latest.zip -L http://downloads.raspberrypi.org/raspbian_latest
unzip raspbian_latest.zip
mv 20* raspbian_latest.img
# not easily possible to mount ext4 to make the following change to the image, so we load the filesystem by running the image
qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -hda raspbian_latest.img
# QEMU window (guest) - spawned by previous line
sed -i -e 's/^/#/' /etc/ld.so.preload
# TODO: clean this up with a bash var for path
touch /etc/udev/rules.d/90-qemu.rules
echo 'KERNEL=="sda", SYMLINK+="mmcblk0"' >> /etc/udev/rules.d/90-qemu.rules
echo 'KERNEL=="sda?", SYMLINK+="mmcblk0p%n"' >> /etc/udev/rules.d/90-qemu.rules
echo 'KERNEL=="sda2", SYMLINK+="root"' >> /etc/udev/rules.d/90-qemu.rules
exit
# OSX terminal (host)
# start the final raspberry pi image and specify a network mapping
# of local port 5022 to the pi's port 22
qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda raspbian_latest.img -redir tcp:5022::22
# QEMU (guest) - raspberry pi emulation
# @FIXME
# for some reason it drops out of the startup procedure
# and dumps you to the terminal prematurely
exit
# if this is not first-run, we are done..
# else we are ushered into raspi-config (first run only)
# select "Finish" when done configuring
# OSX terminal (host)
ssh -p 5022 pi@localhost
# If we want to create a Heroku-like atmosphere, we can do
# with Dokku
## Still figuring this part out..
@mariohmol
Copy link

after all working.. can i open .img file and save all files in a SD card, then just input in raspberrypi?

@mariohmol
Copy link

Tested using both kernels and the latest raspbian img (2016-02-09-raspbian-jessie.img).
It closes the emulation window with message: "Kernel panic - not syncing : Attempted to kill init"

Looking in internet people say that is a problem with not compatible kernel, but image and kernel are same version 4.1.

Any tips? @jasonlagaac

Tested those commands:

  • qemu-system-arm -kernel kernel-qemu-4.1.13-jessie -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda 2016-02-09-raspbian-jessie.img
  • qemu-system-arm -kernel kernel-qemu-4.1.7-jessie -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -hda 2016-02-09-raspbian-jessie.img

@dpishchukhin
Copy link

@mariohmol update image, edit /etc/ld.so.preload and comment line #/usr/lib/arm-linux-gnueabihf/libarmmem.so

source: http://blog.3mdeb.com/2015/12/30/emulate-rapberry-pi-2-in-qemu/

@MrDwZ
Copy link

MrDwZ commented Apr 21, 2016

I wanna ask how to make the network able to work : (

@JPaulMora
Copy link

Hello guys! Check this project out: Raspi Simulator (not mine). If you see in package contents, its all shell scripts in a wrapper.

@qris
Copy link

qris commented Jun 15, 2017

I wasn't able to get a working init using this process. I had more luck with these instructions, although they are incomplete, and I don't have permission to edit the page. I had to mount the Ubuntu cloud image using qemu-nbd xenial-server-cloudimg-arm64-uefi1.img --connect=/dev/nbd0 and manually add a password for the ubuntu account as shown here, and wait for cloud-init to time out, before I could log into the machine and apt remove cloud-init.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment