Subversion Repositories DevTools

Rev

Rev 3993 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/bin/bash
################################################################################
# This file is to be run by root
# It is used to configure a new VM on first boot
#
# Source is controlled in the VIXubuntu1204LTS_VMcfg package
#
# This script is called from
#   /etc/kde4/kdm/Xsetup
#
################################################################################

mkdir -p /root/Desktop
HelpFile="/root/Desktop/configureVM Help.txt"
function createHelpText {
cat > "$HelpFile" <<HelpDoc
Initial Machine Setup

This process will setup the VM.
It will be performed once, but can be manually rerun via $0
This text file can be found at: "$HelpFile"

Use Information:
Root Password: maple01

Machine Name:
This should be globally unique so as to avoid problems with IP address
assignment. The recommended name is based on:
  1) Your Desktop machine Name. ie AUPERAWS123
  2) The number of VMs that you already have created
ie: AUPERAWS123VM01

Configuration:
    Automount home drivers
    Intended for a Virtual Machine based in Perth attached to the Perth Unix
    home drives. Use outside of Perth may suffer performance delays

    Local Home
    Intended for a VM not based in Perth, but aware of the Perth VIX NIS. The
    machine will have a local home drive, but will use the Perth VIX NIS.

    A local home directory will be configured. The user will authenicate against
    the NIS, but use the local home drive.

    Stand Alone:
    Intended for a Virtual Machine that will not be attached to the Perth file
    system. This type of machine will require that the user set up a
    non-root user.

Package Server:
    This is the name of the build system package server. It will be used to:
        1) Provide the location of JATS
        2) Provide dpkg_archive
    Example: auperaarc01
    It is expected that dpkg_archive will be found at:
        /net/PackageServerName/export/devl/dpkg_archive

Additional Notes:
    * Kdbg has been installed. This is a GUI interface to GDB

HelpDoc
}

#
#   Examine user commandline.
#   Expect one of
#       -install    - Used by installer
#       -remove     - Used by installer
#       -auto       - Invoked by startup at boot
#                   - User invocation
#
startupMode=No
for ii in $*; do
    [ "$ii" = "-auto" ] && startupMode=
    [ "$ii" = "-install" ] && startupMode=install
    [ "$ii" = "-remove" ] && startupMode=remove
done

#
#   Self install and removal
#   Normally run from the installer
#
if [ "$startupMode" = "install" ] ; then
    ln -s $0 /root/Desktop/configureVM
    rm -f /root/.vix/config
    createHelpText
    exit 0
elif [ "$startupMode" = "remove" ] ; then
    rm -f /root/Desktop/configureVM*
    rm -rf /root/.vix
    exit 0
fi

#
#   Non Manual: Only run once
#
[ -z "$startupMode" -a -f /root/.vix/config ] && exit

#
#   Running full script
#   Perform full logging to another log file
mkdir -p /root/.vix
exec 1>/root/.vix/vix_full.log 2>&1
echo Starting $*
env
set -x

#
#   Create the Help Text
#   Place link to this script on the Desktop
#
[ -e /root/Desktop/configureVM ] || /root/Desktop/configureVM
createHelpText

#
# Extract information from the user
#
#
TITLE='First Time VM Configuration'

#
#   Display the Help Text
#
kdialog --title "$TITLE" --textbox "${HelpFile}" 800 800

#
#   Set defaults, before overiding with users last values
#
mname=$(cat /etc/hostname)
mode='Automount home drives'
pkgServer='auperaarc01'
luser=""
[ -f /root/.vix/config.data ] && source /root/.vix/config.data

ok=false
prompt="Unique machine name\
        <pre>Suggested format: AUPERAWSxxxVMnn\
        <br>Where:\
        <br>   AUPERA - Site prefix\
        <br>   WSxxx  - Your Workstation ID\
        <br>   VM     - Indicated a VM\
        <br>   nn     - VM Instance\
        </pre>Machine Name:"
while ! $ok ; do
    [ -z "$mname" ] && mname=AUPERAWSxxxVMnn
    mname=$(kdialog --title "$TITLE" --inputbox "$prompt" "$mname")
    [[ $mname =~ ^[a-zA-Z][a-zA-Z0-9-]+$ ]] && ok=true
    [[ $mname =~ AUPERAWSx ]] && ok=false
    [[ $mname =~ VMnn ]] && ok=false
done

ok=false
prompt="<pre>Stand Alone VM:        No  NIS, Setup local user\
         <br>Local Home:            Use NIS, Setup local home\
         <br>Automount home drives: Use NIS, Mount Unix Home Drive\
        </pre>Select configuration:"
while ! $ok ; do
    umode=$(kdialog --title "$TITLE" --combobox "$prompt" \
            'Stand Alone VM' \
            'Local Home' \
            'Automount home drives' \
            --default "$mode"  )
    [ -n "$umode" ] && ok=true
done
mode="$umode"

