stupid! if it does not exist, dd it!
[outofuni/dib.git] / build.sh
index 3446087..b04f89a 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -5,7 +5,7 @@
 
 function check_prereq() {
        # read config
-       [ -f ./config.build ] && . ./config.build
+       [ -f ./build.config ] && . ./build.config
        # check
        tc="gcc-`echo $MAKEOPT | sed 's/.*\ CROSS_COMPILE=\(.*\)-.*/\1/'`"
        deps=0
@@ -34,31 +34,35 @@ function umount_if_mounted() {
 
 function build_all {
        # read config
-       [ -f ./config.build ] && . ./config.build
+       [ -f ./build.config ] && . ./build.config
        # build stage 1
        if [ -f .build_stage1 ]; then
                echo "stage one already done, skipping ..."
        else
                echo "building debian image, first stage ..."
                # image
-               validimg="no"
                if [ -f ./rootfs.img ]; then
                        size=`ls -al ./rootfs.img | awk '{ print $5/1048576 }'`
                        if [ "$size" = "$IMGSIZE" ]; then
                                echo "rootfs.img exists"
-                               validimg="yes"
                        else
                                echo "creating rootfs.img"
+                               dd if=/dev/zero of=./rootfs.img \
+                                       bs=1M count=$IMGSIZE
                        fi
+               else
+                       echo "creating rootfs.img"
+                       dd if=/dev/zero of=./rootfs.img \
+                               bs=1M count=$IMGSIZE
                fi
-               if [ "$validimg" = "no" ]; then
-                       dd if=/dev/zero of=./rootfs.img bs=1M count=$IMGSIZE
-               fi
-               mkfs.${ROOTFS} -f -L rootfs ./rootfs.img
-               # mount
+               # make fs and mount
                umount_if_mounted rootfs.mnt
+               FORCEFS="-F"
+               [ "$ROOTFS" = "xfs" ] && FORCEFS="-f"
+               mkfs.${ROOTFS} $FORCEFS -L rootfs ./rootfs.img
                rm -rf rootfs.mnt
                mkdir rootfs.mnt
+               modprobe loop
                mount -o loop ./rootfs.img ./rootfs.mnt
                # debootstrap first part
                debootstrap --verbose --arch $ARCH --variant=minbase \
@@ -69,7 +73,11 @@ function build_all {
        fi
        # copy myself
        cp -v ./build.sh ./rootfs.mnt
-       cp -v ./config.build ./rootfs.mnt
+       cp -v ./build.config ./rootfs.mnt
+       mkdir -p ./rootfs.mnt/post_routines
+       cp -v ./post_routines/$NAME/* ./rootfs.mnt/post_routines
+       mkdir -p ./rootfs.mnt/files
+       cp -rv ./files/$NAME/* ./rootfs.mnt/files
        if [ ! -f .build_stage2 ]; then
                # prepare/run second stage
                modprobe binfmt_misc
@@ -90,13 +98,25 @@ function build_all {
                touch .build_stage2
                # cleanup
                rm -v ./rootfs.mnt/build.sh
-               rm -v ./rootfs.mnt/config.build
+               rm -v ./rootfs.mnt/build.config
+               rm -rfv ./rootfs.mnt/post_routines
+               rm -rfv ./rootfs.mnt/files
+       fi
+       # final things
+       echo
+       echo -en "build completed! want me to umount everything? [Y,n]: "
+       read answer
+       if [ "$answer" != "n" ]; then
+               mnts="rootfs.mnt/dev/pts rootfs.mnt/proc rootfs.mnt"
+               for mnt in $mnts; do
+                       umount_if_mounted $mnt
+               done
        fi
 }
 
 function build_in_chroot {
        # read config
-       [ -f ./config.build ] && . ./config.build
+       [ -f ./build.config ] && . ./build.config
        # build second stage
        echo "building debian image, second stage ..."
        [ -f /debootstrap/debootstrap ] && \
@@ -117,16 +137,31 @@ function build_in_chroot {
        echo -en "auto lo\niface lo inet loopback\n" > /etc/network/interfaces
        echo -en "auto eth0\niface eth0 inet dhcp\n" >> /etc/network/interfaces
        echo -en "auto usb0\niface usb0 inet dhcp\n" >> /etc/network/interfaces
-       # login on serial console
-       num=`echo $GETTY | sed 's/\([a-zA-Z].*\)\([0-9].*\)/\2/'`
-       echo "T${num}:2345:respawn:/sbin/getty -L $GETTY 115200 linux" >> \
-               /etc/inittab
        # hostname
        echo $NAME > /etc/hostname
        # root
        echo "/dev/root / $ROOTFS defaults 0 1" > /etc/fstab
-       # behavior of boot scripts      
-       echo "HWCLOCKACCESS=no" >> /etc/default/rcS
+       # determine init type
+       if [ "`file /sbin/init | awk '{ print $2 }'`" = "symbolic" ]; then
+               symlink=`ls -al /sbin/init | awk -F'->' '{ print $2 }'`
+               inittype=`basename $symlink`
+       else
+               inittype=sysvinit
+       fi
+       # serial console
+       num=`echo $GETTY | sed 's/\([a-zA-Z].*\)\([0-9].*\)/\2/'`
+       case "$inittype" in
+               systemd)
+                       # nothing required to enable serial console
+                       ;;
+               sysvinit)
+                       echo -en "T${num}:2345:respawn:/sbin/getty -L " >> \                                    /etc/inittab
+                       echo -en "$GETTY 115200 linux" >> /etc/inittab;;
+               *)
+                       echo "unsupported init system: $inittype";;
+       esac
+       # more boot scripts     
+       echo -en "\nHWCLOCKACCESS=no\n" >> /etc/default/rcS
        echo "CONCURRENCY=shell" >> /etc/default/rcS
        # additional packages
        apt-get -y install $ADDPKGS
@@ -134,6 +169,10 @@ function build_in_chroot {
        passwd -d root
        # profile
        echo -en "\nalias l='ls -al --color'\n\n" >> /etc/profile
+       # post install hooks
+       for file in `ls /post_routines`; do
+               . /post_routines/$file
+       done
 }
 
 #
@@ -141,38 +180,113 @@ function build_in_chroot {
 #
 
 function build_uboot() {
-       [ -f ./config.build ] && . ./config.build
-       git clone $UBSRC
+       echo "building u-boot ..."
+       echo "  argv: $@"
+       nopatch=0
+       noget=0
+       update=0
+       nocheckout=0
+       noclean=0
+       no2all=0
+       while [ "$1" ]; do
+               case "$1" in
+                       -nopatch)       nopatch=1; shift;;
+                       -noget)         noget=1; shift;;
+                       -update)        update=1; shift;;
+                       -nocheckout)    nocheckout=1; shift;;
+                       -noclean)       noclean=1; shift;;
+                       -no2all)        no2all=1; shift;;
+                       *) shift;;
+               esac
+       done
+       if [ "$no2all" = "1" ]; then
+               nopatch=1
+               noget=1
+               nocheckout=1
+               noclean=1
+       fi
+       [ -f ./build.config ] && . ./build.config
+       if [ "$noget" = "0" ]; then
+               rm -rf u-boot
+               git clone $UBSRC
+       fi
+       if [ "$update" = "1" ]; then
+               cd u-boot
+               git pull
+               cd ..
+       fi
        cd u-boot
-       if [ ! -z "$UBVER" ]; then
-               git checkout $UBVER
+       if [ "$nocheckout" = "0" ]; then
+               if [ ! -z "$UBVER" ]; then
+                       git checkout $UBVER
+               fi
+       fi
+       if [ "$nopatch" = "0" ]; then
+               if [ ! -z "$UBPATCH" ]; then
+                       for patch in $UBPATCH; do
+                               patch -Nfp1 < ../patches/$NAME/$patch
+                       done
+               fi
        fi
-       if [ ! -z "$UBPATCH" ]; then
-               patch -Nfp1 < ../patches/$UBPATCH
+       if [ "$noclean" = "0" ]; then
+               make $MAKEOPT distclean
        fi
-       make $MAKEOPT distclean
        make $MAKEOPT $UBCONF
        make $MAKEOPT
        cd ..
 }
 
 function build_kernel() {
-       [ -f ./config.build ] && . ./config.build
+       nopatch=0
+       noget=0
+       nohook=0
+       noclean=0
+       no2all=0
+       while [ "$1" ]; do
+               case "$1" in
+                       -nopatch)       nopatch=1; shift;;
+                       -noget)         noget=1; shift;;
+                       -nohook)        nohook=1; shift;;
+                       -noclean)       noclean=1; shift;;
+                       -no2all)        no2all=1; shift;;
+                       *) shift;;
+               esac
+       done
+       if [ "$no2all" = "1" ]; then
+               nopatch=1
+               noget=1
+               nohook=1
+               noclean=1
+       fi
+       [ -f ./build.config ] && . ./build.config
        KV=`echo $KERVER | awk -F. '{
                if($1=="3") print "3.x"
                else print$1 "." $2
        }'`
-       KERSRC="https://www.kernel.org/pub/linux/kernel/"
-       KERSRC="$KERSRC/v$KV/linux-${KERVER}.tar.bz2"
-       wget $KERSRC
-       tar xfj linux-${KERVER}.tar.bz2
+       if [ "$noget" = "0" ]; then
+               rm -rf linux-${KERVER}*
+               KERSRC="https://www.kernel.org/pub/linux/kernel/"
+               KERSRC="$KERSRC/v$KV/linux-${KERVER}.tar.bz2"
+               wget $KERSRC
+               tar xfj linux-${KERVER}.tar.bz2
+       fi
        cd linux-$KERVER
-       if [ ! -z "$KERPATCH" ]; then
-       patch -Nfp1 < ../patches/$PATCH
+       if [ "$nopatch" = "0" ]; then
+               if [ ! -z "$KERPATCH" ]; then
+                       for patch in $KERPATCH; do
+                               patch -Nfp1 < ../patches/$NAME/$patch
+                       done
+               fi
        fi
-       make $MAKEOPT mrproper
-       if [ ! -z "$KERHOOK" ]; then
-               $KERHOOK
+       if [ "$noclean" = "0" ]; then
+               make $MAKEOPT mrproper
+       fi
+       if [ "$nohook" = "0" ]; then
+               if [ ! -z "$KERHOOKS" ]; then
+                       for hook in $KERHOOKS; do
+                               . ../hooks/$hook
+                       done
+               fi
        fi
        if [ ! -z "$KERCONF" ]; then
                make $MAKEOPT $KERCONF
@@ -192,24 +306,24 @@ function build_sdcard() {
 #
 
 # configuration        
-if [ ! -f ./config.build ]; then
+if [ ! -f ./build.config ]; then
        echo "no configuration found, aborting ..."
 else
-       . ./config.build
+       . ./build.config
 fi
 
-# check prerequisites
-check_prereq
-
 # debootstrap
 if [ -z $1 ]; then
-       build_all > ./build.log 2>&1
+       check_prereq
+       build_all | tee ./build.log 2>&1
 elif [ "$1" = "chroot" ]; then
-       build_in_chroot > ./build_in_chroot.log 2>&1
+       build_in_chroot | tee ./build_in_chroot.log 2>&1
 elif [ "$1" = "uboot" ]; then
-       build_uboot > ./build_uboot.log 2>&1
+       check_prereq
+       build_uboot $@ | tee ./build_uboot.log 2>&1
 elif [ "$1" = "kernel" ]; then
-       build_kernel > ./build_kernel.log 2>&1
+       check_prereq
+       build_kernel $@ | tee ./build_kernel.log 2>&1
 else 
        echo "unknown option: '$1'"
 fi