网站首页 > 技术教程 正文
深度操作系统 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
- 上一篇: 10.开发板与 Windows 共享文件 openwrt入门经典教程
- 下一篇:已经是最后一篇了
猜你喜欢
- 2025-07-06 10.开发板与 Windows 共享文件 openwrt入门经典教程
- 2025-07-06 技术帖 | 飞凌嵌入式T113-i开发板的休眠及唤醒操作
- 2025-07-06 Linux文件系统制作与裁剪(linux 文件切割)
- 2025-07-06 Linux系统的移植和裁剪(linux移植lvgl)
- 2025-07-06 瑞芯微RK3568J如何“调节主频”,实现功耗降低?一文教会您!
- 2025-07-06 16.应用程序软件包测试 openwrt入门经典教程
- 2025-07-06 「技术干货」一文搞懂怎么使用Linux内核模块
- 2025-07-06 ELF 1技术贴|如何支持exFAT和NTFS格式
- 2025-07-06 笔记-利用本地配置文件Actions云编译Openwrt
- 2025-07-06 busybox文件系统制作步骤,带你快速学习
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- sd分区 (65)
- raid5数据恢复 (81)
- 地址转换 (73)
- 手机存储卡根目录 (55)
- tcp端口 (74)
- project server (59)
- 双击ctrl (55)
- 鼠标 单击变双击 (67)
- debugview (59)
- 字符动画 (65)
- flushdns (57)
- ps复制快捷键 (57)
- 清除系统垃圾代码 (58)
- web服务器的架设 (67)
- 16进制转换 (69)
- xclient (55)
- ps源文件 (67)
- filezilla server (59)
- 句柄无效 (56)
- word页眉页脚设置 (59)
- ansys实例 (56)
- 6 1 3固件 (59)
- sqlserver2000挂起 (59)
- vm虚拟主机 (55)
- config (61)
本文暂时没有评论,来添加一个吧(●'◡'●)