[[PageOutline]]
{{{
#!html
實作七:Xen PCI Passthrough 操作
}}}
----
= 【前言】 =
* '''''小叮嚀__'''''
* 為了使 Xen patched Kerenl 能支援 PCI Passthrough, 請務必確認以下Xen 相關的核心配置.
{{{
CONFIG_XEN_PCIDEV_FRONTEND=y
CONFIG_XEN_PCIDEV_BACKEND=y
CONFIG_XEN_PCIDEV_BACKEND_PASS is not set
CONFIG_XEN_PCIDEV_BACKEND_VPCI=y
CONFIG_XEN_PCIDEV_BACKEND_SLOT is not set
}}}
* '''''小技巧__'''''
* Binding at Boot-Time.
* 直接將顯示卡所佔的 PCI_Address 在開機時就隱藏起來,使得一開始便能直接 bind 到該 PCI_Address.
* 透過 lspci 指令來查詢顯示卡所佔的實體 PCI Address.
{{{
/boot/vmlinuz-2.6.22.9 root=UUID=d3fa560e-7071-46d8-a168-036f40960c7b ro console=tty0 pciback.hide=(0000:01:00.0)
}}}
* 如果透過 lspci 指令查出顯示卡為 unknown 或是其他不明資訊, 請先更新 PCI ID database 試看看.
{{{
$ update-pciids
}}}
----
= 【Step 0: 流程】 =
* 本範例將說明如何使用及設定 Xen PCI Passthrough
{{{
#!graphviz
digraph finite_state_machine {
rankdir=LR;
ranksep=0.3;
size="11,8";
node[shape=box,width=3.0];
"1.先確認主機上的顯示卡相關資訊及規格" -> "2. 建立一台虛擬機器供測試用" -> "3.如何設定 PCI Frontend ?";
"1.先確認主機上的顯示卡相關資訊及規格" -> "2. 建立一台虛擬機器供測試用" -> "4.如何設定 PCI Backend ?";
"5.如何設定 Xen 硬體直接存取 ?" -> "6.啟動虛擬機器與基本環境設定" -> "7.檢查虛擬機器有否成功 allocate 到顯示卡的資源";
}
}}}
----
= 【Step 1: 首先查看主機上的顯卡資訊】 =
{{{
$ lspci -vv | more
}}}
{{{
#!sh
01:00.0 VGA compatible controller: nVidia Corporation G92 [GeForce 9800 GT] (rev a2)
Subsystem: ASUSTeK Computer Inc. Device 82a0
Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx-
Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- SERR-
Kernel driver in use: nvidia
Kernel modules: nvidia, nvidiafb
}}}
----
= 【Step 2: 產生一台實驗用虛擬機器】 =
* 設定你想要怎樣規格的虛擬機器.
{{{
$ sudo vim /etc/xen-tools/xen-tools.conf
}}}
{{{
#!sh
dir = /home
install-method = debootstrap
size = 4Gb # Disk image size.
memory = 256Mb # Memory size
swap = 128Mb # Swap size
fs = ext3 # use the EXT3 filesystem for the disk image.
dist = hardy # Default distribution to install. ---> For CUDA Support (Ubuntu 8.0.4)
image = sparse # Specify sparse vs. full disk images.
gateway = 192.168.100.254
netmask = 255.255.255.0
broadcast = 192.168.100.255
kernel = /boot/vmlinuz-`uname -r`
initrd = /boot/initrd.img-`uname -r`
mirror = http://gb.archive.ubuntu.com/ubuntu/
ext3_options = noatime,nodiratime,errors=remount-ro
ext2_options = noatime,nodiratime,errors=remount-ro
xfs_options = defaults
reiser_options = defaults
}}}
{{{
$ sudo xen-create-image --hostname=xg01 --ip=192.168.100.X --mac=00:16:XX:XX:XX:XX --force
}}}
----
= 【Step 3: PCI Frontend Configuration 設定你的 DomU 】 =
{{{
$ sudo vim /etc/xen/xg01.cfg
}}}
{{{
#!sh
kernel = '/boot/vmlinuz-2.6.22.9'
ramdisk = '/boot/initrd.img-2.6.22.9'
memory = '256'
vcpus = '4'
# 配置你的 PCIE 顯示卡
pci = ['01:00.0']
root = '/dev/sda2 ro'
disk = [
'file:/home/domains/xg01/disk.img,sda2,w',
'file:/home/domains/xg01/swap.img,sda1,w',
]
name = 'xg01'
#
# Networking
#
vif = [ 'ip=192.168.100.X ,mac=00:16:XX:XX:XX:XX' ]
#
# Behaviour
#
on_poweroff = 'destroy'
on_reboot = 'restart'
on_crash = 'restart'
}}}
----
= 【Step 4: PCI Backend Configuration 設定你的 Dom0 】 =
* 切換身份為 Super user.
{{{
$ sudo su -
}}}
* 隱藏 dom0 的PCI 匯流排的位址, 並交由 pciback 模組來控制該匯流排存取權.
{{{
$ echo -n "0000:01:00.0" > /sys/bus/pci/drivers/nvidia/unbind
}}}
* 將 dev_ids 轉到 pciback 成為新的 binding slot.
{{{
$ echo -n "0000:01:00.0" > /sys/bus/pci/drivers/pciback/new_slot
$ echo -n "0000:01:00.0" > /sys/bus/pci/drivers/pciback/bind
}}}
{{{
$ cat /sys/bus/pci/drivers/pciback/slots
}}}
{{{
#!sh
0000:01:00.0
}}}
* 警告: 請確認您的顯示卡資源沒有被其 Kernel module 佔有及控制.
{{{
$ ls -al /sys/bus/pci/devices/0000:01:00.0/ | grep driver
}}}
{{{
#!sh
driver -> ../../../../bus/pci/drivers/nvidia ---> 此連結應該已經不存在. NVIDIA module 已無該裝置控制權.
driver -> ../../../../bus/pci/drivers/pciback ---> 控制權應該已經交賦予 pciback module.
}}}
----
= 【Step 5: 硬體定址存取設定】 =
==== Permissive Flag ====
{{{
$ sudo vim /etc/xen/xend-pci-permissive.sxp
}}}
{{{
#!sh
(unconstrained_dev_ids
#('0123:4567:89AB:CDEF')
('0000:01:00.0')
)
}}}
==== User-space Quirks ====
{{{
$ sudo vim /etc/xen/xend-pci-quirks.sxp
}}}
{{{
#!sh
(pci_ids
# Entries are formated as follows:
# :[::]
('10de:0605' # NVIDIA 9800GT
)
)
}}}
----
= 【Step 6: 啟動並登入你的虛擬機器 DomU 】 =
* 說明: 先用 root 免密碼登入.
* @ Dom0
{{{
$ sudo xm create -c xg01.cfg
}}}
----
= 【Step 7: 檢查 DomU 的 PCI Passthrough 有無成功】 =
{{{
$ lspci
}}}
{{{
#!sh
00:00.0 VGA compatible controller: nVidia Corporation Unknown device 0605 (rev a2)
}}}
* 更新 PCI ID Database.(當您發現系統無法正常偵測到你的顯示卡時)
{{{
$ sudo apt-get update
$ sudo apt-get install wget
$ sudo update-pciids
}}}
* 查看顯卡資訊有無正常顯示.
{{{
$ lspci
}}}
{{{
#!sh
00:00.0 VGA compatible controller: nVidia Corporation G92 [GeForce 9800 GT] (rev a2)
}}}
* 查看顯卡資源有無順利分配到 DomU.
{{{
$ dmesg | grep pci
}}}
{{{
#!sh
pcifront pci-0: Installing PCI frontend
pcifront pci-0: Creating PCI Frontend Bus 0000:00
pciback 0000:00:00.0: probing...
pciback: pcistub_init_devices_late
}}}
----
【討論 & 休息】
[[BR]]
[[BR]]