Re: [Hackmeeting] Zen Room

Delete this message

Reply to this message
Autor: c1cc10@ecn.org
Data:  
Dla: hackmeeting
Temat: Re: [Hackmeeting] Zen Room
due
#!/bin/sh
# -> mksdk.sh ver 0.3
#
# bolic1 software development kit - the commandline tool
#
# Copyright (C) 2005  Francesco Rana  c1cc10@???
#                     Andrea Marchesini bakunin@???
#                     Federico Prando bomboclat@???
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published 
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
########################################################################
# standard output message routines
# it's always useful to wrap them, in case we change behaviour later
#
notice() { echo -e "\033[32m\a [*] \033[37m $1 "; }
act() { echo -e "\033[32m\a . \033[37m $1 "; }
error() { echo -e "\033[31m\a\a [!] \033[37m $1"; exit 1; }
func() { if [ $DEBUG ]; then echo -e "\033[32m [D] \033[37m $1"; fi }
### END


# some checks
if [ "`echo $UID`" != "0" ]; then
    error "you must be ROOT on your machine to use dyne:bolic SDK"
fi


# TODO: check out which arch we are riding...
#if [ "`uname -m | grep ppc`" ]; then

#[ "$1" == "-c" -o "$1" == "-a" ] || CBSOPTS="$1" && CBSOPTS=""
CBSOPTS="$1"
CURRENTDIR=`pwd`
WORKINGDIR=$CURRENTDIR/dynebolic

#######################################################################
# BASIC FUNCTIONS
#######################################################################
function basic_steps() {
    # did you already try with no success? let's check
    if [ `cat /proc/mounts | grep dynebolic | wc -l` != "0" ] ; then
        act "umounting proc and dev from previous attempt"
        umount $WORKINGDIR/proc &> /dev/null
        umount $WORKINGDIR/dev &> /dev/null
    fi
    [ -d $WORKINGDIR ] && notice "going to remove '${WORKINGDIR}'. proceed?[S/n]"
    read -t 5 SINO
    [ "${SINO}" == 'n' ] || rm -fr $WORKINGDIR

    
    # start from an empty dir
    [ -d $WORKINGDIR ] || mkdir $WORKINGDIR
    act "creating $WORKINGDIR"


    if ! [ -f $WORKINGDIR/etc/gentoo-release ] ; then
        if ! [ -f $CURRENTDIR/stage1-ppc-20050303.tar.bz2 ] ; then
            notice "do you have a valid stage1 ?\nPlease provide '/ full / path / to / it.bz2'\nor leave blank to download a brand new"
            read -t 5  GIVE_PATH
            if [ -z $GIVE_PATH ] ; then
            # TODO we must add some variable to select the architecture and the cpu version...
                wget ftp://ftp.belnet.be/mirror/rsync.gentoo.org/gentoo/releases/ppc/current/stages/ppc/stage1-ppc-20050303.tar.bz2 || error "Download error :/"


            elif ! [ `file $GIVE_PATH | cut -d" " -f2` != "bzip2" ] ; then
                error "wrong path!"
                act "Proceding to download.."
                wget ftp://ftp.belnet.be/mirror/rsync.gentoo.org/gentoo/releases/ppc/current/stages/ppc/stage1-ppc-20050303.tar.bz2 || error "Download error :/"
            fi
        fi
         act "found downloaded stage1. proceding to untar $GIVE_PATH into $WORKINGDIR"
        tar xvjf $CURRENTDIR/stage1-ppc-20050303.tar.bz2 -C $WORKINGDIR &> /dev/null 
                ! [ -d $WORKINGDIR/home/the_root ] && mkdir $WORKINGDIR/home/the_root
    fi

        
}

