溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

VFIO透?jìng)?/h1>
發(fā)布時(shí)間:2020-07-10 09:42:03 來源:網(wǎng)絡(luò) 閱讀:1863 作者:Lee_1985 欄目:云計(jì)算

在qemu-kvm虛擬化環(huán)境中,為提高虛擬機(jī)性能,需要將主機(jī)(PCI)設(shè)備直通給虛擬機(jī)(vm),即:設(shè)備透?jìng)骷夹g(shù)(也叫設(shè)備直通技術(shù)),該技術(shù)需要主機(jī)支持Intel(VT-d) 或 AMD (IOMMU) 硬件虛擬化加速技術(shù)。

前提要求

linux kernel>3.6
qemu 版本>1.4
虛擬機(jī)系統(tǒng)以u(píng)efi模式啟動(dòng)
宿主機(jī)服務(wù)器開啟IOMMU
宿主機(jī)加載vfio和vfio-pci驅(qū)動(dòng)
宿主機(jī)CPU支持intel-vt/vd

實(shí)驗(yàn)環(huán)境

  Centos7.2-AIO-3.6.0.0.1
  qemu-2.2

準(zhǔn)備工作

服務(wù)器宿主機(jī)

修改系統(tǒng)內(nèi)核啟動(dòng)參數(shù),重啟系統(tǒng),查看是否開啟IOMMU

dmesg | grep -e DMAR -e IOMMU
[ 0.000000] DMAR: IOMMU enabled

查看是否開啟intel-vt-x/vt-d

cat /proc/cpuinfo | grep vmx

執(zhí)行如下腳本檢查是否支持中斷重定向

#!/bin/sh
if [ $(dmesg | grep ecap | wc -l) -eq 0 ]; then
  echo "No interrupt remapping support found"
  exit 1
fi
for i in $(dmesg | grep ecap | awk '{print $NF}'); do
  if [ $(( (0x$i & 0xf) >> 3 )) -ne 1 ]; then
    echo "Interrupt remapping not supported"
    exit 1
  fi
done

如果硬件不支持interrupt remapping,需要執(zhí)行

echo "options vfio_iommu_type1 allow_unsafe_interrupts=1" > /etc/modprobe.d/iommu_unsafe_interrupts.conf

加載vfio驅(qū)動(dòng)

modprobe vfio
modprobe vfio-pci

安裝OVMF引導(dǎo)虛擬機(jī)(OVMF支持uefi啟動(dòng))

wget http://www.kraxel.org/repos/firmware.repo
yum install edk2.git-ovmf-x64.noarch

透?jìng)?/h4>

設(shè)備在主機(jī)中解綁

[root@localhost ~]# lspci -nn|more
00:00.0 Host bridge [0600]: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge [8086:7190] (rev 01)
00:01.0 PCI bridge [0604]: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge [8086:7191] (rev 01)
00:07.0 ISA bridge [0601]: Intel Corporation 82371AB/EB/MB PIIX4 ISA [8086:7110] (rev 08)
00:07.1 IDE interface [0101]: Intel Corporation 82371AB/EB/MB PIIX4 IDE [8086:7111] (rev 01)
00:07.3 Bridge [0680]: Intel Corporation 82371AB/EB/MB PIIX4 ACPI [8086:7113] (rev 08)
00:07.7 System peripheral [0880]: VMware Virtual Machine Communication Interface [15ad:0740] (rev 10)
00:0f.0 VGA compatible controller [0300]: VMware SVGA II Adapter [15ad:0405]
00:10.0 SCSI storage controller [0100]: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI [1000:0030] (rev 01)
00:11.0 PCI bridge [0604]: VMware PCI bridge [15ad:0790] (rev 02)
00:15.0 PCI bridge [0604]: VMware PCI Express Root Port [15ad:07a0] (rev 01)
00:15.1 PCI bridge [0604]: VMware PCI Express Root Port [15ad:07a0] (rev 01)
00:15.2 PCI bridge [0604]: VMware PCI Express Root Port [15ad:07a0] (rev 01)

找到設(shè)備,解綁

echo 0000:00:07.1 > /sys/bus/pci/devices/0000\:00\:07.1/driver/unbind

生成vfio設(shè)備

echo 8086 7111 > /sys/bus/pci/drivers/vfio-pci/new_id

此時(shí):/dev/vfio下面會(huì)有個(gè)以阿拉伯?dāng)?shù)字命名的文件,對(duì)應(yīng)vfio設(shè)備組

啟動(dòng)虛擬機(jī)

加參數(shù):-device vfio-pci,host=00:05:00.0  #透?jìng)鞯脑O(shè)備
加參數(shù):-drive if=pflash,format=raw,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd #虛擬機(jī)bios
/usr/libexec/qemu-kvm -M pc-i440fx-rhel7.0.0 -enable-kvm -m 2048 -smp 2 -drive if=pflash,format=raw,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd -drive id=disk0,if=none,format=qcow2,file=test.qcow2 -device virtio-blk-pci,drive=disk0,bootindex=0 -drive id=cd0,if=none,format=raw,readonly,file=CentOS-7-x86_64-Minimal-1503-01.iso -device ide-cd,bus=ide.1,drive=cd0,bootindex=1 -global PIIX4_PM.disable_s3=0 -global isa-debugcon.iobase=0x402 -debugcon file:fedora.ovmf.log -monitor stdio -device piix3-usb-uhci -device usb-tablet -netdev id=net0,type=user -device virtio-net-pci,netdev=net0,romfile= -device qxl-vga -spice port=3000,disable-ticketing
/usr/libexec/qemu-kvm -M pc-i440fx-rhel7.0.0 -enable-kvm -m 2048 -smp 2 -drive if=pflash,format=raw,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd -drive id=disk0,if=none,format=qcow2,file=test.qcow2 -device virtio-blk-pci,drive=disk0,bootindex=0 -global PIIX4_PM.disable_s3=0 -global isa-debugcon.iobase=0x402 -debugcon file:fedora.ovmf.log -monitor stdio -device piix3-usb-uhci -device usb-tablet -netdev id=net0,type=user -device virtio-net-pci,netdev=net0,romfile= -device qxl-vga -spice port=3000,disable-ticketing -device vfio-pci,host=00:05:00.0

客戶端訪問

 spicy -h ip -p 3000

在虛擬機(jī)內(nèi)部執(zhí)行

lspci -nn
lsblk

查看是否透?jìng)鞒晒?/p>

參考資料

https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF
https://www.kernel.org/doc/Documentation/vfio.txt
http://pve.proxmox.com/wiki/Pci_passthrough

向AI問一下細(xì)節(jié)
AI