Skip to content

Commit 2a75cbb

Browse files
committed
armbian-update: add kernel rescue method(-r)
1 parent 829e332 commit 2a75cbb

File tree

3 files changed

+96
-15
lines changed

3 files changed

+96
-15
lines changed

README.cn.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ armbian-update
6767
| -k | auto latest | [内核名称](https://github.com/ophub/kernel/tree/main/pub/stable) | 设置更新内核名称 |
6868
| -v | stable | stable/rk3588/dev | 指定内核版本分支 |
6969
| -m | no | yes/no | 使用主线 u-boot |
70+
| -r | "" | "" | [救援专用] 使用 USB 中的系统内核更新 eMMC |
7071

7172
举例: `armbian-update -k 5.15.50 -v dev -m yes`
7273

7374
如果当前目录下有成套的内核文件,将使用当前目录的内核进行更新(更新需要的 4 个内核文件是 `header-xxx.tar.gz`, `boot-xxx.tar.gz`, `dtb-amlogic-xxx.tar.gz`, `modules-xxx.tar.gz`。其他内核文件不需要,如果同时存在也不影响更新,系统可以准确识别需要的内核文件)。如果当前目录没有内核文件,将从服务器查询并下载同系列的最新内核进行更新。在设备支持的可选内核里可以自由更新,如从 5.10.125 内核更新为 5.15.50 内核。
7475

76+
因特殊原因导致的更新不完整等问题,造成系统无法从 eMMC 启动时,可以从 USB 中启动任意内核版本的 Armbian 系统,运行 `armbian-update -r` 命令可以把 USB 中的系统内核更新至 eMMC 中,实现救援的目的。
77+
7578
- ### 安装常用软件
7679

7780
登录 Armbian 系统 → 输入命令:

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ armbian-update
6767
| -k | auto latest | [kernel name](https://github.com/ophub/kernel/tree/main/pub/stable) | Set the kernel name |
6868
| -v | stable | stable/rk3588/dev | Set the kernel version branch |
6969
| -m | no | yes/no | Use Mainline u-boot |
70+
| -r | "" | "" | [Rescue]. Update eMMC using the system kernel in USB |
7071

7172
Example: `armbian-update -k 5.15.50 -v dev -m yes`
7273

7374
If there is a set of kernel files in the current directory, it will be updated with the kernel in the current directory (The 4 kernel files required for the update are `header-xxx.tar.gz`, `boot-xxx.tar.gz`, `dtb-amlogic-xxx.tar.gz`, `modules-xxx.tar.gz`. Other kernel files are not required. If they exist at the same time, it will not affect the update. The system can accurately identify the required kernel files). If there is no kernel file in the current directory, it will query and download the latest kernel of the same series from the server for update. The optional kernel supported by the device can be freely updated, such as from 5.10.125 kernel to 5.15.50 kernel.
7475

76+
When the system cannot be started from eMMC due to incomplete updates and other problems caused by special reasons, you can start any kernel version of the Armian system from USB, and run the `armbian-update -r` command to update the system kernel in USB to eMMC to achieve the purpose of rescue.
77+
7578
- ### Install common software
7679

7780
Login in to armbian → input command:

build-armbian/armbian-files/common-files/usr/sbin/armbian-update

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
# Optional parameter meaning: -k = set the kernel name
1919
# -v = set the kernel version branch
2020
# -m = ues mainline_u-boot
21+
# -r = Rescue the kernel
2122
#
2223
# Command optional parameters: armbian-update -k 5.15.50 -v stable/dev -m yes/no
2324
# Set the kernel name command: armbian-update -k 5.15.50
2425
# Set the kernel branch command: armbian-update -v dev
25-
# Use mainline u-boot command: armbian-update -m yes
26+
# Use mainline u-boot command: armbian-update -m yes/no
27+
# Rescue the Armbian kernel: armbian-update -r
2628
#
2729
#========================================= Functions list =========================================
2830
#
2931
# error_msg : Output error message
3032
# check_depends : Check dependencies
3133
# get_textoffset : Get kernel TEXT_OFFSET
3234
# init_var : Initialize all variables
35+
# rescue_kernel : Rescue the kernel
3336
# download_kernel : Download the kernel
3437
# check_kernel : Check kernel files list
3538
# update_kernel : Update the kernel
@@ -39,6 +42,8 @@
3942
#
4043
# Set current path
4144
current_path="${PWD}"
45+
# Set the kernel backup directory
46+
backup_path="/backup/kernel"
4247
# Set the release check file
4348
ophub_release_file="/etc/ophub-release"
4449
# Set the kernel download repository
@@ -203,6 +208,78 @@ init_var() {
203208
sync && echo ""
204209
}
205210

211+
# Rescue the kernel
212+
rescue_kernel() {
213+
echo -e "${STEPS} Start restoring kernel files..."
214+
215+
# Check the current system running disk
216+
root_devname="$(df / | tail -n1 | awk '{print $1}' | awk -F '/' '{print substr($3, 1, length($3)-2)}')"
217+
if lsblk -l | grep -E "^${root_devname}boot0" >/dev/null; then
218+
error_msg "You are running in eMMC mode, please boot system with usb or tf card!"
219+
fi
220+
221+
# Find emmc disk, first find emmc containing boot0 partition
222+
box_emmc="$(lsblk -l -o NAME | grep -oE '(mmcblk[0-9]?boot0)' | sed "s/boot0//g")"
223+
# Find emmc disk, find emmc that does not contain the boot0 partition
224+
[[ -z "${box_emmc}" ]] && box_emmc="$(lsblk -l -o NAME | grep -oE '(mmcblk[0-9]?)' | grep -vE ^${root_devname} | uniq)"
225+
# Check if emmc exists
226+
[[ -z "${box_emmc}" ]] && error_msg "The eMMC storage not found in this device!"
227+
# Location of emmc
228+
restore_emmc="/dev/${box_emmc}"
229+
echo -e "${INFO} The device eMMC name: [ ${restore_emmc} ]"
230+
231+
# Check if there is enough free space
232+
available_space="$(df -Tk / | grep '/dev/' | awk '{print $5}' | echo $(($(xargs) / 1024 / 1024)))"
233+
if [[ -z "$(echo "${available_space}" | sed -n "/^[0-9]\+$/p")" ]]; then
234+
error_msg "The remaining space cannot be obtained."
235+
fi
236+
if [[ "${available_space}" -lt "3" ]]; then
237+
error_msg "The remaining space is [ ${available_space}GiB ], less than 3GiB, please use [ armbian-tf ] to expand the USB."
238+
fi
239+
240+
# Create a temporary mount directory
241+
rm -rf ${backup_path}/*
242+
mkdir -p ${backup_path}/{bootfs/,rootfs/}
243+
244+
# Mount eMMC to USB
245+
mount ${restore_emmc}p1 ${backup_path}/bootfs
246+
[[ "${?}" -eq "0" ]] || error_msg "mount ${restore_emmc}p1 failed!"
247+
mount ${restore_emmc}p2 ${backup_path}/rootfs
248+
[[ "${?}" -eq "0" ]] || error_msg "mount ${restore_emmc}p2 failed!"
249+
250+
# Identify the current kernel files
251+
kernel_signature="$(uname -r)"
252+
253+
# 01. For /boot files
254+
cd ${backup_path}/bootfs
255+
rm -rf config-* initrd.img-* System.map-* vmlinuz-* uInitrd* *Image dtb*
256+
[[ "${PLATFORM}" == "amlogic" ]] && cp -rf /boot/{u-boot.ext,u-boot.emmc} -t . 2>/dev/null
257+
cp -rf /boot/{*-${kernel_signature},uInitrd,*Image,dtb} -t .
258+
[[ "$?" -ne "0" ]] && error_msg "(1/3) [ boot ] files recovery failed."
259+
echo -e "${INFO} (1/3) [ boot ] files restored successful."
260+
261+
# 02. For /usr/lib/modules/${kernel_signature}
262+
cd ${backup_path}/rootfs/usr/lib/modules
263+
rm -rf *
264+
cp -rf /usr/lib/modules/${kernel_signature} -t .
265+
[[ "$?" -ne "0" ]] && error_msg "(2/3) [ modules ] files recovery failed."
266+
echo -e "${INFO} (2/3) [ modules ] files restored successful."
267+
268+
# 03. For /usr/src/linux-headers-${kernel_signature}
269+
cd ${backup_path}/rootfs/usr/src
270+
rm -rf linux-headers-*
271+
cp -rf /usr/src/linux-headers-${kernel_signature} -t .
272+
[[ "$?" -ne "0" ]] && error_msg "(3/3) [ headers ] files recovery failed."
273+
echo -e "${INFO} (3/3) [ headers ] files restored successful."
274+
275+
# Unmount the emmc partition
276+
cd ${backup_path}
277+
umount -f ${backup_path}/bootfs
278+
umount -f ${backup_path}/rootfs
279+
280+
sync && echo ""
281+
}
282+
206283
# Download the kernel
207284
download_kernel() {
208285
cd ${current_path}
@@ -384,20 +461,18 @@ update_uboot() {
384461
# Check script permission
385462
[[ "$(id -u)" == "0" ]] || error_msg "Please run this script as root: [ sudo $0 ]"
386463
echo -e "${STEPS} Welcome to the kernel update tool."
387-
#
388-
# Check dependencies
389-
check_depends
390-
# Initialize all variables
391-
init_var "${@}"
392-
# Download the kernel
393-
download_kernel
394-
# Check kernel files list
395-
check_kernel
396-
# Update the kernel
397-
update_kernel
398-
# Update the uboot for Amlogic boxes
399-
[[ "${PLATFORM}" == "amlogic" ]] && update_uboot
400-
#
464+
465+
if [[ "${1}" == "-r" ]]; then
466+
rescue_kernel
467+
else
468+
check_depends
469+
init_var "${@}"
470+
download_kernel
471+
check_kernel
472+
update_kernel
473+
[[ "${PLATFORM}" == "amlogic" ]] && update_uboot
474+
fi
475+
401476
sync && sleep 3
402477
echo -e "${SUCCESS} Successfully updated, automatic restarting..."
403478
reboot

0 commit comments

Comments
 (0)