A Note of qemu-kvm

2023-10-06

Preparation

Ignore MSRS, see: Certain Windows games/applications crashing/causing a bluescreen - ArchLinux Wiki

Run:

sudo modprobe kvm; echo 1 | sudo tee /sys/module/kvm/parameters/ignore_msrs

Then add this to /etc/modprobe.md/kvm.conf:

options kvm ignore_msrs=1

Configure permissions:

sudo usermod -aG kvm $(whoami)
sudo usermod -aG libvirt $(whoami)
sudo usermod -aG input $(whoami)

Create Virtual Machine

Create disk image:

qemu-img create -f qcow2 os.qcow2 256G

Then prepare a install image, for example Windows 10 or Ubuntu. Here we assume that the install image is install.iso. Then create a startup script start.sh:

#!/bin/bash

chipset="type=q35,kernel_irqchip=on,mem-merge=on"
vcpu="host"
hyper="kvm,thread=multi"
CPU_SOCKETS="1"
CPU_CORES="2"
CPU_THREADS="2"
MEM=4096

qemu-system-x86_64 \
    -enable-kvm \
    -m "${MEM}" \
    -cpu Penryn,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt,check \
    -machine ${chipset} \
    -cpu ${vcpu} \
    -device ich9-ahci,id=sata \
    -smp "$CPU_THREADS",cores="$CPU_CORES",sockets="$CPU_SOCKETS" \
    -drive id=HDD,if=none,file="./os.qcow2",format=qcow2 \
    -device ide-hd,bus=sata.3,drive=HDD \
    -netdev user,id=net0,hostfwd=tcp::8022-:22 \
    -device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \ # Software NAT,port 22 mapped to host's 8022
    -usb  \
    -device usb-tablet \ # Seamless switch of mouse
    -device qemu-xhci,id=xhci \ # Full speed USB device
    -vga virtio \
    -vnc :0 \ # VNC listens port 5900
    -cdrom ./install.iso \ # Boot disk
    #-usb -device usb-host,hostbus=1,hostaddr=4 \

The last line with "cdrom" can be commented out once the installation is finished.

Connect to USB Devices

Use lsusb to query the bus number and device number of USB port, then edit the last line of the script. Now you need sudo to run this script.

I failed to configure the QEMU virtual sound device, but I finally managed to bridge a USB sound card to the virtual machine so I can hear the music playing in the virtual machine.



Email: i (at) mistivia (dot) com