深度操作系统 Deepin V23 安装最新的 linux 内核 #!/usr/bin/env bash ### # Upgrade Linux Kernel # ### ## # 最新代码位于:https://jihulab.com/-/snippets/2310 ## check_apt() { command -v apt > /dev/null 2>&1 } install_deps() { apt -y install libncurses5-dev openssl libssl-dev build-essential openssl pkg-config libc6-dev bison libidn11-dev libidn11 minizip flex libelf-dev # # zlibc } upgrade() { pushd "./linux-${VERSION}" > /dev/null 2>&1 || exit 1 make clean time make mrproper printf ":::::::make mrproper::::::: " cp "/boot/config-${CURRENT_KERNEL}" "./.config" time make menuconfig # Load -> (.config) OK -> SAVE -> (.config) OK -> EXIT # 选择(使用 Tab 键): 选择 Load -> 回车 -> 回车 -> 选择 Exit -> 回车 -> 回车 printf ":::::::make menuconfig:::::::: " time make bzImage -j${CPU_COUNT} printf ":::::::make bzImage:::::::: " time make modules -j${CPU_COUNT} printf ":::::::make module:::::::: " time make INSTALL_MOD_STRIP=1 modules_install printf ":::::::install module:::::::: " time mkinitramfs "/lib/modules/${TARGET_KERNEL}" -o "/boot/initrd.img-${TARGET_KERNEL}" printf ":::::::mkinitramfs kernel:::::::: " cp "arch/x86/boot/bzImage" "/boot/vmlinuz-${TARGET_KERNEL}" cp "System.map" "/boot/System.map-${TARGET_KERNEL}" update-grub2 popd > /dev/null 2>&1 || exit 1 } main() { set -e if ! check_apt; then printf "only apt package manager is supported " exit 1 fi VERSION="${1}" # check kernel version if [[ -z "${VERSION}" ]]; then printf "please enter kernel version " exit 1 fi printf "will install linux kernel %s " "${VERSION}" if [[ ! -d "linux-${VERSION}" ]]; then # download kernel package if [[ ! -f "linux-${VERSION}.tar.gz" ]]; then VER_FIRST=$(echo ${VERSION} | cut -d "." -f 1) wget -O "linux-${VERSION}.tar.gz" "https://mirrors.aliyun.com/linux-kernel/v${VER_FIRST}.x/linux-${VERSION}.tar.gz" printf "download kernel %s from aliyun mirror " "${VERSION}" fi if [[ ! -f "linux-${VERSION}.tar.gz" ]]; then printf "no found kernel package file "linux-%s.tar.gz". " "${VERSION}" exit 1 fi tar -zxf "linux-${VERSION}.tar.gz" fi if [[ ! -d "linux-${VERSION}" ]]; then printf "no found folder "linux-%s". " "${VERSION}" exit 1 fi install_deps || exit 1 CURRENT_KERNEL=$(uname -r) TARGET_KERNEL="${VERSION}-${CURRENT_KERNEL#*-}" printf " Current: %s ==> Target: %s " "${CURRENT_KERNEL}" "${TARGET_KERNEL}" CPU_COUNT=$(cat /proc/cpuinfo| grep "processor"| wc -l) upgrade 2>&1 | tee ./kernel.log } main "$@" || exit 1 脚本已托管至 JihuLab Git 平台:https://jihulab.com/-/snippets/2310 升级 Linux 内核到指定的版本仅支持 apt 包管理工具。 可自行更改并安装相关依赖,以支持别的包管理器需要 root 权限添加可执行权限:chmod +x kernel.sh 执行命令:sudo ./kernel.sh 6.1.1 当前目录下,生成的日志为 kernel.log 脚本分析:// 定义语法为 bash #!/usr/bin/env bash // 判断是否为 apt 包管理器 check_apt() { command -v apt > /dev/null 2>&1 } // 安装依赖库 install_deps() { apt -y install libncurses5-dev openssl libssl-dev build-essential openssl pkg-config libc6-dev bison libidn11-dev libidn11 minizip flex libelf-dev # # zlibc } // 升级内核的函数 upgrade() { // 进入内核源码的文件夹,失败则退出 pushd "./linux-${VERSION}" > /dev/null 2>&1 || exit 1 // 清理上次编译的残留文件 make clean // 在相关命令行前面添加 time,可以统计编译时长 // 删除不必要的文件和目录 time make mrproper printf ":::::::make mrproper::::::: " // 复制当前内核的配置信息到当前目录,并命名为 .config cp "/boot/config-${CURRENT_KERNEL}" "./.config" // 基于文本选单的配置界面,对应的还有 make config 传统的配置方式 time make menuconfig // UI 界面按顺序选择及确定 # Load -> (.config) OK -> Save -> (.config) OK -> Exit # 选择(使用 Tab 键): Load -> 回车 -> 回车 -> 选择 Exit -> 回车 -> 回车 printf ":::::::make menuconfig:::::::: " // 编译内核 -j16 不使用多线程进行加速编译 time make bzImage -j${CPU_COUNT} printf ":::::::make bzImage:::::::: " // 编译模块 time make modules -j${CPU_COUNT} printf ":::::::make module:::::::: " // 安装模块 time make INSTALL_MOD_STRIP=1 modules_install printf ":::::::install module:::::::: " // 打包新内核对应的 .ko 驱动到 initrd.img 文件 time mkinitramfs "/lib/modules/${TARGET_KERNEL}" -o "/boot/initrd.img-${TARGET_KERNEL}" printf ":::::::mkinitramfs kernel:::::::: " // 内核镜像文件 bzImage 和内核符号表文件 System.map 拷贝到/boot/ cp "arch/x86/boot/bzImage" "/boot/vmlinuz-${TARGET_KERNEL}" cp "System.map" "/boot/System.map-${TARGET_KERNEL}" // 更新内核 update-grub2 // 退出当前目录 popd > /dev/null 2>&1 || exit 1 } // 入口函数 main() { // 遇到错误退出脚本 set -e // 判断是否为 apt 包安装器,当前只支持 apt 包管理器 if ! check_apt; then printf "only apt package manager is supported " exit 1 fi // 第一个参数为内核版本号 VERSION="${1}" // 判断版本号的参数是否存在 if [[ -z "${VERSION}" ]]; then printf "please enter kernel version " exit 1 fi printf "will install linux kernel %s " "${VERSION}" // 判断是否已存在内核源码的文件夹(防止上次安装出错后,又重新下载和解压内核源码包) if [[ ! -d "linux-${VERSION}" ]]; then // 若不存在内核源码目录,则判断是否存在内核源码压缩包 if [[ ! -f "linux-${VERSION}.tar.gz" ]]; then // 提取版本的大版本号,供下载地址使用 VER_FIRST=$(echo ${VERSION} | cut -d "." -f 1) // 从阿里云镜像下载源码包 wget -O "linux-${VERSION}.tar.gz" "https://mirrors.aliyun.com/linux-kernel/v${VER_FIRST}.x/linux-${VERSION}.tar.gz" printf "download kernel %s from aliyun mirror " "${VERSION}" fi // 再次判断是否存在源码包。即,上次或上一步下载的源码包是否保存 if [[ ! -f "linux-${VERSION}.tar.gz" ]]; then printf "no found kernel package file "linux-%s.tar.gz". " "${VERSION}" exit 1 fi // 解压源码包 tar -zxf "linux-${VERSION}.tar.gz" fi // 再次判断是否存在内核源码的文件夹 if [[ ! -d "linux-${VERSION}" ]]; then printf "no found folder "linux-%s". " "${VERSION}" exit 1 fi // 安装相关依赖,失败则退出 install_deps || exit 1 // 获取当前内核名称 CURRENT_KERNEL=$(uname -r) // 将升级的目标内核名称 TARGET_KERNEL="${VERSION}-${CURRENT_KERNEL#*-}" printf " Current: %s ==> Target: %s " "${CURRENT_KERNEL}" "${TARGET_KERNEL}" // 获取逻辑 CPU 个数,以便编译加速 CPU_COUNT=$(cat /proc/cpuinfo| grep "processor"| wc -l) // 执行升级,并将升级的日志保存到 kernel.log upgrade 2>&1 | tee ./kernel.log } // 调用入口函数,$@ 传入所有参数,若出错则退出 main "$@" || exit 1