Monday, September 9, 2019

How to emulate a Raspbian OS in QEMU on Windows 10

I wanted to test out developing for a Raspberry Pi on a Windows 10 PC platform running an emulated Raspbian OS. After trying out a few methods to get  the emulator running, I settled on the following procedure.

Install QEMU for 64-bit Windows 10
  1. Go to the following web site https://www.qemu.org/download/ and download the latest QEMU installer for Windows 64bit.
  2. Run the installer.

    The QEMU executable files are installed, e.g. C:\Program Files\qemu\
Download a Linux kernel
  1. Go to the following web site https://github.com/dhruvvyas90/qemu-rpi-kernel and download a suitable kernel, e.g. kernel-qemu-4.4.34-jessie.
  2. Place the kernel file in a folder e.g. D:/Temp/raspbian/kernel-qemu-4.4.34-jessie
Download an Raspbian OS image
  1. Go to the following web site http://downloads.raspberrypi.org/raspbian/images/ and download the latest image e.g. 2019-06-20-raspbian-buster.zip
  2. Unzip the image file into a folder, e.g. D:/Temp/raspbian/2019-06-20-raspbian-buster.img

Create a Windows bat file
  1. Run a text editor.
  2. Type in the following lines.

    "c:\Program Files\qemu\qemu-system-arm.exe" ^
    -kernel kernel-qemu-4.4.34-jessie ^
    -cpu arm1176 ^
    -m 256 ^
    -M versatilepb ^
    -serial stdio ^
    -append "root=/dev/sda2 rootfstype=ext4 rw" ^
    -hda 2019-06-20-raspbian-buster.img ^
    -net nic ^
    -net user,hostfwd=tcp::5022-:22 ^
    -no-reboot


    Note: rename the kernel and image file names (in bold) to match the downloaded file names accordingly.

  3. Save as a bat file e.g. run_raspbian.bat and close the editor.
Run QEMU
  1. Open up a Command Prompt.
     
  2. Change directory to the folder containing the kernel and image files e.g. D:/Temp/raspbian.
  3. Type in the command:

    D:\> run_raspbian.bat


    The QEMU window appears running Raspbian OS

14 comments:

Rok Banko said...

Thank you for this tutorial!

Chuck said...

You absolutely ROCK for this tutorial. In my case I had to add the -dtb parameter with the compiled device tree. After that, the latest Buster release cranked right on up. Thank you VERY much for this! -- Chuck

Bassili said...

you are the Best akmost ,all other Tutorials are outdated

rulet said...

C:\Temp\raspbian>"C:\Program Files\qemu\qemu-system-arm.exe" -kernel kernel-qemu-4.19.50-buster -cpu arm1176 -m 256 -M versatilepb -serial stdio -append "root=/dev/sda2 rootfstype=ext4 rw" -hda 2020-05-27-raspios-buster-full-armhf.img -net nic -net user,hostfwd=tcp::5022-:22 -no-reboot
WARNING: Image format was not specified for '2020-05-27-raspios-buster-full-armhf.img' and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
dsound: Could not initialize DirectSoundCapture
dsound: Reason: No sound driver is available for use, or the given GUID is not a valid DirectSound device ID

Error: invalid dtb and unrecognized/unsupported machine ID
r1=0x00000183, r2=0x00000100
r2[]=05 00 00 00 01 00 41 54 01 00 00 00 00 10 00 00
Available machine support:

ID (hex) NAME
ffffffff Generic DT based system
ffffffff ARM-Versatile (Device Tree Support)

Please check your kernel config and/or bootloader.
PS C:\Temp\raspbian>

dominoc925 said...

Try this:

"c:\Program Files\qemu\qemu-system-arm.exe" ^
-kernel kernel-qemu-4.19.50-buster ^
-cpu arm1176 ^
-m 256 ^
-M versatilepb ^
-serial stdio ^
-append "root=/dev/sda2 rootfstype=ext4 rw" ^
-hda 2020-02-13-raspbian-buster.img ^
-dtb versatile-pb-buster.dtb ^
-net nic ^
-net user,hostfwd=tcp::5022-:22 ^
-no-reboot

Ricky said...

Hi,
Do you know how to configure this to emulate raspi 3B+?
Thanks

ThatAverageCodist said...

dominoc925, it says this:

D:\Downloads\Captive Portal OS Project\Emulator>"D:\Other Programs\qemu\qemu-system-arm.exe" -kernel kernel-qemu-4.19.50-buster -cpu arm1176 -m 256 -M versatilepb -serial stdio -append "root=/dev/sda2 rootfstype=ext4 rw" -hda 2020-02-13-raspbian-buster.img -dtb versatile-pb-buster.dtb
WARNING: Image format was not specified for '2020-02-13-raspbian-buster.img' and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
Couldn't open dtb file versatile-pb-buster.dtb

D:\Downloads\Captive Portal OS Project\Emulator>-net nic -net user,hostfwd=tcp::5022-:22 -no-reboot
'-net' is not recognized as an internal or external command,
operable program or batch file.

dominoc925 said...

Hi @ThatAverageCodist, for the latest kernel you have to download versatile-pb-buster.dtb from the kernel repo https://github.com/dhruvvyas90/qemu-rpi-kernel

Felipe Arce said...

Hi, thanks for this!

I assume changing "-m 256 ^" for "-m 1024 ^" will set 1GB of ram, is it true?
In the other hand, how can I set e.g. 10 or 15 GB for disk usage?

dominoc925 said...

Hi @Felipe Arce, yeah -m defaults to mega but you can use optional suffix k, M, G, T, P or E means kilo-, mega-, giga-, tera-, peta-and exabytes for clarity too.

I'm not sure whether I can change the allocated Raspbian disk image usage size; I would think you'd need to add in another disk image. Needs some investigation.

Felipe Arce said...

@dominoc925 I just tried to asign more ram, but it seems that it is not posible because versatilepb board is limited to 256MB RAM. You know how to avoid this?

Unknown said...

Hello, my update config:
"c:\Program Files\qemu\qemu-system-arm.exe" ^
-kernel kernel-qemu-4.19.50-buster ^
-dtb versatile-pb-buster.dtb ^
-cpu arm1176 ^
-m 256 ^
-M versatilepb ^
-serial stdio ^
-append "root=/dev/sda2 rootfstype=ext4 rw" ^
-hda 2020-02-13-raspbian-buster.img ^
-net nic ^
-net user,hostfwd=tcp::5022-:22 ^
-no-reboot

;-)

Unknown said...

Hello, can you explain how I can configure a network in qemu with Windows 10?

Denn said...

You'll have to add dtb file asking with the kernel and invoke in the qemu command

Related Posts with Thumbnails