网站首页 > 技术教程 正文
深度操作系统 Deepin V23 安装最新的 linux 内核
#!/usr/bin/env bash
###
# Upgrade Linux Kernel
#
# Author: Jetsung Chan <jetsungchan@gmail.com>
###
##
# 最新代码位于: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
}
pre_upgrade() {
time make mrproper
printf ":::::::make mrproper:::::::\n\n"
CONFIG="/boot/config-${CURRENT_KERNEL}"
if [[ ! -f "${CONFIG}" ]]; then
CONFIG=$(find /boot/config* | awk '{print $1}')
fi
if [[ ! -f "${CONFIG}" ]]; then
printf "No configuration file found: %s\n" "${CONFIG}"
exit 1
fi
cp "${CONFIG}" "./.config"
# 经过多次验证。此命令需要在终端手动输入才可以选择按键选择。
#
# make menuconfig
#
# (4 次 Tab 键) -> Load(回车) -> OK(回车) -> (1 次 Tab 键) -> Exit(回车) -> Yes(回车)
printf ":::::::::::::::::::::::::::::::::::::::::::::
Copy the following script and execute it:
\e[1;33m%s\e[0m
\e[1;33m%s\e[0m
\e[1;33m%s\e[0m
\e[1;33m%s\e[0m
" "cd ./linux-${VERSION}" "make menuconfig" "cd ../" "${0} ${1}"
}
do_upgrade() {
pushd "./linux-${VERSION}" > /dev/null 2>&1 || exit 1
if [[ ! -f "./.config" ]]; then
pre_upgrade "$@"
exit
fi
CPU_COUNT=$(cat /proc/cpuinfo| grep "processor"| wc -l)
time make bzImage -j${CPU_COUNT}
printf ":::::::make bzImage::::::::\n\n"
time make modules -j${CPU_COUNT}
printf ":::::::make module::::::::\n\n"
time make INSTALL_MOD_STRIP=1 modules_install
printf ":::::::install module::::::::\n\n"
time mkinitramfs "/lib/modules/${TARGET_KERNEL}" -o "/boot/initrd.img-${TARGET_KERNEL}"
printf ":::::::mkinitramfs kernel::::::::\n\n"
cp "./.config" "/boot/config-${TARGET_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\n"
exit 1
fi
VERSION="${1}"
# check kernel version
if [[ -z "${VERSION}" ]]; then
printf "Please enter the kernel version\n"
exit 1
fi
printf "Kernel %s will be installed\n\n" "${VERSION}"
if [[ ! -d "linux-${VERSION}" ]]; then
# download kernel package
if [[ ! -f "linux-${VERSION}.tar.gz" ]]; then
DOWN_URL="${2}"
if [[ -z "${DOWN_URL}" ]]; then
VER_FIRST=$(echo ${VERSION} | cut -d '.' -f 1)
DOWN_URL="https://mirrors.aliyun.com/linux-kernel/v${VER_FIRST}.x/linux-${VERSION}.tar.gz"
fi
wget -O "linux-${VERSION}.tar.gz" "${DOWN_URL}"
printf "Download kernel %s from %s\n\n" "${VERSION}"
fi
if [[ ! -f "linux-${VERSION}.tar.gz" ]]; then
printf "No 'linux-%s.tar.gz' file found\n" "${VERSION}"
exit 1
fi
tar -zxf "linux-${VERSION}.tar.gz"
fi
if [[ ! -d "linux-${VERSION}" ]]; then
printf "The 'linux-%s' folder was not found\n" "${VERSION}"
exit 1
fi
install_deps || exit 1
CURRENT_KERNEL=$(uname -r)
TARGET_KERNEL="${VERSION}-${CURRENT_KERNEL#*-}"
printf "\nCurrent: %s ==> Target: %s\n\n" "${CURRENT_KERNEL}" "${TARGET_KERNEL}"
do_upgrade "$@" 2>&1 | tee ./kernel.log
}
main "$@" || exit 1
脚本已托管至 JihuLab Git 平台:
https://jihulab.com/-/snippets/2310
升级 Linux 内核到指定的版本
1. 仅支持 `apt` 包管理工具
可自行更改并安装相关依赖,以支持别的包管理器
2. 需要 `root` 权限,仅在 `Deepin V23` 下测试过
3. 添加可执行权限:`chmod +x ./kernel.sh`
4. 执行命令:`./kernel.sh 6.1.1` ,复制显示的黄色命令行,粘贴到终端执行
可自行使用指定的 URL:
./kernel.sh 6.1.1 https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.1.1.tar.xz
5. 当前目录下,生成的日志为 `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:::::::\n\n"
// 复制当前内核的配置信息到当前目录,并命名为 .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::::::::\n\n"
// 编译内核 -j16 使用多线程进行加速编译
time make bzImage -j${CPU_COUNT}
printf ":::::::make bzImage::::::::\n\n"
// 编译模块
time make modules -j${CPU_COUNT}
printf ":::::::make module::::::::\n\n"
// 安装模块
time make INSTALL_MOD_STRIP=1 modules_install
printf ":::::::install module::::::::\n\n"
// 打包新内核对应的 .ko 驱动到 initrd.img 文件
time mkinitramfs "/lib/modules/${TARGET_KERNEL}" -o "/boot/initrd.img-${TARGET_KERNEL}"
printf ":::::::mkinitramfs kernel::::::::\n\n"
// 内核镜像文件 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\n"
exit 1
fi
// 第一个参数为内核版本号
VERSION="${1}"
// 判断版本号的参数是否存在
if [[ -z "${VERSION}" ]]; then
printf "please enter kernel version\n"
exit 1
fi
printf "will install linux kernel %s\n\n" "${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\n\n" "${VERSION}"
fi
// 再次判断是否存在源码包。即,上次或上一步下载的源码包是否保存
if [[ ! -f "linux-${VERSION}.tar.gz" ]]; then
printf "no found kernel package file 'linux-%s.tar.gz'.\n" "${VERSION}"
exit 1
fi
// 解压源码包
tar -zxf "linux-${VERSION}.tar.gz"
fi
// 再次判断是否存在内核源码的文件夹
if [[ ! -d "linux-${VERSION}" ]]; then
printf "no found folder 'linux-%s'.\n" "${VERSION}"
exit 1
fi
// 安装相关依赖,失败则退出
install_deps || exit 1
// 获取当前内核名称
CURRENT_KERNEL=$(uname -r)
// 将升级的目标内核名称
TARGET_KERNEL="${VERSION}-${CURRENT_KERNEL#*-}"
printf "\nCurrent: %s ==> Target: %s\n\n" "${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
猜你喜欢
- 2025-05-03 通俗易懂:把linux驱动编译进内核(linux驱动编译命令)
- 2025-05-03 Linux之父:Linux内核5.8是“我们有史以来最大的发行版之一”
- 2025-05-03 TCP/IP协议栈在Linux内核中的运行时序分析
- 2025-05-03 Linux内核源码分析(linux内核源码谁会看)
- 2025-05-03 多年一直用Linux内核,微软也内疚
- 2025-05-03 深入了解Linux的虚拟内存管理「嵌入式工程师」
- 2025-05-03 从 0 到 1:如何用 eBPF 深入监控 Linux 内核?
- 2025-05-03 「技术干货」一文搞懂怎么使用Linux内核模块
- 2025-05-03 详解Linux内核源码体系结构与内核结构(图解)
- 2025-05-03 如何阅读Linux内核源码?Linux内存管理中SLAB分配器(源码分析)
你 发表评论:
欢迎- 最近发表
-
- linux日志文件的管理、备份及日志服务器的搭建
- Linux下挂载windows的共享目录操作方法
- Linux系统中的备份文件命令(linux系统中的备份文件命令有哪些)
- 麒麟KYLINOS|通过不同方法设置用户访问文件及目录权限
- 「Linux笔记」系统目录结构(linux目录的结构及含义)
- linux中修改归属权chown命令和chgrp命令
- 工作日报 2021.10.27 Android-SEAndroid权限问题指南
- Windows和Linux环境下,修改Ollama的模型默认保存路径
- 如何强制用户在 Linux 上下次登录时更改密码?
- 如何删除Linux文件夹中除某些扩展名之外的所有文件?
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)