Local Displayが使えるiGVT gが有効なKVMを構築する - YoshiakiSugiyama/xen-igvtg-install GitHub Wiki

Local Displayが使えるiGVT-gが有効なKVMを構築する

※書きかけ。
XenGTのやり方を基に手順書書いてやってみた記録。

  • Ubuntu Server 18.04.2 LTS、EncryptLVM環境、ホストはSecure Bootが無効。(Ubuntu 18.04.1かつLinux Kernel 4.17までは有効なUEFIブート環境で利用。) ** Serverを利用した理由はUSBからのみBIOSブートが可能な環境へインストールしたため。また、公式の情報を確認したうえでCUI環境でのセットアップのしやすさを考え、Serverを選択した。(mini.isoはUEFIモードでコンピュータを起動するための適切なファイルが入っていない。) ** 詳しくはMinimalCDのダウンロードページInstallationのUEFI and BIOSを参照。
  • Intel PTTによるTPM2.0は/dev/tpm0が「Operation Timed out」と表示されることがあるが、Discrete TPM 1.2が使えればそれを使うことで対応ができる。(Lenovo L560で確認)
  • 基本的にはGVTg_Setup_Guide に従い、これにはGuideにはない、気を付けるべきことを主に記述しています。
  • EFIで512MB、/bootで2GB(構築後1.2GBも消費したため)確保し、EncryptLVMで64GBをホストOS領域として用いて利用している環境で構築した。他の空き領域はデータ領域などとして利用。
  • このiGVT-gをインストールする前に通常のapt-getなどでインストールできるKVMなどをインストールしてはならない。(KVMGTは特殊なQEMUを使用するため)
  • PCIパススルーも問題なくできるが、vGPUと同時はSeaBIOSを利用するときに問題が発生することがある。OVMFも同様なところがある?
  • GVT-gが有効のとき(ブート時にi915.enable_gvt=1となっているとき)はメモリがおよそ512MB占有される。なお、これはLinuxのタスクマネージャーからは見えないことがある。
  • できないこと ** Windowsの高速スタートアップには対応していない ** UEFI+GVT-gの組み合わせ(libvirtでは動いていたので4.14から変更された可能性あり。)
  • KVMGT+dma_buf+SPICEの組み合わせだと、ディスプレイが「汎用PnPモニタ」と表示されるようになる。SPICEのクライアント等をゲストに入れなくても映像+音声をホストで再生できる。(QEMUがオーディオエミュレートに対応しておらず、gtkは映像がとてもなめらかだけど、音が聞けない。)
  • LinuxゲストでGVT-gを利用する際、使っているホストOSよりもゲストは新しいものを利用すること。カーネルに仮想化に必要なGPUドライバが含まれており、clocksourceがtscの際におかしくなったり、安定して動かなくなることがある。
  • 描画転送についてはhttps://www.atmarkit.co.jp/ait/articles/1008/11/news107.html

/rootで作業を始めることを想定しているので、読み間違いなどを防ぐために、特にここから先は/rootで作業を始めることを勧める。

cd /root

セキュリティ設定

ユーザーのパーミッションの厳格化。 初期設定のパーミッションが 755 となっており、ホームディレクトリに対する他のユーザの権限が「読み込み」及び「実行」となっているのはセキュリティの観点からよろしくないことから解除。別途必要に応じ設定を行うが、以下はグループまでの権限は変更しない場合の設定の例。

chmod 0750 /home/<ユーザー名>
sed -i "s/DIR_MODE=0755/DIR_MODE=0750/g" /etc/adduser.conf

カーネルコンパイル中に入り込まれないようUFWで簡素にiptablesを設定し、外部からログインされないようにする。 取り急ぎ、以下の設定で

apt-get update
apt-get upgrade
reboot	// マイクロコードのアップデートが存在した場合
apt-get install ufw
ufw default deny incoming
ufw enable

↓結果↓

ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

Library Dependence カーネルソースをビルドするために必要な以下のものをインストールする

今回はKVMGTでLocal Display機能を有効にすることを前提、さらにはコンパイルのツールとしてgccのほかにclangを求められたことが過去のバージョンで存在しているので、以下を先にインストール。 LXDEなどのGUIは先に入れないとstartxを取られてしまう。openboxが先に入り設定されれば、ログオンをCUIで行うことができる。(CUIでは最小構成でインストールした際に後からLXDEなどのGUIログオンに変更され、その際にログオンができなくなることがある。)

