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

22 comments:

  1. 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

    ReplyDelete
  2. you are the Best akmost ,all other Tutorials are outdated

    ReplyDelete
  3. 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>

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

      Delete
  4. 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

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

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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?

    ReplyDelete
  9. 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.

    ReplyDelete
  10. @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?

    ReplyDelete
  11. 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

    ;-)

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

    ReplyDelete
  13. "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 2020-02-05-raspbian-buster.img -dtb versatile-pb-buster.dtb -net nic -net user,hostfwd=tcp::5022-:22 -no-reboot
    The system cannot find the path specified.

    Hey i'm facing this issue , can anyone tell me whats wrong . I tried checking the path . Its all correct.

    ReplyDelete
  14. ? @Unknown, at first glance, the command line seems fine. Try troubleshooting the options.
    Are you able to run the qemu-system-arm.exe by itself - that should print out some help messages. If so, add in more options and run again.

    ReplyDelete
  15. I want to emulate latest raspbian (2020-08-20-raspios-buster-armhf.img).i want to use 5.4 kernel.
    what will be the command for version 5.4 kernel?

    ReplyDelete
  16. This is my batch file:

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

    The Raspberry VM starts but I get the following error:

    Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
    CPU: 0 PID: 1 Comm: swapper Tainted: G W 5.4.51 #1
    Hardware name: ARM-Versatile (Device Tree Support)
    [] (unwind_backtrace) from [] (show_stack+0x10/0x14)
    [] (show_stack) from [] (panic+0xe4/0x2d8)
    [] (panic) from [] (mount_block_root+0x1f0/0x2b4)
    [] (mount_block_root) from [] (prepare_namespace+0x15c/0x1bc)
    [] (prepare_namespace) from [] (kernel_init+0x8/0xe8)
    [] (kernel_init) from [] (ret_from_fork+0x14/0x2c)
    Exception stack(0xcf825fb0 to 0xcf825ff8)
    5fa0: 00000000 00000000 00000000 00000000
    5fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    5fe0: 00000000 00000000 00000000 00000000 00000013 00000000
    ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0) ]---

    ReplyDelete
  17. @Roel Janssens - I'm seeing the same thing.

    ReplyDelete
  18. me too

    Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
    CPU: 0 PID: 1 Comm: swapper Not tainted 4.19.50+ #1
    Hardware name: ARM-Versatile (Device Tree Support)
    [] (unwind_backtrace) from [] (show_stack+0x10/0x14)
    [] (show_stack) from [] (panic+0xc8/0x240)
    [] (panic) from [] (mount_block_root+0x1dc/0x2a8)
    [] (mount_block_root) from [] (mount_root+0xfc/0x140)
    [] (mount_root) from [] (prepare_namespace+0x168/0x1c8)
    [] (prepare_namespace) from [] (kernel_init_freeable+0x174/0x1bc)
    [] (kernel_init_freeable) from [] (kernel_init+0x8/0xe8)
    [] (kernel_init) from [] (ret_from_fork+0x14/0x2c)
    Exception stack(0xcf831fb0 to 0xcf831ff8)
    1fa0: 00000000 00000000 00000000 00000000
    1fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    1fe0: 00000000 00000000 00000000 00000000 00000013 00000000
    Rebooting in 1 seconds..

    ReplyDelete
  19. With this https://howtoraspberrypi.com/download/raspbian_lite_latest.zip image
    and this file:
    "...\qemu-system-arm.exe" ^
    -M versatilepb ^
    -cpu arm1176 ^
    -m 256 ^
    -net nic ^
    -net user,hostfwd=tcp::5022-:22 ^
    -serial stdio ^
    -drive "file=...2020-12-02-raspios-buster-armhf-lite.img,if=none,index=0,media=disk,format=raw,id=disk0" ^
    -device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" ^
    -dtb ...versatile-pb-buster.dtb ^
    -kernel ...kernel\kernel_qemu_5451_buster ^
    -append "root=/dev/vda2 panic=1" ^
    -no-reboot

    ReplyDelete
  20. Hello,
    Did anyone find a way to increase the ram for buster?

    ReplyDelete