Skip to content

Instantly share code, notes, and snippets.

@hfreire
Last active March 24, 2024 14:35
Show Gist options
  • Save hfreire/5846b7aa4ac9209699ba to your computer and use it in GitHub Desktop.
Save hfreire/5846b7aa4ac9209699ba to your computer and use it in GitHub Desktop.
How to emulate a Raspberry Pi (Raspbian Jessie) on Mac OSX (El Capitan)
# Install QEMU OSX port with ARM support
sudo port install qemu +target_arm
export QEMU=$(which qemu-system-arm)
# Dowload kernel and export location
curl -OL \
https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.1.7-jessie
export RPI_KERNEL=./kernel-qemu-4.1.7-jessie
# Download filesystem and export location
curl -o 2015-11-21-raspbian-jessie.zip \
-L http://downloads.raspberrypi.org/raspbian/images/raspbian-2015-11-24/2015-11-21-raspbian-jessie.zip
unzip 2015-11-21-raspbian-jessie.zip
export RPI_FS=./2015-11-21-raspbian-jessie.img
# Tweak filesystem: start qemu with init flag, switch to guest window to execute tweak and close window afterwards
$QEMU -kernel $RPI_KERNEL \
-cpu arm1176 -m 256 \
-M versatilepb -no-reboot -serial stdio \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" \
-hda $RPI_FS
sed -i -e 's/^/#/' /etc/ld.so.conf
sed -i -e 's/^/#/' /etc/fstab
# Emulate Raspberry Pi
$QEMU -kernel $RPI_KERNEL \
-cpu arm1176 -m 256 \
-M versatilepb -no-reboot -serial stdio \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-hda $RPI_FS \
-redir tcp:5022::22
# Login to Raspberry Pi
ssh -p 5022 pi@localhost
# Referenced from OSX raspberry pi emulation via QEMU - https://gist.github.com/JasonGhent/e7deab904b30cbc08a7d
# Referenced from Emulating Jessie image with 4.1.7 kernel - https://github.com/dhruvvyas90/qemu-rpi-kernel/wiki/Emulating-Jessie-image-with-4.1.7-kernel
@azpwnz
Copy link

azpwnz commented Nov 29, 2017

@224XS Hi, did you have success with enabling networking on RPi emulator?

@chaimpeck
Copy link

The instructions from @bainss work on Mac OS High Sierra.

@alfredosola
Copy link

export RPI_FS=./2017-03-02-raspbian-jessie.zip should be export RPI_FS=./2017-03-02-raspbian-jessie.img

@esell
Copy link

esell commented Feb 11, 2018

@224XS, I was able to get things working by tweaking the networking flags to:

-net user,hostfwd=tcp::5022-:22,vlan=0 -net nic,vlan=0

Per the qemu docs this format is deprecated but it still seems work just fine

@tofutim
Copy link

tofutim commented Feb 20, 2018

I still get ssh_exchange_identification: Connection closed by remote host. Any idea how to overcome that? I did add to hosts.allow sshd = ALL, to no avail.

@gae123
Copy link

gae123 commented Mar 26, 2018

@tofutim Initially, I had the same experience.

I realized that no ssh daemon was running on the device. I started one by running the following on the device:

sudo service ssh start

Then I was able to ssh using ssh -p 5022 pi@localhost and the default password raspberry

@madhavajay
Copy link

Anyone have any idea how to get this working for Tinker OS?

@hanscappelle
Copy link

Tested on High Sierra with instructions from @bainss combined with instructions from @MrAndersonMD

Install QEMU OSX port with ARM support

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" && brew update && brew install qemu
export QEMU=$(which qemu-system-arm)
brew install wget

get jessie kernel and raspbian image

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.4.34-jessie
export RPI_KERNEL=./kernel-qemu-4.4.34-jessie
wget http://downloads.raspberrypi.org/raspbian/images/raspbian-2017-03-03/2017-03-02-raspbian-jessie.zip
unzip 2017-03-02-raspbian-jessie.zip
export RPI_FS=./2017-03-02-raspbian-jessie.img

