Qemu

From wikinotes

qemu is a great CLI vitualization software package. It is used a lot for cross compiling.

Documentation

Official
User Manual https://qemu.weilnetz.de/doc/qemu-doc.html
Official Wiki https://wiki.qemu.org/Main_Page
3rd Party
Arch Wiki Docs (good recommendations) https://wiki.archlinux.org/index.php/QEMU
Gentoo Wiki Docs (quick start) https://wiki.gentoo.org/wiki/QEMU
SUSE QEMU (detailed, approachable) https://www.suse.com/documentation/sles11/book_kvm/data/part_2_book_book_kvm.html

Addrs/Ports

within guest
10.0.2.2 host ip
\\10.0.2.4\qemu qemu's smbshare

Install

sudo pacman -S qemu             # qemu-system-x86_64
sudo pacman -S qemu-arch-extra  # qemu-system-arm, qemu-system-aarch64, etc

Usage

This is just an overview. See qemu syntax for more comprehensive notes.

Running Qemu

sudo qemu-system-x86_64 \
    -hda /dev/sdb                    `# set hda (install media, maybe?)` \
    -hdb /home/will/dev/myhdd.disk   `# set hdb` \
    -m 2G                            `# run with 2G memory`

Qemu Monitor

After running qemu, you'll be prompted with a monitor window where you can watch/interact with your qemu window.

There are a few different implementations of this (GTK (default), SDL, ...) and each has a different set of hotkeys.

# ===
# GTK
# ===
Ctrl + Alt + G   # release mouse

# ===
# SDL
# ===
L_Ctrl + L_Alt    # release mouse

# ===========
# QXL (spice)
# ===========
L_Shift + F12    # release mouse

Creating Virtual Disks

qemu-img create -f raw myhdd.disk 4G    # Create Virtual image
qemu-img resize myhdd.disk +10G         # You can increase disk size if needed

This is covered in much greater detail in qemu syntax .

using EFI

Intel provides a bios implementation, but it is not included in the base qemu package.

sudo pacman -S ovmf   # intel's qemu bios
cp /usr/share/ovmf/x64/OVMR_CODE.fd ./bios.bin
sudo qemu-system-86_64 -L . --bios bios.bin      # start qemu using bios

shared folders

If you already have a samba server running with a network share, you can access it within qemu. Otherwise, you can start a network share from qemu controlled using it's params.

sudo qemu-system-x86_64 \
    -net nic \
    -net user,smb=/home/will/progs

Within your host, you can access it:

\\10.0.2.4\qemu

mklink /D  C:\Users\willjp\progs_shared  \\10.0.2.4\qemu  # on windows, you may bind this to a directory for convenience

forward ports

forwarding ports is done with hostfwd and guestfwd.

# forward guest-ports 445/139 to host-9445/9139
sudo qemu-system-x86_64 \
    -device e1000,netdev=net0 \
    -netdev user,id=net0,hostfwd=tcp::9445-:445,hostfwd=tcp::9139-:139


Sharing VideoCard

WARNING:

  • core-i7-3770k does not have required vt-d
  • best LGA1155 socket CPU with vt-d is core-i7-3770 (non-k model ~350$USD)

Wait until next build, sadly.

(best overall guide) archwiki pci passthrough with OVMF https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF#X_does_not_start_after_enabling_vfio_pci
archwiki Xorg prime https://wiki.archlinux.org/index.php/PRIME
arch/i3/nvidia gpu passthrough https://medium.com/@authmane512/how-to-configure-a-second-graphic-card-on-arch-linux-or-debian-9-stretch-9bd7f7fb7ff6
qemu gpu passthrough tut https://chanster.net/2016/03/05/qemu-gpu-passthrough/
gpu swapping scripts https://gitlab.com/YuriAlek/vfio
gpu swapping scripts https://github.com/joeknock90/Single-GPU-Passthrough

Xorg PRIME allows you to assign different GPUs to different monitors.
Use a 2nd videocard to render (but display from main gpu)
And finally QEMU GPU passthrough (allow VM to use GPU).

