-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathbuild-kernel.sh
More file actions
executable file
·475 lines (400 loc) · 12.4 KB
/
Copy pathbuild-kernel.sh
File metadata and controls
executable file
·475 lines (400 loc) · 12.4 KB
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
#!/usr/bin/env bash
set -Eeuo pipefail
shopt -s inherit_errexit
IFS=$'\n\t'
readonly SCRIPT_NAME="${0##*/}"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
readonly SCRIPT_VERSION="4.0.0"
readonly KERNEL_REPO_URL="https://github.com/microsoft/WSL2-Linux-Kernel.git"
readonly DEFAULT_KERNEL_SERIES="6"
if [[ -z "${NO_COLOR-}" && -t 1 && -t 2 ]]; then
readonly RED=$'\033[0;31m'
readonly GREEN=$'\033[0;32m'
readonly YELLOW=$'\033[0;33m'
readonly RESET=$'\033[0m'
else
readonly RED=""
readonly GREEN=""
readonly YELLOW=""
readonly RESET=""
fi
readonly TARGET_UID="${SUDO_UID:-$(id -u)}"
readonly TARGET_GID="${SUDO_GID:-$(id -g)}"
output_directory="$PWD"
kernel_version=""
kernel_series=""
generate_wslconfig="ask"
keep_build_dir="false"
list_versions_only="false"
workspace=""
archive_path=""
source_dir=""
build_log=""
selected_version=""
apt_updated="false"
config_path=""
show_help() {
cat <<EOF
Usage: ${SCRIPT_NAME} [options]
Build the Microsoft WSL2 kernel from source and copy the resulting vmlinux file
to the selected output directory.
Options:
-h, --help Show this help message.
-V, --script-version Show the script version.
-v, --version <version> Build a specific WSL kernel tag version.
--series <5|6> Build the latest version from a major series.
-o, --output-directory <dir> Directory to receive the built vmlinux file.
-c, --config <path> Use this kernel .config instead of the
upstream Microsoft/config-wsl. If omitted,
a 'config-wsl' file shipped next to this
script is used when present (full CAN
bus support, etc.); otherwise the upstream
config from the source tarball is used.
--list-versions Print available upstream kernel versions.
--generate-wslconfig Run the local .wslconfig generator after build.
--skip-wslconfig Skip the .wslconfig generator prompt.
--keep-build-dir Preserve the temporary build directory.
Examples:
sudo bash ${SCRIPT_NAME}
sudo bash ${SCRIPT_NAME} --series 6 --output-directory "\$HOME/WSL2"
sudo bash ${SCRIPT_NAME} --version 6.6.87.2 --skip-wslconfig
sudo bash ${SCRIPT_NAME} --config ./my-config-wsl
EOF
}
log() {
printf '%s[INFO]%s %s\n' "$GREEN" "$RESET" "$*"
}
warn() {
printf '%s[WARN]%s %s\n' "$YELLOW" "$RESET" "$*" >&2
}
fail() {
printf '%s[ERROR]%s %s\n' "$RED" "$RESET" "$*" >&2
exit 1
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
cleanup() {
local status=$?
if [[ -z "$workspace" || ! -d "$workspace" ]]; then
return "$status"
fi
if (( status != 0 )); then
warn "Build workspace preserved for inspection: $workspace"
return "$status"
fi
if [[ "$keep_build_dir" == "true" ]]; then
log "Build workspace retained: $workspace"
return 0
fi
rm -rf -- "$workspace"
return 0
}
trap cleanup EXIT
ensure_root() {
[[ "$EUID" -eq 0 ]] || fail "Run this script with root privileges."
}
ensure_directory() {
mkdir -p -- "$1"
}
ensure_output_directory() {
ensure_directory "$output_directory"
}
own_file_for_user() {
local target=$1
if command_exists chown; then
chown "$TARGET_UID:$TARGET_GID" "$target" 2>/dev/null || true
fi
}
apt_install_missing_packages() {
local -a required_packages=(
bc
bison
build-essential
ca-certificates
ccache
curl
dwarves
flex
git
libcap-dev
libelf-dev
libncurses-dev
libssl-dev
python3
rsync
wget
xz-utils
)
local -a missing_packages=()
local package
for package in "${required_packages[@]}"; do
if ! dpkg-query -W -f='${Status}' "$package" 2>/dev/null | grep -q "ok installed"; then
missing_packages+=("$package")
fi
done
if [[ ${#missing_packages[@]} -eq 0 ]]; then
log "Build dependencies are already installed."
return
fi
log "Installing missing packages: ${missing_packages[*]}"
if [[ "$apt_updated" != "true" ]]; then
apt-get update
apt_updated="true"
fi
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends "${missing_packages[@]}"
}
fetch_available_versions() {
command_exists git || fail "git is required to resolve available kernel versions."
git ls-remote --tags --refs "$KERNEL_REPO_URL" 'refs/tags/linux-msft-wsl-*' |
awk '{print $2}' |
sed 's#refs/tags/linux-msft-wsl-##' |
grep -E '^[0-9]+(\.[0-9]+)*$' |
sort -Vru
}
list_available_versions() {
log "Available upstream kernel versions:"
fetch_available_versions
}
resolve_latest_version_for_series() {
local series=$1
fetch_available_versions | awk -F. -v target="$series" '$1 == target { print; exit }'
}
prompt_for_version_selection() {
local choice
while true; do
cat <<'EOF'
Choose the WSL2 kernel version to build:
1. Latest Linux series 6 kernel
2. Latest Linux series 5 kernel
3. Specific version
4. List available versions
5. Exit
EOF
read -r -p "Enter your choice (1-5): " choice
case "$choice" in
1)
kernel_series="6"
return
;;
2)
kernel_series="5"
return
;;
3)
read -r -p "Enter the full version (example: 6.6.87.2): " kernel_version
[[ -n "$kernel_version" ]] || fail "A version value is required."
return
;;
4)
list_available_versions
;;
5)
exit 0
;;
*)
warn "Invalid choice. Enter a number between 1 and 5."
;;
esac
done
}
resolve_selected_version() {
if [[ -n "$kernel_version" ]]; then
selected_version="$kernel_version"
return
fi
if [[ -z "$kernel_series" ]]; then
if [[ -t 0 ]]; then
prompt_for_version_selection
else
kernel_series="$DEFAULT_KERNEL_SERIES"
log "No version specified; defaulting to latest series ${kernel_series} kernel."
fi
fi
selected_version="$(resolve_latest_version_for_series "$kernel_series")"
[[ -n "$selected_version" ]] || fail "Failed to resolve the latest series ${kernel_series} kernel version."
}
prepare_workspace() {
workspace="$(mktemp -d "${TMPDIR:-/tmp}/wsl2-kernel-build.XXXXXX")"
archive_path="${workspace}/kernel.tar.gz"
source_dir="${workspace}/source"
build_log="${workspace}/build.log"
mkdir -p -- "$source_dir"
}
download_source_archive() {
local version=$1
local archive_url="https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/tags/linux-msft-wsl-${version}.tar.gz"
log "Downloading kernel source for ${version}..."
curl --fail --location --retry 3 --retry-delay 2 --silent --show-error \
--output "$archive_path" "$archive_url"
}
extract_source_archive() {
log "Extracting source archive..."
tar -xzf "$archive_path" -C "$source_dir" --strip-components=1
[[ -f "${source_dir}/Microsoft/config-wsl" ]] || fail "The upstream source archive is missing Microsoft/config-wsl."
}
run_logged() {
{
printf '\n===== %s | %s =====\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*"
"$@"
} 2>&1 | tee -a -- "$build_log"
}
resolve_kernel_config() {
if [[ -n "$config_path" ]]; then
log "Using kernel config from --config: $config_path"
printf '%s\n' "$config_path"
return
fi
local bundled="${SCRIPT_DIR}/config-wsl"
if [[ -f "$bundled" ]]; then
log "Using bundled kernel config: $bundled"
printf '%s\n' "$bundled"
return
fi
local upstream="${source_dir}/Microsoft/config-wsl"
log "Using upstream kernel config: $upstream"
printf '%s\n' "$upstream"
}
build_kernel() {
local effective_config
effective_config="$(resolve_kernel_config)"
log "Preparing kernel configuration..."
cp -- "$effective_config" "${source_dir}/.config"
(
cd -- "$source_dir"
run_logged make olddefconfig
run_logged make -j"$(nproc --all)"
run_logged make modules_install headers_install
)
}
copy_vmlinux() {
local source_vmlinux="${source_dir}/vmlinux"
local destination="${output_directory}/vmlinux"
[[ -f "$source_vmlinux" ]] || fail "Kernel build completed without producing vmlinux."
install -m 0644 "$source_vmlinux" "$destination"
own_file_for_user "$destination"
log "Kernel build complete. vmlinux written to ${destination}"
}
prompt_wslconfig_generation() {
local choice
if [[ "$generate_wslconfig" == "false" ]]; then
return
fi
if [[ "$generate_wslconfig" == "true" ]]; then
run_wslconfig_generator
return
fi
[[ -t 0 ]] || return
while true; do
read -r -p "Run the local .wslconfig generator now? (y/n): " choice
case "$choice" in
[Yy]*)
run_wslconfig_generator
return
;;
[Nn]*)
return
;;
*)
warn "Enter y or n."
;;
esac
done
}
run_wslconfig_generator() {
local generator_path="${SCRIPT_DIR}/wslconfig-generator.sh"
[[ -x "$generator_path" || -f "$generator_path" ]] || fail "Unable to find ${generator_path}."
log "Launching the local .wslconfig generator..."
bash "$generator_path"
if [[ -f "${PWD}/.wslconfig" ]]; then
own_file_for_user "${PWD}/.wslconfig"
log ".wslconfig written to ${PWD}/.wslconfig"
fi
}
print_next_steps() {
cat <<EOF
Next steps:
1. Copy ${output_directory}/vmlinux somewhere stable on Windows, for example:
C:\\Users\\<your-user>\\WSL2\\vmlinux
2. Point your Windows .wslconfig file at that kernel path.
3. Run 'wsl --shutdown' from Windows to apply the new kernel.
EOF
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-V|--script-version)
printf '%s\n' "$SCRIPT_VERSION"
exit 0
;;
-v|--version)
[[ $# -ge 2 ]] || fail "Missing value for $1."
kernel_version="$2"
shift 2
;;
--series)
[[ $# -ge 2 ]] || fail "Missing value for $1."
[[ "$2" =~ ^[56]$ ]] || fail "Kernel series must be 5 or 6."
kernel_series="$2"
shift 2
;;
-o|--output-directory)
[[ $# -ge 2 ]] || fail "Missing value for $1."
output_directory="$2"
shift 2
;;
-c|--config)
[[ $# -ge 2 ]] || fail "Missing value for $1."
[[ -f "$2" ]] || fail "Config file not found: $2"
config_path="$(cd -- "$(dirname -- "$2")" && pwd)/$(basename -- "$2")"
shift 2
;;
--list-versions)
list_versions_only="true"
shift
;;
--generate-wslconfig)
generate_wslconfig="true"
shift
;;
--skip-wslconfig)
generate_wslconfig="false"
shift
;;
--keep-build-dir)
keep_build_dir="true"
shift
;;
*)
fail "Unknown option: $1"
;;
esac
done
if [[ -n "$kernel_version" && -n "$kernel_series" ]]; then
warn "Both --version and --series were supplied; --version takes precedence."
fi
}
main() {
parse_args "$@"
if [[ "$list_versions_only" == "true" ]]; then
list_available_versions
exit 0
fi
ensure_root
ensure_output_directory
apt_install_missing_packages
resolve_selected_version
prepare_workspace
download_source_archive "$selected_version"
extract_source_archive
build_kernel
copy_vmlinux
print_next_steps
prompt_wslconfig_generation
}
main "$@"