Here make sure to reference the .img file in the last EXPORT and not the .zip file as per original instructions.

Tweak filesystem: start qemu with init flag, switch to guest window to execute tweak and close window afterwards

$QEMU -kernel $RPI_KERNEL -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -drive "file=2017-03-02-raspbian-jessie.img,index=0,media=disk,format=raw"

So at this point you should get the emulator window and a terminal you can add the following commands in.

sed -i -e 's/^/#/' /etc/ld.so.preload
sed -i -e 's/^/#/' /etc/ld.so.conf
sed -i -e 's/^/#/' /etc/fstab

Exit that and the next command will start the actual raspberry pi emulation.

Emulate Raspberry Pi

$QEMU -kernel $RPI_KERNEL -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -drive "file=2017-03-02-raspbian-jessie.img,index=0,media=disk,format=raw" -net user,hostfwd=tcp::5022-:22

Available over network with this SSH command.

Login to Raspberry Pi

ssh -p 5022 pi@localhost

References

Referenced from OSX raspberry pi emulation via QEMU - https://gist.github.com/JasonGhent/e7deab904b30cbc08a7d
Referenced from Emulating Jessie image with 4.1.7 kernel - https://github.com/dhruvvyas90/qemu-rpi-kernel/wiki/Emulating-Jessie-image-with-4.1.7-kernel

@k0Iry
Copy link

k0Iry commented Oct 5, 2018

Mojave test succeeded :)

was trying this lab: Azeria-Labs

@SPGWhistler
Copy link

SPGWhistler commented Nov 27, 2018

The regex to comment out the bad lines in the fstab file did not work for me, and it would not boot past emergency mode. I had to manually edit fstab and comment out the mmcblk lines.

Also, to get networking working with the latest version of qemu, I had to change the -net part of the command to this:
-net user,hostfwd=tcp::5022-:22 -net nic

And as the user above mentioned, once the pi boots, you'll need to start ssh first:
sudo service ssh start

@mariotti
Copy link

mariotti commented Dec 21, 2018

I confirm it is working also on

  • macOS High Sierra
  • MacBook Air, mid 2011

With this addition/changes:

  • I edited by hand the files and not used sed (not for a particular reason, I just wanted to check what was in there ;) )
  • I needed to install snappy: 'brew install snappy'

In case the error for snappy was:

$QEMU -kernel $RPI_KERNEL -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -drive "file=2017-03-02-raspbian-jessie.img,index=0,media=disk,format=raw"
dyld: Library not loaded: /usr/local/opt/snappy/lib/libsnappy.1.dylib
  Referenced from: /usr/local/bin/qemu-system-arm
  Reason: image not found
Abort trap: 6

@carylewis
Copy link

carylewis commented Dec 27, 2018

Will the above scripts, etc., work for later versions of the kernel / image. The raspbian image referenced above is quite old.

UPDATE: I was able to download and run the stretch distro:

http://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/2018-11-13-raspbian-stretch.zip

I was able to run it without having to run the edit commands noted above"
sed -i -e 's/^/#/' /etc/ld.so.preload
sed -i -e 's/^/#/' /etc/ld.so.conf
sed -i -e 's/^/#/' /etc/fstab

However, using the -M versatilepb option limits the emulated machine to 256MB, which is much less than the real Rasberry PI 3.

Is there a way to run QEMU with another virtual ARM processor?

@pnyccz
Copy link

pnyccz commented Jan 2, 2019

Hello guys, these are my notes for setting up qemu RPI emulation, based on notes above, modified for strech:

brew install qemu
export QEMU=$(which qemu-system-arm)
mkdir ~/tmp/qemu-rpi; cd ~/tmp/qemu-rpi
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.9.41-stretch
export RPI_KERNEL=./kernel-qemu-4.9.41-stretch
wget http://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/2018-11-13-raspbian-stretch.zip
unzip 2018-11-13-raspbian-stretch.zip
export RPI_FS=2018-11-13-raspbian-stretch.img
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb

