using tagged config for gnuradio
[build.git] / vbuild-init-vserver.sh
index 8b21957..4c71dd2 100755 (executable)
@@ -1,5 +1,8 @@
 #!/bin/bash
 # -*-shell-*-
+# $Id$
+
+#shopt -s huponexit
 
 COMMAND=$(basename $0)
 DIRNAME=$(dirname $0)
@@ -7,9 +10,13 @@ DIRNAME=$(dirname $0)
 # pkgs parsing utilities
 PATH=$(dirname $0):$PATH . build.common
 
-DEFAULT_FCDISTRO=f7
+DEFAULT_FCDISTRO=f8
 DEFAULT_PLDISTRO=planetlab
 DEFAULT_PERSONALITY=linux32
+DEFAULT_IFNAME=eth0
+
+COMMAND_VBUILD="vbuild-init-vserver.sh"
+COMMAND_MYPLC="vtest-init-vserver.sh"
 
 function failure () {
     echo "$COMMAND : Bailing out"
@@ -27,24 +34,24 @@ function configure_yum_in_vserver () {
     vserver=$1; shift
     fcdistro=$1; shift
 
-    cd /etc/vservers/.distributions/${fcdistro}
-    if [ -f yum/yum.conf ] ; then
-       echo "Initializing yum.conf in $vserver from $(pwd)/yum"
+    templates=/etc/vservers/.distributions/${fcdistro}
+    if [ -f ${templates}/yum/yum.conf ] ; then
+       echo "Initializing yum.conf in $vserver from ${templates}/yum"
         sed -e "s!@YUMETCDIR@!/etc!g;
                 s!@YUMCACHEDIR@!/var/cache/yum!g;
                 s!@YUMLOGDIR@!/var/log!g;
                 s!@YUMLOCKDIR@!/var/lock!g;
-               " yum/yum.conf > /vservers/$vserver/etc/yum.conf
+               " ${templates}/yum/yum.conf > /vservers/$vserver/etc/yum.conf
 
        # post process the various @...@ variables from this yum.conf file.
     else
        echo "Using $fcdistro default for yum.conf"
     fi
 
-    if [ -d yum.repos.d ] ; then
-       echo "Initializing yum.repos.d in $vserver from $(pwd)/yum.repos.d"
+    if [ -d ${templates}/yum.repos.d ] ; then
+       echo "Initializing yum.repos.d in $vserver from ${templates}/yum.repos.d"
        rm -rf /vservers/$vserver/etc/yum.repos.d
-       tar cf - yum.repos.d | tar -C /vservers/$vserver/etc -xvf -
+       tar -C ${templates} -cf - yum.repos.d | tar -C /vservers/$vserver/etc -xvf -
     else
        echo "Cannot initialize yum.repos.d in $vserver"
     fi
@@ -54,8 +61,8 @@ function configure_yum_in_vserver () {
            echo "WARNING : cannot create myplc repo"
        else
             # exclude kernel from fedora repos 
-           for i in /vservers/$vserver/etc/yum.repos.d/* ; do
-               echo "exclude=kernel* ulogd iptables" >> $i
+           for repo in /vservers/$vserver/etc/yum.repos.d/* ; do
+               [ -f $repo ] && yumconf_exclude $repo "exclude=$pl_KEXCLUDES" 
            done
            # the build repo is not signed at this stage
            cat > /vservers/$vserver/etc/yum.repos.d/myplc.repo <<EOF
@@ -67,9 +74,32 @@ gpgcheck=0
 EOF
        fi
     fi
-    cd -
 }    
 
+# return yum or debootstrap
+function package_method () {
+    fcdistro=$1; shift
+    case $fcdistro in
+       f[0-9]*|centos[0-9]*) echo yum ;;
+       lenny|etch) echo debootstrap ;;
+       *) echo Unknown distro $fcdistro ;;
+    esac 
+}
+
+# return arch from debian distro and personality
+function canonical_arch () {
+    personality=$1; shift
+    fcdistro=$1; shift
+    case $(package_method $fcdistro) in
+       yum)
+           case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
+       debootstrap)
+           case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
+       *)
+           echo Unknown-arch-3 ;;
+    esac
+}
+
 function setup_vserver () {
 
     set -x
@@ -85,27 +115,45 @@ function setup_vserver () {
        exit 1
     fi
 
+    pkg_method=$(package_method $fcdistro)
+    case $pkg_method in
+       yum)
+           build_options="-m yum -- -d $fcdistro" 
+           ;;
+       debootstrap)
+           arch=$(canonical_arch $personality $fcdistro)
+           build_options="-m debootstrap -- -d $fcdistro -- --arch $arch"
+           ;;
+       *)
+           build_options="something wrong" ;;
+    esac
+
     # create it
     # try to work around the vserver issue:
     # vc_ctx_migrate: No such process
     # rpm-fake.so: failed to initialize communication with resolver
-    for i in 1 2 3 4 5 ; do
-       $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS -m yum -- -d $fcdistro && break || true
-       echo "Waiting for one minute"
-       sleep 60
+    for i in $(seq 20) ; do
+       $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS $build_options && break || true
+       echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
+       sleep 3
     done
     # check success
     [ -d /vservers/$vserver ] 
 
     if [ ! -z "$personality" ] ; then
-       registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
+       if [ -f "/etc/vservers/$vserver/personality" ] ; then
+           registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
+       else
+           registered_personality=0
+       fi
        if [ $registered_personality -eq 0 -a "$personality" != "linux64" ] ; then
            echo $personality >> /etc/vservers/$vserver/personality
        fi
     fi
 
     if [ -n "$VBUILD_MODE" ] ; then 
-    # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
+       ### capabilities required for a build vserver
+        # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
        BCAPFILE=/etc/vservers/$vserver/bcapabilities
        touch $BCAPFILE
        cap=$(grep ^CAP_SYS_ADMIN /etc/vservers/$vserver/bcapabilities | wc -l)
@@ -114,24 +162,58 @@ function setup_vserver () {
        [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
        cap=$(grep ^CAP_IPC_LOCK /etc/vservers/$vserver/bcapabilities | wc -l)
        [ $cap -eq 0 ] && echo 'CAP_IPC_LOCK' >> /etc/vservers/$vserver/bcapabilities
+    else
+       ### capabilities required for a myplc vserver
+       # for /etc/plc.d/gpg - need to init /dev/random
+       cap=$(grep ^CAP_MKNOD /etc/vservers/$vserver/bcapabilities | wc -l)
+       [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
+       cap=$(grep ^CAP_NET_BIND_SERVICE /etc/vservers/$vserver/bcapabilities | wc -l)
+       [ $cap -eq 0 ] && echo 'CAP_NET_BIND_SERVICE' >> /etc/vservers/$vserver/bcapabilities
     fi
 
-    $personality vyum $vserver -- -y install yum
-    $personality vserver $VERBOSE $vserver pkgmgmt internalize
+    if [ "$pkg_method" = "yum" ] ; then
+       $personality vyum $vserver -- -y install yum
+        # ditto
+       for i in $(seq 20) ; do
+           $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
+           echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
+           sleep 3
+       done
+    fi
 
     # start the vserver so we can do the following operations
+#    rm -f /tmp/go*
+#    echo -n ' about to start - WAITING for /tmp/go1' ; while true ; do [ -f /tmp/go1 ] && break || : ; done
     $personality vserver $VERBOSE $vserver start
-    $personality vserver $VERBOSE $vserver exec rm -f /var/lib/rpm/__db*
-    $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
-
+#    echo -n ' started - WAITING for /tmp/go2' ; while true ; do [ -f /tmp/go2 ] && break || : ; done
+#if true ; then 
+#    echo SKIPPING for debug --- vserver was started
+#else
+    [ "$pkg_method" = "yum" ] && $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
+    [ "$pkg_method" = "yum" ] && $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
+
+    # check if the vserver kernel is using VSERVER_DEVICE (vdevmap) support
+    need_vdevmap=$(grep "CONFIG_VSERVER_DEVICE=y" /boot/config-$(uname -r) | wc -l)
+
+    if [ $need_vdevmap -eq 1 ] ; then
+       ctx=$(cat /etc/vservers/$vserver/context)
+       vdevmap --set --xid $ctx --open --create --target /dev/null
+       vdevmap --set --xid $ctx --open --create --target /dev/root
+    fi
+           
     # minimal config in the vserver for yum to work
-    configure_yum_in_vserver $vserver $fcdistro 
+    [ "$pkg_method" = "yum" ] && configure_yum_in_vserver $vserver $fcdistro 
 
     # set up resolv.conf
     cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
+    # and /etc/hosts for at least localhost
+    [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
+
+#fi
+
 }
 
-function devel_tools () {
+function devel_or_vtest_tools () {
 
     set -x 
     set -e 
@@ -142,20 +224,33 @@ function devel_tools () {
     pldistro=$1; shift
     personality=$1; shift
 
+    pkg_method=$(package_method $fcdistro)
+
     # check for .pkgs file based on pldistro
     if [ -n "$VBUILD_MODE" ] ; then
        pkgsname=devel.pkgs
     else
-       pkgsname=native-shell.pkgs
+       pkgsname=vtest.pkgs
     fi
     pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
 
-    # install individual packages, then groups
-    packages=$(pl_getPackages ${fcdistro} $pkgsfile)
-    groups=$(pl_getGroups ${fcdistro} $pkgsfile)
+    ### install individual packages, then groups
+    # get target arch - use uname -i here (we want either x86_64 or i386)
+    vserver_arch=$($personality vserver $vserver exec uname -i)
+    # on debian systems we get arch through the 'arch' command
+    [ "$vserver_arch" = "unknown" ] && vserver_arch=$($personality vserver $vserver exec arch)
+    
+    packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
+    groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
+
+    [ "$pkg_method" = yum ] && [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
+    [ "$pkg_method" = yum ] && [ -n "$groups" ] && $personality vserver $vserver exec yum -y groupinstall $groups
 
-    [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
-    [ -n "$groups" ] && $personality vserver $vserver exec yum -y groupinstall $groups
+    [ "$pkg_method" = debootstrap ] && $personality vserver $vserver exec apt-get update
+    [ "$pkg_method" = debootstrap ] && for package in $packages ; do 
+       $personality vserver $vserver exec apt-get install -y $package 
+    done
+    
     return 0
 }
 
@@ -165,6 +260,9 @@ function post_install () {
     else
        post_install_myplc "$@"
     fi
+    # setup localtime from the host
+    vserver=$1; shift 
+    cp /etc/localtime /vservers/$vserver/etc/localtime
 }
 
 function post_install_vbuild () {
@@ -185,7 +283,7 @@ function post_install_vbuild () {
     done
     
     # create symlink for /dev/fd
-    ln -fs /proc/self/fd /dev/fd
+    [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
 
     # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
     sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
@@ -238,6 +336,18 @@ function post_install_myplc  () {
 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
     cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
 
+    # create /etc/sysconfig/network if missing
+    [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
+
+    # create symlink for /dev/fd
+    [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
+
+    # turn off regular crond, as plc invokes plc_crond
+    chkconfig crond off
+
+    # take care of loginuid in /etc/pam.d 
+    sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
+
     # customize root's prompt
     cat << PROFILE > /root/.profile
 export PS1="[$vserver] \\w # "
@@ -246,24 +356,51 @@ PROFILE
 EOF
 }
 
-COMMAND_VBUILD="vbuild-init-vserver.sh"
-COMMAND_MYPLC="myplc-init-vserver.sh"
+# parses ifconfig's output to find out ip address and mask
+# will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
+# default is to use lo, that's enough for local mirrors
+# use -i eth0 in case your fedora mirror is on a separate box on the network
+function vserverIfconfig () {
+    ifname=$1; shift
+    local result="" 
+    line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
+    if [ -n "$line" ] ; then
+       set $line
+       for word in "$@" ; do
+           addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
+           mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
+           if [ "$word" != "$addr" ] ; then
+               result="${addr}"
+           elif [ "$word" != "$mask" ] ; then
+               result="${result}/${mask}"
+           fi
+       done
+    fi
+    if [ -z "$result" ] ; then 
+       echo "vserverIfconfig failed to locate $ifname"
+       exit 1
+    else
+       echo $result
+    fi
+}
+
 function usage () {
     set +x 
-    echo "Usage: $COMMAND_VBUILD [-v] vserver-name [ -- vserver-options ]"
-    echo "Usage: $COMMAND_MYPLC [-v] vserver-name repo-url [ -- vserver-options ]"
+    echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
+    echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
     echo "Requirements: you need to have a vserver-compliant kernel,"
-    echo "  as well as the util-vserver RPM installed"
+    echo "   as well as the util-vserver RPM installed"
     echo "Description:"
-    echo "  This command creates a fresh vserver instance, for building, or running, myplc"
+    echo "   This command creates a fresh vserver instance, for building, or running, myplc"
     echo "Supported options"
     echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
-    echo " -v : passes -v to calls to vserver"
+    echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
+    echo " -v : verbose - passes -v to calls to vserver"
     echo "vserver-options"
-    echo " all args after the optional -- are passed to vserver <name> build <options>"
-    echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
+    echo "  all args after the optional -- are passed to vserver <name> build <options>"
+    echo "  typical usage is e.g. --interface eth0:200.150.100.10/24"
     exit 1
 }
 
@@ -283,11 +420,14 @@ function main () {
     esac
 
     VERBOSE=
-    while getopts "f:d:p:v" opt ; do
+    IFNAME=""
+    VSERVER_OPTIONS=""
+    while getopts "f:d:p:i:v" opt ; do
        case $opt in
            f) fcdistro=$OPTARG;;
            d) pldistro=$OPTARG;;
            p) personality=$OPTARG;;
+           i) IFNAME=$OPTARG;;
            v) VERBOSE="-v" ;;
            *) usage ;;
        esac
@@ -313,12 +453,21 @@ function main () {
        fi
     fi
 
+    # with new util-vserver, it is mandatory to provide an IP even for building
+    if [ -n "$VBUILD_MODE" ] ; then
+       [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
+    fi
+    if [ -n "$IFNAME" ] ; then
+       localip=$(vserverIfconfig $IFNAME)
+       VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
+    fi
+
     [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
     [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
     [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
 
     setup_vserver $vserver $fcdistro $personality 
-    devel_tools $vserver $fcdistro $pldistro $personality
+    devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
     post_install $vserver $personality
 
 }