smarter modules.update, was missing onelab-specific modules
[build.git] / vbuild-init-vserver.sh
index 17219d1..31514cb 100755 (executable)
@@ -1,5 +1,6 @@
 #!/bin/bash
 # -*-shell-*-
+# $Id$
 
 COMMAND=$(basename $0)
 DIRNAME=$(dirname $0)
@@ -10,6 +11,10 @@ PATH=$(dirname $0):$PATH . build.common
 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,7 +32,7 @@ function configure_yum_in_vserver () {
     vserver=$1; shift
     fcdistro=$1; shift
 
-    cd /etc/vservers/.distributions/${fcdistro}
+    pushd /etc/vservers/.distributions/${fcdistro}
     if [ -f yum/yum.conf ] ; then
        echo "Initializing yum.conf in $vserver from $(pwd)/yum"
         sed -e "s!@YUMETCDIR@!/etc!g;
@@ -67,7 +72,7 @@ gpgcheck=0
 EOF
        fi
     fi
-    cd -
+    popd
 }    
 
 function setup_vserver () {
@@ -89,10 +94,10 @@ function setup_vserver () {
     # 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
+    for i in $(seq 20) ; do
        $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS -m yum -- -d $fcdistro && break || true
-       echo "Waiting for one minute"
-       sleep 60
+       echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
+       sleep 3
     done
     # check success
     [ -d /vservers/$vserver ] 
@@ -129,21 +134,56 @@ function setup_vserver () {
     fi
 
     $personality vyum $vserver -- -y install yum
-    $personality vserver $VERBOSE $vserver pkgmgmt internalize
+    # 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
 
     # start the vserver so we can do the following operations
     $personality vserver $VERBOSE $vserver start
     $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
     $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
 
+    # with vserver 2.3, granting the vserver CAP_MKNOD is not enough
+    # check whether we run vs2.3 or above
+    vs_version=$(uname -a  | sed -e 's,.*[\.\-]vs\([0-9]\)\.\([0-9]\)\..*,\1\2,')
+    # at this stage we have here 22 or 23
+    need_vdevmap=$(( $vs_version >= 23 ))
+
+    if [ "$need_vdevmap" == 1 ] ; then
+       util_vserver_215=0
+       vdevmap --help | grep -- --set &> /dev/null && util_vserver_215=1
+       
+       if [ "$util_vserver_215" == 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
+       else
+           echo "You seem to be running vs2.3 with util-vserver < 0.30.215"
+           echo "This combination is not supported by $COMMAND"
+           echo "Please upgrade your environment"
+           exit 1
+# this supposedly is an equivalent to using vdevmap as invoked above
+# but it's not going to work in this case
+#          mkdir -p /etc/vservers/$vserver/apps/vdevmap/default-{block,char}
+#          touch /etc/vservers/$vserver/apps/vdevmap/default-{block,char}/{open,create}
+#          echo /dev/root > /etc/vservers/$vserver/apps/vdevmap/default-block/target
+#          echo /dev/null > /etc/vservers/$vserver/apps/vdevmap/default-char/target
+       fi
+    fi
+           
     # minimal config in the vserver for yum to work
     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
 }
 
-function devel_tools () {
+function devel_or_vtest_tools () {
 
     set -x 
     set -e 
@@ -162,9 +202,12 @@ function devel_tools () {
     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)
+    
+    packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
+    groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
 
     [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
     [ -n "$groups" ] && $personality vserver $vserver exec yum -y groupinstall $groups
@@ -177,6 +220,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 () {
@@ -253,6 +299,9 @@ function post_install_myplc  () {
     # 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
+
     # customize root's prompt
     cat << PROFILE > /root/.profile
 export PS1="[$vserver] \\w # "
@@ -261,8 +310,34 @@ PROFILE
 EOF
 }
 
-COMMAND_VBUILD="vbuild-init-vserver.sh"
-COMMAND_MYPLC="vtest-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 [options] vserver-name [ -- vserver-options ]"
@@ -275,6 +350,7 @@ function usage () {
     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 " -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>"
@@ -298,11 +374,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
@@ -328,12 +407,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
 
 }