3 # debian image build tool
4 # author hackbard@hackdaworld.org
6 function check_prereq() {
8 [ -f ./build.config ] && . ./build.config
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 ./build.config ] && . ./build.config
39 if [ -f .build_stage1 ]; then
40 echo "stage one already done, skipping ..."
42 echo "building debian image, first stage ..."
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"
49 echo "creating rootfs.img"
50 dd if=/dev/zero of=./rootfs.img \
55 umount_if_mounted rootfs.mnt
57 [ "$ROOTFS" = "xfs" ] && FORCEFS="-f"
58 mkfs.${ROOTFS} $FORCEFS -L rootfs ./rootfs.img
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
66 cp -v `which $EMU` ./rootfs.mnt/usr/bin
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
79 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc"
81 umount_if_mounted $mnt
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
88 # running build_stage2()
91 umount_if_mounted $mnt
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
102 echo -en "build completed! want me to umount everything? [Y,n]: "
104 if [ "$answer" != "n" ]; then
105 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc rootfs.mnt"
107 umount_if_mounted $mnt
112 function build_in_chroot {
114 [ -f ./build.config ] && . ./build.config
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
126 apt-get -y install $PKGS
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
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
136 echo $NAME > /etc/hostname
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`
147 num=`echo $GETTY | sed 's/\([a-zA-Z].*\)\([0-9].*\)/\2/'`
150 # nothing required to enable serial console
153 echo -en "T${num}:2345:respawn:/sbin/getty -L " >> \ /etc/inittab
154 echo -en "$GETTY 115200 linux" >> /etc/inittab;;
156 echo "unsupported init system: $inittype";;
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
166 echo -en "\nalias l='ls -al --color'\n\n" >> /etc/profile
168 for file in `ls /post_routines`; do
169 . /post_routines/$file
177 function build_uboot() {
178 echo "building u-boot ..."
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;;
197 if [ "$no2all" = "1" ]; then
203 [ -f ./build.config ] && . ./build.config
204 if [ "$noget" = "0" ]; then
208 if [ "$update" = "1" ]; then
214 if [ "$nocheckout" = "0" ]; then
215 if [ ! -z "$UBVER" ]; then
219 if [ "$nopatch" = "0" ]; then
220 if [ ! -z "$UBPATCH" ]; then
221 for patch in $UBPATCH; do
222 patch -Nfp1 < ../patches/$NAME/$patch
226 if [ "$noclean" = "0" ]; then
227 make $MAKEOPT distclean
229 make $MAKEOPT $UBCONF
234 function build_kernel() {
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;;
250 if [ "$no2all" = "1" ]; then
256 [ -f ./build.config ] && . ./build.config
257 KV=`echo $KERVER | awk -F. '{
258 if($1=="3") print "3.x"
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"
266 tar xfj linux-${KERVER}.tar.bz2
269 if [ "$nopatch" = "0" ]; then
270 if [ ! -z "$KERPATCH" ]; then
271 for patch in $KERPATCH; do
272 patch -Nfp1 < ../patches/$NAME/$patch
276 if [ "$noclean" = "0" ]; then
277 make $MAKEOPT mrproper
279 if [ "$nohook" = "0" ]; then
280 if [ ! -z "$KERHOOKS" ]; then
281 for hook in $KERHOOKS; do
286 if [ ! -z "$KERCONF" ]; then
287 make $MAKEOPT $KERCONF
290 make $MAKEOPT modules
291 make $MAKEOPT INSTALL_MOD_PATH=../rootfs.mnt modules_install
295 function build_sdcard() {
296 echo "creating sdcard ..."
304 if [ ! -f ./build.config ]; then
305 echo "no configuration found, aborting ..."
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
318 build_uboot $@ | tee ./build_uboot.log 2>&1
319 elif [ "$1" = "kernel" ]; then
321 build_kernel $@ | tee ./build_kernel.log 2>&1
323 echo "unknown option: '$1'"