# the line below can be used for pre-boot modifications; seems like it is not needed
# $QEMU -kernel $RPI_KERNEL -cpu arm1176 -m 256 -M versatilepb -dtb versatile-pb.dtb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -drive "file=${RPI_FS},index=0,media=disk,format=raw"
$QEMU -kernel $RPI_KERNEL -cpu arm1176 -m 256 -M versatilepb -dtb versatile-pb.dtb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -drive "file=${RPI_FS},index=0,media=disk,format=raw" -net user,hostfwd=tcp::5022-:22

@rom3r4
Copy link

rom3r4 commented Feb 11, 2019

Ok, got it working for Mojave

$ cat ./rpi-qemu.sh

#!/bin/sh

#  MacOS Mojave

brew install qemu

export QEMU=$(which qemu-system-arm)
export TMP_DIR=~/tmp/qemu-rpi
export RPI_KERNEL=${TMP_DIR}/kernel-qemu-4.14.79-stretch
export RPI_FS=${TMP_DIR}/2018-11-13-raspbian-stretch-lite.img
export PTB_FILE=${TMP_DIR}/versatile-pb.dtb
export IMAGE_FILE=2018-11-13-raspbian-stretch-lite.zip
export IMAGE=http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-11-15/${IMAGE_FILE}

mkdir -p $TMP_DIR; cd $TMP_DIR

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.14.79-stretch?raw=true \
	    -O ${RPI_KERNEL}

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb \
	    -O ${PTB_FILE}

wget $IMAGE
unzip $IMAGE_FILE


$QEMU -kernel ${RPI_KERNEL} \
	-cpu arm1176 -m 256 -M versatilepb \
	-dtb ${PTB_FILE} -no-reboot \
	-serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
	-drive "file=${RPI_FS},index=0,media=disk,format=raw" \
	-net user,hostfwd=tcp::5022-:22 -net nic \

@tinjaw
Copy link

tinjaw commented Jun 25, 2019

If you are going to post code, please make a gist and post a link to the gist instead of pasting in the code. Thanks.

@tinjaw
Copy link

tinjaw commented Jun 25, 2019

Ok, got it working for Mojave

$ cat ./rpi-qemu.sh

#!/bin/sh

#  MacOS Mojave

brew install qemu

export QEMU=$(which qemu-system-arm)
export TMP_DIR=~/tmp/qemu-rpi
export RPI_KERNEL=${TMP_DIR}/kernel-qemu-4.14.79-stretch
export RPI_FS=${TMP_DIR}/2018-11-13-raspbian-stretch-lite.img
export PTB_FILE=${TMP_DIR}/versatile-pb.dtb
export IMAGE_FILE=2018-11-13-raspbian-stretch-lite.zip
export IMAGE=http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-11-15/${IMAGE_FILE}

mkdir -p $TMP_DIR; cd $TMP_DIR

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.14.79-stretch?raw=true \
	    -O ${RPI_KERNEL}

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb \
	    -O ${PTB_FILE}

wget $IMAGE
unzip $IMAGE_FILE


$QEMU -kernel ${RPI_KERNEL} \
	-cpu arm1176 -m 256 -M versatilepb \
	-dtb ${PTB_FILE} -no-reboot \
	-serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
	-drive "file=${RPI_FS},index=0,media=disk,format=raw" \
	-net user,hostfwd=tcp::5022-:22 -net nic \

I have turned this into a gist

https://gist.github.com/tinjaw/5bc5527ff379e8dd299a0b67e2bc9b62

@subatomicglue
Copy link

subatomicglue commented Sep 10, 2019

@romera94 I got your script to work (thanks, really easy!)

Has anyone gotten sound to work using this combination?

I added -soundhw all to the qemu command launcher $QEMU -soundhw all ...rest of the args...
Then inside the booted rPI machine, I run lspci -v and get [disabled] audio devices.

