-
Notifications
You must be signed in to change notification settings - Fork 1
/
puipui-linux-tool
executable file
·352 lines (293 loc) · 8.63 KB
/
puipui-linux-tool
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
#!/bin/bash
# vim: set ts=4:
#---help---
# Usage: puipui-linux-tool [options]
#
# This script builds puipui linux kernel and it's initramfs.
#
# Options:
# -u Only updates linux kernel config files. it does not build kernel or initramfs.
#---help---
set -eu
version=1.0.3
srcdir="$PWD"
target_arch="aarch64 x86_64"
kernver=6.11.5
busyboxver=1.36.1
dropbearver=2024.86
socatver=1.8.0.1
name="PUI PUI Linux"
#if [ -z "$SOURCE_DATE_EPOCH" ]; then
SOURCE_DATE_EPOCH=$(date -u "+%s")
#fi
msg() { echo -e "\033]2; $*\007\n=== $*"; }
usage() {
sed -En '/^#---help---/,/^#---help---/p' "$0" | sed -E 's/^# ?//; 1d;$d;'
}
fetch() {
local url="$1"
local archive=${url##*/}
(cd $srcdir && curl -OL $url)
tar -xvf "$srcdir"/"$archive" -C "$srcdir"
}
_kernelarch() {
local arch="$1"
if [ $arch == aarch64 ]; then
echo arm64
elif [ $arch == x86_64 ]; then
echo x86
else echo "Unknown TARGET $arch"; exit 1;
fi
}
_kconfig() {
local fname="$1"
echo "kconfig/$fname"
}
_builddir() {
local arch="$1"
local dirname="$2"
echo "$srcdir"/build-$arch/$dirname
}
_artifact() {
local arch="$1"
_builddir $arch "artifact"
}
_cross_compile_target() {
local arch="$1"
echo $arch-linux-musl-
}
_musl_path() {
local arch="$1"
local machine_arch="$(uname -m)"
if [ $arch == $machine_arch ]; then
echo $arch-linux-musl-native
else
echo $arch-linux-musl-cross
fi
}
setup_musl_path() {
local arch="$1"
local musl_path="$(_musl_path $arch)"
local musl_abs_path="$srcdir/$musl_path"
local musl_prefix="$arch-linux-musl-"
local machine_arch="$(uname -m)"
if [ "aarch64" == $machine_arch ]; then
fetch "https://github.com/Code-Hex/musl-cross-make-arm64/releases/download/v0.0.4/${musl_path}.tar.gz"
tar xvf "$musl_abs_path.tar.gz"
else
fetch "https://musl.cc/${musl_path}.tgz"
tar xvf "$musl_abs_path.tgz"
ln -s $musl_abs_path/bin/{strip,"${musl_prefix}strip"}
fi
}
_prepareconfig() {
local _arch="$1"
local _config=$(_kconfig $_arch.config)
local _builddir=$(_builddir $_arch linux-kernel)
mkdir -p "$_builddir"
local PATH="$srcdir/$(_musl_path $_arch)/bin:$PATH"
cp "$srcdir"/$_config "$_builddir"/.config
msg "Configuring $name kernel ($_arch)"
make -C "$srcdir"/linux-$kernver \
CROSS_COMPILE=$(_cross_compile_target $_arch) \
O="$_builddir" \
ARCH="$(_kernelarch $_arch)" \
olddefconfig
}
build_kern() {
unset LDFLAGS
export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"
for _arch in $target_arch; do
_prepareconfig "$_arch"
done
for _arch in $target_arch; do
msg "Building $_arch $name kernel"
# Target validation
if [ $_arch == aarch64 ]; then
VMLINUX=arch/arm64/boot/Image.gz
elif [ $_arch == x86_64 ]; then
VMLINUX=arch/x86/boot/bzImage
else echo "Unknown TARGET $_arch"; exit 1;
fi
local PATH="$srcdir/$(_musl_path $_arch)/bin:$PATH"
cd $(_builddir $_arch linux-kernel)
make CROSS_COMPILE=$(_cross_compile_target $_arch) \
ARCH="$(_kernelarch $_arch)" \
AWK="${AWK:-mawk}" \
KBUILD_BUILD_VERSION="$version-PUI PUI!" \
KBUILD_BUILD_HOST="molbuilder" \
KBUILD_BUILD_USER="codehex" \
-j $(nproc)
local artifactdir=$(_artifact $_arch)
mkdir -p $artifactdir
cp "$VMLINUX" "$artifactdir"
done
}
build_initramfs() {
unset LDFLAGS
for _arch in $target_arch; do
msg "Building $_arch busybox and initramfs"
local _fsdir=$(_builddir $_arch fs)
local _builddir=$(_builddir $_arch busybox)
# Copy original fs to build target
rm -rf $_fsdir
cp -r "$srcdir"/fs $_fsdir
mkdir -p "$_fsdir"/bin
# Install dropbear
install_dropbear $_arch $_fsdir
# Install socat
install_socat $_arch $_fsdir
# Install static curl binary to bin directory
local _curl_tool_dir=$(_builddir $_arch curl)
install -Dm0755 "$_curl_tool_dir"/curl "$_fsdir"/bin
# Copy ca-certificates for using curl (https)
mkdir -p "$_fsdir"/etc/ssl/certs
cp /etc/ssl/certs/ca-certificates.crt "$_fsdir"/etc/ssl/certs
# Remove man pages
rm -rf "$_fsdir"/usr/share/man
# Make build directory
rm -rf $_builddir
mkdir -p $_builddir
cp "$srcdir"/busybox_config/config "$_builddir"/.config
local PATH="$srcdir/$(_musl_path $_arch)/bin:$PATH"
make -C "$srcdir"/busybox-$busyboxver \
CROSS_COMPILE=$(_cross_compile_target $_arch) \
O="$_builddir" \
oldconfig
# Change workspace to busybox target directory
cd $_builddir
make CROSS_COMPILE=$(_cross_compile_target $_arch) \
LDFLAGS="-static" \
ARCH="$(_kernelarch $_arch)" \
CONFIG_PREFIX="$_fsdir" || exit 1
install -Dm0755 busybox "$_fsdir"/bin
cd $_fsdir
find . | cpio -o -H newc | gzip -9 > $(_artifact $_arch)/initramfs.cpio.gz
done
}
download_curl() {
local _mirror="https://github.com/moparisthebest/static-curl/releases/download/v7.86.0"
for _arch in $target_arch; do
local _curl_dir=$(_builddir $_arch curl)
local _target="curl-$([ $_arch == x86_64 ] && echo amd64 || echo aarch64)"
local _curl_url="$_mirror/$_target"
if ! [ -f "$_curl_dir"/"$_target" ]; then
mkdir -p $_curl_dir
(cd $_curl_dir && curl -OL $_curl_url && cp $_target curl)
fi
done
}
# install to fs on each arch
install_dropbear() {
local _arch="$1"
local _fsdir="$2"
local PATH="$srcdir/$(_musl_path $_arch)/bin:$PATH"
local _target=$(_cross_compile_target $_arch)
local CC="${_target}cc"
local AR="${_target}ar"
local RANLIB="${_target}ranlib"
local STRIP="${_target}strip"
local _builddir=$(_builddir $_arch dropbear)
local _dropbear_src="$srcdir"/"dropbear-${dropbearver}"
mkdir -p "$_builddir"
(cd $_dropbear_src && ./configure --build=$_arch -prefix=$_fsdir --disable-zlib --enable-static --disable-loginfunc --disable-wtmp)
cd $_builddir
make -C $_dropbear_src \
CC=$CC AR=$AR RANLIB=$RANLIB STRIP=$STRIP \
PROGRAMS="dropbear" STATIC=1 clean strip install
}
install_socat() {
local _arch="$1"
local _fsdir="$2"
local PATH="$srcdir/$(_musl_path $_arch)/bin:$PATH"
local _target=$(_cross_compile_target $_arch)
local CC="${_target}cc"
local AR="${_target}ar"
local RANLIB="${_target}ranlib"
local STRIP="${_target}strip"
local _builddir=$(_builddir $_arch socat)
local _socat_src="$srcdir"/"socat-${socatver}"
mkdir -p "$_builddir"
# socat-1.7.4.3 - fdname.c:(.text+<snip>): undefined reference to getprotobynumber_r
# https://git.alpinelinux.org/aports/commit/main/socat/APKBUILD?id=5edc9195355ced3db991c1a7cda5648d52019b11
(
cd $_socat_src && \
CFLAGS="-static -Os -s" ./configure \
--build=$_arch \
--prefix=$_fsdir \
--disable-man \
--disable-openssl \
--disable-readline \
--disable-libwrap \
--disable-sycls \
--disable-filan && \
sed -Ei 's/#define\s+HAVE_GETPROTOBYNUMBER_R.*/#define HAVE_GETPROTOBYNUMBER_R 0/' config.h && \
sed -Ei 's/#define\s+HAVE_(.*)64.*/#define HAVE_\164 0/' config.h
)
cd $_builddir
make -C $_socat_src \
CC=$CC AR=$AR RANLIB=$RANLIB STRIP=$STRIP \
clean install
}
pack_artifacts() {
cd $srcdir
for _arch in $target_arch; do
msg "Packing artifacts for $_arch"
local _artifactdir=$(_artifact $_arch)
tar -czvf "puipui_linux_v${version}_$_arch.tar.gz" -C $_artifactdir .
done
}
updateconfigs() {
for _arch in $target_arch; do
msg "Update $name kernel config ($_arch)"
local _config=$(_kconfig $_arch.config)
local _builddir=$(_builddir $_arch linux-kernel)
mkdir -p "$_builddir"
local actions="listnewconfig oldconfig"
if ! [ -f "$_builddir"/.config ]; then
cp "$srcdir"/$_config "$_builddir"/.config
actions="olddefconfig"
env | grep ^CONFIG_ >> "$_builddir"/.config || true
fi
make -j1 -C "$srcdir"/linux-$kernver \
O="$_builddir" \
ARCH="$(_kernelarch $_arch)" \
$actions savedefconfig
cp "$_builddir"/defconfig "$srcdir"/$_config
done
}
while getopts ':uhv' OPTION; do
case "$OPTION" in
u) updateconfigs || true; exit 0;;
h) usage; exit 0;;
v) echo "puipui-linux-tool $version"; exit 0;;
esac
done
if ! [ -d linux-"$kernver" ]; then
msg "Download linux kernel $kernver"
fetch "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${kernver}.tar.xz"
fi
if ! [ -d busybox-"$busyboxver" ]; then
msg "Download busybox $busyboxver"
fetch "https://busybox.net/downloads/busybox-${busyboxver}.tar.bz2"
fi
if ! [ -d dropbear-"$dropbearver" ]; then
msg "Download dropbear $dropbearver"
fetch "https://matt.ucc.asn.au/dropbear/releases/dropbear-${dropbearver}.tar.bz2"
fi
if ! [ -d socat-"$socatver" ]; then
msg "Download socat $socatver"
fetch "http://www.dest-unreach.org/socat/download/socat-${socatver}.tar.gz"
fi
if ! [ -d $(_musl_path "aarch64") ]; then
msg "Download aarch64-linux-musl"
setup_musl_path "aarch64"
fi
if ! [ -d $(_musl_path "x86_64") ]; then
msg "Download x86_64-linux-musl"
setup_musl_path "x86_64"
fi
build_kern
download_curl
build_initramfs
pack_artifacts