第 7 節

QEMU-KVM virtual machine

0瀏覽次數0訪問次數--跳出率--平均停留

QEMU-KVM is a high-performance virtual machine. The following uses the installation of Rocky Linux 9 as an example.

Install the required software

Make sure the host has QEMU, KVM, and related tools installed. For Debian/Ubuntu-based systems, run the following command:

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 systems based on Fedora/CentOS/Rocky Linux, 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, ensure the current user is in the kvm group (otherwise they may not have permission to use KVM acceleration):

sudo usermod -aG kvm $USER

Note: You may need to log out and log back in for the group change to take effect.

Enable KVM hardware acceleration

Run the following command to check if KVM is enabled:

egrep -c '(vmx|svm)' /proc/cpuinfo

If the output is 0, it means the CPU does not support KVM or VT-x (Intel) / SVM (AMD) is not enabled. You need to enable Intel VT-x or AMD SVM in the BIOS.

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 处理器

Create a virtual hard disk file

You need to create a hard disk image file for the virtual machine. Here, using qcow2 format and 100GB as an example (you can adjust the size as needed). Run the following 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.

Prepare the OVMF (UEFI firmware) files.

Install the OVMF package. For example, on Ubuntu it can be installed with the following command:


# 一般电脑都默认安装过了
sudo apt install ovmf # Debian/Ubuntu
sudo dnf install edk2-ovmf # RHEL/CentOS/Fedora

After installation, locate the file path, usually at /usr/share/OVMF/OVMF_CODE.fd or /usr/share/ovmf/OVMF.fd. Use the correct path directly in the command. (There is an example in the script below; you just need to ensure that these two files are present under your /usr/share/OVMF/.)

You can also download the following file:

Unable to display this content outside of Feishu documents for now.

  1. Write or adjust the run.sh script.

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, give the script execute permissions:

sudo chmod +x run.sh

Start the virtual machine.

Make sure the following files are in the current directory:

  • run.sh
  • disk.qcow2 (the virtual hard disk you just created)
  • Rocky-9.4-x86_64-dvd.iso (Rocky Linux 9 Installation ISO)
  • OVMF-pure-efi64.fd file

Then run the script in the terminal:

./run.sh

At this point, QEMU should launch a window and load the ISO image to enter the installation interface.

Install the system

In the virtual machine window, you will see the Linux installation screen. Follow the steps in the installation wizard to complete the installation. After installation, you may need to adjust the boot order to set the hard drive as the boot medium (if it still defaults to booting from the CD).

Tip: After installing the system, shut down the virtual machine, then modify run.sh to remove the ISO image or change it to an optional boot device. This way, the next time you start up, it will boot directly from the hard drive.

For example, remove -drive file=Rocky-9.5-x86_64-dvd.iso,media=cdrom, or replace it with a startup sequence parameter.

Other operations

GPU Passthrough (Proceed with caution; do not attempt if you are unsure)

This operation means dedicating the GPU entirely to the virtual machine, so the physical host machine can no longer use the graphics card.

  1. Host and IOMMU Configuration

(1) Enable IOMMU (using Intel as an example)

Edit /etc/default/grub, add to GRUB_CMDLINE_LINUX_DEFAULT:

intel_iommu=on iommu=pt

For AMD hosts, set amd_iommu=on.

After updating grub, restart:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg   # CentOS/Fedora/Rocky 系列

# 或者
sudo update-grub   # Ubuntu/Debian 系统

You can use the following command after a reboot to confirm whether IOMMU is enabled:

dmesg | grep -e DMAR -e IOMMU

If you see information like "DMAR: IOMMU enabled", it means it has taken effect.

(2) Find your NVIDIA graphics card device ID Use lspci -nn|grep NVIDIA` searches for graphics cards, 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)

Write down the PCI address and device ID above.

Graphics Card PCI Address: 01:00.0 Device ID: 10de:2560

Graphics Card Audio PCI Address: 01:00.1 Device ID: 10de:228e

(3) Binding the device to the vfio driver

Another way is to create a modprobe configuration file so that vfio-pci binds these devices at load time.

  1. Create the file (e.g., /etc/modprobe.d/vfio.conf):
sudo vim /etc/modprobe.d/vfio.conf
  1. Write into the file:
options vfio-pci ids=10de:2560,10de:228e
  1. After saving the file, update the initramfs:

# debian系选这个
sudo update-initramfs -u

# 红帽系选这个
sudo dracut --force
  1. Restart the system.
  2. Check the device binding status.

After rebooting, 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

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, it means the binding was successful.

(4) Startup Options

In run.sh, add the following two lines:


    # 直通 NVIDIA 显卡
    -device vfio-pci,host=01:00.0,multifunction=on,x-vga=on \
    -device vfio-pci,host=01:00.1 \
音乐页