-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-kernel.sh
498 lines (416 loc) · 12.6 KB
/
custom-kernel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env bash
### Global variables
declare CONFIG_FOLDER_PATH="/etc/custom-kernel"
declare CONFIG_PATH="${CONFIG_FOLDER_PATH}/config.ini"
### Helper functions
function show_help() {
echo "Usage: custom-kernel [OPTION]"
echo "Manage custom kernels on systems that use kernelstub."
echo " "
echo "Options:"
echo " -d, --dry-run Print actions without executing them."
echo " -u, --update Update to the next available custom kernel."
echo " -i, --init-config Initialize the configuration file. Uses the values from kernelstub."
echo " -h, --help, help Display this help message."
echo " "
echo "Default behavior (no option provided or with --dry-run only):"
echo " Starts an interactive command line dialog to select and set a custom kernel."
}
function require_root() {
if [[ ${EUID} -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
}
function execute_or_dry_run() {
if [[ ${DRY_RUN} -eq 1 ]]; then
echo "Dry run: $*"
else
"$@"
fi
}
### Config functions
# uses: CONFIG_FOLDER_PATH, CONFIG_PATH, show_help
function read_config() {
if [[ ! -d ${CONFIG_FOLDER_PATH} ]]; then
echo ""
echo "Config folder not found at '${CONFIG_FOLDER_PATH}'."
echo "You need to initialize the configuration file first."
echo ""
show_help
exit 1
elif [[ ! -f ${CONFIG_PATH} ]]; then
echo ""
echo "Config file not found at ${CONFIG_PATH}'."
echo "You need to initialize the configuration file first."
echo ""
show_help
exit 1
else
# shellcheck disable=SC1090
source "${CONFIG_PATH}"
fi
if [[ -z ${BOOT_PATH} ]]; then
echo ""
echo "Error: BOOT_PATH is not set."
echo "You need to initialize the configuration file first."
echo "This will set the BOOT_PATH variable in the config file at '${CONFIG_PATH}'."
echo ""
show_help
exit 1
elif [[ ! -d ${BOOT_PATH} ]]; then
echo ""
echo "Error: The specified BOOT_PATH '${BOOT_PATH}' does not exist on the filesystem."
echo "Please make sure the output of 'kernelstub --print-config' has the correct BOOT_PATH."
echo ""
exit 1
fi
if [[ -z ${EFI_PATH} ]]; then
echo ""
echo "Error: EFI_PATH is not set."
echo "You need to initialize the configuration file first."
echo "This will set the EFI_PATH variable in the config file at '${CONFIG_PATH}'."
echo ""
show_help
exit 1
elif [[ ! -d ${EFI_PATH} ]]; then
echo ""
echo "Error: The specified EFI_PATH '${EFI_PATH}' does not exist on the filesystem."
echo "Please make sure the output of 'kernelstub --print-config' has the correct ESP_PATH and Root FS UUID."
exit 1
fi
}
# uses: CONFIG_FOLDER_PATH, CONFIG_PATH
function init_config() {
# Fetch the kernelstub configuration
local kernelstub_output
kernelstub_output=$(
kernelstub --print-config 2>&1
)
# Parse necessary values
local boot_path esp_path root_fs_uuid
boot_path=$(
echo "${kernelstub_output}" \
| grep 'Kernel Image Path:' \
| awk -F':' '{print $2}' \
| sed 's/^\.*//'
)
# Remove the kernel image name and retain the directory path
boot_path="${boot_path%/*}"
echo -e "\nBoot Path: ${boot_path}"
esp_path=$(
echo "${kernelstub_output}" \
| grep 'ESP Path:' \
| awk -F':' '{print $2}' \
| sed 's/^\.*//'
)
echo "ESP Path: ${esp_path}"
root_fs_uuid=$(
echo "${kernelstub_output}" \
| grep 'Root FS UUID:' \
| awk -F':' '{print $2}' \
| sed 's/^\.*//'
)
echo "Root FS UUID: ${root_fs_uuid}"
efi_path=$(
find "${esp_path}/EFI" -type d -name "*${root_fs_uuid}*" -print \
| head -n 1
)
if [[ -n "$efi_path" ]]; then
echo "EFI folder path found: ${efi_path}"
else
echo "EFI folder path not found with the given UUID."
return 1 # Return failure if not found
fi
# Ensure the config folder and file exists or create it
if [[ ! -d ${CONFIG_FOLDER_PATH} ]]; then
echo -e "\nCreating config folder at ${CONFIG_FOLDER_PATH}"
execute_or_dry_run mkdir -p "${CONFIG_FOLDER_PATH}"
fi
if [[ ! -f ${CONFIG_PATH} ]]; then
echo -e "\nCreating config file at${CONFIG_PATH}"
execute_or_dry_run touch "${CONFIG_PATH}"
fi
# Write to config file
echo -e "\nInitializing config.ini with newest values from kernelstub..."
config_content="### Configuration settings for custom kernel management
# BOOT_PATH: Specifies the directory where kernel images (vmlinuz) and initial RAM disks (initrd.img) are located.
BOOT_PATH=${boot_path}
# EFI_PATH: Specifies the full path to the EFI folder on the EFI System Partition for the local operating system.
EFI_PATH=${efi_path}
"
execute_or_dry_run bash -c "echo -e \"${config_content}\" > \"${CONFIG_PATH}\""
echo -e "\nConfiguration file updated successfully."
}
### Kernel management functions
# in: ($1) BOOT_PATH
# out: (echo) available kernels
function get_available_kernels() {
local boot_path=$1
# shellcheck disable=SC2231
for file in ${boot_path}/vmlinuz-*; do
echo "${file##*/vmlinuz-}"
done
}
# in: ($1) boot_path
# out: (echo) current custom kernel
function get_current_custom_kernel() {
local boot_path=$1
local result
if [[ ! -f "${boot_path}/vmlinuz.custom" ]]; then
echo "No custom kernel set."
return 1
fi
result=$(
readlink "${boot_path}/vmlinuz.custom" \
| cut -d '-' -f 2-
)
echo "${result}"
}
# in: ($1) sorted_kernels
# out: (echo) formatting length
function calculate_formatting_length() {
local sorted_kernels=$1
local num_kernels num_longest_string additional_chars
num_kernels=$(
echo -e "${sorted_kernels}" \
| wc -l
)
num_longest_string=$(
echo -e "${sorted_kernels}" \
| awk '{ print length }' \
| sort -n \
| tail -1
)
additional_chars=$((num_kernels < 10 ? 3 : 4))
echo $((num_longest_string + additional_chars))
}
# in: ($1) sorted_kernels, ($2) total_length, ($3) current_custom_kernel,
# ($4) __ret__prompt_for_kernel_selection
# out: (__ret__prompt_for_kernel_selection) selected kernel
function prompt_for_kernel_selection() {
local sorted_kernels=$1
local total_length=$2
local current_custom_kernel=$3
local -n __ret__prompt_for_kernel_selection=$4
local current_custom_kernel num_kernels selection
echo -e "\nYour current custom kernel is: ${current_custom_kernel}"
echo -e "\nChoose kernel:"
printf '%*s\n' "${total_length}" '' \
| tr ' ' '-'
echo -e "${sorted_kernels}" \
| awk '{ print NR ": " $0 }'
printf '%*s\n' "${total_length}" '' \
| tr ' ' '-'
num_kernels=$(
echo -e "${sorted_kernels}" \
| wc -l
)
read -r -p "Enter [1-${num_kernels}]: " num
if [[ -z "$num" ]]; then
selection=""
else
selection=$(
echo -e "${sorted_kernels}" \
| sed -n "${num}p;d"
)
fi
__ret__prompt_for_kernel_selection=$selection
}
# in: ($1) available_kernels, ($2) boot_path,
# ($3) __ret__choose_kernel
# out: (__ret__choose_kernel) selected kernel
# uses: prompt_for_kernel_selection, calculate_formatting_length, get_current_custom_kernel
function choose_kernel() {
local available_kernels=$1
local boot_path=$2
local -n __ret__choose_kernel=$3
local sorted_kernels total_length
sorted_kernels=$(
echo -e "${available_kernels}" \
| sort -V
)
total_length=$(calculate_formatting_length "${sorted_kernels}")
prompt_for_kernel_selection \
"${sorted_kernels}" "${total_length}" \
"$(get_current_custom_kernel "${boot_path}")" \
__ret__choose_kernel
}
# in: ($1) current_custom_kernel
# out: (echo) dynamic regex
function generate_dynamic_regex() {
local current_custom_kernel=$1
echo "${current_custom_kernel}" \
| sed -E 's/[0-9]+/[0-9]+/g'
}
# in: ($1) available_kernels, ($2) regex
# out: (echo) sorted kernels
function filter_and_sort_kernels() {
local available_kernels=$1
local regex=$2
echo -e "${available_kernels}" \
| grep -E "${regex}" \
| sort -V
}
# in: ($1) sorted_kernels, ($2) current_custom_kernel
# out: (echo) next kernel
function get_next_kernel() {
local sorted_kernels=$1
local current_custom_kernel=$2
local found_current next_kernel
found_current=0
next_kernel=""
for kernel in ${sorted_kernels}; do
if [[ "${kernel}" == "${current_custom_kernel}" ]]; then
found_current=1
continue
fi
if [[ ${found_current} -eq 1 ]]; then
next_kernel="${kernel}"
break
fi
done
echo "${next_kernel}"
}
# in: ($1) sorted_kernels, ($2) current_custom_kernel
# out: (__ret__search_next_kernel) next kernel
# uses: generate_dynamic_regex, filter_and_sort_kernels, get_next_kernel
function search_next_kernel() {
local available_kernels=$1
local current_custom_kernel=$2
local -n __ret__search_next_kernel=$3
local dynamic_regex sorted_kernels next_kern
dynamic_regex=$(generate_dynamic_regex "${current_custom_kernel}")
sorted_kernels=$(filter_and_sort_kernels "${available_kernels}" "${dynamic_regex}")
next_kern=$(get_next_kernel "${sorted_kernels}" "${current_custom_kernel}")
__ret__search_next_kernel="$next_kern"
}
# in: ($1) selected_kernel, ($2) boot_path
# uses: execute_or_dry_run
function update_kernel_links() {
local selected_kernel=$1
local boot_path=$2
execute_or_dry_run ln -sf initrd.img-"${selected_kernel}" "${boot_path}/initrd.img.custom"
execute_or_dry_run ln -sf vmlinuz-"${selected_kernel}" "${boot_path}/vmlinuz.custom"
}
# in: ($1) efi_path, ($2) boot_path
# uses: execute_or_dry_run
function copy_to_efi() {
local boot_path=$1
local efi_path=$2
execute_or_dry_run cp "${boot_path}/initrd.img.custom" "${efi_path}/initrd.img-custom"
execute_or_dry_run cp "${boot_path}/vmlinuz.custom" "${efi_path}/vmlinuz-custom.efi"
}
# uses: get_available_kernels, choose_kernel, update_kernel_links, copy_to_efi
function set_custom_kernel() {
local available_kernels selected_kernel retry_choice
available_kernels=$(get_available_kernels "${BOOT_PATH}")
while true; do
choose_kernel "${available_kernels}" "${BOOT_PATH}" selected_kernel
if [[ -n ${selected_kernel} ]]; then
break # Valid kernel selected, exit the loop
else
echo -e "\nNo kernel selected."
# TODO: Check if we have to use the "-r" flag for read
# shellcheck disable=SC2162
read -p "Do you want to try again? [y|N]: " retry_choice
if [[ "${retry_choice,,}" != "y" && "${retry_choice,,}" != "yes" ]]; then
echo -e "\nExiting without selecting a kernel.\n"
exit 1
fi
fi
done
update_kernel_links "${selected_kernel}" "${BOOT_PATH}"
copy_to_efi "${BOOT_PATH}" "${EFI_PATH}"
}
# uses: get_available_kernels, get_current_custom_kernel, search_next_kernel, update_kernel_links, copy_to_efi
function update_to_next_kernel() {
local available_kernels current_custom_kernel next_kernel
available_kernels=$(get_available_kernels "${BOOT_PATH}")
if current_custom_kernel=$(get_current_custom_kernel "${BOOT_PATH}"); then
echo -e "\nCurrent custom kernel: ${current_custom_kernel}"
else
echo -e "\nError: No custom kernel set."
echo -e "Run the script without options to set a custom kernel first.\n"
show_help
exit 1
fi
search_next_kernel "${available_kernels}" "${current_custom_kernel}" next_kernel
if [[ -n ${next_kernel} ]]; then
echo -e "\nThe new custom kernel is: ${next_kernel}\n"
else
echo -e "\nNo next kernel found for current custom kernel: ${current_custom_kernel}\n"
exit 0
fi
update_kernel_links "${next_kernel}" "${BOOT_PATH}"
copy_to_efi "${BOOT_PATH}" "${EFI_PATH}"
}
### CLI handling
# Translate long options to short ones
for arg in "$@"; do
shift
case "$arg" in
"--dry-run")
set -- "$@" "-d"
;;
"--update")
set -- "$@" "-u"
;;
"--init-config")
set -- "$@" "-i"
;;
"--help")
set -- "$@" "-h"
;;
"help")
set -- "$@" "-h"
;;
"--")
set -- "$@" "--"
;;
*)
set -- "$@" "$arg"
;;
esac
done
DRY_RUN=0
OPTION_PROVIDED=0
while getopts "udih" opt; do
case ${opt} in
d)
DRY_RUN=1
;;
u)
OPTION_PROVIDED=1
require_root
read_config
update_to_next_kernel
;;
i)
OPTION_PROVIDED=1
require_root
init_config
;;
h)
show_help
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
show_help
exit 1
;;
esac
done
shift $((OPTIND -1))
# Handle default action when no options are provided
if [[ ${OPTION_PROVIDED} -eq 0 ]]; then
require_root
read_config
set_custom_kernel
fi
if [[ ${DRY_RUN} -eq 1 ]]; then
echo -e "\nThis was a dry run. No changes were made.\n"
else
echo -e "\nAll operations completed successfully.\n"
fi