Wiki
QEMU-KVM Virtual Machines
Existing English translations were reused. 2 current source blocks remain in Simplified Chinese, without triggering paid translation.
QEMU-KVM is a high-performance virtual machine. The following uses the installation of Rocky Linux 9 as an example.
1.Install required software
确保主机已经安装了 QEMU、KVM 和相关工具。以基于 Debian/Ubuntu 的系统为例,执行以下命令:
sudo apt update
sudo apt install qemu-kvm qemu-img virt-manager virt-install virt-viewer libvirt libvirt-daemon libvirt-daemon-qemu bridge-utils virglrenderer
For Fedora/CentOS/Rocky Linux-based systems, the command may be:
sudo dnf install qemu-kvm qemu-img virt-manager virt-install virt-viewer libvirt libvirt-daemon libvirt-daemon-qemu bridge-utils virglrenderer
After installation is complete, ensure that the current user is in the kvm group (otherwise, you may not have permission to use KVM acceleration):
sudo usermod -aG kvm $USER
Note: After modifying the group, you may need to log out and log in again for the changes to take effect.
2.Enable KVM hardware acceleration
Run the following command to check whether KVM is enabled:
egrep -c '(vmx|svm)' /proc/cpuinfo
如果输出 0,说明 CPU 不支持 KVM 或未开启 VT-x(Intel)/ SVM(AMD),需要在 BIOS 里开启 Intel VT-x 或 AMD SVM 。
Then check whether the KVM module is loaded:
# 验证 KVM 加速可用
lsmod | grep kvm
# 验证当前用户有权限访问 /dev/kvm
ls -l /dev/kvm
If there is no output, load the KVM module:
sudo modprobe kvm_intel # Intel 处理器
sudo modprobe kvm_amd # AMD 处理器
3.Create a virtual hard disk file
You need to create a hard disk image file for the virtual machine. Here, we use the qcow2 format and 100GB as an example (you can adjust the size as needed). Run in the terminal:
qemu-img create -f qcow2 disk.qcow2 100G
This will generate a virtual hard disk file named disk.qcow2 in the current directory.
4.Prepare the OVMF (UEFI firmware) file
Install the OVMF package. For example, on Ubuntu it can be installed using the following command:
# 一般电脑都默认安装过了
sudo apt install ovmf # Debian/Ubuntu
sudo dnf install edk2-ovmf # RHEL/CentOS/Fedora
After installation, locate the file path, which is usually under /usr/share/OVMF/OVMF_CODE.fd or /usr/share/ovmf/OVMF.fd. Use the correct path directly in the command. (There are examples in the script below; you only need to ensure that these two files exist under your /usr/share/OVMF/.)

