forked from slyfox1186/wsl2-kernel-build-script
-
Notifications
You must be signed in to change notification settings - Fork 26
/
build-kernel
272 lines (233 loc) · 6.93 KB
/
build-kernel
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
#!/bin/bash
# shellcheck disable=SC2016,SC2046,SC2066,SC2068,SC2086,SC2162,SC2317
clear
###########################################################################################################
##
## GitHub: https://github.com/slyfox1186/Windows-WSL2-Kernel-build-script
##
## Supported Distros: Debian-based ( Debian, Ubuntu, etc. )
##
## Supported architecture: x86_x64
##
## Purpose: Build the latest Microsoft WSL2 kernel release to link to your current distros.
##
## Install info: The new kernel will be located in the root build directory; the filename will be: vmlinux
##
## Place vmlinux into a folder inside your Windows "%USERPROFILE%" folder.
## I placed mine into the folder I created called: "%USERPROFILE%\WSL2\vmlinux"
##
## Create a file called "%USERPROFILE%\.wslconfig" and visit the below website for
## instructions on how to link the new kernel to your linux distros.
##
## https://learn.microsoft.com/en-us/windows/wsl/wsl-config
##
## Updated: 04.19.23
##
## Version: 1.0
##
###########################################################################################################
##
## define variables
##
script_ver='1.0'
##
## set the available cpu thread and core count for parallel processing (speeds up the build process)
##
if [ -f '/proc/cpuinfo' ]; then
cpu_threads="$(grep -c ^processor '/proc/cpuinfo')"
else
cpu_threads="$(nproc --all)"
fi
cpu_cores="$(grep ^cpu\\scores '/proc/cpuinfo' | uniq | awk '{print $4}')"
##
## define functions
##
exit_fn()
{
echo
echo 'The new kernel will be located in the root build directory'
echo
echo 'The filename will be called: vmlinux'
echo
echo 'Place the kernel into a folder inside your "%USERPROFILE%"'
echo
echo 'I placed mine into the folder I created called: "%USERPROFILE%\WSL2\vmlinux"'
echo
echo 'Create a file called "%USERPROFILE%\.wslconfig" and look up online how to link'
echo 'the new kernel you copied so that your WSL2 linux Distro loads it.'
echo
echo 'Make sure to star this repository to show your support!'
echo
echo 'https://github.com/slyfox1186/Windows-WSL2-Kernel-build-script/'
echo
exit 0
}
fail_fn()
{
echo
echo "$1"
echo
echo 'Please create a support ticket'
echo
echo 'https://github.com/slyfox1186/Windows-WSL2-Kernel-build-script/issues/'
echo
exit 1
}
cleanup_fn()
{
echo '=========================================='
echo ' Do you want to clean up the build files? '
echo '=========================================='
echo
echo '[1] Yes'
echo '[2] No'
echo
read -p 'Your choices are (1 or 2): ' cleanup_ans
if [ "$cleanup_ans" -eq '1' ]; then
remove_file "$0"
echo 'cleanup finished.'
exit_fn
elif [ "$cleanup_ans" -eq '2' ]; then
exit_fn
else
echo 'Bad user input'
echo
read -p 'Press enter to try again.'
clear
cleanup_fn
fi
}
make_dir()
{
remove_dir "$1"
if ! mkdir "$1"; then
printf "\n Failed to create dir %s" "$1"
echo
exit 1
fi
}
remove_file()
{
if [ -f "$1" ]; then
sudo rm -f "$1"
fi
}
remove_dir()
{
if [ -d "$1" ]; then
sudo rm -fr "$1"
fi
}
download()
{
dl_path="$PWD"
dl_file="${2:-"${1##*/}"}"
if [[ "$dl_file" =~ tar. ]]; then
target_dir="${dl_file%.*}"
target_dir="${3:-"${target_dir%.*}"}"
else
target_dir="${3:-"${dl_file%.*}"}"
fi
if [ ! -f "$dl_path/$dl_file" ]; then
echo "Downloading $1 as $dl_file"
if ! curl -Lso "$dl_path/$dl_file" "$1"; then
echo
echo "The script failed to download \"$1\" and will try again in 10 seconds"
sleep 10
echo
if ! curl -Lso "$dl_path/$dl_file" "$1"; then
echo
echo "The script failed to download \"$1\" two times and will exit the build"
echo
fail_fn
fi
fi
echo 'Download Completed'
echo
else
echo "$dl_file is already downloaded"
fi
make_dir "$dl_path/$target_dir"
if [ -n "$3" ]; then
if ! tar -xf "$dl_path/$dl_file" -C "$dl_path/$target_dir" &>/dev/null; then
fail_fn "Failed to extract $dl_file"
fi
else
if ! tar -xf "$dl_path/$dl_file" -C "$dl_path/$target_dir" --strip-components 1 &>/dev/null; then
fail_fn "Failed to extract $dl_file"
fi
fi
echo "File extracted: $dl_file"
cd "$dl_path/$target_dir" || fail_fn "Unable to change the working directory to $target_dir"
}
execute()
{
echo "$ $*"
if ! output=$("$@" 2>&1); then
fail_fn "Failed to Execute $*"
fi
}
build()
{
echo
echo "building $1 - version $2"
echo '===================================='
if [ -f "$PWD/$1.done" ]; then
if grep -Fx "$2" "$PWD/$1.done" 2>/dev/null; then
echo "$1 version $2 already built. Remove $PWD/$1.done lockfile to rebuild it."
return 1
elif $latest; then
echo "$1 is outdated and will be rebuilt using version $2"
return 0
else
echo "$1 is outdated, but will not be rebuilt. Pass in --latest to rebuild it or remove $PWD/$1.done lockfile."
return 1
fi
fi
return 0
}
build_done() { echo "$2" > "$PWD/$1.done"; }
installed() { return $(dpkg-query -W -f '${Status}\n' "$1" 2>&1 | awk '/ok installed/{print 0;exit}{print 1}'); }
clear
echo "WSL2-Custom-Kernel build script v$script_ver"
echo '======================================'
echo
echo "The script will utilize ( $cpu_threads cpu cores ) for parallel processing to accelerate the build speed."
##
## required apt packages
##
build_pkgs_fn()
{
echo
echo 'Installing required apt packages'
echo '=================================='
pkgs=(build-essential curl flex bison dwarves gcc g++ gcc-12 g++-12 jq libssl-dev libelf-dev bc)
for pkg in ${pkgs[@]}
do
if ! installed "$pkg"; then
missing_pkgs+=" $pkg"
fi
done
if [ -n "$missing_pkgs" ]; then
for pkg in "$missing_pkgs"
do
if sudo apt -y install $pkg; then
echo
echo 'The required apt packages were installed.'
echo
else
fail_fn 'The required apt packages failed to install'
fi
done
echo 'The required apt packages are already installed.'
fi
}
# install required apt packages
build_pkgs_fn
VERSION=6.6.36.6
if build 'wsl2-linux-kernel' "linux-msft-wsl-${VERSION}"; then
download "https://api.github.com/repos/microsoft/WSL2-Linux-Kernel/tarball/linux-msft-wsl-${VERSION}" "linux-msft-wsl-${VERSION}.tar.gz"
echo 'yes' | make KCONFIG_CONFIG=../config-wsl -j "$cpu_cores"
build_done 'wsl2-linux-kernel' "linux-msft-wsl-${VERSION}"
fi
exit_fn