|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) 2022 The Toltec Contributors |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# kernel staging dir |
| 6 | +kernelctl_dir=/opt/usr/share/kernelctl |
| 7 | + |
| 8 | +# Formatting |
| 9 | +bf="\033[1m" # bold |
| 10 | +sf="\033[0m" # standard |
| 11 | +gr="\033[1;32m" # green |
| 12 | +bl="\033[1;34m" # blue |
| 13 | + |
| 14 | +# change our working directory to / to ease filesystem operations |
| 15 | +cd / |
| 16 | + |
| 17 | +help() { |
| 18 | + read -r -d '' msg <<- EOM |
| 19 | + Usage: $(basename "$0") COMMAND |
| 20 | + Manage your booting kernel. |
| 21 | +
|
| 22 | + ${gr}Available commands:${sf} |
| 23 | + ${bf} backup <kernel_name> ${sf}Backup current kernel and name it <kernel_name>. This is guaranteed to work only with vanilla (i.e. upstream) kernels. |
| 24 | + ${bf} help ${sf}Show this help message. |
| 25 | + ${bf} list ${sf}List available kernels. |
| 26 | + ${bf} show ${sf}Show the current configured kernel. |
| 27 | + ${bf} delete <kernel> ${sf}Delete kernel from the staging dir. WARNING this is irreversible. |
| 28 | + ${bf} set <kernel> ${sf}Change booting kernel. |
| 29 | +
|
| 30 | + ${bf} <kernel> ${sf}Kernel name or number (from 'list' command) or "default" to revert to the upstram kernel. |
| 31 | + EOM |
| 32 | + echo -e "$msg" |
| 33 | +} |
| 34 | + |
| 35 | +# backup current kernel |
| 36 | +backup() { |
| 37 | + if [[ "$1" = "vanilla" ]]; then |
| 38 | + kernel_name="vanilla-$(< /etc/version)" |
| 39 | + else |
| 40 | + kernel_name=$1 |
| 41 | + fi |
| 42 | + |
| 43 | + kernel_file="$kernelctl_dir"/"$kernel_name".tar.bz2 |
| 44 | + |
| 45 | + if [[ -e "$kernel_file" ]]; then |
| 46 | + read -r -d "" msg <<- EOM |
| 47 | + It looks like there is already copy of $kernel_name in the staging area. |
| 48 | + If you really want to back it up again please run |
| 49 | +
|
| 50 | + $(basename "$0") delete $kernel_name |
| 51 | + $(basename "$0") backup $kernel_name |
| 52 | + EOM |
| 53 | + echo -e "$msg" |
| 54 | + exit 1 |
| 55 | + else |
| 56 | + tar cpjf "$kernel_file" lib/modules/* boot/zImage* boot/*.dtb |
| 57 | + rm -f "$kernelctl_dir"/current.tar.bz2 |
| 58 | + ln "$kernel_file" "$kernelctl_dir"/current.tar.bz2 |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +# get available kernels |
| 63 | +get_kernel_names() { |
| 64 | + mapfile -t kernel_names < <(find "$kernelctl_dir" -path "*.tar.bz2" ! -name current.tar.bz2 -print0 | xargs -0 -I"{}" basename {} .tar.bz2) |
| 65 | +} |
| 66 | + |
| 67 | +# get current kernel |
| 68 | +get_current_kernel_name() { |
| 69 | + current_kernel_name=$(find "$kernelctl_dir" -samefile "$kernelctl_dir"/current.tar.bz2 ! -name current.tar.bz2 -print0 | xargs -0 -I"{}" basename {} .tar.bz2) |
| 70 | +} |
| 71 | + |
| 72 | +# translate input into a kernel name |
| 73 | +to_kernel_name() { |
| 74 | + local ker |
| 75 | + get_kernel_names |
| 76 | + if [[ "$1" =~ ^[0-9]+$ ]] && ((0 < $1 && $1 <= ${#kernel_names[@]})); then |
| 77 | + echo "${kernel_names[$(($1 - 1))]}" |
| 78 | + return |
| 79 | + elif [[ "$1" = "default" ]]; then |
| 80 | + ker="vanilla-$(< /etc/version)" |
| 81 | + else |
| 82 | + ker="$1" |
| 83 | + fi |
| 84 | + if [[ $(echo "${kernel_names[@]}" | grep -ow "$ker" | wc -w) -gt 0 ]]; then |
| 85 | + echo "$ker" |
| 86 | + else |
| 87 | + echo "Can't find $ker in the staging area." |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# list available kernels |
| 93 | +list() { |
| 94 | + get_kernel_names |
| 95 | + get_current_kernel_name |
| 96 | + echo -e "${gr}Available kernels:${sf}" |
| 97 | + for i in "${!kernel_names[@]}"; do |
| 98 | + if [[ "$current_kernel_name" = "${kernel_names[$i]}" ]]; then cur=" ${bl}*${sf}"; else cur=""; fi |
| 99 | + echo -e " ${bf}[$((i + 1))]${sf}\t${kernel_names[$i]}$cur" |
| 100 | + done |
| 101 | +} |
| 102 | + |
| 103 | +# show the current configured kernel |
| 104 | +show() { |
| 105 | + get_current_kernel_name |
| 106 | + echo -e "${gr}Current kernel:${sf}" |
| 107 | + echo -e " ${bf}${current_kernel_name}${sf}" |
| 108 | +} |
| 109 | + |
| 110 | +# actually switch kernels |
| 111 | +switch() { |
| 112 | + tar tjf "$1" | sort -r | xargs -r -I {} bash -c 'if [[ -d "{}" ]]; then rmdir "{}"; else rm "{}"; fi' |
| 113 | + tar xpjf "$2" |
| 114 | +} |
| 115 | + |
| 116 | +# change the kernel that will boot next time |
| 117 | +set() { |
| 118 | + new_kernel_name=$(to_kernel_name "$1") |
| 119 | + new_kernel_file="${kernelctl_dir}/${new_kernel_name}.tar.bz2" |
| 120 | + current_kernel_file="${kernelctl_dir}/current.tar.bz2" |
| 121 | + if [[ ! -e "$current_kernel_file" ]]; then |
| 122 | + read -r -d '' msg <<- EOM |
| 123 | + ${bf}There is no link to the current running kernel in the staging |
| 124 | + area, you might have accidentally deleted it!${sf} |
| 125 | + Try making a backup first. |
| 126 | + EOM |
| 127 | + echo -e "$msg" |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | + if switch "$current_kernel_file" "$new_kernel_file"; then |
| 131 | + rm -f "$current_kernel_file" |
| 132 | + ln "$new_kernel_file" "$current_kernel_file" |
| 133 | + /bin/sync |
| 134 | + echo "Reboot system to use your newly installed kernel." |
| 135 | + else |
| 136 | + read -r -d '' msg <<- EOM |
| 137 | + ${bf}WARNING: failed to extract the new kernel!${sf} |
| 138 | + This may be due to a lack of space in the root partition. |
| 139 | + Attempting to revert to the current running kernel. |
| 140 | + EOM |
| 141 | + echo -e "$msg" |
| 142 | + if switch "$new_kernel_file" "$current_kernel_file"; then |
| 143 | + /bin/sync |
| 144 | + read -r -d '' msg <<- EOM |
| 145 | + Successfully reverted to the current running kernel. |
| 146 | + Please check that everything is in order before rebooting. |
| 147 | + EOM |
| 148 | + echo -e "$msg" |
| 149 | + else |
| 150 | + read -r -d '' msg <<- EOM |
| 151 | + Unable to revert to a working configuration. |
| 152 | + If you do not know how to proceed, please do not reboot your device, |
| 153 | + do not let the battery die, and seek assistance |
| 154 | + in the reMarkable's community Discord server: https://discord.gg/ATqQGfu |
| 155 | + EOM |
| 156 | + echo -e "$msg" |
| 157 | + exit 1 |
| 158 | + fi |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +# delete kernel from staging dir |
| 163 | +delete() { |
| 164 | + kernel_name=$(to_kernel_name "$1") |
| 165 | + echo "Deleting $kernel_name from the staging area is irreversible." |
| 166 | + echo "If the kernel was installed as a toltec package this may confuse the package manager" |
| 167 | + echo -n "Do you want to proceed? [N/y]: " |
| 168 | + read -r ans |
| 169 | + if [[ "$ans" = "y" || "$ans" = "Y" ]]; then |
| 170 | + rm "${kernelctl_dir}/${kernel_name}.tar.bz2" |
| 171 | + fi |
| 172 | +} |
| 173 | + |
| 174 | +if [[ $0 = "${BASH_SOURCE[0]}" ]]; then |
| 175 | + if [[ $# -eq 0 ]]; then |
| 176 | + help |
| 177 | + exit 1 |
| 178 | + fi |
| 179 | + |
| 180 | + action="$1" |
| 181 | + shift |
| 182 | + |
| 183 | + case $action in |
| 184 | + help | -h | --help) |
| 185 | + help |
| 186 | + ;; |
| 187 | + list) |
| 188 | + list |
| 189 | + ;; |
| 190 | + show) |
| 191 | + show |
| 192 | + ;; |
| 193 | + backup) |
| 194 | + if [[ $# -ne 1 ]]; then |
| 195 | + help |
| 196 | + exit 1 |
| 197 | + fi |
| 198 | + backup "$1" |
| 199 | + ;; |
| 200 | + set) |
| 201 | + if [[ $# -ne 1 ]]; then |
| 202 | + help |
| 203 | + exit 1 |
| 204 | + fi |
| 205 | + set "$1" |
| 206 | + ;; |
| 207 | + delete) |
| 208 | + if [[ $# -ne 1 ]]; then |
| 209 | + help |
| 210 | + exit 1 |
| 211 | + fi |
| 212 | + delete "$1" |
| 213 | + ;; |
| 214 | + *) |
| 215 | + echo -e "Error: Invalid command '$action'\n" |
| 216 | + help |
| 217 | + exit 1 |
| 218 | + ;; |
| 219 | + esac |
| 220 | +fi |
0 commit comments