added nellboard post routines (not yet completed!)
[outofuni/dib.git] / build.sh
index 703a604..48ea033 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -41,22 +41,21 @@ function build_all {
        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
                fi
-               if [ "$validimg" = "no" ]; then
-                       dd if=/dev/zero of=./rootfs.img bs=1M count=$IMGSIZE
-               fi
                # make fs and mount
                umount_if_mounted rootfs.mnt
-               mkfs.${ROOTFS} -f -L rootfs ./rootfs.img
+               FORCEFS="-F"
+               [ "$ROOTFS" = "xfs" ] && FORCEFS="-f"
+               mkfs.${ROOTFS} $FORCEFS -L rootfs ./rootfs.img
                rm -rf rootfs.mnt
                mkdir rootfs.mnt
                mount -o loop ./rootfs.img ./rootfs.mnt
@@ -70,6 +69,10 @@ function build_all {
        # copy myself
        cp -v ./build.sh ./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
@@ -91,6 +94,18 @@ function build_all {
                # cleanup
                rm -v ./rootfs.mnt/build.sh
                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
 }
 
@@ -117,16 +132,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 +164,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
 }
 
 #
@@ -185,7 +219,7 @@ function build_uboot() {
        if [ "$nopatch" = "0" ]; then
                if [ ! -z "$UBPATCH" ]; then
                        for patch in $UBPATCH; do
-                               patch -Nfp1 < ../patches/$patch
+                               patch -Nfp1 < ../patches/$NAME/$patch
                        done
                fi
        fi
@@ -235,7 +269,7 @@ function build_kernel() {
        if [ "$nopatch" = "0" ]; then
                if [ ! -z "$KERPATCH" ]; then
                        for patch in $KERPATCH; do
-                               patch -Nfp1 < ../patches/$patch
+                               patch -Nfp1 < ../patches/$NAME/$patch
                        done
                fi
        fi
@@ -243,8 +277,10 @@ function build_kernel() {
                make $MAKEOPT mrproper
        fi
        if [ "$nohook" = "0" ]; then
-               if [ ! -z "$KERHOOK" ]; then
-                       $KERHOOK
+               if [ ! -z "$KERHOOKS" ]; then
+                       for hook in $KERHOOKS; do
+                               . ../hooks/$hook
+                       done
                fi
        fi
        if [ ! -z "$KERCONF" ]; then