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 \
54 echo "creating rootfs.img"
55 dd if=/dev/zero of=./rootfs.img \
59 umount_if_mounted rootfs.mnt
61 [ "$ROOTFS" = "xfs" ] && FORCEFS="-f"
62 mkfs.${ROOTFS} $FORCEFS -L rootfs ./rootfs.img
66 mount -o loop ./rootfs.img ./rootfs.mnt
67 # debootstrap first part
68 debootstrap --verbose --arch $ARCH --variant=minbase \
69 --foreign $SUITE rootfs.mnt $DEBMIRROR
71 cp -v `which $EMU` ./rootfs.mnt/usr/bin
75 cp -v ./build.sh ./rootfs.mnt
76 cp -v ./build.config ./rootfs.mnt
77 mkdir -p ./rootfs.mnt/post_routines
78 cp -v ./post_routines/$NAME/* ./rootfs.mnt/post_routines
79 mkdir -p ./rootfs.mnt/files
80 cp -rv ./files/$NAME/* ./rootfs.mnt/files
81 if [ ! -f .build_stage2 ]; then
82 # prepare/run second stage
84 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc"
86 umount_if_mounted $mnt
88 mount -t proc proc ./rootfs.mnt/proc
89 mkdir -p ./rootfs.mnt/dev/pts
90 mount -t devpts devpts ./rootfs.mnt/dev/pts
91 chroot ./rootfs.mnt /build.sh chroot
93 # running build_stage2()
96 umount_if_mounted $mnt
100 rm -v ./rootfs.mnt/build.sh
101 rm -v ./rootfs.mnt/build.config
102 rm -rfv ./rootfs.mnt/post_routines
103 rm -rfv ./rootfs.mnt/files
107 echo -en "build completed! want me to umount everything? [Y,n]: "
109 if [ "$answer" != "n" ]; then
110 mnts="rootfs.mnt/dev/pts rootfs.mnt/proc rootfs.mnt"
112 umount_if_mounted $mnt
117 function build_in_chroot {
119 [ -f ./build.config ] && . ./build.config
121 echo "building debian image, second stage ..."
122 [ -f /debootstrap/debootstrap ] && \
123 /debootstrap/debootstrap --second-stage
124 # additional packages + configuration
125 echo "deb ${DEBMIRROR}/ $SUITE main contrib non-free" > \
126 /etc/apt/sources.list
127 echo "deb-src ${DEBMIRROR}/ $SUITE main contrib non-free" >> \
128 /etc/apt/sources.list
131 apt-get -y install $PKGS
133 locale-gen --purge en_US.UTF-8
134 echo 'LANG="en_US.UTF-8"' > /etc/default/locale
135 echo 'LANGUAGE="en_US:en"' >> /etc/default/locale
137 echo -en "auto lo\niface lo inet loopback\n" > /etc/network/interfaces
138 echo -en "auto eth0\niface eth0 inet dhcp\n" >> /etc/network/interfaces
139 echo -en "auto usb0\niface usb0 inet dhcp\n" >> /etc/network/interfaces
141 echo $NAME > /etc/hostname
143 echo "/dev/root / $ROOTFS defaults 0 1" > /etc/fstab
144 # determine init type
145 if [ "`file /sbin/init | awk '{ print $2 }'`" = "symbolic" ]; then
146 symlink=`ls -al /sbin/init | awk -F'->' '{ print $2 }'`
147 inittype=`basename $symlink`
152 num=`echo $GETTY | sed 's/\([a-zA-Z].*\)\([0-9].*\)/\2/'`
155 # nothing required to enable serial console
158 echo -en "T${num}:2345:respawn:/sbin/getty -L " >> \ /etc/inittab
159 echo -en "$GETTY 115200 linux" >> /etc/inittab;;
161 echo "unsupported init system: $inittype";;
164 echo -en "\nHWCLOCKACCESS=no\n" >> /etc/default/rcS
165 echo "CONCURRENCY=shell" >> /etc/default/rcS
166 # additional packages
167 apt-get -y install $ADDPKGS
171 echo -en "\nalias l='ls -al --color'\n\n" >> /etc/profile
173 for file in `ls /post_routines`; do
174 . /post_routines/$file
182 function build_uboot() {
183 echo "building u-boot ..."
193 -nopatch) nopatch=1; shift;;
194 -noget) noget=1; shift;;
195 -update) update=1; shift;;
196 -nocheckout) nocheckout=1; shift;;
197 -noclean) noclean=1; shift;;
198 -no2all) no2all=1; shift;;
202 if [ "$no2all" = "1" ]; then
208 [ -f ./build.config ] && . ./build.config
209 if [ "$noget" = "0" ]; then
213 if [ "$update" = "1" ]; then
219 if [ "$nocheckout" = "0" ]; then
220 if [ ! -z "$UBVER" ]; then
224 if [ "$nopatch" = "0" ]; then
225 if [ ! -z "$UBPATCH" ]; then
226 for patch in $UBPATCH; do
227 patch -Nfp1 < ../patches/$NAME/$patch
231 if [ "$noclean" = "0" ]; then
232 make $MAKEOPT distclean
234 make $MAKEOPT $UBCONF
239 function build_kernel() {
247 -nopatch) nopatch=1; shift;;
248 -noget) noget=1; shift;;
249 -nohook) nohook=1; shift;;
250 -noclean) noclean=1; shift;;
251 -no2all) no2all=1; shift;;
255 if [ "$no2all" = "1" ]; then
261 [ -f ./build.config ] && . ./build.config
262 KV=`echo $KERVER | awk -F. '{
263 if($1=="3") print "3.x"
266 if [ "$noget" = "0" ]; then
267 rm -rf linux-${KERVER}*
268 KERSRC="https://www.kernel.org/pub/linux/kernel/"
269 KERSRC="$KERSRC/v$KV/linux-${KERVER}.tar.bz2"
271 tar xfj linux-${KERVER}.tar.bz2
274 if [ "$nopatch" = "0" ]; then
275 if [ ! -z "$KERPATCH" ]; then
276 for patch in $KERPATCH; do
277 patch -Nfp1 < ../patches/$NAME/$patch
281 if [ "$noclean" = "0" ]; then
282 make $MAKEOPT mrproper
284 if [ "$nohook" = "0" ]; then
285 if [ ! -z "$KERHOOKS" ]; then
286 for hook in $KERHOOKS; do
291 if [ ! -z "$KERCONF" ]; then
292 make $MAKEOPT $KERCONF
295 make $MAKEOPT modules
296 make $MAKEOPT INSTALL_MOD_PATH=../rootfs.mnt modules_install
300 function build_sdcard() {
301 echo "creating sdcard ..."
309 if [ ! -f ./build.config ]; then
310 echo "no configuration found, aborting ..."
318 build_all | tee ./build.log 2>&1
319 elif [ "$1" = "chroot" ]; then
320 build_in_chroot | tee ./build_in_chroot.log 2>&1
321 elif [ "$1" = "uboot" ]; then
323 build_uboot $@ | tee ./build_uboot.log 2>&1
324 elif [ "$1" = "kernel" ]; then
326 build_kernel $@ | tee ./build_kernel.log 2>&1
328 echo "unknown option: '$1'"