function get_ready_chroot() {
        if [ `cat /proc/mounts | grep dynebolic | wc -l` == "0" ] ; then
             mount -o bind /proc $WORKINGDIR/proc
             mount -o bind /dev $WORKINGDIR/dev
        fi
        [ -f $WORKINGDIR/etc/resolv.conf ] || cp -a /etc/resolv.conf $WORKINGDIR/etc/
        # First we check if it's just a jump into the SDK-system...
        if [ "${CBSOPTS}" == "-c" ] ; then
            chroot "${WORKINGDIR}" "${2}"
            exit 0;
        fi
cat > $WORKINGDIR/sbin/createbasesystem.sh << E0F



#!/bin/sh
# createbasesystem.sh
# software development kit - the commandline tool
#
# Copyright (C) 2004  Francesco Rana  c1cc10@???
#                     Andrea Marchesini bakunin@???
#                     Federico Prando bomboclat@??? 
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published 
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#


INSTDIR=/home/the_root
if ! [ -d "\${INSTDIR}/var/dynebolic" ]; then
    mkdir -p "\${INSTDIR}/var/dynebolic"
fi
CHECKPATH=\${INSTDIR}/var/dynebolic


########################################################################
# standard output message routines
# it's always useful to wrap them, in case we change behaviour later
#
notice() { echo -e "\033[31m [*] \$1\033[37m"; }
act() { echo -e "\033[32m . \$1\033[37m"; }
error() { echo -e "\033[31m [!] \$1\033[37m"; exit 1; }
func() { if [ \$DEBUG ]; then echo -e "\033[32m [D] \$1\033[31m"; fi }

function debug() {
    echo -e "\a\a\a\$?"
}


########################################################################
# MAIN FUNCTIONS
########################################################################

function set_flags() {
    notice "choose which CFLAG to set. default: '-O2 -mcpu=7450 -maltivec -mabi=altivec -mpowerpc-gfxopt -pipe'"
    read -t 15 CFLAG_SET
    [ -z \$CFLAG_SET ] && export CFLAG_SET="-O2 -mcpu=7450 -maltivec -mabi=altivec -mpowerpc-gfxopt -pipe" 
    act "\$CFLAG_SET  ...ok! modifying /etc/make.conf"


cat > /etc/make.conf << EOF
# These settings were set by the catalyst build script that automatically built this stage
# Please consult /etc/make.conf.example for a more detailed example
CFLAGS="\$CFLAG_SET"
CHOST="powerpc-unknown-linux-gnu"
CXXFLAGS="\${CFLAGS}"
EOF

}

function install_baselayout(){
    if ! [ -f ./baselayout.tgz ] ; then
        wget http://www.autistici.org/bolic1/baselayout.tgz 
    fi
    tar -C \$INSTDIR -zxvf ./baselayout.tgz || error "Can't untar the baselayout package :("
    rm ./baselayout.tgz || notice "Can't remove the baselayout.tgz package... do it by yourself, please"
    touch "\${CHECKPATH}"/.install_baselayout ;
    export CHECK="install_baselayout"
}


function check_step() {
    if ! [ -f "\${CHECKPATH}"/."\${CHECK}" ]; then
        error "something wicked happened. my checkpath is '\$CHECKPATH' and my check is '\$CHECK'\n"
    fi
    act "my checkpath is '\$CHECKPATH' and my check is '\$CHECK'. going to unset"
    unset \$CHECK
}



# get portage and bootstrap your compiling env
function initrd_root() {
    act "entering initrd_root"
    if [ -d /usr/portage ] ; then
        notice "there is allready a dir '/usr/portage'. Should I use it?(N/s)"
        read -t 5 YESNO
        if ! [ "\${YESNO}" == "s" -o "\${YESNO}" == "S" ] ; then
            emerge sync || error "Emerge error :/"
        fi
    else 
        emerge sync || error "Emerge error :/"
    fi


    if ! [ -d /var/tmp/portage/ ] ; then
        /usr/portage/scripts/bootstrap.sh
    else 
        notice "esiste gia' /var/tmp/portage. hai gia' compilato il bootstrap?(S/n)"
        read -t 5 YES_NO
        if  [ "\${YES_NO}" == "n" ] ; then
            /usr/portage/scripts/bootstrap.sh
        fi
    fi
    emerge gentoolkit || error "Emerge error :/"


    [ -d "\${INSTDIR}" ] || mkdir "\${INSTDIR}" && act "'\${INSTDIR}' exist."
    cp -a /var/db \$INSTDIR/var && act "copy var/db in the_root ..success!" 


    touch "\${CHECKPATH}"/.initrd_root
    CHECK="initrd_root" ; act "setting check flag to \$CHECK"
}



# install the base sources to have a 1 level FSH ready for becoming an initrd

function install_initrd_pkg () {
    check_step
    cat > /tmp/.initrd.lst << EOF
linux-headers
devfsd
glibc
ncurses
bash
coreutils
util-linux
iputils
net-tools
glib
expat
grep
gawk
zlib
EOF
    for PKG in \`cat /tmp/.initrd.lst\` ; do
        ROOT="\${INSTDIR}" emerge "\${PKG}" || error "Emerge '\${PKG}' error :/";
    done
    rm /tmp/.initrd.lst 
    touch "\${CHECKPATH}"/.install_initrd_pkg ;
    export CHECK="install_initrd_pkg"
}



function build_xfree() {
    check_step
    # We must copy this file 'couse else the libpam won't compile
    if ! [ -f \$INSTDIR/usr/lib/libglib.a ] ; then
        act "Goin' to copy the \$WORKINDIR/usr/lib/libglib.a in \$INSTDIR/usr/lib/libglib.a"
        cp -a \$WORKINGDIR/usr/lib/libglib.a \$INSTDIR/usr/lib/libglib.a || notice "can't find libglib.a" 
    fi
    ROOT=\$INSTDIR USE='mmx sse 3dfx 3dnow' emerge ati-drivers-8.13.3 || error "Emerge 'X' error :/"
    # Inserire qui la parte di autoconfigurazione (XFree86 -configure chrootato in /home/the_root/)


    ROOT=\$INSTDIR USE='mmx sse 3dfx 3dnow' emerge windowmaker || error "Emerge 'windowmaker' error :/"
    touch \${CHECKPATH}/.build_xfree
    export CHECK="build_xfree"
}


###############################################################################

##############################################################################
# Let's GO!
##############################################################################
# siamo dentro lo stage1. e' dove decidiamo i CFLAGS, per costruire prima l'ambiente di compilazione
# gentoo da cui poi deriviamo in /home/the_root la dynebolic.
# ogni aspetto l'abbiamo infilato in una funzione per auspicare uno sviluppo di ciascun passaggio


    case \$1 in
        -d)    
            CHECK="initrd_root" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"
            CHECK="install_initrd_pkg" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"
            CHECK="install_baselayout" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"
            CHECK="build_xfree" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"

    
            INITRD_ROOT_V=no
            INSTALL_INITRD_PKG_V=no
            INSTALL_BASELAYOUT_V=no
            BUILD_XFREE_V=no
            ;;
        -f)    set_flags 
            exit 0
            ;;

    
        -b)    
            CHECK="initrd_root" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"
            CHECK="install_initrd_pkg" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"

    
            INITRD_ROOT_V=no
            INSTALL_INITRD_PKG_V=no
            INSTALL_BASELAYOUT_V=yes
            BUILD_XFREE_V=yes
            ;;
        -i)    
            CHECK="initrd_root" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"

    
            INITRD_ROOT_V=no
            INSTALL_INITRD_PKG_V=yes
            INSTALL_BASELAYOUT_V=yes
            BUILD_XFREE_V=yes
            ;;
        -x)    
            CHECK="initrd_root" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"
            CHECK="install_initrd_pkg" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"
            CHECK="install_baselayout" ; [ -f \`ls "\${CHECKPATH}"/."\${CHECK}"\` ] || error "\$CHECK not complete!"

    
            INITRD_ROOT_V=no
            INSTALL_INITRD_PKG_V=no
            INSTALL_BASELAYOUT_V=no
            BUILD_XFREE_V=yes
            ;;
        -v)    head -n 22 \$0 | tail -n 21 ;;
        -a)    INITRD_ROOT_V=yes;
            INSTALL_INITRD_PKG_V=yes
            INSTALL_BASELAYOUT_V=yes
            BUILD_XFREE_V=yes
            ;;
        *)    error "read help with -h or --help!" 
            ;;
        -h|--help)
            act "-h show this help"
            act "-d restart from configuration files (baselayout-db)" 
             act "-f set flags"
            act "-i restart from initrd" 
            act "-x restart from xfree"
            exit 1
            ;;
    esac


env-update

source /etc/profile

set_flags

if [ "\${INITRD_ROOT_V}" == "yes" ] ; then
    initrd_root && notice "done with initrd_root." || error "something wrong with initrd_root."
fi


if [ "\${INSTALL_INITRD_PKG_V}" == "yes" ] ; then
    install_initrd_pkg && notice "install_initrd_pkg complete." || error "something wrong with install_initrd_pkg."
fi


if [ "\${INSTALL_BASELAYOUT_V}" == "yes" ] ; then
    install_baselayout && notice "install_baselayout complete." || error "something wrong with install_baselayout" 
fi


if [ "\${BUILD_XFREE_V}" == "yes" ] ; then
    build_xfree  && notice "build_xfree complete." || error "something wrong with build_xfree."
fi



E0F

        chmod 740 "${WORKINGDIR}"/sbin/createbasesystem.sh        
        if [ "${CBSOPTS}" == "-b" -o "${CBSOPTS}" == "-f" -o "${CBSOPTS}" == "-x" -o \
                "${CBSOPTS}" == "-i" -o "${CBSOPTS}" == "-d" ] ; then
            chroot "${WORKINGDIR}" ./sbin/createbasesystem.sh "${CBSOPTS}"
        else
            notice "No option for createbasesystem.sh... Starting all the d::b process from the beginning!"    
            act "${CBSOPTS}"
            chroot "${WORKINGDIR}" ./sbin/createbasesystem.sh -a
        fi
}


######################################################################
# HERE WE GO!
######################################################################

case $1 in
    -a) # Get the stage1 for your compiling environment
        act "entering basic_steps"
        basic_steps
        act "entering get_ready_chroot"
        get_ready_chroot 
        ;;
    -c) # Done with basic stuff, let's get into the chroot for compiling
        act "entering get_ready_chroot"
        if [ -z "$2" ] ; then
            CHCOMMAND="/bin/bash"
        else
            CHCOMMAND="$2"
        fi
        # It's rought, but should work for most cases...
        get_ready_chroot -c ${CHCOMMAND}
        ;;
    -h|--help)
        act "COMMON TASKS:"
         act "-h show this help"
              act "-a start the entire process from beginning (WARN! will delete previous attempts)"
              act "-c already downloaded and untarred. chroot me!"
         act "-v show details"
        act "Commands passed to createbasesystem.sh:"
        act "-b install the baselayout (conf files and HFS directories). You must have already an sdk installed" 
        act "-d get latest baselayout-db and build it (you must have already an sdk installed)" 
         act "-f set flags (you must have allready an sdk installed)"
        act "-i restart from initrd" 
         act "-x build Xfree86 (you must have allready an sdk installed)"
        ;;
        # options to pass to createbasesystem.sh
    -b)    get_ready_chroot -b 
        ;;
    -d)    get_ready_chroot -d 
        ;;
    -f)    get_ready_chroot -f 
        ;;
    -i)    get_ready_chroot -i 
        ;;
    -x)    get_ready_chroot -x 
        ;;
    -v)     head -n 22 $0 | tail -n 21 
        ;;
    *)    error "read help with -h or --help!" 
        ;;
esac