@vipulgupta2048

Chip shortage. What's that?

@vipulgupta2048

I am Vipul!

@vipulgupta2048

We-pull

  • Product Owner & Documentation Lead at balena
  • Run docs initiative called Mixster
  • Contributions: Sugar Labs, PSF - ScrapingHub 
  • Organizes SustainOSS India, PyCon India & ALiAS
  • Pronouns: He/him/his

@vipulgupta2048

This is a Raspberry Pi

It ain't much ...

Devices like these power...

CO2 sensors, farms, smart dustbins, cars, locks, your room, home devices...

@vipulgupta2048

and more like this...

Underwater drones, satellites, parking, and your talking wireless speakers

@vipulgupta2048

@vipulgupta2048

But, when someone parks their ship the wrong way around...

The world loses their mind ... 

And events like these don't help...

 

Is it a bird? Is it a Pokemon? No, it's ...

The solution to this

@vipulgupta2048

Meet Quick EMUlator

  • Generic & open-source machine emulator and virtualizer
  • Allows you to run an OS within another OS (Same or different)
  • First release on 4th March 2009 under GPL v2

It's also well supported

What really is QEMU? How does it work? How does it accomplish this dark magic?

  • The kitchen is your computer, QEMU is the chef
  • Customers are your operating systems/applications
  • Customers are picky, need different dishes (hardware)

@vipulgupta2048

And, QEMU can be overwhelming

Like you know you have to go to the 5th floor
... But which staircase will get me to there. No clue.

How
Does
it Work?

I am here to help, let's start with

Glados, our host system.

➜  ~ screenfetch
                             vipulgupta2048@pop-os
                             OS: Pop 22.04 jammy
                             Kernel: x86_64 Linux 6.4.6-76060406-generic
         #####               Uptime: 4h 14m
        #######              Packages: Unknown
        ##O#O##              Shell: fish 3.6.1
        #######              Resolution: 1920x1080
      ###########            DE: GNOME 41.4
     #############           WM: Mutter
    ###############          WM Theme: Sweet
    ################         GTK Theme: Pop-dark [GTK2/3]
   #################         Icon Theme: candy-icons-master
 #####################       Font: Fira Sans Semi-Light 10
 #####################       Disk: 228G / 307G (79%)
   #################         CPU: Intel Core i7-10510U @ 8x 800MHz
                             GPU: Intel Corporation CometLake-U GT2
                             RAM: 8328MiB / 15623MiB

Raspberry Pi 3, our actual device.

  • Cortex-A53 (ARMv8) 64-bit SoC @ 1.4GHz
  • 1GB LPDDR2 SDRAM
  • 2.4GHz and 5GHz IEEE 802.11.b/g/n/ac wireless LAN
  • Gigabit Ethernet over USB 2.0
  • Extended 40-pin GPIO header
  • Full-size HDMI
  • 4 USB 2.0 ports
  • CSI camera port // DSI display port
  • 4-pole stereo output and composite video port

Meet the virtual Raspberry Pi 3.

  • 4x 64-bit cores
  • 1GB RAM 
  • Internet access from host system
  • Stdin access is available with serial or even SSH
  • USB ports with pass through
  • Video output feature to SDL or VGA 
  • Audio pass through
  • Lot of Storage 

QEMU emulates specifications, not machines.

Steps before the build

Check Virtualization status in the system

➜ egrep -c '(vmx|svm)' /proc/cpuinfo
16

If the value is more than 0, then your system has an Intel VT-x (vmx) processor or AMD-V (svm) processor.

Steps before the build

Check if we have the KVM module in the system

➜ lsmod | grep kvm
kvm_intel             528384  0
kvm                  1347584  1 kvm_intel

And, we got the KVM modules needed by QEMU when we need it. To be explained more later.

Steps before the build

Some important, other optional to install in your distro

qemu-system-aarch64
KVM 
bridge-utils 
libvirt
qemu-monitor

Disclaimer

Use a GUI if you want to!

Full comparison list of alternatives because it's perfectly fine to use Virt-manager, parallels, VirtualBox, Hyper-V (for Windows)

Getting Started

