Raspberry virtual machine from sdcard (linux/mac)

27 Jul 2017

This was originally published on medium.com but they recently started paywalling everybody so I’m moving my articles here.

Here’s a quick way to run raspbian from sdcard on your mac or linux machine. It will emulate Raspberry Pi 2 (different version) but it doesn’t matter because all the raspberries are binary compatible.

Download latest raspbian

raspbian

Flash to sdcard using Etcher (or whatever else)

Get qemu

# or apt-get install qemu on linux
brew install qemu

Get kernel and device tree from the first (vfat) partition of the sdcard

# This is how it's called on mac, on linux it's probably /dev/sdb1 or something
cp /Volumes/boot/{*.img,*.dtb} ./

Start qemu with io forwarded to your terminal:

# optionally unmount /dev/disk2 first
# Mac: sudo diskutil unmountDisk /dev/disk2
sudo qemu-system-arm \
-no-reboot \
-serial stdio \
-machine raspi2 -m 256 \
-dtb ./bcm2709-rpi-2-b.dtb \
-kernel ./kernel7.img \
-drive file=/dev/disk2,format=raw \

If what you want to do is to just run it in the VM, you are done here, in my case I actually wanted to setup raspi to run on my network and then boot it on the real hardware, and I had to do it from the VM because I didn’t have a keyboard at hand.

Setup network:

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

Enable ssh:

sudo raspi-config

Shutdown and boot with regular raspberry pi:

sudo shutdown now