$ lspci -v
00:0c.0 SCSI storage controller: LSI Logic / Symbios Logic 53c895a
	Flags: bus master, fast devsel, latency 255, IRQ 66
	I/O ports at 1400 [size=256]
	Memory at 50006000 (32-bit, non-prefetchable) [size=1K]
	Memory at 50004000 (32-bit, non-prefetchable) [size=8K]
	Kernel driver in use: sym53c8xx
lspci: Unable to load libkmod resources: error -12

00:0d.0 Multimedia audio controller: Ensoniq ES1370 [AudioPCI]
	Subsystem: Device 4942:4c4c
	Flags: slow devsel
	I/O ports at 1800 [disabled] [size=256]

00:0e.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)
	Subsystem: Red Hat, Inc QEMU Virtual Machine
	Flags: medium devsel
	I/O ports at 1000 [disabled] [size=1K]
	I/O ports at 1c00 [disabled] [size=256]

00:0f.0 Audio device: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) High Definition Audio Controller (rev 01)
	Subsystem: Red Hat, Inc QEMU Virtual Machine
	Flags: fast devsel
	Memory at 50000000 (32-bit, non-prefetchable) [disabled] [size=16K]

Running aplay -l gives

$ aplay -l
aplay: device_list:270: no soundcards found...

@janwillemCA
Copy link

Working on Mac OS Catalina with the most recent Raspbian version..

`#!/bin/sh

MacOS Catalina

brew install qemu

export QEMU=$(which qemu-system-arm)
export TMP_DIR=~/tmp/qemu-rpi
export RPI_KERNEL=${TMP_DIR}/kernel-qemu-4.14.79-stretch
export RPI_FS=${TMP_DIR}/2019-09-26-raspbian-buster-lite.img
export PTB_FILE=${TMP_DIR}/versatile-pb.dtb
export IMAGE_FILE=2019-09-26-raspbian-buster-lite.zip
export IMAGE=http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30//${IMAGE_FILE}

mkdir -p $TMP_DIR; cd $TMP_DIR

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.14.79-stretch?raw=true
-O ${RPI_KERNEL}

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb
-O ${PTB_FILE}

wget $IMAGE
unzip $IMAGE_FILE

$QEMU -kernel ${RPI_KERNEL}
-cpu arm1176 -m 256 -M versatilepb
-dtb ${PTB_FILE} -no-reboot
-serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw"
-drive "file=${RPI_FS},index=0,media=disk,format=raw"
-net user,hostfwd=tcp::5022-:22 -net nic `

Working perfectly

@tinjaw
Copy link

tinjaw commented Nov 6, 2019

@faf0
Copy link

faf0 commented Dec 28, 2019

Turned this into robust Bash scripts: https://github.com/faf0/macos-qemu-rpi

@gorozco58
Copy link

Working on Mac OS Catalina with the most recent Raspbian version..

`#!/bin/sh

MacOS Catalina

brew install qemu

export QEMU=$(which qemu-system-arm)
export TMP_DIR=~/tmp/qemu-rpi
export RPI_KERNEL=${TMP_DIR}/kernel-qemu-4.14.79-stretch
export RPI_FS=${TMP_DIR}/2019-09-26-raspbian-buster-lite.img
export PTB_FILE=${TMP_DIR}/versatile-pb.dtb
export IMAGE_FILE=2019-09-26-raspbian-buster-lite.zip
export IMAGE=http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30//${IMAGE_FILE}

mkdir -p $TMP_DIR; cd $TMP_DIR

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.14.79-stretch?raw=true
-O ${RPI_KERNEL}

wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb
-O ${PTB_FILE}

wget $IMAGE
unzip $IMAGE_FILE

$QEMU -kernel ${RPI_KERNEL}
-cpu arm1176 -m 256 -M versatilepb
-dtb ${PTB_FILE} -no-reboot
-serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw"
-drive "file=${RPI_FS},index=0,media=disk,format=raw"
-net user,hostfwd=tcp::5022-:22 -net nic `

Working perfectly

Can you update it changing the lite version to full version of RPI

