-
Notifications
You must be signed in to change notification settings - Fork 7
/
kdeploy
executable file
·381 lines (309 loc) · 8.75 KB
/
kdeploy
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
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -euo pipefail
################################################################
## Following functions must be self-contained since they may be
## sent to a remote and executed there. Global params won't work
local_sync_to_dest() {
local tmpdir="$1"
shift
local sync_files_args=("$@")
if [[ -z "$tmpdir" ]]; then
return
fi
rsync -tcKrv "${sync_files_args[@]}" --progress "$tmpdir/." /
}
initrd() {
local module_only=$1
shift
local k=$1
if [[ "$module_only" != "no" ]]; then
return
fi
# shellcheck disable=SC1091
. /etc/os-release
if [[ "$ID_LIKE" == "arch" ]] || [[ "$ID" == "arch" ]]; then
mkinitcpio -k "/boot/vmlinuz-$k" -g "/boot/initramfs-$k.img"
elif [[ "$ID" == "ubuntu" ]]; then
update-initramfs -c -k "$k"
fi
}
bootloader() {
local module_only=$1
if [[ "$module_only" != "no" ]]; then
return
fi
# shellcheck disable=SC1091
. /etc/os-release
update-grub
}
reboot() {
local should_reboot="$1"
if [[ "$should_reboot" == "yes" ]]; then
command reboot
fi
}
sudo-function() {
(($#)) || { echo "Usage: sudo-function FUNC [ARGS...]" >&2; return 1; }
sudo bash -c "$(declare -f "$1");$(printf ' %q' "$@")"
}
################################################################
SCRIPT_FILENAME=$(basename "${BASH_SOURCE[0]}")
usage() {
cat - <<-EOF
$SCRIPT_FILENAME - Deploy kernel builds
USAGE:
$SCRIPT_FILENAME [OPTIONS]
Deploy Linux kernel build from the current directory to a local/remote system.
This command is expected to be used after building a kernel checkout and in the
directory where that build happened.
For remote deployment, both ssh and rsync are used - it's recommended to have
ssh key configured to avoid multiple authentication requests and the remote user
should be configured to be able to use sudo without password (TODO: remove this requirement)
OPTIONS:
-h, --help Show this help
-i, --include[=] GLOB Include only specific module glob
-C DIR Change to DIR before running
-m, --module-only Only deploy modules, skip kernel/bootloader changes
-r, --remote-host[=] HOST Host to be used for remote deploy via ssh/rsync
-u, --remote-user[=] USER User to be used for remote deploy via ssh/rsync
-p, --remote-port[=] PORT Port to be used for remote deploy via ssh/rsync
--remote-reboot Reboot remote host after complete [ default: no ]
--wait-remote-reboot Like --remote-reboot (implied), but also wait it
come back [ default: no ]
-t, --tmpdir[=] DIR Directory to use for temporary installation
--remote-tmpdir[=] DIR Set the remote directory to use as temporary location
for installation. An empty string disables it, with
the same behavior as --no-remote-tmpdir.
[ default: /tmp/kdeploy ]
--no-remote-tmpdir Disable use of tmpdir on the remote and it's the same
as passing --remote-tmpdir="". It requires remote
user to have sudo permission without password,
i.e. '<user> ALL=(ALL) NOPASSWD: ALL' to be configured
in sudoers.
EXAMPLES:
Install on the same host:
$ sudo $SCRIPT_FILENAME
Deploy kernel to a remote system:
$ $SCRIPT_FILENAME -r 192.168.0.2
Deploy kernel to a VM running on this host with port 4022 forwarded and with a
different user name (requirement: remote user must have sudo power):
$ $SCRIPT_FILENAME -p 4022 -r localhost -u test-user
Deploy only one module to a remote system:
$ $SCRIPT_FILENAME -r remote-system -m -i 'xe.ko*'
Deploy 2 modules to a remote system:
$ $SCRIPT_FILENAME -r remote-system -m -i 'xe.ko*' -i 'i915.ko*'
EOF
}
argerr() {
echo "invalid argument: $*" >&2
echo >&2
usage >&2
exit 1
}
parse_arg_val() {
local -n ret=$1
shift
local key val n=0
if [[ "$1" =~ ^([^=]+)(=(.*)) ]]; then
key=${BASH_REMATCH[1]}
val=${BASH_REMATCH[3]}
n=0
else
if [[ $# -lt 2 ]]; then
argerr "Invalid option $1"
fi
key="$1"
val="$2"
n=1
fi
# shellcheck disable=SC2034
ret=("$n" "$key" "$val")
}
OPT_MODULE_INCLUDE=()
OPT_MODULE_ONLY=no
OPT_REMOTE_USER=
OPT_REMOTE_PORT=
OPT_REMOTE_HOST=
OPT_REMOTE_REBOOT=no
OPT_REMOTE_TMPDIR=/tmp/kdeploy
OPT_WAIT_REMOTE_REBOOT=no
OPT_TMPDIR=
OPT_CHDIR=
SSH_REMOTE_ARGS=()
SYNC_FILES_ARGS=()
parse_args() {
local args
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-C)
parse_arg_val args "$@"
shift "${args[0]}"
OPT_CHDIR="${args[2]}"
;;
-i|--include|--include=*)
parse_arg_val args "$@"
shift "${args[0]}"
OPT_MODULE_INCLUDE+=("${args[2]}")
;;
-m|--module-only)
OPT_MODULE_ONLY=yes
;;
-r|--remote-host|--remote-host=*)
parse_arg_val args "$@"
shift "${args[0]}"
OPT_REMOTE_HOST="${args[2]}"
;;
-u|--remote-user|--remote-user=*)
parse_arg_val args "$@"
shift "${args[0]}"
OPT_REMOTE_USER="${args[2]}"
;;
-p|--remote-port|--remote-port=*)
parse_arg_val args "$@"
shift "${args[0]}"
OPT_REMOTE_PORT="${args[2]}"
;;
--remote-tmpdir|--remote-tmpdir=*)
parse_arg_val args "$@"
shift "${args[0]}"
OPT_REMOTE_TMPDIR="${args[2]}"
;;
--no-remote-tmpdir)
OPT_REMOTE_TMPDIR=
;;
-t|--tmpdir|--tmpdir=*)
parse_arg_val args "$@"
shift "${args[0]}"
OPT_TMPDIR="$(realpath -m "${args[2]}")"
;;
--remote-reboot)
OPT_REMOTE_REBOOT="yes"
;;
--wait-remote-reboot)
OPT_REMOTE_REBOOT="yes"
OPT_WAIT_REMOTE_REBOOT="yes"
;;
*)
argerr "Unknown option $1"
;;
esac
shift
done
if [[ -z "$OPT_REMOTE_HOST" ]]; then
if [[ -n "$OPT_REMOTE_USER" ]] ||
[[ -n "$OPT_REMOTE_PORT" ]] ||
[[ "$OPT_REMOTE_REBOOT" != no ]] ||
[[ "$OPT_WAIT_REMOTE_REBOOT" != no ]]; then
argerr "remote options require a remote specified with -r"
fi
fi
if [[ -z "$OPT_TMPDIR" ]]; then
OPT_TMPDIR=$(mktemp --tmpdir -d tmp.install-kernel.XXXX)
# shellcheck disable=SC2064
trap "rm -rf $OPT_TMPDIR" EXIT
fi
if [[ -n "$OPT_REMOTE_PORT" ]]; then
SSH_REMOTE_ARGS+=("-p" "$OPT_REMOTE_PORT")
fi
if [[ -n "$OPT_REMOTE_USER" ]]; then
SSH_REMOTE_ARGS+=("-l" "$OPT_REMOTE_USER")
fi
if [[ ${#OPT_MODULE_INCLUDE[@]} -gt 0 ]]; then
# include specific files given in the command line
readarray -t SYNC_FILES_ARGS< <(printf -- "--include=%s\n" "${OPT_MODULE_INCLUDE[@]}")
# include our script to finalize installation
SYNC_FILES_ARGS+=("--include=kdeploy-remote.sh")
# recurse subdirs, but exclude anything not included above
SYNC_FILES_ARGS+=("--include=*/")
SYNC_FILES_ARGS+=("--exclude=*")
fi
}
install_kernel() {
local krel="$1"
if [[ "$OPT_MODULE_ONLY" != "no" ]]; then
return
fi
mkdir -p "$OPT_TMPDIR/boot/"
# TODO: figure out arch
cp arch/x86/boot/bzImage "$OPT_TMPDIR/boot/vmlinuz-$krel"
}
install_modules() {
make "-j$(nproc)" INSTALL_MOD_PATH="$OPT_TMPDIR" modules_install
}
sync_to_dest() {
local rsync_path=() rtmp
if [[ -n "$OPT_REMOTE_HOST" ]]; then
# send functions to the remote that will finish the installation later
mkdir -p "$OPT_TMPDIR/tmp/"
typeset -f initrd bootloader reboot local_sync_to_dest \
> "$OPT_TMPDIR/tmp/kdeploy-remote.sh"
if [[ -n "$OPT_REMOTE_TMPDIR" ]]; then
rtmp=$OPT_REMOTE_TMPDIR
else
rsync_path+=("--rsync-path=sudo -u root rsync")
rtmp="/"
fi
rsync -tcKrv \
-e "ssh ${SSH_REMOTE_ARGS[*]}" \
"${rsync_path[@]}" \
"${SYNC_FILES_ARGS[@]}" \
--progress "$OPT_TMPDIR/." "$OPT_REMOTE_HOST:$rtmp"
else
sudo-function local_sync_to_dest "$OPT_TMPDIR" "${SYNC_FILES_ARGS[@]}"
fi
}
post_kernel_install() {
local ret rtmp
if [[ -n "$OPT_REMOTE_HOST" ]]; then
rtmp=${OPT_REMOTE_TMPDIR:-}
set +e
# shellcheck disable=SC2029
ssh -t "${SSH_REMOTE_ARGS[@]}" "$OPT_REMOTE_HOST" \
"sudo -s bash -c 'source $rtmp/tmp/kdeploy-remote.sh; \
local_sync_to_dest $OPT_REMOTE_TMPDIR ${SYNC_FILES_ARGS[*]} && \
initrd $OPT_MODULE_ONLY $krel && \
bootloader $OPT_MODULE_ONLY && \
reboot $OPT_REMOTE_REBOOT'"
ret=$?
set -e
if [[ $ret != 0 ]] && [[ "$OPT_REMOTE_REBOOT" != "yes" ]]; then
exit $ret
fi
else
sudo-function initrd "$OPT_MODULE_ONLY" "$krel" && sudo-function bootloader "$OPT_MODULE_ONLY"
fi
}
wait_reboot() {
local retries=20 i
if [[ -z "$OPT_REMOTE_HOST" ]] || [[ "$OPT_WAIT_REMOTE_REBOOT" == "no" ]]; then
return
fi
for (( i=1; i <= retries; i++ )); do
echo "Trying to re-connect [$i/$retries]"
if ssh -o BatchMode=yes \
"${SSH_REMOTE_ARGS[@]}"
"$OPT_REMOTE_HOST" true; then
break
fi
sleep 3
done
if [[ $retries -eq 0 ]]; then
echo "Max timeout (60s) to reconnect reached" >&2
exit 1
fi
}
parse_args "$@"
if [[ -n "$OPT_CHDIR" ]]; then
cd "$OPT_CHDIR"
fi
krel=$(make kernelrelease)
install_kernel "$krel"
install_modules
sync_to_dest
post_kernel_install "$krel"
wait_reboot