Hardware Requirements


  • vt-d (not just vt-x)
  • vt-x (also required)
  • iommu compatible motherboard/cpu



BIOS Settings


Tested With:

  • Gigabyte-UD5H + i7-3770k


Check for and enable

  • Settings related to virtualization
    some proxy of vt-d, vt-x
  • Always enable intel GPU (not auto)
    without, dedicated gpu-only will appear in xrandr --listproviders
  • Booted OS must be UEFI


Confirm GPUs available


install video drivers

  • for your chipset (ex: xf86-video-intel)
  • for your gpu (ex: nvidia)


Do not generate a /etc/X11/xorg.conf for your proprietary driver.
they can be configured to restrict available devices. Keep xorg config entirely automated.

xrandr --listprovicers

You should see output like

Providers: number : 2
Provider 0: id: 0x1b8 cap: 0x1, Source Output crtcs: 4 outputs: 7 associated providers: 1 name:NVIDIA-0
Provider 1: id: 0x20e cap: 0xb, Source Output, Sink Output, Sink Offload crtcs: 4 outputs: 8 associated providers: 1 name:Intel

If you do not

  • confirm your proprietary driver has not installed any Xorg configs
  • confirm your BIOS settings


Enable IOMMU in kernel


Enable iommu for your chipset manufacturer (ex: intel/amd).

1. Add kernel parameters from your bootloader, to enable IOMMU (vtx-d/amd-vi)

/boot/loader/entries/archlinux.conf

title Arch Linux
linux /vmlinux-linux
initrd /initramfsf-linux.img
options root=PARTUUID=LKJLKJKJ rw  iommu=pt intel_iommu=on  # <-- add 'intel_iommu=on', or 'amd_iommu=on'

reboot, then confirm IOMMU is enabled

dmesg | grep -e DMAR -e IOMMU

# GOOD:  [    0.074932] DMAR: IOMMU enabled
# BAD:   [    0.397606] AMD-Vi: AMD IOMMUv2 functionality not available on this system

Also ensure iommu_groups exist (otherwise vt-d or vt-x may not be enabled or supported)

ls /sys/kernel/iommu_groups/

assign your videocard to vfio (takes ownership before host)


1. verify the vfio-pci kernel module

modprobe vfio-pci  # loads vfio-pci kernel module - simply verify there is no errors on load

2. blacklist driver used by videocard in host, so that it does not try to use it on boot (optional?)

# /etc/modprobe.d/00-modprobe.conf
blacklist nvidia

3. Obtain Bus Address of VideoCard

# obtain PCI device id of videocard  (ex:  '01:00.1')
lspci -nn|grep -i "nvidia\|radeon"

# obtain the full bus address (ex: '0000:01:00.1')
ls /sys/bus/pci/devices | grep -i '01:00.1'

4. Enable vfio kernel module

/etc/modprobe.d/vfio.conf

options vfio-pci ids=10de:13c2,10de:0fbb  # <-- list of PCI bus ids to assign to vfio

5. Modify kernel-module load order, enforcing that vfio loads before another module tries to use GPU.

/etc/mkinitcpio.conf

MODULES=(... vfio_pci vfio vfio_iommu_type1 vfio_virqfd ...)  # load-order BEFORE graphics drivers
HOOKS=(... modconf ...)                                       # modconf must be present

6. Regenerate initramfs

mkinitcpio -p linux

7. Reboot, confirm everything worked okay

dmesg | grep -i vfio         # check for VFIO, vfio-pci
ls /sys/kernel/iommu_groups  # if blank, vt-d not enabled

pass device to qemu


qemu-system-x86_64 \
    -enable-kvm \
    -device vfio-pci,host=01:00.1,x-vga=on,multifunction=on \

viewers

spice fast, high resolution, no mouse-grabbing, shared clipboard
virt-viewer standard

Examples

qemu example archlinux
qemu example existing windows disk
qemu example VGA passthrough

Qemu Projects

qemu android-x86 virtualize android
qemu osx-kvm virtualize macos