initial checkin of debin image builder
[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 ./config.build ] && . ./config.build
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 ./config.build ] && . ./config.build
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                 validimg="no"
45                 if [ -f ./rootfs.img ]; then
46                         size=`ls -al ./rootfs.img | awk '{ print $5/1048576 }'`
47                         if [ "$size" = "$IMGSIZE" ]; then
48                                 echo "rootfs.img exists"
49                                 validimg="yes"
50                         else
51                                 echo "creating rootfs.img"
52                         fi
53                 fi
54                 if [ "$validimg" = "no" ]; then
55                         dd if=/dev/zero of=./rootfs.img bs=1M count=$IMGSIZE
56                 fi
57                 mkfs.${ROOTFS} -f -L rootfs ./rootfs.img
58                 # mount
59                 umount_if_mounted rootfs.mnt
60                 rm -rf rootfs.mnt
61                 mkdir rootfs.mnt
62                 mount -o loop ./rootfs.img ./rootfs.mnt
63                 # debootstrap first part
64                 debootstrap --verbose --arch $ARCH --variant=minbase \
65                         --foreign $SUITE rootfs.mnt $DEBMIRROR
66                 # copy emulator
67                 cp -v `which $EMU` ./rootfs.mnt/usr/bin
68                 touch .build_stage1
69         fi
70         # copy myself
71         cp -v ./build.sh ./rootfs.mnt
72         cp -v ./config.build ./rootfs.mnt
73         if [ ! -f .build_stage2 ]; then
74                 # prepare/run second stage
75                 modprobe binfmt_misc
76                 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc"
77                 for mnt in $mnts; do 
78                         umount_if_mounted $mnt
79                 done
80                 mount -t proc proc ./rootfs.mnt/proc
81                 mkdir -p ./rootfs.mnt/dev/pts
82                 mount -t devpts devpts ./rootfs.mnt/dev/pts
83                 chroot ./rootfs.mnt /build.sh chroot
84                 #
85                 # running build_stage2()
86                 #
87                 for mnt in $mnts; do 
88                         umount_if_mounted $mnt
89                 done
90                 touch .build_stage2
91                 # cleanup
92                 rm -v ./rootfs.mnt/build.sh
93                 rm -v ./rootfs.mnt/config.build
94         fi
95 }
96
97 function build_in_chroot {
98         # read config
99         [ -f ./config.build ] && . ./config.build
100         # build second stage
101         echo "building debian image, second stage ..."
102         [ -f /debootstrap/debootstrap ] && \
103                 /debootstrap/debootstrap --second-stage
104         # additional packages + configuration
105         echo "deb ${DEBMIRROR}/ $SUITE main contrib non-free" > \
106                 /etc/apt/sources.list
107         echo "deb-src ${DEBMIRROR}/ $SUITE main contrib non-free" >> \
108                 /etc/apt/sources.list
109         # run apt
110         apt-get update
111         apt-get -y install $PKGS
112         # locales
113         locale-gen --purge en_US.UTF-8
114         echo 'LANG="en_US.UTF-8"' > /etc/default/locale
115         echo 'LANGUAGE="en_US:en"' >> /etc/default/locale
116         # network
117         echo -en "auto lo\niface lo inet loopback\n" > /etc/network/interfaces
118         echo -en "auto eth0\niface eth0 inet dhcp\n" >> /etc/network/interfaces
119         echo -en "auto usb0\niface usb0 inet dhcp\n" >> /etc/network/interfaces
120         # login on serial console
121         num=`echo $GETTY | sed 's/\([a-zA-Z].*\)\([0-9].*\)/\2/'`
122         echo "T${num}:2345:respawn:/sbin/getty -L $GETTY 115200 linux" >> \
123                 /etc/inittab
124         # hostname
125         echo $NAME > /etc/hostname
126         # root
127         echo "/dev/root / $ROOTFS defaults 0 1" > /etc/fstab
128         # behavior of boot scripts      
129         echo "HWCLOCKACCESS=no" >> /etc/default/rcS
130         echo "CONCURRENCY=shell" >> /etc/default/rcS
131         # additional packages
132         apt-get -y install $ADDPKGS
133         # (un)set passwd
134         passwd -d root
135         # profile
136         echo -en "\nalias l='ls -al --color'\n\n" >> /etc/profile
137 }
138
139 #
140 # post chroot tasks
141 #
142
143 function build_uboot() {
144         [ -f ./config.build ] && . ./config.build
145         git clone $UBSRC
146         cd u-boot
147         if [ ! -z "$UBVER" ]; then
148                 git checkout $UBVER
149         fi
150         if [ ! -z "$UBPATCH" ]; then
151                 patch -Nfp1 < ../patches/$UBPATCH
152         fi
153         make $MAKEOPT distclean
154         make $MAKEOPT $UBCONF
155         make $MAKEOPT
156         cd ..
157 }
158
159 function build_kernel() {
160         [ -f ./config.build ] && . ./config.build
161         KV=`echo $KERVER | awk -F. '{
162                 if($1=="3") print "3.x"
163                 else print$1 "." $2
164         }'`
165         KERSRC="https://www.kernel.org/pub/linux/kernel/"
166         KERSRC="$KERSRC/v$KV/linux-${KERVER}.tar.bz2"
167         wget $KERSRC
168         tar xfj linux-${KERVER}.tar.bz2
169         cd linux-$KERVER
170         if [ ! -z "$KERPATCH" ]; then
171         patch -Nfp1 < ../patches/$PATCH
172         fi
173         make $MAKEOPT mrproper
174         if [ ! -z "$KERHOOK" ]; then
175                 $KERHOOK
176         fi
177         if [ ! -z "$KERCONF" ]; then
178                 make $MAKEOPT $KERCONF
179         fi
180         make $MAKEOPT zImage
181         make $MAKEOPT modules
182         make $MAKEOPT INSTALL_MOD_PATH=../rootfs.mnt modules_install
183         cd ..
184 }
185
186 function build_sdcard() {
187         echo "creating sdcard ..."
188 }
189
190 #
191 # main
192 #
193
194 # configuration 
195 if [ ! -f ./config.build ]; then
196         echo "no configuration found, aborting ..."
197 else
198         . ./config.build
199 fi
200
201 # check prerequisites
202 check_prereq
203
204 # debootstrap
205 if [ -z $1 ]; then
206         build_all > ./build.log 2>&1
207 elif [ "$1" = "chroot" ]; then
208         build_in_chroot > ./build_in_chroot.log 2>&1
209 elif [ "$1" = "uboot" ]; then
210         build_uboot > ./build_uboot.log 2>&1
211 elif [ "$1" = "kernel" ]; then
212         build_kernel > ./build_kernel.log 2>&1
213 else 
214         echo "unknown option: '$1'"
215 fi
216