3 # debian image build tool
4 # author hackbard@hackdaworld.org
6 function check_prereq() {
8 [ -f ./config.build ] && . ./config.build
10 tc="gcc-`echo $MAKEOPT | sed 's/.*\ CROSS_COMPILE=\(.*\)-.*/\1/'`"
12 for deb in debootstrap binfmt-support qemu-user-static $tc; do
13 ip=`dpkg -l | grep $deb | awk '{ print $2 }'`
15 echo "package $deb required but not installed"
19 if [ "$deps" = "1" ]; then
20 echo "install prerequisites"
25 function umount_if_mounted() {
27 rpath="`realpath $PWD`"
28 mntpt=`mount | grep $rpath/$mnt | awk '{ print $3 }'`
29 if [ ! -z "$mntpt" ]; then
30 echo "umounting $mntpt"
37 [ -f ./config.build ] && . ./config.build
39 if [ -f .build_stage1 ]; then
40 echo "stage one already done, skipping ..."
42 echo "building debian image, first stage ..."
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"
51 echo "creating rootfs.img"
54 if [ "$validimg" = "no" ]; then
55 dd if=/dev/zero of=./rootfs.img bs=1M count=$IMGSIZE
57 mkfs.${ROOTFS} -f -L rootfs ./rootfs.img
59 umount_if_mounted 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
67 cp -v `which $EMU` ./rootfs.mnt/usr/bin
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
76 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc"
78 umount_if_mounted $mnt
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
85 # running build_stage2()
88 umount_if_mounted $mnt
92 rm -v ./rootfs.mnt/build.sh
93 rm -v ./rootfs.mnt/config.build
97 function build_in_chroot {
99 [ -f ./config.build ] && . ./config.build
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
111 apt-get -y install $PKGS
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
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" >> \
125 echo $NAME > /etc/hostname
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
136 echo -en "\nalias l='ls -al --color'\n\n" >> /etc/profile
143 function build_uboot() {
144 [ -f ./config.build ] && . ./config.build
147 if [ ! -z "$UBVER" ]; then
150 if [ ! -z "$UBPATCH" ]; then
151 patch -Nfp1 < ../patches/$UBPATCH
153 make $MAKEOPT distclean
154 make $MAKEOPT $UBCONF
159 function build_kernel() {
160 [ -f ./config.build ] && . ./config.build
161 KV=`echo $KERVER | awk -F. '{
162 if($1=="3") print "3.x"
165 KERSRC="https://www.kernel.org/pub/linux/kernel/"
166 KERSRC="$KERSRC/v$KV/linux-${KERVER}.tar.bz2"
168 tar xfj linux-${KERVER}.tar.bz2
170 if [ ! -z "$KERPATCH" ]; then
171 patch -Nfp1 < ../patches/$PATCH
173 make $MAKEOPT mrproper
174 if [ ! -z "$KERHOOK" ]; then
177 if [ ! -z "$KERCONF" ]; then
178 make $MAKEOPT $KERCONF
181 make $MAKEOPT modules
182 make $MAKEOPT INSTALL_MOD_PATH=../rootfs.mnt modules_install
186 function build_sdcard() {
187 echo "creating sdcard ..."
195 if [ ! -f ./config.build ]; then
196 echo "no configuration found, aborting ..."
201 # check prerequisites
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
214 echo "unknown option: '$1'"