This is the command to start your RPi Machine

qemu-system-aarch64  -enable-kvm  \
-machine raspi3b -cpu cortex-a53  \
-dtb bcm2710-rpi-3-b.dtb          \ 
-m 1G -smp 4                      \
-kernel kernel8.img               \
-sd ./Downloads/rpi.img           \
-append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1"  \
-device usb-net,netdev=net0       \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \ 
-no-reboot -nographic             \

Breaking it down: KVM

Kernel module used with QEMU to provide direct hardware virtualization and boost performance

qemu-system-aarch64  -enable-kvm  \
-machine raspi3b -cpu cortex-a53  \
-m 1G -smp 4                      \
-dtb bcm2710-rpi-3-b.dtb          \ 
-kernel kernel8.img               \
-sd ./Downloads/rpi.img           \
-append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 
dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1"  \
-device usb-net,netdev=net0       \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \ 
-no-reboot -nographic             \

Machine & CPU

Specifying machine model, memory allocation & cores

qemu-system-aarch64  -enable-kvm  \
-machine raspi3b -cpu cortex-a53  \
-m 1G -smp 4                      \
-dtb bcm2710-rpi-3-b.dtb          \ 
-kernel kernel8.img               \
-sd ./Downloads/rpi.img           \
-append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 
dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1"  \
-device usb-net,netdev=net0       \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \ 
-no-reboot -nographic             \

Device Tree Blob, Kernel & Image

Specifying the hardware layout of RPi3, informing QEMU what hardware is available to virtualize.

qemu-system-aarch64  -enable-kvm  \
-machine raspi3b -cpu cortex-a53  \
-m 1G -smp 4                      \
-dtb bcm2710-rpi-3-b.dtb          \ 
-kernel kernel8.img               \
-sd ./Downloads/rpi.img           \
-append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 
dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1"  \
-device usb-net,netdev=net0       \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \ 
-no-reboot -nographic             \

Network, Connected devices

Options to configure network connections with host computer, GUI, and SSH ports

qemu-system-aarch64  -enable-kvm  \
-machine raspi3b -cpu cortex-a53  \
-m 1G -smp 4                      \
-dtb bcm2710-rpi-3-b.dtb          \ 
-kernel kernel8.img               \
-sd ./Downloads/rpi.img           \
-append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 
dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1"  \
-device usb-net,netdev=net0       \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \ 
-no-reboot -nographic             \

Barely scratching the surface

  • USB Pass-through from the host
  • SPICE - Copy and paste from host/guest
  • Snapshots to restore systems
  • Mounting host drives to guest
  • Lots more to experiment

But Vipul, how can I use this now at work?

  • Production Deployments
  • Resource Segmentation
  • IaaS & PaaS
  • Disaster recovery
  • Experimentation chamber
  • Training & research
  • Employee desktop virtualization

VM1

VM2

VM4

VM3

Autokit

(controller)

Feedback on changes

Developing and testing software
on target architecture
in a CI/CD pipeline

?

QEMU

?

?

?

?

?

?

?

?

?

?

?

Hardware Protoyping

Build software, before hardware gets ready

Testing firmware changes before releases

  • Support Legacy systems
  • Play games on emulators 
  • Safe data migration from old tech
  • Penetration testing
  • Malware testing
  • Stress testing

Testing Operating Systems on virtual devices

Quick Summary

  • QEMU is not a pokemon
  • It requires breaking things to learn
  • It can near natively emulate hardware 
  • It's quite extensive in both features, & usecases
  • And, you too can be a master chef!

And, that's about it!

Questions? Collaborate? Work with us? Reach out!

Vipul Gupta

Reviews cheesecakes, solves GitHub issues & runs memeservice.

Feedback please + Link to the slides

[OSSEU 23] Building a Virtual Raspberry Pi using QEMU

By Vipul Gupta

[OSSEU 23] Building a Virtual Raspberry Pi using QEMU

In this session, we will focus on the basics of virtualization, and how it differs from emulation, containerization, what is QEMU, and alternatives like KVM & libvirt and more. The talk was presented to folks attending Open Source Summit 2023 in Bilbao, Spain

  • 408