// ホストでサスペンド(S3)を使う場合
apt install molly-guard pm-utils

// 最小限のGUI
apt install xorg openbox menu gconf2 xvfb

// Linuxカーネルをコンパイルして使う際に必要なもの
apt-get install clang kernel-package module-assistant

// KVMGTが有効だとshutdown -h nowなどで電源が切れなくなるので、acpi-supportも別途インストール。(これを入れても電源が切れるのに最新カーネル4.17でも時間がかかる)
apt-get install acpi-support

// bridge-utilsはブリッジを制御するための brctl が入っている。
apt-get install libfdt-dev libpixman-1-dev libssl-dev vim socat libsdl1.2-dev libsdl2-dev libspice-server-dev autoconf libtool xtightvncviewer tightvncserver x11vnc uuid uuid-dev uuid-runtime uml-utilities bridge-utils python-dev liblzma-dev libc6-dev libegl1-mesa-dev libudev-dev libepoxy-dev libdrm-dev libgbm-dev

// dma-buf用
apt-get install spice-client-gtk libgtk2.0-dev libgtk-3-dev

// XenGTのときにWindowsを正常に動かすために要求されたもの。同じように使う模様。
apt-get install libx11-dev libglib2.0-dev pkg-config iproute2 bison flex rpm

// USB関係
apt-get install libusb-dev libusbredirparser-dev libusb-1.0-0-dev

// exFATを使う場合に必要。
apt-get install exfat-fuse exfat-utils

// Ubuntuを最小構成でインストールすると他に必要になるもの
apt-get install wget git bcc acpica-tools libncurses5-dev libaio-dev libyajl2 libyajl-dev libc6-dev-i386 libelf-dev

最新のIntelのLinuxファームウェアをダウンロードし入れる。

公式

  • WindowsとEFIブート領域は共有、ほかは前述通りに/bootの領域とEncryptLVM領域を設定しKVMでない環境では起動できていたが、KVMGTのインストールでカーネルのバージョンが上がったときに最新のドライバが入ってこず、さらにドライバの署名を厳しく確認されたためかドライバを読み込むところでティアリングが起きて画面が荒れたりフリーズしたりで起動しなくなったため、i915のファームウェアを別途ダウンロードすることにした。(レッツノートSZ6でこの現象が発生、カーネル4.14で発生した。)