You can also download the following file:
This content cannot be displayed outside of Feishu Docs at this time
- Write or adjust run.sh scripts
Write the following content into your run.sh file (you can use a text editor).
sudo vim ./run.sh
The following script example uses QEMU to start a virtual machine and load an ISO image:
#!/bin/bash
qemu-system-x86_64 \
-machine q35,vmport=off,kernel_irqchip=on \
-accel kvm \
-cpu host,kvm=on,vmx=on,migratable=on,+invtsc \
-smp 8,sockets=1,cores=4,threads=2 \
-m 8G,slots=4,maxmem=32G \
-device virtio-gpu-gl-pci,max_outputs=1 \
-vga none \
-display sdl,gl=on \
-audiodev pa,id=pa1,server=unix:/run/user/1000/pulse/native \
-device ich9-intel-hda \
-device hda-micro,audiodev=pa1 \
-device qemu-xhci,id=xhci \
-device virtio-tablet-pci \
-device usb-kbd,bus=xhci.0 \
-bios OVMF-pure-efi64.fd \
-boot d \
-blockdev driver=qcow2,node-name=disk1,file.driver=file,file.cache.direct=on,file.aio=io_uring,file.filename=disk.qcow2 \
-device virtio-blk-pci,drive=disk1 \
-drive file=kubuntu-20.04.6-desktop-amd64.iso,media=cdrom,if=none,id=cdrom \
-device usb-storage,drive=cdrom,removable=on \
-nic user,model=virtio-net-pci,hostfwd=tcp::8022-:22 \
-monitor stdio \
-parallel none \
-serial none \
-msg timestamp=on
After saving, grant execution permission to the script:
sudo chmod +x run.sh
5.Start virtual machine
Ensure the following files exist in the current directory:
-
run.sh -
disk.qcow2(The newly created virtual hard disk) -
Rocky-9.4-x86_64-dvd.iso(Rocky Linux 9 installation ISO) -
OVMF-pure-efi64.fdFile
Then run the script in the terminal:
./run.sh
At this point, QEMU should launch a window and load the ISO image into the installation interface.
6.Install system
In the virtual machine window, you will see the Linux installation interface. Follow the steps of the installation wizard to proceed with the installation. After the installation is complete, you may need to adjust the boot order to use the hard disk as the boot medium (if it still boots from the CD by default).
Recommendation: After installing the system, shut down the virtual machine, then modify run.sh Remove the ISO image or change it to an optional boot device, so that the next time it starts, it will boot directly from the hard disk.
For example, remove -drive file=Rocky-9.5-x86_64-dvd.iso,media=cdrom or replace it with a boot order parameter.
7.Other operations
7.1.GPU passthrough (be cautious; do not mess with it if you don't know what you're doing)
This operation means giving the graphics card entirely to the virtual machine; the physical host will then be unable to use the graphics card.
- Host and IOMMU configuration
(1) Enable IOMMU (using Intel as an example)
Edit /etc/default/grub,in GRUB_CMDLINE_LINUX_DEFAULT add:
intel_iommu=on iommu=pt
For AMD hosts, set amd_iommu=on.

Reboot after updating GRUB:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg # CentOS/Fedora/Rocky 系列
# 或者
sudo update-grub # Ubuntu/Debian 系统
After restarting, you can use the following command to confirm whether IOMMU is enabled:
dmesg | grep -e DMAR -e IOMMU
If you see information like “DMAR: IOMMU enabled”, it indicates that it has taken effect.
(2) Find your NVIDIA graphics card device ID Use lspci -nn | grep NVIDIA to find the graphics card, for example:
tungchiahui@Dell-G15-5511:~/Downloads$ lspci -nn | grep NVIDIA
01:00.0 VGA compatible controller [0300]: NVIDIA Corporation GA106M [GeForce RTX 3060 Mobile / M
ax-Q] [10de:2560] (rev a1)
01:00.1 Audio device [0403]: NVIDIA Corporation GA106 High Definition Audio Controller [10de:228
e] (rev a1)
Note the above PCI address and device ID.
GPU PCI Address:01:00.0 Device ID:10de:2560
Graphics card audio PCI address:01:00.1 Device ID:10de:228e
(3) Bind the device to the vfio driver
Another way is to create a modprobe configuration file to let vfio-pci bind these devices when loaded.
- Create file (e.g.,
/etc/modprobe.d/vfio.conf):
sudo vim /etc/modprobe.d/vfio.conf
- Write to file:
options vfio-pci ids=10de:2560,10de:228e
- After saving the file, update initramfs:
# debian系选这个
sudo update-initramfs -u
# 红帽系选这个
sudo dracut --force
-
Restart the system.
-
Check device binding status
After restarting, you can use the following command to check whether the device has been taken over by the vfio-pci driver:
lspci -nnk | grep -A3 "10de:2560"
You should see something like:
01:00.0 VGA compatible controller [0300]: NVIDIA Corporation GA106M [GeForce RTX 3060 Mobile / Max-Q] [10de:2560]
Subsystem: ...
Kernel driver in use: vfio-pci
Similarly, use a similar command to check the audio device (10de:228e).
lspci -nnk | grep -A3 "10de:228e"
If you see Kernel driver in use: vfio-pci, the binding is successful.
(4) Startup options
Add the following two lines in run.sh
# 直通 NVIDIA 显卡
-device vfio-pci,host=01:00.0,multifunction=on,x-vga=on \
-device vfio-pci,host=01:00.1 \