-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
udm-bootctl
executable file
·316 lines (264 loc) · 7.4 KB
/
udm-bootctl
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
#!/bin/bash
# Bash script for booting into custom kernels on the UDM (Pro).
#
# Copyright (C) 2021 Fabian Mastenbroek.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
set -e
TOOLS_PATH=/usr/lib/udm-kernel-tools
# Copy files to the UniFi OS root system.
# Arguments:
# $1 - src: Path to source to copy.
# $2 - dst: Path to destination to copy to.
_copy() {
scp -P "$(cat /etc/unifi-os/ssh_proxy_port)" -o StrictHostKeyChecking=no -q -r "$1" "root@localhost:$2"
}
# Load kernel image and possible initramfs into memory before boot.
# This function will copy over udm-kernel-tools, kexec and kernel image to host.
# Then, it will use the kexec binary to load the kernel image into memory.
# Arguments:
# $1 - target: Target directory to copy the kernel and initramfs to.
# $2 - kernel: Path to kernel to copy.
# $3 - initrd: Path to initramfs to copy.
_do_load() {
target=$1
kernel=$2
initrd=$3
_copy $TOOLS_PATH /usr/lib
_copy /sbin/kexec /sbin/
_copy "$kernel" "$target/kernel.gz"
if [ -n "$initrd" ]; then
_copy "$initrd" "$target/initramfs.gz"
fi
_copy $TOOLS_PATH/udm-kexec.init.d /etc/init.d/S00000kexec
# Decompress kernel before loading via Kexec as it does not support
# compressed kernels yet.
ssh-proxy "gunzip $target/kernel.gz"
args="-l $target/kernel --reuse-cmdline"
# Force kexec-tools to load the kernel image inside the right memory region
# See https://github.com/fabianishere/udm-kernel-tools/pull/14#issuecomment-850786069
args+=" --mem-min=0x04000000 --mem-max=0x06000000"
if [ -n "$initrd" ]; then
args+=" --initrd=$target/initramfs.gz"
fi
ssh-proxy "$TOOLS_PATH/udm-kexec $args"
}
# Perform the kexec boot.
# This function triggers a reboot, which will in turn trigger the kexec command
# as soon the the inittab services are stopped.
_do_boot() {
echo "Triggering system boot..."
ssh-proxy "reboot"
}
# Print the help message of this program.
bootctl_usage()
{
cat << EOF
Usage: $0 [OPTIONS...] COMMAND [ARGS]...
Manage custom kernels for the UniFi Dream Machine (Pro).
Commands:
list List available kernels to boot into
boot ID Boot into custom kernel
set-default ID Set default boot entry
Options:
-h, --help Show this message and exit
See udm-bootctl(8) for further details.
EOF
}
# Print the help message for the list command.
bootctl_list_usage()
{
cat << EOF
Usage: $0 list
List the available kernels that the user can boot into.
EOF
}
# List the available kernels.
bootctl_list() {
while getopts ":h" arg; do
case $arg in
h | *) # Display help.
bootctl_list_usage
exit 0
;;
esac
done
shift $((OPTIND - 1))
if test -z "$(find /boot -maxdepth 1 -name 'vmlinuz-*' -print -quit)"; then
echo "No kernels found."
exit 0
fi
printf "%-40s\t%s\n" "Version" "Initrd"
for kernel in /boot/vmlinuz-*; do
version=$(basename "$kernel" | sed -e "s/^vmlinuz-//")
initrd=$([ -f /boot/initramfs-"$version".gz ] && echo "yes" || echo "no")
if [ "$(readlink -- "/boot/vmlinuz")" = "$kernel" ]; then
printf "%-40s\t%s\n" "$version (default)" "$initrd"
else
printf "%-40s\t%s\n" "$version" "$initrd"
fi
done
}
# Print the help message for the boot command.
bootctl_boot_usage() {
cat << EOF
Usage: $0 boot [OPTIONS...] [<kernel>]
Boot into custom kernel.
Options:
-i Path to custom initramfs file
-h, --help Show this message and exit
EOF
}
# Boot into a custom kernel.
bootctl_boot() {
initrd=
while getopts ":hi:" arg; do
case $arg in
i)
initrd=$OPTARG
;;
h | *) # Display help.
bootctl_boot_usage
exit 0
;;
esac
done
shift $((OPTIND - 1))
kernel=$1
kernel_path=
initrd_path=
if [ -z "$kernel" ]; then
if [ -e /boot/vmlinuz ]; then
kernel_path=$(realpath /boot/vmlinuz)
else
echo "No default kernel available."
exit 1
fi
elif [ -e "$KERNEL_IMAGE" ]; then
kernel_path=$(realpath "$kernel")
elif [ -e "/boot/vmlinuz-$kernel" ]; then
kernel_path="/boot/vmlinuz-$kernel"
else
echo "$1: kernel not found."
exit 1
fi
version=$(basename "$kernel_path" | sed -e "s/^vmlinuz-//")
if [ -n "$initrd" ]; then
if [ -e "$initrd" ]; then
initrd_path=$(realpath "$initrd")
else
echo "$initrd: initrd not found."
exit 1
fi
elif [ -e "/boot/initramfs-$version".gz ]; then
initrd_path=$(realpath "/boot/initramfs-$version.gz")
fi
target=$(ssh-proxy "mktemp -d -t udmXXXXXX")
echo "Loading kernel $version..."
_do_load "$target" "$kernel_path" "$initrd_path"
_do_boot
}
# Print the help message for the autoboot command.
bootctl_autoboot_usage() {
cat << EOF
Usage: $0 autoboot [OPTIONS...]
Automatically boot into custom kernel.
Options:
-h, --help Show this message and exit
EOF
}
# Automatically boot into custom kernel
bootctl_autoboot() {
while getopts ":h" arg; do
case $arg in
h | *) # Display help.
bootctl_autoboot_usage
exit 0
;;
esac
done
shift $((OPTIND - 1))
if ssh-proxy "test -f /.udm-kernel-tools"; then
echo "Successfully booted into custom kernel.."
# Remove mark to indicate that we are booting into a custom kernel
rm -f /mnt/persistent/.udm-boot-mark
exit 0
elif [ -f /mnt/persistent/.udm-boot-mark ]; then
echo "Custom kernel boot seems to have failed. Aborting..."
exit 1
else
touch /mnt/persistent/.udm-boot-mark
bootctl_boot
fi
}
# Print the help message for the set-default command.
bootctl_set_default_usage()
{
cat << EOF
Usage: $0 set-default [OPTIONS...] <kernel>
Set the specified kernel as the default kernel
Options:
-h, --help Show this message and exit
EOF
}
# Set the default kernel.
bootctl_set_default() {
while getopts ":h" arg; do
case $arg in
h | *) # Display help.
bootctl_set_default_usage
exit 0
;;
esac
done
shift $((OPTIND - 1))
version=$1
if [ -z "$version" ]; then
echo "Please specify a kernel version."
bootctl_set_default_usage
exit 1
elif [ ! -e "/boot/vmlinuz-$version" ]; then
echo "$version: kernel version not found."
exit 1
fi
ln -sf "/boot/vmlinuz-$version" /boot/vmlinuz
if [ -e "/boot/initramfs-$version.gz" ]; then
ln -sf "/boot/initramfs-$version.gz" /boot/initramfs.gz
else
rm -f /boot/initramfs.gz
fi
}
# Check if the user passed any argument
if [ $# -eq 0 ]; then
bootctl_usage
exit 1
fi
case $1 in
-h|--help)
bootctl_usage
exit 0
;;
list)
shift
bootctl_list "$@"
;;
boot)
shift
bootctl_boot "$@"
;;
autoboot)
shift
bootctl_autoboot "$@"
;;
set-default)
shift
bootctl_set_default "$@"
;;
*)
bootctl_usage
exit 1
;;
esac