git clone https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git
cp ./linux-firmware/i915/*.bin /lib/firmware/i915

Build the initrd (initial ramdisk)

/etc/initramfs-tools/modules にあるモジュールをロードする設定でKVMGTをロードするように記述(以下4行を最後に追記する)

kvmgt
xengt
vfio-iommu-type1
vfio-mdev

Host Environment Setup(Build Kernel Source)

git clone https://github.com/intel/gvt-linux.git
cd gvt-linux
git checkout gvt-stable-4.17
echo ""|make oldconfig

.configをvimなどで開き、以下の項目が次のようになっていることを確認する。なっていなかったら変更する。CONFIG_BLK_DEV_NVMEについてはKVMGTではQEMUでNVME関連のコンパイルがあり、NVMEデバイスが使えるように設定しないとコンパイルに失敗した。

CONFIG_BLK_DEV_NVME=y
CONFIG_DRM_I915_GVT=y
CONFIG_DRM_I915_GVT_KVMGT=m
CONFIG_VFIO_MDEV=m
CONFIG_VFIO_MDEV_DEVICE=m

コンパイル&インストール。

make -j4 && make modules_install && make install

Ubuntuを使っている場合、Debianカーネルパッケージを直接生成してインストールすることができます。(上記でmake installまで行っていれば必須ではない。4.17では実行していない。) Linuxのカーネル構築についてはここを参考にした

make -j4 deb-pkg
cd ..
dpkg -i linux-image-4.17.0+_4.17.0+-2_amd64.deb

以下、GRUB設定の例。

cd /root
vim grub-kvmgt.cfg
cp /etc/grub.d/10_linux /etc/grub.d/09_linux_kvmgt
sed -i "/top_level=true$/a . '/root/grub-kvmgt.cfg'" /etc/grub.d/09_linux_kvmgt
update-grub
reboot

grub-kvmgt.cfgの中身は以下の通り。drm.debug=0を入れないと別のコンソールを開いたときにi915のデバッグログがすごいことになる。

OS='KVMGT'
GRUB_CMDLINE_LINUX='iommu=1 msi=1 intel_iommu=on i915.enable_gvt=1 loglvl=all guest_loglvl=all log_buf_len=128M conring_size=4M kvm.ignore_msrs=1 drm.debug=0'
list='/boot/vmlinuz-4.17.0+'

音声について

オーディオはalsaとpulseaudioを使ったが、劣化が著しいためjackも検討中 参考(Jack)(https://www.linuxquestions.org/questions/linux-software-2/qemu-kvm-alsa-jack-850725/) 参考(PulseAudioとJackの比較)(https://pyocopon.sakura.ne.jp/linuxdtm/?p=83)

sudo apt install alsa pulseaudio
pulseaudio --start	// 先にGUIを起動していた場合

私のレッツノートの場合

バックライトがそのままでは動かないので、/usr/share/X11/xorg.conf.d/20-intel.confの作成。ファイルの中身は次の通り。

Section "Device"
        Identifier      "card0"
        Driver  "intel"
        Option  "Backlight"     "intel_backlight"
        BusID   "PCI:0:2:0"
EndSection

タッチパッドのスクロールをMacと同じように合わせる 以下が参考になった https://blog.zelkova.cc/2017/10/reverse-mouse-scroll-direction-in-xfce.html https://wiki.archlinux.jp/index.php/Libinput 353 xinput list 354 xinput list-props 11 355 xinput set-prop 11 "libinput Natural Scrolling Enabled" 1 356 xinput set-prop 11 "libinput Tapping Enabled" 1

続いて、電源管理ツールとして(レッツノートの「Fn」+「F1」「F2」の組み合わせを動かすため)xfce4-power-manager、タスクマネージャとしてlxtask、仮想マシンのUEFI起動のためにovmfを入れる。(導入後、xfce4-power-manager-settingsで設定画面を開ける)

apt-get install lxtask xfce4-power-manager
apt install ovmf

//////////////////////////////////////// apt-get install lxterminal lxsession lxde-common apt-get install lxappearance // GUIによるアイコン変更などを行いたい場合 startx

////////////////////////////////////////

virglrendererのセットアップ

GVT-gとは直接関係はないが、マシンにより使えるGPUのメモリ容量はマシンにより異なる。もっとGPUの性能を求めるときにvirtio-vga / virtio-gpuを利用しようとした際に必要となる。 virtioのGPUについては次を参照→https://wiki.archlinux.jp/index.php/QEMU#virtio 参考→https://www.collabora.com/news-and-blog/blog/2018/02/12/virtualizing-gpu-access/ 分かっているlibvirtのバグの対処法→http://web.archive.org/web/20171216170334/https://www.kraxel.org/blog/tag/virgl/

git clone git://anongit.freedesktop.org/virglrenderer
cd virglrenderer
./autogen.sh --prefix=/usr
make -j4
make install

Host Environment Setup(Build Qemu)

git clone https://github.com/intel/igvtg-qemu
cd igvtg-qemu
git checkout stable-2.12.0
git submodule update --init roms/seabios
./configure --prefix=/usr \
        --enable-kvm \
        --disable-xen \
        --enable-debug-info \
        --enable-debug \
        --enable-sdl \
        --enable-vhost-net \
        --enable-spice \
        --disable-debug-tcg \
        --enable-opengl \
        --enable-gtk \
	--enable-virglrenderer \
        --target-list=x86_64-softmmu

./configureの結果は例として次のようになる

Install prefix    /usr
BIOS directory    /usr/share/qemu
firmware path     /usr/share/qemu-firmware
binary directory  /usr/bin
library directory /usr/lib
module directory  /usr/lib/qemu
libexec directory /usr/libexec
include directory /usr/include
config directory  /usr/etc
local state directory   /usr/var
Manual directory  /usr/share/man
ELF interp prefix /usr/gnemul/qemu-%M
Source path       /root/igvtg-qemu
GIT binary        git
GIT submodules    ui/keycodemapdb capstone
C compiler        cc
Host C compiler   cc
C++ compiler      c++
Objective-C compiler clang
ARFLAGS           rv
CFLAGS            -g 
QEMU_CFLAGS       -I/usr/include/pixman-1  -Werror -pthread -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -fPIE -DPIE -m64 -mcx16 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common -fwrapv  -Wexpansion-to-defined -Wendif-labels -Wno-shift-negative-value -Wno-missing-include-dirs -Wempty-body -Wnested-externs -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wold-style-declaration -Wold-style-definition -Wtype-limits -fstack-protector-strong -I/usr/include/libpng16 -I/usr/include/libdrm -I/usr/include/spice-server -I/usr/include/spice-1 -I$(SRC_PATH)/capstone/include
LDFLAGS           -Wl,--warn-common -Wl,-z,relro -Wl,-z,now -pie -m64 -g 
make              make
install           install
python            python -B
smbd              /usr/sbin/smbd
module support    no
host CPU          x86_64
host big endian   no
target list       x86_64-softmmu
gprof enabled     no
sparse enabled    no
strip binaries    no
profiler          no
static build      no
SDL support       yes (2.0.8)
Intel GVT-g support yes
GTK support       yes (3.22.30)
GTK GL support    yes
VTE support       no 
TLS priority      NORMAL
GNUTLS support    no
GNUTLS rnd        no
libgcrypt         no
libgcrypt kdf     no
nettle            no 
nettle kdf        no
libtasn1          no
curses support    no
virgl support     no
curl support      no
mingw32 support   no
Audio drivers     oss
Block whitelist (rw) 
Block whitelist (ro) 
VirtFS support    no
Multipath support no
VNC support       yes
VNC SASL support  no
VNC JPEG support  no
VNC PNG support   yes
xen support       no
brlapi support    no
bluez  support    no
Documentation     no
PIE               yes
vde support       no
netmap support    no
Linux AIO support yes
ATTR/XATTR support yes
Install blobs     yes
KVM support       yes
HAX support       no
HVF support       no
WHPX support      no
TCG support       yes
TCG debug enabled no
TCG interpreter   no
malloc trim support yes
RDMA support      no
fdt support       yes
membarrier        no
preadv support    yes
fdatasync         yes
madvise           yes
posix_madvise     yes
posix_memalign    yes
libcap-ng support no
vhost-net support yes
vhost-crypto support yes
vhost-scsi support yes
vhost-vsock support yes
vhost-user support yes
Trace backends    log
spice support     yes (0.12.13/0.14.0)
rbd support       no
xfsctl support    no
smartcard support no
libusb            yes
usb net redir     yes
OpenGL support    yes
OpenGL dmabufs    yes
libiscsi support  no
libnfs support    no
build guest agent yes
QGA VSS support   no
QGA w32 disk info no
QGA MSI support   no
seccomp support   no
coroutine backend ucontext
coroutine pool    yes
debug stack usage no
crypto afalg      no
GlusterFS support no
gcov              gcov
gcov enabled      no
TPM support       yes
libssh2 support   no
TPM passthrough   yes
TPM emulator      yes
QOM debugging     yes
Live block migration yes
lzo support       no
snappy support    no
bzip2 support     no
NUMA host support no
libxml2           no
tcmalloc support  no
jemalloc support  no
avx2 optimization yes
replication support yes
VxHS block device no
capstone          git

コンパイルしてインストールする

make -j4
cd roms/seabios
make -j4
cd -
make install
cp roms/seabios/out/bios.bin /usr/bin/bios.bin

libvirt関連のセットアップ(virt-manager)

virt-manager(Virtual Machine Manager)を使う場合は入れておく。gnome-boxes(GNOME Boxes)を使うこともできる。

apt-get install virt-manager

明るさセンサー関連のセットアップ(Ambient Light Sensor)

/////////バックライト設定検証はまだしていない https://github.com/FedeDP/Clight/ https://github.com/FedeDP/Clightd/ 必要なライブラリなどのインストール apt-get install cmake libsystemd-dev libpopt-dev libgsl-dev libconfig-dev 284 git clone https://github.com/FedeDP/Clight 286 cd ./Clight 289 mkdir build 290 cd build 291 cmake ../ 323 make && make install

325 cd /root 327 git clone https://github.com/FedeDP/Clightd 336 cd ./Clightd 338 mkdir build 339 cd build 340 cmake -DENABLE_DOC=1 -DENABLE_GAMMA=1 -DENABLE_IDLE=1 -DENABLE_DPMS=1 ../ 347 cppcheck --enable=style --enable=performance --enable=unusedFunction /////////ここまで未検証。

アクセス権限の厳格化

UFWの設定方法については以下を参照。このUFWの内部の動作はiptablesが利用されている。 https://linode.com/docs/security/firewalls/configure-firewall-with-ufw/

UFWを利用して、すべてのコネクションの接続をdenyにする ufw default deny incoming ufw default deny outgoing

DNSサーバへのアクセスを許可 ufw allow out on [NAT_interface] from any port 53 proto udp

Webサーバへのアクセスを許可 ufw allow out on [NAT_interface] from any port 80 proto tcp ufw allow out on [NAT_interface] from any port 443 proto tcp

Guest Setup

vfio-pciにてパススルーしたいデバイスがある場合

  • GVTgのデバイス指定後にPCIパススルーするデバイスの情報を記述することで対応可能。逆にするとIOMMU番号が見つからないとエラーが出た。詳しい事情は不明。参考(http://mmi.hatenablog.com/entry/2018/03/27/015015)

ネットワークの設定(従来)

ネットワーク設定を行う(ネットワークインターフェイスに関してはnetworkctl -aなどで確認しておくこと。)

vim /etc/network/interfaces
/etc/init.d/networking restart

/etc/network/interfacesの中身

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug enp0s31f6
iface enp0s31f6 inet manual
auto xenbr0
# If the hostOS is used to DHCP assignment, replace "manual" with "dhcp"
iface xenbr0 inet manual
bridge_ports enp0s31f6

ネットワークの設定(netplanを使う)

ネットワーク設定を行う(ネットワークインターフェイスに関してはnetworkctl -aなどで確認しておくこと。)

cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak
vim /etc/netplan/01-netcfg.yaml
netplan apply

/etc/netplan/01-netcfg.yamlの中身

以下、実行できないとき。

QEMUプロセスを実行するアカウントに対し制限が入っていることがあるので、以下2か所のコメントアウトを外す。 /etc/libvirt/qemu.conf

# user = "root"
user = "root"
# group = "root"
group = "root"

libvirtを使う際にSPICEがipv6が有効になっていることで設定できないことがあるので、以下を編集(nc -6 -l 127.0.0.1 5900とかやってるみたい?) 以下のコメントを解除 /etc/gai.conf

# precedence ::ffff:0:0/96 100
precedence ::ffff:0:0/96 100

実行できないとき ここまで

起動するときはrootで、さらに最後に&をつける

sudo virt-manager &

仮想マシンの起動:コマンド直接

パーティションの作成はEncryptLVMであったのでpvdisplayとlvcreateを使った ボリュームグループの作成 vgcreate ubuntu-vg /dev/sda7 pvdisplay -m ボリュームの作成 lvcreate -L 128G -n win10 ubuntu-vg →削除は「lvremove /dev/ubuntu-vg/win10」

仮想マシンへのdiskに関する記述は以下も参照。 https://xenbits.xen.org/docs/4.3-testing/misc/xl-disk-configuration.txt

仮想ハードディスクを作成する 166 qemu-img-xen create -f qcow2 guest.qcow2 30G

virshでのエラーは以下が参考になった https://bugzilla.redhat.com/show_bug.cgi?id=1624735 https://unix.stackexchange.com/questions/235414/libvirt-how-to-pass-qemu-command-line-args と同時にqemu:commandlineは書かないと消えてしまう

後からaptなどでアプリを入れたり、他にmake installなどでインストールしたプログラムによりGVT-gを動かしたときに「EGL support is disabled.」などと出てきたときにはQEMUをmake installしなおすことで対処ができることがある。

machine typeがサポートされていないなどの理由で起動できないときには以下が参考になった https://askubuntu.com/questions/842459/cannot-open-virtual-machine-after-upgrading-to-16-10