Skip to main content

Wiki QEMU

Learning and using the QEMU help me understand how the linux operating system works including fields:

  1. Linux boot process.
  2. Cross compile for target system(such as arm64) on host system(such as x86_64), and test the binary.

OS image Resources

QEMU Keyboard shortcuts

  • Switch between QEMU monitor console and the guest non-graphic OS
    • CTRL+a c
  • Exit the guest non-graphic OS
    • CTRL+a x
  • Switch between QEMU monitor console and the guest graphic OS
    • CTRL+ALT+1, CTRL+ALT+2

Discover the VM device tree

Enter the QEMU monitor console, using info qtree command,

$ info qtree

dev: gpex-pcihost, id ""
...
bus: pcie.0
type PCIE
dev: virtio-scsi-pci, id ""
...
bus: virtio-bus
type virtio-pci-bus
dev: virtio-scsi-device, id ""
...
bus: scsi.0
type SCSI
dev: scsi-hd, id ""
drive = "hd"
...
dev: nvme, id ""
drive = "drive0"
...
bus: nvme-bus.0
type nvme-bus
dev: virtio-net-pci, id ""
...
bus: virtio-bus
type virtio-pci-bus
dev: virtio-net-device, id ""
...

List supported devices

$ qemu-system-aarch64 -device help
$ qemu-system-aarch64 -device scsi-hd,help

Create disk image

qemu-img create -f raw ubuntu.raw 20G
qemu-img create -f qcow2 ubuntu.qcow2 20G

QEMU can boot from 3 ways:

  • BIOS in default
  • Linux kernel and initrad
  • UEFI

For UEFI boot, the -bios option should be used alongside UEFI firmware(OVMF.fd file) being provided to help QEMU do UEFI boot. For instance it is like: -bios OVMF.fd.

Get a prebuilt OVMF file from the OVMF.

BIOS boot

Test entering BIOS,

qemu-system-x86_64 -monitor stdio -m 1G

Then QEMU will show like this,

Kernel boot

UEFI boot

Test UEFI boot

aarch64,

efi="$PWD/UEFI/aarch64/QEMU_EFI.fd"

qemu-system-aarch64 -monitor stdio -M virt -cpu cortex-a57 -m 1G -net none -bios $efi

qemu-system-aarch64 -nographic -M virt -cpu cortex-a57 -m 1G -net none -bios $efi

x86_64,

efi="$PWD/UEFI/ovmf-x64/OVMF-pure-efi.fd"

qemu-system-x86_64 -monitor stdio -m 1G -net none -bios $efi

Then QEMU will drop into the UEFI shell, like this following image show,

efi

Options in detail:

  • -nographic: Don't create a video for the VM, just use the terminal.
info

quit QEMU: Ctrl+A X.
enter QEMU monitor console: Ctrl+A C.
see at How to quit the QEMU monitor when not using a GUI?

  • -monitor stdio: Put QEMU monitor console in the terminal, while guest OS kept in created video device.
info

switch between monitor console and guest OS: Ctrl+Alt+1 or Ctrl+Alt+2.

  • -net none: Disable iPXE.

Boot x86_64 ISO image

Boot x86_64 image in Windows,

efi="$PWD/UEFI/ovmf-x64/OVMF-pure-efi.fd"
iso=ubuntu-22.04-live-server-amd64.iso
note

ubuntu-**-amd64.iso support both UEFI and Legacy BIOS boot, QEMU use BIOS when the option -bios is not specified!

  1. Create a disk image to install the ubuntu OS,
qemu-img create -f qcow2 ubuntu-image.qcow2 20G
  1. Boot to run the Ubuntu OS
qemu-system-x86_64 \
-monitor stdio \
-accel whpx \
-m 8G \
-smp 4 \
-drive file=ubuntu-image.qcow2 \
-bios $efi \
-cdrom $iso

Options in details,

  • -accel whpx: use hardware acceleration
  1. [?]Boot the installed Ubuntu OS
# Install OS into a disk image
qemu-system-x86_64 \
-accel whpx \
-m 8G \
-smp 4 \
-bios $efi \
-drive file=ubuntu.qcow2,format=qcow2,if=virtio \

Boot aarch64 ISO image

Emulate aarch64 ISO image in Windows,

efi="$PWD/UEFI/aarch64/QEMU_EFI.fd"
iso="ubuntu-22.04-live-server-arm64.iso"

qemu-system-aarch64 \
-monitor stdio \
-machine virt \
-cpu cortex-a57 \
-m 4G \
-smp 4 \
-drive file=ubuntu.qcow2,format=raw,if=virtio \
-bios $efi \
-cdrom $iso

Emulate aarch64 ISO image in mac M1,

qemu-system-aarch64 \
-monitor stdio \
-machine virt \
-accel hvf \
-cpu host \
-m 4G \
-smp 4 \
-drive file=ubuntu.qcow2,format=raw,if=virtio \
-bios $efi \
-cdrom $iso

Options in details,

  • -accel hvf: use hardware acceleration in mac M1.
  • -cpu host: use mac M1 arm CPU.

Boot a preinstalled image

# linux
fdisk -l ubuntu-core-22-arm64+raspi.img

# osx
hdiutil imageinfo ubuntu-core-22-arm64+raspi.img
kernel="$PWD/TinyCore/boot/vmlinuz64"
initrd=$"$PWD/TinyCore/boot/corepure64.gz"
img=$"$PWD/TinyCorePure64-14.0.iso"
efi="$PWD/UEFI/ovmf-x64/OVMF-pure-efi.fd"

kernel="$PWD/linux_qemu/x86_64/bzImage"
vmlinuz="$PWD/linux_qemu/x86_64/vmlinux"
initrd="$PWD/linux_qemu/x86_64/rootfs.ext2"
img="$PWD/linux_qemu/x86_64/rootfs.ext2"
qemu-system-x86_64 \
-nographic \
-m 4G \
-kernel $kernel \
-initrd $img \
-append "console=ttyS0" \
-netdev user,id=mynet,hostfwd=tcp::2222-:22 \
-device virtio-net-pci,netdev=mynet

Boot linux kernel

Troubleshooting

Resources

UEFI, PC boot process and UEFI with QEMU | joonas.fi

https://medium.com/@ThyCrow/compiling-the-linux-kernel-and-creating-a-bootable-iso-from-it-6afb8d23ba22

https://levelup.gitconnected.com/probably-the-simplest-way-to-install-debian-ubuntu-in-qemu-2db6afde27ef

UEFI on AARCH64 | Welcome to the Mike’s homepage!

OVMF · tianocore/tianocore.github.io Wiki · GitHub

https://wiki.debian.org/Arm64Qemu

http://cdn.kernel.org/pub/linux/kernel/people/will/docs/qemu/qemu-arm64-howto.html

https://futurewei-cloud.github.io/ARM-Datacenter/qemu/how-to-launch-aarch64-vm/

https://xryan.net/p/212