if [ "$mode" = "Local Home" ] ; then
    ok=false
    prompt="Local Home with NIS support has been selected\
            <br>Enter the name of a NIS user and a local home\
            directory will be created\
            <br>Cancel to skip this operation"
    while ! $ok ; do
        utext=$(kdialog --title "$TITLE" --inputbox "$prompt" "$luser")
        [[ -z "$utext" ]] && ok=true
        [[ -n "$utext" && "$utext" =~ ^[a-zA-Z][a-zA-Z0-9_]+$ ]] && ok=true
    done
    luser="$utext"
fi

ok=false
utext=""
prompt="The name of the build system package server\
        <br>Package Server:"
while ! $ok ; do
    [ -z "$utext" ] && utext="$pkgServer"
    utext=$(kdialog --title "$TITLE" --inputbox "$prompt" "$utext")
    [[ "$utext" =~ ^[a-zA-Z][a-zA-Z0-9_-]+$ ]] && ok=true
done
pkgServer="$utext"

erase=1
if [ -n "$startupMode" ]; then
    kdialog --title "$TITLE" --yesno \
        "This utility is being manually run.<br>\
        Do you want to force the script to be run on the next reboot"
    erase=$?
fi

#
#  Save config for next time
#
echo $mname
echo $mode
echo $pkgServer
mkdir -p /root/.vix
cat > /root/.vix/config.data <<hereData
mname="$mname"
mode="$mode"
pkgServer="$pkgServer"
luser="$luser"
hereData

################################################################################
#
#   Perform the initialisation
#
echo "--------------------------------"
echo "Data collected. Configure system"

# Extend the path
PATH="/sbin:$PATH"

# Machine-specific, so remove in case this system is going to be
# cloned.  These will be regenerated on the first boot.
if [ -z "$startupMode" ] ; then
    rm -f /etc/udev/rules.d/70-persistent-cd.rules
    rm -f /etc/udev/rules.d/70-persistent-net.rules

    # Potentially sensitive.
    rm -f /root/.ssh/known_hosts
    sed -i /etc/apt/apt.conf -e 's~http://.*@proxy~http://username:password@proxy~'
fi

#
#   Set new machine name
#
echo $mname > /etc/hostname
sed -i /etc/hosts -e "s~^127\.0\.1\.1.*~127.0.1.1\t$mname.vix.local $mname~"
hostname -b -F /etc/hostname

#
#   Set the required Mode
#
autoHome=false
perthNis=false
addUser=false

if [ "$mode" = "Stand Alone VM" ] ; then
    autoHome=false
    perthNis=false
    addUser=true

elif [ "$mode" = "Local Home" ] ; then
    autoHome=false
    perthNis=true

else
    autoHome=true
    perthNis=true
fi

#
#   Configure NIS
#
echo "Configure NIS"
if $perthNis; then
    echo "Enable yp binding"
    rm -f /etc/init/ypbind.override
    [ -f /etc/defaultdomain.saved ] && mv /etc/defaultdomain.saved /etc/defaultdomain
    start ypbind
    for ii in passwd shadow group ; do
        sed -i /etc/$ii -e "/^+/d"
        echo >> /etc/$ii '+'
    done
else
    echo "Disable yp binding"
    stop ypbind
    echo 'manual' > /etc/init/ypbind.override
    [ -f /etc/defaultdomain ] && mv /etc/defaultdomain /etc/defaultdomain.saved
    for ii in passwd shadow group; do
        sed -i /etc/$ii -e "/^+/d"
    done
fi

#
#   Configure auto mounting of Home Drives
#
if $autoHome; then
    echo "Setup automount of /home"
    sed -i /etc/auto.master -e 's~^#/home~/home~'
    reload autofs
else
    echo "Stop automount of /home"
    sed -i /etc/auto.master -e 's~^/home~#/home~'
    restart autofs
fi

#
#
#   Create local user if need be
#   Note: There is no need to create a local home directory for local users
#         as Ubuntu appears to do this on the fly.
#
echo "Configure Local User"
if $addUser; then
    kuser
fi
if [ -n "$luser" ] ; then
    userPath="/home/$luser"
    if [ ! -d "$userPath" ] ; then
        mkdir -p "$userPath"
        chmod 0777 "$userPath"
    fi
fi

#
#   Configure the package server
#
echo "Configure Package Server"
sed -i /etc/profile.d/jats.sh -e "s~^\(GBE_DPKG=/net\)/\([^/]*\)/\(.*\)~\1/$pkgServer/\3~"

#
#   Flag - configuration done
#   This should stop the auto script from running again
#
if [ -z "$startupMode" ]; then
    echo "Flag - don't run again"
    mkdir -p /root/.vix
    touch /root/.vix/config
fi
if [ "$erase" -eq "0" ] ; then
    echo "Erase config details. Force script to run on next reboot"
    rm -f /root/.vix/config
fi

#
#   Reboot the system
#   If manually invoked then give the user the option of aborting the boot
#
ktype=yesno
[ -z "$startupMode" ] && ktype=msgbox
kdialog --title "$TITLE" --$ktype "The system will now reboot in order for the changes to take effect"
if [ $? -ne 1 ]; then
    echo "Reboot the system"
    reboot
    echo "Reboot failed. Command returned"
fi
echo "Script complete"