added ea313x.c file ...
[outofuni/dib.git] / build.sh
1 #!/bin/bash
2
3 # debian image build tool
4 # author hackbard@hackdaworld.org
5
6 function check_prereq() {
7         # read config
8         [ -f ./build.config ] && . ./build.config
9         # check
10         tc="gcc-`echo $MAKEOPT | sed 's/.*\ CROSS_COMPILE=\(.*\)-.*/\1/'`"
11         deps=0
12         for deb in debootstrap binfmt-support qemu-user-static $tc; do
13                 ip=`dpkg -l | grep $deb | awk '{ print $2 }'`
14                 if [ -z "$ip" ]; then
15                         echo "package $deb required but not installed"
16                         deps=1
17                 fi
18         done
19         if [ "$deps" = "1" ]; then
20                 echo "install prerequisites"
21                 exit 1
22         fi
23 }
24
25 function umount_if_mounted() {
26         mnt=$1
27         rpath="`realpath $PWD`"
28         mntpt=`mount | grep $rpath/$mnt | awk '{ print $3 }'`
29         if [ ! -z "$mntpt" ]; then
30                 echo "umounting $mntpt"
31                 umount $mntpt
32         fi
33 }
34
35 function build_all {
36         # read config
37         [ -f ./build.config ] && . ./build.config
38         # build stage 1
39         if [ -f .build_stage1 ]; then
40                 echo "stage one already done, skipping ..."
41         else
42                 echo "building debian image, first stage ..."
43                 # image
44                 if [ -f ./rootfs.img ]; then
45                         size=`ls -al ./rootfs.img | awk '{ print $5/1048576 }'`
46                         if [ "$size" = "$IMGSIZE" ]; then
47                                 echo "rootfs.img exists"
48                         else
49                                 echo "creating rootfs.img"
50                                 dd if=/dev/zero of=./rootfs.img \
51                                         bs=1M count=$IMGSIZE
52                         fi
53                 fi
54                 # make fs and mount
55                 umount_if_mounted rootfs.mnt
56                 FORCEFS="-F"
57                 [ "$ROOTFS" = "xfs" ] && FORCEFS="-f"
58                 mkfs.${ROOTFS} $FORCEFS -L rootfs ./rootfs.img
59                 rm -rf rootfs.mnt
60                 mkdir rootfs.mnt
61                 mount -o loop ./rootfs.img ./rootfs.mnt
62                 # debootstrap first part
63                 debootstrap --verbose --arch $ARCH --variant=minbase \
64                         --foreign $SUITE rootfs.mnt $DEBMIRROR
65                 # copy emulator
66                 cp -v `which $EMU` ./rootfs.mnt/usr/bin
67                 touch .build_stage1
68         fi
69         # copy myself
70         cp -v ./build.sh ./rootfs.mnt
71         cp -v ./build.config ./rootfs.mnt
72         mkdir -p ./rootfs.mnt/post_routines
73         cp -v ./post_routines/$NAME/* ./rootfs.mnt/post_routines
74         mkdir -p ./rootfs.mnt/files
75         cp -rv ./files/$NAME/* ./rootfs.mnt/files
76         if [ ! -f .build_stage2 ]; then
77                 # prepare/run second stage
78                 modprobe binfmt_misc
79                 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc"
80                 for mnt in $mnts; do 
81                         umount_if_mounted $mnt
82                 done
83                 mount -t proc proc ./rootfs.mnt/proc
84                 mkdir -p ./rootfs.mnt/dev/pts
85                 mount -t devpts devpts ./rootfs.mnt/dev/pts
86                 chroot ./rootfs.mnt /build.sh chroot
87                 #
88                 # running build_stage2()
89                 #
90                 for mnt in $mnts; do 
91                         umount_if_mounted $mnt
92                 done
93                 touch .build_stage2
94                 # cleanup
95                 rm -v ./rootfs.mnt/build.sh
96                 rm -v ./rootfs.mnt/build.config
97                 rm -rfv ./rootfs.mnt/post_routines
98                 rm -rfv ./rootfs.mnt/files
99         fi
100         # final things
101         echo
102         echo -en "build completed! want me to umount everything? [Y,n]: "
103         read answer
104         if [ "$answer" != "n" ]; then
105                 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc rootfs.mnt"
106                 for mnt in $mnts; do
107                         umount_if_mounted $mnt
108                 done
109         fi
110 }
111
112 function build_in_chroot {
113         # read config
114         [ -f ./build.config ] && . ./build.config
115         # build second stage
116         echo "building debian image, second stage ..."
117         [ -f /debootstrap/debootstrap ] && \
118                 /debootstrap/debootstrap --second-stage
119         # additional packages + configuration
120         echo "deb ${DEBMIRROR}/ $SUITE main contrib non-free" > \
121                 /etc/apt/sources.list
122         echo "deb-src ${DEBMIRROR}/ $SUITE main contrib non-free" >> \
123                 /etc/apt/sources.list
124         # run apt
125         apt-get update
126         apt-get -y install $PKGS
127         # locales
128         locale-gen --purge en_US.UTF-8
129         echo 'LANG="en_US.UTF-8"' > /etc/default/locale
130         echo 'LANGUAGE="en_US:en"' >> /etc/default/locale
131         # network
132         echo -en "auto lo\niface lo inet loopback\n" > /etc/network/interfaces
133         echo -en "auto eth0\niface eth0 inet dhcp\n" >> /etc/network/interfaces
134         echo -en "auto usb0\niface usb0 inet dhcp\n" >> /etc/network/interfaces
135         # hostname
136         echo $NAME > /etc/hostname
137         # root
138         echo "/dev/root / $ROOTFS defaults 0 1" > /etc/fstab
139         # determine init type
140         if [ "`file /sbin/init | awk '{ print $2 }'`" = "symbolic" ]; then
141                 symlink=`ls -al /sbin/init | awk -F'->' '{ print $2 }'`
142                 inittype=`basename $symlink`
143         else
144                 inittype=sysvinit
145         fi
146         # serial console
147         num=`echo $GETTY | sed 's/\([a-zA-Z].*\)\([0-9].*\)/\2/'`
148         case "$inittype" in
149                 systemd)
150                         # nothing required to enable serial console
151                         ;;
152                 sysvinit)
153                         echo -en "T${num}:2345:respawn:/sbin/getty -L " >> \                                    /etc/inittab
154                         echo -en "$GETTY 115200 linux" >> /etc/inittab;;
155                 *)
156                         echo "unsupported init system: $inittype";;
157         esac
158         # more boot scripts     
159         echo -en "\nHWCLOCKACCESS=no\n" >> /etc/default/rcS
160         echo "CONCURRENCY=shell" >> /etc/default/rcS
161         # additional packages
162         apt-get -y install $ADDPKGS
163         # (un)set passwd
164         passwd -d root
165         # profile
166         echo -en "\nalias l='ls -al --color'\n\n" >> /etc/profile
167         # post install hooks
168         for file in `ls /post_routines`; do
169                 . /post_routines/$file
170         done
171 }
172
173 #
174 # post chroot tasks
175 #
176
177 function build_uboot() {
178         echo "building u-boot ..."
179         echo "  argv: $@"
180         nopatch=0
181         noget=0
182         update=0
183         nocheckout=0
184         noclean=0
185         no2all=0
186         while [ "$1" ]; do
187                 case "$1" in
188                         -nopatch)       nopatch=1; shift;;
189                         -noget)         noget=1; shift;;
190                         -update)        update=1; shift;;
191                         -nocheckout)    nocheckout=1; shift;;
192                         -noclean)       noclean=1; shift;;
193                         -no2all)        no2all=1; shift;;
194                         *) shift;;
195                 esac
196         done
197         if [ "$no2all" = "1" ]; then
198                 nopatch=1
199                 noget=1
200                 nocheckout=1
201                 noclean=1
202         fi
203         [ -f ./build.config ] && . ./build.config
204         if [ "$noget" = "0" ]; then
205                 rm -rf u-boot
206                 git clone $UBSRC
207         fi
208         if [ "$update" = "1" ]; then
209                 cd u-boot
210                 git pull
211                 cd ..
212         fi
213         cd u-boot
214         if [ "$nocheckout" = "0" ]; then
215                 if [ ! -z "$UBVER" ]; then
216                         git checkout $UBVER
217                 fi
218         fi
219         if [ "$nopatch" = "0" ]; then
220                 if [ ! -z "$UBPATCH" ]; then
221                         for patch in $UBPATCH; do
222                                 patch -Nfp1 < ../patches/$NAME/$patch
223                         done
224                 fi
225         fi
226         if [ "$noclean" = "0" ]; then
227                 make $MAKEOPT distclean
228         fi
229         make $MAKEOPT $UBCONF
230         make $MAKEOPT
231         cd ..
232 }
233
234 function build_kernel() {
235         nopatch=0
236         noget=0
237         nohook=0
238         noclean=0
239         no2all=0
240         while [ "$1" ]; do
241                 case "$1" in
242                         -nopatch)       nopatch=1; shift;;
243                         -noget)         noget=1; shift;;
244                         -nohook)        nohook=1; shift;;
245                         -noclean)       noclean=1; shift;;
246                         -no2all)        no2all=1; shift;;
247                         *) shift;;
248                 esac
249         done
250         if [ "$no2all" = "1" ]; then
251                 nopatch=1
252                 noget=1
253                 nohook=1
254                 noclean=1
255         fi
256         [ -f ./build.config ] && . ./build.config
257         KV=`echo $KERVER | awk -F. '{
258                 if($1=="3") print "3.x"
259                 else print$1 "." $2
260         }'`
261         if [ "$noget" = "0" ]; then
262                 rm -rf linux-${KERVER}*
263                 KERSRC="https://www.kernel.org/pub/linux/kernel/"
264                 KERSRC="$KERSRC/v$KV/linux-${KERVER}.tar.bz2"
265                 wget $KERSRC
266                 tar xfj linux-${KERVER}.tar.bz2
267         fi
268         cd linux-$KERVER
269         if [ "$nopatch" = "0" ]; then
270                 if [ ! -z "$KERPATCH" ]; then
271                         for patch in $KERPATCH; do
272                                 patch -Nfp1 < ../patches/$NAME/$patch
273                         done
274                 fi
275         fi
276         if [ "$noclean" = "0" ]; then
277                 make $MAKEOPT mrproper
278         fi
279         if [ "$nohook" = "0" ]; then
280                 if [ ! -z "$KERHOOKS" ]; then
281                         for hook in $KERHOOKS; do
282                                 . ../hooks/$hook
283                         done
284                 fi
285         fi
286         if [ ! -z "$KERCONF" ]; then
287                 make $MAKEOPT $KERCONF
288         fi
289         make $MAKEOPT zImage
290         make $MAKEOPT modules
291         make $MAKEOPT INSTALL_MOD_PATH=../rootfs.mnt modules_install
292         cd ..
293 }
294
295 function build_sdcard() {
296         echo "creating sdcard ..."
297 }
298
299 #
300 # main
301 #
302
303 # configuration 
304 if [ ! -f ./build.config ]; then
305         echo "no configuration found, aborting ..."
306 else
307         . ./build.config
308 fi
309
310 # debootstrap
311 if [ -z $1 ]; then
312         check_prereq
313         build_all | tee ./build.log 2>&1
314 elif [ "$1" = "chroot" ]; then
315         build_in_chroot | tee ./build_in_chroot.log 2>&1
316 elif [ "$1" = "uboot" ]; then
317         check_prereq
318         build_uboot $@ | tee ./build_uboot.log 2>&1
319 elif [ "$1" = "kernel" ]; then
320         check_prereq
321         build_kernel $@ | tee ./build_kernel.log 2>&1
322 else 
323         echo "unknown option: '$1'"
324 fi
325