1.编译前的准备(该部分操作在非实时核下)
a.安装依赖项
sudo apt-get install build-essential bc curl ca-certificates gnupg2 libssl-dev lsb-release libelf-dev bison flex dwarves zstd libncurses-dev
b.下载实时核文件
1.查看非实时核版本
2.在https://www.kernel.org/pub/linux/kernel下载对应实时核版本文件(非实时核是6.8.0 对应实时核也是6.8.0) ,在/home目录下创建一个文件夹,存放下载的4个实时核文件
curl -SLO https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.14.12.tar.xz
curl -SLO https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.14.12.tar.sign
curl -SLO https://www.kernel.org/pub/linux/kernel/projects/rt/4.14/older/patch-4.14.12-rt10.patch.xz
curl -SLO https://www.kernel.org/pub/linux/kernel/projects/rt/4.14/older/patch-4.14.12-rt10.patch.sign
3.解压下载的实时核文件
xz -d *.xz
c.验证文件完整性
gpg2 --verify linux-*.tar.sign
gpg2 --verify patch-*.patch.sign
运行上述命令后出现以下问题;
使用以下命令获取密钥:
gpg2 --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys XXXXXXXXXXXXXXXXX
2.编译内核
一旦确定文件下载正确,就可以提取源代码并应用补丁:
tar xf linux-*.tar
cd linux-*/
patch -p1 < ../patch-*.patch
接下来复制当前启动的内核配置作为新实时内核的默认配置:
cp -v /boot/config-$(uname -r) .config
接下来可以使用此配置作为默认配置来配置构建:
make olddefconfig
make menuconfig
编译内核:
make -j$(nproc) deb-pkg
提示:如果编译结束后,给内核打 deb 格式包的时候报错:recipe for traget 'deb-pkg' failed.
则去到现在编译这个目录下 ctrl+h
显示隐藏文件,找到并且修改 .config
文件,把 CONFIG_MODULE_SIG_ALL、CONFIG_MODULE_SIG_KEY、CONFIG_SYSTEM_TRUSTED_KEYS 三项注释掉,编译时系统会自动生成一次性密钥来加密,把 CONFIG_DEBUG_INFO=y 去掉
sudo dpkg -i ../linux-headers-*.deb ../linux-image-*.deb
3.安装NVIDIA驱动
1.在非实时核下查看推荐安装的驱动版本
ubuntu-drivers devices
2.在非实时核下安装驱动,电脑的推荐版本是第一个,安装推荐版本驱动
sudo apt-get install nvidia-driver-550
验证是否安装成功:
nvidia-smi
3.下载驱动对应的.run文件及建立install_nvidia.sh文件
上一步的驱动版本为550,所以需要下载相应的.run文件NVIDIA-Linux-x86_64-550.120.01.run,大家可以查看各自对应的驱动版本,下载的地址为nvidia显卡驱动下载地址,下载第一个,下载完成放置在家目录~即可
在/home目录~下创建install_nvidia.sh文件
touch install_nvidia.sh
将下面脚本代码拷贝到install_nvidia.sh
中,并按照提示修改变量“NV_FILE”,完成后保存即可。
#!/bin/bashBUILD_BASE=`pwd`
NV_FILE="NVIDIA-Linux-x86_64-550.120.01.run" # 这里改成大家自己下载的.run文件名(上面安装过的)
#NV_URL="https://us.download.nvidia.cn/XFree86/Linux-x86_64/430.50/${NV_FILE}" # 之前已经下好了显卡驱动.run文件,就不用从网上下载了,直接注释掉,而且如果要下载的话,则慢
NEED_TO_COMPILE_NV_KO=1function clean_env() {[ -d ./${NV_DIR} ] && rm -rf ./${NV_DIR}
}function check_env() {# check if in rt kerneluname -r | grep rt 1>/dev/null 2>&1 if [ $? -ne 0 ]thenecho "Not in rt kernel, Please install apollo kernel and reboot machine first."exit 2fi# check if nv ko already in kernelif [ ! -f /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko ]thenexport NEED_TO_COMPILE_NV_KO=1fi
}function prepare_nv() {## download nv install file from nvidia home page #if [ ! -f ./${NV_FILE} ]#then# echo "Downloading ${NV_FILE} from nvidia website..."# wget ${NV_URL} -O ${NV_FILE}# if [ $? -ne 0 ]# then# echo "Downloading ${NV_FILE} failed, please check your network connection!"# rm -rf ./${NV_FILE}# exit 1# fi#fi###########上面是下载驱动的代码,我们已经提前下载好了,就不需要这段代码了,直接注释掉############# +x chmod +x ./${NV_FILE}echo "Extracting nvidia install run file..."./${NV_FILE} -x 1>/dev/null 2>&1NV_DIR="`echo ${NV_FILE} | awk -F '.run' '{print $1}'`"NV_VERSION="`echo ${NV_FILE} | awk -F '-' '{print $4}' | awk -F '.run' '{print $1}'`"export NV_DIRexport NV_VERSIONexport NVIDIA_SOURCE="${NV_DIR}/kernel"
}function install_lib() {NV_LIB_OUTPUT_PATH="/usr/lib/x86_64-linux-gnu/"NV_BIN_OUTPUT_PATH="/usr/bin/"[ -f ./${NV_DIR}/libnvidia-ml.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libnvidia-ml.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}[ -f ./${NV_DIR}/libnvidia-fatbinaryloader.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libnvidia-fatbinaryloader.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}[ -f ./${NV_DIR}/libnvidia-ptxjitcompiler.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libnvidia-ptxjitcompiler.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}[ -f ./${NV_DIR}/libcuda.so.${NV_VERSION} ] && /bin/cp -f ./${NV_DIR}/libcuda.so.${NV_VERSION} ${NV_LIB_OUTPUT_PATH}[ -f ./${NV_DIR}/nvidia-modprobe ] && /bin/cp -f ./${NV_DIR}/nvidia-modprobe ${NV_BIN_OUTPUT_PATH}[ -f ./${NV_DIR}/nvidia-smi ] && /bin/cp -f ./${NV_DIR}/nvidia-smi ${NV_BIN_OUTPUT_PATH}chmod +x /usr/bin/nvidia*chmod +s /usr/bin/nvidia-modprobe# link for nvidia/bin/rm -rf /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1 /usr/lib/x86_64-linux-gnu/libnvidia-ml.so/bin/ln -s /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.${NV_VERSION} /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1/bin/ln -s /usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1 /usr/lib/x86_64-linux-gnu/libnvidia-ml.so/bin/rm -rf /usr/lib/x86_64-linux-gnu/libcuda.so /usr/lib/x86_64-linux-gnu/libcuda.so.1/bin/ln -s /usr/lib/x86_64-linux-gnu/libcuda.so.${NV_VERSION} /usr/lib/x86_64-linux-gnu/libcuda.so.1/bin/ln -s /usr/lib/x86_64-linux-gnu/libcuda.so.1 /usr/lib/x86_64-linux-gnu/libcuda.so# take effect/sbin/ldconfig 1>/dev/null 2>&1
}function build_nv() {if [ ${NEED_TO_COMPILE_NV_KO} == 0 ]thenreturnfiNVIDIA_MOD_REL_PATH='kernel/drivers/video'NVIDIA_OUTPUT_PATH="/lib/modules/`uname -r`/${NVIDIA_MOD_REL_PATH}"CPUNUM=`cat /proc/cpuinfo | grep processor | wc | awk -F " " '{print $1}'`export IGNORE_PREEMPT_RT_PRESENCE=truecd ${NVIDIA_SOURCE} && make -j ${CPUNUM} modulecd ${BUILD_BASE}unset IGNORE_PREEMPT_RT_PRESENCEmkdir -p ${NVIDIA_OUTPUT_PATH}[ -f ${NVIDIA_SOURCE}/nvidia.ko ] && cp ${NVIDIA_SOURCE}/nvidia.ko ${NVIDIA_OUTPUT_PATH}[ -f ${NVIDIA_SOURCE}/nvidia-modeset.ko ] && cp ${NVIDIA_SOURCE}/nvidia-modeset.ko ${NVIDIA_OUTPUT_PATH}[ -f ${NVIDIA_SOURCE}/nvidia-drm.ko ] && cp ${NVIDIA_SOURCE}/nvidia-drm.ko ${NVIDIA_OUTPUT_PATH}[ -f ${NVIDIA_SOURCE}/nvidia-uvm.ko ] && cp ${NVIDIA_SOURCE}/nvidia-uvm.ko ${NVIDIA_OUTPUT_PATH}depmod -a
}# check environment
check_env# prepare for nvidia
prepare_nv# build nvidia.ko
build_nv# install user lib
install_lib# clean environment
clean_envecho "Done to install nvidia kernel driver and user libraries."
4.实时内核安装显卡驱动
为install_nvidia.sh添加可执行权限并运行
cd ~
chmod +x ./install_nvidia.sh
sudo ./install_nvidia.sh
安装完成后检查是否安装成功