From: hackbard Date: Sun, 4 Jan 2015 21:56:55 +0000 (+0100) Subject: initial checkin of debin image builder X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=b0954d42425046f559118b2cba77b718f5be5295;p=outofuni%2Fdib.git initial checkin of debin image builder --- b0954d42425046f559118b2cba77b718f5be5295 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..3446087 --- /dev/null +++ b/build.sh @@ -0,0 +1,216 @@ +#!/bin/bash + +# debian image build tool +# author hackbard@hackdaworld.org + +function check_prereq() { + # read config + [ -f ./config.build ] && . ./config.build + # check + tc="gcc-`echo $MAKEOPT | sed 's/.*\ CROSS_COMPILE=\(.*\)-.*/\1/'`" + deps=0 + for deb in debootstrap binfmt-support qemu-user-static $tc; do + ip=`dpkg -l | grep $deb | awk '{ print $2 }'` + if [ -z "$ip" ]; then + echo "package $deb required but not installed" + deps=1 + fi + done + if [ "$deps" = "1" ]; then + echo "install prerequisites" + exit 1 + fi +} + +function umount_if_mounted() { + mnt=$1 + rpath="`realpath $PWD`" + mntpt=`mount | grep $rpath/$mnt | awk '{ print $3 }'` + if [ ! -z "$mntpt" ]; then + echo "umounting $mntpt" + umount $mntpt + fi +} + +function build_all { + # read config + [ -f ./config.build ] && . ./config.build + # 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" + fi + 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 + umount_if_mounted rootfs.mnt + rm -rf rootfs.mnt + mkdir rootfs.mnt + mount -o loop ./rootfs.img ./rootfs.mnt + # debootstrap first part + debootstrap --verbose --arch $ARCH --variant=minbase \ + --foreign $SUITE rootfs.mnt $DEBMIRROR + # copy emulator + cp -v `which $EMU` ./rootfs.mnt/usr/bin + touch .build_stage1 + fi + # copy myself + cp -v ./build.sh ./rootfs.mnt + cp -v ./config.build ./rootfs.mnt + if [ ! -f .build_stage2 ]; then + # prepare/run second stage + modprobe binfmt_misc + mnts="rootfs.mnt/dev/pts rootfs.mnt/proc" + for mnt in $mnts; do + umount_if_mounted $mnt + done + mount -t proc proc ./rootfs.mnt/proc + mkdir -p ./rootfs.mnt/dev/pts + mount -t devpts devpts ./rootfs.mnt/dev/pts + chroot ./rootfs.mnt /build.sh chroot + # + # running build_stage2() + # + for mnt in $mnts; do + umount_if_mounted $mnt + done + touch .build_stage2 + # cleanup + rm -v ./rootfs.mnt/build.sh + rm -v ./rootfs.mnt/config.build + fi +} + +function build_in_chroot { + # read config + [ -f ./config.build ] && . ./config.build + # build second stage + echo "building debian image, second stage ..." + [ -f /debootstrap/debootstrap ] && \ + /debootstrap/debootstrap --second-stage + # additional packages + configuration + echo "deb ${DEBMIRROR}/ $SUITE main contrib non-free" > \ + /etc/apt/sources.list + echo "deb-src ${DEBMIRROR}/ $SUITE main contrib non-free" >> \ + /etc/apt/sources.list + # run apt + apt-get update + apt-get -y install $PKGS + # locales + locale-gen --purge en_US.UTF-8 + echo 'LANG="en_US.UTF-8"' > /etc/default/locale + echo 'LANGUAGE="en_US:en"' >> /etc/default/locale + # network + 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 + echo "CONCURRENCY=shell" >> /etc/default/rcS + # additional packages + apt-get -y install $ADDPKGS + # (un)set passwd + passwd -d root + # profile + echo -en "\nalias l='ls -al --color'\n\n" >> /etc/profile +} + +# +# post chroot tasks +# + +function build_uboot() { + [ -f ./config.build ] && . ./config.build + git clone $UBSRC + cd u-boot + if [ ! -z "$UBVER" ]; then + git checkout $UBVER + fi + if [ ! -z "$UBPATCH" ]; then + patch -Nfp1 < ../patches/$UBPATCH + fi + make $MAKEOPT distclean + make $MAKEOPT $UBCONF + make $MAKEOPT + cd .. +} + +function build_kernel() { + [ -f ./config.build ] && . ./config.build + 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 + cd linux-$KERVER + if [ ! -z "$KERPATCH" ]; then + patch -Nfp1 < ../patches/$PATCH + fi + make $MAKEOPT mrproper + if [ ! -z "$KERHOOK" ]; then + $KERHOOK + fi + if [ ! -z "$KERCONF" ]; then + make $MAKEOPT $KERCONF + fi + make $MAKEOPT zImage + make $MAKEOPT modules + make $MAKEOPT INSTALL_MOD_PATH=../rootfs.mnt modules_install + cd .. +} + +function build_sdcard() { + echo "creating sdcard ..." +} + +# +# main +# + +# configuration +if [ ! -f ./config.build ]; then + echo "no configuration found, aborting ..." +else + . ./config.build +fi + +# check prerequisites +check_prereq + +# debootstrap +if [ -z $1 ]; then + build_all > ./build.log 2>&1 +elif [ "$1" = "chroot" ]; then + build_in_chroot > ./build_in_chroot.log 2>&1 +elif [ "$1" = "uboot" ]; then + build_uboot > ./build_uboot.log 2>&1 +elif [ "$1" = "kernel" ]; then + build_kernel > ./build_kernel.log 2>&1 +else + echo "unknown option: '$1'" +fi + diff --git a/config.build b/config.build new file mode 100644 index 0000000..edb6257 --- /dev/null +++ b/config.build @@ -0,0 +1,71 @@ +# debian image build configuration + +# +# size of the image in MB +# +# 2GB - 48MB +IMGSIZE=2000 + +# +# target architecture / suite +# +ARCH=arm +#ARCH=armel +MAKEOPT="ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-" +#MAKEOPT="ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-" +EMU=qemu-arm-static +SUITE=unstable + +# name +NAME=beagleboard +#NAME=nellboard + +# +# filesystems +# +ROOTFS=xfs +#ROOTFS=ext4 +BOOTFS=ext3 + +# debian mirror +DEBMIRROR=http://ftp.de.debian.org/debian + +# packages +PKGS="apt-utils dialog locales" + +# serial console +GETTY=ttyO0 + +# add your packages here +ADDPKGS="vim git make gcc lua5.2 golang net-tools iputils-ping kmod" +ADDPKGS="$ADDPKGS wget openssh-server" +# beagle +# nellboard +#ADDPKGS="$ADDPKGS python dnsmasq" + +# +# kernel +# +# beagleboard +# use/copy debian linux-image package! +# nellboard +KERVER="2.6.33" +KERPATCH="linux-2.6.33_lpc313x-v1.01.patch" +KERCONF="ea313x_defconfig" +KERHOOK="sed 's/# CONFIG_DEVTMPFS.*/CONFIG_DEVTMPFS=y/' -i ./arch/arm/configs/ea313x_defconfig;" +KERHOOK="sed 's/# CONFIG_EXT4_FS.*/CONFIG_EXT4_FS=y/' -i ./arch/arm/configs/ea313x_defconfig;" +KERHOOK="$KERHOOK cp ../files/lpc313x/ea313x.c arch/arm/mach-lpc313x/;" + +# +# u-boot patch and target +# +UBSRC="git://git.denx.de/u-boot.git" +# beagleboard +#UBSRC="" +#UBPATCH="" +UBCONF=omap3_beagle_config +# nellboard +#UBVER="v2009.11" +#UBPATCH="u-boot-2009.11_lpc313x-v1.01.patch" +#UBCONF="EA3131_config" +