Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@MrAndersonMD

This comment has been minimized.

MrAndersonMD commented Mar 6, 2017

Hi, first of all great job!! Your script helped me a lot trying to emulate Raspbian on MacOS Sierra using qemu.

I'd like to make some contributions.

It's easier to install qemu by using hombrew so line 2 should be replaced by

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install qemu

Using curl to download files it's nice but using wget it easier. Yo can install wget using hombrew using

brew install wget

There is a newer version of kernel so lines 6-8 can be replaced by

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

There is a newer version of Raspbian Jessie compatible with last kernel available so lines 11-14 can be replaced by

wget http://downloads.raspberrypi.org/raspbian/images/raspbian-2016-11-29/2016-11-25-raspbian-jessie.zip
unzip raspbian-2016-11-29/2016-11-25-raspbian-jessie.zip
export RPI_FS=./raspbian-2016-11-29/2016-11-25-raspbian-jessie.zip

With -hda option I had a warning that raw format was selected automatically but it should be avoided, so line 21 and line 31 should be replaced by

-drive "file=2016-11-25-raspbian-jessie.img,index=0,media=disk,format=raw"

It's supposed that lines 23 and 24 should modify /etc/ld.so.conf and /etc/fstab, but every time that part of the script failed and stated an error.

Newer guides that give directions for emulation of Raspbian using qemu states that the first file you need to modify is /etc/ld.so.preload so line 23 should be replaced by

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

Finally -redir option is deprecated, I managed to solve it with -net option so line 32 can be replaced by

-net user,hostfwd=tcp::5022-:22

Again, thanks for your great job, I will not be able to emulate Raspbian on MacOS Sierra without your script. I hope you consider my contributions, it's done with the best of intentions.

@bainss

This comment has been minimized.

bainss commented Apr 7, 2017

Here's my working version

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)

Dowload kernel and export location

curl -OL
https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.4.34-jessie
export RPI_KERNEL=./kernel-qemu-4.4.34-jessie

Download filesystem and export location

curl -o 2017-03-02-raspbian-jessie.zip
-L 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.zip

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"

enter these on the qemu terminal and exit after

sed -i -e 's/^/#/' /etc/ld.so.preload
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"
-drive "file=2017-03-02-raspbian-jessie.img,index=0,media=disk,format=raw"
-net user,hostfwd=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

@theconsultant

This comment has been minimized.

theconsultant commented Jun 18, 2017

@224XS

This comment has been minimized.

224XS commented Aug 29, 2017

Has anyone discovered a working set of qemu invocation switches/options to enable networking for the RPi/jessie-4.4.34 on macOS 10.12.6 Sierra?
Otherwise, the script works. Needs two sequential boots on the RPi, and the SED lines that comment out the ld.so's and fstab fail, but a
working RPi is instantiated and even startx works. Just no network. Only pinging to the loopback works, nothing else.

Here is my version of the script:
`qemu-system-arm -kernel ./kernel-qemu-4.4.34-jessie
-cpu arm1176 -m 256
-M versatilepb -no-reboot -serial stdio
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash"
-drive "file=2016-11-25-raspbian-jessie.img,index=0,media=disk,format=raw"

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

    # Emulate Raspberry Pi

qemu-system-arm -kernel ./kernel-qemu-4.4.34-jessie
-cpu arm1176 -m 256
-M versatilepb -no-reboot -serial stdio
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw"
-drive "file=2016-11-25-raspbian-jessie.img,index=0,media=disk,format=raw"
-net user,hostfwd=tcp::5022-:22

    # Login to Raspberry Pi
    ssh -p 5022 pi@localhost

`
Of course, since the network fails with no NICs, the ssh also fails

@224XS

This comment has been minimized.

224XS commented Aug 30, 2017

The script generally works well. I can boot the RPi on my macOS Sierra system without a problem.
There is a new Jessie and a new QEMU since these scripts were written.
The new QEMU (>= 2.8) no longer requires commenting out /etc/ld.so.conf or fstab
I cannot discover a way to enable networking on the RPi, however, ifconfig contains only a LO (local loopback), no eth0, etc.
There the final line of ssh'ing to the RPi is nfg.
Here is my working script with the updates:

`qemu-system-arm -kernel ./kernel-qemu-4.4.34-jessie
-cpu arm1176 -m 256
-M versatilepb -no-reboot -serial stdio
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash"
-drive "file=2016-11-25-raspbian-jessie.img,index=0,media=disk,format=raw"

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

    # Emulate Raspberry Pi

qemu-system-arm -kernel ./kernel-qemu-4.4.34-jessie
-cpu arm1176 -m 256
-M versatilepb -no-reboot -serial stdio
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw"
-drive "file=2016-11-25-raspbian-jessie.img,index=0,media=disk,format=raw"
-net user,hostfwd=tcp::5022-:22

    # Login to Raspberry Pi
    ssh -p 5022 pi@localhost

`

@zhukandrey

This comment has been minimized.

zhukandrey commented Nov 29, 2017

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

@chaimpeck

This comment has been minimized.

chaimpeck commented Dec 29, 2017

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

@alfredosola

This comment has been minimized.

alfredosola commented Feb 1, 2018

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

@esell

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

madhavajay commented May 22, 2018

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

@hanscappelle

This comment has been minimized.

hanscappelle commented Sep 27, 2018

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

This comment has been minimized.

k0Iry commented Oct 5, 2018

Mojave test succeeded :)

was trying this lab: Azeria-Labs

@SPGWhistler

This comment has been minimized.

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

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