-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·312 lines (272 loc) · 8.09 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
#!/bin/sh
. ./vars.sh
. ./ReqCheck.sh
#DO NOT EDIT BELOW it should not be necessary.
#-----------------------------------------------------------
MAKEINIT=false #we dont want to overdo a makeinit, used internaly
MODULE=false #add modules to linux (asuming kernel already supports this)
#first stuff happening here.
mkdir -p $TOP
cd $TOP
#a bunch of helpfull functions
#----------------------------------------------------------------------
function DoQemu() {
cd $TOP
qemu-system-$ARCH \
-m $RAM \
-kernel obj/linux-$ARC/arch/$ARCH/boot/bzImage \
-initrd obj/initramfs-busybox-$ARC.cpio.gz \
-nographic -append "console=ttyS0" $NET $OPTION
}
#----------------------------------------------------------------------
#Download if necessary, clean an unclean build
function download() {
cd $TOP
if [ ! -f $TOP/linux-$KERNEL.tar.$KTYPE ]; then #Maybe now partial downloads work?
wget -c https://cdn.kernel.org/pub/linux/kernel/v$V.x/linux-$KERNEL.tar.$KTYPE
fi
if [ ! -f "$TOP/busybox-$BUSY.tar.bz2" ]; then
wget -c https://busybox.net/downloads/busybox-$BUSY.tar.bz2
fi
}
#----------------------------------------------------------------------
function delete() {
cd $TOP
mv linux-$KERNEL.tar.$KTYPE ../
mv busybox-$BUSY.tar.bz2 ../
rm -rf *
mv ../linux-$KERNEL.tar.$KTYPE linux-$KERNEL.tar.$KTYPE
mv ../busybox-$BUSY.tar.bz2 busybox-$BUSY.tar.bz2
exit 0
}
#----------------------------------------------------------------------
function writeInit() {
cat << EOF > init
#!/bin/sh
syslogd
mount -t devtmpfs devtmpfs /dev
mkdir /dev/pts
mount -t devpts devpts /dev/pts
mount -t proc none /proc
mount -t sysfs none /sys
hostname
/sbin/mdev -s
/sbin/ifconfig lo 127.0.0.1 netmask 255.0.0.0 up
/sbin/ifconfig eth0 up $IP netmask 255.255.255.0 up
/sbin/route add default gw $GATEWAY
hostname $HOSTNAME
echo -e '\nWelcome to Teeny Linux\n'
echo -e 'Amount of seconds to boot: '
cut -d' ' -f1 /proc/uptime
echo -e 'To shutdown and return to your CLI'
echo -e 'type poweroff -f or \n Ctrl+a C, then "quit"\n'
cat /proc/version
ifconfig eth0 | grep -B1 'inet addr' | grep 'inet'
while [ 1 ]; do
/usr/bin/setsid /bin/cttyhack $LOGINREQUIRED
done
EOF
}
#----------------------------------------------------------------------
function copytoimage() { #This function will copy nececairy files into the initramfs
# modules option
if $MODULE ; then
mkdir -pv $MODULEURL
cp $TOP/hello.ko $MODULEURL/.
fi
# the extra builded files to be included into the initramfs
if [ -d $TOP/build/ ]; then
#add packages (non overwrite, add)
cat $TOP/build/var/lib/dpkg/status >> $TOP/initramfs/$ARC-busybox/var/lib/dpkg/status
mv $TOP/build/var/lib/dpkg/status $TOP/status
#copy files, overwrite if nececairy
cp -r $TOP/build/. $TOP/initramfs/$ARC-busybox/
#restore build dir
mv $TOP/status $TOP/build/var/lib/dpkg/status
fi
#add user?
cat << EOF > $TOP/initramfs/$ARC-busybox/etc/passwd
root:LTMW6A/nz.KWI:0:0:root:/root:/bin/sh
EOF
}
#----------------------------------------------------------------------
function buildBusyBox {
cd $TOP
rm -rf busybox-$BUSY/
tar xjf busybox-$BUSY.tar.bz2
rm -rf obj/busybox-$ARC
cd $TOP/busybox-$BUSY
mkdir -pv ../obj/busybox-$ARC
make O=../obj/busybox-$ARC defconfig $COMPILER
# do a static lib thing for busy,
sed -i '/# CONFIG_STATIC is not set/c\CONFIG_STATIC=y' ../obj/busybox-$ARC/.config
#for musl we experimentaly determined these to be nececairy
#There are traffic control related symbols removed in Kernel 6.8
sed -i '/CONFIG_TC=y/c\# CONFIG_TC is not set\r\n# CONFIG_FEATURE_TC_INGRESS is not set' ../obj/busybox-$ARC/.config
# Gentoo has some patches availeble but disableing this command seems to fix it for me.
# https://bugs.gentoo.org/926872
cd ../obj/busybox-$ARC
make -j$CORECOUNT $COMPILER
make install $COMPILER
}
function makeNewInitramfs() {
#Make the initramfs (first clean ofcourse)
rm -rf $TOP/initramfs
mkdir -pv $TOP/initramfs/$ARC-busybox
cd $TOP/initramfs/$ARC-busybox
mkdir -pv {bin,sbin,root,etc,proc,sys,usr/{bin,sbin,local/{bin,lib}}}
mkdir -pv {var/{run,lib/dpkg},etc/network/{if-down.d,if-up.d,if-down.d,if-post-down.d,if-post-up.d,if-pre-down.d,if-pre-up.d}}
cat << EOF > var/lib/dpkg/status
Package: busybox
Status: hold ok installed
Priority: optional
Section: base
Version: $BUSY
EOF
makeInitramfs
}
function makeInitramfs() {
cd $TOP/initramfs/$ARC-busybox
cp -av $TOP/obj/busybox-$ARC/_install/* .
#add new files to copy here?
writeInit
copytoimage
cd $TOP/initramfs/$ARC-busybox/root
cat << EOF > .profile
alias ls='ls --color=auto'
alias today='date +"%Y-%m-%d"'
alias todaytime='date +"%Y-%m-%d %H:%M"'
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin:tools
PS1='\[\033[35m\]\t\[\033[m\][\[\033[1;31m\]\u\[\033[0m\]@\[\e[1;34m\]\h\[\e[0m\]:\[\e[94m\]\w\[\e[0m\]]\\$ \[\e[m\]'
EOF
cat << EOF > hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world.\n";
return 0;
}
EOF
cd $TOP/initramfs/$ARC-busybox
# DNS entry
echo nameserver $DNS > etc/resolv.conf
chmod +x init
if [ -f $TOP/obj/busybox-$ARC/busybox ]; then
find . -print0 \
| cpio --null -ov --format=newc \
| gzip -9 > $TOP/obj/initramfs-busybox-$ARC.cpio.gz
else
echo "[ error ] busybox is missing"
exit 1;
fi
}
#----------------------------------------------------------------------
function makeKernel() {
cd $TOP
rm -rf linux-$KERNEL/
rm -rf obj/linux-$ARC
if [ ! $KTYPE == "gz" ]; then #for gz we need xvf, due to RC being diferent
tar xJf linux-$KERNEL.tar.$KTYPE
else
tar xvf linux-$KERNEL.tar.$KTYPE
fi
#Make our Kernel
cd $TOP/linux-$KERNEL
make mrproper
make O=../obj/linux-$ARC x86_64_defconfig
make O=../obj/linux-$ARC kvm_guest.config
sed -i 's/CONFIG_WERROR=y/# CONFIG_WERROR is not set/' ../obj/linux-x86/.config
make O=../obj/linux-$ARC EXTRA_CFLAGS="-Wno-use-after-free" -j$CORECOUNT
}
#----------------------------------------------------------------------
#process commandline arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-arch|-cpu)
ARCH="$2"
ARC="$2"
shift; shift # past argument and value
;;-init|-makeInit|-makeinit)
MAKEINIT=true
ARCH="x86_64"
shift; # past argument and value
;;-d|-delete|-deleteall)
delete
shift; # past argument and value
;;-k|-kernel)
KERNEL="$2"
shift; shift
;;-nologin|-nl)
LOGINREQUIRED="/bin/sh"
MAKEINIT=true
shift;
;;-net)
NET="-net nic,model=e1000,macaddr=$2 -net bridge,br=br0"
shift; shift
;;-mod|-module)
MODULE=true
MAKEINIT=true
shift;
;;-option)
OPTION="$2"
shift; shift
;;-t|-time)
# To be sure we have the files, we try download. will not be counted in the time value
download
echo "Timed compilation"
OVERALL_START="$(date +%s)"
buildBusyBox
INIT_START="$(date +%s)"
makeNewInitramfs
INIT_END="$(date +%s)"
makeKernel
OVERALL_END="$(date +%s)"
echo "Busybox took: $[ ${INIT_START} - ${OVERALL_START} ] seconds to compile"
echo "Initram took: $[ ${INIT_END} - ${INIT_START} ] seconds to build"
echo "Kernel took: $[ ${OVERALL_END} - ${INIT_END} ] seconds to compile"
echo "The overall code took: $[ ${OVERALL_END} - ${OVERALL_START} ] seconds to run"
exit 0
shift; # past argument and value
;;
esac
done
#sets defaults if arguments are empty or incorrect
if [ -z $ARCH ]; then
ARCH="x86_64"; fi
download
if [ $ARCH == "ppc" ]; then #this is to have a full arch name, but working functions
ARCHF="powerpc"
else
ARCHF=$ARCH
fi
if [ $MAKEINIT == "true" ]; then
makeNewInitramfs
fi
if [ -f "$TOP/obj/initramfs-busybox-$ARC.cpio.gz" ]; then
if [ ! -f $TOP/obj/linux-$ARC/arch/$ARCHF/boot/bzImage ]; then
makeKernel
fi
DoQemu
exit
else
if [ -f "$TOP/obj/busybox-$ARC/busybox" ]; then
makeNewInitramfs
if [ ! -f "$TOP/obj/linux-$ARC/arch/$ARCHF/boot/bzImage" ]; then
makeKernel
fi
DoQemu
exit
else
buildBusyBox
makeNewInitramfs
if [ ! -f "$TOP/obj/linux-$ARC/arch/$ARCHF/boot/bzImage" ]; then
makeKernel
fi
DoQemu
exit
fi
fi
fi