#! /bin/bash -x
#
# ST - 2012-03-09
#
# Create the front end, up to basic system installation.
#
# Get the directory of the script.
SCRIPT_PATH=`dirname $0`
# Get the script name.
SCRIPT_NAME=`basename $0`
# Source the common configuration variables.
. $SCRIPT_PATH/c-i-a-b.common
#
# Check that the front-end virtual machine does not already exist.
#
CURRENT_HOST_NAME=$FRONT_END_HOST_NAME
IS_RUNNING=`xm list | grep $CURRENT_HOST_NAME`
if [ -n "$IS_RUNNING" ]
  then
    echo
    echo The \"$CURRENT_HOST_NAME\" domU already exists. Aborting!
    echo
    exit 1
fi

# Create the compute node model system disk.
lvcreate -L$COMPUTE_NODE_MODEL_SYSTEM_DISK_SIZE \
         -n  $COMPUTE_NODE_MODEL_SYSTEM_DISK \
         $COMPUTE_NODE_MODEL_SYSTEM_VOLUME_GROUP
# Create a File System on the compute node model system disk.
mkfs -t $COMPUTE_NODE_SYSTEM_DISK_FILE_SYSTEM \
        $COMPUTE_NODE_MODEL_SYSTEM_VOLUME_GROUP_DEVICE/$COMPUTE_NODE_MODEL_SYSTEM_DISK
# If necessary, create the swap volume for the compute node model.
if [ $COMPUTE_NODE_MODEL_SWAP_DISK_SIZE != $CONST_NULL_DISK_SIZE ] ; then
  lvcreate -L $COMPUTE_NODE_MODEL_SWAP_DISK_SIZE \
           -n $COMPUTE_NODE_MODEL_SWAP_DISK \
           $COMPUTE_NODE_MODEL_SWAP_VOLUME_GROUP
  mkswap $COMPUTE_NODE_MODEL_SWAP_VOLUME_GROUP_DEVICE/$COMPUTE_NODE_MODEL_SWAP_DISK
fi
# No home disk: they are mounted from the front-end
#
# Mount the system disk to install the system.
if [ ! -d $COMPUTE_NODE_MODEL_FILE_SYSTEM_MOUNT_POINT ]; then
  mkdir $COMPUTE_NODE_MODEL_FILE_SYSTEM_MOUNT_POINT
fi
mount $COMPUTE_NODE_MODEL_SYSTEM_VOLUME_GROUP_DEVICE/$COMPUTE_NODE_MODEL_SYSTEM_DISK \
      $COMPUTE_NODE_MODEL_FILE_SYSTEM_MOUNT_POINT
if [ $? -ne 0 ] ; then
  exit 1
fi 
#
# Install the system.
# 
debootstrap --arch $CONST_DEBIAN_ARCH $CONST_DEBIAN_VERSION \
    $COMPUTE_NODE_MODEL_FILE_SYSTEM_MOUNT_POINT
#
exit 0
