../

QEMU-KVM Usage Notes

2023-10-06

Preparation

Ignore MSRS, refer to:

Run:

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

Also add the following to /etc/modprobe.d/kvm.conf:

options kvm ignore_msrs=1

Permission configuration:

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

Creating a Virtual Machine

First, create the disk image:

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

Then prepare the installation disk image, such as Windows 10 or Ubuntu. Here we assume the installation disk is install.iso. Then create the 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 \ # User networking (Soft NAT), port 22 mapped to host's 8022
    -usb  \
    -device usb-tablet \ # Support seamless mouse switching
    -device qemu-xhci,id=xhci \ # Support full-speed USB devices
    -vga virtio \
    -vnc :0 \ # VNC listens on port 5900
    -cdrom ./install.iso \ # Boot disk
    #-usb -device usb-host,hostbus=1,hostaddr=4 \

After completing the installation, you can comment out the cdrom line.

Connecting External USB Devices

Use lsusb to query the bus number and device number of the port, then use the last line (in the script above) to connect it to the virtual machine. You will need to run this with sudo privileges.

I was unable to successfully configure the audio, but using an external USB sound card and passing through the USB works fine.


Mistivia - https://mistivia.com