Emulating Raspberry Pi on Windows

You can emulate the raspberry pi on your Windows Computer with QEMU. It will not have the extension pins, no sound and some other restrictions: It will simulate Raspberry 1 hardware, have only 256 Mbytes of RAM and only one core. It will have network connections and you can easily change the installed software. However it is slow and unless raspi2/3 emulation is not updated to include network and mouse and keyboard support its not useful for this course at this time. The purpose of this effort was to figure out how to easily create an updated software image for distribution to the class. However its easier to use the real hardware.

1. Download latest version of QEMU for windows and install it from https://www.qemu.org/download/#windows

2. Download latest version of Raspian from https://downloads.raspberrypi.org/raspbian_latest

3. Download latest Raspberry Pi Kernel from https://github.com/dhruvvyas90/qemu-rpi-kernel e.g. kernel.qemu-4.14.50-stretch

4. From same place also download latest versatile-pb.dtb

5. Download OpenVPN from https://openvpn.net/community-downloads/. Run the installation and only install the TAP driver.

6. Unzip the raspian image file.

7. Copy the image file, the kernel and the dtb to the QEMU directory. Make those files writable using windows file properties/security.

8. Open the network settings by opening the Control Panel\All Control Panel Items\Network and Sharing Center and then Change Adapter Settings. Rename your new adapter TAP. On your wireless adapter select properties then sharing, then enable allow other users to connect through this connection and in the list of users select TAP. This will allow your wireless internet connection be used by the raspberry emulator.

9. Run the emulator with:

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -dtb versatile-pb.dtb -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -drive "file=image.img, index=0,media=disk" -net nic -net tap,ifname=TAP
Select the appropriate country, key board and time zone. Do not update software.

10. Fix some settings in the raspberry installation by logging in with pi/raspberry as username/pw and then open a terminal/shell and type:

sudo nano /etc/udev/rules.d/90-qemu.rules

In that file you need to add the following:

KERNEL=="sda", SYMLINK+="mmcblk0"KERNEL=="sda?", SYMLINK+="mmcblk0p%n"KERNEL=="sda2", SYMLINK+="root"

11. Overclock the CPU

In a terminal windowsudo raspi-config
Select option "6 Overclock"Select "Ok" in the warning messageSelect "Turbo 1000MHz ARM"Select "Ok" in the confirmation message
Now you can shutdown: sudo shutdown -r now

12. Change the disk size so that it expands to the full size :

Expand the image by adding extra space: qemu-img.exe resize image.vhd +5G
Run fdisk on raspberry to change the partition table:sudo fdisk /dev/sda
Print the partition table by entering "p". Take note of the Boot Start / starting block of the second partition (Linux).Delete the second partition "d"Create new partition "n". Make it a primary partition "p". And place it at position 2.The start block one you took note above. The size is the full size of the image file which is the default (just press "enter").Now write the partition table "w" to the disk
Reboot:shutdown -r now
After reboot:sudo resize2fs /dev/sda2
Verify the new size of the disk:df -h

13. Increase the swap size:

sudo nano /etc/dphys-swapfile
Then you need to find the lineCONF_SWAPSIZE=100and change it toCONF_SWAPSIZE=1024
Restart the service that manages the swapfile:sudo /etc/init.d/dphys-swapfile stopsudo /etc/init.d/dphys-swapfile start
If everything finishes successfully, you can verify the change in the swap file by using the commandfree -m
The output next to Swap should say 1023sudo shutdown -r now

14. Increase screen resolution

open file /etc/X11/app-defaults/Editresand edit the 2 lines to your desired resolution:*porthole.tree.width: 1280*porthole.tree.height: 1024
Rebootsudo shutdown -r now

16. Start the simulator with:

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -smp 1 -M versatilepb -dtb versatile-pb.dtb -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -drive "file=image.img,index=0,media=disk,format=raw" -net nic -net tap,ifname=TAP

Comments:

Much information is from https://blogs.msdn.microsoft.com/iliast/2016/11/10/how-to-emulate-raspberry-pi/
Convert the image file to a vhd file: qemu-img convert 2018-11-13-raspbian-stretch-full.img -O vpc -o subformat=dynamic image.vhdqemu-img convert -f vpc -O raw image.vhd image.img
One can also run the raspi2 emulator but network, mouse and keyboard does not work.For this one needs to get latest kernel from https://github.com/raspberrypi/firmware/tree/master/boot and raspian image from https://downloads.raspberrypi.org/raspbian_latest
qemu-system-aarch64 -kernel kernel7.img -M raspi2 -cpu cortex-a53 -smp 4 -m 1024 -global bcm2835-fb.xres=1280 -global bcm2835-fb.yres=1024 -global bcm2835-fb.pixo=0 -dtb bcm2709-rpi-2-b.dtb -serial stdio -drive "file=pi2stretch.img,index=0,media=disk,format=raw" -append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait dma.dmachans=0x3c" -device usb-kbd -device usb-mouse -display gtk
Issue is that if disk size is enlarged the rapi2 emulator has problems recognizing the new disk.