@angeload
Copy link

Hi, great job with this tutorial.

Could anyone help me to configure SSH in qemu-system-arm on macOS Catalina? I have tried I lot with no success.

I also would like to transfer files direct from the host into the guest without using scp. Any suggestion?

Thanks a lot!

@KrifaYounes
Copy link

Hi, thanks,
How to have root access ?
pi@raspberrypi:~$ mosquitto -c /etc/mosquitto/mosquitto.conf
1587242593: Error: Unable to open log file /var/log/mosquitto/mosquitto.log for writing.

pi@raspberrypi:/var/log/mosquitto$ chmod 777 mosquitto.log
chmod: changing permissions of 'mosquitto.log': Operation not permitted

I have got this error

@scubbo
Copy link

scubbo commented Apr 23, 2020

EDIT: Turns out that the emulated pi's claims about its IP address are bogus, and I should do (from my host) ssh pi@localhost -p 5022.


Thanks @gorozco58 - that worked for me to start the emulation. However, I can't ssh to it - even after running sudo systemctl enable ssh; sudo systemctl start ssh; sudo service ssh start (I know - I'm paranoid!) on the emulated Pi, the following (from the host machine) times out:

$ ssh -vvv pi@<pi_IP_address> -p 5022
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/jackjack/.ssh/config
debug1: /Users/jackjack/.ssh/config line 3: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug2: resolve_canonicalize: hostname <pi_IP_address> is address
debug2: ssh_connect_direct
debug1: Connecting to <pi_IP_address> [<pi_IP_address>] port 5022.
debug1: connect to address <pi_IP_address> port 5022: Operation timed out
ssh: connect to host <pi_IP_address> port 5022: Operation timed out

Copy link

ghost commented Mar 1, 2021

Hi, thanks,
How to have root access ?
pi@raspberrypi:~$ mosquitto -c /etc/mosquitto/mosquitto.conf
1587242593: Error: Unable to open log file /var/log/mosquitto/mosquitto.log for writing.

pi@raspberrypi:/var/log/mosquitto$ chmod 777 mosquitto.log
chmod: changing permissions of 'mosquitto.log': Operation not permitted

I have got this error

does sudo su work?

@jmgilman
Copy link

When runnung QEMU I'm getting this error:

qemu-system-arm: arm1176: Could not open 'arm1176': No such file or directory

Searching around hasn't really yielded anything. Perhaps someone ran into something similar?

Copy link

ghost commented Jul 15, 2021

When runnung QEMU I'm getting this error:

qemu-system-arm: arm1176: Could not open 'arm1176': No such file or directory

Searching around hasn't really yielded anything. Perhaps someone ran into something similar?

What command did you run?

@jmgilman
Copy link

Sorry, I thought I had pasted it in my first reply:

$QEMU -kernel $RPI_KERNEL \ 
-cpu arm1176 \
-m 256 \
-M versatilepb \
-no-reboot \
-serial stdio \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" \
-hda $RPI_FS

The only difference between the original is I installed QEMU via homebrew and I'm wondering if there was an option I needed to pass that I missed. The ARM QEMU binaries exist - it's just complaining about the CPU. I tried substituting it with a few different ARM CPUs and they all resulted in the same error.

@jmgilman
Copy link

Ok, turns out a fresh reinstall of QEMU was needed (thanks brew). Running the same command now results in the QEMU GUI opening up but the screen is black and there's no output being redirected to my local console. It appears -serial stdio should redirect to my local session, but nothing is output and it's unresponsive to any input.

Any idea on what to look at?

Copy link

ghost commented Jul 16, 2021

Ok, turns out a fresh reinstall of QEMU was needed (thanks brew). Running the same command now results in the QEMU GUI opening up but the screen is black and there's no output being redirected to my local console. It appears -serial stdio should redirect to my local session, but nothing is output and it's unresponsive to any input.

Any idea on what to look at?

Yeah, same goes for me, but I think you should wait more. (I do not recall if I used this method or another method)

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