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
@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