linux qemu - ghdrako/doc_snipets GitHub Wiki

QEMU

Emulator + wirtualizator - Potrafi emulować dowolną architekturę procesora (x86, ARM, RISC-V, MIPS, PowerPC itd.).

Tryby pracy:

  • pełna emulacja CPU – działa wolniej, ale pozwala odpalić np. program ARM na PC x86, albo RISC-V kernel na zwykłym laptopie.
  • KVM (Linux) – jeśli host i guest mają tę samą architekturę (np. obie x86_64), QEMU może korzystać z akceleracji sprzętowej i działa wtedy szybciej (z prędkością zbliżoną do VirtualBox/VMware).

QEMU is a machine emulator.

Example of emulation

  • qemu-system-arm: ARM
  • qemu-system-mips: MIPS
  • qemu-system-ppc: PowerPC
  • qemu-system-x86: x86 and x86_64

For each architecture, QEMU emulates a range of hardware, which you can see by using the option —machine help. Each machine emulates most of the hardware that would normally be found on that board. There are options to link hardware to local resources, such as using a local file for the emulated disk drive. Here is a concrete example:

$ $ qemu-system-arm -machine vexpress-a9 -m 256M -drive 
file=rootfs.ext4,sd -net nic -net use -kernel zImage -dtb 
vexpress- v2p-ca9.dtb -append "console=ttyAMA0,115200 root=/
 dev/mmcblk0" -serial stdio -net nic,model=lan9118 -net 
tap,ifname=tap0

The options used in the preceding command line are as follows:

  • -machine vexpress-a9: Creates an emulation of an Arm Versatile Express development board with a Cortex A-9 processor
  • -m 256M: Populates it with 256 MiB of RAM
  • -drive file=rootfs.ext4,sd: Connects the SD interface to the local file rootfs.ext4 (which contains a filesystem image)
  • -kernel zImage: Loads the Linux kernel from the local file named zImage
  • -dtb vexpress-v2p- ca9.dtb: Loads the device tree from the local file vexpress-v2p-ca9.dtb
  • -append "...": Appends this string as the kernel command line
  • -serial stdio: Connects the serial port to the terminal that launched QEMU, usually so that you can log on to the emulated machine via the serial console
  • -net nic,model=lan9118: Creates a network interface
  • -net tap,ifname=tap0: Connects the network interface to the virtual network interface, tap0

To configure the host side of the network, you need the tunctl command from the User Mode Linux (UML) project; on Debian and Ubuntu, the package is named uml-utilites:

 $ sudo tunctl -u $(whoami) -t tap0

This creates a network interface named tap0 that is connected to the network controller in the emulated QEMU machine. You configure tap0 in exactly the same way as any other interface

Przyklad emulacji Risk-V

qemu-system-riscv64 \
    -machine virt \
    -nographic \
    -bios default \
    -kernel kernel.elf
  • machine virt → wirtualna płyta RISC-V dostarczana przez QEMU (z UART, timerem itd.).
  • nographic → wyłącza GUI, używa terminala jako konsoli (wygodne przy bare-metal).
  • bios default → ładuje OpenSBI (firmware, który obsłuży M-mode i uruchomi kernel w S-mode).
  • kernel kernel.elf → Twój własny kernel lub program bare-metal.

linux Risk-V

qemu-system-riscv64 \
    -machine virt \
    -nographic \
    -bios default \
    -kernel bbl \
    -append "console=ttyS0"

(gdzie bbl to bootloader+kernel RISC-V Linuxa)

curl -O https://nl.alpinelinux.org/alpine/v3.8/releases/x86_64/alpine-standard-3.8.0-x86_64.iso qemu-img create -f qcow2 alpine.qcow2 16G
qemu-system-x86_64 \
    -enable-kvm \
    -m 2048 \
    -nic user,model=virtio \
    -drive file=alpine.qcow2,media=disk,if=virtio \
    -cdrom alpine-standard-3.8.0-x86_64.iso \
    -sdl
  • enable-kvm: This enables use of the KVM (kernel virtual machine) subsystem to use hardware accelerated virtualisation on Linux hosts.
  • m 2048: This specifies 2048M (2G) of RAM to provide to the guest.
  • nic user,model=virtio: Adds a virtual network interface controller, using a virtual LAN emulated by qemu. This is the most straightforward way to get internet in a guest, but there are other options (for example, you will probably want to use -nic tap if you want the guest to do networking directly on the host NIC). model=virtio specifies a special virtio NIC model, which is used by the virtio kernel module in the guest to provide faster networking.
  • drive file=alpine.qcow2,media=disk,if=virtio: This attaches our virtual disk to the guest. It’ll show up as /dev/vda. We specify if=virtio for the same reason we did for -nic: it’s the fastest interface, but requires special guest support from the Linux virtio kernel module.
  • cdrom alpine-standard-3.8.0-x86_64.iso - connects a virtual CD drive to the guest and loads our install media into it.
  • sdl - finally specifies the graphical configuration. We’re using the SDL backend, which is the simplest usable graphical backend. It attaches a display to the guest and shows it in an SDL window on the host.