-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathof-builder.sh
executable file
·352 lines (287 loc) · 10.8 KB
/
of-builder.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
#!/usr/bin/env bash
## of-builder.sh v1.44 (21st September 2024)
## Builds kernels, modules and images.
if [ $# -lt 5 ]; then
echo "Usage: $0 <distro> <codename> <apt> <kernel> <output>"
echo
echo " distro : Distribution name, eg. \"debian\" or \"ubuntu\"."
echo " codename : Release codename, eg. \"bullseye\" or \"bionic\"."
echo " apt : An HTTP(S) URL link to the apt source for your chosen distribution."
echo " kernel : An HTTPS URL link to the kernel source in .tar.xz format."
echo " output : Local path for output."
echo
exit 1
fi
## Configurable Bits
OURKERNVER="op"
GITREPOURL="https://github.com/birdslikewires"
GITREPOKER="openframe-kernel"
GITREPOLIN="openframe-linux"
COREDIVIDER=1
WEBUSER="www-data"
## Everything Else
THISSCRIPTPATH="$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P)"
STARTTIME=`date +'%Y-%m-%d-%H%M'`
IDISTNAME="$1"
ICODENAME="$2"
IDOWNLURL="$3"
KBRANCH="$4\."
OUTPUTPATH="$5"
GITKERNELOWNER=$(stat -c '%U' $THISSCRIPTPATH/../$GITREPOKER)
GITLINUXOOWNER=$(stat -c '%U' $THISSCRIPTPATH/../$GITREPOLIN)
GITKERNELUPDATED=0
GITLINUXOUPDATED=0
# Use the URL we're given for the kernel, or figure out the latest archive of that branch to download.
KDOWNLOAD=""
if [[ "$KBRANCH" =~ "https://" ]]; then
KDOWNLOAD="$KBRANCH"
else
KARCHIVES=`curl --silent https://www.kernel.org/index.html`
KDOWNLOAD=`echo "$KARCHIVES" | grep -v "*-rc*" | grep ".xz" | grep -m 1 "linux-$KBRANCH" | awk -F\" {'print $2'}`
fi
if [[ "$KDOWNLOAD" = "" ]]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: Kernel branch $KBRANCH was not found as stable or longterm on the kernel.org homepage."
exit 1
fi
# Check whether I've been removed from my repo or not. Die if I have.
if [[ ! -d "$THISSCRIPTPATH/../$GITREPOLIN" ]]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: You seem to be running me outside of my repo. I'm not much use without the rest of $GITREPOURL/$GITREPOLIN."
exit 1
fi
# Check whether we've got the kernel repo available, otherwise kernel builds will obviously fail.
if [[ ! -d "$THISSCRIPTPATH/../$GITREPOKER" ]]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: You're going to need $GITREPOURL/$GITREPOKER as well. Cloning..."
git clone "$GITREPOURL/$GITREPOKER" "$THISSCRIPTPATH/../$GITREPOKER"
fi
sync
sleep 2
KFILENAME=`echo "$KDOWNLOAD" | sed 's:.*/::'`
KLATESTMAJVER=`echo "$KFILENAME" | awk -F\- {'print $2'} | awk -F\. {'print $1'}`
KLATESTMIDVER=`echo "$KFILENAME" | awk -F\- {'print $2'} | awk -F\. {'print $2'}`
KLATESTMINVER=`echo "$KFILENAME" | awk -F\- {'print $2'} | awk -F\. {'print $3'}`
KOURNAME="$KLATESTMAJVER.$KLATESTMIDVER.$KLATESTMINVER$OURKERNVER"
KOURBUILD="linux-$KLATESTMAJVER.$KLATESTMIDVER.$KLATESTMINVER"
KDLPATH="$OUTPUTPATH/openframe/kernel/$KLATESTMAJVER.$KLATESTMIDVER/$KOURNAME"
[ -d $KDLPATH ] && [ $GITKERNELUPDATED -eq 0 ] && KBUILDIT=0 || KBUILDIT=1
KCONFIGFILE=`ls $THISSCRIPTPATH/../$GITREPOKER/configs | grep "$KLATESTMAJVER.$KLATESTMIDVER"`
if [[ $KCONFIGFILE == "" ]]; then
echo
echo "`date +'%Y-%m-%d %H:%M:%S'`: Build aborted, no config file found for this kernel."
exit 1
fi
if [[ "$KBUILDIT" == 0 ]]; then
KSTALE=$(find "$KDLPATH" -maxdepth 0 -mtime +30)
if [ "$KSTALE" != "" ]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: Kernel $KOURNAME has gone stale. Time to bake a new one!"
/bin/mv "$KDLPATH" "$KDLPATH-$(date -d "30 days ago" +'%Y-%m-%d')"
KBUILDIT=1
fi
fi
IDLPATH="$OUTPUTPATH/openframe/images/${IDISTNAME,,}/${ICODENAME,,}/$KLATESTMAJVER.$KLATESTMIDVER/$KOURNAME"
[ -d $IDLPATH ] && [ $GITLINUXOUPDATED -eq 0 ] && IBUILDIT=0 || IBUILDIT=1
if [[ "$IBUILDIT" == 0 ]]; then
ISTALE=$(find "$IDLPATH" -maxdepth 0 -mtime +30)
if [ "$ISTALE" != "" ]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: Image ${IDISTNAME^} ${ICODENAME^} $KOURNAME has gone stale. Time to bake a new one!"
/bin/mv "$IDLPATH" "$IDLPATH-$(date -d "30 days ago" +'%Y-%m-%d')"
IBUILDIT=1
fi
fi
## Work To Do!
cleanup() {
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Cleaning up..."
rm -rf ./$KOURBUILD*
rm -rf ./*.deb
rm -rf ./*.img*
rm -rf ./tmp
echo " done."
}
if [[ "$KBUILDIT" == 0 ]]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: Kernel $KOURNAME has already been processed."
else
echo "`date +'%Y-%m-%d %H:%M:%S'`: Building $KOURNAME kernel..."
echo
if [ ! -f "$KFILENAME" ]; then
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Downloading $KFILENAME..."
wget --quiet "$KDOWNLOAD"
echo " done."
else
echo "`date +'%Y-%m-%d %H:%M:%S'`: Kernel archive $KFILENAME found."
fi
if [ ! -f "$KFILENAME" ]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: Download appears to have failed."
exit 1
fi
if [ -d "$KOURBUILD" ]; then
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Removing previous build cruft..."
rm -rf "$KOURBUILD"
rm -rf *.deb
rm -rf ./tmp/*
echo " done."
fi
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Decompressing $KFILENAME..."
tar xJf $KFILENAME
echo " done."
echo "`date +'%Y-%m-%d %H:%M:%S'`: Applying OpenFrame kernel patches..."
for p in `ls $THISSCRIPTPATH/../$GITREPOKER/patches/$KLATESTMAJVER.$KLATESTMIDVER`; do
patch -f -p1 -d "$KOURBUILD" < "$THISSCRIPTPATH/../$GITREPOKER/patches/$KLATESTMAJVER.$KLATESTMIDVER/$p"
done
echo
# This checks through the exit codes so far and kills us if any have been greater than zero.
RCS=${PIPESTATUS[*]}; RC=0; for i in ${RCS}; do RC=$(($i > $RC ? $i : $RC)); done
if [[ $RC -gt 0 ]]; then
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Build failed, check the log."
cleanup
exit $RC
fi
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Applying extraversion to makefile..."
KMAKEFILE=`cat "$KOURBUILD/Makefile"`
if [[ ! "$KMAKEFILE" =~ "EXTRAVERSION = $OURKERNVER" ]]; then
sed -i "s/EXTRAVERSION =/EXTRAVERSION = $OURKERNVER/g" "$KOURBUILD/Makefile"
fi
echo " done."
echo "`date +'%Y-%m-%d %H:%M:%S'`: Updating config file with new defaults..."
cp "$THISSCRIPTPATH/../$GITREPOKER/configs/$KCONFIGFILE" "$KOURBUILD/.config"
cd "$KOURBUILD"
make olddefconfig
echo
echo "Go! Go! Go!"
echo
make -j$((`nproc`/$COREDIVIDER)) deb-pkg
RCS=${PIPESTATUS[*]}; RC=0; for i in ${RCS}; do RC=$(($i > $RC ? $i : $RC)); done
if [[ $RC -gt 0 ]]; then
echo
echo "`date +'%Y-%m-%d %H:%M:%S'`: Build failed, check the log."
cd ..
cleanup
exit $RC
fi
echo
echo "`date +'%Y-%m-%d %H:%M:%S'`: Kernel $KOURNAME build succeeded!"
echo
if [ -d $KDLPATH ]; then
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Removing outdated $KOURNAME kernel..."
rm -rf $KDLPATH
echo " done."
fi
mkdir -p $KDLPATH
cd ..
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Moving kernel $KOURNAME packages..."
mv *.deb $KDLPATH
echo " done."
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Copying kernel $KOURNAME config file..."
cp "$KOURBUILD/.config" "$KDLPATH/$KOURNAME.config"
echo " done."
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Copying kernel $KOURNAME patches..."
cp -R "$THISSCRIPTPATH/../$GITREPOKER/patches/$KLATESTMAJVER.$KLATESTMIDVER" "$KDLPATH/patches"
echo " done."
echo
cleanup
echo
echo "`date +'%Y-%m-%d %H:%M:%S'`: Kernel compiled, packages ready."
echo
echo
fi
if [ -f $KDLPATH/modules-$KOURNAME.tgz ]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: Modules for kernel $KOURNAME have already been processed."
else
echo "`date +'%Y-%m-%d %H:%M:%S'`: Building $KOURNAME kernel companion modules..."
[ ! -d /lib/modules/$KOURNAME ] && dpkg -i $KDLPATH/linux-headers*
if [[ "$KLATESTMAJVER" -lt 4 ]]; then
## RTL8821CU Wireless Support
[ -d rtl8821cu_wlan ] && rm -rf rtl8821cu_wlan
git clone https://github.com/andydvsn/rtl8821cu_wlan.git
mkdir -p lib/modules/$KOURNAME/kernel/drivers/net/wireless
sed -i 's/KVER := $(shell uname -r)/KVER := '$KOURNAME'/g' "./rtl8821cu_wlan/Makefile"
cd rtl8821cu_wlan
make -j`nproc`
cd ..
cp rtl8821cu_wlan/rtl8821cu.ko lib/modules/$KOURNAME/kernel/drivers/net/wireless
rm -rf rtl8821cu_wlan
## RTL8821CU Bluetooth Support
[ -d rtl8821cu_bt ] && rm -rf rtl8821cu_bt
git clone https://github.com/andydvsn/rtl8821cu_bt.git
mkdir -p lib/modules/$KOURNAME/kernel/drivers/bluetooth
cd rtl8821cu_bt/bluetooth_usb_driver
make -C /lib/modules/$KOURNAME/build M=`pwd` modules
cd ../..
cp rtl8821cu_bt/bluetooth_usb_driver/rtk_btusb.ko lib/modules/$KOURNAME/kernel/drivers/bluetooth
rm -rf rtl8821cu_bt
fi
## Crystal HD Driver
# [ -d crystalhd ] && rm -rf crystalhd
# git clone https://github.com/birdslikewires/crystalhd.git
# mkdir -p etc/udev/rules.d
# mkdir -p lib/udev/rules.d
# mkdir -p lib/modules/$KOURNAME/kernel/drivers/video/broadcom
# cd crystalhd/driver/linux
# autoconf
# ./configure
# make -C /lib/modules/$KOURNAME/build M=`pwd`
# cd ../../..
# cp crystalhd/driver/linux/20-crystalhd.rules etc/udev/rules.d
# cp crystalhd/driver/linux/20-crystalhd.rules lib/udev/rules.d
# cp crystalhd/driver/linux/crystalhd.ko lib/modules/$KOURNAME/kernel/drivers/video/broadcom
# rm -rf crystalhd
## Firmware Hub Module
[ -d fh ] && rm -rf fh
git clone https://github.com/andydvsn/fh.git
mkdir -p lib/modules/$KOURNAME/extra
cd fh
make -C /lib/modules/$KOURNAME/build M=`pwd` modules
cd ..
cp fh/fh.ko lib/modules/$KOURNAME/extra
rm -rf fh
echo
echo "`date +'%Y-%m-%d %H:%M:%S'`: Companion modules done, but check the log above. Compressing and moving..."
#tar zcvf modules-$KOURNAME.tgz etc lib
tar zcvf modules-$KOURNAME.tgz lib
rm -rf lib
mv modules-$KOURNAME.tgz $KDLPATH
echo
cleanup
echo
echo "`date +'%Y-%m-%d %H:%M:%S'`: Companion modules ready."
echo
echo
fi
if [[ "$IBUILDIT" == 0 ]]; then
echo "`date +'%Y-%m-%d %H:%M:%S'`: Image for kernel $KOURNAME has already been processed."
else
echo "`date +'%Y-%m-%d %H:%M:%S'`: Building ${IDISTNAME^} ${ICODENAME^} $KOURNAME image..."
echo
rm -rf ./*.img*
$THISSCRIPTPATH/of-imgcreate.sh "$(echo ${ICODENAME,,} | head -c 3)" ext2 1 uni 43 "${IDISTNAME,,} ${ICODENAME,,}" "$THISSCRIPTPATH/../$GITREPOLIN/overlay-${IDISTNAME,,}-${ICODENAME,,}" "$KDLPATH" "$IDOWNLURL"
# This checks through the exit codes so far and kills us if any have been greater than zero.
RCS=${PIPESTATUS[*]}; RC=0; for i in ${RCS}; do RC=$(($i > $RC ? $i : $RC)); done
if [[ $RC -gt 0 ]]; then
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Build failed, check the log."
cleanup
exit $RC
fi
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Compressing and checkumming..."
IBUILTIT=`ls | grep -m 1 .img`
gzip $IBUILTIT
md5sum $IBUILTIT.gz > $IBUILTIT.gz.md5
echo " done."
if [ -d $IDLPATH ]; then
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Removing outdated ${IDISTNAME^} ${ICODENAME^} $KOURNAME image..."
rm -rf $IDLPATH
echo " done."
fi
echo -n "`date +'%Y-%m-%d %H:%M:%S'`: Moving to webserver..."
mkdir -p $IDLPATH
mv ./*.img* $IDLPATH
chown -R $WEBUSER: $IDLPATH
chmod 775 $IDLPATH
chmod 664 $IDLPATH/*
rm "$OUTPUTPATH/openframe/images/${IDISTNAME,,}/${ICODENAME,,}/latest" 2>/dev/null
ln -s "$IDLPATH" "$OUTPUTPATH/openframe/images/${IDISTNAME,,}/${ICODENAME,,}/latest"
echo " done."
echo
echo "`date +'%Y-%m-%d %H:%M:%S'`: Image build completed."
echo
echo
fi
cleanup
exit 0