|
18 | 18 | # Optional parameter meaning: -k = set the kernel name
|
19 | 19 | # -v = set the kernel version branch
|
20 | 20 | # -m = ues mainline_u-boot
|
| 21 | +# -r = Rescue the kernel |
21 | 22 | #
|
22 | 23 | # Command optional parameters: armbian-update -k 5.15.50 -v stable/dev -m yes/no
|
23 | 24 | # Set the kernel name command: armbian-update -k 5.15.50
|
24 | 25 | # 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 |
26 | 28 | #
|
27 | 29 | #========================================= Functions list =========================================
|
28 | 30 | #
|
29 | 31 | # error_msg : Output error message
|
30 | 32 | # check_depends : Check dependencies
|
31 | 33 | # get_textoffset : Get kernel TEXT_OFFSET
|
32 | 34 | # init_var : Initialize all variables
|
| 35 | +# rescue_kernel : Rescue the kernel |
33 | 36 | # download_kernel : Download the kernel
|
34 | 37 | # check_kernel : Check kernel files list
|
35 | 38 | # update_kernel : Update the kernel
|
|
39 | 42 | #
|
40 | 43 | # Set current path
|
41 | 44 | current_path="${PWD}"
|
| 45 | +# Set the kernel backup directory |
| 46 | +backup_path="/backup/kernel" |
42 | 47 | # Set the release check file
|
43 | 48 | ophub_release_file="/etc/ophub-release"
|
44 | 49 | # Set the kernel download repository
|
@@ -203,6 +208,78 @@ init_var() {
|
203 | 208 | sync && echo ""
|
204 | 209 | }
|
205 | 210 |
|
| 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 | + |
206 | 283 | # Download the kernel
|
207 | 284 | download_kernel() {
|
208 | 285 | cd ${current_path}
|
@@ -384,20 +461,18 @@ update_uboot() {
|
384 | 461 | # Check script permission
|
385 | 462 | [[ "$(id -u)" == "0" ]] || error_msg "Please run this script as root: [ sudo $0 ]"
|
386 | 463 | 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 | + |
401 | 476 | sync && sleep 3
|
402 | 477 | echo -e "${SUCCESS} Successfully updated, automatic restarting..."
|
403 | 478 | reboot
|
|
0 commit comments