forked from FOGProject/fos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·449 lines (437 loc) · 13.6 KB
/
build.sh
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
#!/bin/bash
[[ -z $KERNEL_VERSION ]] && KERNEL_VERSION='5.15.93'
[[ -z $BUILDROOT_VERSION ]] && BUILDROOT_VERSION='2022.02.9'
Usage() {
echo -e "Usage: $0 [-knfvh?] [-a x64]"
echo -e "\t\t-a --arch [x86|x64|arm64] (optional) pick the architecture to build. Default is to build for all."
echo -e "\t\t-f --filesystem-only (optional) Build the FOG filesystem but not the kernel."
echo -e "\t\t-k --kernel-only (optional) Build the FOG kernel but not the filesystem."
echo -e "\t\t-p --path (optional) Specify a path to download and build the sources."
echo -e "\t\t-n --noconfirm (optional) Build systems without confirmation."
echo -e "\t\t-h --help -? Display this message."
}
[[ -n $arch ]] && unset $arch
optspec="?hknfh-:a:v:p:"
while getopts "$optspec" o; do
case "${o}" in
-)
case $OPTARG in
help)
Usage
exit 0
;;
arch)
val="${!OPTIND}"; OPTIND=$(($OPTIND + 1))
if [[ -z $val ]]; then
echo "Option --${OPTARG} requires a value"
Usage
exit 2
fi
hasa=1
arch=$val
;;
arch=*)
val=${OPTARG#*=}
opt=${OPTARG%=$val}
if [[ -z $val ]]; then
echo "Option --${opt} requires a value"
Usage
exit 2
fi
hasa=1
arch=$val
;;
path)
val="${!OPTIND}"; OPTIND=$(($OPTIND + 1))
if [[ -z $val ]]; then
echo "Option --${OPTARG} requires a value"
Usage
exit 2
fi
buildPath=${val}
;;
path=*)
val=${OPTARG#*=}
opt=${OPTARG%=$val}
if [[ -z $val ]]; then
echo "Option --${opt} requires a value"
Usage
exit 2
fi
buildPath=${val}
;;
kernel-only)
buildKernelOnly="y"
;;
filesystem-only)
buildFSOnly="y"
;;
noconfirm)
confirm="n"
;;
*)
if [[ $OPTERR == 1 && ${optspec:0:1} != : ]]; then
echo "Unknown option: --${OPTARG}"
Usage
exit 1
fi
;;
esac
;;
h|'?')
Usage
exit 0
;;
a)
hasa=1
arch=${OPTARG}
;;
p)
buildPath=${OPTARG}
;;
k)
buildKernelOnly="y"
;;
f)
buildFSOnly="y"
;;
n)
confirm="n"
;;
:)
echo "Option -${OPTARG} requires a value"
Usage
exit 2
;;
*)
if [[ ${OPTERR} -eq 1 && ${optspec:0:1} != : ]]; then
echo "Unknown option: -${OPTARG}"
Usage
exit 1
fi
;;
esac
done
debDeps="git meld build-essential rsync libncurses5-dev bison flex gcc-aarch64-linux-gnu libelf-dev"
rhelDeps="git meld rsync ncurses-devel bison flex gcc-aarch64-linux-gnu elfutils-libelf-devel"
[[ -z $arch ]] && arch="x64 x86 arm64"
[[ -z $buildPath ]] && buildPath=$(dirname $(readlink -f $0))
[[ -z $confirm ]] && confirm="y"
echo "Checking packages needed for building"
if grep -iqE "Debian|Ubuntu" /proc/version ; then
os="deb"
eabi="eabi"
pkgmgr="dpkg -l"
elif grep -iqE "Red Hat|Redhat" /proc/version ; then
os="rhel"
eabi=""
pkgmgr="rpm -qa"
fi
osDeps=${os}Deps
for pkg in ${!osDeps}
do
$pkgmgr | grep -q "$pkg" >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo " * Package $pkg missing!"
fail=1
fi
done
if [[ $fail == 1 ]]; then
echo "Package(s) missing, can't build, exiting now."
exit 1
fi
cd $buildPath || exit 1
function buildFilesystem() {
local arch="$1"
if [[ -f patch/filesystem/fs.patch ]]; then
dots " * Applying filesystem patch"
echo
patch -p1 < patch/filesystem/fs.patch
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
else
echo " * WARNING: Did not find a patch file building filesystem without patches!"
fi
brURL="https://buildroot.org/downloads/buildroot-$BUILDROOT_VERSION.tar.xz"
echo "Preparing buildroot $BUILDROOT_VERSION on $arch build:"
if [[ ! -d fssource$arch ]]; then
if [[ ! -f buildroot-$BUILDROOT_VERSION.tar.xz ]]; then
dots "Downloading buildroot source package"
wget -q $brURL && echo "Done"
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
fi
dots "Extracting buildroot sources"
tar xJf buildroot-$BUILDROOT_VERSION.tar.xz
mv buildroot-$BUILDROOT_VERSION fssource$arch
echo "Done"
fi
dots "Preparing code"
cd fssource$arch
if [[ ! -f .packConfDone ]]; then
cat ../Buildroot/package/newConf.in >> package/Config.in
touch .packConfDone
fi
rsync -avPrI ../Buildroot/ . > /dev/null
sed -i "s/^export initversion=[0-9][0-9]*$/export initversion=$(date +%Y%m%d)/" board/FOG/FOS/rootfs_overlay/usr/share/fog/lib/funcs.sh
if [[ ! -f .config ]]; then
cp ../configs/fs$arch.config .config
case "${arch}" in
x64)
make oldconfig
;;
x86)
make ARCH=i486 oldconfig
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
;;
*)
make oldconfig
;;
esac
fi
echo "Done"
if [[ $confirm != n ]]; then
read -p "We are ready to build. Would you like to edit the config file [y|n]?" config
if [[ $config == y ]]; then
case "${arch}" in
x64)
make menuconfig
;;
x86)
make ARCH=i486 menuconfig
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
;;
*)
make menuconfig
;;
esac
else
echo "Ok, running make oldconfig instead to ensure the config is clean."
case "${arch}" in
x64)
make oldconfig
;;
x86)
make ARCH=i486 oldconfig
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
;;
*)
make oldconfig
;;
esac
fi
read -p "We are ready to build are you [y|n]?" ready
if [[ $ready == n ]]; then
echo "Nothing to build!? Skipping."
cd ..
return
fi
fi
bash -c "while true; do echo \$(date) - building ...; sleep 30s; done" &
PING_LOOP_PID=$!
case "${arch}" in
x64)
make >buildroot$arch.log 2>&1
status=$?
;;
x86)
make ARCH=i486 >buildroot$arch.log 2>&1
status=$?
;;
arm64)
make ARCH=aarch64 CROSS_COMPILE=aarch64-linux-gnu- >buildroot$arch.log 2>&1
status=$?
;;
*)
make >buildroot$arch.log 2>&1
status=$?
;;
esac
kill $PING_LOOP_PID
[[ $status -gt 0 ]] && tail buildroot$arch.log && exit $status
cd ..
[[ ! -d dist ]] && mkdir dist
cd dist
case "${arch}" in
x64)
compiledfile="../fssource$arch/output/images/rootfs.ext2.xz"
initfile='init.xz'
;;
x86)
compiledfile="../fssource$arch/output/images/rootfs.ext2.xz"
initfile='init_32.xz'
;;
arm64)
compiledfile="../fssource$arch/output/images/rootfs.cpio.gz"
initfile='arm_init.cpio.gz'
;;
esac
[[ ! -f $compiledfile ]] && echo 'File not found.' || cp $compiledfile $initfile && sha256sum $initfile > ${initfile}.sha256
cd ..
}
function buildKernel() {
local arch="$1"
kernelURL="https://www.kernel.org/pub/linux/kernel/v${KERNEL_VERSION:0:1}.x/linux-$KERNEL_VERSION.tar.xz"
echo "Preparing kernel $KERNEL_VERSION on $arch build:"
[[ -d kernelsource$arch ]] && rm -rf kernelsource$arch
if [[ ! -f linux-$KERNEL_VERSION.tar.xz ]]; then
dots "Downloading kernel source"
wget -q $kernelURL && echo "Done"
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
fi
dots "Extracting kernel source"
tar xJf linux-$KERNEL_VERSION.tar.xz
mv linux-$KERNEL_VERSION kernelsource$arch
echo "Done"
dots "Preparing kernel source"
cd kernelsource$arch
make mrproper
cp ../configs/kernel$arch.config .config
echo "Done"
if [[ -f ../patch/kernel/linux.patch ]]; then
dots " * Applying patch"
echo
patch -p1 < ../patch/kernel/linux.patch
if [[ $? -ne 0 ]]; then
echo "Failed"
exit 1
fi
else
echo " * WARNING: Did not find a patch file building vanilla kernel without patches!"
fi
dots "Cloning Linux firmware repository"
git clone git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git >/dev/null 2>&1
echo "Done"
if [[ $confirm != n ]]; then
read -p "We are ready to build. Would you like to edit the config file [y|n]?" config
if [[ $config == y ]]; then
case "${arch}" in
x64)
make menuconfig
;;
x86)
make ARCH=i386 menuconfig
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
;;
*)
make menuconfig
;;
esac
else
echo "Ok, running make oldconfig instead to ensure the config is clean."
case "${arch}" in
x64)
make oldconfig
;;
x86)
make ARCH=i386 oldconfig
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
;;
*)
make oldconfig
;;
esac
fi
read -p "We are ready to build are you [y|n]?" ready
if [[ $ready == y ]]; then
echo "This make take a long time. Get some coffee, you'll be here a while!"
case "${arch}" in
x64)
make -j $(nproc) bzImage
status=$?
;;
x86)
make ARCH=i386 -j $(nproc) bzImage
status=$?
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j $(nproc) Image
status=$?
;;
*)
make -j $(nproc) bzImage
status=$?
;;
esac
else
echo "Nothing to build!? Skipping."
cd ..
return
fi
[[ $status -gt 0 ]] && exit $status
else
case "${arch}" in
x64)
make oldconfig
make -j $(nproc) bzImage
status=$?
;;
x86)
make ARCH=i386 oldconfig
make ARCH=i386 -j $(nproc) bzImage
status=$?
;;
arm64)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j $(nproc) Image
status=$?
;;
*)
make oldconfig
make -j $(nproc) bzImage
status=$?
;;
esac
fi
[[ $status -gt 0 ]] && exit $status
cd ..
mkdir -p dist
cd dist
case "$arch" in
x64)
compiledfile="../kernelsource$arch/arch/x86/boot/bzImage"
kernelfile='bzImage'
;;
x86)
compiledfile="../kernelsource$arch/arch/x86/boot/bzImage"
kernelfile='bzImage32'
;;
arm64)
compiledfile="../kernelsource$arch/arch/$arch/boot/Image"
kernelfile='arm_Image'
;;
esac
[[ ! -f $compiledfile ]] && echo 'File not found.' || cp $compiledfile $kernelfile && sha256sum $kernelfile > ${kernelfile}.sha256
cd ..
}
dots() {
local pad=$(printf "%0.1s" "."{1..60})
printf " * %s%*.*s" "$1" 0 $((60-${#1})) "$pad"
return 0
}
for buildArch in $arch
do
if [[ -z $buildKernelOnly ]]; then
buildFilesystem $buildArch
fi
if [[ -z $buildFSOnly ]]; then
buildKernel $buildArch
fi
done