lxc root fs might be created already but almost empty, with the timestamp in it only
[build.git] / lbuild-initvm.sh
index 3bdd9fa..82793ed 100755 (executable)
@@ -23,17 +23,6 @@ COMMAND_LBUILD="lbuild-initvm.sh"
 COMMAND_LTEST="ltest-initvm.sh"
 
 ##########
-# when creating build boxes we use private NAT'ed addresses for the VMs
-# as per virbr0 that is taken care of by libvirt at startup
-PRIVATE_BRIDGE="virbr0"
-PRIVATE_PREFIX="192.168.122."
-PRIVATE_GATEWAY="192.168.122.1"
-# beware that changing this would break the logic of random_private_byte...
-PRIVATE_MASKLEN=24
-
-# we just try randomly in that range until a free IP address shows up
-PRIVATE_ATTEMPTS=20
-
 # constant
 PUBLIC_BRIDGE=br0
 
@@ -63,22 +52,6 @@ function discover_interface () {
     # still not found ? that's bad
     echo unknown
 }
-########## check for a free IP
-function ip_is_busy () {
-    target=$1; shift
-    ping -c 1 -W 1 $target >& /dev/null
-}
-
-function random_private_byte () {
-    for attempt in $(seq $PRIVATE_ATTEMPTS); do
-       byte=$(($RANDOM % 256))
-       if [ "$byte" == 0 -o "$byte" == 1 ] ; then continue; fi
-       ip=${PRIVATE_PREFIX}${byte}
-       ip_is_busy $ip || { echo $byte; return; }
-    done
-    echo "Cannot seem to find a free IP address in range ${PRIVATE_PREFIX}.xx/24 after $PRIVATE_ATTEMPTS attempts - exiting"
-    exit 1
-}
 
 ########## networking -- ctd
 function gethostbyname () {
@@ -532,7 +505,7 @@ function write_guest_interfaces_test () {
     cat <<EOF
 auto $VIF_GUEST
 iface $VIF_GUEST
-    address $IP
+    address $GUEST_IP
     netmask $NETMASK
     gateway $GATEWAY
 EOF
@@ -673,14 +646,14 @@ MTU=1500
 EOF
 }
 
-# use fixed IP as specified by GUEST_HOSTNAME
+# use fixed GUEST_IP as specified by GUEST_HOSTNAME
 function write_guest_ifcfg_test () {
     cat <<EOF
 DEVICE=$VIF_GUEST
 BOOTPROTO=static
 ONBOOT=yes
 HOSTNAME=$GUEST_HOSTNAME
-IPADDR=$IP
+IPADDR=$GUEST_IP
 NETMASK=$NETMASK
 GATEWAY=$GATEWAY
 NM_CONTROLLED=no
@@ -883,7 +856,7 @@ function wait_for_ssh () {
 
     lxc=$1; shift
   
-    echo $IP is up, waiting for ssh...
+    echo network in guest is up, waiting for ssh...
 
     #wait max 5 min for sshd to start 
     ssh_up=""
@@ -893,7 +866,7 @@ function wait_for_ssh () {
     counter=1
     while [ "$current_time" -lt "$stop_time" ] ; do
          echo "$counter-th attempt to reach sshd in container $lxc ..."
-         ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
+         ssh -o "StrictHostKeyChecking no" $GUEST_IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
          sleep 10
          current_time=$(($current_time + 10))
          counter=$(($counter+1))
@@ -948,8 +921,6 @@ function main () {
            usage ;;
     esac
 
-    echo 'build mode=' $BUILD_MODE 'test mode=' $TEST_MODE
-
     # the set of preinstalled packages - depends on vbuild or vtest
     if [ -n "$BUILD_MODE" ] ; then
        PREINSTALLED=devel.pkgs
@@ -974,7 +945,12 @@ function main () {
     # parse fixed arguments
     [[ -z "$@" ]] && usage
     lxc=$1 ; shift
-    lxc_root=$path/$lxc
+    lxc_root=/vservers/$lxc
+    # rainchecks
+    almost_empty $lxc_root || \
+       { echo "container $lxc already exists in $lxc_root - exiting" ; exit 1 ; }
+    virsh -c lxc:/// domuuid $lxc >& /dev/null && \
+       { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
     mkdir -p $lxc_root
 
     # check we've exhausted the arguments
@@ -1009,20 +985,15 @@ function main () {
         echo "Unknown personality: $personality"
     fi
 
-    if [ -n "$BUILD_MODE" ] ; then
+    # compute networking details for the test mode
+    # (build mode relies entirely on dhcp on the private subnet)
+    if [ -z "$BUILD_MODE" ] ; then
 
-       # Bridge IP affectation
-       byte=$(random_private_byte)
-       IP=${PRIVATE_PREFIX}$byte
-       NETMASK=$(masklen_to_netmask $PRIVATE_MASKLEN)
-       GATEWAY=$PRIVATE_GATEWAY
-       VIF_HOST="i$byte"
-    else
         [[ -z "GUEST_HOSTNAME" ]] && usage
        
        create_bridge_if_needed
 
-       IP=$(gethostbyname $GUEST_HOSTNAME)
+       GUEST_IP=$(gethostbyname $GUEST_HOSTNAME)
        # use same NETMASK as bridge interface br0
        MASKLEN=$(ip addr show $PUBLIC_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2)
         NETMASK=$(masklen_to_netmask $MASKLEN)
@@ -1030,14 +1001,6 @@ function main () {
         VIF_HOST="i$(echo $GUEST_HOSTNAME | cut -d. -f1)"
     fi
 
-    echo "the IP address of container $lxc is $IP, host virtual interface is $VIF_HOST"
-
-    # rainchecks
-    [ -d $lxc_root ] && \
-       { echo "container $lxc already exists in filesystem - exiting" ; exit 1 ; }
-    virsh -c lxc:/// domuuid $lxc >& /dev/null && \
-       { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
-
     setup_lxc $lxc $fcdistro $pldistro $personality 
 
     devel_or_vtest_tools $lxc $fcdistro $pldistro $personality