2 # Enable WC in VFIO-PCI driver
4 # * Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-0bb3fad3c0286ebd5
5 # * Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type - ami-015232c01a82b847b
6 # * Red Hat Enterprise Linux 8 (HVM), SSD Volume Type - ami-08f4717d06813bf00
7 # * Ubuntu Server 20.04 LTS (HVM), SSD Volume Type - ami-06fd8a495a537da8b
8 # * Ubuntu Server 18.04 LTS (HVM), SSD Volume Type - ami-0823c236601fef765
14 # Kernel modules location:
15 P1="/usr/lib/modules/`uname -r`/kernel/drivers/vfio"
16 P2="/lib/modules/`uname -r`/kernel/drivers/vfio"
18 # This may return an error if executed from inside the script
21 GREEN="$(tput setaf 2)"
28 echo -e "${BOLD}${@}${NORMAL}"
32 bold "${RED}ERROR: ${@}"
39 function get_kernel_version {
40 local ver=$(uname -r | cut -f 1 -d '-')
41 local ver_major=$(echo $ver | cut -f1 -d '.')
42 local ver_minor=$(echo $ver | cut -f2 -d '.')
43 local ver_subminor=$(echo $ver | cut -f3 -d '.')
45 printf "%d%02d%04d" "${ver_major}" "${ver_minor}" "${ver_subminor}"
48 function download_kernel_src_yum {
49 echo "Use yum to get the kernel sources"
51 bold "\nInstall required applications and kernel headers"
52 yum install -y gcc "kernel-$(uname -r)" "kernel-devel-$(uname -r)" \
53 git make elfutils-libelf-devel patch yum-utils
56 # Download kernel source
57 bold "\nDownload kernel source with vfio"
58 yumdownloader --source "kernel-devel-$(uname -r)"
59 rpm2cpio kernel*.src.rpm | cpio -idmv
64 rm -f linux-*.tar* linux-*.patch
67 function download_kernel_src_apt {
68 echo "Use apt-get to get the kernel sources"
72 bold "\nInstall required applications"
73 apt-get -q -y install dpkg-dev build-essential git
76 bold "\nDownload Linux kernel source with vfio"
77 if ! apt-get -q -y source -t focal linux-image-$(uname -r); then
78 err "Cannot download Linux kernel source.\nPlease uncomment appropriate 'deb-src' line in the /etc/apt/sources.list file"
83 rm -f linux-*.dsc linux-*.gz
86 function download_kernel_src {
87 bold "[1] Downloading prerequisites..."
92 if apt-get -v >/dev/null 2>/dev/null; then
93 download_kernel_src_apt
95 download_kernel_src_yum
100 function apply_wc_patch {
101 echo "Using patch for kernel version 4.10"
102 local wc_patch="${BASE_PATH}/patches/linux-4.10-vfio-wc.patch"
104 if ! patch --ignore-whitespace -p1 < "${wc_patch}"; then
105 err "Cannot apply patch: ${wc_patch}!"
110 function compile_vfio_driver {
111 bold "\n[2] Patch and build the vfio driver"
112 # Adjust VFIO-PCI driver
114 bold "Apply patch for the write combining to the vfio-pci"
119 # Configure Makefile - build VFIO with support for NOIOMMU mode
120 bold "\nConfigure Makefile for standalone vfio build and noiommu mode support"
121 echo "ccflags-y := -DCONFIG_VFIO_NOIOMMU=1" >> Makefile
122 echo 'all:' >> Makefile
123 echo ' make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules' >> Makefile
126 bold "\nBuild the driver"
128 err "Compilation error."
134 function get_module_location {
135 for p in ${P1} ${P2}; do
136 if find "${p}" -name "vfio.*" >/dev/null 2>/dev/null; then
142 if [ -z "${MOD_PATH}" ]; then
143 err "Cannot find kernel modules location..."
148 function get_module_compression {
149 if ls "${MOD_PATH}/vfio.ko.xz" >/dev/null 2>/dev/null; then
156 function replace_module {
159 bold "\n[3] Install module"
161 get_module_compression
163 for name in "pci/vfio-pci.ko" "pci/vfio-pci-core.ko" "vfio.ko"; do
164 if test -e "${MOD_PATH}/${name}${XZ}"; then
165 if [ -n "${XZ}" ]; then
166 xz "${name}" -c > "${name}${XZ}"
168 mv "${MOD_PATH}/${name}${XZ}" "${MOD_PATH}/${name}${XZ}_no_wc"
169 cp "${name}${XZ}" "${MOD_PATH}/${name}${XZ}"
170 bold "Installing: ${MOD_PATH}/${name}${XZ}"
174 if [ "${installed}" -eq 1 ]; then
175 green "Module installed at: ${MOD_PATH}"
177 err "Failure during vfio-pci module installation. Prehaps it's not provided as a kernel module!"
182 ###############################################
184 ###############################################
186 if [ "$(id -u)" -ne 0 ]; then
187 err 'Please execute script as a root'
194 KERNEL_VERSION=$(get_kernel_version)
196 if [ "${KERNEL_VERSION}" -lt 4100000 ]; then
197 err "Kernel version: $(uname -r) is not supported by the script. Please upgrade kernel to at least v4.10."